Port to new Wayland and xkbcommon APIs
For Wayland, this is mostly the input protocol having changed, although there's also the SHM pool API, the cursor API, as well as fullscreen and ping. Also port to the new (months-old) xkbcommon API, as used by Weston 0.95. This involves having xkbcommon manage the state for us, where appropriate. Fans of multi-layout keyboards (or just caps lock) will no doubt appreciate these changes. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
This commit is contained in:

committed by
Rob Bradford

parent
a158d66abb
commit
8f4e39b6d7
@ -29,6 +29,8 @@
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <wayland-util.h>
|
||||
#include <wayland-client.h>
|
||||
|
||||
@ -53,10 +55,9 @@ G_DEFINE_TYPE (ClutterInputDeviceWayland,
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_motion (void *data,
|
||||
struct wl_input_device *input_device,
|
||||
struct wl_pointer *pointer,
|
||||
uint32_t _time,
|
||||
int32_t x, int32_t y,
|
||||
int32_t sx, int32_t sy)
|
||||
wl_fixed_t x, wl_fixed_t y)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl = device->pointer_focus;
|
||||
@ -67,21 +68,19 @@ clutter_wayland_handle_motion (void *data,
|
||||
event->motion.device = CLUTTER_INPUT_DEVICE (device);
|
||||
event->motion.time = _time;
|
||||
event->motion.modifier_state = 0;
|
||||
event->motion.x = sx;
|
||||
event->motion.y = sy;
|
||||
event->motion.x = wl_fixed_to_double(x);
|
||||
event->motion.y = wl_fixed_to_double(y);
|
||||
|
||||
device->surface_x = sx;
|
||||
device->surface_y = sy;
|
||||
device->x = x;
|
||||
device->y = y;
|
||||
device->x = event->motion.x;
|
||||
device->y = event->motion.y;
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_button (void *data,
|
||||
struct wl_input_device *input_device,
|
||||
uint32_t _time,
|
||||
struct wl_pointer *pointer,
|
||||
uint32_t serial, uint32_t _time,
|
||||
uint32_t button, uint32_t state)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
@ -97,10 +96,11 @@ clutter_wayland_handle_button (void *data,
|
||||
event = clutter_event_new (type);
|
||||
event->button.stage = stage_cogl->wrapper;
|
||||
event->button.device = CLUTTER_INPUT_DEVICE (device);
|
||||
event->button.time = _time;
|
||||
event->button.x = device->surface_x;
|
||||
event->button.y = device->surface_y;
|
||||
event->button.modifier_state = device->modifier_state;
|
||||
event->button.time = /*_time*/ serial;
|
||||
event->button.x = device->x;
|
||||
event->button.y = device->y;
|
||||
event->button.modifier_state =
|
||||
xkb_state_serialize_mods (device->xkb, XKB_STATE_EFFECTIVE);
|
||||
|
||||
/* evdev button codes */
|
||||
switch (button) {
|
||||
@ -118,135 +118,323 @@ clutter_wayland_handle_button (void *data,
|
||||
_clutter_event_push (event, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_axis (void *data,
|
||||
struct wl_pointer *pointer,
|
||||
uint32_t time,
|
||||
uint32_t axis,
|
||||
wl_fixed_t value)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl = device->pointer_focus;
|
||||
ClutterEvent *event;
|
||||
gdouble delta_x, delta_y;
|
||||
|
||||
event = clutter_event_new (CLUTTER_SCROLL);
|
||||
event->scroll.time = time;
|
||||
event->scroll.stage = stage_cogl->wrapper;
|
||||
event->scroll.direction = CLUTTER_SCROLL_SMOOTH;
|
||||
event->scroll.x = device->x;
|
||||
event->scroll.y = device->y;
|
||||
|
||||
if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
|
||||
{
|
||||
delta_x = -wl_fixed_to_double(value) * 23;
|
||||
delta_y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
delta_x = 0;
|
||||
delta_y = -wl_fixed_to_double(value) * 23; /* XXX: based on my bcm5794 */
|
||||
}
|
||||
clutter_event_set_scroll_delta (event, delta_x, delta_y);
|
||||
|
||||
event->scroll.modifier_state =
|
||||
xkb_state_serialize_mods(device->xkb, XKB_STATE_EFFECTIVE);
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_keymap (void *data,
|
||||
struct wl_keyboard *keyboard,
|
||||
uint32_t format,
|
||||
int32_t fd,
|
||||
uint32_t size)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
struct xkb_context *ctx;
|
||||
struct xkb_keymap *keymap;
|
||||
char *map_str;
|
||||
|
||||
if (device->xkb)
|
||||
{
|
||||
xkb_state_unref (device->xkb);
|
||||
device->xkb = NULL;
|
||||
}
|
||||
|
||||
ctx = xkb_context_new (0);
|
||||
if (!ctx)
|
||||
{
|
||||
close (fd);
|
||||
return;
|
||||
}
|
||||
|
||||
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
|
||||
{
|
||||
close (fd);
|
||||
return;
|
||||
}
|
||||
|
||||
map_str = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (map_str == MAP_FAILED)
|
||||
{
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
keymap = xkb_map_new_from_string (ctx,
|
||||
map_str,
|
||||
XKB_KEYMAP_FORMAT_TEXT_V1,
|
||||
0);
|
||||
xkb_context_unref (ctx);
|
||||
munmap (map_str, size);
|
||||
close (fd);
|
||||
|
||||
if (!keymap)
|
||||
{
|
||||
g_warning ("failed to compile keymap\n");
|
||||
return;
|
||||
}
|
||||
|
||||
device->xkb = xkb_state_new(keymap);
|
||||
xkb_map_unref (keymap);
|
||||
if (!device->xkb)
|
||||
{
|
||||
g_warning ("failed to create XKB state object\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_key (void *data,
|
||||
struct wl_input_device *input_device,
|
||||
uint32_t _time,
|
||||
struct wl_keyboard *keyboard,
|
||||
uint32_t serial, uint32_t _time,
|
||||
uint32_t key, uint32_t state)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl = device->keyboard_focus;
|
||||
ClutterEvent *event;
|
||||
|
||||
if (!device->xkb)
|
||||
return;
|
||||
|
||||
event = _clutter_key_event_new_from_evdev ((ClutterInputDevice *) device,
|
||||
stage_cogl->wrapper,
|
||||
device->xkb,
|
||||
_time, key, state,
|
||||
&device->modifier_state);
|
||||
_time, key, state);
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_pointer_focus (void *data,
|
||||
struct wl_input_device *input_device,
|
||||
uint32_t _time,
|
||||
clutter_wayland_handle_modifiers (void *data,
|
||||
struct wl_keyboard *keyboard,
|
||||
uint32_t serial,
|
||||
uint32_t mods_depressed,
|
||||
uint32_t mods_latched,
|
||||
uint32_t mods_locked,
|
||||
uint32_t group)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
|
||||
if (!device->xkb)
|
||||
return;
|
||||
|
||||
xkb_state_update_mask (device->xkb,
|
||||
mods_depressed,
|
||||
mods_latched,
|
||||
mods_locked,
|
||||
0,
|
||||
0,
|
||||
group);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_pointer_enter (void *data,
|
||||
struct wl_pointer *pointer,
|
||||
uint32_t serial,
|
||||
struct wl_surface *surface,
|
||||
int32_t x, int32_t y, int32_t sx, int32_t sy)
|
||||
wl_fixed_t x, wl_fixed_t y)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl;
|
||||
ClutterEvent *event;
|
||||
ClutterBackend *backend;
|
||||
ClutterBackendWayland *backend_wayland;
|
||||
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
|
||||
device->pointer_focus = stage_cogl;
|
||||
_clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device),
|
||||
stage_cogl->wrapper);
|
||||
|
||||
event = clutter_event_new (CLUTTER_ENTER);
|
||||
event->crossing.stage = stage_cogl->wrapper;
|
||||
event->crossing.time = 0; /* ?! */
|
||||
event->crossing.x = wl_fixed_to_double(x);
|
||||
event->crossing.y = wl_fixed_to_double(y);
|
||||
event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
|
||||
event->crossing.device = CLUTTER_INPUT_DEVICE (device);
|
||||
|
||||
device->x = event->crossing.x;
|
||||
device->y = event->crossing.y;
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
|
||||
/* Set the cursor to the cursor loaded at backend initialisation */
|
||||
backend = clutter_get_default_backend ();
|
||||
backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
|
||||
|
||||
wl_pointer_set_cursor (pointer,
|
||||
serial,
|
||||
backend_wayland->cursor_surface,
|
||||
backend_wayland->cursor_x,
|
||||
backend_wayland->cursor_y);
|
||||
wl_surface_attach (backend_wayland->cursor_surface,
|
||||
backend_wayland->cursor_buffer,
|
||||
0,
|
||||
0);
|
||||
wl_surface_damage (backend_wayland->cursor_surface,
|
||||
0,
|
||||
0,
|
||||
32, /* XXX: FFS */
|
||||
32);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_pointer_leave (void *data,
|
||||
struct wl_pointer *pointer,
|
||||
uint32_t serial,
|
||||
struct wl_surface *surface)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl;
|
||||
ClutterEvent *event;
|
||||
|
||||
if (!surface)
|
||||
{
|
||||
stage_cogl = device->pointer_focus;
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
g_assert (device->pointer_focus == stage_cogl);
|
||||
|
||||
event = clutter_event_new (CLUTTER_LEAVE);
|
||||
event->crossing.stage = stage_cogl->wrapper;
|
||||
event->crossing.time = _time;
|
||||
event->crossing.x = sx;
|
||||
event->crossing.y = sy;
|
||||
event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
|
||||
event->crossing.device = CLUTTER_INPUT_DEVICE (device);
|
||||
event = clutter_event_new (CLUTTER_LEAVE);
|
||||
event->crossing.stage = stage_cogl->wrapper;
|
||||
event->crossing.time = 0; /* ?! */
|
||||
event->crossing.x = device->x;
|
||||
event->crossing.y = device->y;
|
||||
event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
|
||||
event->crossing.device = CLUTTER_INPUT_DEVICE (device);
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
_clutter_event_push (event, FALSE);
|
||||
|
||||
device->pointer_focus = NULL;
|
||||
_clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device), NULL);
|
||||
}
|
||||
|
||||
if (surface)
|
||||
{
|
||||
ClutterBackend *backend;
|
||||
ClutterBackendWayland *backend_wayland;
|
||||
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
|
||||
device->pointer_focus = stage_cogl;
|
||||
_clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device),
|
||||
stage_cogl->wrapper);
|
||||
|
||||
event = clutter_event_new (CLUTTER_ENTER);
|
||||
event->crossing.stage = stage_cogl->wrapper;
|
||||
event->crossing.time = _time;
|
||||
event->crossing.x = sx;
|
||||
event->crossing.y = sy;
|
||||
event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
|
||||
event->crossing.device = CLUTTER_INPUT_DEVICE (device);
|
||||
|
||||
_clutter_event_push (event, FALSE);
|
||||
|
||||
device->surface_x = sx;
|
||||
device->surface_y = sy;
|
||||
device->x = x;
|
||||
device->y = y;
|
||||
|
||||
/* Set the cursor to the cursor loaded at backend initialisation */
|
||||
backend = clutter_get_default_backend ();
|
||||
backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
|
||||
|
||||
wl_input_device_attach (input_device,
|
||||
_time,
|
||||
backend_wayland->cursor_buffer,
|
||||
backend_wayland->cursor_x,
|
||||
backend_wayland->cursor_y);
|
||||
}
|
||||
device->pointer_focus = NULL;
|
||||
_clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device), NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_keyboard_focus (void *data,
|
||||
struct wl_input_device *input_device,
|
||||
uint32_t _time,
|
||||
clutter_wayland_handle_keyboard_enter (void *data,
|
||||
struct wl_keyboard *keyboard,
|
||||
uint32_t serial,
|
||||
struct wl_surface *surface,
|
||||
struct wl_array *keys)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl;
|
||||
uint32_t *k, *end;
|
||||
|
||||
if (device->keyboard_focus)
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
g_assert (device->keyboard_focus == NULL);
|
||||
device->keyboard_focus = stage_cogl;
|
||||
|
||||
_clutter_stage_update_state (stage_cogl->wrapper,
|
||||
0,
|
||||
CLUTTER_STAGE_STATE_ACTIVATED);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_keyboard_leave (void *data,
|
||||
struct wl_keyboard *keyboard,
|
||||
uint32_t serial,
|
||||
struct wl_surface *surface)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
ClutterStageCogl *stage_cogl;
|
||||
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
g_assert (device->keyboard_focus == stage_cogl);
|
||||
|
||||
_clutter_stage_update_state (stage_cogl->wrapper,
|
||||
CLUTTER_STAGE_STATE_ACTIVATED,
|
||||
0);
|
||||
|
||||
device->keyboard_focus = NULL;
|
||||
}
|
||||
|
||||
static const struct wl_keyboard_listener _clutter_keyboard_wayland_listener = {
|
||||
clutter_wayland_handle_keymap,
|
||||
clutter_wayland_handle_keyboard_enter,
|
||||
clutter_wayland_handle_keyboard_leave,
|
||||
clutter_wayland_handle_key,
|
||||
clutter_wayland_handle_modifiers,
|
||||
};
|
||||
|
||||
static const struct wl_pointer_listener _clutter_pointer_wayland_listener = {
|
||||
clutter_wayland_handle_pointer_enter,
|
||||
clutter_wayland_handle_pointer_leave,
|
||||
clutter_wayland_handle_motion,
|
||||
clutter_wayland_handle_button,
|
||||
clutter_wayland_handle_axis,
|
||||
};
|
||||
|
||||
static void
|
||||
clutter_wayland_handle_seat (void *data,
|
||||
struct wl_seat *seat,
|
||||
uint32_t capabilities)
|
||||
{
|
||||
ClutterInputDeviceWayland *device = data;
|
||||
|
||||
/* XXX: Needs to handle removals too. */
|
||||
|
||||
if ((capabilities & WL_SEAT_CAPABILITY_POINTER) && !device->has_pointer)
|
||||
{
|
||||
stage_cogl = device->keyboard_focus;
|
||||
device->keyboard_focus = NULL;
|
||||
struct wl_pointer *pointer;
|
||||
|
||||
_clutter_stage_update_state (stage_cogl->wrapper,
|
||||
CLUTTER_STAGE_STATE_ACTIVATED,
|
||||
0);
|
||||
pointer = wl_seat_get_pointer (seat);
|
||||
if (pointer)
|
||||
{
|
||||
wl_pointer_add_listener (pointer,
|
||||
&_clutter_pointer_wayland_listener,
|
||||
device);
|
||||
wl_pointer_set_user_data (pointer, device);
|
||||
device->has_pointer = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (surface)
|
||||
if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !device->has_keyboard)
|
||||
{
|
||||
stage_cogl = wl_surface_get_user_data (surface);
|
||||
device->keyboard_focus = stage_cogl;
|
||||
struct wl_keyboard *keyboard;
|
||||
|
||||
_clutter_stage_update_state (stage_cogl->wrapper,
|
||||
0,
|
||||
CLUTTER_STAGE_STATE_ACTIVATED);
|
||||
|
||||
end = (uint32_t *)((guint8 *)keys->data + keys->size);
|
||||
device->modifier_state = 0;
|
||||
for (k = keys->data; k < end; k++)
|
||||
device->modifier_state |= device->xkb->map->modmap[*k];
|
||||
keyboard = wl_seat_get_keyboard (seat);
|
||||
if (keyboard)
|
||||
{
|
||||
wl_keyboard_add_listener (keyboard,
|
||||
&_clutter_keyboard_wayland_listener,
|
||||
device);
|
||||
wl_keyboard_set_user_data (keyboard, device);
|
||||
device->has_keyboard = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const struct wl_input_device_listener _clutter_input_device_wayland_listener = {
|
||||
clutter_wayland_handle_motion,
|
||||
clutter_wayland_handle_button,
|
||||
clutter_wayland_handle_key,
|
||||
clutter_wayland_handle_pointer_focus,
|
||||
clutter_wayland_handle_keyboard_focus,
|
||||
const struct wl_seat_listener _clutter_seat_wayland_listener = {
|
||||
clutter_wayland_handle_seat,
|
||||
};
|
||||
|
||||
static gboolean
|
||||
@ -287,8 +475,8 @@ clutter_input_device_wayland_init (ClutterInputDeviceWayland *self)
|
||||
*
|
||||
* Since: 1.10
|
||||
*/
|
||||
struct wl_input_device *
|
||||
clutter_wayland_input_device_get_wl_input_device (ClutterInputDevice *device)
|
||||
struct wl_seat *
|
||||
clutter_wayland_input_device_get_wl_seat (ClutterInputDevice *device)
|
||||
{
|
||||
ClutterInputDeviceWayland *wayland_device;
|
||||
|
||||
|
Reference in New Issue
Block a user