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;
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);
39 static int json_start_map(void *ctx) {
40 current_nesting_level++;
44 static int json_end_map(void *ctx) {
45 current_nesting_level--;
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,
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
62 void handle_key_press(xcb_key_press_event_t *event) {
63 bool key_release = (event->response_type == XCB_KEY_RELEASE);
65 last_timestamp = event->time;
67 DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
69 Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event);
71 /* if we couldn't find a binding, we are done */
75 yajl_gen gen = yajl_gen_alloc(NULL);
77 char *command_copy = sstrdup(bind->command);
78 CommandResult *result = parse_command(command_copy, gen);
81 if (result->needs_tree_render)
84 command_result_free(result);
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;
90 yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
91 yajl_gen_get_buf(gen, &reply, &length);
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);
100 if (command_failed) {
102 sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
104 NULL, /* will be replaced by the executable path */
110 "The configured command for this shortcut could not be run successfully.",
116 start_nagbar(&command_error_nagbar_pid, argv);