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.
This commit is contained in:
Brian Ashworth 2019-07-09 02:57:59 -04:00 committed by Simon Ser
parent 538b36c0e2
commit 152e30c374
6 changed files with 63 additions and 26 deletions

View file

@ -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);
};
}
}