diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-01-13 20:41:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-13 20:41:05 -0500 |
commit | 81bb6752748436788418c2fa3e7bef775c42c262 (patch) | |
tree | f90b770d160cf5ca773a3d63d52311f7ba731b16 /sway | |
parent | Merge pull request #3343 from RedSoxFan/seat-cursor-buttons-improved (diff) | |
parent | bar_cmd_bind: utilize mouse button helpers (diff) | |
download | sway-81bb6752748436788418c2fa3e7bef775c42c262.tar.gz sway-81bb6752748436788418c2fa3e7bef775c42c262.tar.zst sway-81bb6752748436788418c2fa3e7bef775c42c262.zip |
Merge pull request #3344 from RedSoxFan/bar-mouse-bindings-improved
Improve mouse button parsing: bar mouse bindings
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands/bar.c | 1 | ||||
-rw-r--r-- | sway/commands/bar/bind.c | 106 | ||||
-rw-r--r-- | sway/commands/bar/bindsym.c | 68 | ||||
-rw-r--r-- | sway/ipc-json.c | 29 | ||||
-rw-r--r-- | sway/meson.build | 2 | ||||
-rw-r--r-- | sway/sway-bar.5.scd | 16 |
6 files changed, 149 insertions, 73 deletions
diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 3e7c1b0f..2a82d508 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c | |||
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | // Must be in alphabetical order for bsearch | 9 | // Must be in alphabetical order for bsearch |
10 | static struct cmd_handler bar_handlers[] = { | 10 | static struct cmd_handler bar_handlers[] = { |
11 | { "bindcode", bar_cmd_bindcode }, | ||
11 | { "binding_mode_indicator", bar_cmd_binding_mode_indicator }, | 12 | { "binding_mode_indicator", bar_cmd_binding_mode_indicator }, |
12 | { "bindsym", bar_cmd_bindsym }, | 13 | { "bindsym", bar_cmd_bindsym }, |
13 | { "colors", bar_cmd_colors }, | 14 | { "colors", bar_cmd_colors }, |
diff --git a/sway/commands/bar/bind.c b/sway/commands/bar/bind.c new file mode 100644 index 00000000..a4c65ec4 --- /dev/null +++ b/sway/commands/bar/bind.c | |||
@@ -0,0 +1,106 @@ | |||
1 | #include <libevdev/libevdev.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <strings.h> | ||
5 | #include "sway/commands.h" | ||
6 | #include "sway/config.h" | ||
7 | #include "sway/input/cursor.h" | ||
8 | #include "list.h" | ||
9 | #include "log.h" | ||
10 | #include "stringop.h" | ||
11 | |||
12 | static struct cmd_results *bar_cmd_bind(int argc, char **argv, bool code) { | ||
13 | const char *command = code ? "bar bindcode" : "bar bindsym"; | ||
14 | struct cmd_results *error = NULL; | ||
15 | if ((error = checkarg(argc, command, EXPECTED_AT_LEAST, 2))) { | ||
16 | return error; | ||
17 | } | ||
18 | if (!config->current_bar) { | ||
19 | return cmd_results_new(CMD_FAILURE, command, "No bar defined."); | ||
20 | } | ||
21 | |||
22 | struct bar_binding *binding = calloc(1, sizeof(struct bar_binding)); | ||
23 | if (!binding) { | ||
24 | return cmd_results_new(CMD_FAILURE, command, | ||
25 | "Unable to allocate bar binding"); | ||
26 | } | ||
27 | |||
28 | binding->release = false; | ||
29 | if (strcmp("--release", argv[0]) == 0) { | ||
30 | binding->release = true; | ||
31 | argv++; | ||
32 | argc--; | ||
33 | } | ||
34 | |||
35 | char *message = NULL; | ||
36 | if (code) { | ||
37 | binding->button = get_mouse_bindcode(argv[0], &message); | ||
38 | } else { | ||
39 | binding->button = get_mouse_bindsym(argv[0], &message); | ||
40 | } | ||
41 | if (message) { | ||
42 | free_bar_binding(binding); | ||
43 | error = cmd_results_new(CMD_INVALID, command, message); | ||
44 | free(message); | ||
45 | return error; | ||
46 | } else if (!binding->button) { | ||
47 | free_bar_binding(binding); | ||
48 | return cmd_results_new(CMD_INVALID, command, | ||
49 | "Unknown button %s", argv[0]); | ||
50 | } | ||
51 | |||
52 | const char *name = libevdev_event_code_get_name(EV_KEY, binding->button); | ||
53 | if (!name) { | ||
54 | switch (binding->button) { | ||
55 | case SWAY_SCROLL_UP: | ||
56 | name = "SWAY_SCROLL_UP"; | ||
57 | break; | ||
58 | case SWAY_SCROLL_DOWN: | ||
59 | name = "SWAY_SCROLL_DOWN"; | ||
60 | break; | ||
61 | case SWAY_SCROLL_LEFT: | ||
62 | name = "SWAY_SCROLL_LEFT"; | ||
63 | break; | ||
64 | case SWAY_SCROLL_RIGHT: | ||
65 | name = "SWAY_SCROLL_RIGHT"; | ||
66 | break; | ||
67 | default: | ||
68 | // Unreachable | ||
69 | break; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | binding->command = join_args(argv + 1, argc - 1); | ||
74 | |||
75 | list_t *bindings = config->current_bar->bindings; | ||
76 | bool overwritten = false; | ||
77 | for (int i = 0; i < bindings->length; i++) { | ||
78 | struct bar_binding *other = bindings->items[i]; | ||
79 | if (other->button == binding->button && | ||
80 | other->release == binding->release) { | ||
81 | overwritten = true; | ||
82 | bindings->items[i] = binding; | ||
83 | free_bar_binding(other); | ||
84 | wlr_log(WLR_DEBUG, "[bar %s] Updated binding for %u (%s)%s", | ||
85 | config->current_bar->id, binding->button, name, | ||
86 | binding->release ? " - release" : ""); | ||
87 | break; | ||
88 | } | ||
89 | } | ||
90 | if (!overwritten) { | ||
91 | list_add(bindings, binding); | ||
92 | wlr_log(WLR_DEBUG, "[bar %s] Added binding for %u (%s)%s", | ||
93 | config->current_bar->id, binding->button, name, | ||
94 | binding->release ? " - release" : ""); | ||
95 | } | ||
96 | |||
97 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
98 | } | ||
99 | |||
100 | struct cmd_results *bar_cmd_bindcode(int argc, char **argv) { | ||
101 | return bar_cmd_bind(argc, argv, true); | ||
102 | } | ||
103 | |||
104 | struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { | ||
105 | return bar_cmd_bind(argc, argv, false); | ||
106 | } | ||
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c deleted file mode 100644 index e6d6220e..00000000 --- a/sway/commands/bar/bindsym.c +++ /dev/null | |||
@@ -1,68 +0,0 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | #include <strings.h> | ||
4 | #include "sway/commands.h" | ||
5 | #include "sway/config.h" | ||
6 | #include "list.h" | ||
7 | #include "log.h" | ||
8 | #include "stringop.h" | ||
9 | |||
10 | struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { | ||
11 | struct cmd_results *error = NULL; | ||
12 | if ((error = checkarg(argc, "bar bindsym", EXPECTED_AT_LEAST, 2))) { | ||
13 | return error; | ||
14 | } | ||
15 | if (!config->current_bar) { | ||
16 | return cmd_results_new(CMD_FAILURE, "bar bindsym", "No bar defined."); | ||
17 | } | ||
18 | |||
19 | struct bar_binding *binding = calloc(1, sizeof(struct bar_binding)); | ||
20 | if (!binding) { | ||
21 | return cmd_results_new(CMD_FAILURE, "bar bindsym", | ||
22 | "Unable to allocate bar binding"); | ||
23 | } | ||
24 | |||
25 | binding->release = false; | ||
26 | if (strcmp("--release", argv[0]) == 0) { | ||
27 | binding->release = true; | ||
28 | argv++; | ||
29 | argc--; | ||
30 | } | ||
31 | |||
32 | binding->button = 0; | ||
33 | if (strncasecmp(argv[0], "button", strlen("button")) == 0 && | ||
34 | strlen(argv[0]) == strlen("button0")) { | ||
35 | binding->button = argv[0][strlen("button")] - '0'; | ||
36 | } | ||
37 | if (binding->button < 1 || binding->button > 9) { | ||
38 | free_bar_binding(binding); | ||
39 | return cmd_results_new(CMD_FAILURE, "bar bindsym", | ||
40 | "Only button<1-9> is supported"); | ||
41 | } | ||
42 | |||
43 | binding->command = join_args(argv + 1, argc - 1); | ||
44 | |||
45 | list_t *bindings = config->current_bar->bindings; | ||
46 | bool overwritten = false; | ||
47 | for (int i = 0; i < bindings->length; i++) { | ||
48 | struct bar_binding *other = bindings->items[i]; | ||
49 | if (other->button == binding->button && | ||
50 | other->release == binding->release) { | ||
51 | overwritten = true; | ||
52 | bindings->items[i] = binding; | ||
53 | free_bar_binding(other); | ||
54 | wlr_log(WLR_DEBUG, "[bar %s] Updated binding for button%u%s", | ||
55 | config->current_bar->id, binding->button, | ||
56 | binding->release ? " (release)" : ""); | ||
57 | break; | ||
58 | } | ||
59 | } | ||
60 | if (!overwritten) { | ||
61 | list_add(bindings, binding); | ||
62 | wlr_log(WLR_DEBUG, "[bar %s] Added binding for button%u%s", | ||
63 | config->current_bar->id, binding->button, | ||
64 | binding->release ? " (release)" : ""); | ||
65 | } | ||
66 | |||
67 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
68 | } | ||
diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 15f89f65..6e5ba4fd 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c | |||
@@ -1,4 +1,5 @@ | |||
1 | #include <json-c/json.h> | 1 | #include <json-c/json.h> |
2 | #include <libevdev/libevdev.h> | ||
2 | #include <stdio.h> | 3 | #include <stdio.h> |
3 | #include <ctype.h> | 4 | #include <ctype.h> |
4 | #include "config.h" | 5 | #include "config.h" |
@@ -10,6 +11,7 @@ | |||
10 | #include "sway/tree/workspace.h" | 11 | #include "sway/tree/workspace.h" |
11 | #include "sway/output.h" | 12 | #include "sway/output.h" |
12 | #include "sway/input/input-manager.h" | 13 | #include "sway/input/input-manager.h" |
14 | #include "sway/input/cursor.h" | ||
13 | #include "sway/input/seat.h" | 15 | #include "sway/input/seat.h" |
14 | #include <wlr/backend/libinput.h> | 16 | #include <wlr/backend/libinput.h> |
15 | #include <wlr/types/wlr_box.h> | 17 | #include <wlr/types/wlr_box.h> |
@@ -647,6 +649,31 @@ json_object *ipc_json_describe_seat(struct sway_seat *seat) { | |||
647 | return object; | 649 | return object; |
648 | } | 650 | } |
649 | 651 | ||
652 | static uint32_t event_to_x11_button(uint32_t event) { | ||
653 | switch (event) { | ||
654 | case BTN_LEFT: | ||
655 | return 1; | ||
656 | case BTN_MIDDLE: | ||
657 | return 2; | ||
658 | case BTN_RIGHT: | ||
659 | return 3; | ||
660 | case SWAY_SCROLL_UP: | ||
661 | return 4; | ||
662 | case SWAY_SCROLL_DOWN: | ||
663 | return 5; | ||
664 | case SWAY_SCROLL_LEFT: | ||
665 | return 6; | ||
666 | case SWAY_SCROLL_RIGHT: | ||
667 | return 7; | ||
668 | case BTN_SIDE: | ||
669 | return 8; | ||
670 | case BTN_EXTRA: | ||
671 | return 9; | ||
672 | default: | ||
673 | return 0; | ||
674 | } | ||
675 | } | ||
676 | |||
650 | json_object *ipc_json_describe_bar_config(struct bar_config *bar) { | 677 | json_object *ipc_json_describe_bar_config(struct bar_config *bar) { |
651 | if (!sway_assert(bar, "Bar must not be NULL")) { | 678 | if (!sway_assert(bar, "Bar must not be NULL")) { |
652 | return NULL; | 679 | return NULL; |
@@ -792,6 +819,8 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { | |||
792 | struct bar_binding *binding = bar->bindings->items[i]; | 819 | struct bar_binding *binding = bar->bindings->items[i]; |
793 | json_object *bind = json_object_new_object(); | 820 | json_object *bind = json_object_new_object(); |
794 | json_object_object_add(bind, "input_code", | 821 | json_object_object_add(bind, "input_code", |
822 | json_object_new_int(event_to_x11_button(binding->button))); | ||
823 | json_object_object_add(bind, "event_code", | ||
795 | json_object_new_int(binding->button)); | 824 | json_object_new_int(binding->button)); |
796 | json_object_object_add(bind, "command", | 825 | json_object_object_add(bind, "command", |
797 | json_object_new_string(binding->command)); | 826 | json_object_new_string(binding->command)); |
diff --git a/sway/meson.build b/sway/meson.build index b49d550a..93858336 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -105,8 +105,8 @@ sway_sources = files( | |||
105 | 'commands/workspace_layout.c', | 105 | 'commands/workspace_layout.c', |
106 | 'commands/ws_auto_back_and_forth.c', | 106 | 'commands/ws_auto_back_and_forth.c', |
107 | 107 | ||
108 | 'commands/bar/bind.c', | ||
108 | 'commands/bar/binding_mode_indicator.c', | 109 | 'commands/bar/binding_mode_indicator.c', |
109 | 'commands/bar/bindsym.c', | ||
110 | 'commands/bar/colors.c', | 110 | 'commands/bar/colors.c', |
111 | 'commands/bar/font.c', | 111 | 'commands/bar/font.c', |
112 | 'commands/bar/gaps.c', | 112 | 'commands/bar/gaps.c', |
diff --git a/sway/sway-bar.5.scd b/sway/sway-bar.5.scd index e1a4a937..3f6b4298 100644 --- a/sway/sway-bar.5.scd +++ b/sway/sway-bar.5.scd | |||
@@ -71,10 +71,18 @@ Sway allows configuring swaybar in the sway configuration file. | |||
71 | *height* <height> | 71 | *height* <height> |
72 | Sets the height of the bar. Default height (0) will match the font size. | 72 | Sets the height of the bar. Default height (0) will match the font size. |
73 | 73 | ||
74 | *bindsym* [--release] button<n> <command> | 74 | *bindcode* [--release] <event-code> <command> |
75 | Executes _command_ when mouse button _n_ has been pressed (or if _released_ | 75 | Executes _command_ when the mouse button has been pressed (or if _released_ |
76 | is given, when mouse button _n_ has been released). To disable the default | 76 | is given, when the button has been released). The buttons can be given as |
77 | behavior for a button, use the command _nop_. | 77 | an event code, which can be obtaining from `libinput debug-events`. To |
78 | disable the default behavior for a button, use the command _nop_. | ||
79 | |||
80 | *bindsym* [--release] button[1-9]|<event-name> <command> | ||
81 | Executes _command_ when the mouse button has been pressed (or if _released_ | ||
82 | is given, when the button has been released). The buttons can be given as a | ||
83 | x11 button number or an event name, which can be obtained from `libinput | ||
84 | debug-events`. To disable the default behavior for a button, use the | ||
85 | command _nop_. | ||
78 | 86 | ||
79 | *mode* dock|hide|invisible | 87 | *mode* dock|hide|invisible |
80 | Specifies the visibility of the bar. In _dock_ mode, it is permanently | 88 | Specifies the visibility of the bar. In _dock_ mode, it is permanently |