From 7a93906626203464938bbc95851e47314d5c76f5 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Sun, 5 Dec 2021 15:24:29 +0200 Subject: [PATCH] Implement `modifiers` array in swaybar click events --- include/sway/input/keyboard.h | 7 +++++++ include/swaybar/ipc.h | 2 ++ sway/input/keyboard.c | 5 +---- sway/ipc-json.c | 15 +++++++++++++++ swaybar/i3bar.c | 5 +++++ swaybar/ipc.c | 20 ++++++++++++++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h index 2c61e5a7..5cb5340a 100644 --- a/include/sway/input/keyboard.h +++ b/include/sway/input/keyboard.h @@ -5,6 +5,13 @@ #define SWAY_KEYBOARD_PRESSED_KEYS_CAP 32 +struct modifier_key { + char *name; + uint32_t mod; +}; + +extern struct modifier_key modifiers[10]; + /** * Get modifier mask from modifier name. * diff --git a/include/swaybar/ipc.h b/include/swaybar/ipc.h index d8cd0c76..cef70f54 100644 --- a/include/swaybar/ipc.h +++ b/include/swaybar/ipc.h @@ -1,6 +1,7 @@ #ifndef _SWAYBAR_IPC_H #define _SWAYBAR_IPC_H #include +#include #include "swaybar/bar.h" bool ipc_initialize(struct swaybar *bar); @@ -8,5 +9,6 @@ bool handle_ipc_readable(struct swaybar *bar); bool ipc_get_workspaces(struct swaybar *bar); void ipc_send_workspace_command(struct swaybar *bar, const char *ws); void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind); +json_object *ipc_get_keyboard_modifiers(struct status_line *status); #endif diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index f258ac7d..a16ce405 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -16,10 +16,7 @@ #include "sway/ipc-server.h" #include "log.h" -static struct modifier_key { - char *name; - uint32_t mod; -} modifiers[] = { +struct modifier_key modifiers[] = { { XKB_MOD_NAME_SHIFT, WLR_MODIFIER_SHIFT }, { XKB_MOD_NAME_CAPS, WLR_MODIFIER_CAPS }, { XKB_MOD_NAME_CTRL, WLR_MODIFIER_CTRL }, diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 1b64f86e..a59442a8 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -17,6 +17,7 @@ #include "sway/input/input-manager.h" #include "sway/input/cursor.h" #include "sway/input/seat.h" +#include "sway/input/keyboard.h" #include "wlr-layer-shell-unstable-v1-protocol.h" #include "sway/desktop/idle_inhibit_v1.h" @@ -1000,6 +1001,20 @@ json_object *ipc_json_describe_input(struct sway_input_device *device) { layout ? json_object_new_string(layout) : NULL); } } + + json_object *modifiers_json = json_object_new_array(); + uint32_t keyboard_modifiers = wlr_keyboard_get_modifiers(keyboard); + for (int i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); i++) { + if (keyboard_modifiers & modifiers[i].mod) { + json_object_array_add(modifiers_json, + json_object_new_string(modifiers[i].name)); + } + } + if (json_object_array_length(modifiers_json) > 0) { + json_object_object_add(object, "xkb_modifiers", modifiers_json); + } else { + json_object_put(modifiers_json); + } } if (device->wlr_device->type == WLR_INPUT_DEVICE_POINTER) { diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 6d00befb..c629d347 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -9,6 +9,7 @@ #include "swaybar/bar.h" #include "swaybar/config.h" #include "swaybar/i3bar.h" +#include "swaybar/ipc.h" #include "swaybar/input.h" #include "swaybar/status_line.h" @@ -286,6 +287,10 @@ enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, json_object_object_add(event_json, "button", json_object_new_int(event_to_x11_button(button))); json_object_object_add(event_json, "event", json_object_new_int(button)); + json_object *modifiers_json = ipc_get_keyboard_modifiers(status); + if (modifiers_json) { + json_object_object_add(event_json, "modifiers", modifiers_json); + } if (status->float_event_coords) { json_object_object_add(event_json, "x", json_object_new_double(x)); json_object_object_add(event_json, "y", json_object_new_double(y)); diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 2cb235bf..1553a03c 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -606,3 +606,23 @@ bool handle_ipc_readable(struct swaybar *bar) { free_ipc_response(resp); return bar_is_dirty; } + +json_object *ipc_get_keyboard_modifiers(struct status_line *status) { + uint32_t len = 0; + char *buf = ipc_single_command(status->bar->ipc_socketfd, + IPC_GET_INPUTS, NULL, &len); + json_object *inputs_json = json_tokener_parse(buf); + free(buf); + if (!inputs_json) { + return NULL; + } + for (size_t i = 0; i < json_object_array_length(inputs_json); i++) { + json_object *input_json = json_object_array_get_idx(inputs_json, i); + json_object *modifiers_json = NULL; + json_object_object_get_ex(input_json, "xkb_modifiers", &modifiers_json); + if (modifiers_json) { + return modifiers_json; + } + } + return NULL; +}