summaryrefslogtreecommitdiff
path: root/src/wayland/meta-wayland-seat.c
blob: e53637904e0d2a022b9406cae74b0aaa01040254 (plain)
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
/*
 * Wayland Support
 *
 * Copyright (C) 2013 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "config.h"

#include <clutter/clutter.h>
#include <clutter/wayland/clutter-wayland-compositor.h>
#include <clutter/wayland/clutter-wayland-surface.h>
#include <linux/input.h>
#include <stdlib.h>
#include <string.h>
#include "meta-wayland-seat.h"
#include "meta-wayland-keyboard.h"
#include "meta-wayland-private.h"

struct _MetaWaylandSeat
{
  struct wl_seat parent;

  struct wl_pointer pointer;
  MetaWaylandKeyboard keyboard;

  struct wl_display *display;

  MetaWaylandSurface *sprite;
  int hotspot_x, hotspot_y;
  struct wl_listener sprite_destroy_listener;

  ClutterActor *current_stage;
};

static void
unbind_resource (struct wl_resource *resource)
{
  wl_list_remove (&resource->link);
  free (resource);
}

static void
transform_stage_point_fixed (MetaWaylandSurface *surface,
                             wl_fixed_t x,
                             wl_fixed_t y,
                             wl_fixed_t *sx,
                             wl_fixed_t *sy)
{
  float xf, yf;

  clutter_actor_transform_stage_point (surface->actor,
                                       wl_fixed_to_double (x),
                                       wl_fixed_to_double (y),
                                       &xf, &yf);

  *sx = wl_fixed_from_double (xf);
  *sy = wl_fixed_from_double (yf);
}

static void
pointer_unmap_sprite (MetaWaylandSeat *seat)
{
  if (seat->sprite)
    {
      if (seat->sprite->actor)
        clutter_actor_hide (seat->sprite->actor);
      wl_list_remove (&seat->sprite_destroy_listener.link);
      seat->sprite = NULL;
    }
}

static void
pointer_set_cursor (struct wl_client *client,
                    struct wl_resource *resource,
                    uint32_t serial,
                    struct wl_resource *surface_resource,
                    int32_t x, int32_t y)
{
  MetaWaylandSeat *seat = resource->data;
  MetaWaylandSurface *surface;

  surface = surface_resource ? surface_resource->data : NULL;

  if (seat->parent.pointer->focus == NULL)
    return;
  if (seat->parent.pointer->focus->resource.client != client)
    return;
  if (seat->parent.pointer->focus_serial - serial > G_MAXUINT32 / 2)
    return;

  pointer_unmap_sprite (seat);

  if (!surface)
    return;

  wl_signal_add (&surface->wayland_surface.resource.destroy_signal,
                 &seat->sprite_destroy_listener);

  seat->sprite = surface;
  seat->hotspot_x = x;
  seat->hotspot_y = y;
}

static const struct wl_pointer_interface
pointer_interface =
  {
    pointer_set_cursor
  };

static void
seat_get_pointer (struct wl_client *client,
                  struct wl_resource *resource,
                  uint32_t id)
{
  MetaWaylandSeat *seat = resource->data;
  struct wl_resource *cr;

  if (!seat->parent.pointer)
    return;

  cr = wl_client_add_object (client, &wl_pointer_interface,
                             &pointer_interface, id, seat);
  wl_list_insert (&seat->parent.pointer->resource_list, &cr->link);
  cr->destroy = unbind_resource;

  if (seat->parent.pointer->focus &&
      seat->parent.pointer->focus->resource.client == client)
    {
      MetaWaylandSurface *surface;
      wl_fixed_t sx, sy;

      surface = (MetaWaylandSurface *) seat->parent.pointer->focus;
      transform_stage_point_fixed (surface,
                                   seat->parent.pointer->x,
                                   seat->parent.pointer->y,
                                   &sx, &sy);
      wl_pointer_set_focus (seat->parent.pointer,
                            seat->parent.pointer->focus,
                            sx, sy);
    }
}

static void
seat_get_keyboard (struct wl_client *client,
                   struct wl_resource *resource,
                   uint32_t id)
{
  MetaWaylandSeat *seat = resource->data;
  struct wl_resource *cr;

  if (!seat->parent.keyboard)
    return;

  cr = wl_client_add_object (client, &wl_keyboard_interface, NULL, id, seat);
  wl_list_insert (&seat->parent.keyboard->resource_list, &cr->link);
  cr->destroy = unbind_resource;

  wl_keyboard_send_keymap (cr,
                           WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
                           seat->keyboard.xkb_info.keymap_fd,
                           seat->keyboard.xkb_info.keymap_size);

  if (seat->parent.keyboard->focus &&
      seat->parent.keyboard->focus->resource.client == client)
    {
      wl_keyboard_set_focus (seat->parent.keyboard,
                             seat->parent.keyboard->focus);
      wl_data_device_set_keyboard_focus (&seat->parent);
    }
}

static void
seat_get_touch (struct wl_client *client,
                struct wl_resource *resource,
                uint32_t id)
{
  /* Touch not supported */
}

static const struct wl_seat_interface
seat_interface =
  {
    seat_get_pointer,
    seat_get_keyboard,
    seat_get_touch
  };

static void
bind_seat (struct wl_client *client,
           void *data,
           guint32 version,
           guint32 id)
{
  struct wl_seat *seat = data;
  struct wl_resource *resource;

  resource = wl_client_add_object (client,
                                   &wl_seat_interface,
                                   &seat_interface,
                                   id,
                                   data);
  wl_list_insert (&seat->base_resource_list, &resource->link);
  resource->destroy = unbind_resource;

  wl_seat_send_capabilities (resource,
                             WL_SEAT_CAPABILITY_POINTER |
                             WL_SEAT_CAPABILITY_KEYBOARD);
}

static void
pointer_handle_sprite_destroy (struct wl_listener *listener, void *data)
{
  MetaWaylandSeat *seat =
    wl_container_of (listener, seat, sprite_destroy_listener);

  seat->sprite = NULL;
}

MetaWaylandSeat *
meta_wayland_seat_new (struct wl_display *display)
{
  MetaWaylandSeat *seat = g_new (MetaWaylandSeat, 1);

  wl_seat_init (&seat->parent);

  wl_pointer_init (&seat->pointer);
  wl_seat_set_pointer (&seat->parent, &seat->pointer);

  meta_wayland_keyboard_init (&seat->keyboard, display);
  wl_seat_set_keyboard (&seat->parent, &seat->keyboard.parent);

  seat->display = display;

  seat->current_stage = 0;

  seat->sprite = NULL;
  seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
  seat->hotspot_x = 16;
  seat->hotspot_y = 16;

  wl_display_add_global (display, &wl_seat_interface, seat, bind_seat);

  return seat;
}

static void
notify_motion (MetaWaylandSeat *seat,
               const ClutterEvent *event)
{
  struct wl_pointer *pointer = seat->parent.pointer;
  float x, y;

  clutter_event_get_coords (event, &x, &y);
  pointer->x = wl_fixed_from_double (x);
  pointer->y = wl_fixed_from_double (y);

  meta_wayland_seat_repick (seat,
                            clutter_event_get_time (event),
                            clutter_event_get_source (event));

  pointer->grab->interface->motion (pointer->grab,
                                    clutter_event_get_time (event),
                                    pointer->grab->x,
                                    pointer->grab->y);
}

static void
handle_motion_event (MetaWaylandSeat *seat,
                     const ClutterMotionEvent *event)
{
  notify_motion (seat, (const ClutterEvent *) event);
}

static void
handle_button_event (MetaWaylandSeat *seat,
                     const ClutterButtonEvent *event)
{
  struct wl_pointer *pointer = seat->parent.pointer;
  gboolean state = event->type == CLUTTER_BUTTON_PRESS;
  uint32_t button;

  notify_motion (seat, (const ClutterEvent *) event);

  switch (event->button)
    {
      /* The evdev input right and middle button numbers are swapped
         relative to how Clutter numbers them */
    case 2:
      button = BTN_MIDDLE;
      break;

    case 3:
      button = BTN_RIGHT;
      break;

    default:
      button = event->button + BTN_LEFT - 1;
      break;
    }

  if (state)
    {
      if (pointer->button_count == 0)
        {
          pointer->grab_button = button;
          pointer->grab_time = event->time;
          pointer->grab_x = pointer->x;
          pointer->grab_y = pointer->y;
        }

      pointer->button_count++;
    }
  else
    pointer->button_count--;

  pointer->grab->interface->button (pointer->grab, event->time, button, state);

  if (pointer->button_count == 1)
    pointer->grab_serial = wl_display_get_serial (seat->display);
}

void
meta_wayland_seat_handle_event (MetaWaylandSeat *seat,
                                const ClutterEvent *event)
{
  switch (event->type)
    {
    case CLUTTER_MOTION:
      handle_motion_event (seat,
                           (const ClutterMotionEvent *) event);
      break;

    case CLUTTER_BUTTON_PRESS:
    case CLUTTER_BUTTON_RELEASE:
      handle_button_event (seat,
                           (const ClutterButtonEvent *) event);
      break;

    case CLUTTER_KEY_PRESS:
    case CLUTTER_KEY_RELEASE:
      meta_wayland_keyboard_handle_event (&seat->keyboard,
                                          (const ClutterKeyEvent *) event);
      break;

    default:
      break;
    }
}

/* The actor argument can be NULL in which case a Clutter pick will be
   performed to determine the right actor. An actor should only be
   passed if the repick is being performed due to an event in which
   case Clutter will have already performed a pick so we can avoid
   redundantly doing another one */
void
meta_wayland_seat_repick (MetaWaylandSeat *seat,
                          uint32_t time,
                          ClutterActor *actor)
{
  struct wl_pointer *pointer = seat->parent.pointer;
  struct wl_surface *surface;
  MetaWaylandSurface *focus;

  if (actor == NULL && seat->current_stage)
    {
      ClutterStage *stage = CLUTTER_STAGE (seat->current_stage);
      actor = clutter_stage_get_actor_at_pos (stage,
                                              CLUTTER_PICK_REACTIVE,
                                              wl_fixed_to_double (pointer->x),
                                              wl_fixed_to_double (pointer->y));
    }

  if (actor)
    seat->current_stage = clutter_actor_get_stage (actor);
  else
    seat->current_stage = NULL;

  if (CLUTTER_WAYLAND_IS_SURFACE (actor))
    {
      ClutterWaylandSurface *wl_surface = CLUTTER_WAYLAND_SURFACE (actor);
      float ax, ay;

      clutter_actor_transform_stage_point (actor,
                                           wl_fixed_to_double (pointer->x),
                                           wl_fixed_to_double (pointer->y),
                                           &ax, &ay);
      pointer->current_x = wl_fixed_from_double (ax);
      pointer->current_y = wl_fixed_from_double (ay);

      surface = clutter_wayland_surface_get_surface (wl_surface);
    }
  else
    surface = NULL;

  if (surface != pointer->current)
    {
      const struct wl_pointer_grab_interface *interface =
        pointer->grab->interface;
      interface->focus (pointer->grab,
                        surface,
                        pointer->current_x, pointer->current_y);
      pointer->current = surface;
    }

  focus = (MetaWaylandSurface *) pointer->grab->focus;
  if (focus)
    {
      float ax, ay;

      clutter_actor_transform_stage_point (focus->actor,
                                           wl_fixed_to_double (pointer->x),
                                           wl_fixed_to_double (pointer->y),
                                           &ax, &ay);
      pointer->grab->x = wl_fixed_from_double (ax);
      pointer->grab->y = wl_fixed_from_double (ay);
    }
}

void
meta_wayland_seat_free (MetaWaylandSeat *seat)
{
  pointer_unmap_sprite (seat);

  wl_pointer_release (&seat->pointer);
  meta_wayland_keyboard_release (&seat->keyboard);
  wl_seat_release (&seat->parent);

  g_slice_free (MetaWaylandSeat, seat);
}