summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-10-10 17:07:02 +0200
committerLibravatar GitHub <noreply@github.com>2018-10-10 17:07:02 +0200
commit0a36d14d7a694a88580da1d9b83837afe608a2eb (patch)
treee21f0b633212c6b8084348f08fc68cec2efece8b
parentMerge pull request #2814 from Emantor/mouse-warping-container (diff)
parentMerge branch 'master' into bar-bindsym (diff)
downloadsway-0a36d14d7a694a88580da1d9b83837afe608a2eb.tar.gz
sway-0a36d14d7a694a88580da1d9b83837afe608a2eb.tar.zst
sway-0a36d14d7a694a88580da1d9b83837afe608a2eb.zip
Merge pull request #2798 from RedSoxFan/bar-bindsym
Implement bar bindsym
-rw-r--r--include/sway/config.h8
-rw-r--r--include/swaybar/config.h8
-rw-r--r--include/swaybar/ipc.h1
-rw-r--r--sway/commands/bar/bindsym.c60
-rw-r--r--sway/config/bar.c13
-rw-r--r--sway/ipc-json.c16
-rw-r--r--sway/sway-bar.5.scd5
-rw-r--r--swaybar/bar.c31
-rw-r--r--swaybar/config.c16
-rw-r--r--swaybar/ipc.c26
10 files changed, 182 insertions, 2 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index 0325042c..bc02c0fd 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -239,6 +239,12 @@ struct bar_config {
239 } colors; 239 } colors;
240}; 240};
241 241
242struct bar_binding {
243 uint32_t button;
244 bool release;
245 char *command;
246};
247
242struct border_colors { 248struct border_colors {
243 float border[4]; 249 float border[4];
244 float background[4]; 250 float background[4];
@@ -533,6 +539,8 @@ struct bar_config *default_bar_config(void);
533 539
534void free_bar_config(struct bar_config *bar); 540void free_bar_config(struct bar_config *bar);
535 541
542void free_bar_binding(struct bar_binding *binding);
543
536void free_workspace_config(struct workspace_config *wsc); 544void free_workspace_config(struct workspace_config *wsc);
537 545
538/** 546/**
diff --git a/include/swaybar/config.h b/include/swaybar/config.h
index 5f5688cf..d0336c27 100644
--- a/include/swaybar/config.h
+++ b/include/swaybar/config.h
@@ -3,6 +3,7 @@
3#include <stdbool.h> 3#include <stdbool.h>
4#include <stdint.h> 4#include <stdint.h>
5#include <wayland-client.h> 5#include <wayland-client.h>
6#include "list.h"
6#include "util.h" 7#include "util.h"
7 8
8struct box_colors { 9struct box_colors {
@@ -17,6 +18,12 @@ struct config_output {
17 size_t index; 18 size_t index;
18}; 19};
19 20
21struct swaybar_binding {
22 uint32_t button;
23 char *command;
24 bool release;
25};
26
20struct swaybar_config { 27struct swaybar_config {
21 char *status_command; 28 char *status_command;
22 bool pango_markup; 29 bool pango_markup;
@@ -29,6 +36,7 @@ struct swaybar_config {
29 bool binding_mode_indicator; 36 bool binding_mode_indicator;
30 bool wrap_scroll; 37 bool wrap_scroll;
31 bool workspace_buttons; 38 bool workspace_buttons;
39 list_t *bindings;
32 struct wl_list outputs; // config_output::link 40 struct wl_list outputs; // config_output::link
33 bool all_outputs; 41 bool all_outputs;
34 int height; 42 int height;
diff --git a/include/swaybar/ipc.h b/include/swaybar/ipc.h
index 81e48a6b..8731dac2 100644
--- a/include/swaybar/ipc.h
+++ b/include/swaybar/ipc.h
@@ -7,5 +7,6 @@ bool ipc_initialize(struct swaybar *bar, const char *bar_id);
7bool handle_ipc_readable(struct swaybar *bar); 7bool handle_ipc_readable(struct swaybar *bar);
8void ipc_get_workspaces(struct swaybar *bar); 8void ipc_get_workspaces(struct swaybar *bar);
9void ipc_send_workspace_command(struct swaybar *bar, const char *ws); 9void ipc_send_workspace_command(struct swaybar *bar, const char *ws);
10void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind);
10 11
11#endif 12#endif
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c
index ac09a03f..4eea3e6a 100644
--- a/sway/commands/bar/bindsym.c
+++ b/sway/commands/bar/bindsym.c
@@ -1,5 +1,7 @@
1#define _XOPEN_SOURCE 500
1#include <stdlib.h> 2#include <stdlib.h>
2#include <string.h> 3#include <string.h>
4#include <strings.h>
3#include "sway/commands.h" 5#include "sway/commands.h"
4#include "sway/config.h" 6#include "sway/config.h"
5#include "list.h" 7#include "list.h"
@@ -7,5 +9,61 @@
7#include "stringop.h" 9#include "stringop.h"
8 10
9struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { 11struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
10 return cmd_results_new(CMD_FAILURE, "bindsym", "TODO"); // TODO 12 struct cmd_results *error = NULL;
13 if ((error = checkarg(argc, "bar bindsym", EXPECTED_MORE_THAN, 1))) {
14 return error;
15 }
16 if (!config->current_bar) {
17 return cmd_results_new(CMD_FAILURE, "bar bindsym", "No bar defined.");
18 }
19
20 struct bar_binding *binding = calloc(1, sizeof(struct bar_binding));
21 if (!binding) {
22 return cmd_results_new(CMD_FAILURE, "bar bindsym",
23 "Unable to allocate bar binding");
24 }
25
26 binding->release = false;
27 if (strcmp("--release", argv[0]) == 0) {
28 binding->release = true;
29 argv++;
30 argc--;
31 }
32
33 binding->button = 0;
34 if (strncasecmp(argv[0], "button", strlen("button")) == 0 &&
35 strlen(argv[0]) == strlen("button0")) {
36 binding->button = argv[0][strlen("button")] - '0';
37 }
38 if (binding->button < 1 || binding->button > 9) {
39 free_bar_binding(binding);
40 return cmd_results_new(CMD_FAILURE, "bar bindsym",
41 "Only button<1-9> is supported");
42 }
43
44 binding->command = join_args(argv + 1, argc - 1);
45
46 list_t *bindings = config->current_bar->bindings;
47 bool overwritten = false;
48 for (int i = 0; i < bindings->length; i++) {
49 struct bar_binding *other = bindings->items[i];
50 if (other->button == binding->button &&
51 other->release == binding->release) {
52 overwritten = true;
53 bindings->items[i] = binding;
54 free_bar_binding(other);
55 wlr_log(WLR_DEBUG, "[bar %s] Updated binding for button%u%s",
56 config->current_bar->id, binding->button,
57 binding->release ? " (release)" : "");
58 break;
59 }
60 }
61 if (!overwritten) {
62 list_add(bindings, binding);
63 wlr_log(WLR_DEBUG, "[bar %s] Added binding for button%u%s",
64 config->current_bar->id, binding->button,
65 binding->release ? " (release)" : "");
66 }
67
68 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
11} 69}
diff --git a/sway/config/bar.c b/sway/config/bar.c
index b8695798..c6899f57 100644
--- a/sway/config/bar.c
+++ b/sway/config/bar.c
@@ -28,6 +28,14 @@ static void terminate_swaybar(pid_t pid) {
28 } 28 }
29} 29}
30 30
31void free_bar_binding(struct bar_binding *binding) {
32 if (!binding) {
33 return;
34 }
35 free(binding->command);
36 free(binding);
37}
38
31void free_bar_config(struct bar_config *bar) { 39void free_bar_config(struct bar_config *bar) {
32 if (!bar) { 40 if (!bar) {
33 return; 41 return;
@@ -39,7 +47,10 @@ void free_bar_config(struct bar_config *bar) {
39 free(bar->status_command); 47 free(bar->status_command);
40 free(bar->font); 48 free(bar->font);
41 free(bar->separator_symbol); 49 free(bar->separator_symbol);
42 // TODO: Free mouse bindings 50 for (int i = 0; i < bar->bindings->length; i++) {
51 struct bar_binding *binding = bar->bindings->items[i];
52 free_bar_binding(binding);
53 }
43 list_free(bar->bindings); 54 list_free(bar->bindings);
44 if (bar->outputs) { 55 if (bar->outputs) {
45 free_flat_list(bar->outputs); 56 free_flat_list(bar->outputs);
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index f02f370b..54d611f2 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -623,6 +623,22 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) {
623 623
624 json_object_object_add(json, "colors", colors); 624 json_object_object_add(json, "colors", colors);
625 625
626 if (bar->bindings->length > 0) {
627 json_object *bindings = json_object_new_array();
628 for (int i = 0; i < bar->bindings->length; ++i) {
629 struct bar_binding *binding = bar->bindings->items[i];
630 json_object *bind = json_object_new_object();
631 json_object_object_add(bind, "input_code",
632 json_object_new_int(binding->button));
633 json_object_object_add(bind, "command",
634 json_object_new_string(binding->command));
635 json_object_object_add(bind, "release",
636 json_object_new_boolean(binding->release));
637 json_object_array_add(bindings, bind);
638 }
639 json_object_object_add(json, "bindings", bindings);
640 }
641
626 // Add outputs if defined 642 // Add outputs if defined
627 if (bar->outputs && bar->outputs->length > 0) { 643 if (bar->outputs && bar->outputs->length > 0) {
628 json_object *outputs = json_object_new_array(); 644 json_object *outputs = json_object_new_array();
diff --git a/sway/sway-bar.5.scd b/sway/sway-bar.5.scd
index 8c7be8e7..6729c9ac 100644
--- a/sway/sway-bar.5.scd
+++ b/sway/sway-bar.5.scd
@@ -60,6 +60,11 @@ Sway allows configuring swaybar in the sway configuration file.
60*height* <height> 60*height* <height>
61 Sets the height of the bar. Default height will match the font size. 61 Sets the height of the bar. Default height will match the font size.
62 62
63*bindsym* [--release] button<n> <command>
64 Executes _command_ when mouse button _n_ has been pressed (or if _released_
65 is given, when mouse button _n_ has been released). To disable the default
66 behavior for a button, use the command _nop_.
67
63## TRAY 68## TRAY
64 69
65Swaybar provides a system tray where third-party applications may place icons. 70Swaybar provides a system tray where third-party applications may place icons.
diff --git a/swaybar/bar.c b/swaybar/bar.c
index 3990f1ca..5b7fea71 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -144,6 +144,19 @@ static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
144 bar->pointer.y = wl_fixed_to_int(surface_y); 144 bar->pointer.y = wl_fixed_to_int(surface_y);
145} 145}
146 146
147static bool check_bindings(struct swaybar *bar, uint32_t x11_button,
148 uint32_t state) {
149 bool released = state == WL_POINTER_BUTTON_STATE_RELEASED;
150 for (int i = 0; i < bar->config->bindings->length; i++) {
151 struct swaybar_binding *binding = bar->config->bindings->items[i];
152 if (binding->button == x11_button && binding->release == released) {
153 ipc_execute_binding(bar, binding);
154 return true;
155 }
156 }
157 return false;
158}
159
147static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, 160static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
148 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { 161 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
149 struct swaybar *bar = data; 162 struct swaybar *bar = data;
@@ -152,6 +165,11 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
152 if (!sway_assert(output, "button with no active output")) { 165 if (!sway_assert(output, "button with no active output")) {
153 return; 166 return;
154 } 167 }
168
169 if (check_bindings(bar, wl_button_to_x11_button(button), state)) {
170 return;
171 }
172
155 if (state != WL_POINTER_BUTTON_STATE_PRESSED) { 173 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
156 return; 174 return;
157 } 175 }
@@ -180,6 +198,15 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
180 return; 198 return;
181 } 199 }
182 200
201 // If there is a button press binding, execute it, skip default behavior,
202 // and check button release bindings
203 if (check_bindings(bar, wl_axis_to_x11_button(axis, value),
204 WL_POINTER_BUTTON_STATE_PRESSED)) {
205 check_bindings(bar, wl_axis_to_x11_button(axis, value),
206 WL_POINTER_BUTTON_STATE_RELEASED);
207 return;
208 }
209
183 struct swaybar_hotspot *hotspot; 210 struct swaybar_hotspot *hotspot;
184 wl_list_for_each(hotspot, &output->hotspots, link) { 211 wl_list_for_each(hotspot, &output->hotspots, link) {
185 double x = pointer->x * output->scale; 212 double x = pointer->x * output->scale;
@@ -247,6 +274,10 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
247 } 274 }
248 275
249 ipc_send_workspace_command(bar, new->name); 276 ipc_send_workspace_command(bar, new->name);
277
278 // Check button release bindings
279 check_bindings(bar, wl_axis_to_x11_button(axis, value),
280 WL_POINTER_BUTTON_STATE_RELEASED);
250} 281}
251 282
252static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { 283static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
diff --git a/swaybar/config.c b/swaybar/config.c
index 4e851cca..09d40c24 100644
--- a/swaybar/config.c
+++ b/swaybar/config.c
@@ -3,6 +3,8 @@
3#include <string.h> 3#include <string.h>
4#include "swaybar/config.h" 4#include "swaybar/config.h"
5#include "wlr-layer-shell-unstable-v1-client-protocol.h" 5#include "wlr-layer-shell-unstable-v1-client-protocol.h"
6#include "stringop.h"
7#include "list.h"
6 8
7uint32_t parse_position(const char *position) { 9uint32_t parse_position(const char *position) {
8 uint32_t horiz = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | 10 uint32_t horiz = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
@@ -34,6 +36,7 @@ struct swaybar_config *init_config(void) {
34 config->binding_mode_indicator = true; 36 config->binding_mode_indicator = true;
35 config->wrap_scroll = false; 37 config->wrap_scroll = false;
36 config->workspace_buttons = true; 38 config->workspace_buttons = true;
39 config->bindings = create_list();
37 wl_list_init(&config->outputs); 40 wl_list_init(&config->outputs);
38 41
39 /* height */ 42 /* height */
@@ -69,11 +72,24 @@ struct swaybar_config *init_config(void) {
69 return config; 72 return config;
70} 73}
71 74
75static void free_binding(struct swaybar_binding *binding) {
76 if (!binding) {
77 return;
78 }
79 free(binding->command);
80 free(binding);
81}
82
72void free_config(struct swaybar_config *config) { 83void free_config(struct swaybar_config *config) {
73 free(config->status_command); 84 free(config->status_command);
74 free(config->font); 85 free(config->font);
75 free(config->mode); 86 free(config->mode);
76 free(config->sep_symbol); 87 free(config->sep_symbol);
88 for (int i = 0; i < config->bindings->length; i++) {
89 struct swaybar_binding *binding = config->bindings->items[i];
90 free_binding(binding);
91 }
92 list_free(config->bindings);
77 struct config_output *coutput, *tmp; 93 struct config_output *coutput, *tmp;
78 wl_list_for_each_safe(coutput, tmp, &config->outputs, link) { 94 wl_list_for_each_safe(coutput, tmp, &config->outputs, link) {
79 wl_list_remove(&coutput->link); 95 wl_list_remove(&coutput->link);
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 7c53a44f..a67814c1 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -7,6 +7,7 @@
7#include "swaybar/config.h" 7#include "swaybar/config.h"
8#include "swaybar/ipc.h" 8#include "swaybar/ipc.h"
9#include "ipc-client.h" 9#include "ipc-client.h"
10#include "list.h"
10 11
11void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { 12void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
12 const char *fmt = "workspace \"%s\""; 13 const char *fmt = "workspace \"%s\"";
@@ -154,6 +155,7 @@ static bool ipc_parse_config(
154 json_object *markup, *mode, *hidden_bar, *position, *status_command; 155 json_object *markup, *mode, *hidden_bar, *position, *status_command;
155 json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; 156 json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers;
156 json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; 157 json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs;
158 json_object *bindings;
157 json_object_object_get_ex(bar_config, "mode", &mode); 159 json_object_object_get_ex(bar_config, "mode", &mode);
158 json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); 160 json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar);
159 json_object_object_get_ex(bar_config, "position", &position); 161 json_object_object_get_ex(bar_config, "position", &position);
@@ -169,6 +171,7 @@ static bool ipc_parse_config(
169 json_object_object_get_ex(bar_config, "colors", &colors); 171 json_object_object_get_ex(bar_config, "colors", &colors);
170 json_object_object_get_ex(bar_config, "outputs", &outputs); 172 json_object_object_get_ex(bar_config, "outputs", &outputs);
171 json_object_object_get_ex(bar_config, "pango_markup", &markup); 173 json_object_object_get_ex(bar_config, "pango_markup", &markup);
174 json_object_object_get_ex(bar_config, "bindings", &bindings);
172 if (status_command) { 175 if (status_command) {
173 free(config->status_command); 176 free(config->status_command);
174 config->status_command = strdup(json_object_get_string(status_command)); 177 config->status_command = strdup(json_object_get_string(status_command));
@@ -202,6 +205,21 @@ static bool ipc_parse_config(
202 if (markup) { 205 if (markup) {
203 config->pango_markup = json_object_get_boolean(markup); 206 config->pango_markup = json_object_get_boolean(markup);
204 } 207 }
208 if (bindings) {
209 int length = json_object_array_length(bindings);
210 for (int i = 0; i < length; ++i) {
211 json_object *bindobj = json_object_array_get_idx(bindings, i);
212 struct swaybar_binding *binding =
213 calloc(1, sizeof(struct swaybar_binding));
214 binding->button = json_object_get_int(
215 json_object_object_get(bindobj, "input_code"));
216 binding->command = strdup(json_object_get_string(
217 json_object_object_get(bindobj, "command")));
218 binding->release = json_object_get_boolean(
219 json_object_object_get(bindobj, "release"));
220 list_add(config->bindings, binding);
221 }
222 }
205 223
206 struct config_output *output, *tmp; 224 struct config_output *output, *tmp;
207 wl_list_for_each_safe(output, tmp, &config->outputs, link) { 225 wl_list_for_each_safe(output, tmp, &config->outputs, link) {
@@ -319,6 +337,14 @@ static void ipc_get_outputs(struct swaybar *bar) {
319 free(res); 337 free(res);
320} 338}
321 339
340void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind) {
341 wlr_log(WLR_DEBUG, "Executing binding for button %u (release=%d): `%s`",
342 bind->button, bind->release, bind->command);
343 uint32_t len = strlen(bind->command);
344 free(ipc_single_command(bar->ipc_socketfd,
345 IPC_COMMAND, bind->command, &len));
346}
347
322bool ipc_initialize(struct swaybar *bar, const char *bar_id) { 348bool ipc_initialize(struct swaybar *bar, const char *bar_id) {
323 uint32_t len = strlen(bar_id); 349 uint32_t len = strlen(bar_id);
324 char *res = ipc_single_command(bar->ipc_socketfd, 350 char *res = ipc_single_command(bar->ipc_socketfd,