2019-03-20 14:47:29 +11:00
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/desktop/transaction.h"
|
|
|
|
#include "sway/input/switch.h"
|
|
|
|
#include <wlr/types/wlr_idle.h>
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct sway_switch *sway_switch_create(struct sway_seat *seat,
|
2019-05-27 14:22:14 +10:00
|
|
|
struct sway_seat_device *device) {
|
|
|
|
struct sway_switch *switch_device =
|
|
|
|
calloc(1, sizeof(struct sway_switch));
|
|
|
|
if (!sway_assert(switch_device, "could not allocate switch")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
device->switch_device = switch_device;
|
|
|
|
switch_device->seat_device = device;
|
2019-07-09 16:57:59 +10:00
|
|
|
switch_device->state = WLR_SWITCH_STATE_OFF;
|
2019-05-27 14:22:14 +10:00
|
|
|
wl_list_init(&switch_device->switch_toggle.link);
|
|
|
|
sway_log(SWAY_DEBUG, "Allocated switch for device");
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-05-27 14:22:14 +10:00
|
|
|
return switch_device;
|
2019-03-20 14:47:29 +11:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:57:59 +10:00
|
|
|
static void execute_binding(struct sway_switch *sway_switch) {
|
2019-05-27 14:22:14 +10:00
|
|
|
struct sway_seat* seat = sway_switch->seat_device->sway_seat;
|
|
|
|
bool input_inhibited = seat->exclusive_client != NULL;
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-05-27 14:22:14 +10:00
|
|
|
list_t *bindings = config->current_mode->switch_bindings;
|
2019-05-30 17:30:08 +10:00
|
|
|
struct sway_switch_binding *matched_binding = NULL;
|
2019-05-27 14:22:14 +10:00
|
|
|
for (int i = 0; i < bindings->length; ++i) {
|
|
|
|
struct sway_switch_binding *binding = bindings->items[i];
|
2019-07-09 16:57:59 +10:00
|
|
|
if (binding->type != sway_switch->type) {
|
2019-05-27 14:22:14 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (binding->state != WLR_SWITCH_STATE_TOGGLE &&
|
2019-07-09 16:57:59 +10:00
|
|
|
binding->state != sway_switch->state) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (config->reloading && (binding->state == WLR_SWITCH_STATE_TOGGLE
|
|
|
|
|| (binding->flags & BINDING_RELOAD) == 0)) {
|
2019-05-27 14:22:14 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool binding_locked = binding->flags & BINDING_LOCKED;
|
|
|
|
if (!binding_locked && input_inhibited) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-05-30 17:30:08 +10:00
|
|
|
matched_binding = binding;
|
|
|
|
|
|
|
|
if (binding_locked == input_inhibited) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched_binding) {
|
|
|
|
struct sway_binding *dummy_binding =
|
|
|
|
calloc(1, sizeof(struct sway_binding));
|
2019-05-27 14:22:14 +10:00
|
|
|
dummy_binding->type = BINDING_SWITCH;
|
2019-05-30 17:30:08 +10:00
|
|
|
dummy_binding->flags = matched_binding->flags;
|
|
|
|
dummy_binding->command = matched_binding->command;
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-05-27 14:22:14 +10:00
|
|
|
seat_execute_command(seat, dummy_binding);
|
|
|
|
free(dummy_binding);
|
|
|
|
}
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-05-27 14:22:14 +10:00
|
|
|
transaction_commit_dirty();
|
2019-03-20 14:47:29 +11:00
|
|
|
|
2019-07-09 16:57:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-05-27 14:22:14 +10:00
|
|
|
free(device_identifier);
|
2019-07-09 16:57:59 +10:00
|
|
|
|
|
|
|
sway_switch->type = event->switch_type;
|
|
|
|
sway_switch->state = event->switch_state;
|
|
|
|
execute_binding(sway_switch);
|
2019-03-20 14:47:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
void sway_switch_configure(struct sway_switch *sway_switch) {
|
2019-05-27 14:22:14 +10:00
|
|
|
struct wlr_input_device *wlr_device =
|
|
|
|
sway_switch->seat_device->input_device->wlr_device;
|
|
|
|
wl_list_remove(&sway_switch->switch_toggle.link);
|
|
|
|
wl_signal_add(&wlr_device->switch_device->events.toggle,
|
|
|
|
&sway_switch->switch_toggle);
|
|
|
|
sway_switch->switch_toggle.notify = handle_switch_toggle;
|
|
|
|
sway_log(SWAY_DEBUG, "Configured switch for device");
|
2019-03-20 14:47:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
void sway_switch_destroy(struct sway_switch *sway_switch) {
|
2019-05-27 14:22:14 +10:00
|
|
|
if (!sway_switch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_list_remove(&sway_switch->switch_toggle.link);
|
|
|
|
free(sway_switch);
|
2019-03-20 14:47:29 +11:00
|
|
|
}
|
2019-07-09 16:57:59 +10:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|