2024-03-30 00:18:51 +01:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
|
|
|
#include <wlr/types/wlr_input_device.h>
|
|
|
|
#include <wlr/types/wlr_data_device.h>
|
|
|
|
#include <stdlib.h>
|
2024-03-31 16:09:01 +02:00
|
|
|
#include <wlr/util/log.h>
|
2024-03-30 00:18:51 +01:00
|
|
|
#include "seat.h"
|
2024-04-01 15:38:57 +02:00
|
|
|
#include "view.h"
|
2024-03-31 16:09:01 +02:00
|
|
|
|
2024-03-30 00:18:51 +01:00
|
|
|
|
|
|
|
static void keyboard_handle_modifiers(
|
|
|
|
struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
/* This event is raised when a modifier key, such as shift or alt, is
|
|
|
|
* pressed. We simply communicate this to the client. */
|
|
|
|
struct diyac_keyboard *keyboard =
|
|
|
|
wl_container_of(listener, keyboard, modifiers);
|
|
|
|
/*
|
|
|
|
* A seat can only have one keyboard, but this is a limitation of the
|
|
|
|
* Wayland protocol - not wlroots. We assign all connected keyboards to the
|
|
|
|
* same seat. You can swap out the underlying wlr_keyboard like this and
|
|
|
|
* wlr_seat handles this transparently.
|
|
|
|
*/
|
|
|
|
wlr_seat_set_keyboard(keyboard->server->seat.wlr_seat, keyboard->wlr_keyboard);
|
|
|
|
/* Send modifiers to the client. */
|
|
|
|
wlr_seat_keyboard_notify_modifiers(keyboard->server->seat.wlr_seat,
|
|
|
|
&keyboard->wlr_keyboard->modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool handle_keybinding(struct diyac_server *server, xkb_keysym_t sym)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Here we handle compositor keybindings. This is when the compositor is
|
|
|
|
* processing keys, rather than passing them on to the client for its own
|
|
|
|
* processing.
|
|
|
|
*
|
|
|
|
* This function assumes Alt is held down.
|
|
|
|
*/
|
|
|
|
switch (sym)
|
|
|
|
{
|
|
|
|
case XKB_KEY_Escape:
|
|
|
|
wl_display_terminate(server->wl_display);
|
|
|
|
break;
|
|
|
|
case XKB_KEY_F1:
|
|
|
|
/* Cycle to the next toplevel */
|
2024-03-31 16:09:01 +02:00
|
|
|
if (wl_list_length(&server->views) < 2)
|
2024-03-30 00:18:51 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2024-03-31 16:09:01 +02:00
|
|
|
struct diyac_view *next_toplevel =
|
|
|
|
wl_container_of(server->views.prev, next_toplevel, link);
|
2024-04-01 15:38:57 +02:00
|
|
|
diyac_focus_view(next_toplevel, true);
|
2024-03-30 00:18:51 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keyboard_handle_key(
|
|
|
|
struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
/* This event is raised when a key is pressed or released. */
|
|
|
|
struct diyac_keyboard *keyboard =
|
|
|
|
wl_container_of(listener, keyboard, key);
|
|
|
|
struct diyac_server *server = keyboard->server;
|
|
|
|
struct wlr_keyboard_key_event *event = data;
|
|
|
|
struct wlr_seat *seat = server->seat.wlr_seat;
|
|
|
|
|
|
|
|
/* Translate libinput keycode -> xkbcommon */
|
|
|
|
uint32_t keycode = event->keycode + 8;
|
|
|
|
/* Get a list of keysyms based on the keymap for this keyboard */
|
|
|
|
const xkb_keysym_t *syms;
|
|
|
|
int nsyms = xkb_state_key_get_syms(
|
|
|
|
keyboard->wlr_keyboard->xkb_state, keycode, &syms);
|
|
|
|
|
|
|
|
bool handled = false;
|
|
|
|
uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->wlr_keyboard);
|
|
|
|
if ((modifiers & WLR_MODIFIER_ALT) &&
|
|
|
|
event->state == WL_KEYBOARD_KEY_STATE_PRESSED)
|
|
|
|
{
|
|
|
|
/* If alt is held down and this button was _pressed_, we attempt to
|
|
|
|
* process it as a compositor keybinding. */
|
|
|
|
for (int i = 0; i < nsyms; i++)
|
|
|
|
{
|
|
|
|
handled = handle_keybinding(server, syms[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
/* Otherwise, we pass it along to the client. */
|
|
|
|
wlr_seat_set_keyboard(seat, keyboard->wlr_keyboard);
|
|
|
|
wlr_seat_keyboard_notify_key(seat, event->time_msec,
|
|
|
|
event->keycode, event->state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keyboard_handle_destroy(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
/* This event is raised by the keyboard base wlr_input_device to signal
|
|
|
|
* the destruction of the wlr_keyboard. It will no longer receive events
|
|
|
|
* and should be destroyed.
|
|
|
|
*/
|
|
|
|
struct diyac_keyboard *keyboard =
|
|
|
|
wl_container_of(listener, keyboard, destroy);
|
|
|
|
wl_list_remove(&keyboard->modifiers.link);
|
|
|
|
wl_list_remove(&keyboard->key.link);
|
|
|
|
wl_list_remove(&keyboard->destroy.link);
|
|
|
|
wl_list_remove(&keyboard->link);
|
|
|
|
free(keyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_new_keyboard(struct diyac_server *server,
|
|
|
|
struct wlr_input_device *device)
|
|
|
|
{
|
|
|
|
struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device);
|
|
|
|
|
|
|
|
struct diyac_keyboard *keyboard = calloc(1, sizeof(*keyboard));
|
|
|
|
keyboard->server = server;
|
|
|
|
keyboard->wlr_keyboard = wlr_keyboard;
|
|
|
|
|
|
|
|
/* We need to prepare an XKB keymap and assign it to the keyboard. This
|
|
|
|
* assumes the defaults (e.g. layout = "us"). */
|
|
|
|
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
|
|
struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, NULL,
|
|
|
|
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
|
|
|
|
|
|
wlr_keyboard_set_keymap(wlr_keyboard, keymap);
|
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
xkb_context_unref(context);
|
|
|
|
wlr_keyboard_set_repeat_info(wlr_keyboard, 25, 600);
|
|
|
|
|
|
|
|
/* Here we set up listeners for keyboard events. */
|
|
|
|
keyboard->modifiers.notify = keyboard_handle_modifiers;
|
|
|
|
wl_signal_add(&wlr_keyboard->events.modifiers, &keyboard->modifiers);
|
|
|
|
keyboard->key.notify = keyboard_handle_key;
|
|
|
|
wl_signal_add(&wlr_keyboard->events.key, &keyboard->key);
|
|
|
|
keyboard->destroy.notify = keyboard_handle_destroy;
|
|
|
|
wl_signal_add(&device->events.destroy, &keyboard->destroy);
|
|
|
|
|
|
|
|
wlr_seat_set_keyboard(server->seat.wlr_seat, keyboard->wlr_keyboard);
|
|
|
|
|
|
|
|
/* And add the keyboard to our list of keyboards */
|
|
|
|
wl_list_insert(&server->seat.keyboards, &keyboard->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_new_pointer(struct diyac_server *server,
|
|
|
|
struct wlr_input_device *device)
|
|
|
|
{
|
|
|
|
/* We don't do anything special with pointers. All of our pointer handling
|
|
|
|
* is proxied through wlr_cursor. On another compositor, you might take this
|
|
|
|
* opportunity to do libinput configuration on the device to set
|
|
|
|
* acceleration, etc. */
|
|
|
|
wlr_cursor_attach_input_device(server->seat.cursor, device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_new_input(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
/* This event is raised by the backend when a new input device becomes
|
|
|
|
* available. */
|
2024-03-31 16:09:01 +02:00
|
|
|
struct diyac_seat *seat =
|
2024-03-30 00:18:51 +01:00
|
|
|
wl_container_of(listener, seat, new_input);
|
|
|
|
struct wlr_input_device *device = data;
|
|
|
|
switch (device->type)
|
|
|
|
{
|
|
|
|
case WLR_INPUT_DEVICE_KEYBOARD:
|
|
|
|
server_new_keyboard(seat->server, device);
|
|
|
|
break;
|
|
|
|
case WLR_INPUT_DEVICE_POINTER:
|
|
|
|
server_new_pointer(seat->server, device);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* We need to let the wlr_seat know what our capabilities are, which is
|
|
|
|
* communiciated to the client. In Diyac we always have a cursor, even if
|
|
|
|
* there are no pointer devices, so we always include that capability. */
|
|
|
|
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
|
|
|
|
if (!wl_list_empty(&seat->keyboards))
|
|
|
|
{
|
|
|
|
caps |= WL_SEAT_CAPABILITY_KEYBOARD;
|
|
|
|
}
|
|
|
|
wlr_seat_set_capabilities(seat->wlr_seat, caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void seat_request_cursor(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
struct diyac_seat *seat = wl_container_of(
|
|
|
|
listener, seat, request_cursor);
|
|
|
|
/* This event is raised by the seat when a client provides a cursor image */
|
|
|
|
struct wlr_seat_pointer_request_set_cursor_event *event = data;
|
|
|
|
struct wlr_seat_client *focused_client =
|
|
|
|
seat->wlr_seat->pointer_state.focused_client;
|
|
|
|
/* This can be sent by any client, so we check to make sure this one is
|
|
|
|
* actually has pointer focus first. */
|
|
|
|
if (focused_client == event->seat_client)
|
|
|
|
{
|
|
|
|
/* Once we've vetted the client, we can tell the cursor to use the
|
|
|
|
* provided surface as the cursor image. It will set the hardware cursor
|
|
|
|
* on the output that it's currently on and continue to do so as the
|
|
|
|
* cursor moves between outputs. */
|
|
|
|
wlr_cursor_set_surface(seat->cursor, event->surface,
|
|
|
|
event->hotspot_x, event->hotspot_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void seat_request_set_selection(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
/* This event is raised by the seat when a client wants to set the selection,
|
|
|
|
* usually when the user copies something. wlroots allows compositors to
|
|
|
|
* ignore such requests if they so choose, but in diyac we always honor
|
|
|
|
*/
|
|
|
|
struct diyac_seat *seat = wl_container_of(
|
|
|
|
listener, seat, request_set_selection);
|
|
|
|
struct wlr_seat_request_set_selection_event *event = data;
|
|
|
|
wlr_seat_set_selection(seat->wlr_seat, event->source, event->serial);
|
|
|
|
}
|
|
|
|
|
2024-04-01 23:51:48 +02:00
|
|
|
static void request_set_selection_notify(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
struct diyac_seat *seat = wl_container_of(
|
|
|
|
listener, seat, request_set_selection);
|
|
|
|
struct wlr_seat_request_set_selection_event *event = data;
|
|
|
|
wlr_seat_set_selection(seat->wlr_seat, event->source,
|
|
|
|
event->serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void request_set_primary_selection_notify(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
struct diyac_seat *seat = wl_container_of(
|
|
|
|
listener, seat, request_set_primary_selection);
|
|
|
|
struct wlr_seat_request_set_primary_selection_event *event = data;
|
|
|
|
wlr_seat_set_primary_selection(seat->wlr_seat, event->source,
|
|
|
|
event->serial);
|
|
|
|
}
|
|
|
|
|
2024-03-30 00:18:51 +01:00
|
|
|
/*
|
|
|
|
* Configures a seat, which is a single "seat" at which a user sits and
|
|
|
|
* operates the computer. This conceptually includes up to one keyboard,
|
|
|
|
* pointer, touch, and drawing tablet device. We also rig up a listener to
|
|
|
|
* let us know when new input devices are available on the backend.
|
|
|
|
*/
|
|
|
|
void diyac_init_seat(struct diyac_server *server)
|
|
|
|
{
|
2024-03-31 16:09:01 +02:00
|
|
|
// server->seat = calloc(1, sizeof(struct diyac_seat));
|
2024-03-30 00:18:51 +01:00
|
|
|
server->seat.server = server;
|
|
|
|
server->seat.new_input.notify = server_new_input;
|
|
|
|
wl_signal_add(&server->backend->events.new_input, &server->seat.new_input);
|
|
|
|
server->seat.wlr_seat = wlr_seat_create(server->wl_display, "seat0");
|
|
|
|
server->seat.request_cursor.notify = seat_request_cursor;
|
|
|
|
wl_signal_add(&server->seat.wlr_seat->events.request_set_cursor,
|
|
|
|
&server->seat.request_cursor);
|
|
|
|
server->seat.request_set_selection.notify = seat_request_set_selection;
|
|
|
|
wl_signal_add(&server->seat.wlr_seat->events.request_set_selection,
|
|
|
|
&server->seat.request_set_selection);
|
2024-04-01 23:51:48 +02:00
|
|
|
server->seat.request_set_selection.notify = request_set_selection_notify;
|
|
|
|
wl_signal_add(&server->seat.wlr_seat->events.request_set_selection,
|
|
|
|
&server->seat.request_set_selection);
|
|
|
|
|
|
|
|
server->seat.request_set_primary_selection.notify =
|
|
|
|
request_set_primary_selection_notify;
|
|
|
|
wl_signal_add(&server->seat.wlr_seat->events.request_set_primary_selection,
|
|
|
|
&server->seat.request_set_primary_selection);
|
2024-03-31 16:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void seat_focus(struct diyac_seat *seat, struct wlr_surface *surface, bool is_lock_surface)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Respect session lock. This check is critical, DO NOT REMOVE.
|
|
|
|
* It should also come before the !surface condition, or the
|
|
|
|
* lock screen may lose focus and become impossible to unlock.
|
|
|
|
*/
|
|
|
|
struct diyac_server *server = seat->server;
|
|
|
|
/*if (server->session_lock && !is_lock_surface)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
if (!surface)
|
|
|
|
{
|
|
|
|
wlr_seat_keyboard_notify_clear_focus(seat->wlr_seat);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Respect input inhibit (also used by some lock screens) */
|
|
|
|
/*
|
|
|
|
if (input_inhibit_blocks_surface(seat, surface->resource)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat);
|
|
|
|
if (keyboard)
|
|
|
|
{
|
|
|
|
wlr_seat_keyboard_notify_enter(seat->wlr_seat, surface,
|
|
|
|
keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
struct wlr_pointer_constraint_v1 *constraint =
|
|
|
|
wlr_pointer_constraints_v1_constraint_for_surface(server->constraints,
|
|
|
|
surface, seat->seat);
|
|
|
|
constrain_cursor(server, constraint);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void diyac_seat_focus_surface(struct diyac_seat *seat, struct wlr_surface *surface)
|
|
|
|
{
|
|
|
|
/* Respect layer-shell exclusive keyboard-interactivity. */
|
|
|
|
if (seat->focused_layer && seat->focused_layer->current.keyboard_interactive == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seat_focus(seat, surface, /*is_lock_surface*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void diyac_seat_focus_layer(struct diyac_seat *seat, struct wlr_layer_surface_v1 *layer)
|
2024-04-01 16:07:07 +02:00
|
|
|
{
|
2024-03-31 16:09:01 +02:00
|
|
|
if (!layer)
|
|
|
|
{
|
|
|
|
seat->focused_layer = NULL;
|
2024-04-01 22:12:35 +02:00
|
|
|
diyac_focus_topmost_view(seat->server, false);
|
2024-03-31 16:09:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
seat_focus(seat, layer->surface, /*is_lock_surface*/ false);
|
|
|
|
if (layer->current.layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP)
|
|
|
|
{
|
|
|
|
seat->focused_layer = layer;
|
|
|
|
}
|
2024-03-30 00:18:51 +01:00
|
|
|
}
|