2019-03-20 04:47:29 +01:00
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/input/switch.h"
|
2024-05-25 00:02:48 +02:00
|
|
|
#include "sway/server.h"
|
2019-03-20 04:47:29 +01:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct sway_switch *sway_switch_create(struct sway_seat *seat,
|
2019-05-27 06:22:14 +02: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;
|
2022-06-21 23:10:38 +02:00
|
|
|
switch_device->wlr = wlr_switch_from_input_device(device->input_device->wlr_device);
|
2019-05-27 06:22:14 +02:00
|
|
|
switch_device->seat_device = device;
|
2019-07-09 08:57:59 +02:00
|
|
|
switch_device->state = WLR_SWITCH_STATE_OFF;
|
2019-05-27 06:22:14 +02:00
|
|
|
wl_list_init(&switch_device->switch_toggle.link);
|
|
|
|
sway_log(SWAY_DEBUG, "Allocated switch for device");
|
2019-03-20 04:47:29 +01:00
|
|
|
|
2019-05-27 06:22:14 +02:00
|
|
|
return switch_device;
|
2019-03-20 04:47:29 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:24:27 +01:00
|
|
|
static bool sway_switch_trigger_test(enum sway_switch_trigger trigger,
|
|
|
|
enum wlr_switch_state state) {
|
|
|
|
switch (trigger) {
|
|
|
|
case SWAY_SWITCH_TRIGGER_ON:
|
|
|
|
return state == WLR_SWITCH_STATE_ON;
|
|
|
|
case SWAY_SWITCH_TRIGGER_OFF:
|
|
|
|
return state == WLR_SWITCH_STATE_OFF;
|
|
|
|
case SWAY_SWITCH_TRIGGER_TOGGLE:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
abort(); // unreachable
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:57:59 +02:00
|
|
|
static void execute_binding(struct sway_switch *sway_switch) {
|
2023-12-04 15:53:57 +01:00
|
|
|
struct sway_seat *seat = sway_switch->seat_device->sway_seat;
|
2024-01-18 16:04:26 +01:00
|
|
|
bool locked = server.session_lock.lock;
|
2019-03-20 04:47:29 +01:00
|
|
|
|
2019-05-27 06:22:14 +02:00
|
|
|
list_t *bindings = config->current_mode->switch_bindings;
|
2019-05-30 09:30:08 +02:00
|
|
|
struct sway_switch_binding *matched_binding = NULL;
|
2019-05-27 06:22:14 +02:00
|
|
|
for (int i = 0; i < bindings->length; ++i) {
|
|
|
|
struct sway_switch_binding *binding = bindings->items[i];
|
2019-07-09 08:57:59 +02:00
|
|
|
if (binding->type != sway_switch->type) {
|
2019-05-27 06:22:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-08 16:24:27 +01:00
|
|
|
if (!sway_switch_trigger_test(binding->trigger, sway_switch->state)) {
|
2019-07-09 08:57:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-08 16:24:27 +01:00
|
|
|
if (config->reloading && (binding->trigger == SWAY_SWITCH_TRIGGER_TOGGLE
|
2019-07-09 08:57:59 +02:00
|
|
|
|| (binding->flags & BINDING_RELOAD) == 0)) {
|
2019-05-27 06:22:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool binding_locked = binding->flags & BINDING_LOCKED;
|
2023-12-04 15:53:57 +01:00
|
|
|
if (!binding_locked && locked) {
|
2019-05-27 06:22:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-20 04:47:29 +01:00
|
|
|
|
2019-05-30 09:30:08 +02:00
|
|
|
matched_binding = binding;
|
|
|
|
|
2023-12-04 15:53:57 +01:00
|
|
|
if (binding_locked == locked) {
|
2019-05-30 09:30:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched_binding) {
|
|
|
|
struct sway_binding *dummy_binding =
|
|
|
|
calloc(1, sizeof(struct sway_binding));
|
2019-05-27 06:22:14 +02:00
|
|
|
dummy_binding->type = BINDING_SWITCH;
|
2019-05-30 09:30:08 +02:00
|
|
|
dummy_binding->flags = matched_binding->flags;
|
2020-03-23 00:18:29 +01:00
|
|
|
dummy_binding->command = matched_binding->command;
|
2019-03-20 04:47:29 +01:00
|
|
|
|
2019-05-27 06:22:14 +02:00
|
|
|
seat_execute_command(seat, dummy_binding);
|
|
|
|
free(dummy_binding);
|
|
|
|
}
|
2019-07-09 08:57:59 +02: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);
|
2022-03-09 22:50:20 +01:00
|
|
|
struct wlr_switch_toggle_event *event = data;
|
2019-12-11 17:00:39 +01:00
|
|
|
struct sway_seat *seat = sway_switch->seat_device->sway_seat;
|
|
|
|
seat_idle_notify_activity(seat, IDLE_SOURCE_SWITCH);
|
2019-07-09 08:57:59 +02:00
|
|
|
|
|
|
|
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 06:22:14 +02:00
|
|
|
free(device_identifier);
|
2019-07-09 08:57:59 +02:00
|
|
|
|
|
|
|
sway_switch->type = event->switch_type;
|
|
|
|
sway_switch->state = event->switch_state;
|
|
|
|
execute_binding(sway_switch);
|
2019-03-20 04:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sway_switch_configure(struct sway_switch *sway_switch) {
|
2019-05-27 06:22:14 +02:00
|
|
|
wl_list_remove(&sway_switch->switch_toggle.link);
|
2022-06-21 23:10:38 +02:00
|
|
|
wl_signal_add(&sway_switch->wlr->events.toggle,
|
2019-05-27 06:22:14 +02:00
|
|
|
&sway_switch->switch_toggle);
|
|
|
|
sway_switch->switch_toggle.notify = handle_switch_toggle;
|
|
|
|
sway_log(SWAY_DEBUG, "Configured switch for device");
|
2019-03-20 04:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sway_switch_destroy(struct sway_switch *sway_switch) {
|
2019-05-27 06:22:14 +02:00
|
|
|
if (!sway_switch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_list_remove(&sway_switch->switch_toggle.link);
|
|
|
|
free(sway_switch);
|
2019-03-20 04:47:29 +01:00
|
|
|
}
|
2019-07-09 08:57:59 +02: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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|