]> git.sur5r.net Git - i3/i3/blob - src/key_press.c
Refactor binding accessor
[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     Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
74
75     /* if we couldn't find a binding, we are done */
76     if (bind == NULL)
77         return;
78
79     char *command_copy = sstrdup(bind->command);
80     struct CommandResult *command_output = parse_command(command_copy);
81     free(command_copy);
82
83     if (command_output->needs_tree_render)
84         tree_render();
85
86     /* We parse the JSON reply to figure out whether there was an error
87      * ("success" being false in on of the returned dictionaries). */
88     const unsigned char *reply;
89 #if YAJL_MAJOR >= 2
90     size_t length;
91     yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
92 #else
93     unsigned int length;
94     yajl_parser_config parse_conf = { 0, 0 };
95
96     yajl_handle handle = yajl_alloc(&command_error_callbacks, &parse_conf, NULL, NULL);
97 #endif
98     yajl_gen_get_buf(command_output->json_gen, &reply, &length);
99
100     current_nesting_level = 0;
101     parse_error_key = false;
102     command_failed = false;
103     yajl_status state = yajl_parse(handle, reply, length);
104     if (state != yajl_status_ok) {
105         ELOG("Could not parse my own reply. That's weird. reply is %.*s\n", (int)length, reply);
106     } else {
107         if (command_failed) {
108             char *pageraction;
109             sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
110             char *argv[] = {
111                 NULL, /* will be replaced by the executable path */
112                 "-f",
113                 config.font.pattern,
114                 "-t",
115                 "error",
116                 "-m",
117                 "The configured command for this shortcut could not be run successfully.",
118                 "-b",
119                 "show errors",
120                 pageraction,
121                 NULL
122             };
123             start_nagbar(&command_error_nagbar_pid, argv);
124             free(pageraction);
125         }
126     }
127
128     yajl_free(handle);
129
130     yajl_gen_free(command_output->json_gen);
131 }