diff --git a/include/config.h b/include/config.h index a8370532b..c2b281f8a 100644 --- a/include/config.h +++ b/include/config.h @@ -26,6 +26,14 @@ struct sway_binding { char *command; }; +/** + * A mouse binding and an associated command. + */ +struct sway_mouse_binding { + uint32_t button; + char *command; +}; + /** * A "mode" of keybindings created via the `mode` command. */ @@ -80,6 +88,7 @@ struct bar_config { */ char *id; uint32_t modifier; + list_t *bindings; enum desktop_shell_panel_position position; char *status_command; char *font; @@ -163,6 +172,10 @@ int sway_binding_cmp(const void *a, const void *b); int sway_binding_cmp_keys(const void *a, const void *b); void free_sway_binding(struct sway_binding *sb); +int sway_mouse_binding_cmp(const void *a, const void *b); +int sway_mouse_binding_cmp_buttons(const void *a, const void *b); +void free_sway_mouse_binding(struct sway_mouse_binding *smb); + /** * Global config singleton. */ diff --git a/sway/commands.c b/sway/commands.c index aa4cb89e4..5119b1062 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -65,6 +65,7 @@ static sway_cmd cmd_sticky; static sway_cmd cmd_workspace; static sway_cmd cmd_ws_auto_back_and_forth; +static sway_cmd bar_cmd_bindsym; static sway_cmd bar_cmd_mode; static sway_cmd bar_cmd_hidden_state; static sway_cmd bar_cmd_id; @@ -1539,6 +1540,41 @@ static struct cmd_handler handlers[] = { { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, }; +static struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { + return error; + } else if (!config->reading) { + return cmd_results_new(CMD_FAILURE, "bindsym", "Can only be used in config file."); + } + + if (strlen(argv[1]) != 7) { + return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", state); + } + uint32_t numbutton = (uint32_t) strtol(argv[1] + 6, NULL, 10); + if (numbutton < 1 || numbutton > 5) { + return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", state); + } + if (strncmp(argv[1], "button", 6) != 0) { + return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", state); + } + struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding)); + binding->button = numbutton; + binding->command = join_args(argv + 1, argc - 1); + + struct bar_config bar = config->bar; + int i = list_seq_find(bar.bindings, sway_mouse_binding_cmp_buttons, binding); + if (i > -1) { + sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); + struct sway_mouse_binding *dup = bar.bindings->items[i]; + free_sway_mouse_binding(dup); + list_del(bar.bindings, i); + } + list_add(bar.bindings, binding); + list_sort(bar.bindings, sway_mouse_binding_cmp); + + sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command); + static struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "hidden_state", EXPECTED_EQUAL_TO, 1))) { @@ -1723,7 +1759,7 @@ static struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { static struct cmd_handler bar_handlers[] = { { "binding_mode_indicator", NULL }, - { "bindsym", NULL }, + { "bindsym", bar_cmd_bindsym }, { "colors", NULL }, { "font", NULL }, { "hidden_state", bar_cmd_hidden_state }, @@ -1740,12 +1776,14 @@ static struct cmd_handler bar_handlers[] = { { "workspace_buttons", bar_cmd_workspace_buttons }, }; + static int handler_compare(const void *_a, const void *_b) { const struct cmd_handler *a = _a; const struct cmd_handler *b = _b; return strcasecmp(a->command, b->command); } + static struct cmd_handler *find_handler(char *line, enum cmd_status block) { struct cmd_handler d = { .command=line }; struct cmd_handler *res = NULL; diff --git a/sway/config.c b/sway/config.c index 2a1f0310b..2fbf57906 100644 --- a/sway/config.c +++ b/sway/config.c @@ -468,7 +468,7 @@ int sway_binding_cmp_keys(const void *a, const void *b) { const struct sway_binding *binda = a, *bindb = b; // Count keys pressed for this binding. important so we check long before - // short ones. for example mod+a+b before mod+a + // short ones. for example mod+a+b before mod+a unsigned int moda = 0, modb = 0, i; // Count how any modifiers are pressed @@ -519,3 +519,33 @@ void free_sway_binding(struct sway_binding *binding) { } free(binding); } + +int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { + const struct sway_mouse_binding *binda = a, *bindb = b; + + if (binda->button > bindb->button) { + return 1; + } + if (binda->button < bindb->button) { + return -1; + } + return 0; +} + +int sway_mouse_binding_cmp(const void *a, const void *b) { + int cmp = 0; + if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { + return cmp; + } + const struct sway_mouse_binding *binda = a, *bindb = b; + return lenient_strcmp(binda->command, bindb->command); +} + +void free_sway_mouse_binding(struct sway_mouse_binding *binding) { + //TODO: AFAIK freeing the button field doesn't make sense + //but better be safe than sorry, so ask first + if (binding->command) { + free(binding->command); + } + free(binding); +}