blob: 5fdf76f71a36943bfd34915c6ab3a3b178daab5e [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øgsberga4036bb2013-05-07 23:52:07 -0400292WL_EXPORT struct weston_pointer *
293weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400295 struct weston_pointer *pointer;
296
297 pointer = malloc(sizeof *pointer);
298 if (pointer == NULL)
299 return NULL;
300
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400301 memset(pointer, 0, sizeof *pointer);
302 wl_list_init(&pointer->resource_list);
303 pointer->focus_listener.notify = lose_pointer_focus;
304 pointer->default_grab.interface = &default_pointer_grab_interface;
305 pointer->default_grab.pointer = pointer;
306 pointer->grab = &pointer->default_grab;
307 wl_signal_init(&pointer->focus_signal);
308
309 /* FIXME: Pick better co-ords. */
310 pointer->x = wl_fixed_from_int(100);
311 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400312
313 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314}
315
316WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400317weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400318{
319 /* XXX: What about pointer->resource_list? */
320 if (pointer->focus_resource)
321 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400322 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323}
324
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400325WL_EXPORT struct weston_keyboard *
326weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400328 struct weston_keyboard *keyboard;
329
330 keyboard = malloc(sizeof *keyboard);
331 if (keyboard == NULL)
332 return NULL;
333
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334 memset(keyboard, 0, sizeof *keyboard);
335 wl_list_init(&keyboard->resource_list);
336 wl_array_init(&keyboard->keys);
337 keyboard->focus_listener.notify = lose_keyboard_focus;
338 keyboard->default_grab.interface = &default_keyboard_grab_interface;
339 keyboard->default_grab.keyboard = keyboard;
340 keyboard->grab = &keyboard->default_grab;
341 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400342
343 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400344}
345
346WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400347weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400348{
349 /* XXX: What about keyboard->resource_list? */
350 if (keyboard->focus_resource)
351 wl_list_remove(&keyboard->focus_listener.link);
352 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400353 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354}
355
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400356WL_EXPORT struct weston_touch *
357weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400359 struct weston_touch *touch;
360
361 touch = malloc(sizeof *touch);
362 if (touch == NULL)
363 return NULL;
364
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400365 memset(touch, 0, sizeof *touch);
366 wl_list_init(&touch->resource_list);
367 touch->focus_listener.notify = lose_touch_focus;
368 touch->default_grab.interface = &default_touch_grab_interface;
369 touch->default_grab.touch = touch;
370 touch->grab = &touch->default_grab;
371 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400372
373 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400374}
375
376WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400377weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400378{
379 /* XXX: What about touch->resource_list? */
380 if (touch->focus_resource)
381 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400382 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400383}
384
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400386seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400387{
388 struct wl_resource *r;
389 enum wl_seat_capability caps = 0;
390
391 if (seat->pointer)
392 caps |= WL_SEAT_CAPABILITY_POINTER;
393 if (seat->keyboard)
394 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
395 if (seat->touch)
396 caps |= WL_SEAT_CAPABILITY_TOUCH;
397
398 wl_list_for_each(r, &seat->base_resource_list, link)
399 wl_seat_send_capabilities(r, caps);
400}
401
402WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400403weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400404 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400405 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400407 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 struct wl_resource *resource, *kr;
409 struct wl_display *display;
410 uint32_t serial;
411
412 resource = pointer->focus_resource;
413 if (resource && pointer->focus != surface) {
414 display = wl_client_get_display(resource->client);
415 serial = wl_display_next_serial(display);
416 wl_pointer_send_leave(resource, serial,
417 &pointer->focus->resource);
418 wl_list_remove(&pointer->focus_listener.link);
419 }
420
421 resource = find_resource_for_surface(&pointer->resource_list,
422 surface);
423 if (resource &&
424 (pointer->focus != surface ||
425 pointer->focus_resource != resource)) {
426 display = wl_client_get_display(resource->client);
427 serial = wl_display_next_serial(display);
428 if (kbd) {
429 kr = find_resource_for_surface(&kbd->resource_list,
430 surface);
431 if (kr) {
432 wl_keyboard_send_modifiers(kr,
433 serial,
434 kbd->modifiers.mods_depressed,
435 kbd->modifiers.mods_latched,
436 kbd->modifiers.mods_locked,
437 kbd->modifiers.group);
438 }
439 }
440 wl_pointer_send_enter(resource, serial, &surface->resource,
441 sx, sy);
442 wl_signal_add(&resource->destroy_signal,
443 &pointer->focus_listener);
444 pointer->focus_serial = serial;
445 }
446
447 pointer->focus_resource = resource;
448 pointer->focus = surface;
449 pointer->default_grab.focus = surface;
450 wl_signal_emit(&pointer->focus_signal, pointer);
451}
452
453WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400454weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400455 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456{
457 struct wl_resource *resource;
458 struct wl_display *display;
459 uint32_t serial;
460
461 if (keyboard->focus_resource && keyboard->focus != surface) {
462 resource = keyboard->focus_resource;
463 display = wl_client_get_display(resource->client);
464 serial = wl_display_next_serial(display);
465 wl_keyboard_send_leave(resource, serial,
466 &keyboard->focus->resource);
467 wl_list_remove(&keyboard->focus_listener.link);
468 }
469
470 resource = find_resource_for_surface(&keyboard->resource_list,
471 surface);
472 if (resource &&
473 (keyboard->focus != surface ||
474 keyboard->focus_resource != resource)) {
475 display = wl_client_get_display(resource->client);
476 serial = wl_display_next_serial(display);
477 wl_keyboard_send_modifiers(resource, serial,
478 keyboard->modifiers.mods_depressed,
479 keyboard->modifiers.mods_latched,
480 keyboard->modifiers.mods_locked,
481 keyboard->modifiers.group);
482 wl_keyboard_send_enter(resource, serial, &surface->resource,
483 &keyboard->keys);
484 wl_signal_add(&resource->destroy_signal,
485 &keyboard->focus_listener);
486 keyboard->focus_serial = serial;
487 }
488
489 keyboard->focus_resource = resource;
490 keyboard->focus = surface;
491 wl_signal_emit(&keyboard->focus_signal, keyboard);
492}
493
494WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400495weston_keyboard_start_grab(struct weston_keyboard *keyboard,
496 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497{
498 keyboard->grab = grab;
499 grab->keyboard = keyboard;
500
501 /* XXX focus? */
502}
503
504WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400505weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506{
507 keyboard->grab = &keyboard->default_grab;
508}
509
510WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400511weston_pointer_start_grab(struct weston_pointer *pointer,
512 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400513{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400514 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515
516 pointer->grab = grab;
517 interface = pointer->grab->interface;
518 grab->pointer = pointer;
519
520 if (pointer->current)
521 interface->focus(pointer->grab, pointer->current,
522 pointer->current_x, pointer->current_y);
523}
524
525WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400526weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400527{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400528 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400529
530 pointer->grab = &pointer->default_grab;
531 interface = pointer->grab->interface;
532 interface->focus(pointer->grab, pointer->current,
533 pointer->current_x, pointer->current_y);
534}
535
536static void
537current_surface_destroy(struct wl_listener *listener, void *data)
538{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400539 struct weston_pointer *pointer =
540 container_of(listener, struct weston_pointer, current_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541
542 pointer->current = NULL;
543}
544
545WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400546weston_pointer_set_current(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400547 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400548{
549 if (pointer->current)
550 wl_list_remove(&pointer->current_listener.link);
551
552 pointer->current = surface;
553
554 if (!surface)
555 return;
556
557 wl_signal_add(&surface->resource.destroy_signal,
558 &pointer->current_listener);
559 pointer->current_listener.notify = current_surface_destroy;
560}
561
562WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400563weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400564{
565 touch->grab = grab;
566 grab->touch = touch;
567}
568
569WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400570weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400571{
572 touch->grab = &touch->default_grab;
573}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400574
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400575static void
576clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
577{
578 struct weston_compositor *ec = seat->compositor;
579 struct weston_output *output, *prev = NULL;
580 int x, y, old_x, old_y, valid = 0;
581
582 x = wl_fixed_to_int(*fx);
583 y = wl_fixed_to_int(*fy);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400584 old_x = wl_fixed_to_int(seat->pointer->x);
585 old_y = wl_fixed_to_int(seat->pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400586
587 wl_list_for_each(output, &ec->output_list, link) {
588 if (pixman_region32_contains_point(&output->region,
589 x, y, NULL))
590 valid = 1;
591 if (pixman_region32_contains_point(&output->region,
592 old_x, old_y, NULL))
593 prev = output;
594 }
595
596 if (!valid) {
597 if (x < prev->x)
598 *fx = wl_fixed_from_int(prev->x);
599 else if (x >= prev->x + prev->width)
600 *fx = wl_fixed_from_int(prev->x +
601 prev->width - 1);
602 if (y < prev->y)
603 *fy = wl_fixed_from_int(prev->y);
604 else if (y >= prev->y + prev->current->height)
605 *fy = wl_fixed_from_int(prev->y +
606 prev->height - 1);
607 }
608}
609
610/* Takes absolute values */
611static void
612move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
613{
614 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400615 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400616 struct weston_output *output;
617 int32_t ix, iy;
618
619 clip_pointer_motion(seat, &x, &y);
620
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400621 pointer->x = x;
622 pointer->y = y;
623
624 ix = wl_fixed_to_int(x);
625 iy = wl_fixed_to_int(y);
626
627 wl_list_for_each(output, &ec->output_list, link)
628 if (output->zoom.active &&
629 pixman_region32_contains_point(&output->region,
630 ix, iy, NULL))
631 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
632
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400633 weston_seat_repick(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400634
635 if (seat->sprite) {
636 weston_surface_set_position(seat->sprite,
637 ix - seat->hotspot_x,
638 iy - seat->hotspot_y);
639 weston_surface_schedule_repaint(seat->sprite);
640 }
641}
642
643WL_EXPORT void
644notify_motion(struct weston_seat *seat,
645 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
646{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400647 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400649 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400650
651 weston_compositor_wake(ec);
652
653 move_pointer(seat, pointer->x + dx, pointer->y + dy);
654
655 interface = pointer->grab->interface;
656 interface->motion(pointer->grab, time,
657 pointer->grab->x, pointer->grab->y);
658}
659
660WL_EXPORT void
661notify_motion_absolute(struct weston_seat *seat,
662 uint32_t time, wl_fixed_t x, wl_fixed_t y)
663{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400664 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400665 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400666 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400667
668 weston_compositor_wake(ec);
669
670 move_pointer(seat, x, y);
671
672 interface = pointer->grab->interface;
673 interface->motion(pointer->grab, time,
674 pointer->grab->x, pointer->grab->y);
675}
676
677WL_EXPORT void
678weston_surface_activate(struct weston_surface *surface,
679 struct weston_seat *seat)
680{
681 struct weston_compositor *compositor = seat->compositor;
682
Kristian Høgsberge3148752013-05-06 23:19:49 -0400683 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400684 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400685 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400686 }
687
688 wl_signal_emit(&compositor->activate_signal, surface);
689}
690
691WL_EXPORT void
692notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
693 enum wl_pointer_button_state state)
694{
695 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400696 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400697 struct weston_surface *focus =
698 (struct weston_surface *) pointer->focus;
699 uint32_t serial = wl_display_next_serial(compositor->wl_display);
700
701 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
702 if (compositor->ping_handler && focus)
703 compositor->ping_handler(focus, serial);
704 weston_compositor_idle_inhibit(compositor);
705 if (pointer->button_count == 0) {
706 pointer->grab_button = button;
707 pointer->grab_time = time;
708 pointer->grab_x = pointer->x;
709 pointer->grab_y = pointer->y;
710 }
711 pointer->button_count++;
712 } else {
713 weston_compositor_idle_release(compositor);
714 pointer->button_count--;
715 }
716
717 weston_compositor_run_button_binding(compositor, seat, time, button,
718 state);
719
720 pointer->grab->interface->button(pointer->grab, time, button, state);
721
722 if (pointer->button_count == 1)
723 pointer->grab_serial =
724 wl_display_get_serial(compositor->wl_display);
725}
726
727WL_EXPORT void
728notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
729 wl_fixed_t value)
730{
731 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400732 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400733 struct weston_surface *focus =
734 (struct weston_surface *) pointer->focus;
735 uint32_t serial = wl_display_next_serial(compositor->wl_display);
736
737 if (compositor->ping_handler && focus)
738 compositor->ping_handler(focus, serial);
739
740 weston_compositor_wake(compositor);
741
742 if (!value)
743 return;
744
745 if (weston_compositor_run_axis_binding(compositor, seat,
746 time, axis, value))
747 return;
748
749 if (pointer->focus_resource)
750 wl_pointer_send_axis(pointer->focus_resource, time, axis,
751 value);
752}
753
754WL_EXPORT void
755notify_modifiers(struct weston_seat *seat, uint32_t serial)
756{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400757 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400758 struct weston_keyboard_grab *grab = keyboard->grab;
759 uint32_t mods_depressed, mods_latched, mods_locked, group;
760 uint32_t mods_lookup;
761 enum weston_led leds = 0;
762 int changed = 0;
763
764 /* Serialize and update our internal state, checking to see if it's
765 * different to the previous state. */
766 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
767 XKB_STATE_DEPRESSED);
768 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
769 XKB_STATE_LATCHED);
770 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
771 XKB_STATE_LOCKED);
772 group = xkb_state_serialize_group(seat->xkb_state.state,
773 XKB_STATE_EFFECTIVE);
774
Kristian Høgsberge3148752013-05-06 23:19:49 -0400775 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
776 mods_latched != seat->keyboard->modifiers.mods_latched ||
777 mods_locked != seat->keyboard->modifiers.mods_locked ||
778 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400779 changed = 1;
780
Kristian Høgsberge3148752013-05-06 23:19:49 -0400781 seat->keyboard->modifiers.mods_depressed = mods_depressed;
782 seat->keyboard->modifiers.mods_latched = mods_latched;
783 seat->keyboard->modifiers.mods_locked = mods_locked;
784 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400785
786 /* And update the modifier_state for bindings. */
787 mods_lookup = mods_depressed | mods_latched;
788 seat->modifier_state = 0;
789 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
790 seat->modifier_state |= MODIFIER_CTRL;
791 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
792 seat->modifier_state |= MODIFIER_ALT;
793 if (mods_lookup & (1 << seat->xkb_info.super_mod))
794 seat->modifier_state |= MODIFIER_SUPER;
795 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
796 seat->modifier_state |= MODIFIER_SHIFT;
797
798 /* Finally, notify the compositor that LEDs have changed. */
799 if (xkb_state_led_index_is_active(seat->xkb_state.state,
800 seat->xkb_info.num_led))
801 leds |= LED_NUM_LOCK;
802 if (xkb_state_led_index_is_active(seat->xkb_state.state,
803 seat->xkb_info.caps_led))
804 leds |= LED_CAPS_LOCK;
805 if (xkb_state_led_index_is_active(seat->xkb_state.state,
806 seat->xkb_info.scroll_led))
807 leds |= LED_SCROLL_LOCK;
808 if (leds != seat->xkb_state.leds && seat->led_update)
809 seat->led_update(seat, leds);
810 seat->xkb_state.leds = leds;
811
812 if (changed) {
813 grab->interface->modifiers(grab,
814 serial,
815 keyboard->modifiers.mods_depressed,
816 keyboard->modifiers.mods_latched,
817 keyboard->modifiers.mods_locked,
818 keyboard->modifiers.group);
819 }
820}
821
822static void
823update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
824 enum wl_keyboard_key_state state)
825{
826 enum xkb_key_direction direction;
827
828 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
829 direction = XKB_KEY_DOWN;
830 else
831 direction = XKB_KEY_UP;
832
833 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
834 * broken keycode system, which starts at 8. */
835 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
836
837 notify_modifiers(seat, serial);
838}
839
840WL_EXPORT void
841notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
842 enum wl_keyboard_key_state state,
843 enum weston_key_state_update update_state)
844{
845 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400846 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400847 struct weston_surface *focus =
848 (struct weston_surface *) keyboard->focus;
849 struct weston_keyboard_grab *grab = keyboard->grab;
850 uint32_t serial = wl_display_next_serial(compositor->wl_display);
851 uint32_t *k, *end;
852
853 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
854 if (compositor->ping_handler && focus)
855 compositor->ping_handler(focus, serial);
856
857 weston_compositor_idle_inhibit(compositor);
858 keyboard->grab_key = key;
859 keyboard->grab_time = time;
860 } else {
861 weston_compositor_idle_release(compositor);
862 }
863
864 end = keyboard->keys.data + keyboard->keys.size;
865 for (k = keyboard->keys.data; k < end; k++) {
866 if (*k == key) {
867 /* Ignore server-generated repeats. */
868 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
869 return;
870 *k = *--end;
871 }
872 }
873 keyboard->keys.size = (void *) end - keyboard->keys.data;
874 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
875 k = wl_array_add(&keyboard->keys, sizeof *k);
876 *k = key;
877 }
878
879 if (grab == &keyboard->default_grab ||
880 grab == &keyboard->input_method_grab) {
881 weston_compositor_run_key_binding(compositor, seat, time, key,
882 state);
883 grab = keyboard->grab;
884 }
885
886 grab->interface->key(grab, time, key, state);
887
888 if (update_state == STATE_UPDATE_AUTOMATIC) {
889 update_modifier_state(seat,
890 wl_display_get_serial(compositor->wl_display),
891 key,
892 state);
893 }
894}
895
896WL_EXPORT void
897notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
898 wl_fixed_t x, wl_fixed_t y)
899{
900 struct weston_compositor *compositor = seat->compositor;
901
902 if (output) {
903 move_pointer(seat, x, y);
904 compositor->focus = 1;
905 } else {
906 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400907 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400908 * NULL) here, but somehow that breaks re-entry... */
909 }
910}
911
912static void
913destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
914{
915 struct weston_seat *ws;
916
917 ws = container_of(listener, struct weston_seat,
918 saved_kbd_focus_listener);
919
920 ws->saved_kbd_focus = NULL;
921}
922
923WL_EXPORT void
924notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
925 enum weston_key_state_update update_state)
926{
927 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400928 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400929 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400930 uint32_t *k, serial;
931
932 serial = wl_display_next_serial(compositor->wl_display);
933 wl_array_copy(&keyboard->keys, keys);
934 wl_array_for_each(k, &keyboard->keys) {
935 weston_compositor_idle_inhibit(compositor);
936 if (update_state == STATE_UPDATE_AUTOMATIC)
937 update_modifier_state(seat, serial, *k,
938 WL_KEYBOARD_KEY_STATE_PRESSED);
939 }
940
941 /* Run key bindings after we've updated the state. */
942 wl_array_for_each(k, &keyboard->keys) {
943 weston_compositor_run_key_binding(compositor, seat, 0, *k,
944 WL_KEYBOARD_KEY_STATE_PRESSED);
945 }
946
947 surface = seat->saved_kbd_focus;
948
949 if (surface) {
950 wl_list_remove(&seat->saved_kbd_focus_listener.link);
951 weston_keyboard_set_focus(keyboard, surface);
952 seat->saved_kbd_focus = NULL;
953 }
954}
955
956WL_EXPORT void
957notify_keyboard_focus_out(struct weston_seat *seat)
958{
959 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400960 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400961 uint32_t *k, serial;
962
963 serial = wl_display_next_serial(compositor->wl_display);
964 wl_array_for_each(k, &keyboard->keys) {
965 weston_compositor_idle_release(compositor);
966 update_modifier_state(seat, serial, *k,
967 WL_KEYBOARD_KEY_STATE_RELEASED);
968 }
969
970 seat->modifier_state = 0;
971
972 if (keyboard->focus) {
973 seat->saved_kbd_focus = keyboard->focus;
974 seat->saved_kbd_focus_listener.notify =
975 destroy_device_saved_kbd_focus;
976 wl_signal_add(&keyboard->focus->resource.destroy_signal,
977 &seat->saved_kbd_focus_listener);
978 }
979
980 weston_keyboard_set_focus(keyboard, NULL);
981 /* FIXME: We really need keyboard grab cancel here to
982 * let the grab shut down properly. As it is we leak
983 * the grab data. */
984 weston_keyboard_end_grab(keyboard);
985}
986
987static void
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400988touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400989{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400990 struct wl_resource *resource;
991
992 if (seat->touch->focus == surface)
993 return;
994
995 if (seat->touch->focus_resource)
996 wl_list_remove(&seat->touch->focus_listener.link);
997 seat->touch->focus = NULL;
998 seat->touch->focus_resource = NULL;
999
1000 if (surface) {
1001 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -04001002 find_resource_for_surface(&seat->touch->resource_list,
1003 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001004 if (!resource) {
1005 weston_log("couldn't find resource\n");
1006 return;
1007 }
1008
1009 seat->touch->focus = surface;
1010 seat->touch->focus_resource = resource;
1011 wl_signal_add(&resource->destroy_signal,
1012 &seat->touch->focus_listener);
1013 }
1014}
1015
1016/**
1017 * notify_touch - emulates button touches and notifies surfaces accordingly.
1018 *
1019 * It assumes always the correct cycle sequence until it gets here: touch_down
1020 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1021 * for sending along such order.
1022 *
1023 */
1024WL_EXPORT void
1025notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1026 wl_fixed_t x, wl_fixed_t y, int touch_type)
1027{
1028 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001029 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001030 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001031 struct weston_surface *es;
1032 wl_fixed_t sx, sy;
1033
1034 /* Update grab's global coordinates. */
1035 touch->grab_x = x;
1036 touch->grab_y = y;
1037
1038 switch (touch_type) {
1039 case WL_TOUCH_DOWN:
1040 weston_compositor_idle_inhibit(ec);
1041
1042 seat->num_tp++;
1043
1044 /* the first finger down picks the surface, and all further go
1045 * to that surface for the remainder of the touch session i.e.
1046 * until all touch points are up again. */
1047 if (seat->num_tp == 1) {
1048 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001049 touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001050 } else if (touch->focus) {
1051 es = (struct weston_surface *) touch->focus;
1052 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1053 } else {
1054 /* Unexpected condition: We have non-initial touch but
1055 * there is no focused surface.
1056 */
1057 weston_log("touch event received with %d points down"
1058 "but no surface focused\n", seat->num_tp);
1059 return;
1060 }
1061
1062 grab->interface->down(grab, time, touch_id, sx, sy);
1063 break;
1064 case WL_TOUCH_MOTION:
1065 es = (struct weston_surface *) touch->focus;
1066 if (!es)
1067 break;
1068
1069 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1070 grab->interface->motion(grab, time, touch_id, sx, sy);
1071 break;
1072 case WL_TOUCH_UP:
1073 weston_compositor_idle_release(ec);
1074 seat->num_tp--;
1075
1076 grab->interface->up(grab, time, touch_id);
1077 if (seat->num_tp == 0)
1078 touch_set_focus(seat, NULL);
1079 break;
1080 }
1081}
1082
1083static void
1084pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1085{
1086 struct weston_seat *seat = container_of(listener, struct weston_seat,
1087 sprite_destroy_listener);
1088
1089 seat->sprite = NULL;
1090}
1091
1092static void
1093pointer_cursor_surface_configure(struct weston_surface *es,
1094 int32_t dx, int32_t dy, int32_t width, int32_t height)
1095{
1096 struct weston_seat *seat = es->configure_private;
1097 int x, y;
1098
1099 if (width == 0)
1100 return;
1101
1102 assert(es == seat->sprite);
1103
1104 seat->hotspot_x -= dx;
1105 seat->hotspot_y -= dy;
1106
Kristian Høgsberge3148752013-05-06 23:19:49 -04001107 x = wl_fixed_to_int(seat->pointer->x) - seat->hotspot_x;
1108 y = wl_fixed_to_int(seat->pointer->y) - seat->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001109
1110 weston_surface_configure(seat->sprite, x, y,
1111 width, height);
1112
1113 empty_region(&es->pending.input);
1114
1115 if (!weston_surface_is_mapped(es)) {
1116 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1117 &es->layer_link);
1118 weston_surface_update_transform(es);
1119 }
1120}
1121
1122static void
1123pointer_unmap_sprite(struct weston_seat *seat)
1124{
1125 if (weston_surface_is_mapped(seat->sprite))
1126 weston_surface_unmap(seat->sprite);
1127
1128 wl_list_remove(&seat->sprite_destroy_listener.link);
1129 seat->sprite->configure = NULL;
1130 seat->sprite->configure_private = NULL;
1131 seat->sprite = NULL;
1132}
1133
1134static void
1135pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1136 uint32_t serial, struct wl_resource *surface_resource,
1137 int32_t x, int32_t y)
1138{
1139 struct weston_seat *seat = resource->data;
1140 struct weston_surface *surface = NULL;
1141
1142 if (surface_resource)
1143 surface = surface_resource->data;
1144
Kristian Høgsberge3148752013-05-06 23:19:49 -04001145 if (seat->pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001146 return;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001147 if (seat->pointer->focus->resource.client != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001148 return;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001149 if (seat->pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001150 return;
1151
1152 if (surface && surface != seat->sprite) {
1153 if (surface->configure) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001154 wl_resource_post_error(&surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001155 WL_DISPLAY_ERROR_INVALID_OBJECT,
1156 "surface->configure already "
1157 "set");
1158 return;
1159 }
1160 }
1161
1162 if (seat->sprite)
1163 pointer_unmap_sprite(seat);
1164
1165 if (!surface)
1166 return;
1167
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001168 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169 &seat->sprite_destroy_listener);
1170
1171 surface->configure = pointer_cursor_surface_configure;
1172 surface->configure_private = seat;
1173 seat->sprite = surface;
1174 seat->hotspot_x = x;
1175 seat->hotspot_y = y;
1176
1177 if (surface->buffer_ref.buffer)
1178 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1179 weston_surface_buffer_height(surface));
1180}
1181
1182static const struct wl_pointer_interface pointer_interface = {
1183 pointer_set_cursor
1184};
1185
1186static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1188 uint32_t id)
1189{
1190 struct weston_seat *seat = resource->data;
1191 struct wl_resource *cr;
1192
Kristian Høgsberge3148752013-05-06 23:19:49 -04001193 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194 return;
1195
1196 cr = wl_client_add_object(client, &wl_pointer_interface,
1197 &pointer_interface, id, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001198 wl_list_insert(&seat->pointer->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 cr->destroy = unbind_resource;
1200
Kristian Høgsberge3148752013-05-06 23:19:49 -04001201 if (seat->pointer->focus &&
1202 seat->pointer->focus->resource.client == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001203 struct weston_surface *surface;
1204 wl_fixed_t sx, sy;
1205
Kristian Høgsberge3148752013-05-06 23:19:49 -04001206 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001208 seat->pointer->x,
1209 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001210 &sx,
1211 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001212 weston_pointer_set_focus(seat->pointer,
1213 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001214 sx,
1215 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216 }
1217}
1218
1219static void
1220seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1221 uint32_t id)
1222{
1223 struct weston_seat *seat = resource->data;
1224 struct wl_resource *cr;
1225
Kristian Høgsberge3148752013-05-06 23:19:49 -04001226 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001227 return;
1228
1229 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
1230 seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001231 wl_list_insert(&seat->keyboard->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232 cr->destroy = unbind_resource;
1233
1234 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1235 seat->xkb_info.keymap_fd,
1236 seat->xkb_info.keymap_size);
1237
Kristian Høgsberge3148752013-05-06 23:19:49 -04001238 if (seat->keyboard->focus &&
1239 seat->keyboard->focus->resource.client == client) {
1240 weston_keyboard_set_focus(seat->keyboard,
1241 seat->keyboard->focus);
1242 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001243 }
1244}
1245
1246static void
1247seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1248 uint32_t id)
1249{
1250 struct weston_seat *seat = resource->data;
1251 struct wl_resource *cr;
1252
Kristian Høgsberge3148752013-05-06 23:19:49 -04001253 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001254 return;
1255
1256 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001257 wl_list_insert(&seat->touch->resource_list, &cr->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001258 cr->destroy = unbind_resource;
1259}
1260
1261static const struct wl_seat_interface seat_interface = {
1262 seat_get_pointer,
1263 seat_get_keyboard,
1264 seat_get_touch,
1265};
1266
1267static void
1268bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1269{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001270 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271 struct wl_resource *resource;
1272 enum wl_seat_capability caps = 0;
1273
1274 resource = wl_client_add_object(client, &wl_seat_interface,
1275 &seat_interface, id, data);
1276 wl_list_insert(&seat->base_resource_list, &resource->link);
1277 resource->destroy = unbind_resource;
1278
1279 if (seat->pointer)
1280 caps |= WL_SEAT_CAPABILITY_POINTER;
1281 if (seat->keyboard)
1282 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1283 if (seat->touch)
1284 caps |= WL_SEAT_CAPABILITY_TOUCH;
1285
1286 wl_seat_send_capabilities(resource, caps);
1287}
1288
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001289int
1290weston_compositor_xkb_init(struct weston_compositor *ec,
1291 struct xkb_rule_names *names)
1292{
1293 if (ec->xkb_context == NULL) {
1294 ec->xkb_context = xkb_context_new(0);
1295 if (ec->xkb_context == NULL) {
1296 weston_log("failed to create XKB context\n");
1297 return -1;
1298 }
1299 }
1300
1301 if (names)
1302 ec->xkb_names = *names;
1303 if (!ec->xkb_names.rules)
1304 ec->xkb_names.rules = strdup("evdev");
1305 if (!ec->xkb_names.model)
1306 ec->xkb_names.model = strdup("pc105");
1307 if (!ec->xkb_names.layout)
1308 ec->xkb_names.layout = strdup("us");
1309
1310 return 0;
1311}
1312
1313static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1314{
1315 if (xkb_info->keymap)
1316 xkb_map_unref(xkb_info->keymap);
1317
1318 if (xkb_info->keymap_area)
1319 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1320 if (xkb_info->keymap_fd >= 0)
1321 close(xkb_info->keymap_fd);
1322}
1323
1324void
1325weston_compositor_xkb_destroy(struct weston_compositor *ec)
1326{
1327 free((char *) ec->xkb_names.rules);
1328 free((char *) ec->xkb_names.model);
1329 free((char *) ec->xkb_names.layout);
1330 free((char *) ec->xkb_names.variant);
1331 free((char *) ec->xkb_names.options);
1332
1333 xkb_info_destroy(&ec->xkb_info);
1334 xkb_context_unref(ec->xkb_context);
1335}
1336
1337static int
1338weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1339{
1340 char *keymap_str;
1341
1342 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1343 XKB_MOD_NAME_SHIFT);
1344 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1345 XKB_MOD_NAME_CAPS);
1346 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1347 XKB_MOD_NAME_CTRL);
1348 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1349 XKB_MOD_NAME_ALT);
1350 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1351 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1352 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1353 XKB_MOD_NAME_LOGO);
1354 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1355
1356 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1357 XKB_LED_NAME_NUM);
1358 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1359 XKB_LED_NAME_CAPS);
1360 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1361 XKB_LED_NAME_SCROLL);
1362
1363 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1364 if (keymap_str == NULL) {
1365 weston_log("failed to get string version of keymap\n");
1366 return -1;
1367 }
1368 xkb_info->keymap_size = strlen(keymap_str) + 1;
1369
1370 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1371 if (xkb_info->keymap_fd < 0) {
1372 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1373 (unsigned long) xkb_info->keymap_size);
1374 goto err_keymap_str;
1375 }
1376
1377 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1378 PROT_READ | PROT_WRITE,
1379 MAP_SHARED, xkb_info->keymap_fd, 0);
1380 if (xkb_info->keymap_area == MAP_FAILED) {
1381 weston_log("failed to mmap() %lu bytes\n",
1382 (unsigned long) xkb_info->keymap_size);
1383 goto err_dev_zero;
1384 }
1385 strcpy(xkb_info->keymap_area, keymap_str);
1386 free(keymap_str);
1387
1388 return 0;
1389
1390err_dev_zero:
1391 close(xkb_info->keymap_fd);
1392 xkb_info->keymap_fd = -1;
1393err_keymap_str:
1394 free(keymap_str);
1395 return -1;
1396}
1397
1398static int
1399weston_compositor_build_global_keymap(struct weston_compositor *ec)
1400{
1401 if (ec->xkb_info.keymap != NULL)
1402 return 0;
1403
1404 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1405 &ec->xkb_names,
1406 0);
1407 if (ec->xkb_info.keymap == NULL) {
1408 weston_log("failed to compile global XKB keymap\n");
1409 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1410 "options %s\n",
1411 ec->xkb_names.rules, ec->xkb_names.model,
1412 ec->xkb_names.layout, ec->xkb_names.variant,
1413 ec->xkb_names.options);
1414 return -1;
1415 }
1416
1417 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1418 return -1;
1419
1420 return 0;
1421}
1422
1423WL_EXPORT int
1424weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1425{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001426 struct weston_keyboard *keyboard;
1427
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001428 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001429 return 0;
1430
1431 if (keymap != NULL) {
1432 seat->xkb_info.keymap = xkb_map_ref(keymap);
1433 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1434 return -1;
1435 } else {
1436 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1437 return -1;
1438 seat->xkb_info = seat->compositor->xkb_info;
1439 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1440 }
1441
1442 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1443 if (seat->xkb_state.state == NULL) {
1444 weston_log("failed to initialise XKB state\n");
1445 return -1;
1446 }
1447
1448 seat->xkb_state.leds = 0;
1449
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001450 keyboard = weston_keyboard_create();
1451 if (keyboard == NULL) {
1452 weston_log("failed to allocate weston keyboard struct\n");
1453 return -1;
1454 }
1455
1456 seat->keyboard = keyboard;
1457 keyboard->seat = seat;
1458
1459 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001460
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001461 return 0;
1462}
1463
1464WL_EXPORT void
1465weston_seat_init_pointer(struct weston_seat *seat)
1466{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001467 struct weston_pointer *pointer;
1468
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001469 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001470 return;
1471
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001472 pointer = weston_pointer_create();
1473 if (pointer == NULL)
1474 return;
1475
1476 seat->pointer = pointer;
1477 pointer->seat = seat;
1478
1479 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001480}
1481
1482WL_EXPORT void
1483weston_seat_init_touch(struct weston_seat *seat)
1484{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001485 struct weston_touch *touch;
1486
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001487 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001488 return;
1489
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001490 touch = weston_touch_create();
1491 if (touch == NULL)
1492 return;
1493
1494 seat->touch = touch;
1495 touch->seat = seat;
1496
1497 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001498}
1499
1500WL_EXPORT void
1501weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
1502{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001503 memset(seat, 0, sizeof *seat);
1504
Kristian Høgsberge3148752013-05-06 23:19:49 -04001505 seat->selection_data_source = NULL;
1506 wl_list_init(&seat->base_resource_list);
1507 wl_signal_init(&seat->selection_signal);
1508 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001509 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001510
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001511 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
1512 bind_seat);
1513
1514 seat->sprite = NULL;
1515 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1516
1517 seat->compositor = ec;
1518 seat->hotspot_x = 16;
1519 seat->hotspot_y = 16;
1520 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
1536 if (seat->sprite)
1537 pointer_unmap_sprite(seat);
1538
1539 if (seat->xkb_state.state != NULL)
1540 xkb_state_unref(seat->xkb_state.state);
1541 xkb_info_destroy(&seat->xkb_info);
1542
Kristian Høgsberge3148752013-05-06 23:19:49 -04001543 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001544 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001545 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001546 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001547 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001548 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001549
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001550 wl_signal_emit(&seat->destroy_signal, seat);
1551}