From 2f1fd8072673b1824f37759e14f5388d7a87fb5c Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 12 Oct 2018 21:14:52 +0100 Subject: [PATCH] swaybar: show hidden bar on key event Since wayland does not currently allow swaybar to create global keybinds, this is handled within sway and sent to the bar using a custom event, so as not to pollute existing events, called bar_state_update. --- include/ipc.h | 3 +++ include/sway/config.h | 1 + include/sway/ipc-server.h | 1 + include/swaybar/config.h | 1 + sway/config/bar.c | 2 ++ sway/input/keyboard.c | 28 ++++++++++++++++++++++------ sway/ipc-server.c | 18 ++++++++++++++++++ swaybar/ipc.c | 19 ++++++++++++++++++- 8 files changed, 66 insertions(+), 7 deletions(-) diff --git a/include/ipc.h b/include/ipc.h index a3f60e193..9063b9337 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -30,6 +30,9 @@ enum ipc_command_type { IPC_EVENT_BINDING = ((1<<31) | 5), IPC_EVENT_SHUTDOWN = ((1<<31) | 6), IPC_EVENT_TICK = ((1<<31) | 7), + + // sway-specific event types + IPC_EVENT_BAR_STATE_UPDATE = ((1<<31) | 20), }; #endif diff --git a/include/sway/config.h b/include/sway/config.h index f21ecbb11..be5a00b56 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -191,6 +191,7 @@ struct bar_config { * In "show" mode, it will always be shown on top of the active workspace. */ char *hidden_state; + bool visible_by_modifier; // only relevant in "hide" mode /** * Id name used to identify the bar through IPC. * diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index 80180ec4e..3c43f74d2 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h @@ -15,6 +15,7 @@ void ipc_event_workspace(struct sway_workspace *old, struct sway_workspace *new, const char *change); void ipc_event_window(struct sway_container *window, const char *change); void ipc_event_barconfig_update(struct bar_config *bar); +void ipc_event_bar_state_update(struct bar_config *bar); void ipc_event_mode(const char *mode, bool pango); void ipc_event_shutdown(const char *reason); void ipc_event_binding(struct sway_binding *binding); diff --git a/include/swaybar/config.h b/include/swaybar/config.h index 10904bca0..5d40790aa 100644 --- a/include/swaybar/config.h +++ b/include/swaybar/config.h @@ -32,6 +32,7 @@ struct swaybar_config { char *sep_symbol; char *mode; char *hidden_state; + char *modifier; bool strip_workspace_numbers; bool binding_mode_indicator; bool wrap_scroll; diff --git a/sway/config/bar.c b/sway/config/bar.c index 5726e95b6..8b88642e8 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c @@ -16,6 +16,7 @@ #include "stringop.h" #include "list.h" #include "log.h" +#include "util.h" static void terminate_swaybar(pid_t pid) { wlr_log(WLR_DEBUG, "Terminating swaybar %d", pid); @@ -101,6 +102,7 @@ struct bar_config *default_bar_config(void) { bar->binding_mode_indicator = true; bar->verbose = false; bar->pid = 0; + bar->modifier = get_modifier_mask_by_name("Mod4"); if (!(bar->mode = strdup("dock"))) { goto cleanup; } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index fb1fe7b51..2c8b41cd7 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -9,6 +9,7 @@ #include "sway/input/input-manager.h" #include "sway/input/keyboard.h" #include "sway/input/seat.h" +#include "sway/ipc-server.h" #include "log.h" /** @@ -66,10 +67,10 @@ static void update_shortcut_state(struct sway_shortcut_state *state, bool last_key_was_a_modifier = raw_modifiers != state->last_raw_modifiers; state->last_raw_modifiers = raw_modifiers; - if (last_key_was_a_modifier && state->last_keycode) { - // Last pressed key before this one was a modifier - state_erase_key(state, state->last_keycode); - } + if (last_key_was_a_modifier && state->last_keycode) { + // Last pressed key before this one was a modifier + state_erase_key(state, state->last_keycode); + } if (event->state == WLR_KEY_PRESSED) { // Add current key to set; there may be duplicates @@ -235,7 +236,6 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { code_modifiers); } - bool handled = false; // Identify active release binding @@ -337,6 +337,19 @@ static int handle_keyboard_repeat(void *data) { return 0; } +static void determine_bar_visibility(uint32_t modifiers) { + for (int i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = config->bars->items[i]; + if (strcmp(bar->mode, bar->hidden_state) == 0) { // both are "hide" + bool should_be_visible = (~modifiers & bar->modifier) == 0; + if (bar->visible_by_modifier != should_be_visible) { + bar->visible_by_modifier = should_be_visible; + ipc_event_bar_state_update(bar); + } + } + } +} + static void handle_keyboard_modifiers(struct wl_listener *listener, void *data) { struct sway_keyboard *keyboard = @@ -346,6 +359,9 @@ static void handle_keyboard_modifiers(struct wl_listener *listener, keyboard->seat_device->input_device->wlr_device; wlr_seat_set_keyboard(wlr_seat, wlr_device); wlr_seat_keyboard_notify_modifiers(wlr_seat, &wlr_device->keyboard->modifiers); + + uint32_t modifiers = wlr_keyboard_get_modifiers(wlr_device->keyboard); + determine_bar_visibility(modifiers); } struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, @@ -464,7 +480,7 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) { keyboard->keyboard_key.notify = handle_keyboard_key; wl_list_remove(&keyboard->keyboard_modifiers.link); - wl_signal_add( &wlr_device->keyboard->events.modifiers, + wl_signal_add(&wlr_device->keyboard->events.modifiers, &keyboard->keyboard_modifiers); keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers; } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 2d915502f..63c95503f 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -349,6 +349,22 @@ void ipc_event_barconfig_update(struct bar_config *bar) { json_object_put(json); } +void ipc_event_bar_state_update(struct bar_config *bar) { + if (!ipc_has_event_listeners(IPC_EVENT_BAR_STATE_UPDATE)) { + return; + } + wlr_log(WLR_DEBUG, "Sending bar_state_update event"); + + json_object *json = json_object_new_object(); + json_object_object_add(json, "id", json_object_new_string(bar->id)); + json_object_object_add(json, "visible_by_modifier", + json_object_new_boolean(bar->visible_by_modifier)); + + const char *json_string = json_object_to_json_string(json); + ipc_send_event(json_string, IPC_EVENT_BAR_STATE_UPDATE); + json_object_put(json); +} + void ipc_event_mode(const char *mode, bool pango) { if (!ipc_has_event_listeners(IPC_EVENT_MODE)) { return; @@ -651,6 +667,8 @@ void ipc_client_handle_command(struct ipc_client *client) { client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE); } else if (strcmp(event_type, "barconfig_update") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE); + } else if (strcmp(event_type, "bar_state_update") == 0) { + client->subscribed_events |= event_mask(IPC_EVENT_BAR_STATE_UPDATE); } else if (strcmp(event_type, "mode") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_MODE); } else if (strcmp(event_type, "shutdown") == 0) { diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 56379fdb9..c7fdffae3 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -367,7 +367,7 @@ bool ipc_initialize(struct swaybar *bar) { struct swaybar_config *config = bar->config; char subscribe[128]; // suitably large buffer len = snprintf(subscribe, 128, - "[ \"barconfig_update\" %s %s ]", + "[ \"barconfig_update\" , \"bar_state_update\" %s %s ]", config->binding_mode_indicator ? ", \"mode\"" : "", config->workspace_buttons ? ", \"workspace\"" : ""); free(ipc_single_command(bar->ipc_event_socketfd, @@ -375,6 +375,20 @@ bool ipc_initialize(struct swaybar *bar) { return true; } +static bool handle_bar_state_update(struct swaybar *bar, json_object *event) { + json_object *json_id; + json_object_object_get_ex(event, "id", &json_id); + const char *id = json_object_get_string(json_id); + if (strcmp(id, bar->id) != 0) { + return false; + } + + json_object *visible_by_modifier; + json_object_object_get_ex(event, "visible_by_modifier", &visible_by_modifier); + bar->visible_by_modifier = json_object_get_boolean(visible_by_modifier); + return determine_bar_visibility(bar, false); +} + static bool handle_barconfig_update(struct swaybar *bar, json_object *json_config) { json_object *json_id; @@ -444,6 +458,9 @@ bool handle_ipc_readable(struct swaybar *bar) { case IPC_EVENT_BARCONFIG_UPDATE: bar_is_dirty = handle_barconfig_update(bar, result); break; + case IPC_EVENT_BAR_STATE_UPDATE: + bar_is_dirty = handle_bar_state_update(bar, result); + break; default: bar_is_dirty = false; break;