blob: 3ac02d45fdae3a876fba9b2e4dbedefda3a69a63 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040026#include <sys/mman.h>
27#include <assert.h>
28#include <unistd.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040029
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040030#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040031#include "compositor.h"
32
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040033static void
34empty_region(pixman_region32_t *region)
35{
36 pixman_region32_fini(region);
37 pixman_region32_init(region);
38}
39
40static void unbind_resource(struct wl_resource *resource)
41{
42 wl_list_remove(&resource->link);
43 free(resource);
44}
45
46void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040047weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040048{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040049 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050 struct weston_surface *surface, *focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -040051 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052
53 if (!pointer)
54 return;
55
56 surface = weston_compositor_pick_surface(seat->compositor,
57 pointer->x,
58 pointer->y,
59 &pointer->current_x,
60 &pointer->current_y);
61
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -040062 if (surface != pointer->current) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040063 interface = pointer->grab->interface;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -040064 weston_pointer_set_current(pointer, surface);
65 interface->focus(pointer->grab, surface,
66 pointer->current_x, pointer->current_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040067 }
68
69 focus = (struct weston_surface *) pointer->grab->focus;
70 if (focus)
71 weston_surface_from_global_fixed(focus,
72 pointer->x,
73 pointer->y,
74 &pointer->grab->x,
75 &pointer->grab->y);
76}
77
78static void
79weston_compositor_idle_inhibit(struct weston_compositor *compositor)
80{
81 weston_compositor_wake(compositor);
82 compositor->idle_inhibit++;
83}
84
85static void
86weston_compositor_idle_release(struct weston_compositor *compositor)
87{
88 compositor->idle_inhibit--;
89 weston_compositor_wake(compositor);
90}
91
Kristian Høgsberg2158a882013-04-18 15:07:39 -040092static void
93lose_pointer_focus(struct wl_listener *listener, void *data)
94{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040095 struct weston_pointer *pointer =
96 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040097
98 pointer->focus_resource = NULL;
99}
100
101static void
102lose_keyboard_focus(struct wl_listener *listener, void *data)
103{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400104 struct weston_keyboard *keyboard =
105 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400106
107 keyboard->focus_resource = NULL;
108}
109
110static void
111lose_touch_focus(struct wl_listener *listener, void *data)
112{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400113 struct weston_touch *touch =
114 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115
116 touch->focus_resource = NULL;
117}
118
119static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400120default_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400121 struct weston_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400122{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400123 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400124
125 if (pointer->button_count > 0)
126 return;
127
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400128 weston_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400129}
130
131static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400132default_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400133 uint32_t time, wl_fixed_t x, wl_fixed_t y)
134{
135 struct wl_resource *resource;
136
137 resource = grab->pointer->focus_resource;
138 if (resource)
139 wl_pointer_send_motion(resource, time, x, y);
140}
141
142static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400143default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400144 uint32_t time, uint32_t button, uint32_t state_w)
145{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400146 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400147 struct wl_resource *resource;
148 uint32_t serial;
149 enum wl_pointer_button_state state = state_w;
150 struct wl_display *display;
151
152 resource = pointer->focus_resource;
153 if (resource) {
154 display = wl_client_get_display(resource->client);
155 serial = wl_display_next_serial(display);
156 wl_pointer_send_button(resource, serial, time, button, state_w);
157 }
158
159 if (pointer->button_count == 0 &&
160 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400161 weston_pointer_set_focus(pointer, pointer->current,
162 pointer->current_x,
163 pointer->current_y);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400164}
165
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400166static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400167 default_pointer_grab_interface = {
168 default_grab_focus,
169 default_grab_motion,
170 default_grab_button
171};
172
Kristian Høgsberge329f362013-05-06 22:19:57 -0400173static void
174default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
175 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400176{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400177 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400178 struct wl_display *display;
179 uint32_t serial;
180
181 if (touch->focus_resource && touch->focus) {
182 display = wl_client_get_display(touch->focus_resource->client);
183 serial = wl_display_next_serial(display);
184 wl_touch_send_down(touch->focus_resource, serial, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400185 &touch->focus->resource,
186 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400187 }
188}
189
Kristian Høgsberge329f362013-05-06 22:19:57 -0400190static void
191default_grab_touch_up(struct weston_touch_grab *grab,
192 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400193{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400194 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400195 struct wl_display *display;
196 uint32_t serial;
197
198 if (touch->focus_resource) {
199 display = wl_client_get_display(touch->focus_resource->client);
200 serial = wl_display_next_serial(display);
201 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
202 }
203}
204
Kristian Høgsberge329f362013-05-06 22:19:57 -0400205static void
206default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
207 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400209 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400210
211 if (touch->focus_resource) {
212 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400213 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400214 }
215}
216
Kristian Høgsberge329f362013-05-06 22:19:57 -0400217static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400218 default_grab_touch_down,
219 default_grab_touch_up,
220 default_grab_touch_motion
221};
222
223static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400224default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400225 uint32_t time, uint32_t key, uint32_t state)
226{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400227 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228 struct wl_resource *resource;
229 struct wl_display *display;
230 uint32_t serial;
231
232 resource = keyboard->focus_resource;
233 if (resource) {
234 display = wl_client_get_display(resource->client);
235 serial = wl_display_next_serial(display);
236 wl_keyboard_send_key(resource, serial, time, key, state);
237 }
238}
239
240static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400241find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400242{
243 struct wl_resource *r;
244
245 if (!surface)
246 return NULL;
247
248 wl_list_for_each(r, list, link) {
249 if (r->client == surface->resource.client)
250 return r;
251 }
252
253 return NULL;
254}
255
256static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400257default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258 uint32_t mods_depressed, uint32_t mods_latched,
259 uint32_t mods_locked, uint32_t group)
260{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400261 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400262 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400263 struct wl_resource *resource, *pr;
264
265 resource = keyboard->focus_resource;
266 if (!resource)
267 return;
268
269 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
270 mods_latched, mods_locked, group);
271
272 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
273 pr = find_resource_for_surface(&keyboard->resource_list,
274 pointer->focus);
275 if (pr) {
276 wl_keyboard_send_modifiers(pr,
277 serial,
278 keyboard->modifiers.mods_depressed,
279 keyboard->modifiers.mods_latched,
280 keyboard->modifiers.mods_locked,
281 keyboard->modifiers.group);
282 }
283 }
284}
285
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400286static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287 default_keyboard_grab_interface = {
288 default_grab_key,
289 default_grab_modifiers,
290};
291
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400292static void
293pointer_unmap_sprite(struct weston_pointer *pointer)
294{
295 if (weston_surface_is_mapped(pointer->sprite))
296 weston_surface_unmap(pointer->sprite);
297
298 wl_list_remove(&pointer->sprite_destroy_listener.link);
299 pointer->sprite->configure = NULL;
300 pointer->sprite->configure_private = NULL;
301 pointer->sprite = NULL;
302}
303
304static void
305pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
306{
307 struct weston_pointer *pointer =
308 container_of(listener, struct weston_pointer,
309 sprite_destroy_listener);
310
311 pointer->sprite = NULL;
312}
313
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400314WL_EXPORT struct weston_pointer *
315weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400316{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400317 struct weston_pointer *pointer;
318
319 pointer = malloc(sizeof *pointer);
320 if (pointer == NULL)
321 return NULL;
322
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323 memset(pointer, 0, sizeof *pointer);
324 wl_list_init(&pointer->resource_list);
325 pointer->focus_listener.notify = lose_pointer_focus;
326 pointer->default_grab.interface = &default_pointer_grab_interface;
327 pointer->default_grab.pointer = pointer;
328 pointer->grab = &pointer->default_grab;
329 wl_signal_init(&pointer->focus_signal);
330
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400331 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
332
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333 /* FIXME: Pick better co-ords. */
334 pointer->x = wl_fixed_from_int(100);
335 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400336
337 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338}
339
340WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400341weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400343 if (pointer->sprite)
344 pointer_unmap_sprite(pointer);
345
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400346 /* XXX: What about pointer->resource_list? */
347 if (pointer->focus_resource)
348 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400349 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400350}
351
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400352WL_EXPORT struct weston_keyboard *
353weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400355 struct weston_keyboard *keyboard;
356
357 keyboard = malloc(sizeof *keyboard);
358 if (keyboard == NULL)
359 return NULL;
360
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361 memset(keyboard, 0, sizeof *keyboard);
362 wl_list_init(&keyboard->resource_list);
363 wl_array_init(&keyboard->keys);
364 keyboard->focus_listener.notify = lose_keyboard_focus;
365 keyboard->default_grab.interface = &default_keyboard_grab_interface;
366 keyboard->default_grab.keyboard = keyboard;
367 keyboard->grab = &keyboard->default_grab;
368 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400369
370 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400371}
372
373WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400374weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400375{
376 /* XXX: What about keyboard->resource_list? */
377 if (keyboard->focus_resource)
378 wl_list_remove(&keyboard->focus_listener.link);
379 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400380 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400381}
382
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400383WL_EXPORT struct weston_touch *
384weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400386 struct weston_touch *touch;
387
388 touch = malloc(sizeof *touch);
389 if (touch == NULL)
390 return NULL;
391
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392 memset(touch, 0, sizeof *touch);
393 wl_list_init(&touch->resource_list);
394 touch->focus_listener.notify = lose_touch_focus;
395 touch->default_grab.interface = &default_touch_grab_interface;
396 touch->default_grab.touch = touch;
397 touch->grab = &touch->default_grab;
398 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400399
400 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401}
402
403WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400404weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405{
406 /* XXX: What about touch->resource_list? */
407 if (touch->focus_resource)
408 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400409 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410}
411
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400412static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400413seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414{
415 struct wl_resource *r;
416 enum wl_seat_capability caps = 0;
417
418 if (seat->pointer)
419 caps |= WL_SEAT_CAPABILITY_POINTER;
420 if (seat->keyboard)
421 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
422 if (seat->touch)
423 caps |= WL_SEAT_CAPABILITY_TOUCH;
424
425 wl_list_for_each(r, &seat->base_resource_list, link)
426 wl_seat_send_capabilities(r, caps);
427}
428
429WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400430weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400431 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400432 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400434 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435 struct wl_resource *resource, *kr;
436 struct wl_display *display;
437 uint32_t serial;
438
439 resource = pointer->focus_resource;
440 if (resource && pointer->focus != surface) {
441 display = wl_client_get_display(resource->client);
442 serial = wl_display_next_serial(display);
443 wl_pointer_send_leave(resource, serial,
444 &pointer->focus->resource);
445 wl_list_remove(&pointer->focus_listener.link);
446 }
447
448 resource = find_resource_for_surface(&pointer->resource_list,
449 surface);
450 if (resource &&
451 (pointer->focus != surface ||
452 pointer->focus_resource != resource)) {
453 display = wl_client_get_display(resource->client);
454 serial = wl_display_next_serial(display);
455 if (kbd) {
456 kr = find_resource_for_surface(&kbd->resource_list,
457 surface);
458 if (kr) {
459 wl_keyboard_send_modifiers(kr,
460 serial,
461 kbd->modifiers.mods_depressed,
462 kbd->modifiers.mods_latched,
463 kbd->modifiers.mods_locked,
464 kbd->modifiers.group);
465 }
466 }
467 wl_pointer_send_enter(resource, serial, &surface->resource,
468 sx, sy);
469 wl_signal_add(&resource->destroy_signal,
470 &pointer->focus_listener);
471 pointer->focus_serial = serial;
472 }
473
474 pointer->focus_resource = resource;
475 pointer->focus = surface;
476 pointer->default_grab.focus = surface;
477 wl_signal_emit(&pointer->focus_signal, pointer);
478}
479
480WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400481weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400482 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483{
484 struct wl_resource *resource;
485 struct wl_display *display;
486 uint32_t serial;
487
488 if (keyboard->focus_resource && keyboard->focus != surface) {
489 resource = keyboard->focus_resource;
490 display = wl_client_get_display(resource->client);
491 serial = wl_display_next_serial(display);
492 wl_keyboard_send_leave(resource, serial,
493 &keyboard->focus->resource);
494 wl_list_remove(&keyboard->focus_listener.link);
495 }
496
497 resource = find_resource_for_surface(&keyboard->resource_list,
498 surface);
499 if (resource &&
500 (keyboard->focus != surface ||
501 keyboard->focus_resource != resource)) {
502 display = wl_client_get_display(resource->client);
503 serial = wl_display_next_serial(display);
504 wl_keyboard_send_modifiers(resource, serial,
505 keyboard->modifiers.mods_depressed,
506 keyboard->modifiers.mods_latched,
507 keyboard->modifiers.mods_locked,
508 keyboard->modifiers.group);
509 wl_keyboard_send_enter(resource, serial, &surface->resource,
510 &keyboard->keys);
511 wl_signal_add(&resource->destroy_signal,
512 &keyboard->focus_listener);
513 keyboard->focus_serial = serial;
514 }
515
516 keyboard->focus_resource = resource;
517 keyboard->focus = surface;
518 wl_signal_emit(&keyboard->focus_signal, keyboard);
519}
520
521WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400522weston_keyboard_start_grab(struct weston_keyboard *keyboard,
523 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524{
525 keyboard->grab = grab;
526 grab->keyboard = keyboard;
527
528 /* XXX focus? */
529}
530
531WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400532weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533{
534 keyboard->grab = &keyboard->default_grab;
535}
536
537WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400538weston_pointer_start_grab(struct weston_pointer *pointer,
539 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400541 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400542
543 pointer->grab = grab;
544 interface = pointer->grab->interface;
545 grab->pointer = pointer;
546
547 if (pointer->current)
548 interface->focus(pointer->grab, pointer->current,
549 pointer->current_x, pointer->current_y);
550}
551
552WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400553weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400555 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556
557 pointer->grab = &pointer->default_grab;
558 interface = pointer->grab->interface;
559 interface->focus(pointer->grab, pointer->current,
560 pointer->current_x, pointer->current_y);
561}
562
563static void
564current_surface_destroy(struct wl_listener *listener, void *data)
565{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400566 struct weston_pointer *pointer =
567 container_of(listener, struct weston_pointer, current_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568
569 pointer->current = NULL;
570}
571
572WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400573weston_pointer_set_current(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400574 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400575{
576 if (pointer->current)
577 wl_list_remove(&pointer->current_listener.link);
578
579 pointer->current = surface;
580
581 if (!surface)
582 return;
583
584 wl_signal_add(&surface->resource.destroy_signal,
585 &pointer->current_listener);
586 pointer->current_listener.notify = current_surface_destroy;
587}
588
589WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400590weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591{
592 touch->grab = grab;
593 grab->touch = touch;
594}
595
596WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400597weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400598{
599 touch->grab = &touch->default_grab;
600}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400601
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400602static void
603clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
604{
605 struct weston_compositor *ec = seat->compositor;
606 struct weston_output *output, *prev = NULL;
607 int x, y, old_x, old_y, valid = 0;
608
609 x = wl_fixed_to_int(*fx);
610 y = wl_fixed_to_int(*fy);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400611 old_x = wl_fixed_to_int(seat->pointer->x);
612 old_y = wl_fixed_to_int(seat->pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400613
614 wl_list_for_each(output, &ec->output_list, link) {
615 if (pixman_region32_contains_point(&output->region,
616 x, y, NULL))
617 valid = 1;
618 if (pixman_region32_contains_point(&output->region,
619 old_x, old_y, NULL))
620 prev = output;
621 }
622
623 if (!valid) {
624 if (x < prev->x)
625 *fx = wl_fixed_from_int(prev->x);
626 else if (x >= prev->x + prev->width)
627 *fx = wl_fixed_from_int(prev->x +
628 prev->width - 1);
629 if (y < prev->y)
630 *fy = wl_fixed_from_int(prev->y);
631 else if (y >= prev->y + prev->current->height)
632 *fy = wl_fixed_from_int(prev->y +
633 prev->height - 1);
634 }
635}
636
637/* Takes absolute values */
638static void
639move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
640{
641 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400642 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400643 struct weston_output *output;
644 int32_t ix, iy;
645
646 clip_pointer_motion(seat, &x, &y);
647
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648 pointer->x = x;
649 pointer->y = y;
650
651 ix = wl_fixed_to_int(x);
652 iy = wl_fixed_to_int(y);
653
654 wl_list_for_each(output, &ec->output_list, link)
655 if (output->zoom.active &&
656 pixman_region32_contains_point(&output->region,
657 ix, iy, NULL))
658 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
659
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400660 weston_seat_repick(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400661
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400662 if (pointer->sprite) {
663 weston_surface_set_position(pointer->sprite,
664 ix - pointer->hotspot_x,
665 iy - pointer->hotspot_y);
666 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400667 }
668}
669
670WL_EXPORT void
671notify_motion(struct weston_seat *seat,
672 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
673{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400674 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400675 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400676 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400677
678 weston_compositor_wake(ec);
679
680 move_pointer(seat, pointer->x + dx, pointer->y + dy);
681
682 interface = pointer->grab->interface;
683 interface->motion(pointer->grab, time,
684 pointer->grab->x, pointer->grab->y);
685}
686
687WL_EXPORT void
688notify_motion_absolute(struct weston_seat *seat,
689 uint32_t time, wl_fixed_t x, wl_fixed_t y)
690{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400691 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400692 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400693 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400694
695 weston_compositor_wake(ec);
696
697 move_pointer(seat, x, y);
698
699 interface = pointer->grab->interface;
700 interface->motion(pointer->grab, time,
701 pointer->grab->x, pointer->grab->y);
702}
703
704WL_EXPORT void
705weston_surface_activate(struct weston_surface *surface,
706 struct weston_seat *seat)
707{
708 struct weston_compositor *compositor = seat->compositor;
709
Kristian Høgsberge3148752013-05-06 23:19:49 -0400710 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400711 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400712 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400713 }
714
715 wl_signal_emit(&compositor->activate_signal, surface);
716}
717
718WL_EXPORT void
719notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
720 enum wl_pointer_button_state state)
721{
722 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400723 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400724 struct weston_surface *focus =
725 (struct weston_surface *) pointer->focus;
726 uint32_t serial = wl_display_next_serial(compositor->wl_display);
727
728 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
729 if (compositor->ping_handler && focus)
730 compositor->ping_handler(focus, serial);
731 weston_compositor_idle_inhibit(compositor);
732 if (pointer->button_count == 0) {
733 pointer->grab_button = button;
734 pointer->grab_time = time;
735 pointer->grab_x = pointer->x;
736 pointer->grab_y = pointer->y;
737 }
738 pointer->button_count++;
739 } else {
740 weston_compositor_idle_release(compositor);
741 pointer->button_count--;
742 }
743
744 weston_compositor_run_button_binding(compositor, seat, time, button,
745 state);
746
747 pointer->grab->interface->button(pointer->grab, time, button, state);
748
749 if (pointer->button_count == 1)
750 pointer->grab_serial =
751 wl_display_get_serial(compositor->wl_display);
752}
753
754WL_EXPORT void
755notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
756 wl_fixed_t value)
757{
758 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400759 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400760 struct weston_surface *focus =
761 (struct weston_surface *) pointer->focus;
762 uint32_t serial = wl_display_next_serial(compositor->wl_display);
763
764 if (compositor->ping_handler && focus)
765 compositor->ping_handler(focus, serial);
766
767 weston_compositor_wake(compositor);
768
769 if (!value)
770 return;
771
772 if (weston_compositor_run_axis_binding(compositor, seat,
773 time, axis, value))
774 return;
775
776 if (pointer->focus_resource)
777 wl_pointer_send_axis(pointer->focus_resource, time, axis,
778 value);
779}
780
781WL_EXPORT void
782notify_modifiers(struct weston_seat *seat, uint32_t serial)
783{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400784 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400785 struct weston_keyboard_grab *grab = keyboard->grab;
786 uint32_t mods_depressed, mods_latched, mods_locked, group;
787 uint32_t mods_lookup;
788 enum weston_led leds = 0;
789 int changed = 0;
790
791 /* Serialize and update our internal state, checking to see if it's
792 * different to the previous state. */
793 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
794 XKB_STATE_DEPRESSED);
795 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
796 XKB_STATE_LATCHED);
797 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
798 XKB_STATE_LOCKED);
799 group = xkb_state_serialize_group(seat->xkb_state.state,
800 XKB_STATE_EFFECTIVE);
801
Kristian Høgsberge3148752013-05-06 23:19:49 -0400802 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
803 mods_latched != seat->keyboard->modifiers.mods_latched ||
804 mods_locked != seat->keyboard->modifiers.mods_locked ||
805 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400806 changed = 1;
807
Kristian Høgsberge3148752013-05-06 23:19:49 -0400808 seat->keyboard->modifiers.mods_depressed = mods_depressed;
809 seat->keyboard->modifiers.mods_latched = mods_latched;
810 seat->keyboard->modifiers.mods_locked = mods_locked;
811 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400812
813 /* And update the modifier_state for bindings. */
814 mods_lookup = mods_depressed | mods_latched;
815 seat->modifier_state = 0;
816 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
817 seat->modifier_state |= MODIFIER_CTRL;
818 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
819 seat->modifier_state |= MODIFIER_ALT;
820 if (mods_lookup & (1 << seat->xkb_info.super_mod))
821 seat->modifier_state |= MODIFIER_SUPER;
822 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
823 seat->modifier_state |= MODIFIER_SHIFT;
824
825 /* Finally, notify the compositor that LEDs have changed. */
826 if (xkb_state_led_index_is_active(seat->xkb_state.state,
827 seat->xkb_info.num_led))
828 leds |= LED_NUM_LOCK;
829 if (xkb_state_led_index_is_active(seat->xkb_state.state,
830 seat->xkb_info.caps_led))
831 leds |= LED_CAPS_LOCK;
832 if (xkb_state_led_index_is_active(seat->xkb_state.state,
833 seat->xkb_info.scroll_led))
834 leds |= LED_SCROLL_LOCK;
835 if (leds != seat->xkb_state.leds && seat->led_update)
836 seat->led_update(seat, leds);
837 seat->xkb_state.leds = leds;
838
839 if (changed) {
840 grab->interface->modifiers(grab,
841 serial,
842 keyboard->modifiers.mods_depressed,
843 keyboard->modifiers.mods_latched,
844 keyboard->modifiers.mods_locked,
845 keyboard->modifiers.group);
846 }
847}
848
849static void
850update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
851 enum wl_keyboard_key_state state)
852{
853 enum xkb_key_direction direction;
854
855 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
856 direction = XKB_KEY_DOWN;
857 else
858 direction = XKB_KEY_UP;
859
860 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
861 * broken keycode system, which starts at 8. */
862 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
863
864 notify_modifiers(seat, serial);
865}
866
867WL_EXPORT void
868notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
869 enum wl_keyboard_key_state state,
870 enum weston_key_state_update update_state)
871{
872 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400873 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400874 struct weston_surface *focus =
875 (struct weston_surface *) keyboard->focus;
876 struct weston_keyboard_grab *grab = keyboard->grab;
877 uint32_t serial = wl_display_next_serial(compositor->wl_display);
878 uint32_t *k, *end;
879
880 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
881 if (compositor->ping_handler && focus)
882 compositor->ping_handler(focus, serial);
883
884 weston_compositor_idle_inhibit(compositor);
885 keyboard->grab_key = key;
886 keyboard->grab_time = time;
887 } else {
888 weston_compositor_idle_release(compositor);
889 }
890
891 end = keyboard->keys.data + keyboard->keys.size;
892 for (k = keyboard->keys.data; k < end; k++) {
893 if (*k == key) {
894 /* Ignore server-generated repeats. */
895 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
896 return;
897 *k = *--end;
898 }
899 }
900 keyboard->keys.size = (void *) end - keyboard->keys.data;
901 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
902 k = wl_array_add(&keyboard->keys, sizeof *k);
903 *k = key;
904 }
905
906 if (grab == &keyboard->default_grab ||
907 grab == &keyboard->input_method_grab) {
908 weston_compositor_run_key_binding(compositor, seat, time, key,
909 state);
910 grab = keyboard->grab;
911 }
912
913 grab->interface->key(grab, time, key, state);
914
915 if (update_state == STATE_UPDATE_AUTOMATIC) {
916 update_modifier_state(seat,
917 wl_display_get_serial(compositor->wl_display),
918 key,
919 state);
920 }
921}
922
923WL_EXPORT void
924notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
925 wl_fixed_t x, wl_fixed_t y)
926{
927 struct weston_compositor *compositor = seat->compositor;
928
929 if (output) {
930 move_pointer(seat, x, y);
931 compositor->focus = 1;
932 } else {
933 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400934 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400935 * NULL) here, but somehow that breaks re-entry... */
936 }
937}
938
939static void
940destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
941{
942 struct weston_seat *ws;
943
944 ws = container_of(listener, struct weston_seat,
945 saved_kbd_focus_listener);
946
947 ws->saved_kbd_focus = NULL;
948}
949
950WL_EXPORT void
951notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
952 enum weston_key_state_update update_state)
953{
954 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400955 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400956 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400957 uint32_t *k, serial;
958
959 serial = wl_display_next_serial(compositor->wl_display);
960 wl_array_copy(&keyboard->keys, keys);
961 wl_array_for_each(k, &keyboard->keys) {
962 weston_compositor_idle_inhibit(compositor);
963 if (update_state == STATE_UPDATE_AUTOMATIC)
964 update_modifier_state(seat, serial, *k,
965 WL_KEYBOARD_KEY_STATE_PRESSED);
966 }
967
968 /* Run key bindings after we've updated the state. */
969 wl_array_for_each(k, &keyboard->keys) {
970 weston_compositor_run_key_binding(compositor, seat, 0, *k,
971 WL_KEYBOARD_KEY_STATE_PRESSED);
972 }
973
974 surface = seat->saved_kbd_focus;
975
976 if (surface) {
977 wl_list_remove(&seat->saved_kbd_focus_listener.link);
978 weston_keyboard_set_focus(keyboard, surface);
979 seat->saved_kbd_focus = NULL;
980 }
981}
982
983WL_EXPORT void
984notify_keyboard_focus_out(struct weston_seat *seat)
985{
986 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400987 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400988 uint32_t *k, serial;
989
990 serial = wl_display_next_serial(compositor->wl_display);
991 wl_array_for_each(k, &keyboard->keys) {
992 weston_compositor_idle_release(compositor);
993 update_modifier_state(seat, serial, *k,
994 WL_KEYBOARD_KEY_STATE_RELEASED);
995 }
996
997 seat->modifier_state = 0;
998
999 if (keyboard->focus) {
1000 seat->saved_kbd_focus = keyboard->focus;
1001 seat->saved_kbd_focus_listener.notify =
1002 destroy_device_saved_kbd_focus;
1003 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1004 &seat->saved_kbd_focus_listener);
1005 }
1006
1007 weston_keyboard_set_focus(keyboard, NULL);
1008 /* FIXME: We really need keyboard grab cancel here to
1009 * let the grab shut down properly. As it is we leak
1010 * the grab data. */
1011 weston_keyboard_end_grab(keyboard);
1012}
1013
1014static void
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001015touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001016{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001017 struct wl_resource *resource;
1018
1019 if (seat->touch->focus == surface)
1020 return;
1021
1022 if (seat->touch->focus_resource)
1023 wl_list_remove(&seat->touch->focus_listener.link);
1024 seat->touch->focus = NULL;
1025 seat->touch->focus_resource = NULL;
1026
1027 if (surface) {
1028 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -04001029 find_resource_for_surface(&seat->touch->resource_list,
1030 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001031 if (!resource) {
1032 weston_log("couldn't find resource\n");
1033 return;
1034 }
1035
1036 seat->touch->focus = surface;
1037 seat->touch->focus_resource = resource;
1038 wl_signal_add(&resource->destroy_signal,
1039 &seat->touch->focus_listener);
1040 }
1041}
1042
1043/**
1044 * notify_touch - emulates button touches and notifies surfaces accordingly.
1045 *
1046 * It assumes always the correct cycle sequence until it gets here: touch_down
1047 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1048 * for sending along such order.
1049 *
1050 */
1051WL_EXPORT void
1052notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1053 wl_fixed_t x, wl_fixed_t y, int touch_type)
1054{
1055 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001056 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001057 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001058 struct weston_surface *es;
1059 wl_fixed_t sx, sy;
1060
1061 /* Update grab's global coordinates. */
1062 touch->grab_x = x;
1063 touch->grab_y = y;
1064
1065 switch (touch_type) {
1066 case WL_TOUCH_DOWN:
1067 weston_compositor_idle_inhibit(ec);
1068
1069 seat->num_tp++;
1070
1071 /* the first finger down picks the surface, and all further go
1072 * to that surface for the remainder of the touch session i.e.
1073 * until all touch points are up again. */
1074 if (seat->num_tp == 1) {
1075 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001076 touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001077 } else if (touch->focus) {
1078 es = (struct weston_surface *) touch->focus;
1079 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1080 } else {
1081 /* Unexpected condition: We have non-initial touch but
1082 * there is no focused surface.
1083 */
1084 weston_log("touch event received with %d points down"
1085 "but no surface focused\n", seat->num_tp);
1086 return;
1087 }
1088
1089 grab->interface->down(grab, time, touch_id, sx, sy);
1090 break;
1091 case WL_TOUCH_MOTION:
1092 es = (struct weston_surface *) touch->focus;
1093 if (!es)
1094 break;
1095
1096 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1097 grab->interface->motion(grab, time, touch_id, sx, sy);
1098 break;
1099 case WL_TOUCH_UP:
1100 weston_compositor_idle_release(ec);
1101 seat->num_tp--;
1102
1103 grab->interface->up(grab, time, touch_id);
1104 if (seat->num_tp == 0)
1105 touch_set_focus(seat, NULL);
1106 break;
1107 }
1108}
1109
1110static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001111pointer_cursor_surface_configure(struct weston_surface *es,
1112 int32_t dx, int32_t dy, int32_t width, int32_t height)
1113{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001114 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001115 int x, y;
1116
1117 if (width == 0)
1118 return;
1119
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001120 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001121
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001122 pointer->hotspot_x -= dx;
1123 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001124
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001125 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1126 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001127
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001128 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001129
1130 empty_region(&es->pending.input);
1131
1132 if (!weston_surface_is_mapped(es)) {
1133 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1134 &es->layer_link);
1135 weston_surface_update_transform(es);
1136 }
1137}
1138
1139static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001140pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1141 uint32_t serial, struct wl_resource *surface_resource,
1142 int32_t x, int32_t y)
1143{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001144 struct weston_pointer *pointer = resource->data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001145 struct weston_surface *surface = NULL;
1146
1147 if (surface_resource)
1148 surface = surface_resource->data;
1149
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001150 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001151 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001152 if (pointer->focus->resource.client != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001153 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001154 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001155 return;
1156
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001157 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001158 if (surface->configure) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001159 wl_resource_post_error(&surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001160 WL_DISPLAY_ERROR_INVALID_OBJECT,
1161 "surface->configure already "
1162 "set");
1163 return;
1164 }
1165 }
1166
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001167 if (pointer->sprite)
1168 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169
1170 if (!surface)
1171 return;
1172
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001173 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001174 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001175
1176 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001177 surface->configure_private = pointer;
1178 pointer->sprite = surface;
1179 pointer->hotspot_x = x;
1180 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001181
1182 if (surface->buffer_ref.buffer)
1183 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1184 weston_surface_buffer_height(surface));
1185}
1186
1187static const struct wl_pointer_interface pointer_interface = {
1188 pointer_set_cursor
1189};
1190
1191static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001192seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1193 uint32_t id)
1194{
1195 struct weston_seat *seat = resource->data;
1196 struct wl_resource *cr;
1197
Kristian Høgsberge3148752013-05-06 23:19:49 -04001198 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 return;
1200
1201 cr = wl_client_add_object(client, &wl_pointer_interface,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001202 &pointer_interface, id, seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001203 wl_list_insert(&seat->pointer->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 cr->destroy = unbind_resource;
1205
Kristian Høgsberge3148752013-05-06 23:19:49 -04001206 if (seat->pointer->focus &&
1207 seat->pointer->focus->resource.client == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001208 struct weston_surface *surface;
1209 wl_fixed_t sx, sy;
1210
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001212 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001213 seat->pointer->x,
1214 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001215 &sx,
1216 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001217 weston_pointer_set_focus(seat->pointer,
1218 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001219 sx,
1220 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001221 }
1222}
1223
1224static void
1225seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1226 uint32_t id)
1227{
1228 struct weston_seat *seat = resource->data;
1229 struct wl_resource *cr;
1230
Kristian Høgsberge3148752013-05-06 23:19:49 -04001231 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232 return;
1233
1234 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
1235 seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001236 wl_list_insert(&seat->keyboard->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001237 cr->destroy = unbind_resource;
1238
1239 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1240 seat->xkb_info.keymap_fd,
1241 seat->xkb_info.keymap_size);
1242
Kristian Høgsberge3148752013-05-06 23:19:49 -04001243 if (seat->keyboard->focus &&
1244 seat->keyboard->focus->resource.client == client) {
1245 weston_keyboard_set_focus(seat->keyboard,
1246 seat->keyboard->focus);
1247 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001248 }
1249}
1250
1251static void
1252seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1253 uint32_t id)
1254{
1255 struct weston_seat *seat = resource->data;
1256 struct wl_resource *cr;
1257
Kristian Høgsberge3148752013-05-06 23:19:49 -04001258 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001259 return;
1260
1261 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 wl_list_insert(&seat->touch->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001263 cr->destroy = unbind_resource;
1264}
1265
1266static const struct wl_seat_interface seat_interface = {
1267 seat_get_pointer,
1268 seat_get_keyboard,
1269 seat_get_touch,
1270};
1271
1272static void
1273bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1274{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001275 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001276 struct wl_resource *resource;
1277 enum wl_seat_capability caps = 0;
1278
1279 resource = wl_client_add_object(client, &wl_seat_interface,
1280 &seat_interface, id, data);
1281 wl_list_insert(&seat->base_resource_list, &resource->link);
1282 resource->destroy = unbind_resource;
1283
1284 if (seat->pointer)
1285 caps |= WL_SEAT_CAPABILITY_POINTER;
1286 if (seat->keyboard)
1287 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1288 if (seat->touch)
1289 caps |= WL_SEAT_CAPABILITY_TOUCH;
1290
1291 wl_seat_send_capabilities(resource, caps);
1292}
1293
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001294int
1295weston_compositor_xkb_init(struct weston_compositor *ec,
1296 struct xkb_rule_names *names)
1297{
1298 if (ec->xkb_context == NULL) {
1299 ec->xkb_context = xkb_context_new(0);
1300 if (ec->xkb_context == NULL) {
1301 weston_log("failed to create XKB context\n");
1302 return -1;
1303 }
1304 }
1305
1306 if (names)
1307 ec->xkb_names = *names;
1308 if (!ec->xkb_names.rules)
1309 ec->xkb_names.rules = strdup("evdev");
1310 if (!ec->xkb_names.model)
1311 ec->xkb_names.model = strdup("pc105");
1312 if (!ec->xkb_names.layout)
1313 ec->xkb_names.layout = strdup("us");
1314
1315 return 0;
1316}
1317
1318static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1319{
1320 if (xkb_info->keymap)
1321 xkb_map_unref(xkb_info->keymap);
1322
1323 if (xkb_info->keymap_area)
1324 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1325 if (xkb_info->keymap_fd >= 0)
1326 close(xkb_info->keymap_fd);
1327}
1328
1329void
1330weston_compositor_xkb_destroy(struct weston_compositor *ec)
1331{
1332 free((char *) ec->xkb_names.rules);
1333 free((char *) ec->xkb_names.model);
1334 free((char *) ec->xkb_names.layout);
1335 free((char *) ec->xkb_names.variant);
1336 free((char *) ec->xkb_names.options);
1337
1338 xkb_info_destroy(&ec->xkb_info);
1339 xkb_context_unref(ec->xkb_context);
1340}
1341
1342static int
1343weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1344{
1345 char *keymap_str;
1346
1347 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1348 XKB_MOD_NAME_SHIFT);
1349 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1350 XKB_MOD_NAME_CAPS);
1351 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1352 XKB_MOD_NAME_CTRL);
1353 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1354 XKB_MOD_NAME_ALT);
1355 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1356 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1357 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1358 XKB_MOD_NAME_LOGO);
1359 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1360
1361 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1362 XKB_LED_NAME_NUM);
1363 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1364 XKB_LED_NAME_CAPS);
1365 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1366 XKB_LED_NAME_SCROLL);
1367
1368 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1369 if (keymap_str == NULL) {
1370 weston_log("failed to get string version of keymap\n");
1371 return -1;
1372 }
1373 xkb_info->keymap_size = strlen(keymap_str) + 1;
1374
1375 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1376 if (xkb_info->keymap_fd < 0) {
1377 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1378 (unsigned long) xkb_info->keymap_size);
1379 goto err_keymap_str;
1380 }
1381
1382 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1383 PROT_READ | PROT_WRITE,
1384 MAP_SHARED, xkb_info->keymap_fd, 0);
1385 if (xkb_info->keymap_area == MAP_FAILED) {
1386 weston_log("failed to mmap() %lu bytes\n",
1387 (unsigned long) xkb_info->keymap_size);
1388 goto err_dev_zero;
1389 }
1390 strcpy(xkb_info->keymap_area, keymap_str);
1391 free(keymap_str);
1392
1393 return 0;
1394
1395err_dev_zero:
1396 close(xkb_info->keymap_fd);
1397 xkb_info->keymap_fd = -1;
1398err_keymap_str:
1399 free(keymap_str);
1400 return -1;
1401}
1402
1403static int
1404weston_compositor_build_global_keymap(struct weston_compositor *ec)
1405{
1406 if (ec->xkb_info.keymap != NULL)
1407 return 0;
1408
1409 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1410 &ec->xkb_names,
1411 0);
1412 if (ec->xkb_info.keymap == NULL) {
1413 weston_log("failed to compile global XKB keymap\n");
1414 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1415 "options %s\n",
1416 ec->xkb_names.rules, ec->xkb_names.model,
1417 ec->xkb_names.layout, ec->xkb_names.variant,
1418 ec->xkb_names.options);
1419 return -1;
1420 }
1421
1422 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1423 return -1;
1424
1425 return 0;
1426}
1427
1428WL_EXPORT int
1429weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1430{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001431 struct weston_keyboard *keyboard;
1432
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001433 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001434 return 0;
1435
1436 if (keymap != NULL) {
1437 seat->xkb_info.keymap = xkb_map_ref(keymap);
1438 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1439 return -1;
1440 } else {
1441 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1442 return -1;
1443 seat->xkb_info = seat->compositor->xkb_info;
1444 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1445 }
1446
1447 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1448 if (seat->xkb_state.state == NULL) {
1449 weston_log("failed to initialise XKB state\n");
1450 return -1;
1451 }
1452
1453 seat->xkb_state.leds = 0;
1454
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001455 keyboard = weston_keyboard_create();
1456 if (keyboard == NULL) {
1457 weston_log("failed to allocate weston keyboard struct\n");
1458 return -1;
1459 }
1460
1461 seat->keyboard = keyboard;
1462 keyboard->seat = seat;
1463
1464 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001465
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466 return 0;
1467}
1468
1469WL_EXPORT void
1470weston_seat_init_pointer(struct weston_seat *seat)
1471{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001472 struct weston_pointer *pointer;
1473
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001474 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001475 return;
1476
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001477 pointer = weston_pointer_create();
1478 if (pointer == NULL)
1479 return;
1480
1481 seat->pointer = pointer;
1482 pointer->seat = seat;
1483
1484 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001485}
1486
1487WL_EXPORT void
1488weston_seat_init_touch(struct weston_seat *seat)
1489{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001490 struct weston_touch *touch;
1491
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001492 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001493 return;
1494
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001495 touch = weston_touch_create();
1496 if (touch == NULL)
1497 return;
1498
1499 seat->touch = touch;
1500 touch->seat = seat;
1501
1502 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001503}
1504
1505WL_EXPORT void
1506weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
1507{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001508 memset(seat, 0, sizeof *seat);
1509
Kristian Høgsberge3148752013-05-06 23:19:49 -04001510 seat->selection_data_source = NULL;
1511 wl_list_init(&seat->base_resource_list);
1512 wl_signal_init(&seat->selection_signal);
1513 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001514 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001515
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001516 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
1517 bind_seat);
1518
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001520 seat->modifier_state = 0;
1521 seat->num_tp = 0;
1522
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001523 wl_list_insert(ec->seat_list.prev, &seat->link);
1524
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001525 clipboard_create(seat);
1526
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001527 wl_signal_emit(&ec->seat_created_signal, seat);
1528}
1529
1530WL_EXPORT void
1531weston_seat_release(struct weston_seat *seat)
1532{
1533 wl_list_remove(&seat->link);
1534 /* The global object is destroyed at wl_display_destroy() time. */
1535
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001536 if (seat->xkb_state.state != NULL)
1537 xkb_state_unref(seat->xkb_state.state);
1538 xkb_info_destroy(&seat->xkb_info);
1539
Kristian Høgsberge3148752013-05-06 23:19:49 -04001540 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001541 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001542 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001543 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001544 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001545 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001546
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001547 wl_signal_emit(&seat->destroy_signal, seat);
1548}