mirror of
https://gitlab.freedesktop.org/wlroots/wlroots.git
synced 2025-11-01 22:58:38 -04:00
types/wlr_keyboard: add base wlr_input_device
wlr_keyboard owns its base wlr_input_device. It will be initialized when the keyboard is initialized, and finished when the keyboard is destroyed.
This commit is contained in:
parent
130c3bcf63
commit
a1978b1299
12 changed files with 66 additions and 85 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-server-core.h>
|
||||
#include <wlr/interfaces/wlr_input_device.h>
|
||||
#include <wlr/interfaces/wlr_keyboard.h>
|
||||
#include <wlr/types/wlr_keyboard.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
|
@ -114,7 +115,10 @@ void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard,
|
|||
}
|
||||
|
||||
void wlr_keyboard_init(struct wlr_keyboard *kb,
|
||||
const struct wlr_keyboard_impl *impl) {
|
||||
const struct wlr_keyboard_impl *impl, const char *name) {
|
||||
wlr_input_device_init(&kb->base, WLR_INPUT_DEVICE_KEYBOARD, NULL, name);
|
||||
kb->base.keyboard = kb;
|
||||
|
||||
kb->impl = impl;
|
||||
wl_signal_init(&kb->events.key);
|
||||
wl_signal_init(&kb->events.modifiers);
|
||||
|
|
@ -134,6 +138,8 @@ void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
|
|||
return;
|
||||
}
|
||||
wlr_signal_emit_safe(&kb->events.destroy, kb);
|
||||
wlr_input_device_finish(&kb->base);
|
||||
|
||||
xkb_state_unref(kb->xkb_state);
|
||||
xkb_keymap_unref(kb->keymap);
|
||||
free(kb->keymap_string);
|
||||
|
|
|
|||
|
|
@ -60,16 +60,7 @@ struct wlr_keyboard_group *wlr_keyboard_group_create(void) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
group->input_device = calloc(1, sizeof(struct wlr_input_device));
|
||||
if (!group->input_device) {
|
||||
wlr_log(WLR_ERROR, "Failed to allocate wlr_input_device for group");
|
||||
free(group);
|
||||
return NULL;
|
||||
}
|
||||
wl_signal_init(&group->input_device->events.destroy);
|
||||
group->input_device->keyboard = &group->keyboard;
|
||||
|
||||
wlr_keyboard_init(&group->keyboard, &impl);
|
||||
wlr_keyboard_init(&group->keyboard, &impl, "keyboard-group");
|
||||
wl_list_init(&group->devices);
|
||||
wl_list_init(&group->keys);
|
||||
|
||||
|
|
@ -335,9 +326,7 @@ void wlr_keyboard_group_destroy(struct wlr_keyboard_group *group) {
|
|||
wlr_keyboard_group_remove_keyboard(group, device->keyboard);
|
||||
}
|
||||
wlr_keyboard_destroy(&group->keyboard);
|
||||
wl_list_remove(&group->input_device->events.destroy.listener_list);
|
||||
wl_list_remove(&group->events.enter.listener_list);
|
||||
wl_list_remove(&group->events.leave.listener_list);
|
||||
free(group->input_device);
|
||||
free(group);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,26 +11,40 @@
|
|||
#include "util/time.h"
|
||||
#include "virtual-keyboard-unstable-v1-protocol.h"
|
||||
|
||||
|
||||
static void keyboard_led_update(struct wlr_keyboard *wlr_kb, uint32_t leds) {
|
||||
// unsupported by virtual keyboard protocol
|
||||
/**
|
||||
* Send release event for each pressed key to bring the keyboard back to
|
||||
* neutral state.
|
||||
*
|
||||
* This may be needed for virtual keyboards. For physical devices, kernel
|
||||
* or libinput will deal with the removal of devices.
|
||||
*/
|
||||
static void keyboard_release_pressed_keys(struct wlr_keyboard *keyboard) {
|
||||
size_t orig_num_keycodes = keyboard->num_keycodes;
|
||||
for (size_t i = 0; i < orig_num_keycodes; ++i) {
|
||||
assert(keyboard->num_keycodes == orig_num_keycodes - i);
|
||||
struct wlr_event_keyboard_key event = {
|
||||
.time_msec = get_current_time_msec(),
|
||||
.keycode = keyboard->keycodes[orig_num_keycodes - i - 1],
|
||||
.update_state = false,
|
||||
.state = WL_KEYBOARD_KEY_STATE_RELEASED,
|
||||
};
|
||||
wlr_keyboard_notify_key(keyboard, &event); // updates num_keycodes
|
||||
}
|
||||
}
|
||||
|
||||
static void keyboard_destroy(struct wlr_keyboard *wlr_kb) {
|
||||
// safe to ignore - keyboard will be destroyed only iff associated virtual
|
||||
// keyboard is torn down, no need to tear down the keyboard separately
|
||||
struct wlr_virtual_keyboard_v1 *keyboard =
|
||||
(struct wlr_virtual_keyboard_v1 *)wlr_kb;
|
||||
|
||||
keyboard_release_pressed_keys(&keyboard->keyboard);
|
||||
wl_resource_set_user_data(keyboard->resource, NULL);
|
||||
wlr_signal_emit_safe(&keyboard->events.destroy, keyboard);
|
||||
wl_list_remove(&keyboard->link);
|
||||
free(keyboard);
|
||||
}
|
||||
|
||||
static const struct wlr_keyboard_impl keyboard_impl = {
|
||||
.destroy = keyboard_destroy,
|
||||
.led_update = keyboard_led_update
|
||||
};
|
||||
|
||||
static void input_device_destroy(struct wlr_input_device *dev) {
|
||||
}
|
||||
|
||||
static const struct wlr_input_device_impl input_device_impl = {
|
||||
.destroy = input_device_destroy
|
||||
};
|
||||
|
||||
static const struct zwp_virtual_keyboard_v1_interface virtual_keyboard_impl;
|
||||
|
|
@ -44,10 +58,11 @@ static struct wlr_virtual_keyboard_v1 *virtual_keyboard_from_resource(
|
|||
|
||||
struct wlr_virtual_keyboard_v1 *wlr_input_device_get_virtual_keyboard(
|
||||
struct wlr_input_device *wlr_dev) {
|
||||
if (wlr_dev->impl != &input_device_impl) {
|
||||
if (wlr_dev->type != WLR_INPUT_DEVICE_KEYBOARD
|
||||
|| wlr_dev->keyboard->impl != &keyboard_impl) {
|
||||
return NULL;
|
||||
}
|
||||
return (struct wlr_virtual_keyboard_v1 *)wlr_dev;
|
||||
return (struct wlr_virtual_keyboard_v1 *)wlr_dev->keyboard;
|
||||
}
|
||||
|
||||
static void virtual_keyboard_keymap(struct wl_client *client,
|
||||
|
|
@ -70,7 +85,7 @@ static void virtual_keyboard_keymap(struct wl_client *client,
|
|||
if (!keymap) {
|
||||
goto keymap_fail;
|
||||
}
|
||||
wlr_keyboard_set_keymap(keyboard->input_device.keyboard, keymap);
|
||||
wlr_keyboard_set_keymap(&keyboard->keyboard, keymap);
|
||||
keyboard->has_keymap = true;
|
||||
xkb_keymap_unref(keymap);
|
||||
xkb_context_unref(context);
|
||||
|
|
@ -101,7 +116,7 @@ static void virtual_keyboard_key(struct wl_client *client,
|
|||
.update_state = false,
|
||||
.state = state,
|
||||
};
|
||||
wlr_keyboard_notify_key(keyboard->input_device.keyboard, &event);
|
||||
wlr_keyboard_notify_key(&keyboard->keyboard, &event);
|
||||
}
|
||||
|
||||
static void virtual_keyboard_modifiers(struct wl_client *client,
|
||||
|
|
@ -115,39 +130,16 @@ static void virtual_keyboard_modifiers(struct wl_client *client,
|
|||
"Cannot send a modifier state before defining a keymap");
|
||||
return;
|
||||
}
|
||||
wlr_keyboard_notify_modifiers(keyboard->input_device.keyboard,
|
||||
wlr_keyboard_notify_modifiers(&keyboard->keyboard,
|
||||
mods_depressed, mods_latched, mods_locked, group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send release event for each pressed key to bring the keyboard back to
|
||||
* neutral state.
|
||||
*
|
||||
* This may be needed for virtual keyboards. For physical devices, kernel
|
||||
* or libinput will deal with the removal of devices.
|
||||
*/
|
||||
static void keyboard_release_pressed_keys(struct wlr_keyboard *keyboard) {
|
||||
size_t orig_num_keycodes = keyboard->num_keycodes;
|
||||
for (size_t i = 0; i < orig_num_keycodes; ++i) {
|
||||
assert(keyboard->num_keycodes == orig_num_keycodes - i);
|
||||
struct wlr_event_keyboard_key event = {
|
||||
.time_msec = get_current_time_msec(),
|
||||
.keycode = keyboard->keycodes[orig_num_keycodes - i - 1],
|
||||
.update_state = false,
|
||||
.state = WL_KEYBOARD_KEY_STATE_RELEASED,
|
||||
};
|
||||
wlr_keyboard_notify_key(keyboard, &event); // updates num_keycodes
|
||||
}
|
||||
}
|
||||
|
||||
static void virtual_keyboard_destroy_resource(struct wl_resource *resource) {
|
||||
struct wlr_virtual_keyboard_v1 *keyboard =
|
||||
virtual_keyboard_from_resource(resource);
|
||||
keyboard_release_pressed_keys(keyboard->input_device.keyboard);
|
||||
wlr_signal_emit_safe(&keyboard->events.destroy, keyboard);
|
||||
wl_list_remove(&keyboard->link);
|
||||
wlr_input_device_destroy(&keyboard->input_device);
|
||||
free(keyboard);
|
||||
if (keyboard != NULL) {
|
||||
wlr_keyboard_destroy(&keyboard->keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
static void virtual_keyboard_destroy(struct wl_client *client,
|
||||
|
|
@ -184,20 +176,13 @@ static void virtual_keyboard_manager_create_virtual_keyboard(
|
|||
return;
|
||||
}
|
||||
|
||||
struct wlr_keyboard* keyboard = calloc(1, sizeof(struct wlr_keyboard));
|
||||
if (!keyboard) {
|
||||
wlr_log(WLR_ERROR, "Cannot allocate wlr_keyboard");
|
||||
free(virtual_keyboard);
|
||||
wl_client_post_no_memory(client);
|
||||
return;
|
||||
}
|
||||
wlr_keyboard_init(keyboard, &keyboard_impl);
|
||||
wlr_keyboard_init(&virtual_keyboard->keyboard, &keyboard_impl,
|
||||
"virtual-keyboard");
|
||||
|
||||
struct wl_resource *keyboard_resource = wl_resource_create(client,
|
||||
&zwp_virtual_keyboard_v1_interface, wl_resource_get_version(resource),
|
||||
id);
|
||||
if (!keyboard_resource) {
|
||||
free(keyboard);
|
||||
free(virtual_keyboard);
|
||||
wl_client_post_no_memory(client);
|
||||
return;
|
||||
|
|
@ -206,12 +191,8 @@ static void virtual_keyboard_manager_create_virtual_keyboard(
|
|||
wl_resource_set_implementation(keyboard_resource, &virtual_keyboard_impl,
|
||||
virtual_keyboard, virtual_keyboard_destroy_resource);
|
||||
|
||||
wlr_input_device_init(&virtual_keyboard->input_device,
|
||||
WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, "virtual keyboard");
|
||||
|
||||
struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat);
|
||||
|
||||
virtual_keyboard->input_device.keyboard = keyboard;
|
||||
virtual_keyboard->resource = keyboard_resource;
|
||||
virtual_keyboard->seat = seat_client->seat;
|
||||
wl_signal_init(&virtual_keyboard->events.destroy);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue