From fdb4c99456c869110a76bde5495993f299117406 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Wed, 10 Feb 2016 19:40:19 +0100 Subject: [PATCH] Remove normal window global. --- Changelog | 3 ++ include/textbox.h | 18 +++++------ include/view-internal.h | 2 ++ include/view.h | 19 +++++++++-- source/dialogs/dmenu.c | 3 ++ source/rofi.c | 42 ++++++------------------ source/view.c | 72 ++++++++++++++++++++++++++++++++--------- source/x11-helper.c | 8 +---- 8 files changed, 100 insertions(+), 67 deletions(-) diff --git a/Changelog b/Changelog index d68172ec..430df32f 100644 --- a/Changelog +++ b/Changelog @@ -1,12 +1,15 @@ 1.0.0 (unreleased): New features: - Have separate config file. + - Use GlibMainLoop. + - Blinking cursor. Bug fixes: - Fix subpixel rendering. (#303) - Fix basic tests on OpenBSD (#272) - Fix wrong use of memcpy (thx to Jasperia). - Work around for sigwaitinfo on OpenBSD. - Ignore invalid (non-utf8) in dmenu mode. + - Glib signal handling. 0.15.12: New features: diff --git a/include/textbox.h b/include/textbox.h index 757b1c62..c321eb47 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -42,15 +42,15 @@ typedef struct typedef enum { - TB_AUTOHEIGHT = 1 << 0, - TB_AUTOWIDTH = 1 << 1, - TB_LEFT = 1 << 16, - TB_RIGHT = 1 << 17, - TB_CENTER = 1 << 18, - TB_EDITABLE = 1 << 19, - TB_MARKUP = 1 << 20, - TB_WRAP = 1 << 21, - TB_PASSWORD = 1 << 22, + TB_AUTOHEIGHT = 1 << 0, + TB_AUTOWIDTH = 1 << 1, + TB_LEFT = 1 << 16, + TB_RIGHT = 1 << 17, + TB_CENTER = 1 << 18, + TB_EDITABLE = 1 << 19, + TB_MARKUP = 1 << 20, + TB_WRAP = 1 << 21, + TB_PASSWORD = 1 << 22, } TextboxFlags; typedef enum diff --git a/include/view-internal.h b/include/view-internal.h index 9632e810..e6c23d55 100644 --- a/include/view-internal.h +++ b/include/view-internal.h @@ -65,6 +65,8 @@ typedef struct RofiViewState // Sidebar view ssize_t num_modi; textbox **modi; + + MenuFlags menu_flags; // Handlers. void ( *x11_event_loop )( struct RofiViewState *state, XEvent *ev ); void ( *finalize )( struct RofiViewState *state ); diff --git a/include/view.h b/include/view.h index 3151a438..82de0d0f 100644 --- a/include/view.h +++ b/include/view.h @@ -15,9 +15,11 @@ typedef struct RofiViewState RofiViewState; typedef enum { /** Create a menu for entering text */ - MENU_NORMAL = 0, + MENU_NORMAL = 0, /** Create a menu for entering passwords */ - MENU_PASSWORD = 1 + MENU_PASSWORD = 1, + /** Create amanaged window. */ + MENU_NORMAL_WINDOW = 2, } MenuFlags; /** @@ -146,4 +148,17 @@ void rofi_view_cleanup ( void ); void rofi_view_call_thread ( gpointer data, gpointer user_data ); /** @} */ +/*** + * @defgroup ViewThreadPool ViewThreadPool + * @ingroup View + * + * The view can (optionally) keep a set of worker threads around to parallize work. + * This includes filtering and sorting. + * + * @{ + */ +void rofi_view_workers_initialize ( void ); +void rofi_view_workers_finalize ( void ); + +/**@}*/ #endif diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index d4c343d7..6362c251 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -382,6 +382,9 @@ int dmenu_switcher_dialog ( void ) if ( find_arg ( "-password" ) >= 0 ) { menu_flags |= MENU_PASSWORD; } + if ( find_arg ( "-normal-window" ) >= 0 ) { + menu_flags |= MENU_NORMAL_WINDOW; + } /* copy filter string */ input = g_strdup ( config.filter ); diff --git a/source/rofi.c b/source/rofi.c index 7c6e61fd..f6f20478 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -63,21 +63,19 @@ gboolean daemon_mode = FALSE; // Pidfile. -char *pidfile = NULL; -const char *cache_dir = NULL; -SnDisplay *sndisplay = NULL; -SnLauncheeContext *sncontext = NULL; -Display *display = NULL; -char *display_str = NULL; -char *config_path = NULL; -unsigned int normal_window_mode = FALSE; +char *pidfile = NULL; +const char *cache_dir = NULL; +SnDisplay *sndisplay = NULL; +SnLauncheeContext *sncontext = NULL; +Display *display = NULL; +char *display_str = NULL; +char *config_path = NULL; // Array of modi. Mode **modi = NULL; unsigned int num_modi = 0; // Current selected switcher. unsigned int curr_switcher = 0; -GThreadPool *tpool = NULL; GMainLoop *main_loop = NULL; GSource *main_loop_source = NULL; gboolean quiet = FALSE; @@ -337,10 +335,7 @@ static int grab_global_keybindings () */ static void cleanup () { - if ( tpool ) { - g_thread_pool_free ( tpool, TRUE, FALSE ); - tpool = NULL; - } + rofi_view_workers_finalize (); if ( main_loop != NULL ) { if ( main_loop_source ) { g_source_destroy ( main_loop_source ); @@ -774,28 +769,9 @@ int main ( int argc, char *argv[] ) return show_error_message ( msg, markup ); } - // Create thread pool - GError *error = NULL; - tpool = g_thread_pool_new ( rofi_view_call_thread, NULL, config.threads, FALSE, &error ); - if ( error == NULL ) { - // Idle threads should stick around for a max of 60 seconds. - g_thread_pool_set_max_idle_time ( 60000 ); - // We are allowed to have - g_thread_pool_set_max_threads ( tpool, config.threads, &error ); - } - // If error occured during setup of pool, tell user and exit. - if ( error != NULL ) { - char *msg = g_strdup_printf ( "Failed to setup thread pool: '%s'", error->message ); - show_error_message ( msg, FALSE ); - g_free ( msg ); - g_error_free ( error ); - return EXIT_FAILURE; - } - - TICK_N ( "Setup Threadpool" ); + rofi_view_workers_initialize (); // Dmenu mode. if ( dmenu_mode == TRUE ) { - normal_window_mode = find_arg ( "-normal-window" ) >= 0; // force off sidebar mode: config.sidebar_mode = FALSE; int retv = run_dmenu (); diff --git a/source/view.c b/source/view.c index 4056a793..b01d7ab2 100644 --- a/source/view.c +++ b/source/view.c @@ -66,12 +66,16 @@ #include "view.h" #include "view-internal.h" +// TODO get rid of num_modi and modi, use an accessor. +extern unsigned int num_modi; +extern Mode **modi; + +// What todo with these. extern Display *display; -extern unsigned int num_modi; -extern Mode **modi; extern SnDisplay *sndisplay; extern SnLauncheeContext *sncontext; -extern GThreadPool *tpool; + +GThreadPool *tpool = NULL; RofiViewState *current_active_menu = NULL; Window main_window = None; @@ -82,7 +86,6 @@ XIM xim; XIC xic; Colormap map = None; XVisualInfo vinfo; -extern unsigned int normal_window_mode; static char * get_matching_state ( void ) { @@ -382,7 +385,7 @@ static void check_is_ascii ( thread_state *t, G_GNUC_UNUSED gpointer user_data ) g_mutex_unlock ( t->mutex ); } -static Window __create_window ( Display *display ) +static Window __create_window ( Display *display, MenuFlags menu_flags ) { XSetWindowAttributes attr; attr.colormap = map; @@ -425,7 +428,7 @@ static Window __create_window ( Display *display ) cairo_font_options_destroy ( fo ); // // make it an unmanaged window - if ( !normal_window_mode && !config.fullscreen ) { + if ( ( ( menu_flags & MENU_NORMAL_WINDOW ) == 0 ) && !config.fullscreen ) { window_set_atom_prop ( display, box, netatoms[_NET_WM_STATE], &netatoms[_NET_WM_STATE_ABOVE], 1 ); XSetWindowAttributes sattr = { .override_redirect = True }; XChangeWindowAttributes ( display, box, CWOverrideRedirect, &sattr ); @@ -1212,10 +1215,14 @@ static void rofi_view_mainloop_iter ( RofiViewState *state, XEvent *ev ) } } else if ( ev->type == FocusIn ) { - take_keyboard ( display, main_window ); + if ( ( state->menu_flags & MENU_NORMAL_WINDOW ) == 0 ) { + take_keyboard ( display, main_window ); + } } else if ( ev->type == FocusOut ) { - release_keyboard ( display ); + if ( ( state->menu_flags & MENU_NORMAL_WINDOW ) == 0 ) { + release_keyboard ( display ); + } } // Handle event. else if ( ev->type == Expose ) { @@ -1387,6 +1394,7 @@ RofiViewState *rofi_view_create ( Mode *sw, { TICK (); RofiViewState *state = __rofi_view_state_create (); + state->menu_flags = menu_flags; state->sw = sw; state->selected_line = UINT32_MAX; state->retv = MENU_CANCEL; @@ -1446,19 +1454,21 @@ RofiViewState *rofi_view_create ( Mode *sw, // Try to grab the keyboard as early as possible. // We grab this using the rootwindow (as dmenu does it). // this seems to result in the smallest delay for most people. - int has_keyboard = take_keyboard ( display, DefaultRootWindow ( display ) ); + if ( ( menu_flags & MENU_NORMAL_WINDOW ) == 0 ) { + int has_keyboard = take_keyboard ( display, DefaultRootWindow ( display ) ); - if ( !has_keyboard ) { - fprintf ( stderr, "Failed to grab keyboard, even after %d uS.", 500 * 1000 ); - // Break off. - rofi_view_free ( state ); - return NULL; + if ( !has_keyboard ) { + fprintf ( stderr, "Failed to grab keyboard, even after %d uS.", 500 * 1000 ); + // Break off. + rofi_view_free ( state ); + return NULL; + } } TICK_N ( "Grab keyboard" ); // main window isn't explicitly destroyed in case we switch modes. Reusing it prevents flicker XWindowAttributes attr; if ( main_window == None || XGetWindowAttributes ( display, main_window, &attr ) == 0 ) { - main_window = __create_window ( display ); + main_window = __create_window ( display, menu_flags ); if ( sncontext != NULL ) { sn_launchee_context_setup_window ( sncontext, main_window ); } @@ -1633,7 +1643,7 @@ void rofi_view_error_dialog ( const char *msg, int markup ) // main window isn't explicitly destroyed in case we switch modes. Reusing it prevents flicker XWindowAttributes attr; if ( main_window == None || XGetWindowAttributes ( display, main_window, &attr ) == 0 ) { - main_window = __create_window ( display ); + main_window = __create_window ( display, MENU_NORMAL ); } rofi_view_calculate_window_and_element_width ( state, &( state->mon ) ); @@ -1692,3 +1702,33 @@ void rofi_view_cleanup () map = None; } } +void rofi_view_workers_initialize ( void ) +{ + TICK_N ( "Setup Threadpool, start" ); + // Create thread pool + GError *error = NULL; + tpool = g_thread_pool_new ( rofi_view_call_thread, NULL, config.threads, FALSE, &error ); + if ( error == NULL ) { + // Idle threads should stick around for a max of 60 seconds. + g_thread_pool_set_max_idle_time ( 60000 ); + // We are allowed to have + g_thread_pool_set_max_threads ( tpool, config.threads, &error ); + } + // If error occured during setup of pool, tell user and exit. + if ( error != NULL ) { + char *msg = g_strdup_printf ( "Failed to setup thread pool: '%s'", error->message ); + show_error_message ( msg, FALSE ); + g_free ( msg ); + g_error_free ( error ); + exit ( EXIT_FAILURE ); + } + TICK_N ( "Setup Threadpool, done" ); +} +void rofi_view_workers_finalize ( void ) +{ + if ( tpool ) { + g_thread_pool_free ( tpool, TRUE, FALSE ); + tpool = NULL; + } +} + diff --git a/source/x11-helper.c b/source/x11-helper.c index b21e6ff3..89c89a7b 100644 --- a/source/x11-helper.c +++ b/source/x11-helper.c @@ -328,12 +328,8 @@ int window_send_message ( Display *display, Window trg, Window subject, Atom ato return r; } -extern unsigned int normal_window_mode; int take_keyboard ( Display *display, Window w ) { - if ( normal_window_mode ) { - return 1; - } for ( int i = 0; i < 500; i++ ) { if ( XGrabKeyboard ( display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime ) == GrabSuccess ) { @@ -347,9 +343,7 @@ int take_keyboard ( Display *display, Window w ) void release_keyboard ( Display *display ) { - if ( !normal_window_mode ) { - XUngrabKeyboard ( display, CurrentTime ); - } + XUngrabKeyboard ( display, CurrentTime ); } // bind a key combination on a root window, compensating for Lock* states void x11_grab_key ( Display *display, unsigned int modmask, KeySym key )