diff options
author | 2018-10-08 11:40:13 -0400 | |
---|---|---|
committer | 2018-10-09 08:12:46 -0400 | |
commit | 1c969e86f50065985ddf35b7fef62c14aa7688a5 (patch) | |
tree | 1d44b026ffb8cd67595b695ac92275ba320ee4d9 /sway | |
parent | Merge pull request #2804 from Emantor/swaynag-double-free (diff) | |
download | sway-1c969e86f50065985ddf35b7fef62c14aa7688a5.tar.gz sway-1c969e86f50065985ddf35b7fef62c14aa7688a5.tar.zst sway-1c969e86f50065985ddf35b7fef62c14aa7688a5.zip |
Implement bar bindsym
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands/bar/bindsym.c | 60 | ||||
-rw-r--r-- | sway/config/bar.c | 16 | ||||
-rw-r--r-- | sway/ipc-json.c | 16 | ||||
-rw-r--r-- | sway/sway-bar.5.scd | 5 |
4 files changed, 95 insertions, 2 deletions
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index ac09a03f..b0df9eff 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")] - '1' + 1; | ||
37 | } | ||
38 | if (binding->button == 0) { | ||
39 | free_bar_binding(binding); | ||
40 | return cmd_results_new(CMD_FAILURE, "bar bindsym", | ||
41 | "Only button<n> 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 | } |
diff --git a/sway/config/bar.c b/sway/config/bar.c index b8695798..f84407c9 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c | |||
@@ -28,6 +28,16 @@ static void terminate_swaybar(pid_t pid) { | |||
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | void free_bar_binding(struct bar_binding *binding) { | ||
32 | if (!binding) { | ||
33 | return; | ||
34 | } | ||
35 | if (binding->command) { | ||
36 | free(binding->command); | ||
37 | } | ||
38 | free(binding); | ||
39 | } | ||
40 | |||
31 | void free_bar_config(struct bar_config *bar) { | 41 | void free_bar_config(struct bar_config *bar) { |
32 | if (!bar) { | 42 | if (!bar) { |
33 | return; | 43 | return; |
@@ -39,7 +49,11 @@ void free_bar_config(struct bar_config *bar) { | |||
39 | free(bar->status_command); | 49 | free(bar->status_command); |
40 | free(bar->font); | 50 | free(bar->font); |
41 | free(bar->separator_symbol); | 51 | free(bar->separator_symbol); |
42 | // TODO: Free mouse bindings | 52 | while (bar->bindings->length) { |
53 | struct bar_binding *binding = bar->bindings->items[0]; | ||
54 | list_del(bar->bindings, 0); | ||
55 | free_bar_binding(binding); | ||
56 | } | ||
43 | list_free(bar->bindings); | 57 | list_free(bar->bindings); |
44 | if (bar->outputs) { | 58 | if (bar->outputs) { |
45 | free_flat_list(bar->outputs); | 59 | free_flat_list(bar->outputs); |
diff --git a/sway/ipc-json.c b/sway/ipc-json.c index f02f370b..54d611f2 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c | |||
@@ -623,6 +623,22 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { | |||
623 | 623 | ||
624 | json_object_object_add(json, "colors", colors); | 624 | json_object_object_add(json, "colors", colors); |
625 | 625 | ||
626 | if (bar->bindings->length > 0) { | ||
627 | json_object *bindings = json_object_new_array(); | ||
628 | for (int i = 0; i < bar->bindings->length; ++i) { | ||
629 | struct bar_binding *binding = bar->bindings->items[i]; | ||
630 | json_object *bind = json_object_new_object(); | ||
631 | json_object_object_add(bind, "input_code", | ||
632 | json_object_new_int(binding->button)); | ||
633 | json_object_object_add(bind, "command", | ||
634 | json_object_new_string(binding->command)); | ||
635 | json_object_object_add(bind, "release", | ||
636 | json_object_new_boolean(binding->release)); | ||
637 | json_object_array_add(bindings, bind); | ||
638 | } | ||
639 | json_object_object_add(json, "bindings", bindings); | ||
640 | } | ||
641 | |||
626 | // Add outputs if defined | 642 | // Add outputs if defined |
627 | if (bar->outputs && bar->outputs->length > 0) { | 643 | if (bar->outputs && bar->outputs->length > 0) { |
628 | json_object *outputs = json_object_new_array(); | 644 | json_object *outputs = json_object_new_array(); |
diff --git a/sway/sway-bar.5.scd b/sway/sway-bar.5.scd index 8c7be8e7..6729c9ac 100644 --- a/sway/sway-bar.5.scd +++ b/sway/sway-bar.5.scd | |||
@@ -60,6 +60,11 @@ Sway allows configuring swaybar in the sway configuration file. | |||
60 | *height* <height> | 60 | *height* <height> |
61 | Sets the height of the bar. Default height will match the font size. | 61 | Sets the height of the bar. Default height will match the font size. |
62 | 62 | ||
63 | *bindsym* [--release] button<n> <command> | ||
64 | Executes _command_ when mouse button _n_ has been pressed (or if _released_ | ||
65 | is given, when mouse button _n_ has been released). To disable the default | ||
66 | behavior for a button, use the command _nop_. | ||
67 | |||
63 | ## TRAY | 68 | ## TRAY |
64 | 69 | ||
65 | Swaybar provides a system tray where third-party applications may place icons. | 70 | Swaybar provides a system tray where third-party applications may place icons. |