]> git.sur5r.net Git - i3/i3/blob - src/key_press.c
refactor both i3-nagbar starts into src/util.c
[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-2012 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     NULL,
55     &json_boolean,
56     NULL,
57     NULL,
58     NULL,
59     NULL,
60     &json_start_map,
61     &json_map_key,
62     &json_end_map,
63     NULL,
64     NULL
65 };
66
67 /*
68  * There was a KeyPress or KeyRelease (both events have the same fields). We
69  * compare this key code with our bindings table and pass the bound action to
70  * parse_command().
71  *
72  */
73 void handle_key_press(xcb_key_press_event_t *event) {
74     bool key_release = (event->response_type == XCB_KEY_RELEASE);
75
76     last_timestamp = event->time;
77
78     DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
79
80     /* Remove the numlock bit, all other bits are modifiers we can bind to */
81     uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
82     DLOG("(removed numlock, state = %d)\n", state_filtered);
83     /* Only use the lower 8 bits of the state (modifier masks) so that mouse
84      * button masks are filtered out */
85     state_filtered &= 0xFF;
86     DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
87
88     if (xkb_current_group == XkbGroup2Index)
89         state_filtered |= BIND_MODE_SWITCH;
90
91     DLOG("(checked mode_switch, state %d)\n", state_filtered);
92
93     /* Find the binding */
94     Binding *bind = get_binding(state_filtered, key_release, event->detail);
95
96     /* No match? Then the user has Mode_switch enabled but does not have a
97      * specific keybinding. Fall back to the default keybindings (without
98      * Mode_switch). Makes it much more convenient for users of a hybrid
99      * layout (like us, ru). */
100     if (bind == NULL) {
101         state_filtered &= ~(BIND_MODE_SWITCH);
102         DLOG("no match, new state_filtered = %d\n", state_filtered);
103         if ((bind = get_binding(state_filtered, key_release, event->detail)) == NULL) {
104             /* This is not a real error since we can have release and
105              * non-release keybindings. On a KeyPress event for which there is
106              * only a !release-binding, but no release-binding, the
107              * corresponding KeyRelease event will trigger this. No problem,
108              * though. */
109             DLOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
110                  state_filtered, event->detail);
111             return;
112         }
113     }
114
115     char *command_copy = sstrdup(bind->command);
116     struct CommandResult *command_output = parse_command(command_copy);
117     free(command_copy);
118
119     if (command_output->needs_tree_render)
120         tree_render();
121
122     /* We parse the JSON reply to figure out whether there was an error
123      * ("success" being false in on of the returned dictionaries). */
124     const unsigned char *reply;
125 #if YAJL_MAJOR >= 2
126     size_t length;
127     yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
128 #else
129     unsigned int length;
130     yajl_parser_config parse_conf = { 0, 0 };
131
132     yajl_handle handle = yajl_alloc(&command_error_callbacks, &parse_conf, NULL, NULL);
133 #endif
134     yajl_gen_get_buf(command_output->json_gen, &reply, &length);
135
136     current_nesting_level = 0;
137     parse_error_key = false;
138     command_failed = false;
139     yajl_status state = yajl_parse(handle, reply, length);
140     if (state != yajl_status_ok) {
141         ELOG("Could not parse my own reply. That's weird. reply is %.*s\n", (int)length, reply);
142     } else {
143         if (command_failed) {
144             char *pageraction;
145             sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
146             char *argv[] = {
147                 NULL, /* will be replaced by the executable path */
148                 "-t",
149                 "error",
150                 "-m",
151                 "The configured command for this shortcut could not be run successfully.",
152                 "-b",
153                 "show errors",
154                 pageraction,
155                 NULL
156             };
157             start_nagbar(&command_error_nagbar_pid, argv);
158             free(pageraction);
159         }
160     }
161
162     yajl_free(handle);
163
164     yajl_gen_free(command_output->json_gen);
165 }