1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
* Authors:
* Matthew Allum
* Robert Bragg
* Kristian Høgsberg
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <wayland-util.h>
#include <wayland-client.h>
#include "clutter-debug.h"
#include "clutter-device-manager-private.h"
#include "clutter-event-private.h"
#include "clutter-private.h"
#include "clutter-keysyms.h"
#include "evdev/clutter-xkb-utils.h"
#include "clutter-input-device-wayland.h"
#include "clutter-backend-wayland.h"
#include "clutter-backend-wayland-priv.h"
#include "clutter-stage-private.h"
#include "clutter-stage-wayland.h"
#include "clutter-wayland.h"
#include "cogl/clutter-stage-cogl.h"
typedef struct _ClutterInputDeviceClass ClutterInputDeviceWaylandClass;
#define clutter_input_device_wayland_get_type \
_clutter_input_device_wayland_get_type
G_DEFINE_TYPE (ClutterInputDeviceWayland,
clutter_input_device_wayland,
CLUTTER_TYPE_INPUT_DEVICE);
/* This gives us a fake time source for higher level abstractions to have an
* understanding of when an event happens. All that matters are that this is a
* monotonic increasing millisecond accurate time for events to be compared with.
*/
static guint32
_clutter_wayland_get_time (void)
{
return g_get_monotonic_time () / 1000;
}
static void
clutter_wayland_handle_motion (void *data,
struct wl_pointer *pointer,
uint32_t _time,
wl_fixed_t x, wl_fixed_t y)
{
ClutterInputDeviceWayland *device = data;
ClutterStageCogl *stage_cogl;
ClutterEvent *event;
if (!device->pointer_focus)
return;
stage_cogl = device->pointer_focus;
event = clutter_event_new (CLUTTER_MOTION);
event->motion.stage = stage_cogl->wrapper;
event->motion.device = CLUTTER_INPUT_DEVICE (device);
event->motion.time = _clutter_wayland_get_time ();
event->motion.modifier_state = 0;
event->motion.x = wl_fixed_to_double (x);
event->motion.y = wl_fixed_to_double (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_pointer *pointer,
uint32_t serial, uint32_t _time,
uint32_t button, uint32_t state)
{
ClutterInputDeviceWayland *device = data;
ClutterStageCogl *stage_cogl;
ClutterEvent *event;
ClutterEventType type;
ClutterModifierType modifier_mask = 0;
if (!device->pointer_focus)
return;
stage_cogl = device->pointer_focus;
if (state)
type = CLUTTER_BUTTON_PRESS;
else
type = CLUTTER_BUTTON_RELEASE;
event = clutter_event_new (type);
event->button.stage = stage_cogl->wrapper;
event->button.device = CLUTTER_INPUT_DEVICE (device);
event->button.time = _clutter_wayland_get_time ();
event->button.x = device->x;
event->button.y = device->y;
_clutter_xkb_translate_state (event, device->xkb, 0);
/* evdev button codes */
switch (button) {
case 272:
event->button.button = 1;
modifier_mask = CLUTTER_BUTTON1_MASK;
break;
case 273:
event->button.button = 3;
modifier_mask = CLUTTER_BUTTON2_MASK;
break;
case 274:
event->button.button = 2;
modifier_mask = CLUTTER_BUTTON3_MASK;
break;
}
if (modifier_mask)
{
if (state)
device->button_modifier_state |= modifier_mask;
else
device->button_modifier_state &= ~modifier_mask;
}
event->button.modifier_state = device->button_modifier_state;
_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;
ClutterEvent *event;
gdouble delta_x, delta_y;
gdouble delta_factor;
if (!device->pointer_focus)
return;
stage_cogl = device->pointer_focus;
event = clutter_event_new (CLUTTER_SCROLL);
event->scroll.time = _clutter_wayland_get_time ();
event->scroll.stage = stage_cogl->wrapper;
event->scroll.direction = CLUTTER_SCROLL_SMOOTH;
event->scroll.x = device->x;
event->scroll.y = device->y;
/* Wayland pointer axis events are in pointer motion coordinate space.
* To convert to Xi2 discrete step coordinate space, multiply the factor
* 1/10. */
delta_factor = 1.0 / 10.0;
if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
{
delta_x = wl_fixed_to_double (value) * delta_factor;
delta_y = 0;
}
else
{
delta_x = 0;
delta_y = wl_fixed_to_double (value) * delta_factor;
}
clutter_event_set_scroll_delta (event, delta_x, delta_y);
_clutter_xkb_translate_state (event, device->xkb, 0);
_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;
}
}
/* XXX: Need a wl_keyboard event to harmonise these across clients. */
#define KEY_REPEAT_DELAY 660
#define KEY_REPEAT_INTERVAL 40
static gboolean
clutter_wayland_repeat_key (void *data)
{
ClutterInputDeviceWayland *device = data;
ClutterStageCogl *stage_cogl = device->keyboard_focus;
ClutterEvent *event;
event = _clutter_key_event_new_from_evdev ((ClutterInputDevice *) device,
(ClutterInputDevice*) device,
stage_cogl->wrapper,
device->xkb, 0,
device->repeat_time,
device->repeat_key,
1);
device->repeat_time += KEY_REPEAT_INTERVAL;
_clutter_event_push (event, FALSE);
if (!device->is_initial_repeat)
return TRUE;
g_source_remove (device->repeat_source);
device->repeat_source = g_timeout_add (KEY_REPEAT_INTERVAL,
clutter_wayland_repeat_key,
device);
device->is_initial_repeat = FALSE;
return FALSE;
}
static void
clutter_wayland_handle_key (void *data,
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->keyboard_focus)
return;
if (!device->xkb)
return;
event = _clutter_key_event_new_from_evdev ((ClutterInputDevice *) device,
(ClutterInputDevice *) device,
stage_cogl->wrapper,
device->xkb, 0,
_clutter_wayland_get_time (),
key, state);
_clutter_event_push (event, FALSE);
if (!xkb_key_repeats (xkb_state_get_map (device->xkb), key))
return;
if (state)
{
if (device->repeat_key != XKB_KEYCODE_INVALID)
g_source_remove (device->repeat_source);
device->repeat_key = key;
device->repeat_time = _time + KEY_REPEAT_DELAY;
device->repeat_source = g_timeout_add (KEY_REPEAT_DELAY,
clutter_wayland_repeat_key,
device);
device->is_initial_repeat = TRUE;
}
else if (device->repeat_key == key)
{
g_source_remove (device->repeat_source);
device->repeat_key = XKB_KEYCODE_INVALID;
}
}
static void
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,
wl_fixed_t x, wl_fixed_t y)
{
ClutterInputDeviceWayland *device = data;
ClutterStageWayland *stage_wayland;
ClutterStageCogl *stage_cogl;
ClutterEvent *event;
ClutterBackend *backend;
ClutterBackendWayland *backend_wayland;
stage_wayland = wl_surface_get_user_data (surface);
if (!CLUTTER_IS_STAGE_COGL (stage_wayland))
return;
stage_cogl = CLUTTER_STAGE_COGL (stage_wayland);
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 = _clutter_wayland_get_time ();
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);
if (stage_wayland->cursor_visible)
{
/* Set the cursor to the cursor loaded at backend initialisation */
backend = clutter_get_default_backend ();
backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
_clutter_backend_wayland_ensure_cursor (backend_wayland);
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);
wl_surface_commit (backend_wayland->cursor_surface);
}
else
{
wl_pointer_set_cursor (pointer, serial, NULL, 0, 0);
}
}
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 == NULL)
return;
if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
return;
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 = _clutter_wayland_get_time ();
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);
device->pointer_focus = NULL;
_clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device), NULL);
}
static void
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;
if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
return;
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;
if (!surface)
return;
if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
return;
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);
if (device->repeat_key != XKB_KEYCODE_INVALID)
{
g_source_remove (device->repeat_source);
device->repeat_key = XKB_KEYCODE_INVALID;
}
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)
{
struct wl_pointer *pointer;
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 ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !device->has_keyboard)
{
struct wl_keyboard *keyboard;
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_seat_listener _clutter_seat_wayland_listener = {
clutter_wayland_handle_seat,
};
static gboolean
clutter_input_device_wayland_keycode_to_evdev (ClutterInputDevice *device,
guint hardware_keycode,
guint *evdev_keycode)
{
/* The hardware keycodes from the wayland backend are already evdev
keycodes */
*evdev_keycode = hardware_keycode;
return TRUE;
}
static void
clutter_input_device_wayland_class_init (ClutterInputDeviceWaylandClass *klass)
{
klass->keycode_to_evdev = clutter_input_device_wayland_keycode_to_evdev;
}
static void
clutter_input_device_wayland_init (ClutterInputDeviceWayland *self)
{
self->repeat_key = XKB_KEYCODE_INVALID;
}
/**
* clutter_wayland_input_device_get_wl_seat: (skip)
* @device: a #ClutterInputDevice
*
* Access the underlying data structure representing the Wayland device that is
* backing this #ClutterInputDevice.
*
* Note: this function can only be called when running on the Wayland platform.
* Calling this function at any other time will return %NULL.
*
* Returns: (transfer none): the Wayland input device associated with the
* @device
*
* Since: 1.10
*/
struct wl_seat *
clutter_wayland_input_device_get_wl_seat (ClutterInputDevice *device)
{
ClutterInputDeviceWayland *wayland_device;
if (!CLUTTER_INPUT_DEVICE_WAYLAND (device))
return NULL;
wayland_device = CLUTTER_INPUT_DEVICE_WAYLAND (device);
return wayland_device->input_device;
}
|