diff --git a/include/kiwmi/input/keyboard.h b/include/kiwmi/input/keyboard.h new file mode 100644 index 0000000..32db941 --- /dev/null +++ b/include/kiwmi/input/keyboard.h @@ -0,0 +1,27 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef KIWMI_INPUT_KEYBOARD_H +#define KIWMI_INPUT_KEYBOARD_H + +#include +#include + +#include "kiwmi/server.h" + +struct kiwmi_keyboard { + struct wl_list link; + struct kiwmi_server *server; + struct wlr_input_device *device; + struct wl_listener modifiers; + struct wl_listener key; +}; + +struct kiwmi_keyboard * +keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device); + +#endif /* KIWMI_INPUT_KEYBOARD_H */ diff --git a/include/kiwmi/server.h b/include/kiwmi/server.h index 04155cb..15e8318 100644 --- a/include/kiwmi/server.h +++ b/include/kiwmi/server.h @@ -23,6 +23,7 @@ struct kiwmi_server { struct wlr_data_device_manager *data_device_manager; struct wlr_output_layout *output_layout; const char *socket; + struct wl_list keyboards; // struct kiwmi_keyboard::link struct wl_list outputs; // struct kiwmi_output::link struct wl_listener new_output; struct kiwmi_cursor *cursor; diff --git a/kiwmi/input.c b/kiwmi/input.c index aba6882..9e56d53 100644 --- a/kiwmi/input.c +++ b/kiwmi/input.c @@ -11,6 +11,7 @@ #include #include +#include "kiwmi/input/keyboard.h" #include "kiwmi/server.h" static void @@ -19,6 +20,17 @@ new_pointer(struct kiwmi_server *server, struct wlr_input_device *device) wlr_cursor_attach_input_device(server->cursor->cursor, device); } +static void +new_keyboard(struct kiwmi_server *server, struct wlr_input_device *device) +{ + struct kiwmi_keyboard *keyboard = keyboard_create(server, device); + if (!keyboard) { + return; + } + + wl_list_insert(&server->keyboards, &keyboard->link); +} + void new_input_notify(struct wl_listener *listener, void *data) { @@ -31,6 +43,9 @@ new_input_notify(struct wl_listener *listener, void *data) case WLR_INPUT_DEVICE_POINTER: new_pointer(server, device); break; + case WLR_INPUT_DEVICE_KEYBOARD: + new_keyboard(server, device); + break; default: // NOT HANDLED break; diff --git a/kiwmi/input/keyboard.c b/kiwmi/input/keyboard.c new file mode 100644 index 0000000..a5c23e5 --- /dev/null +++ b/kiwmi/input/keyboard.c @@ -0,0 +1,113 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "kiwmi/input/keyboard.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "kiwmi/server.h" + +static bool +switch_vt(const xkb_keysym_t *syms, int nsyms, struct wlr_backend *backend) +{ + for (int i = 0; i < nsyms; ++i) { + const xkb_keysym_t sym = syms[i]; + + if (sym >= XKB_KEY_XF86Switch_VT_1 && sym <= XKB_KEY_XF86Switch_VT_12) { + if (wlr_backend_is_multi(backend)) { + struct wlr_session *session = wlr_backend_get_session(backend); + if (session) { + unsigned vt = sym - XKB_KEY_XF86Switch_VT_1 + 1; + wlr_session_change_vt(session, vt); + } + } + return true; + } + } + + return false; +} + +static void +keyboard_modifiers_notify( + struct wl_listener *UNUSED(listener), + void *UNUSED(data)) +{ +} + +static void +keyboard_key_notify(struct wl_listener *listener, void *data) +{ + struct kiwmi_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct kiwmi_server *server = keyboard->server; + struct wlr_event_keyboard_key *event = data; + + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms( + keyboard->device->keyboard->xkb_state, keycode, &syms); + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); + + bool handled = false; + + if (event->state == WLR_KEY_PRESSED) { + handled = switch_vt(syms, nsyms, server->backend); + + if (!handled && (modifiers & WLR_MODIFIER_LOGO)) { + for (int i = 0; i < nsyms; ++i) { + xkb_keysym_t sym = syms[i]; + + switch (sym) { + case XKB_KEY_Escape: + wl_display_terminate(server->wl_display); + handled = true; + break; + } + } + } + } +} + +struct kiwmi_keyboard * +keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) +{ + wlr_log(WLR_DEBUG, "Creating keyboard"); + + struct kiwmi_keyboard *keyboard = malloc(sizeof(*keyboard)); + if (!keyboard) { + return NULL; + } + + keyboard->server = server; + keyboard->device = device; + + struct xkb_rule_names rules = {0}; + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_keymap *keymap = + xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); + + keyboard->modifiers.notify = keyboard_modifiers_notify; + wl_signal_add(&device->keyboard->events.modifiers, &keyboard->modifiers); + + keyboard->key.notify = keyboard_key_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + + wlr_keyboard_set_keymap(device->keyboard, keymap); + xkb_keymap_unref(keymap); + xkb_context_unref(context); + wlr_keyboard_set_repeat_info(device->keyboard, 25, 600); + + return keyboard; +} diff --git a/kiwmi/meson.build b/kiwmi/meson.build index 2af4254..dbc70aa 100644 --- a/kiwmi/meson.build +++ b/kiwmi/meson.build @@ -1,15 +1,17 @@ kiwmi_sources = files( 'main.c', - 'server.c', 'input.c', 'output.c', + 'server.c', 'desktop/output.c', 'input/cursor.c', + 'input/keyboard.c', ) kiwmi_deps = [ wayland_server, wlroots, + xkbcommon, ] executable( diff --git a/kiwmi/server.c b/kiwmi/server.c index d531739..3946681 100644 --- a/kiwmi/server.c +++ b/kiwmi/server.c @@ -53,6 +53,7 @@ server_init(struct kiwmi_server *server) return false; } + wl_list_init(&server->keyboards); wl_list_init(&server->outputs); server->new_output.notify = new_output_notify; diff --git a/meson.build b/meson.build index a3b3e08..7eaf74b 100644 --- a/meson.build +++ b/meson.build @@ -16,9 +16,10 @@ add_project_arguments( language: 'c', ) -git = find_program('git', required: false) +git = find_program('git', required: false) wayland_server = dependency('wayland-server') -wlroots = dependency('wlroots') +wlroots = dependency('wlroots') +xkbcommon = dependency('xkbcommon') include = include_directories('include')