From a0c8799c8008da4eccde3ae4bd5865b5c4422058 Mon Sep 17 00:00:00 2001 From: David Eklov Date: Mon, 11 Jul 2016 00:11:38 -0500 Subject: [PATCH] Compute what workspace button is clicked This commit does not do anything with this information other than logging it. --- include/bar/ipc.h | 6 ++++++ swaybar/bar.c | 29 ++++++++++++++++++++++++++++- swaybar/ipc.c | 4 ++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/bar/ipc.h b/include/bar/ipc.h index 741c067bc..c11931d0f 100644 --- a/include/bar/ipc.h +++ b/include/bar/ipc.h @@ -13,5 +13,11 @@ void ipc_bar_init(struct bar *bar, const char *bar_id); */ bool handle_ipc_event(struct bar *bar); + +/** + * Send workspace command to sway + */ +void ipc_send_workspace_command(const char *workspace_name); + #endif /* _SWAYBAR_IPC_H */ diff --git a/swaybar/bar.c b/swaybar/bar.c index ed18b5e66..4f8063ac8 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -58,8 +58,35 @@ struct output *new_output(const char *name) { return output; } -static void mouse_button_notify(struct window *window, wl_fixed_t x, wl_fixed_t y, uint32_t button) { +static void mouse_button_notify(struct window *window, int x, int y, uint32_t button) { sway_log(L_DEBUG, "Mouse button %d clicked at %d %d\n", button, x, y); + + struct output *clicked_output = NULL; + for (int i = 0; i < swaybar.outputs->length; i++) { + struct output *output = swaybar.outputs->items[i]; + if (window == output->window) { + clicked_output = output; + break; + } + } + + if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) { + return; + } + + double button_x = 0.5; + for (int i = 0; i < clicked_output->workspaces->length; i++) { + struct workspace *workspace = clicked_output->workspaces->items[i]; + int button_width, button_height; + + workspace_button_size(window, workspace->name, &button_width, &button_height); + + button_x += button_width; + if (x <= button_x) { + ipc_send_workspace_command(workspace->name); + break; + } + } } void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { diff --git a/swaybar/ipc.c b/swaybar/ipc.c index dacee4c29..6697742ea 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -7,6 +7,10 @@ #include "bar/config.h" #include "bar/ipc.h" +void ipc_send_workspace_command(const char *workspace_name) { + sway_log(L_DEBUG, "Clicked on window %s", workspace_name); +} + static void ipc_parse_config(struct config *config, const char *payload) { json_object *bar_config = json_tokener_parse(payload); json_object *tray_output, *mode, *hidden_bar, *position, *status_command;