Merge pull request #1697 from Airblader/feature-1695

Extend mouse commands on i3bar
This commit is contained in:
Michael Stapelberg
2015-06-18 20:50:56 +02:00
11 changed files with 310 additions and 55 deletions

View File

@ -20,6 +20,7 @@
#include "common.h"
static char *cur_key;
static bool parsing_bindings;
/*
* Parse a key.
@ -34,6 +35,14 @@ static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t
strncpy(cur_key, (const char *)keyVal, keyLen);
cur_key[keyLen] = '\0';
if (strcmp(cur_key, "bindings") == 0)
parsing_bindings = true;
return 1;
}
static int config_end_array_cb(void *params_) {
parsing_bindings = false;
return 1;
}
@ -63,6 +72,27 @@ static int config_string_cb(void *params_, const unsigned char *val, size_t _len
if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
return 1;
if (parsing_bindings) {
if (strcmp(cur_key, "command") == 0) {
binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
if (binding == NULL) {
ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
return 0;
}
if (binding->command != NULL) {
ELOG("The binding for input_code = %d already has a command. This is a bug in i3.\n", binding->input_code);
return 0;
}
sasprintf(&(binding->command), "%.*s", len, val);
return 1;
}
ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
return 0;
}
if (!strcmp(cur_key, "mode")) {
DLOG("mode = %.*s, len = %d\n", len, val, len);
config.hide_on_modifier = (len == 4 && !strncmp((const char *)val, "dock", strlen("dock")) ? M_DOCK
@ -112,17 +142,25 @@ static int config_string_cb(void *params_, const unsigned char *val, size_t _len
return 1;
}
/* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
* users updating from that version and restarting i3bar before i3. */
if (!strcmp(cur_key, "wheel_up_cmd")) {
DLOG("wheel_up_cmd = %.*s\n", len, val);
FREE(config.wheel_up_cmd);
sasprintf(&config.wheel_up_cmd, "%.*s", len, val);
binding_t *binding = scalloc(sizeof(binding_t));
binding->input_code = 4;
sasprintf(&(binding->command), "%.*s", len, val);
TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
return 1;
}
/* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
* users updating from that version and restarting i3bar before i3. */
if (!strcmp(cur_key, "wheel_down_cmd")) {
DLOG("wheel_down_cmd = %.*s\n", len, val);
FREE(config.wheel_down_cmd);
sasprintf(&config.wheel_down_cmd, "%.*s", len, val);
binding_t *binding = scalloc(sizeof(binding_t));
binding->input_code = 5;
sasprintf(&(binding->command), "%.*s", len, val);
TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
return 1;
}
@ -232,11 +270,34 @@ static int config_boolean_cb(void *params_, int val) {
return 0;
}
/*
* Parse an integer value
*
*/
static int config_integer_cb(void *params_, long long val) {
if (parsing_bindings) {
if (strcmp(cur_key, "input_code") == 0) {
binding_t *binding = scalloc(sizeof(binding_t));
binding->input_code = val;
TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
return 1;
}
ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
return 0;
}
return 0;
}
/* A datastructure to pass all these callbacks to yajl */
static yajl_callbacks outputs_callbacks = {
.yajl_null = config_null_cb,
.yajl_boolean = config_boolean_cb,
.yajl_integer = config_integer_cb,
.yajl_string = config_string_cb,
.yajl_end_array = config_end_array_cb,
.yajl_map_key = config_map_key_cb,
};
@ -249,6 +310,8 @@ void parse_config_json(char *json) {
yajl_status state;
handle = yajl_alloc(&outputs_callbacks, NULL, NULL);
TAILQ_INIT(&(config.bindings));
state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
/* FIXME: Proper error handling for JSON parsing */

View File

@ -468,20 +468,23 @@ void handle_button(xcb_button_press_event_t *event) {
return;
}
/* If a custom command was specified for this mouse button, it overrides
* the default behavior. */
binding_t *binding;
TAILQ_FOREACH(binding, &(config.bindings), bindings) {
if (binding->input_code != event->detail)
continue;
i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, binding->command);
return;
}
switch (event->detail) {
case 4:
/* Mouse wheel up. We select the previous ws, if any.
* If there is no more workspace, dont even send the workspace
* command, otherwise (with workspace auto_back_and_forth) wed end
* up on the wrong workspace. */
/* If `wheel_up_cmd [COMMAND]` was specified, it should override
* the default behavior */
if (config.wheel_up_cmd) {
i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, config.wheel_up_cmd);
return;
}
if (cur_ws == TAILQ_FIRST(walk->workspaces))
return;
@ -492,14 +495,6 @@ void handle_button(xcb_button_press_event_t *event) {
* If there is no more workspace, dont even send the workspace
* command, otherwise (with workspace auto_back_and_forth) wed end
* up on the wrong workspace. */
/* if `wheel_down_cmd [COMMAND]` was specified, it should override
* the default behavior */
if (config.wheel_down_cmd) {
i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, config.wheel_down_cmd);
return;
}
if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
return;