diff options
Diffstat (limited to 'sway/commands/bar/bind.c')
-rw-r--r-- | sway/commands/bar/bind.c | 106 |
1 files changed, 106 insertions, 0 deletions
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 | } | ||