diff options
Diffstat (limited to 'sway/commands/bar/bindsym.c')
-rw-r--r-- | sway/commands/bar/bindsym.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c new file mode 100644 index 00000000..4f50e708 --- /dev/null +++ b/sway/commands/bar/bindsym.c | |||
@@ -0,0 +1,45 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | #include "commands.h" | ||
4 | #include "config.h" | ||
5 | #include "list.h" | ||
6 | #include "log.h" | ||
7 | #include "stringop.h" | ||
8 | |||
9 | struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { | ||
10 | struct cmd_results *error = NULL; | ||
11 | if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { | ||
12 | return error; | ||
13 | } else if (!config->reading) { | ||
14 | return cmd_results_new(CMD_FAILURE, "bindsym", "Can only be used in config file."); | ||
15 | } | ||
16 | |||
17 | if (!config->current_bar) { | ||
18 | return cmd_results_new(CMD_FAILURE, "bindsym", "No bar defined."); | ||
19 | } | ||
20 | |||
21 | if (strlen(argv[1]) != 7) { | ||
22 | return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); | ||
23 | } | ||
24 | uint32_t numbutton = (uint32_t)atoi(argv[1] + 6); | ||
25 | if (numbutton < 1 || numbutton > 5 || strncmp(argv[1], "button", 6) != 0) { | ||
26 | return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); | ||
27 | } | ||
28 | struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding)); | ||
29 | binding->button = numbutton; | ||
30 | binding->command = join_args(argv + 1, argc - 1); | ||
31 | |||
32 | struct bar_config *bar = config->current_bar; | ||
33 | int i = list_seq_find(bar->bindings, sway_mouse_binding_cmp_buttons, binding); | ||
34 | if (i > -1) { | ||
35 | sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); | ||
36 | struct sway_mouse_binding *dup = bar->bindings->items[i]; | ||
37 | free_sway_mouse_binding(dup); | ||
38 | list_del(bar->bindings, i); | ||
39 | } | ||
40 | list_add(bar->bindings, binding); | ||
41 | list_qsort(bar->bindings, sway_mouse_binding_cmp_qsort); | ||
42 | |||
43 | sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command); | ||
44 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
45 | } | ||