/* * Loads the XKB keymap from the X11 server and feeds it to xkbcommon. * Necessary so that we can properly let xkbcommon track the keyboard state and * translate keypresses to utf-8. * */ static bool load_keymap(void) { if (xkb_context == NULL) { if ((xkb_context = xkb_context_new(0)) == NULL) { fprintf(stderr, "[i3lock] could not create xkbcommon context\n"); return false; } } xkb_keymap_unref(xkb_keymap); int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn); DEBUG("device = %d\n", device_id); if ((xkb_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) { fprintf(stderr, "[i3lock] xkb_x11_keymap_new_from_device failed\n"); return false; } struct xkb_state *new_state = xkb_x11_state_new_from_device(xkb_keymap, conn, device_id); if (new_state == NULL) { fprintf(stderr, "[i3lock] xkb_x11_state_new_from_device failed\n"); return false; } xkb_state_unref(xkb_state); xkb_state = new_state; return true; }
/** * Process X11 events in the main-loop (gui-thread) of the application. */ static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer data ) { if ( ev == NULL ) { int status = xcb_connection_has_error ( xcb->connection ); fprintf ( stderr, "The XCB connection to X server had a fatal error: %d\n", status ); g_main_loop_quit ( main_loop ); return G_SOURCE_REMOVE; } uint8_t type = ev->response_type & ~0x80; if ( type == xkb.first_event ) { switch ( ev->pad0 ) { case XCB_XKB_MAP_NOTIFY: xkb_state_unref ( xkb.state ); xkb_keymap_unref ( xkb.keymap ); xkb.keymap = xkb_x11_keymap_new_from_device ( xkb.context, xcb->connection, xkb.device_id, 0 ); xkb.state = xkb_x11_state_new_from_device ( xkb.keymap, xcb->connection, xkb.device_id ); break; case XCB_XKB_STATE_NOTIFY: { xcb_xkb_state_notify_event_t *ksne = (xcb_xkb_state_notify_event_t *) ev; xkb_state_update_mask ( xkb.state, ksne->baseMods, ksne->latchedMods, ksne->lockedMods, ksne->baseGroup, ksne->latchedGroup, ksne->lockedGroup ); break; } } return G_SOURCE_CONTINUE; } RofiViewState *state = rofi_view_get_active (); if ( xcb->sndisplay != NULL ) { sn_xcb_display_process_event ( xcb->sndisplay, ev ); } if ( state != NULL ) { rofi_view_itterrate ( state, ev, &xkb ); if ( rofi_view_get_completed ( state ) ) { // This menu is done. rofi_view_finalize ( state ); // cleanup if ( rofi_view_get_active () == NULL ) { teardown ( pfd ); g_main_loop_quit ( main_loop ); } } } return G_SOURCE_CONTINUE; }
static bool grab_inputs(xcb_connection_t *conn, xcb_screen_t *screen) { const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000, }; int tries; for (tries = 1000; tries > 0; tries--) { if (grab_keyboard(conn, screen)) { break; } nanosleep(&req, NULL); } for (tries = 1000; tries > 0; tries--) { if (grab_pointer(conn, screen)) { break; } nanosleep(&req, NULL); } return (tries > 0); } typedef struct { xcb_connection_t *conn; struct xkb_context *ctx; struct xkb_keymap *keymap; struct xkb_state *state; struct xkb_compose_state *compose_state; struct xkb_compose_table *compose_table; uint8_t first_xkb_event; int32_t device_id; } Keyboard; static void update_keymap(Keyboard *keyboard) { keyboard->keymap = xkb_x11_keymap_new_from_device(keyboard->ctx, keyboard->conn, keyboard->device_id, XKB_KEYMAP_COMPILE_NO_FLAGS); if (!keyboard->keymap) { fprintf(stderr, "Failed to create keymap.\n"); exit(EXIT_FAILURE); } keyboard->state = xkb_x11_state_new_from_device(keyboard->keymap, keyboard->conn, keyboard->device_id); if (!keyboard->state) { fprintf(stderr, "Failed to create keyboard state.\n"); exit(EXIT_FAILURE); } }
int xrtb_keyboard_reload(struct xcb_rutabaga *xrtb) { struct xkb_keymap *new_keymap; int32_t device_id; assert(xrtb->xkb_ctx); device_id = xkb_x11_get_core_keyboard_device_id(xrtb->xcb_conn); if (device_id == -1) goto err_get_kbd_device; if (!(new_keymap = xkb_x11_keymap_new_from_device(xrtb->xkb_ctx, xrtb->xcb_conn, device_id, XKB_KEYMAP_COMPILE_NO_FLAGS))) goto err_new_keymap; if (xrtb->xkb_keymap) xkb_keymap_unref(xrtb->xkb_keymap); xrtb->xkb_keymap = new_keymap; if (xrtb->xkb_state) xkb_state_unref(xrtb->xkb_state); if (!(xrtb->xkb_state = xkb_x11_state_new_from_device(new_keymap, xrtb->xcb_conn, device_id))) goto err_new_state; #define CACHE_MOD_INDEX(name, constant) \ xrtb->mod_indices.name = xkb_keymap_mod_get_index(new_keymap, constant) CACHE_MOD_INDEX(shift, XKB_MOD_NAME_SHIFT); CACHE_MOD_INDEX(ctrl, XKB_MOD_NAME_CTRL); CACHE_MOD_INDEX(alt, XKB_MOD_NAME_ALT); CACHE_MOD_INDEX(super, XKB_MOD_NAME_LOGO); #undef CACHE_MOD_INDEX return 0; err_new_state: xkb_keymap_unref(xrtb->xkb_keymap); xrtb->xkb_keymap = NULL; err_new_keymap: err_get_kbd_device: return -1; }
int main ( int argc, char *argv[] ) { TIMINGS_START (); cmd_set_arguments ( argc, argv ); // Version if ( find_arg ( "-v" ) >= 0 || find_arg ( "-version" ) >= 0 ) { #ifdef GIT_VERSION fprintf ( stdout, "Version: "GIT_VERSION "\n" ); #else fprintf ( stdout, "Version: "VERSION "\n" ); #endif exit ( EXIT_SUCCESS ); } // Detect if we are in dmenu mode. // This has two possible causes. // 1 the user specifies it on the command-line. if ( find_arg ( "-dmenu" ) >= 0 ) { dmenu_mode = TRUE; } // 2 the binary that executed is called dmenu (e.g. symlink to rofi) else{ // Get the base name of the executable called. char *base_name = g_path_get_basename ( argv[0] ); const char * const dmenu_str = "dmenu"; dmenu_mode = ( strcmp ( base_name, dmenu_str ) == 0 ); // Free the basename for dmenu detection. g_free ( base_name ); } TICK (); // Get the path to the cache dir. cache_dir = g_get_user_cache_dir (); // Create pid file path. const char *path = g_get_user_runtime_dir (); if ( path ) { pidfile = g_build_filename ( path, "rofi.pid", NULL ); } config_parser_add_option ( xrm_String, "pid", (void * *) &pidfile, "Pidfile location" ); if ( find_arg ( "-config" ) < 0 ) { const char *cpath = g_get_user_config_dir (); if ( cpath ) { config_path = g_build_filename ( cpath, "rofi", "config", NULL ); } } else { char *c = NULL; find_arg_str ( "-config", &c ); config_path = rofi_expand_path ( c ); } TICK (); // Register cleanup function. atexit ( cleanup ); TICK (); // Get DISPLAY, first env, then argument. char *display_str = getenv ( "DISPLAY" ); find_arg_str ( "-display", &display_str ); if ( setlocale ( LC_ALL, "" ) == NULL ) { fprintf ( stderr, "Failed to set locale.\n" ); return EXIT_FAILURE; } xcb->connection = xcb_connect ( display_str, &xcb->screen_nbr ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Failed to open display: %s", display_str ); return EXIT_FAILURE; } TICK_N ( "Open Display" ); xcb->screen = xcb_aux_get_screen ( xcb->connection, xcb->screen_nbr ); xcb_intern_atom_cookie_t *ac = xcb_ewmh_init_atoms ( xcb->connection, &xcb->ewmh ); xcb_generic_error_t *errors = NULL; xcb_ewmh_init_atoms_replies ( &xcb->ewmh, ac, &errors ); if ( errors ) { fprintf ( stderr, "Failed to create EWMH atoms\n" ); free ( errors ); } if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xkb.first_event, NULL ) < 0 ) { fprintf ( stderr, "cannot setup XKB extension!\n" ); return EXIT_FAILURE; } xkb.context = xkb_context_new ( XKB_CONTEXT_NO_FLAGS ); if ( xkb.context == NULL ) { fprintf ( stderr, "cannot create XKB context!\n" ); return EXIT_FAILURE; } xkb.xcb_connection = xcb->connection; xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb->connection ); enum { required_events = ( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY ), required_nkn_details = ( XCB_XKB_NKN_DETAIL_KEYCODES ), required_map_parts = ( XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS | XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS | XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS | XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ), required_state_details = ( XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH | XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE | XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK ), }; static const xcb_xkb_select_events_details_t details = { .affectNewKeyboard = required_nkn_details, .newKeyboardDetails = required_nkn_details, .affectState = required_state_details, .stateDetails = required_state_details, }; xcb_xkb_select_events ( xcb->connection, xkb.device_id, required_events, /* affectWhich */ 0, /* clear */ required_events, /* selectAll */ required_map_parts, /* affectMap */ required_map_parts, /* map */ &details ); xkb.keymap = xkb_x11_keymap_new_from_device ( xkb.context, xcb->connection, xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS ); if ( xkb.keymap == NULL ) { fprintf ( stderr, "Failed to get Keymap for current keyboard device.\n" ); return EXIT_FAILURE; } xkb.state = xkb_x11_state_new_from_device ( xkb.keymap, xcb->connection, xkb.device_id ); if ( xkb.state == NULL ) { fprintf ( stderr, "Failed to get state object for current keyboard device.\n" ); return EXIT_FAILURE; } xkb.compose.table = xkb_compose_table_new_from_locale ( xkb.context, setlocale ( LC_CTYPE, NULL ), 0 ); if ( xkb.compose.table != NULL ) { xkb.compose.state = xkb_compose_state_new ( xkb.compose.table, 0 ); } else { fprintf ( stderr, "Failed to get keyboard compose table. Trying to limp on.\n" ); } if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } x11_setup ( &xkb ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } const xcb_query_extension_reply_t *er = xcb_get_extension_data ( xcb->connection, &xcb_xinerama_id ); if ( er ) { if ( er->present ) { xcb_xinerama_is_active_cookie_t is_active_req = xcb_xinerama_is_active ( xcb->connection ); xcb_xinerama_is_active_reply_t *is_active = xcb_xinerama_is_active_reply ( xcb->connection, is_active_req, NULL ); xcb->has_xinerama = is_active->state; free ( is_active ); } } main_loop = g_main_loop_new ( NULL, FALSE ); TICK_N ( "Setup mainloop" ); // startup not. xcb->sndisplay = sn_xcb_display_new ( xcb->connection, error_trap_push, error_trap_pop ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } if ( xcb->sndisplay != NULL ) { xcb->sncontext = sn_launchee_context_new_from_environment ( xcb->sndisplay, xcb->screen_nbr ); } if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } TICK_N ( "Startup Notification" ); // Initialize Xresources subsystem. config_parse_xresource_init (); TICK_N ( "Initialize Xresources system" ); // Setup keybinding setup_abe (); TICK_N ( "Setup abe" ); if ( find_arg ( "-no-config" ) < 0 ) { load_configuration ( ); } if ( !dmenu_mode ) { // setup_modi setup_modi (); } if ( find_arg ( "-no-config" ) < 0 ) { // Reload for dynamic part. load_configuration_dynamic ( ); } // Dump. // catch help request if ( find_arg ( "-h" ) >= 0 || find_arg ( "-help" ) >= 0 || find_arg ( "--help" ) >= 0 ) { help ( argc, argv ); exit ( EXIT_SUCCESS ); } if ( find_arg ( "-dump-xresources" ) >= 0 ) { config_parse_xresource_dump (); exit ( EXIT_SUCCESS ); } if ( find_arg ( "-dump-xresources-theme" ) >= 0 ) { config_parse_xresources_theme_dump (); exit ( EXIT_SUCCESS ); } main_loop_source = g_water_xcb_source_new_for_connection ( NULL, xcb->connection, main_loop_x11_event_handler, NULL, NULL ); TICK_N ( "X11 Setup " ); rofi_view_workers_initialize (); // Setup signal handling sources. // SIGINT g_unix_signal_add ( SIGINT, main_loop_signal_handler_int, NULL ); g_idle_add ( startup, NULL ); // Start mainloop. g_main_loop_run ( main_loop ); return return_code; }