diff options
Diffstat (limited to 'sway/commands/bar')
-rw-r--r-- | sway/commands/bar/bind.c | 22 | ||||
-rw-r--r-- | sway/commands/bar/tray_bind.c | 97 | ||||
-rw-r--r-- | sway/commands/bar/tray_bindsym.c | 55 |
3 files changed, 98 insertions, 76 deletions
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) { | |||
46 | free_bar_binding(binding); | 46 | free_bar_binding(binding); |
47 | return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]); | 47 | return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]); |
48 | } | 48 | } |
49 | 49 | const char *name = get_mouse_button_name(binding->button); | |
50 | const char *name = libevdev_event_code_get_name(EV_KEY, binding->button); | ||
51 | if (!name) { | ||
52 | switch (binding->button) { | ||
53 | case SWAY_SCROLL_UP: | ||
54 | name = "SWAY_SCROLL_UP"; | ||
55 | break; | ||
56 | case SWAY_SCROLL_DOWN: | ||
57 | name = "SWAY_SCROLL_DOWN"; | ||
58 | break; | ||
59 | case SWAY_SCROLL_LEFT: | ||
60 | name = "SWAY_SCROLL_LEFT"; | ||
61 | break; | ||
62 | case SWAY_SCROLL_RIGHT: | ||
63 | name = "SWAY_SCROLL_RIGHT"; | ||
64 | break; | ||
65 | default: | ||
66 | // Unreachable | ||
67 | break; | ||
68 | } | ||
69 | } | ||
70 | 50 | ||
71 | binding->command = join_args(argv + 1, argc - 1); | 51 | binding->command = join_args(argv + 1, argc - 1); |
72 | 52 | ||
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 @@ | |||
1 | #include <strings.h> | ||
2 | #include "config.h" | ||
3 | #include "sway/commands.h" | ||
4 | #include "sway/config.h" | ||
5 | #include "sway/input/cursor.h" | ||
6 | #include "log.h" | ||
7 | |||
8 | static struct cmd_results *tray_bind(int argc, char **argv, bool code) { | ||
9 | #if HAVE_TRAY | ||
10 | const char *command = code ? "bar tray_bindcode" : "bar tray_bindsym"; | ||
11 | struct cmd_results *error = NULL; | ||
12 | if ((error = checkarg(argc, command, EXPECTED_EQUAL_TO, 2))) { | ||
13 | return error; | ||
14 | } | ||
15 | if (!config->current_bar) { | ||
16 | return cmd_results_new(CMD_FAILURE, "No bar defined."); | ||
17 | } | ||
18 | |||
19 | struct tray_binding *binding = calloc(1, sizeof(struct tray_binding)); | ||
20 | if (!binding) { | ||
21 | return cmd_results_new(CMD_FAILURE, "Unable to allocate tray binding"); | ||
22 | } | ||
23 | |||
24 | char *message = NULL; | ||
25 | if (code) { | ||
26 | binding->button = get_mouse_bindcode(argv[0], &message); | ||
27 | } else { | ||
28 | binding->button = get_mouse_bindsym(argv[0], &message); | ||
29 | } | ||
30 | if (message) { | ||
31 | free(binding); | ||
32 | error = cmd_results_new(CMD_INVALID, message); | ||
33 | free(message); | ||
34 | return error; | ||
35 | } else if (!binding->button) { | ||
36 | free(binding); | ||
37 | return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]); | ||
38 | } | ||
39 | const char *name = get_mouse_button_name(binding->button); | ||
40 | |||
41 | static const char *commands[] = { | ||
42 | "ContextMenu", | ||
43 | "Activate", | ||
44 | "SecondaryActivate", | ||
45 | "ScrollDown", | ||
46 | "ScrollLeft", | ||
47 | "ScrollRight", | ||
48 | "ScrollUp", | ||
49 | "nop" | ||
50 | }; | ||
51 | |||
52 | for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) { | ||
53 | if (strcasecmp(argv[1], commands[i]) == 0) { | ||
54 | binding->command = commands[i]; | ||
55 | } | ||
56 | } | ||
57 | if (!binding->command) { | ||
58 | return cmd_results_new(CMD_INVALID, "[Bar %s] Invalid tray command %s", | ||
59 | config->current_bar->id, argv[1]); | ||
60 | } | ||
61 | |||
62 | bool overwritten = false; | ||
63 | struct tray_binding *other = NULL; | ||
64 | wl_list_for_each(other, &config->current_bar->tray_bindings, link) { | ||
65 | if (other->button == binding->button) { | ||
66 | overwritten = true; | ||
67 | other->command = binding->command; | ||
68 | free(binding); | ||
69 | binding = other; | ||
70 | wlr_log(WLR_DEBUG, | ||
71 | "[bar %s] Updated tray binding for %u (%s) to %s", | ||
72 | config->current_bar->id, binding->button, name, | ||
73 | binding->command); | ||
74 | break; | ||
75 | } | ||
76 | } | ||
77 | if (!overwritten) { | ||
78 | wl_list_insert(&config->current_bar->tray_bindings, &binding->link); | ||
79 | wlr_log(WLR_DEBUG, "[bar %s] Added tray binding for %u (%s) to %s", | ||
80 | config->current_bar->id, binding->button, name, | ||
81 | binding->command); | ||
82 | } | ||
83 | |||
84 | return cmd_results_new(CMD_SUCCESS, NULL); | ||
85 | #else | ||
86 | return cmd_results_new(CMD_INVALID, | ||
87 | "Sway has been compiled without tray support"); | ||
88 | #endif | ||
89 | } | ||
90 | |||
91 | struct cmd_results *bar_cmd_tray_bindcode(int argc, char **argv) { | ||
92 | return tray_bind(argc, argv, true); | ||
93 | } | ||
94 | |||
95 | struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) { | ||
96 | return tray_bind(argc, argv, false); | ||
97 | } | ||
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 @@ | |||
1 | #include <strings.h> | ||
2 | #include "config.h" | ||
3 | #include "sway/commands.h" | ||
4 | #include "sway/config.h" | ||
5 | #include "log.h" | ||
6 | |||
7 | struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) { | ||
8 | #if HAVE_TRAY | ||
9 | struct cmd_results *error = NULL; | ||
10 | if ((error = checkarg(argc, "tray_bindsym", EXPECTED_EQUAL_TO, 2))) { | ||
11 | return error; | ||
12 | } | ||
13 | |||
14 | if (!config->current_bar) { | ||
15 | return cmd_results_new(CMD_FAILURE, "No bar defined."); | ||
16 | } | ||
17 | |||
18 | int button = 0; | ||
19 | if (strncasecmp(argv[0], "button", strlen("button")) == 0 && | ||
20 | strlen(argv[0]) == strlen("button0")) { | ||
21 | button = argv[0][strlen("button")] - '0'; | ||
22 | } | ||
23 | if (button < 1 || button > 9) { | ||
24 | return cmd_results_new(CMD_FAILURE, | ||
25 | "[Bar %s] Only buttons 1 to 9 are supported", | ||
26 | config->current_bar->id); | ||
27 | } | ||
28 | |||
29 | static const char *commands[] = { | ||
30 | "ContextMenu", | ||
31 | "Activate", | ||
32 | "SecondaryActivate", | ||
33 | "ScrollDown", | ||
34 | "ScrollLeft", | ||
35 | "ScrollRight", | ||
36 | "ScrollUp", | ||
37 | "nop" | ||
38 | }; | ||
39 | |||
40 | for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) { | ||
41 | if (strcasecmp(argv[1], commands[i]) == 0) { | ||
42 | wlr_log(WLR_DEBUG, "[Bar %s] Binding button %d to %s", | ||
43 | config->current_bar->id, button, commands[i]); | ||
44 | config->current_bar->tray_bindings[button] = commands[i]; | ||
45 | return cmd_results_new(CMD_SUCCESS, NULL); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | return cmd_results_new(CMD_INVALID, | ||
50 | "[Bar %s] Invalid command %s", config->current_bar->id, argv[1]); | ||
51 | #else | ||
52 | return cmd_results_new(CMD_INVALID, | ||
53 | "Sway has been compiled without tray support"); | ||
54 | #endif | ||
55 | } | ||