summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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.c16
-rw-r--r--sway/ipc-json.c16
-rw-r--r--sway/sway-bar.5.scd5
-rw-r--r--swaybar/bar.c26
-rw-r--r--swaybar/config.c10
-rw-r--r--swaybar/ipc.c24
10 files changed, 172 insertions, 2 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index 00b5f25b..549d2677 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];
@@ -527,6 +533,8 @@ struct bar_config *default_bar_config(void);
527 533
528void free_bar_config(struct bar_config *bar); 534void free_bar_config(struct bar_config *bar);
529 535
536void free_bar_binding(struct bar_binding *binding);
537
530void free_workspace_config(struct workspace_config *wsc); 538void free_workspace_config(struct workspace_config *wsc);
531 539
532/** 540/**
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..b0df9eff 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")] - '1' + 1;
37 }
38 if (binding->button == 0) {
39 free_bar_binding(binding);
40 return cmd_results_new(CMD_FAILURE, "bar bindsym",
41 "Only button<n> 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..f84407c9 100644
--- a/sway/config/bar.c
+++ b/sway/config/bar.c
@@ -28,6 +28,16 @@ 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 if (binding->command) {
36 free(binding->command);
37 }
38 free(binding);
39}
40
31void free_bar_config(struct bar_config *bar) { 41void free_bar_config(struct bar_config *bar) {
32 if (!bar) { 42 if (!bar) {
33 return; 43 return;
@@ -39,7 +49,11 @@ void free_bar_config(struct bar_config *bar) {
39 free(bar->status_command); 49 free(bar->status_command);
40 free(bar->font); 50 free(bar->font);
41 free(bar->separator_symbol); 51 free(bar->separator_symbol);
42 // TODO: Free mouse bindings 52 while (bar->bindings->length) {
53 struct bar_binding *binding = bar->bindings->items[0];
54 list_del(bar->bindings, 0);
55 free_bar_binding(binding);
56 }
43 list_free(bar->bindings); 57 list_free(bar->bindings);
44 if (bar->outputs) { 58 if (bar->outputs) {
45 free_flat_list(bar->outputs); 59 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..3eeec5d4 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -144,6 +144,22 @@ 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 wlr_log(WLR_DEBUG, "Checking [%u, %d] against [%u, %d, %s]",
153 x11_button, released,
154 binding->button, binding->release, binding->command);
155 if (binding->button == x11_button && binding->release == released) {
156 ipc_execute_binding(bar, binding);
157 return true;
158 }
159 }
160 return false;
161}
162
147static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, 163static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
148 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { 164 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
149 struct swaybar *bar = data; 165 struct swaybar *bar = data;
@@ -152,6 +168,11 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
152 if (!sway_assert(output, "button with no active output")) { 168 if (!sway_assert(output, "button with no active output")) {
153 return; 169 return;
154 } 170 }
171
172 if (check_bindings(bar, wl_button_to_x11_button(button), state)) {
173 return;
174 }
175
155 if (state != WL_POINTER_BUTTON_STATE_PRESSED) { 176 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
156 return; 177 return;
157 } 178 }
@@ -180,6 +201,11 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
180 return; 201 return;
181 } 202 }
182 203
204 if (check_bindings(bar, wl_axis_to_x11_button(axis, value),
205 WL_POINTER_BUTTON_STATE_PRESSED)) {
206 return;
207 }
208
183 struct swaybar_hotspot *hotspot; 209 struct swaybar_hotspot *hotspot;
184 wl_list_for_each(hotspot, &output->hotspots, link) { 210 wl_list_for_each(hotspot, &output->hotspots, link) {
185 double x = pointer->x * output->scale; 211 double x = pointer->x * output->scale;
diff --git a/swaybar/config.c b/swaybar/config.c
index 4e851cca..c646fe66 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 */
@@ -74,6 +77,13 @@ void free_config(struct swaybar_config *config) {
74 free(config->font); 77 free(config->font);
75 free(config->mode); 78 free(config->mode);
76 free(config->sep_symbol); 79 free(config->sep_symbol);
80 while (config->bindings->length) {
81 struct swaybar_binding *binding = config->bindings->items[0];
82 list_del(config->bindings, 0);
83 free(binding->command);
84 free(binding);
85 }
86 list_free(config->bindings);
77 struct config_output *coutput, *tmp; 87 struct config_output *coutput, *tmp;
78 wl_list_for_each_safe(coutput, tmp, &config->outputs, link) { 88 wl_list_for_each_safe(coutput, tmp, &config->outputs, link) {
79 wl_list_remove(&coutput->link); 89 wl_list_remove(&coutput->link);
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 7c53a44f..70086a36 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,12 @@ 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 uint32_t len = strlen(bind->command);
342 free(ipc_single_command(bar->ipc_socketfd,
343 IPC_COMMAND, bind->command, &len));
344}
345
322bool ipc_initialize(struct swaybar *bar, const char *bar_id) { 346bool ipc_initialize(struct swaybar *bar, const char *bar_id) {
323 uint32_t len = strlen(bar_id); 347 uint32_t len = strlen(bar_id);
324 char *res = ipc_single_command(bar->ipc_socketfd, 348 char *res = ipc_single_command(bar->ipc_socketfd,