aboutsummaryrefslogtreecommitdiff
path: root/sway
diff options
context:
space:
mode:
authorBrian Ashworth <bosrsf04@gmail.com>2019-01-15 21:25:28 -0500
committeremersion <contact@emersion.fr>2019-01-16 11:12:45 +0100
commit02bbefda20b9a4f86e740d33bbaa21c661bb2fac (patch)
tree1b753e414749374be1e144a326a744c4b6812480 /sway
parent247817f68c69c8f4533d948502ae300520dcad59 (diff)
bar_cmd_tray_bind: Use mouse button helpers
This modifies `bar_cmd_tray_bindsym` to use `get_mouse_bindsym` for parsing mouse buttons. This also introduces `bar_cmd_tray_bindcode`, which will use `get_mouse_bindcode` for parsing mouse buttons. Like with sway bindings, the two commands are encapsulated in a single file to maximize shared code. This also modifies tray bindings to work off of events codes rather than x11 buttons, which allows for any mouse buttons to be used. For `get_bar_config`, `event_code` has been added to the `tray_bindings` section and will include to event code for the button. If the event code can be mapped to a x11 button, `input_code` will still be the x11 button number. Otherwise, `input_code` will be `0`.
Diffstat (limited to 'sway')
-rw-r--r--sway/commands/bar.c1
-rw-r--r--sway/commands/bar/bind.c22
-rw-r--r--sway/commands/bar/tray_bind.c97
-rw-r--r--sway/commands/bar/tray_bindsym.c55
-rw-r--r--sway/config/bar.c7
-rw-r--r--sway/input/cursor.c16
-rw-r--r--sway/ipc-json.c19
-rw-r--r--sway/meson.build2
-rw-r--r--sway/sway-bar.5.scd20
9 files changed, 146 insertions, 93 deletions
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index b19d9574..2cfc538f 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -28,6 +28,7 @@ static struct cmd_handler bar_handlers[] = {
{ "status_padding", bar_cmd_status_padding },
{ "strip_workspace_name", bar_cmd_strip_workspace_name },
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
+ { "tray_bindcode", bar_cmd_tray_bindcode },
{ "tray_bindsym", bar_cmd_tray_bindsym },
{ "tray_output", bar_cmd_tray_output },
{ "tray_padding", bar_cmd_tray_padding },
diff --git a/sway/commands/bar/bind.c b/sway/commands/bar/bind.c
index 71adced8..4b0be804 100644
--- a/sway/commands/bar/bind.c
+++ b/sway/commands/bar/bind.c
@@ -46,27 +46,7 @@ static struct cmd_results *bar_cmd_bind(int argc, char **argv, bool code) {
free_bar_binding(binding);
return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]);
}
-
- const char *name = libevdev_event_code_get_name(EV_KEY, binding->button);
- if (!name) {
- switch (binding->button) {
- case SWAY_SCROLL_UP:
- name = "SWAY_SCROLL_UP";
- break;
- case SWAY_SCROLL_DOWN:
- name = "SWAY_SCROLL_DOWN";
- break;
- case SWAY_SCROLL_LEFT:
- name = "SWAY_SCROLL_LEFT";
- break;
- case SWAY_SCROLL_RIGHT:
- name = "SWAY_SCROLL_RIGHT";
- break;
- default:
- // Unreachable
- break;
- }
- }
+ const char *name = get_mouse_button_name(binding->button);
binding->command = join_args(argv + 1, argc - 1);
diff --git a/sway/commands/bar/tray_bind.c b/sway/commands/bar/tray_bind.c
new file mode 100644
index 00000000..48a15462
--- /dev/null
+++ b/sway/commands/bar/tray_bind.c
@@ -0,0 +1,97 @@
+#include <strings.h>
+#include "config.h"
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "sway/input/cursor.h"
+#include "log.h"
+
+static struct cmd_results *tray_bind(int argc, char **argv, bool code) {
+#if HAVE_TRAY
+ const char *command = code ? "bar tray_bindcode" : "bar tray_bindsym";
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, command, EXPECTED_EQUAL_TO, 2))) {
+ return error;
+ }
+ if (!config->current_bar) {
+ return cmd_results_new(CMD_FAILURE, "No bar defined.");
+ }
+
+ struct tray_binding *binding = calloc(1, sizeof(struct tray_binding));
+ if (!binding) {
+ return cmd_results_new(CMD_FAILURE, "Unable to allocate tray binding");
+ }
+
+ char *message = NULL;
+ if (code) {
+ binding->button = get_mouse_bindcode(argv[0], &message);
+ } else {
+ binding->button = get_mouse_bindsym(argv[0], &message);
+ }
+ if (message) {
+ free(binding);
+ error = cmd_results_new(CMD_INVALID, message);
+ free(message);
+ return error;
+ } else if (!binding->button) {
+ free(binding);
+ return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]);
+ }
+ const char *name = get_mouse_button_name(binding->button);
+
+ static const char *commands[] = {
+ "ContextMenu",
+ "Activate",
+ "SecondaryActivate",
+ "ScrollDown",
+ "ScrollLeft",
+ "ScrollRight",
+ "ScrollUp",
+ "nop"
+ };
+
+ for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
+ if (strcasecmp(argv[1], commands[i]) == 0) {
+ binding->command = commands[i];
+ }
+ }
+ if (!binding->command) {
+ return cmd_results_new(CMD_INVALID, "[Bar %s] Invalid tray command %s",
+ config->current_bar->id, argv[1]);
+ }
+
+ bool overwritten = false;
+ struct tray_binding *other = NULL;
+ wl_list_for_each(other, &config->current_bar->tray_bindings, link) {
+ if (other->button == binding->button) {
+ overwritten = true;
+ other->command = binding->command;
+ free(binding);
+ binding = other;
+ wlr_log(WLR_DEBUG,
+ "[bar %s] Updated tray binding for %u (%s) to %s",
+ config->current_bar->id, binding->button, name,
+ binding->command);
+ break;
+ }
+ }
+ if (!overwritten) {
+ wl_list_insert(&config->current_bar->tray_bindings, &binding->link);
+ wlr_log(WLR_DEBUG, "[bar %s] Added tray binding for %u (%s) to %s",
+ config->current_bar->id, binding->button, name,
+ binding->command);
+ }
+
+ return cmd_results_new(CMD_SUCCESS, NULL);
+#else
+ return cmd_results_new(CMD_INVALID,
+ "Sway has been compiled without tray support");
+#endif
+}
+
+struct cmd_results *bar_cmd_tray_bindcode(int argc, char **argv) {
+ return tray_bind(argc, argv, true);
+}
+
+struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) {
+ return tray_bind(argc, argv, false);
+}
diff --git a/sway/commands/bar/tray_bindsym.c b/sway/commands/bar/tray_bindsym.c
deleted file mode 100644
index 4e57e35e..00000000
--- a/sway/commands/bar/tray_bindsym.c
+++ /dev/null
@@ -1,55 +0,0 @@
-#include <strings.h>
-#include "config.h"
-#include "sway/commands.h"
-#include "sway/config.h"
-#include "log.h"
-
-struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) {
-#if HAVE_TRAY
- struct cmd_results *error = NULL;
- if ((error = checkarg(argc, "tray_bindsym", EXPECTED_EQUAL_TO, 2))) {
- return error;
- }
-
- if (!config->current_bar) {
- return cmd_results_new(CMD_FAILURE, "No bar defined.");
- }
-
- int button = 0;
- if (strncasecmp(argv[0], "button", strlen("button")) == 0 &&
- strlen(argv[0]) == strlen("button0")) {
- button = argv[0][strlen("button")] - '0';
- }
- if (button < 1 || button > 9) {
- return cmd_results_new(CMD_FAILURE,
- "[Bar %s] Only buttons 1 to 9 are supported",
- config->current_bar->id);
- }
-
- static const char *commands[] = {
- "ContextMenu",
- "Activate",
- "SecondaryActivate",
- "ScrollDown",
- "ScrollLeft",
- "ScrollRight",
- "ScrollUp",
- "nop"
- };
-
- for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
- if (strcasecmp(argv[1], commands[i]) == 0) {
- wlr_log(WLR_DEBUG, "[Bar %s] Binding button %d to %s",
- config->current_bar->id, button, commands[i]);
- config->current_bar->tray_bindings[button] = commands[i];
- return cmd_results_new(CMD_SUCCESS, NULL);
- }
- }
-
- return cmd_results_new(CMD_INVALID,
- "[Bar %s] Invalid command %s", config->current_bar->id, argv[1]);
-#else
- return cmd_results_new(CMD_INVALID,
- "Sway has been compiled without tray support");
-#endif
-}
diff --git a/sway/config/bar.c b/sway/config/bar.c
index 701bf051..b1aa2313 100644
--- a/sway/config/bar.c
+++ b/sway/config/bar.c
@@ -81,6 +81,12 @@ void free_bar_config(struct bar_config *bar) {
#if HAVE_TRAY
list_free_items_and_destroy(bar->tray_outputs);
free(bar->icon_theme);
+
+ struct tray_binding *tray_bind = NULL, *tmp_tray_bind = NULL;
+ wl_list_for_each_safe(tray_bind, tmp_tray_bind, &bar->tray_bindings, link) {
+ wl_list_remove(&tray_bind->link);
+ free(tray_bind);
+ }
#endif
free(bar);
}
@@ -174,6 +180,7 @@ struct bar_config *default_bar_config(void) {
#if HAVE_TRAY
bar->tray_padding = 2;
+ wl_list_init(&bar->tray_bindings);
#endif
list_add(config->bars, bar);
diff --git a/sway/input/cursor.c b/sway/input/cursor.c
index 731e82ad..5eb44412 100644
--- a/sway/input/cursor.c
+++ b/sway/input/cursor.c
@@ -1237,3 +1237,19 @@ uint32_t get_mouse_button(const char *name, char **error) {
}
return button;
}
+
+const char *get_mouse_button_name(uint32_t button) {
+ const char *name = libevdev_event_code_get_name(EV_KEY, button);
+ if (!name) {
+ if (button == SWAY_SCROLL_UP) {
+ name = "SWAY_SCROLL_UP";
+ } else if (button == SWAY_SCROLL_DOWN) {
+ name = "SWAY_SCROLL_DOWN";
+ } else if (button == SWAY_SCROLL_LEFT) {
+ name = "SWAY_SCROLL_LEFT";
+ } else if (button == SWAY_SCROLL_RIGHT) {
+ name = "SWAY_SCROLL_RIGHT";
+ }
+ }
+ return name;
+}
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 6e5ba4fd..d72fc5db 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -852,15 +852,16 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) {
}
json_object *tray_bindings = json_object_new_array();
- for (int i = 0; i < 10; ++i) {
- if (bar->tray_bindings[i]) {
- json_object *bind = json_object_new_object();
- json_object_object_add(bind, "input_code",
- json_object_new_int(i));
- json_object_object_add(bind, "command",
- json_object_new_string(bar->tray_bindings[i]));
- json_object_array_add(tray_bindings, bind);
- }
+ struct tray_binding *tray_bind = NULL;
+ wl_list_for_each(tray_bind, &bar->tray_bindings, link) {
+ json_object *bind = json_object_new_object();
+ json_object_object_add(bind, "input_code",
+ json_object_new_int(event_to_x11_button(tray_bind->button)));
+ json_object_object_add(bind, "event_code",
+ json_object_new_int(tray_bind->button));
+ json_object_object_add(bind, "command",
+ json_object_new_string(tray_bind->command));
+ json_object_array_add(tray_bindings, bind);
}
if (json_object_array_length(tray_bindings) > 0) {
json_object_object_add(json, "tray_bindings", tray_bindings);
diff --git a/sway/meson.build b/sway/meson.build
index c2ed6298..0a08ee74 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -127,7 +127,7 @@ sway_sources = files(
'commands/bar/strip_workspace_numbers.c',
'commands/bar/strip_workspace_name.c',
'commands/bar/swaybar_command.c',
- 'commands/bar/tray_bindsym.c',
+ 'commands/bar/tray_bind.c',
'commands/bar/tray_output.c',
'commands/bar/tray_padding.c',
'commands/bar/workspace_buttons.c',
diff --git a/sway/sway-bar.5.scd b/sway/sway-bar.5.scd
index 3f6b4298..9a6397e3 100644
--- a/sway/sway-bar.5.scd
+++ b/sway/sway-bar.5.scd
@@ -115,13 +115,19 @@ Sway allows configuring swaybar in the sway configuration file.
Swaybar provides a system tray where third-party applications may place icons.
The following commands configure the tray.
-The _button_ argument in all cases is a platform-specific button code. On Linux
-you can find a list of these at linux/input-event-codes.h.
-
-*tray\_bindsym* button<n> ContextMenu|Activate|SecondaryActivate|ScrollDown|ScrollLeft|ScrollRight|ScrollUp|nop
- Binds mouse button _n_ (1 to 9) to the specified action. Use the command
- _nop_ to disable the default action (Activate for button 1, ContextMenu for
- button 2 and SecondaryActivate for button 3).
+*tray\_bindcode* <event-code>
+ContextMenu|Activate|SecondaryActivate|ScrollDown|ScrollLeft|ScrollRight|ScrollUp|nop
+ Executes the action when the mouse button has been pressed. The buttons can
+ be given as an event code, which can be obtained from `libinput debug-events`.
+ To disable the default behavior for a button, use the command _nop_.
+
+*tray\_bindsym* button[1-9]|<event-name>
+ContextMenu|Activate|SecondaryActivate|ScrollDown|ScrollLeft|ScrollRight|ScrollUp|nop
+ Executes the action when the mouse button has been pressed. The buttons can
+ be given as a x11 button number or an event name, which can be obtained
+ from `libinput debug-events`. Use the command _nop_ to disable the default
+ action (Activate for button1, ContextMenu for button2 and SecondaryActivate
+ for button3).
*tray\_padding* <px> [px]
Sets the pixel padding of the system tray. This padding will surround the