xdg-seat: keyboard grabs

This commit is contained in:
Tony Crisci 2017-10-07 11:02:11 -04:00
parent 27ee171d25
commit 4680943e74
5 changed files with 211 additions and 18 deletions

View File

@ -38,6 +38,28 @@ struct wlr_pointer_grab_interface {
void (*cancel)(struct wlr_seat_pointer_grab *grab);
};
struct wlr_seat_keyboard_grab;
struct wlr_keyboard_grab_interface {
void (*enter)(struct wlr_seat_keyboard_grab *grab, struct wlr_surface *surface);
void (*key)(struct wlr_seat_keyboard_grab *grab, uint32_t time,
uint32_t key, uint32_t state);
void (*modifiers)(struct wlr_seat_keyboard_grab *grab,
uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group);
void (*cancel)(struct wlr_seat_keyboard_grab *grab);
};
/**
* Passed to `wlr_seat_keyboard_start_grab()` to start a grab of the keyboard.
* The grabber is responsible for handling keyboard events for the seat.
*/
struct wlr_seat_keyboard_grab {
const struct wlr_keyboard_grab_interface *interface;
struct wlr_seat *seat;
void *data;
};
/**
* Passed to `wlr_seat_pointer_start_grab()` to start a grab of the pointer. The
* grabber is responsible for handling pointer events for the seat.
@ -77,6 +99,9 @@ struct wlr_seat_keyboard_state {
struct wl_listener surface_destroy;
struct wl_listener resource_destroy;
struct wlr_seat_keyboard_grab *grab;
struct wlr_seat_keyboard_grab *default_grab;
};
struct wlr_seat {
@ -97,12 +122,14 @@ struct wlr_seat {
struct wl_signal pointer_grab_begin;
struct wl_signal pointer_grab_end;
struct wl_signal keyboard_grab_begin;
struct wl_signal keyboard_grab_end;
} events;
void *data;
};
/**
* Allocates a new wlr_seat and adds a wl_seat global to the display.
*/
@ -231,10 +258,48 @@ void wlr_seat_attach_keyboard(struct wlr_seat *seat,
*/
void wlr_seat_detach_keyboard(struct wlr_seat *seat, struct wlr_keyboard *kb);
/**
* Start a grab of the keyboard of this seat. The grabber is responsible for
* handling all keyboard events until the grab ends.
*/
void wlr_seat_keyboard_start_grab(struct wlr_seat *wlr_seat,
struct wlr_seat_keyboard_grab *grab);
/**
* End the grab of the keyboard of this seat. This reverts the grab back to the
* default grab for the keyboard.
*/
void wlr_seat_keyboard_end_grab(struct wlr_seat *wlr_seat);
/**
* Send the keyboard key to focused keyboard resources. Compositors should use
* `wlr_seat_attach_keyboard()` to automatically handle keyboard events.
*/
void wlr_seat_keyboard_send_key(struct wlr_seat *seat, uint32_t time,
uint32_t key, uint32_t state);
/**
* Send the modifier state to focused keyboard resources. Compositors should use
* `wlr_seat_attach_keyboard()` to automatically handle keyboard events.
*/
void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat,
uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked,
uint32_t group);
/**
* Notify the seat that the keyboard focus has changed and request it to be the
* focused surface for this keyboard. Defers to any current grab of the seat's
* keyboard.
*/
void wlr_seat_keyboard_notify_enter(struct wlr_seat *wlr_seat,
struct wlr_surface *surface);
/**
* Send a keyboard enter event to the given surface and consider it to be the
* focused surface for the keyboard. This will send a leave event to the last
* surface that was entered. Pass an array of currently pressed keys.
* surface that was entered. Compositors should use
* `wlr_seat_keyboard_notify_enter()` to change keyboard focus to respect
* keyboard grabs.
*/
void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat,
struct wlr_surface *surface);

View File

@ -46,6 +46,7 @@ struct wlr_xdg_popup_v6 {
struct wlr_xdg_popup_grab_v6 {
struct wl_client *client;
struct wlr_seat_pointer_grab pointer_grab;
struct wlr_seat_keyboard_grab keyboard_grab;
struct wlr_seat *seat;
struct wl_list popups;
struct wl_list link; // wlr_xdg_shell_v6::popup_grabs

View File

@ -234,7 +234,7 @@ static void do_cursor_button_press(struct roots_input *input,
% (sizeof(input->input_events) / sizeof(input->input_events[0]));
set_view_focus(input, desktop, view);
if (view) {
wlr_seat_keyboard_enter(input->wl_seat, surface);
wlr_seat_keyboard_notify_enter(input->wl_seat, surface);
}
break;
}

View File

@ -205,6 +205,34 @@ static const struct wlr_pointer_grab_interface default_pointer_grab_impl = {
.cancel = default_pointer_cancel,
};
static void default_keyboard_enter(struct wlr_seat_keyboard_grab *grab,
struct wlr_surface *surface) {
wlr_seat_keyboard_enter(grab->seat, surface);
}
static void default_keyboard_key(struct wlr_seat_keyboard_grab *grab,
uint32_t time, uint32_t key, uint32_t state) {
wlr_seat_keyboard_send_key(grab->seat, time, key, state);
}
static void default_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab,
uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group) {
wlr_seat_keyboard_send_modifiers(grab->seat, mods_depressed,
mods_latched, mods_locked, group);
}
static void default_keyboard_cancel(struct wlr_seat_keyboard_grab *grab) {
// cannot be cancelled
}
static const struct wlr_keyboard_grab_interface default_keyboard_grab_impl = {
.enter = default_keyboard_enter,
.key = default_keyboard_key,
.modifiers = default_keyboard_modifiers,
.cancel = default_keyboard_cancel,
};
struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
struct wlr_seat *wlr_seat = calloc(1, sizeof(struct wlr_seat));
if (!wlr_seat) {
@ -215,16 +243,28 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
wl_list_init(&wlr_seat->pointer_state.surface_destroy.link);
wl_list_init(&wlr_seat->pointer_state.resource_destroy.link);
struct wlr_seat_pointer_grab *default_grab =
struct wlr_seat_pointer_grab *pointer_grab =
calloc(1, sizeof(struct wlr_seat_pointer_grab));
if (!default_grab) {
if (!pointer_grab) {
free(wlr_seat);
return NULL;
}
default_grab->interface = &default_pointer_grab_impl;
default_grab->seat = wlr_seat;
wlr_seat->pointer_state.default_grab = default_grab;
wlr_seat->pointer_state.grab = default_grab;
pointer_grab->interface = &default_pointer_grab_impl;
pointer_grab->seat = wlr_seat;
wlr_seat->pointer_state.default_grab = pointer_grab;
wlr_seat->pointer_state.grab = pointer_grab;
struct wlr_seat_keyboard_grab *keyboard_grab =
calloc(1, sizeof(struct wlr_seat_keyboard_grab));
if (!keyboard_grab) {
free(pointer_grab);
free(wlr_seat);
return NULL;
}
keyboard_grab->interface = &default_keyboard_grab_impl;
keyboard_grab->seat = wlr_seat;
wlr_seat->keyboard_state.default_grab = keyboard_grab;
wlr_seat->keyboard_state.grab = keyboard_grab;
wlr_seat->keyboard_state.wlr_seat = wlr_seat;
wl_list_init(&wlr_seat->keyboard_state.resource_destroy.link);
@ -249,6 +289,9 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
wl_signal_init(&wlr_seat->events.pointer_grab_begin);
wl_signal_init(&wlr_seat->events.pointer_grab_end);
wl_signal_init(&wlr_seat->events.keyboard_grab_begin);
wl_signal_init(&wlr_seat->events.keyboard_grab_end);
return wlr_seat;
}
@ -265,6 +308,7 @@ void wlr_seat_destroy(struct wlr_seat *wlr_seat) {
wl_global_destroy(wlr_seat->wl_global);
free(wlr_seat->pointer_state.default_grab);
free(wlr_seat->keyboard_state.default_grab);
free(wlr_seat->data_device);
free(wlr_seat->name);
free(wlr_seat);
@ -495,6 +539,18 @@ static void keyboard_switch_seat_keyboard(struct wlr_seat_handle *handle,
handle->seat_keyboard = seat_kb;
}
void wlr_seat_keyboard_send_key(struct wlr_seat *wlr_seat, uint32_t time,
uint32_t key, uint32_t state) {
struct wlr_seat_handle *handle = wlr_seat->keyboard_state.focused_handle;
if (!handle || !handle->keyboard) {
return;
}
uint32_t serial = wl_display_next_serial(wlr_seat->display);
wl_keyboard_send_key(handle->keyboard, serial,
time, key, state);
}
static void keyboard_key_notify(struct wl_listener *listener, void *data) {
struct wlr_seat_keyboard *seat_kb = wl_container_of(listener, seat_kb, key);
struct wlr_seat *seat = seat_kb->seat;
@ -508,9 +564,9 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) {
struct wlr_event_keyboard_key *event = data;
enum wlr_key_state key_state = event->state;
uint32_t key_serial = wl_display_next_serial(seat->display);
wl_keyboard_send_key(handle->keyboard, key_serial,
(uint32_t)event->time_usec, event->keycode, key_state);
struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab;
grab->interface->key(grab, (uint32_t)(event->time_usec / 1000),
event->keycode, key_state);
}
static void keyboard_modifiers_notify(struct wl_listener *listener,
@ -527,8 +583,9 @@ static void keyboard_modifiers_notify(struct wl_listener *listener,
struct wlr_keyboard *keyboard = seat_kb->keyboard;
uint32_t modifiers_serial = wl_display_next_serial(seat->display);
wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial,
struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab;
grab->interface->modifiers(grab,
keyboard->modifiers.depressed, keyboard->modifiers.latched,
keyboard->modifiers.locked, keyboard->modifiers.group);
}
@ -584,6 +641,21 @@ void wlr_seat_detach_keyboard(struct wlr_seat *seat, struct wlr_keyboard *kb) {
}
}
void wlr_seat_keyboard_start_grab(struct wlr_seat *wlr_seat,
struct wlr_seat_keyboard_grab *grab) {
grab->seat = wlr_seat;
wlr_seat->keyboard_state.grab = grab;
wl_signal_emit(&wlr_seat->events.keyboard_grab_begin, grab);
}
void wlr_seat_keyboard_end_grab(struct wlr_seat *wlr_seat) {
struct wlr_seat_keyboard_grab *grab = wlr_seat->keyboard_state.grab;
wlr_seat->keyboard_state.grab = wlr_seat->keyboard_state.default_grab;
wl_signal_emit(&wlr_seat->events.keyboard_grab_end, grab);
}
static void keyboard_surface_destroy_notify(struct wl_listener *listener,
void *data) {
struct wlr_seat_keyboard_state *state = wl_container_of(
@ -604,6 +676,21 @@ static void keyboard_resource_destroy_notify(struct wl_listener *listener,
wlr_seat_keyboard_clear_focus(state->wlr_seat);
}
void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat,
uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked,
uint32_t group) {
struct wlr_seat_handle *handle = seat->keyboard_state.focused_handle;
if (!handle || !handle->keyboard) {
return;
}
uint32_t serial = wl_display_next_serial(seat->display);
wl_keyboard_send_modifiers(handle->keyboard, serial,
mods_depressed, mods_latched,
mods_locked, group);
}
void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat,
struct wlr_surface *surface) {
if (wlr_seat->keyboard_state.focused_surface == surface) {
@ -660,6 +747,12 @@ void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat,
wlr_seat->keyboard_state.focused_surface = surface;
}
void wlr_seat_keyboard_notify_enter(struct wlr_seat *wlr_seat, struct
wlr_surface *surface) {
struct wlr_seat_keyboard_grab *grab = wlr_seat->keyboard_state.grab;
grab->interface->enter(grab, surface);
}
void wlr_seat_keyboard_clear_focus(struct wlr_seat *wlr_seat) {
struct wl_array keys;
wl_array_init(&keys);

View File

@ -102,6 +102,33 @@ static const struct wlr_pointer_grab_interface xdg_pointer_grab_impl = {
.axis = xdg_pointer_grab_axis,
};
static void xdg_keyboard_grab_enter(struct wlr_seat_keyboard_grab *grab, struct wlr_surface *surface) {
// keyboard focus should remain on the popup
}
static void xdg_keyboard_grab_key(struct wlr_seat_keyboard_grab *grab, uint32_t time,
uint32_t key, uint32_t state) {
wlr_seat_keyboard_send_key(grab->seat, time, key, state);
}
static void xdg_keyboard_grab_modifiers(struct wlr_seat_keyboard_grab *grab,
uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group) {
wlr_seat_keyboard_send_modifiers(grab->seat, mods_depressed, mods_latched,
mods_locked, group);
}
static void xdg_keyboard_grab_cancel(struct wlr_seat_keyboard_grab *grab) {
wlr_seat_keyboard_end_grab(grab->seat);
}
static const struct wlr_keyboard_grab_interface xdg_keyboard_grab_impl = {
.enter = xdg_keyboard_grab_enter,
.key = xdg_keyboard_grab_key,
.modifiers = xdg_keyboard_grab_modifiers,
.cancel = xdg_keyboard_grab_cancel,
};
static struct wlr_xdg_popup_grab_v6 *xdg_shell_popup_grab_from_seat(
struct wlr_xdg_shell_v6 *shell, struct wlr_seat *seat) {
struct wlr_xdg_popup_grab_v6 *xdg_grab;
@ -118,7 +145,8 @@ static struct wlr_xdg_popup_grab_v6 *xdg_shell_popup_grab_from_seat(
xdg_grab->pointer_grab.data = xdg_grab;
xdg_grab->pointer_grab.interface = &xdg_pointer_grab_impl;
// TODO: keyboard grab
xdg_grab->keyboard_grab.data = xdg_grab;
xdg_grab->keyboard_grab.interface = &xdg_keyboard_grab_impl;
wl_list_init(&xdg_grab->popups);
@ -130,6 +158,7 @@ static struct wlr_xdg_popup_grab_v6 *xdg_shell_popup_grab_from_seat(
static void xdg_surface_destroy(struct wlr_xdg_surface_v6 *surface) {
// TODO: probably need to ungrab before this event
wl_signal_emit(&surface->events.destroy, surface);
if (surface->configure_idle) {
@ -166,9 +195,13 @@ static void xdg_surface_destroy(struct wlr_xdg_surface_v6 *surface) {
wl_list_remove(&surface->popup_state->grab_link);
if (wl_list_empty(&grab->popups) &&
grab->seat->pointer_state.grab == &grab->pointer_grab) {
wlr_seat_pointer_end_grab(grab->seat);
if (wl_list_empty(&grab->popups)) {
if (grab->seat->pointer_state.grab == &grab->pointer_grab) {
wlr_seat_pointer_end_grab(grab->seat);
}
if (grab->seat->keyboard_state.grab == &grab->keyboard_grab) {
wlr_seat_keyboard_end_grab(grab->seat);
}
}
}
@ -355,6 +388,7 @@ static void xdg_popup_protocol_grab(struct wl_client *client,
wl_list_insert(&popup_grab->popups, &surface->popup_state->grab_link);
wlr_seat_pointer_start_grab(handle->wlr_seat, &popup_grab->pointer_grab);
wlr_seat_keyboard_start_grab(handle->wlr_seat, &popup_grab->keyboard_grab);
}
static const struct zxdg_popup_v6_interface zxdg_popup_v6_implementation = {