2012-08-12 06:18:43 -04:00
|
|
|
#undef I3__FILE__
|
|
|
|
#define I3__FILE__ "key_press.c"
|
2012-08-02 11:43:00 -04:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2013-01-26 03:55:38 -05:00
|
|
|
* © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
|
2012-08-02 11:43:00 -04:00
|
|
|
*
|
|
|
|
* key_press.c: key press handler
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "all.h"
|
|
|
|
|
|
|
|
static int current_nesting_level;
|
2012-09-11 07:17:36 -04:00
|
|
|
static bool parse_error_key;
|
2012-08-02 11:43:00 -04:00
|
|
|
static bool command_failed;
|
|
|
|
|
2012-12-24 10:53:20 -05:00
|
|
|
pid_t command_error_nagbar_pid = -1;
|
2012-08-02 11:43:00 -04:00
|
|
|
|
|
|
|
static int json_boolean(void *ctx, int boolval) {
|
2012-09-11 07:17:36 -04:00
|
|
|
DLOG("Got bool: %d, parse_error_key %d, nesting_level %d\n", boolval, parse_error_key, current_nesting_level);
|
2012-08-02 11:43:00 -04:00
|
|
|
|
2012-09-11 07:17:36 -04:00
|
|
|
if (parse_error_key && current_nesting_level == 1 && boolval)
|
2012-08-02 11:43:00 -04:00
|
|
|
command_failed = true;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
static int json_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
|
|
|
|
#else
|
|
|
|
static int json_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
|
|
|
|
#endif
|
2012-09-11 07:17:36 -04:00
|
|
|
parse_error_key = (stringlen >= strlen("parse_error") &&
|
|
|
|
strncmp((const char*)stringval, "parse_error", strlen("parse_error")) == 0);
|
2012-08-02 11:43:00 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_start_map(void *ctx) {
|
|
|
|
current_nesting_level++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_end_map(void *ctx) {
|
|
|
|
current_nesting_level--;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static yajl_callbacks command_error_callbacks = {
|
2014-01-05 07:05:31 -05:00
|
|
|
NULL,
|
|
|
|
&json_boolean,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&json_start_map,
|
|
|
|
&json_map_key,
|
|
|
|
&json_end_map,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2012-08-02 11:43:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-09-06 11:04:31 -04:00
|
|
|
* There was a KeyPress or KeyRelease (both events have the same fields). We
|
|
|
|
* compare this key code with our bindings table and pass the bound action to
|
|
|
|
* parse_command().
|
2012-08-02 11:43:00 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void handle_key_press(xcb_key_press_event_t *event) {
|
2012-09-06 11:04:31 -04:00
|
|
|
bool key_release = (event->response_type == XCB_KEY_RELEASE);
|
2012-08-02 11:43:00 -04:00
|
|
|
|
|
|
|
last_timestamp = event->time;
|
|
|
|
|
2012-09-06 11:04:31 -04:00
|
|
|
DLOG("%s %d, state raw = %d\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state);
|
2012-08-02 11:43:00 -04:00
|
|
|
|
|
|
|
/* Remove the numlock bit, all other bits are modifiers we can bind to */
|
|
|
|
uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
|
|
|
|
DLOG("(removed numlock, state = %d)\n", state_filtered);
|
|
|
|
/* Only use the lower 8 bits of the state (modifier masks) so that mouse
|
|
|
|
* button masks are filtered out */
|
|
|
|
state_filtered &= 0xFF;
|
|
|
|
DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
|
|
|
|
|
|
|
|
if (xkb_current_group == XkbGroup2Index)
|
|
|
|
state_filtered |= BIND_MODE_SWITCH;
|
|
|
|
|
|
|
|
DLOG("(checked mode_switch, state %d)\n", state_filtered);
|
|
|
|
|
|
|
|
/* Find the binding */
|
2012-09-06 11:04:31 -04:00
|
|
|
Binding *bind = get_binding(state_filtered, key_release, event->detail);
|
2012-08-02 11:43:00 -04:00
|
|
|
|
|
|
|
/* No match? Then the user has Mode_switch enabled but does not have a
|
|
|
|
* specific keybinding. Fall back to the default keybindings (without
|
|
|
|
* Mode_switch). Makes it much more convenient for users of a hybrid
|
|
|
|
* layout (like us, ru). */
|
|
|
|
if (bind == NULL) {
|
|
|
|
state_filtered &= ~(BIND_MODE_SWITCH);
|
|
|
|
DLOG("no match, new state_filtered = %d\n", state_filtered);
|
2012-09-06 11:04:31 -04:00
|
|
|
if ((bind = get_binding(state_filtered, key_release, event->detail)) == NULL) {
|
|
|
|
/* This is not a real error since we can have release and
|
|
|
|
* non-release keybindings. On a KeyPress event for which there is
|
|
|
|
* only a !release-binding, but no release-binding, the
|
|
|
|
* corresponding KeyRelease event will trigger this. No problem,
|
|
|
|
* though. */
|
|
|
|
DLOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
|
2012-08-02 11:43:00 -04:00
|
|
|
state_filtered, event->detail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *command_copy = sstrdup(bind->command);
|
|
|
|
struct CommandResult *command_output = parse_command(command_copy);
|
|
|
|
free(command_copy);
|
|
|
|
|
|
|
|
if (command_output->needs_tree_render)
|
|
|
|
tree_render();
|
|
|
|
|
|
|
|
/* We parse the JSON reply to figure out whether there was an error
|
|
|
|
* ("success" being false in on of the returned dictionaries). */
|
|
|
|
const unsigned char *reply;
|
|
|
|
#if YAJL_MAJOR >= 2
|
|
|
|
size_t length;
|
|
|
|
yajl_handle handle = yajl_alloc(&command_error_callbacks, NULL, NULL);
|
|
|
|
#else
|
|
|
|
unsigned int length;
|
|
|
|
yajl_parser_config parse_conf = { 0, 0 };
|
|
|
|
|
|
|
|
yajl_handle handle = yajl_alloc(&command_error_callbacks, &parse_conf, NULL, NULL);
|
|
|
|
#endif
|
|
|
|
yajl_gen_get_buf(command_output->json_gen, &reply, &length);
|
|
|
|
|
|
|
|
current_nesting_level = 0;
|
2012-09-11 07:17:36 -04:00
|
|
|
parse_error_key = false;
|
2012-08-02 11:43:00 -04:00
|
|
|
command_failed = false;
|
|
|
|
yajl_status state = yajl_parse(handle, reply, length);
|
|
|
|
if (state != yajl_status_ok) {
|
2012-08-05 10:34:38 -04:00
|
|
|
ELOG("Could not parse my own reply. That's weird. reply is %.*s\n", (int)length, reply);
|
2012-08-02 11:43:00 -04:00
|
|
|
} else {
|
2012-12-24 10:53:20 -05:00
|
|
|
if (command_failed) {
|
|
|
|
char *pageraction;
|
|
|
|
sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
|
|
|
|
char *argv[] = {
|
|
|
|
NULL, /* will be replaced by the executable path */
|
2013-01-26 03:55:38 -05:00
|
|
|
"-f",
|
|
|
|
config.font.pattern,
|
2012-12-24 10:53:20 -05:00
|
|
|
"-t",
|
|
|
|
"error",
|
|
|
|
"-m",
|
|
|
|
"The configured command for this shortcut could not be run successfully.",
|
|
|
|
"-b",
|
|
|
|
"show errors",
|
|
|
|
pageraction,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
start_nagbar(&command_error_nagbar_pid, argv);
|
|
|
|
free(pageraction);
|
|
|
|
}
|
2012-08-02 11:43:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
|
|
|
yajl_gen_free(command_output->json_gen);
|
|
|
|
}
|