From a223030b70c8e360f81b820244705e007e3ac1ec Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sun, 23 Dec 2018 10:54:54 -0500 Subject: Change mouse buttons to x11 map and libevdev names This modifies the way mouse bindings are parsed. Instead of adding to BTN_LEFT, which results in button numbers that may not be expected, buttons will be parsed in one of the following ways: 1. `button[1-9]` will now map to their x11 equivalents. This is already the case for bar bindings. This adds support for binding to axis events, which was not possible in the previous approach. 2. Anything that starts with `BTN_` will be parsed as an event code name using `libevdev_event_code_from_name`. This allows for any button to be mapped to instead of limiting usage to the ones near BTN_LEFT. This also adds a dependency on libevdev, but since libevdev is already a dependency of libinput, this should be fine. If needed, this option can have dependency guards added. Binding changes: - button1: BTN_LEFT -> BTN_LEFT - button2: BTN_RIGHT -> BTN_MIDDLE - button3: BTN_MIDDLE -> BTN_RIGHT - button4: BTN_SIDE -> SWAY_SCROLL_UP - button5: BTN_EXTRA -> SWAY_SCROLL_DOWN - button6: BTN_FORWARD -> SWAY_SCROLL_LEFT - button7: BTN_BACK -> SWAY_SCROLL_RIGHT - button8: BTN_TASK -> BTN_SIDE - button9: BTN_JOYSTICK -> BTN_EXTRA Since the axis events need to be mapped to an event code, this uses the following mappings to avoid any conflicts: - SWAY_SCROLL_UP: KEY_MAX + 1 - SWAY_SCROLL_DOWN: KEY_MAX + 2 - SWAY_SCROLL_LEFT: KEY_MAX + 3 - SWAY_SCROLL_RIGHT: KEY_MAX + 4 --- sway/commands/bind.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 01a35cf2..5990166a 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include #ifdef __linux__ #include #elif __FreeBSD__ @@ -10,6 +11,7 @@ #include #include "sway/commands.h" #include "sway/config.h" +#include "sway/input/cursor.h" #include "sway/ipc-server.h" #include "list.h" #include "log.h" @@ -102,9 +104,26 @@ static struct cmd_results *identify_key(const char* name, bool first_key, // Check for mouse binding uint32_t button = 0; - if (strncasecmp(name, "button", strlen("button")) == 0 && - strlen(name) == strlen("button0")) { - button = name[strlen("button")] - '1' + BTN_LEFT; + if (strncasecmp(name, "button", strlen("button")) == 0) { + // Map to x11 mouse buttons + button = name[strlen("button")] - '0'; + if (button < 1 || button > 9 || strlen(name) > strlen("button0")) { + return cmd_results_new(CMD_INVALID, "bindsym", + "Only buttons 1-9 are supported. For other mouse " + "buttons, use the name of the event code."); + } + uint32_t buttons[] = {BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, + SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT, + SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA}; + button = buttons[button - 1]; + } else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) { + // Get event code + int code = libevdev_event_code_from_name(EV_KEY, name); + if (code == -1) { + return cmd_results_new(CMD_INVALID, "bindsym", + "Invalid event code name %s", name); + } + button = code; } if (*type == BINDING_KEYSYM) { -- cgit v1.2.3