diff options
Diffstat (limited to 'sway/commands/bar/tray_bind.c')
-rw-r--r-- | sway/commands/bar/tray_bind.c | 97 |
1 files changed, 97 insertions, 0 deletions
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 | } | ||