2 #define I3__FILE__ "key_press.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
9 * key_press.c: key press handler
12 #include <sys/types.h>
18 static int current_nesting_level;
19 static bool parse_error_key;
20 static bool command_failed;
22 pid_t command_error_nagbar_pid = -1;
24 static int json_boolean(void *ctx, int boolval) {
25 DLOG("Got bool: %d, parse_error_key %d, nesting_level %d\n", boolval, parse_error_key, current_nesting_level);
27 if (parse_error_key && current_nesting_level == 1 && boolval)
28 command_failed = true;
34 static int json_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
36 static int json_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
38 parse_error_key = (stringlen >= strlen("parse_error") &&
39 strncmp((const char*)stringval, "parse_error", strlen("parse_error")) == 0);
43 static int json_start_map(void *ctx) {
44 current_nesting_level++;
48 static int json_end_map(void *ctx) {
49 current_nesting_level--;
53 static yajl_callbacks command_error_callbacks = {
54 .yajl_boolean = json_boolean,
55 .yajl_start_map = json_start_map,
56 .yajl_map_key = json_map_key,
57 .yajl_end_map = json_end_map,
61 * There was a KeyPress or KeyRelease (both events have the same fields). We
62 * compare this key code with our bindings table and pass the bound action to
66 void handle_key_press(xcb_key_press_event_t *event) {
67 bool key_release = (event->response_type == XCB_KEY_RELEASE);
69 last_timestamp = event->time;
71 DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
73 /* Remove the numlock bit, all other bits are modifiers we can bind to */
74 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
75 DLOG("(removed numlock, state = %d)\n", state_filtered);
76 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
77 * button masks are filtered out */
78 state_filtered &= 0xFF;
79 DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
81 if (xkb_current_group == XkbGroup2Index)
82 state_filtered |= BIND_MODE_SWITCH;
84 DLOG("(checked mode_switch, state %d)\n", state_filtered);
86 /* Find the binding */
87 Binding *bind = get_keyboard_binding(state_filtered, key_release, event->detail);
89 /* No match? Then the user has Mode_switch enabled but does not have a
90 * specific keybinding. Fall back to the default keybindings (without
91 * Mode_switch). Makes it much more convenient for users of a hybrid
92 * layout (like us, ru). */
94 state_filtered &= ~(BIND_MODE_SWITCH);
95 DLOG("no match, new state_filtered = %d\n", state_filtered);
96 if ((bind = get_keyboard_binding(state_filtered, key_release, event->detail)) == NULL) {
97 /* This is not a real error since we can have release and
98 * non-release keybindings. On a KeyPress event for which there is
99 * only a !release-binding, but no release-binding, the
100 * corresponding KeyRelease event will trigger this. No problem,
102 DLOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
103 state_filtered, event->detail);
108 char *command_copy = sstrdup(bind->command);
109 struct CommandResult *command_output = parse_command(command_copy);
112 if (command_output->needs_tree_render)
115 /* We parse the JSON reply to figure out whether there was an error
116 * ("success" being false in on of the returned dictionaries). */
117 const unsigned char *reply;
120 yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
123 yajl_parser_config parse_conf = { 0, 0 };
125 yajl_handle handle = yajl_alloc(&command_error_callbacks, &parse_conf, NULL, NULL);
127 yajl_gen_get_buf(command_output->json_gen, &reply, &length);
129 current_nesting_level = 0;
130 parse_error_key = false;
131 command_failed = false;
132 yajl_status state = yajl_parse(handle, reply, length);
133 if (state != yajl_status_ok) {
134 ELOG("Could not parse my own reply. That's weird. reply is %.*s\n", (int)length, reply);
136 if (command_failed) {
138 sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
140 NULL, /* will be replaced by the executable path */
146 "The configured command for this shortcut could not be run successfully.",
152 start_nagbar(&command_error_nagbar_pid, argv);
159 yajl_gen_free(command_output->json_gen);