diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h new file mode 100644 index 00000000..2e04065c --- /dev/null +++ b/include/sway/input/keyboard.h @@ -0,0 +1,13 @@ +#include "sway/input/seat.h" + +struct sway_keyboard { + struct sway_seat *seat; + struct wlr_input_device *device; + struct wl_list link; // sway_seat::keyboards + + struct wl_listener keyboard_key; + struct wl_listener keyboard_modifiers; +}; + +struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, + struct wlr_input_device *device); diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h index 964c0f7b..a0c6ab07 100644 --- a/include/sway/input/seat.h +++ b/include/sway/input/seat.h @@ -10,6 +10,8 @@ struct sway_seat { struct sway_input_manager *input; swayc_t *focus; + struct wl_list keyboards; + struct wl_listener focus_destroy; }; diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c new file mode 100644 index 00000000..59f81e62 --- /dev/null +++ b/sway/input/keyboard.c @@ -0,0 +1,57 @@ +#include "sway/input/seat.h" +#include "sway/input/keyboard.h" +#include "log.h" + +static void handle_keyboard_key(struct wl_listener *listener, void *data) { + struct sway_keyboard *keyboard = + wl_container_of(listener, keyboard, keyboard_key); + struct wlr_event_keyboard_key *event = data; + wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device); + wlr_seat_keyboard_notify_key(keyboard->seat->seat, event->time_msec, + event->keycode, event->state); +} + +static void handle_keyboard_modifiers(struct wl_listener *listener, + void *data) { + struct sway_keyboard *keyboard = + wl_container_of(listener, keyboard, keyboard_modifiers); + wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device); + wlr_seat_keyboard_notify_modifiers(keyboard->seat->seat); +} + +struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, + struct wlr_input_device *device) { + struct sway_keyboard *keyboard = + calloc(1, sizeof(struct sway_keyboard)); + if (!sway_assert(keyboard, "could not allocate sway keyboard")) { + return NULL; + } + + keyboard->device = device; + keyboard->seat = seat; + + // TODO keyboard config + struct xkb_rule_names rules; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!sway_assert(context, "cannot create XKB context")) { + return NULL; + } + + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + + wl_signal_add(&device->keyboard->events.key, &keyboard->keyboard_key); + keyboard->keyboard_key.notify = handle_keyboard_key; + + wl_signal_add(&device->keyboard->events.modifiers, &keyboard->keyboard_modifiers); + keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers; + + return keyboard; +} diff --git a/sway/input/seat.c b/sway/input/seat.c index b3d36681..0a5329c8 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -4,6 +4,7 @@ #include "sway/input/seat.h" #include "sway/input/cursor.h" #include "sway/input/input-manager.h" +#include "sway/input/keyboard.h" #include "sway/output.h" #include "sway/view.h" #include "log.h" @@ -36,6 +37,8 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input, sway_seat_configure_xcursor(seat); + wl_list_init(&seat->keyboards); + return seat; } @@ -45,6 +48,13 @@ static void seat_add_pointer(struct sway_seat *seat, wlr_cursor_attach_input_device(seat->cursor->cursor, device); } +static void seat_add_keyboard(struct sway_seat *seat, + struct wlr_input_device *device) { + struct sway_keyboard *keyboard = sway_keyboard_create(seat, device); + wl_list_insert(&seat->keyboards, &keyboard->link); + wlr_seat_set_keyboard(seat->seat, device); +} + void sway_seat_add_device(struct sway_seat *seat, struct wlr_input_device *device) { sway_log(L_DEBUG, "input add: %s", device->name); @@ -53,6 +63,8 @@ void sway_seat_add_device(struct sway_seat *seat, seat_add_pointer(seat, device); break; case WLR_INPUT_DEVICE_KEYBOARD: + seat_add_keyboard(seat, device); + break; case WLR_INPUT_DEVICE_TOUCH: case WLR_INPUT_DEVICE_TABLET_PAD: case WLR_INPUT_DEVICE_TABLET_TOOL: @@ -138,7 +150,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) { view->iface.set_activated(view, true); wl_signal_add(&container->events.destroy, &seat->focus_destroy); seat->focus_destroy.notify = handle_focus_destroy; - // TODO give keyboard focus + wlr_seat_keyboard_notify_enter(seat->seat, view->surface); } seat->focus = container; diff --git a/sway/meson.build b/sway/meson.build index 18955693..79201f3a 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -5,6 +5,7 @@ sway_sources = files( 'input/input-manager.c', 'input/seat.c', 'input/cursor.c', + 'input/keyboard.c', 'commands/exit.c', 'commands/exec.c', 'commands/exec_always.c', @@ -30,6 +31,7 @@ sway_deps = [ libcap, math, libinput, + xkbcommon, ] executable(