aboutsummaryrefslogtreecommitdiff
path: root/sway/input
diff options
context:
space:
mode:
authorRyan Walklin <ryan@testtoast.com>2019-03-20 14:47:29 +1100
committerBrian Ashworth <bosrsf04@gmail.com>2019-03-19 23:58:47 -0400
commitbdb402404cd6d54242b0b1dc2360cfc5679e52f2 (patch)
tree5a355e025c24b3de0bc69db4b8cc9d002bbd1167 /sway/input
parentbfa20e65d846f8e8bf7b967b2440d99d82ca9a86 (diff)
Support WLR_INPUT_DEVICE_SWITCH in sway
This commit adds support for laptop lid and tablet mode switches as provided by evdev/libinput and handled by wlroots. Adds a new bindswitch command with syntax: bindswitch <switch>:<state> <command> Where <switch> is one of: tablet for WLR_SWITCH_TYPE_TABLET_MODE lid for WLR_SWITCH_TYPE_LID <state> is one of: on for WLR_SWITCH_STATE_ON off for WLR_SWITCH_STATE_OFF toggle for WLR_SWITCH_STATE_TOGGLE (Note that WLR_SWITCH_STATE_TOGGLE doesn't map to libinput and will trigger at both on and off events)
Diffstat (limited to 'sway/input')
-rw-r--r--sway/input/input-manager.c47
-rw-r--r--sway/input/seat.c18
-rw-r--r--sway/input/switch.c85
3 files changed, 146 insertions, 4 deletions
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
index f99fc395..adb36af9 100644
--- a/sway/input/input-manager.c
+++ b/sway/input/input-manager.c
@@ -155,6 +155,47 @@ static void input_manager_libinput_reset_keyboard(
libinput_device, send_events));
}
+static void input_manager_libinput_config_switch(
+ struct sway_input_device *input_device) {
+ struct wlr_input_device *wlr_device = input_device->wlr_device;
+ struct input_config *ic = input_device_get_config(input_device);
+ struct libinput_device *libinput_device;
+
+ if (!ic || !wlr_input_device_is_libinput(wlr_device)) {
+ return;
+ }
+
+ libinput_device = wlr_libinput_get_device_handle(wlr_device);
+ sway_log(SWAY_DEBUG, "input_manager_libinput_config_switch(%s)",
+ ic->identifier);
+
+ if (ic->send_events != INT_MIN) {
+ sway_log(SWAY_DEBUG, "libinput_config_switch(%s) send_events_set_mode(%d)",
+ ic->identifier, ic->send_events);
+ log_libinput_config_status(libinput_device_config_send_events_set_mode(
+ libinput_device, ic->send_events));
+ }
+}
+
+static void input_manager_libinput_reset_switch(
+ struct sway_input_device *input_device) {
+ struct wlr_input_device *wlr_device = input_device->wlr_device;
+ struct libinput_device *libinput_device;
+
+ if (!wlr_input_device_is_libinput(wlr_device)) {
+ return;
+ }
+
+ libinput_device = wlr_libinput_get_device_handle(wlr_device);
+
+ uint32_t send_events =
+ libinput_device_config_send_events_get_default_mode(libinput_device);
+ sway_log(SWAY_DEBUG, "libinput_reset_switch(%s) send_events_set_mode(%d)",
+ input_device->identifier, send_events);
+ log_libinput_config_status(libinput_device_config_send_events_set_mode(
+ libinput_device, send_events));
+}
+
static void input_manager_libinput_config_touch(
struct sway_input_device *input_device) {
struct wlr_input_device *wlr_device = input_device->wlr_device;
@@ -471,6 +512,8 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
input_manager_libinput_config_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_config_keyboard(input_device);
+ } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
+ input_manager_libinput_config_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_config_touch(input_device);
}
@@ -624,6 +667,8 @@ void input_manager_apply_input_config(struct input_config *input_config) {
input_manager_libinput_config_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_config_keyboard(input_device);
+ } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
+ input_manager_libinput_config_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_config_touch(input_device);
}
@@ -642,6 +687,8 @@ void input_manager_reset_input(struct sway_input_device *input_device) {
input_manager_libinput_reset_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_reset_keyboard(input_device);
+ } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
+ input_manager_libinput_reset_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_reset_touch(input_device);
}
diff --git a/sway/input/seat.c b/sway/input/seat.c
index 7fc87ee4..d58ff9e6 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -15,6 +15,7 @@
#include "sway/input/input-manager.h"
#include "sway/input/keyboard.h"
#include "sway/input/seat.h"
+#include "sway/input/switch.h"
#include "sway/ipc-server.h"
#include "sway/layers.h"
#include "sway/output.h"
@@ -482,8 +483,8 @@ static void seat_update_capabilities(struct sway_seat *seat) {
case WLR_INPUT_DEVICE_TABLET_TOOL:
caps |= WL_SEAT_CAPABILITY_POINTER;
break;
- case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_SWITCH:
+ case WLR_INPUT_DEVICE_TABLET_PAD:
break;
}
}
@@ -570,6 +571,15 @@ static void seat_configure_keyboard(struct sway_seat *seat,
}
}
+static void seat_configure_switch(struct sway_seat *seat,
+ struct sway_seat_device *seat_device) {
+ if (!seat_device->switch_device) {
+ sway_switch_create(seat, seat_device);
+ }
+ seat_apply_input_config(seat, seat_device);
+ sway_switch_configure(seat_device->switch_device);
+}
+
static void seat_configure_touch(struct sway_seat *seat,
struct sway_seat_device *sway_device) {
wlr_cursor_attach_input_device(seat->cursor->cursor,
@@ -611,6 +621,9 @@ void seat_configure_device(struct sway_seat *seat,
case WLR_INPUT_DEVICE_KEYBOARD:
seat_configure_keyboard(seat, seat_device);
break;
+ case WLR_INPUT_DEVICE_SWITCH:
+ seat_configure_switch(seat, seat_device);
+ break;
case WLR_INPUT_DEVICE_TOUCH:
seat_configure_touch(seat, seat_device);
break;
@@ -620,9 +633,6 @@ void seat_configure_device(struct sway_seat *seat,
case WLR_INPUT_DEVICE_TABLET_PAD:
sway_log(SWAY_DEBUG, "TODO: configure tablet pad");
break;
- case WLR_INPUT_DEVICE_SWITCH:
- sway_log(SWAY_DEBUG, "TODO: configure switch device");
- break;
}
}
diff --git a/sway/input/switch.c b/sway/input/switch.c
new file mode 100644
index 00000000..d56e6525
--- /dev/null
+++ b/sway/input/switch.c
@@ -0,0 +1,85 @@
+#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,
+ 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;
+ 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);
+ 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;
+ for (int i = 0; i < bindings->length; ++i) {
+ struct sway_switch_binding *binding = bindings->items[i];
+ if (binding->type != type) {
+ continue;
+ }
+ if (binding->state != WLR_SWITCH_STATE_TOGGLE &&
+ binding->state != state) {
+ continue;
+ }
+ bool binding_locked = binding->flags & BINDING_LOCKED;
+ if (!binding_locked && input_inhibited) {
+ continue;
+ }
+
+ struct sway_binding *dummy_binding = calloc(1, sizeof(struct sway_binding));
+ dummy_binding->type = BINDING_SWITCH;
+ dummy_binding->flags = binding->flags;
+ dummy_binding->command = binding->command;
+
+ seat_execute_command(seat, dummy_binding);
+ free(dummy_binding);
+ }
+
+ transaction_commit_dirty();
+
+ free(device_identifier);
+}
+
+void sway_switch_configure(struct sway_switch *sway_switch) {
+ 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");
+}
+
+void sway_switch_destroy(struct sway_switch *sway_switch) {
+ if (!sway_switch) {
+ return;
+ }
+ wl_list_remove(&sway_switch->switch_toggle.link);
+ free(sway_switch);
+}