]> git.sur5r.net Git - i3/i3/blob - src/key_press.c
Move keyboard binding accessor to bindings.[ch]
[i3/i3] / src / key_press.c
1 #undef I3__FILE__
2 #define I3__FILE__ "key_press.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * key_press.c: key press handler
10  *
11  */
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/wait.h>
15 #include <fcntl.h>
16 #include "all.h"
17
18 static int current_nesting_level;
19 static bool parse_error_key;
20 static bool command_failed;
21
22 pid_t command_error_nagbar_pid = -1;
23
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);
26
27     if (parse_error_key && current_nesting_level == 1 && boolval)
28         command_failed = true;
29
30     return 1;
31 }
32
33 #if YAJL_MAJOR >= 2
34 static int json_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
35 #else
36 static int json_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
37 #endif
38     parse_error_key = (stringlen >= strlen("parse_error") &&
39                        strncmp((const char*)stringval, "parse_error", strlen("parse_error")) == 0);
40     return 1;
41 }
42
43 static int json_start_map(void *ctx) {
44     current_nesting_level++;
45     return 1;
46 }
47
48 static int json_end_map(void *ctx) {
49     current_nesting_level--;
50     return 1;
51 }
52
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,
58 };
59
60 /*
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
63  * parse_command().
64  *
65  */
66 void handle_key_press(xcb_key_press_event_t *event) {
67     bool key_release = (event->response_type == XCB_KEY_RELEASE);
68
69     last_timestamp = event->time;
70
71     DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
72
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);
80
81     if (xkb_current_group == XkbGroup2Index)
82         state_filtered |= BIND_MODE_SWITCH;
83
84     DLOG("(checked mode_switch, state %d)\n", state_filtered);
85
86     /* Find the binding */
87     Binding *bind = get_keyboard_binding(state_filtered, key_release, event->detail);
88
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). */
93     if (bind == NULL) {
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,
101              * though. */
102             DLOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
103                  state_filtered, event->detail);
104             return;
105         }
106     }
107
108     char *command_copy = sstrdup(bind->command);
109     struct CommandResult *command_output = parse_command(command_copy);
110     free(command_copy);
111
112     if (command_output->needs_tree_render)
113         tree_render();
114
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;
118 #if YAJL_MAJOR >= 2
119     size_t length;
120     yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
121 #else
122     unsigned int length;
123     yajl_parser_config parse_conf = { 0, 0 };
124
125     yajl_handle handle = yajl_alloc(&command_error_callbacks, &parse_conf, NULL, NULL);
126 #endif
127     yajl_gen_get_buf(command_output->json_gen, &reply, &length);
128
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);
135     } else {
136         if (command_failed) {
137             char *pageraction;
138             sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
139             char *argv[] = {
140                 NULL, /* will be replaced by the executable path */
141                 "-f",
142                 config.font.pattern,
143                 "-t",
144                 "error",
145                 "-m",
146                 "The configured command for this shortcut could not be run successfully.",
147                 "-b",
148                 "show errors",
149                 pageraction,
150                 NULL
151             };
152             start_nagbar(&command_error_nagbar_pid, argv);
153             free(pageraction);
154         }
155     }
156
157     yajl_free(handle);
158
159     yajl_gen_free(command_output->json_gen);
160 }