]> git.sur5r.net Git - i3/i3/blob - src/key_press.c
Refactor parse_command
[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 static int json_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
34     parse_error_key = (stringlen >= strlen("parse_error") &&
35                        strncmp((const char*)stringval, "parse_error", strlen("parse_error")) == 0);
36     return 1;
37 }
38
39 static int json_start_map(void *ctx) {
40     current_nesting_level++;
41     return 1;
42 }
43
44 static int json_end_map(void *ctx) {
45     current_nesting_level--;
46     return 1;
47 }
48
49 static yajl_callbacks command_error_callbacks = {
50     .yajl_boolean = json_boolean,
51     .yajl_start_map = json_start_map,
52     .yajl_map_key = json_map_key,
53     .yajl_end_map = json_end_map,
54 };
55
56 /*
57  * There was a KeyPress or KeyRelease (both events have the same fields). We
58  * compare this key code with our bindings table and pass the bound action to
59  * parse_command().
60  *
61  */
62 void handle_key_press(xcb_key_press_event_t *event) {
63     bool key_release = (event->response_type == XCB_KEY_RELEASE);
64
65     last_timestamp = event->time;
66
67     DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
68
69     Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
70
71     /* if we couldn't find a binding, we are done */
72     if (bind == NULL)
73         return;
74
75     yajl_gen gen = yajl_gen_alloc(NULL);
76
77     char *command_copy = sstrdup(bind->command);
78     CommandResult *result = parse_command(command_copy, gen);
79     free(command_copy);
80
81     if (result->needs_tree_render)
82         tree_render();
83
84     command_result_free(result);
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     size_t length;
90     yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
91     yajl_gen_get_buf(gen, &reply, &length);
92
93     current_nesting_level = 0;
94     parse_error_key = false;
95     command_failed = false;
96     yajl_status state = yajl_parse(handle, reply, length);
97     if (state != yajl_status_ok) {
98         ELOG("Could not parse my own reply. That's weird. reply is %.*s\n", (int)length, reply);
99     } else {
100         if (command_failed) {
101             char *pageraction;
102             sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
103             char *argv[] = {
104                 NULL, /* will be replaced by the executable path */
105                 "-f",
106                 config.font.pattern,
107                 "-t",
108                 "error",
109                 "-m",
110                 "The configured command for this shortcut could not be run successfully.",
111                 "-b",
112                 "show errors",
113                 pageraction,
114                 NULL
115             };
116             start_nagbar(&command_error_nagbar_pid, argv);
117             free(pageraction);
118         }
119     }
120
121     yajl_free(handle);
122
123     yajl_gen_free(gen);
124 }