i3
key_press.c
Go to the documentation of this file.
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 
19 static bool parse_error_key;
20 static bool command_failed;
21 
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) {
45  return 1;
46 }
47 
48 static int json_end_map(void *ctx) {
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,
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 
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  "-f",
150  "-t",
151  "error",
152  "-m",
153  "The configured command for this shortcut could not be run successfully.",
154  "-b",
155  "show errors",
156  pageraction,
157  NULL
158  };
160  free(pageraction);
161  }
162  }
163 
164  yajl_free(handle);
165 
166  yajl_gen_free(command_output->json_gen);
167 }
#define DLOG(fmt,...)
Definition: log.h:28
Config config
Definition: config.c:19
xcb_timestamp_t last_timestamp
The last timestamp we got from X11 (timestamps are included in some events and are used for some thin...
Definition: main.c:52
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static struct CommandResult command_output
Binding * get_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode)
Returns a pointer to the Binding with the specified modifiers and keycode or NULL if no such binding ...
Definition: config.c:58
static int current_nesting_level
Definition: key_press.c:18
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:216
void handle_key_press(xcb_key_press_event_t *event)
There was a key press.
Definition: key_press.c:73
struct CommandResult * parse_command(const char *input)
static bool command_failed
Definition: key_press.c:20
char * command
Command, like in command mode.
Definition: data.h:253
static int json_start_map(void *ctx)
Definition: key_press.c:43
static int json_end_map(void *ctx)
Definition: key_press.c:48
static cmdp_state state
char * pattern
The pattern/name used to load the font.
Definition: libi3.h:51
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
static int json_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen)
Definition: key_press.c:36
i3Font font
Definition: config.h:93
static bool parse_error_key
Definition: key_press.c:19
pid_t command_error_nagbar_pid
Definition: key_press.c:22
yajl_gen json_gen
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
Definition: util.c:391
static int json_boolean(void *ctx, int boolval)
Definition: key_press.c:24
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:501
char * errorfilename
Definition: log.c:38
static yajl_callbacks command_error_callbacks
Definition: key_press.c:53
#define ELOG(fmt,...)
Definition: libi3.h:80
unsigned int xcb_numlock_mask
Definition: xcb.c:14
int xkb_current_group
Definition: main.c:36