From 152e30c374382eecccb917e5c26d72a2ea53ef87 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Tue, 9 Jul 2019 02:57:59 -0400 Subject: cmd_bindswitch: add option to execute on reload This adds a --reload flag to cmd_bindswitch that allows for the binding to be executed on reload. One possible use case for this is to allow users to disable outputs when the lid closes and enable them when the lid opens without having to open and re-close the lid after a reload. --- include/sway/config.h | 1 + include/sway/input/switch.h | 10 +++++--- sway/commands/bind.c | 2 ++ sway/config.c | 12 ++++++---- sway/input/switch.c | 57 +++++++++++++++++++++++++++++++-------------- sway/sway.5.scd | 7 +++++- 6 files changed, 63 insertions(+), 26 deletions(-) diff --git a/include/sway/config.h b/include/sway/config.h index 4adce8ab..c5558c0d 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -39,6 +39,7 @@ enum binding_flags { BINDING_CONTENTS=8, // mouse only; trigger on container contents BINDING_TITLEBAR=16, // mouse only; trigger on container titlebar BINDING_CODE=32, // keyboard only; convert keysyms into keycodes + BINDING_RELOAD=62, // switch only; (re)trigger binding on reload }; /** diff --git a/include/sway/input/switch.h b/include/sway/input/switch.h index 19bb1e77..213b471d 100644 --- a/include/sway/input/switch.h +++ b/include/sway/input/switch.h @@ -4,16 +4,20 @@ #include "sway/input/seat.h" struct sway_switch { - struct sway_seat_device *seat_device; + struct sway_seat_device *seat_device; + enum wlr_switch_state state; + enum wlr_switch_type type; - struct wl_listener switch_toggle; + struct wl_listener switch_toggle; }; struct sway_switch *sway_switch_create(struct sway_seat *seat, - struct sway_seat_device *device); + struct sway_seat_device *device); void sway_switch_configure(struct sway_switch *sway_switch); void sway_switch_destroy(struct sway_switch *sway_switch); +void sway_switch_retrigger_bindings_for_all(void); + #endif diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 49b511ad..767c2fee 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -478,6 +478,8 @@ struct cmd_results *cmd_bind_or_unbind_switch(int argc, char **argv, binding->flags |= BINDING_LOCKED; } else if (strcmp("--no-warn", argv[0]) == 0) { warn = false; + } else if (strcmp("--reload", argv[0]) == 0) { + binding->flags |= BINDING_RELOAD; } else { break; } diff --git a/sway/config.c b/sway/config.c index 70f2d706..74080898 100644 --- a/sway/config.c +++ b/sway/config.c @@ -17,6 +17,7 @@ #include #include "sway/input/input-manager.h" #include "sway/input/seat.h" +#include "sway/input/switch.h" #include "sway/commands.h" #include "sway/config.h" #include "sway/criteria.h" @@ -520,6 +521,12 @@ bool load_main_config(const char *file, bool is_active, bool validating) { } if (is_active) { + input_manager_verify_fallback_seat(); + for (int i = 0; i < config->seat_configs->length; i++) { + input_manager_apply_seat_config(config->seat_configs->items[i]); + } + sway_switch_retrigger_bindings_for_all(); + reset_outputs(); spawn_swaybg(); @@ -527,11 +534,6 @@ bool load_main_config(const char *file, bool is_active, bool validating) { if (config->swaynag_config_errors.client != NULL) { swaynag_show(&config->swaynag_config_errors); } - - input_manager_verify_fallback_seat(); - for (int i = 0; i < config->seat_configs->length; i++) { - input_manager_apply_seat_config(config->seat_configs->items[i]); - } } if (old_config) { diff --git a/sway/input/switch.c b/sway/input/switch.c index d825c5c3..72d1245f 100644 --- a/sway/input/switch.c +++ b/sway/input/switch.c @@ -13,39 +13,30 @@ struct sway_switch *sway_switch_create(struct sway_seat *seat, } device->switch_device = switch_device; switch_device->seat_device = device; + switch_device->state = WLR_SWITCH_STATE_OFF; wl_list_init(&switch_device->switch_toggle.link); sway_log(SWAY_DEBUG, "Allocated switch for device"); return switch_device; } -static void handle_switch_toggle(struct wl_listener *listener, void *data) { - struct sway_switch *sway_switch = - wl_container_of(listener, sway_switch, switch_toggle); +static void execute_binding(struct sway_switch *sway_switch) { struct sway_seat* seat = sway_switch->seat_device->sway_seat; - struct wlr_seat *wlr_seat = seat->wlr_seat; - struct wlr_input_device *wlr_device = - sway_switch->seat_device->input_device->wlr_device; - - wlr_idle_notify_activity(server.idle, wlr_seat); bool input_inhibited = seat->exclusive_client != NULL; - char *device_identifier = input_device_get_identifier(wlr_device); - - struct wlr_event_switch_toggle *event = data; - enum wlr_switch_type type = event->switch_type; - enum wlr_switch_state state = event->switch_state; - sway_log(SWAY_DEBUG, "%s: type %d state %d", device_identifier, type, state); - list_t *bindings = config->current_mode->switch_bindings; struct sway_switch_binding *matched_binding = NULL; for (int i = 0; i < bindings->length; ++i) { struct sway_switch_binding *binding = bindings->items[i]; - if (binding->type != type) { + if (binding->type != sway_switch->type) { continue; } if (binding->state != WLR_SWITCH_STATE_TOGGLE && - binding->state != state) { + binding->state != sway_switch->state) { + continue; + } + if (config->reloading && (binding->state == WLR_SWITCH_STATE_TOGGLE + || (binding->flags & BINDING_RELOAD) == 0)) { continue; } bool binding_locked = binding->flags & BINDING_LOCKED; @@ -73,7 +64,25 @@ static void handle_switch_toggle(struct wl_listener *listener, void *data) { transaction_commit_dirty(); +} + +static void handle_switch_toggle(struct wl_listener *listener, void *data) { + struct sway_switch *sway_switch = + wl_container_of(listener, sway_switch, switch_toggle); + struct wlr_event_switch_toggle *event = data; + struct wlr_seat* wlr_seat = sway_switch->seat_device->sway_seat->wlr_seat; + wlr_idle_notify_activity(server.idle, wlr_seat); + + struct wlr_input_device *wlr_device = + sway_switch->seat_device->input_device->wlr_device; + char *device_identifier = input_device_get_identifier(wlr_device); + sway_log(SWAY_DEBUG, "%s: type %d state %d", device_identifier, + event->switch_type, event->switch_state); free(device_identifier); + + sway_switch->type = event->switch_type; + sway_switch->state = event->switch_state; + execute_binding(sway_switch); } void sway_switch_configure(struct sway_switch *sway_switch) { @@ -93,3 +102,17 @@ void sway_switch_destroy(struct sway_switch *sway_switch) { wl_list_remove(&sway_switch->switch_toggle.link); free(sway_switch); } + +void sway_switch_retrigger_bindings_for_all(void) { + struct sway_seat *seat; + wl_list_for_each(seat, &server.input->seats, link) { + struct sway_seat_device *seat_device; + wl_list_for_each(seat_device, &seat->devices, link) { + struct sway_input_device *input_device = seat_device->input_device; + if (input_device->wlr_device->type != WLR_INPUT_DEVICE_SWITCH) { + continue; + } + execute_binding(seat_device->switch_device); + }; + } +} diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 90d7fed8..ca47c1f9 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -378,7 +378,7 @@ runtime. *bindcode* [--whole-window] [--border] [--exclude-titlebar] [--release] [--locked] [--input-device=] [--no-warn] is also available for binding with key/button codes instead of key/button names. -*bindswitch* [--locked] [--no-warn] : +*bindswitch* [--locked] [--no-warn] [--reload] : Binds to execute the sway command _command_ on state changes. Supported switches are _lid_ (laptop lid) and _tablet_ (tablet mode) switches. Valid values for _state_ are _on_, _off_ and _toggle_. These @@ -391,6 +391,11 @@ runtime. and without _--locked_, the one with will be preferred when locked and the one without will be preferred when unlocked. + If the _--reload_ flag is given, the binding will also be executed when + the config is reloaded. _toggle_ bindings will not be executed on reload. + The _--locked_ flag will operate as normal so if the config is reloaded + while locked and _--locked_ is not given, the binding will not be executed. + By default, if you overwrite a binding, swaynag will give you a warning. To silence this, use the _--no-warn_ flag. -- cgit v1.2.3-70-g09d2