summaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-10-08 11:40:13 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-10-09 08:12:46 -0400
commit1c969e86f50065985ddf35b7fef62c14aa7688a5 (patch)
tree1d44b026ffb8cd67595b695ac92275ba320ee4d9 /swaybar
parentMerge pull request #2804 from Emantor/swaynag-double-free (diff)
downloadsway-1c969e86f50065985ddf35b7fef62c14aa7688a5.tar.gz
sway-1c969e86f50065985ddf35b7fef62c14aa7688a5.tar.zst
sway-1c969e86f50065985ddf35b7fef62c14aa7688a5.zip
Implement bar bindsym
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/bar.c26
-rw-r--r--swaybar/config.c10
-rw-r--r--swaybar/ipc.c24
3 files changed, 60 insertions, 0 deletions
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,