From 1c969e86f50065985ddf35b7fef62c14aa7688a5 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 8 Oct 2018 11:40:13 -0400 Subject: Implement bar bindsym --- sway/commands/bar/bindsym.c | 60 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'sway/commands/bar') 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 @@ +#define _XOPEN_SOURCE 500 #include #include +#include #include "sway/commands.h" #include "sway/config.h" #include "list.h" @@ -7,5 +9,61 @@ #include "stringop.h" struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { - return cmd_results_new(CMD_FAILURE, "bindsym", "TODO"); // TODO + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bar bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } + if (!config->current_bar) { + return cmd_results_new(CMD_FAILURE, "bar bindsym", "No bar defined."); + } + + struct bar_binding *binding = calloc(1, sizeof(struct bar_binding)); + if (!binding) { + return cmd_results_new(CMD_FAILURE, "bar bindsym", + "Unable to allocate bar binding"); + } + + binding->release = false; + if (strcmp("--release", argv[0]) == 0) { + binding->release = true; + argv++; + argc--; + } + + binding->button = 0; + if (strncasecmp(argv[0], "button", strlen("button")) == 0 && + strlen(argv[0]) == strlen("button0")) { + binding->button = argv[0][strlen("button")] - '1' + 1; + } + if (binding->button == 0) { + free_bar_binding(binding); + return cmd_results_new(CMD_FAILURE, "bar bindsym", + "Only button is supported"); + } + + binding->command = join_args(argv + 1, argc - 1); + + list_t *bindings = config->current_bar->bindings; + bool overwritten = false; + for (int i = 0; i < bindings->length; i++) { + struct bar_binding *other = bindings->items[i]; + if (other->button == binding->button && + other->release == binding->release) { + overwritten = true; + bindings->items[i] = binding; + free_bar_binding(other); + wlr_log(WLR_DEBUG, "[bar %s] Updated binding for button%u%s", + config->current_bar->id, binding->button, + binding->release ? " (release)" : ""); + break; + } + } + if (!overwritten) { + list_add(bindings, binding); + wlr_log(WLR_DEBUG, "[bar %s] Added binding for button%u%s", + config->current_bar->id, binding->button, + binding->release ? " (release)" : ""); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); } -- cgit v1.2.3 From d3f0e52784712696c7174d3adf8ec6cf3ac31b19 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Tue, 9 Oct 2018 08:12:02 -0400 Subject: bar-bindsym: address ianyfan's comments --- sway/commands/bar/bindsym.c | 6 +++--- sway/config/bar.c | 9 +++------ swaybar/bar.c | 13 +++++++++---- swaybar/config.c | 16 +++++++++++----- swaybar/ipc.c | 2 ++ 5 files changed, 28 insertions(+), 18 deletions(-) (limited to 'sway/commands/bar') diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index b0df9eff..4eea3e6a 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c @@ -33,12 +33,12 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { binding->button = 0; if (strncasecmp(argv[0], "button", strlen("button")) == 0 && strlen(argv[0]) == strlen("button0")) { - binding->button = argv[0][strlen("button")] - '1' + 1; + binding->button = argv[0][strlen("button")] - '0'; } - if (binding->button == 0) { + if (binding->button < 1 || binding->button > 9) { free_bar_binding(binding); return cmd_results_new(CMD_FAILURE, "bar bindsym", - "Only button is supported"); + "Only button<1-9> is supported"); } binding->command = join_args(argv + 1, argc - 1); diff --git a/sway/config/bar.c b/sway/config/bar.c index f84407c9..c6899f57 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c @@ -32,9 +32,7 @@ void free_bar_binding(struct bar_binding *binding) { if (!binding) { return; } - if (binding->command) { - free(binding->command); - } + free(binding->command); free(binding); } @@ -49,9 +47,8 @@ void free_bar_config(struct bar_config *bar) { free(bar->status_command); free(bar->font); free(bar->separator_symbol); - while (bar->bindings->length) { - struct bar_binding *binding = bar->bindings->items[0]; - list_del(bar->bindings, 0); + for (int i = 0; i < bar->bindings->length; i++) { + struct bar_binding *binding = bar->bindings->items[i]; free_bar_binding(binding); } list_free(bar->bindings); diff --git a/swaybar/bar.c b/swaybar/bar.c index 3eeec5d4..5b7fea71 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -149,9 +149,6 @@ static bool check_bindings(struct swaybar *bar, uint32_t x11_button, bool released = state == WL_POINTER_BUTTON_STATE_RELEASED; for (int i = 0; i < bar->config->bindings->length; i++) { struct swaybar_binding *binding = bar->config->bindings->items[i]; - wlr_log(WLR_DEBUG, "Checking [%u, %d] against [%u, %d, %s]", - x11_button, released, - binding->button, binding->release, binding->command); if (binding->button == x11_button && binding->release == released) { ipc_execute_binding(bar, binding); return true; @@ -201,8 +198,12 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, return; } + // If there is a button press binding, execute it, skip default behavior, + // and check button release bindings if (check_bindings(bar, wl_axis_to_x11_button(axis, value), - WL_POINTER_BUTTON_STATE_PRESSED)) { + WL_POINTER_BUTTON_STATE_PRESSED)) { + check_bindings(bar, wl_axis_to_x11_button(axis, value), + WL_POINTER_BUTTON_STATE_RELEASED); return; } @@ -273,6 +274,10 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, } ipc_send_workspace_command(bar, new->name); + + // Check button release bindings + check_bindings(bar, wl_axis_to_x11_button(axis, value), + WL_POINTER_BUTTON_STATE_RELEASED); } static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { diff --git a/swaybar/config.c b/swaybar/config.c index c646fe66..09d40c24 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -72,16 +72,22 @@ struct swaybar_config *init_config(void) { return config; } +static void free_binding(struct swaybar_binding *binding) { + if (!binding) { + return; + } + free(binding->command); + free(binding); +} + void free_config(struct swaybar_config *config) { free(config->status_command); free(config->font); free(config->mode); free(config->sep_symbol); - while (config->bindings->length) { - struct swaybar_binding *binding = config->bindings->items[0]; - list_del(config->bindings, 0); - free(binding->command); - free(binding); + for (int i = 0; i < config->bindings->length; i++) { + struct swaybar_binding *binding = config->bindings->items[i]; + free_binding(binding); } list_free(config->bindings); struct config_output *coutput, *tmp; diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 70086a36..a67814c1 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -338,6 +338,8 @@ static void ipc_get_outputs(struct swaybar *bar) { } void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind) { + wlr_log(WLR_DEBUG, "Executing binding for button %u (release=%d): `%s`", + bind->button, bind->release, bind->command); uint32_t len = strlen(bind->command); free(ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, bind->command, &len)); -- cgit v1.2.3