diff options
Diffstat (limited to 'sway/commands/bar/bindsym.c')
-rw-r--r-- | sway/commands/bar/bindsym.c | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index ac09a03f..4eea3e6a 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c | |||
@@ -1,5 +1,7 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
2 | #include <string.h> | 3 | #include <string.h> |
4 | #include <strings.h> | ||
3 | #include "sway/commands.h" | 5 | #include "sway/commands.h" |
4 | #include "sway/config.h" | 6 | #include "sway/config.h" |
5 | #include "list.h" | 7 | #include "list.h" |
@@ -7,5 +9,61 @@ | |||
7 | #include "stringop.h" | 9 | #include "stringop.h" |
8 | 10 | ||
9 | struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { | 11 | struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { |
10 | return cmd_results_new(CMD_FAILURE, "bindsym", "TODO"); // TODO | 12 | struct cmd_results *error = NULL; |
13 | if ((error = checkarg(argc, "bar bindsym", EXPECTED_MORE_THAN, 1))) { | ||
14 | return error; | ||
15 | } | ||
16 | if (!config->current_bar) { | ||
17 | return cmd_results_new(CMD_FAILURE, "bar bindsym", "No bar defined."); | ||
18 | } | ||
19 | |||
20 | struct bar_binding *binding = calloc(1, sizeof(struct bar_binding)); | ||
21 | if (!binding) { | ||
22 | return cmd_results_new(CMD_FAILURE, "bar bindsym", | ||
23 | "Unable to allocate bar binding"); | ||
24 | } | ||
25 | |||
26 | binding->release = false; | ||
27 | if (strcmp("--release", argv[0]) == 0) { | ||
28 | binding->release = true; | ||
29 | argv++; | ||
30 | argc--; | ||
31 | } | ||
32 | |||
33 | binding->button = 0; | ||
34 | if (strncasecmp(argv[0], "button", strlen("button")) == 0 && | ||
35 | strlen(argv[0]) == strlen("button0")) { | ||
36 | binding->button = argv[0][strlen("button")] - '0'; | ||
37 | } | ||
38 | if (binding->button < 1 || binding->button > 9) { | ||
39 | free_bar_binding(binding); | ||
40 | return cmd_results_new(CMD_FAILURE, "bar bindsym", | ||
41 | "Only button<1-9> is supported"); | ||
42 | } | ||
43 | |||
44 | binding->command = join_args(argv + 1, argc - 1); | ||
45 | |||
46 | list_t *bindings = config->current_bar->bindings; | ||
47 | bool overwritten = false; | ||
48 | for (int i = 0; i < bindings->length; i++) { | ||
49 | struct bar_binding *other = bindings->items[i]; | ||
50 | if (other->button == binding->button && | ||
51 | other->release == binding->release) { | ||
52 | overwritten = true; | ||
53 | bindings->items[i] = binding; | ||
54 | free_bar_binding(other); | ||
55 | wlr_log(WLR_DEBUG, "[bar %s] Updated binding for button%u%s", | ||
56 | config->current_bar->id, binding->button, | ||
57 | binding->release ? " (release)" : ""); | ||
58 | break; | ||
59 | } | ||
60 | } | ||
61 | if (!overwritten) { | ||
62 | list_add(bindings, binding); | ||
63 | wlr_log(WLR_DEBUG, "[bar %s] Added binding for button%u%s", | ||
64 | config->current_bar->id, binding->button, | ||
65 | binding->release ? " (release)" : ""); | ||
66 | } | ||
67 | |||
68 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
11 | } | 69 | } |