blob: 782572bb6225f34de5876173db2f1d4c3dc9db9e [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øgsberg02bbabb2013-05-06 22:15:05 -040051 struct weston_pointer *pointer = seat->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
62 if (&surface->surface != pointer->current) {
63 interface = pointer->grab->interface;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040064 weston_pointer_set_current(pointer, &surface->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040065 interface->focus(pointer->grab, &surface->surface,
66 pointer->current_x,
67 pointer->current_y);
68 }
69
70 focus = (struct weston_surface *) pointer->grab->focus;
71 if (focus)
72 weston_surface_from_global_fixed(focus,
73 pointer->x,
74 pointer->y,
75 &pointer->grab->x,
76 &pointer->grab->y);
77}
78
79static void
80weston_compositor_idle_inhibit(struct weston_compositor *compositor)
81{
82 weston_compositor_wake(compositor);
83 compositor->idle_inhibit++;
84}
85
86static void
87weston_compositor_idle_release(struct weston_compositor *compositor)
88{
89 compositor->idle_inhibit--;
90 weston_compositor_wake(compositor);
91}
92
Kristian Høgsberg2158a882013-04-18 15:07:39 -040093static void
94lose_pointer_focus(struct wl_listener *listener, void *data)
95{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040096 struct weston_pointer *pointer =
97 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040098
99 pointer->focus_resource = NULL;
100}
101
102static void
103lose_keyboard_focus(struct wl_listener *listener, void *data)
104{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400105 struct weston_keyboard *keyboard =
106 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400107
108 keyboard->focus_resource = NULL;
109}
110
111static void
112lose_touch_focus(struct wl_listener *listener, void *data)
113{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400114 struct weston_touch *touch =
115 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116
117 touch->focus_resource = NULL;
118}
119
120static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400121default_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400122 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
123{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400124 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400125
126 if (pointer->button_count > 0)
127 return;
128
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400129 weston_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400130}
131
132static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400133default_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400134 uint32_t time, wl_fixed_t x, wl_fixed_t y)
135{
136 struct wl_resource *resource;
137
138 resource = grab->pointer->focus_resource;
139 if (resource)
140 wl_pointer_send_motion(resource, time, x, y);
141}
142
143static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400144default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400145 uint32_t time, uint32_t button, uint32_t state_w)
146{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400147 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400148 struct wl_resource *resource;
149 uint32_t serial;
150 enum wl_pointer_button_state state = state_w;
151 struct wl_display *display;
152
153 resource = pointer->focus_resource;
154 if (resource) {
155 display = wl_client_get_display(resource->client);
156 serial = wl_display_next_serial(display);
157 wl_pointer_send_button(resource, serial, time, button, state_w);
158 }
159
160 if (pointer->button_count == 0 &&
161 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400162 weston_pointer_set_focus(pointer, pointer->current,
163 pointer->current_x,
164 pointer->current_y);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400165}
166
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400167static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400168 default_pointer_grab_interface = {
169 default_grab_focus,
170 default_grab_motion,
171 default_grab_button
172};
173
Kristian Høgsberge329f362013-05-06 22:19:57 -0400174static void
175default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
176 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400177{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400178 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179 struct wl_display *display;
180 uint32_t serial;
181
182 if (touch->focus_resource && touch->focus) {
183 display = wl_client_get_display(touch->focus_resource->client);
184 serial = wl_display_next_serial(display);
185 wl_touch_send_down(touch->focus_resource, serial, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400186 &touch->focus->resource,
187 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400188 }
189}
190
Kristian Høgsberge329f362013-05-06 22:19:57 -0400191static void
192default_grab_touch_up(struct weston_touch_grab *grab,
193 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400195 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196 struct wl_display *display;
197 uint32_t serial;
198
199 if (touch->focus_resource) {
200 display = wl_client_get_display(touch->focus_resource->client);
201 serial = wl_display_next_serial(display);
202 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
203 }
204}
205
Kristian Høgsberge329f362013-05-06 22:19:57 -0400206static void
207default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
208 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400209{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400210 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400211
212 if (touch->focus_resource) {
213 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400214 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215 }
216}
217
Kristian Høgsberge329f362013-05-06 22:19:57 -0400218static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400219 default_grab_touch_down,
220 default_grab_touch_up,
221 default_grab_touch_motion
222};
223
224static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400225default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400226 uint32_t time, uint32_t key, uint32_t state)
227{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400228 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400229 struct wl_resource *resource;
230 struct wl_display *display;
231 uint32_t serial;
232
233 resource = keyboard->focus_resource;
234 if (resource) {
235 display = wl_client_get_display(resource->client);
236 serial = wl_display_next_serial(display);
237 wl_keyboard_send_key(resource, serial, time, key, state);
238 }
239}
240
241static struct wl_resource *
242find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
243{
244 struct wl_resource *r;
245
246 if (!surface)
247 return NULL;
248
249 wl_list_for_each(r, list, link) {
250 if (r->client == surface->resource.client)
251 return r;
252 }
253
254 return NULL;
255}
256
257static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400258default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400259 uint32_t mods_depressed, uint32_t mods_latched,
260 uint32_t mods_locked, uint32_t group)
261{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400262 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400263 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400264 struct wl_resource *resource, *pr;
265
266 resource = keyboard->focus_resource;
267 if (!resource)
268 return;
269
270 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
271 mods_latched, mods_locked, group);
272
273 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
274 pr = find_resource_for_surface(&keyboard->resource_list,
275 pointer->focus);
276 if (pr) {
277 wl_keyboard_send_modifiers(pr,
278 serial,
279 keyboard->modifiers.mods_depressed,
280 keyboard->modifiers.mods_latched,
281 keyboard->modifiers.mods_locked,
282 keyboard->modifiers.group);
283 }
284 }
285}
286
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400287static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400288 default_keyboard_grab_interface = {
289 default_grab_key,
290 default_grab_modifiers,
291};
292
293WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400294weston_pointer_init(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400295{
296 memset(pointer, 0, sizeof *pointer);
297 wl_list_init(&pointer->resource_list);
298 pointer->focus_listener.notify = lose_pointer_focus;
299 pointer->default_grab.interface = &default_pointer_grab_interface;
300 pointer->default_grab.pointer = pointer;
301 pointer->grab = &pointer->default_grab;
302 wl_signal_init(&pointer->focus_signal);
303
304 /* FIXME: Pick better co-ords. */
305 pointer->x = wl_fixed_from_int(100);
306 pointer->y = wl_fixed_from_int(100);
307}
308
309WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400310weston_pointer_release(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400311{
312 /* XXX: What about pointer->resource_list? */
313 if (pointer->focus_resource)
314 wl_list_remove(&pointer->focus_listener.link);
315}
316
317WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400318weston_keyboard_init(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400319{
320 memset(keyboard, 0, sizeof *keyboard);
321 wl_list_init(&keyboard->resource_list);
322 wl_array_init(&keyboard->keys);
323 keyboard->focus_listener.notify = lose_keyboard_focus;
324 keyboard->default_grab.interface = &default_keyboard_grab_interface;
325 keyboard->default_grab.keyboard = keyboard;
326 keyboard->grab = &keyboard->default_grab;
327 wl_signal_init(&keyboard->focus_signal);
328}
329
330WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400331weston_keyboard_release(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400332{
333 /* XXX: What about keyboard->resource_list? */
334 if (keyboard->focus_resource)
335 wl_list_remove(&keyboard->focus_listener.link);
336 wl_array_release(&keyboard->keys);
337}
338
339WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400340weston_touch_init(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341{
342 memset(touch, 0, sizeof *touch);
343 wl_list_init(&touch->resource_list);
344 touch->focus_listener.notify = lose_touch_focus;
345 touch->default_grab.interface = &default_touch_grab_interface;
346 touch->default_grab.touch = touch;
347 touch->grab = &touch->default_grab;
348 wl_signal_init(&touch->focus_signal);
349}
350
351WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400352weston_touch_release(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353{
354 /* XXX: What about touch->resource_list? */
355 if (touch->focus_resource)
356 wl_list_remove(&touch->focus_listener.link);
357}
358
359WL_EXPORT void
360wl_seat_init(struct wl_seat *seat)
361{
362 memset(seat, 0, sizeof *seat);
363
364 wl_signal_init(&seat->destroy_signal);
365
366 seat->selection_data_source = NULL;
367 wl_list_init(&seat->base_resource_list);
368 wl_signal_init(&seat->selection_signal);
369 wl_list_init(&seat->drag_resource_list);
370 wl_signal_init(&seat->drag_icon_signal);
371}
372
373WL_EXPORT void
374wl_seat_release(struct wl_seat *seat)
375{
376 wl_signal_emit(&seat->destroy_signal, seat);
377
378 if (seat->pointer)
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400379 weston_pointer_release(seat->pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400380 if (seat->keyboard)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400381 weston_keyboard_release(seat->keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382 if (seat->touch)
Kristian Høgsberge329f362013-05-06 22:19:57 -0400383 weston_touch_release(seat->touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400384}
385
386static void
387seat_send_updated_caps(struct wl_seat *seat)
388{
389 struct wl_resource *r;
390 enum wl_seat_capability caps = 0;
391
392 if (seat->pointer)
393 caps |= WL_SEAT_CAPABILITY_POINTER;
394 if (seat->keyboard)
395 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
396 if (seat->touch)
397 caps |= WL_SEAT_CAPABILITY_TOUCH;
398
399 wl_list_for_each(r, &seat->base_resource_list, link)
400 wl_seat_send_capabilities(r, caps);
401}
402
403WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400404wl_seat_set_pointer(struct wl_seat *seat, struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405{
406 if (pointer && (seat->pointer || pointer->seat))
407 return; /* XXX: error? */
408 if (!pointer && !seat->pointer)
409 return;
410
411 seat->pointer = pointer;
412 if (pointer)
413 pointer->seat = seat;
414
415 seat_send_updated_caps(seat);
416}
417
418WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400419wl_seat_set_keyboard(struct wl_seat *seat, struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400420{
421 if (keyboard && (seat->keyboard || keyboard->seat))
422 return; /* XXX: error? */
423 if (!keyboard && !seat->keyboard)
424 return;
425
426 seat->keyboard = keyboard;
427 if (keyboard)
428 keyboard->seat = seat;
429
430 seat_send_updated_caps(seat);
431}
432
433WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400434wl_seat_set_touch(struct wl_seat *seat, struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435{
436 if (touch && (seat->touch || touch->seat))
437 return; /* XXX: error? */
438 if (!touch && !seat->touch)
439 return;
440
441 seat->touch = touch;
442 if (touch)
443 touch->seat = seat;
444
445 seat_send_updated_caps(seat);
446}
447
448WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400449weston_pointer_set_focus(struct weston_pointer *pointer,
450 struct wl_surface *surface,
451 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400452{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400453 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454 struct wl_resource *resource, *kr;
455 struct wl_display *display;
456 uint32_t serial;
457
458 resource = pointer->focus_resource;
459 if (resource && pointer->focus != surface) {
460 display = wl_client_get_display(resource->client);
461 serial = wl_display_next_serial(display);
462 wl_pointer_send_leave(resource, serial,
463 &pointer->focus->resource);
464 wl_list_remove(&pointer->focus_listener.link);
465 }
466
467 resource = find_resource_for_surface(&pointer->resource_list,
468 surface);
469 if (resource &&
470 (pointer->focus != surface ||
471 pointer->focus_resource != resource)) {
472 display = wl_client_get_display(resource->client);
473 serial = wl_display_next_serial(display);
474 if (kbd) {
475 kr = find_resource_for_surface(&kbd->resource_list,
476 surface);
477 if (kr) {
478 wl_keyboard_send_modifiers(kr,
479 serial,
480 kbd->modifiers.mods_depressed,
481 kbd->modifiers.mods_latched,
482 kbd->modifiers.mods_locked,
483 kbd->modifiers.group);
484 }
485 }
486 wl_pointer_send_enter(resource, serial, &surface->resource,
487 sx, sy);
488 wl_signal_add(&resource->destroy_signal,
489 &pointer->focus_listener);
490 pointer->focus_serial = serial;
491 }
492
493 pointer->focus_resource = resource;
494 pointer->focus = surface;
495 pointer->default_grab.focus = surface;
496 wl_signal_emit(&pointer->focus_signal, pointer);
497}
498
499WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400500weston_keyboard_set_focus(struct weston_keyboard *keyboard,
501 struct wl_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400502{
503 struct wl_resource *resource;
504 struct wl_display *display;
505 uint32_t serial;
506
507 if (keyboard->focus_resource && keyboard->focus != surface) {
508 resource = keyboard->focus_resource;
509 display = wl_client_get_display(resource->client);
510 serial = wl_display_next_serial(display);
511 wl_keyboard_send_leave(resource, serial,
512 &keyboard->focus->resource);
513 wl_list_remove(&keyboard->focus_listener.link);
514 }
515
516 resource = find_resource_for_surface(&keyboard->resource_list,
517 surface);
518 if (resource &&
519 (keyboard->focus != surface ||
520 keyboard->focus_resource != resource)) {
521 display = wl_client_get_display(resource->client);
522 serial = wl_display_next_serial(display);
523 wl_keyboard_send_modifiers(resource, serial,
524 keyboard->modifiers.mods_depressed,
525 keyboard->modifiers.mods_latched,
526 keyboard->modifiers.mods_locked,
527 keyboard->modifiers.group);
528 wl_keyboard_send_enter(resource, serial, &surface->resource,
529 &keyboard->keys);
530 wl_signal_add(&resource->destroy_signal,
531 &keyboard->focus_listener);
532 keyboard->focus_serial = serial;
533 }
534
535 keyboard->focus_resource = resource;
536 keyboard->focus = surface;
537 wl_signal_emit(&keyboard->focus_signal, keyboard);
538}
539
540WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400541weston_keyboard_start_grab(struct weston_keyboard *keyboard,
542 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543{
544 keyboard->grab = grab;
545 grab->keyboard = keyboard;
546
547 /* XXX focus? */
548}
549
550WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400551weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400552{
553 keyboard->grab = &keyboard->default_grab;
554}
555
556WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400557weston_pointer_start_grab(struct weston_pointer *pointer,
558 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400559{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400560 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561
562 pointer->grab = grab;
563 interface = pointer->grab->interface;
564 grab->pointer = pointer;
565
566 if (pointer->current)
567 interface->focus(pointer->grab, pointer->current,
568 pointer->current_x, pointer->current_y);
569}
570
571WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400572weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400574 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400575
576 pointer->grab = &pointer->default_grab;
577 interface = pointer->grab->interface;
578 interface->focus(pointer->grab, pointer->current,
579 pointer->current_x, pointer->current_y);
580}
581
582static void
583current_surface_destroy(struct wl_listener *listener, void *data)
584{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400585 struct weston_pointer *pointer =
586 container_of(listener, struct weston_pointer, current_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400587
588 pointer->current = NULL;
589}
590
591WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400592weston_pointer_set_current(struct weston_pointer *pointer,
593 struct wl_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594{
595 if (pointer->current)
596 wl_list_remove(&pointer->current_listener.link);
597
598 pointer->current = surface;
599
600 if (!surface)
601 return;
602
603 wl_signal_add(&surface->resource.destroy_signal,
604 &pointer->current_listener);
605 pointer->current_listener.notify = current_surface_destroy;
606}
607
608WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400609weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400610{
611 touch->grab = grab;
612 grab->touch = touch;
613}
614
615WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400616weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617{
618 touch->grab = &touch->default_grab;
619}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400620
621static void
622weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
623
624static void
625clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
626{
627 struct weston_compositor *ec = seat->compositor;
628 struct weston_output *output, *prev = NULL;
629 int x, y, old_x, old_y, valid = 0;
630
631 x = wl_fixed_to_int(*fx);
632 y = wl_fixed_to_int(*fy);
633 old_x = wl_fixed_to_int(seat->seat.pointer->x);
634 old_y = wl_fixed_to_int(seat->seat.pointer->y);
635
636 wl_list_for_each(output, &ec->output_list, link) {
637 if (pixman_region32_contains_point(&output->region,
638 x, y, NULL))
639 valid = 1;
640 if (pixman_region32_contains_point(&output->region,
641 old_x, old_y, NULL))
642 prev = output;
643 }
644
645 if (!valid) {
646 if (x < prev->x)
647 *fx = wl_fixed_from_int(prev->x);
648 else if (x >= prev->x + prev->width)
649 *fx = wl_fixed_from_int(prev->x +
650 prev->width - 1);
651 if (y < prev->y)
652 *fy = wl_fixed_from_int(prev->y);
653 else if (y >= prev->y + prev->current->height)
654 *fy = wl_fixed_from_int(prev->y +
655 prev->height - 1);
656 }
657}
658
659/* Takes absolute values */
660static void
661move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
662{
663 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400664 struct weston_pointer *pointer = seat->seat.pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400665 struct weston_output *output;
666 int32_t ix, iy;
667
668 clip_pointer_motion(seat, &x, &y);
669
670 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
671
672 pointer->x = x;
673 pointer->y = y;
674
675 ix = wl_fixed_to_int(x);
676 iy = wl_fixed_to_int(y);
677
678 wl_list_for_each(output, &ec->output_list, link)
679 if (output->zoom.active &&
680 pixman_region32_contains_point(&output->region,
681 ix, iy, NULL))
682 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
683
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400684 weston_seat_repick(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400685
686 if (seat->sprite) {
687 weston_surface_set_position(seat->sprite,
688 ix - seat->hotspot_x,
689 iy - seat->hotspot_y);
690 weston_surface_schedule_repaint(seat->sprite);
691 }
692}
693
694WL_EXPORT void
695notify_motion(struct weston_seat *seat,
696 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
697{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400698 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400699 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400700 struct weston_pointer *pointer = seat->seat.pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400701
702 weston_compositor_wake(ec);
703
704 move_pointer(seat, pointer->x + dx, pointer->y + dy);
705
706 interface = pointer->grab->interface;
707 interface->motion(pointer->grab, time,
708 pointer->grab->x, pointer->grab->y);
709}
710
711WL_EXPORT void
712notify_motion_absolute(struct weston_seat *seat,
713 uint32_t time, wl_fixed_t x, wl_fixed_t y)
714{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400715 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400716 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400717 struct weston_pointer *pointer = seat->seat.pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400718
719 weston_compositor_wake(ec);
720
721 move_pointer(seat, x, y);
722
723 interface = pointer->grab->interface;
724 interface->motion(pointer->grab, time,
725 pointer->grab->x, pointer->grab->y);
726}
727
728WL_EXPORT void
729weston_surface_activate(struct weston_surface *surface,
730 struct weston_seat *seat)
731{
732 struct weston_compositor *compositor = seat->compositor;
733
734 if (seat->seat.keyboard) {
735 weston_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
736 wl_data_device_set_keyboard_focus(&seat->seat);
737 }
738
739 wl_signal_emit(&compositor->activate_signal, surface);
740}
741
742WL_EXPORT void
743notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
744 enum wl_pointer_button_state state)
745{
746 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400747 struct weston_pointer *pointer = seat->seat.pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400748 struct weston_surface *focus =
749 (struct weston_surface *) pointer->focus;
750 uint32_t serial = wl_display_next_serial(compositor->wl_display);
751
752 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
753 if (compositor->ping_handler && focus)
754 compositor->ping_handler(focus, serial);
755 weston_compositor_idle_inhibit(compositor);
756 if (pointer->button_count == 0) {
757 pointer->grab_button = button;
758 pointer->grab_time = time;
759 pointer->grab_x = pointer->x;
760 pointer->grab_y = pointer->y;
761 }
762 pointer->button_count++;
763 } else {
764 weston_compositor_idle_release(compositor);
765 pointer->button_count--;
766 }
767
768 weston_compositor_run_button_binding(compositor, seat, time, button,
769 state);
770
771 pointer->grab->interface->button(pointer->grab, time, button, state);
772
773 if (pointer->button_count == 1)
774 pointer->grab_serial =
775 wl_display_get_serial(compositor->wl_display);
776}
777
778WL_EXPORT void
779notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
780 wl_fixed_t value)
781{
782 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400783 struct weston_pointer *pointer = seat->seat.pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400784 struct weston_surface *focus =
785 (struct weston_surface *) pointer->focus;
786 uint32_t serial = wl_display_next_serial(compositor->wl_display);
787
788 if (compositor->ping_handler && focus)
789 compositor->ping_handler(focus, serial);
790
791 weston_compositor_wake(compositor);
792
793 if (!value)
794 return;
795
796 if (weston_compositor_run_axis_binding(compositor, seat,
797 time, axis, value))
798 return;
799
800 if (pointer->focus_resource)
801 wl_pointer_send_axis(pointer->focus_resource, time, axis,
802 value);
803}
804
805WL_EXPORT void
806notify_modifiers(struct weston_seat *seat, uint32_t serial)
807{
808 struct weston_keyboard *keyboard = &seat->keyboard;
809 struct weston_keyboard_grab *grab = keyboard->grab;
810 uint32_t mods_depressed, mods_latched, mods_locked, group;
811 uint32_t mods_lookup;
812 enum weston_led leds = 0;
813 int changed = 0;
814
815 /* Serialize and update our internal state, checking to see if it's
816 * different to the previous state. */
817 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
818 XKB_STATE_DEPRESSED);
819 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
820 XKB_STATE_LATCHED);
821 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
822 XKB_STATE_LOCKED);
823 group = xkb_state_serialize_group(seat->xkb_state.state,
824 XKB_STATE_EFFECTIVE);
825
826 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
827 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
828 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
829 group != seat->seat.keyboard->modifiers.group)
830 changed = 1;
831
832 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
833 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
834 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
835 seat->seat.keyboard->modifiers.group = group;
836
837 /* And update the modifier_state for bindings. */
838 mods_lookup = mods_depressed | mods_latched;
839 seat->modifier_state = 0;
840 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
841 seat->modifier_state |= MODIFIER_CTRL;
842 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
843 seat->modifier_state |= MODIFIER_ALT;
844 if (mods_lookup & (1 << seat->xkb_info.super_mod))
845 seat->modifier_state |= MODIFIER_SUPER;
846 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
847 seat->modifier_state |= MODIFIER_SHIFT;
848
849 /* Finally, notify the compositor that LEDs have changed. */
850 if (xkb_state_led_index_is_active(seat->xkb_state.state,
851 seat->xkb_info.num_led))
852 leds |= LED_NUM_LOCK;
853 if (xkb_state_led_index_is_active(seat->xkb_state.state,
854 seat->xkb_info.caps_led))
855 leds |= LED_CAPS_LOCK;
856 if (xkb_state_led_index_is_active(seat->xkb_state.state,
857 seat->xkb_info.scroll_led))
858 leds |= LED_SCROLL_LOCK;
859 if (leds != seat->xkb_state.leds && seat->led_update)
860 seat->led_update(seat, leds);
861 seat->xkb_state.leds = leds;
862
863 if (changed) {
864 grab->interface->modifiers(grab,
865 serial,
866 keyboard->modifiers.mods_depressed,
867 keyboard->modifiers.mods_latched,
868 keyboard->modifiers.mods_locked,
869 keyboard->modifiers.group);
870 }
871}
872
873static void
874update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
875 enum wl_keyboard_key_state state)
876{
877 enum xkb_key_direction direction;
878
879 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
880 direction = XKB_KEY_DOWN;
881 else
882 direction = XKB_KEY_UP;
883
884 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
885 * broken keycode system, which starts at 8. */
886 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
887
888 notify_modifiers(seat, serial);
889}
890
891WL_EXPORT void
892notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
893 enum wl_keyboard_key_state state,
894 enum weston_key_state_update update_state)
895{
896 struct weston_compositor *compositor = seat->compositor;
897 struct weston_keyboard *keyboard = &seat->keyboard;
898 struct weston_surface *focus =
899 (struct weston_surface *) keyboard->focus;
900 struct weston_keyboard_grab *grab = keyboard->grab;
901 uint32_t serial = wl_display_next_serial(compositor->wl_display);
902 uint32_t *k, *end;
903
904 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
905 if (compositor->ping_handler && focus)
906 compositor->ping_handler(focus, serial);
907
908 weston_compositor_idle_inhibit(compositor);
909 keyboard->grab_key = key;
910 keyboard->grab_time = time;
911 } else {
912 weston_compositor_idle_release(compositor);
913 }
914
915 end = keyboard->keys.data + keyboard->keys.size;
916 for (k = keyboard->keys.data; k < end; k++) {
917 if (*k == key) {
918 /* Ignore server-generated repeats. */
919 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
920 return;
921 *k = *--end;
922 }
923 }
924 keyboard->keys.size = (void *) end - keyboard->keys.data;
925 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
926 k = wl_array_add(&keyboard->keys, sizeof *k);
927 *k = key;
928 }
929
930 if (grab == &keyboard->default_grab ||
931 grab == &keyboard->input_method_grab) {
932 weston_compositor_run_key_binding(compositor, seat, time, key,
933 state);
934 grab = keyboard->grab;
935 }
936
937 grab->interface->key(grab, time, key, state);
938
939 if (update_state == STATE_UPDATE_AUTOMATIC) {
940 update_modifier_state(seat,
941 wl_display_get_serial(compositor->wl_display),
942 key,
943 state);
944 }
945}
946
947WL_EXPORT void
948notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
949 wl_fixed_t x, wl_fixed_t y)
950{
951 struct weston_compositor *compositor = seat->compositor;
952
953 if (output) {
954 move_pointer(seat, x, y);
955 compositor->focus = 1;
956 } else {
957 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400958 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400959 * NULL) here, but somehow that breaks re-entry... */
960 }
961}
962
963static void
964destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
965{
966 struct weston_seat *ws;
967
968 ws = container_of(listener, struct weston_seat,
969 saved_kbd_focus_listener);
970
971 ws->saved_kbd_focus = NULL;
972}
973
974WL_EXPORT void
975notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
976 enum weston_key_state_update update_state)
977{
978 struct weston_compositor *compositor = seat->compositor;
979 struct weston_keyboard *keyboard = seat->seat.keyboard;
980 struct wl_surface *surface;
981 uint32_t *k, serial;
982
983 serial = wl_display_next_serial(compositor->wl_display);
984 wl_array_copy(&keyboard->keys, keys);
985 wl_array_for_each(k, &keyboard->keys) {
986 weston_compositor_idle_inhibit(compositor);
987 if (update_state == STATE_UPDATE_AUTOMATIC)
988 update_modifier_state(seat, serial, *k,
989 WL_KEYBOARD_KEY_STATE_PRESSED);
990 }
991
992 /* Run key bindings after we've updated the state. */
993 wl_array_for_each(k, &keyboard->keys) {
994 weston_compositor_run_key_binding(compositor, seat, 0, *k,
995 WL_KEYBOARD_KEY_STATE_PRESSED);
996 }
997
998 surface = seat->saved_kbd_focus;
999
1000 if (surface) {
1001 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1002 weston_keyboard_set_focus(keyboard, surface);
1003 seat->saved_kbd_focus = NULL;
1004 }
1005}
1006
1007WL_EXPORT void
1008notify_keyboard_focus_out(struct weston_seat *seat)
1009{
1010 struct weston_compositor *compositor = seat->compositor;
1011 struct weston_keyboard *keyboard = seat->seat.keyboard;
1012 uint32_t *k, serial;
1013
1014 serial = wl_display_next_serial(compositor->wl_display);
1015 wl_array_for_each(k, &keyboard->keys) {
1016 weston_compositor_idle_release(compositor);
1017 update_modifier_state(seat, serial, *k,
1018 WL_KEYBOARD_KEY_STATE_RELEASED);
1019 }
1020
1021 seat->modifier_state = 0;
1022
1023 if (keyboard->focus) {
1024 seat->saved_kbd_focus = keyboard->focus;
1025 seat->saved_kbd_focus_listener.notify =
1026 destroy_device_saved_kbd_focus;
1027 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1028 &seat->saved_kbd_focus_listener);
1029 }
1030
1031 weston_keyboard_set_focus(keyboard, NULL);
1032 /* FIXME: We really need keyboard grab cancel here to
1033 * let the grab shut down properly. As it is we leak
1034 * the grab data. */
1035 weston_keyboard_end_grab(keyboard);
1036}
1037
1038static void
1039touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
1040{
1041 struct wl_seat *seat = &ws->seat;
1042 struct wl_resource *resource;
1043
1044 if (seat->touch->focus == surface)
1045 return;
1046
1047 if (seat->touch->focus_resource)
1048 wl_list_remove(&seat->touch->focus_listener.link);
1049 seat->touch->focus = NULL;
1050 seat->touch->focus_resource = NULL;
1051
1052 if (surface) {
1053 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -04001054 find_resource_for_surface(&seat->touch->resource_list,
1055 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001056 if (!resource) {
1057 weston_log("couldn't find resource\n");
1058 return;
1059 }
1060
1061 seat->touch->focus = surface;
1062 seat->touch->focus_resource = resource;
1063 wl_signal_add(&resource->destroy_signal,
1064 &seat->touch->focus_listener);
1065 }
1066}
1067
1068/**
1069 * notify_touch - emulates button touches and notifies surfaces accordingly.
1070 *
1071 * It assumes always the correct cycle sequence until it gets here: touch_down
1072 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1073 * for sending along such order.
1074 *
1075 */
1076WL_EXPORT void
1077notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1078 wl_fixed_t x, wl_fixed_t y, int touch_type)
1079{
1080 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001081 struct weston_touch *touch = seat->seat.touch;
1082 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001083 struct weston_surface *es;
1084 wl_fixed_t sx, sy;
1085
1086 /* Update grab's global coordinates. */
1087 touch->grab_x = x;
1088 touch->grab_y = y;
1089
1090 switch (touch_type) {
1091 case WL_TOUCH_DOWN:
1092 weston_compositor_idle_inhibit(ec);
1093
1094 seat->num_tp++;
1095
1096 /* the first finger down picks the surface, and all further go
1097 * to that surface for the remainder of the touch session i.e.
1098 * until all touch points are up again. */
1099 if (seat->num_tp == 1) {
1100 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1101 touch_set_focus(seat, &es->surface);
1102 } else if (touch->focus) {
1103 es = (struct weston_surface *) touch->focus;
1104 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1105 } else {
1106 /* Unexpected condition: We have non-initial touch but
1107 * there is no focused surface.
1108 */
1109 weston_log("touch event received with %d points down"
1110 "but no surface focused\n", seat->num_tp);
1111 return;
1112 }
1113
1114 grab->interface->down(grab, time, touch_id, sx, sy);
1115 break;
1116 case WL_TOUCH_MOTION:
1117 es = (struct weston_surface *) touch->focus;
1118 if (!es)
1119 break;
1120
1121 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1122 grab->interface->motion(grab, time, touch_id, sx, sy);
1123 break;
1124 case WL_TOUCH_UP:
1125 weston_compositor_idle_release(ec);
1126 seat->num_tp--;
1127
1128 grab->interface->up(grab, time, touch_id);
1129 if (seat->num_tp == 0)
1130 touch_set_focus(seat, NULL);
1131 break;
1132 }
1133}
1134
1135static void
1136pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1137{
1138 struct weston_seat *seat = container_of(listener, struct weston_seat,
1139 sprite_destroy_listener);
1140
1141 seat->sprite = NULL;
1142}
1143
1144static void
1145pointer_cursor_surface_configure(struct weston_surface *es,
1146 int32_t dx, int32_t dy, int32_t width, int32_t height)
1147{
1148 struct weston_seat *seat = es->configure_private;
1149 int x, y;
1150
1151 if (width == 0)
1152 return;
1153
1154 assert(es == seat->sprite);
1155
1156 seat->hotspot_x -= dx;
1157 seat->hotspot_y -= dy;
1158
1159 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1160 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1161
1162 weston_surface_configure(seat->sprite, x, y,
1163 width, height);
1164
1165 empty_region(&es->pending.input);
1166
1167 if (!weston_surface_is_mapped(es)) {
1168 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1169 &es->layer_link);
1170 weston_surface_update_transform(es);
1171 }
1172}
1173
1174static void
1175pointer_unmap_sprite(struct weston_seat *seat)
1176{
1177 if (weston_surface_is_mapped(seat->sprite))
1178 weston_surface_unmap(seat->sprite);
1179
1180 wl_list_remove(&seat->sprite_destroy_listener.link);
1181 seat->sprite->configure = NULL;
1182 seat->sprite->configure_private = NULL;
1183 seat->sprite = NULL;
1184}
1185
1186static void
1187pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1188 uint32_t serial, struct wl_resource *surface_resource,
1189 int32_t x, int32_t y)
1190{
1191 struct weston_seat *seat = resource->data;
1192 struct weston_surface *surface = NULL;
1193
1194 if (surface_resource)
1195 surface = surface_resource->data;
1196
1197 if (seat->seat.pointer->focus == NULL)
1198 return;
1199 if (seat->seat.pointer->focus->resource.client != client)
1200 return;
1201 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
1202 return;
1203
1204 if (surface && surface != seat->sprite) {
1205 if (surface->configure) {
1206 wl_resource_post_error(&surface->surface.resource,
1207 WL_DISPLAY_ERROR_INVALID_OBJECT,
1208 "surface->configure already "
1209 "set");
1210 return;
1211 }
1212 }
1213
1214 if (seat->sprite)
1215 pointer_unmap_sprite(seat);
1216
1217 if (!surface)
1218 return;
1219
1220 wl_signal_add(&surface->surface.resource.destroy_signal,
1221 &seat->sprite_destroy_listener);
1222
1223 surface->configure = pointer_cursor_surface_configure;
1224 surface->configure_private = seat;
1225 seat->sprite = surface;
1226 seat->hotspot_x = x;
1227 seat->hotspot_y = y;
1228
1229 if (surface->buffer_ref.buffer)
1230 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1231 weston_surface_buffer_height(surface));
1232}
1233
1234static const struct wl_pointer_interface pointer_interface = {
1235 pointer_set_cursor
1236};
1237
1238static void
1239handle_drag_surface_destroy(struct wl_listener *listener, void *data)
1240{
1241 struct weston_seat *seat;
1242
1243 seat = container_of(listener, struct weston_seat,
1244 drag_surface_destroy_listener);
1245
1246 seat->drag_surface = NULL;
1247}
1248
1249static void
1250seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1251 uint32_t id)
1252{
1253 struct weston_seat *seat = resource->data;
1254 struct wl_resource *cr;
1255
1256 if (!seat->seat.pointer)
1257 return;
1258
1259 cr = wl_client_add_object(client, &wl_pointer_interface,
1260 &pointer_interface, id, seat);
1261 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
1262 cr->destroy = unbind_resource;
1263
1264 if (seat->seat.pointer->focus &&
1265 seat->seat.pointer->focus->resource.client == client) {
1266 struct weston_surface *surface;
1267 wl_fixed_t sx, sy;
1268
1269 surface = (struct weston_surface *) seat->seat.pointer->focus;
1270 weston_surface_from_global_fixed(surface,
1271 seat->seat.pointer->x,
1272 seat->seat.pointer->y,
1273 &sx,
1274 &sy);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001275 weston_pointer_set_focus(seat->seat.pointer,
1276 seat->seat.pointer->focus,
1277 sx,
1278 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001279 }
1280}
1281
1282static void
1283seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1284 uint32_t id)
1285{
1286 struct weston_seat *seat = resource->data;
1287 struct wl_resource *cr;
1288
1289 if (!seat->seat.keyboard)
1290 return;
1291
1292 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
1293 seat);
1294 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
1295 cr->destroy = unbind_resource;
1296
1297 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1298 seat->xkb_info.keymap_fd,
1299 seat->xkb_info.keymap_size);
1300
1301 if (seat->seat.keyboard->focus &&
1302 seat->seat.keyboard->focus->resource.client == client) {
1303 weston_keyboard_set_focus(seat->seat.keyboard,
1304 seat->seat.keyboard->focus);
1305 wl_data_device_set_keyboard_focus(&seat->seat);
1306 }
1307}
1308
1309static void
1310seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1311 uint32_t id)
1312{
1313 struct weston_seat *seat = resource->data;
1314 struct wl_resource *cr;
1315
1316 if (!seat->seat.touch)
1317 return;
1318
1319 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
1320 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
1321 cr->destroy = unbind_resource;
1322}
1323
1324static const struct wl_seat_interface seat_interface = {
1325 seat_get_pointer,
1326 seat_get_keyboard,
1327 seat_get_touch,
1328};
1329
1330static void
1331bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1332{
1333 struct wl_seat *seat = data;
1334 struct wl_resource *resource;
1335 enum wl_seat_capability caps = 0;
1336
1337 resource = wl_client_add_object(client, &wl_seat_interface,
1338 &seat_interface, id, data);
1339 wl_list_insert(&seat->base_resource_list, &resource->link);
1340 resource->destroy = unbind_resource;
1341
1342 if (seat->pointer)
1343 caps |= WL_SEAT_CAPABILITY_POINTER;
1344 if (seat->keyboard)
1345 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1346 if (seat->touch)
1347 caps |= WL_SEAT_CAPABILITY_TOUCH;
1348
1349 wl_seat_send_capabilities(resource, caps);
1350}
1351
1352static void
1353device_handle_new_drag_icon(struct wl_listener *listener, void *data)
1354{
1355 struct weston_seat *seat;
1356
1357 seat = container_of(listener, struct weston_seat,
1358 new_drag_icon_listener);
1359
1360 weston_seat_update_drag_surface(seat, 0, 0);
1361}
1362
1363int
1364weston_compositor_xkb_init(struct weston_compositor *ec,
1365 struct xkb_rule_names *names)
1366{
1367 if (ec->xkb_context == NULL) {
1368 ec->xkb_context = xkb_context_new(0);
1369 if (ec->xkb_context == NULL) {
1370 weston_log("failed to create XKB context\n");
1371 return -1;
1372 }
1373 }
1374
1375 if (names)
1376 ec->xkb_names = *names;
1377 if (!ec->xkb_names.rules)
1378 ec->xkb_names.rules = strdup("evdev");
1379 if (!ec->xkb_names.model)
1380 ec->xkb_names.model = strdup("pc105");
1381 if (!ec->xkb_names.layout)
1382 ec->xkb_names.layout = strdup("us");
1383
1384 return 0;
1385}
1386
1387static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1388{
1389 if (xkb_info->keymap)
1390 xkb_map_unref(xkb_info->keymap);
1391
1392 if (xkb_info->keymap_area)
1393 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1394 if (xkb_info->keymap_fd >= 0)
1395 close(xkb_info->keymap_fd);
1396}
1397
1398void
1399weston_compositor_xkb_destroy(struct weston_compositor *ec)
1400{
1401 free((char *) ec->xkb_names.rules);
1402 free((char *) ec->xkb_names.model);
1403 free((char *) ec->xkb_names.layout);
1404 free((char *) ec->xkb_names.variant);
1405 free((char *) ec->xkb_names.options);
1406
1407 xkb_info_destroy(&ec->xkb_info);
1408 xkb_context_unref(ec->xkb_context);
1409}
1410
1411static int
1412weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1413{
1414 char *keymap_str;
1415
1416 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1417 XKB_MOD_NAME_SHIFT);
1418 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1419 XKB_MOD_NAME_CAPS);
1420 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1421 XKB_MOD_NAME_CTRL);
1422 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1423 XKB_MOD_NAME_ALT);
1424 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1425 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1426 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1427 XKB_MOD_NAME_LOGO);
1428 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1429
1430 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1431 XKB_LED_NAME_NUM);
1432 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1433 XKB_LED_NAME_CAPS);
1434 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1435 XKB_LED_NAME_SCROLL);
1436
1437 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1438 if (keymap_str == NULL) {
1439 weston_log("failed to get string version of keymap\n");
1440 return -1;
1441 }
1442 xkb_info->keymap_size = strlen(keymap_str) + 1;
1443
1444 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1445 if (xkb_info->keymap_fd < 0) {
1446 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1447 (unsigned long) xkb_info->keymap_size);
1448 goto err_keymap_str;
1449 }
1450
1451 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1452 PROT_READ | PROT_WRITE,
1453 MAP_SHARED, xkb_info->keymap_fd, 0);
1454 if (xkb_info->keymap_area == MAP_FAILED) {
1455 weston_log("failed to mmap() %lu bytes\n",
1456 (unsigned long) xkb_info->keymap_size);
1457 goto err_dev_zero;
1458 }
1459 strcpy(xkb_info->keymap_area, keymap_str);
1460 free(keymap_str);
1461
1462 return 0;
1463
1464err_dev_zero:
1465 close(xkb_info->keymap_fd);
1466 xkb_info->keymap_fd = -1;
1467err_keymap_str:
1468 free(keymap_str);
1469 return -1;
1470}
1471
1472static int
1473weston_compositor_build_global_keymap(struct weston_compositor *ec)
1474{
1475 if (ec->xkb_info.keymap != NULL)
1476 return 0;
1477
1478 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1479 &ec->xkb_names,
1480 0);
1481 if (ec->xkb_info.keymap == NULL) {
1482 weston_log("failed to compile global XKB keymap\n");
1483 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1484 "options %s\n",
1485 ec->xkb_names.rules, ec->xkb_names.model,
1486 ec->xkb_names.layout, ec->xkb_names.variant,
1487 ec->xkb_names.options);
1488 return -1;
1489 }
1490
1491 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1492 return -1;
1493
1494 return 0;
1495}
1496
1497WL_EXPORT int
1498weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1499{
1500 if (seat->has_keyboard)
1501 return 0;
1502
1503 if (keymap != NULL) {
1504 seat->xkb_info.keymap = xkb_map_ref(keymap);
1505 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1506 return -1;
1507 } else {
1508 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1509 return -1;
1510 seat->xkb_info = seat->compositor->xkb_info;
1511 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1512 }
1513
1514 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1515 if (seat->xkb_state.state == NULL) {
1516 weston_log("failed to initialise XKB state\n");
1517 return -1;
1518 }
1519
1520 seat->xkb_state.leds = 0;
1521
1522 weston_keyboard_init(&seat->keyboard);
1523 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
1524
1525 seat->has_keyboard = 1;
1526
1527 return 0;
1528}
1529
1530WL_EXPORT void
1531weston_seat_init_pointer(struct weston_seat *seat)
1532{
1533 if (seat->has_pointer)
1534 return;
1535
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001536 weston_pointer_init(&seat->pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001537 wl_seat_set_pointer(&seat->seat, &seat->pointer);
1538
1539 seat->has_pointer = 1;
1540}
1541
1542WL_EXPORT void
1543weston_seat_init_touch(struct weston_seat *seat)
1544{
1545 if (seat->has_touch)
1546 return;
1547
Kristian Høgsberge329f362013-05-06 22:19:57 -04001548 weston_touch_init(&seat->touch);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001549 wl_seat_set_touch(&seat->seat, &seat->touch);
1550
1551 seat->has_touch = 1;
1552}
1553
1554WL_EXPORT void
1555weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
1556{
1557 wl_seat_init(&seat->seat);
1558 seat->has_pointer = 0;
1559 seat->has_keyboard = 0;
1560 seat->has_touch = 0;
1561
1562 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
1563 bind_seat);
1564
1565 seat->sprite = NULL;
1566 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1567
1568 seat->compositor = ec;
1569 seat->hotspot_x = 16;
1570 seat->hotspot_y = 16;
1571 seat->modifier_state = 0;
1572 seat->num_tp = 0;
1573
1574 seat->drag_surface_destroy_listener.notify =
1575 handle_drag_surface_destroy;
1576
1577 wl_list_insert(ec->seat_list.prev, &seat->link);
1578
1579 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
1580 wl_signal_add(&seat->seat.drag_icon_signal,
1581 &seat->new_drag_icon_listener);
1582
1583 clipboard_create(seat);
1584
1585 wl_signal_init(&seat->destroy_signal);
1586 wl_signal_emit(&ec->seat_created_signal, seat);
1587}
1588
1589WL_EXPORT void
1590weston_seat_release(struct weston_seat *seat)
1591{
1592 wl_list_remove(&seat->link);
1593 /* The global object is destroyed at wl_display_destroy() time. */
1594
1595 if (seat->sprite)
1596 pointer_unmap_sprite(seat);
1597
1598 if (seat->xkb_state.state != NULL)
1599 xkb_state_unref(seat->xkb_state.state);
1600 xkb_info_destroy(&seat->xkb_info);
1601
1602 wl_seat_release(&seat->seat);
1603 wl_signal_emit(&seat->destroy_signal, seat);
1604}
1605
1606static void
1607drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
1608{
1609 empty_region(&es->pending.input);
1610
1611 weston_surface_configure(es,
1612 es->geometry.x + sx, es->geometry.y + sy,
1613 width, height);
1614}
1615
1616static int
1617device_setup_new_drag_surface(struct weston_seat *ws,
1618 struct weston_surface *surface)
1619{
1620 struct wl_seat *seat = &ws->seat;
1621
1622 if (surface->configure) {
1623 wl_resource_post_error(&surface->surface.resource,
1624 WL_DISPLAY_ERROR_INVALID_OBJECT,
1625 "surface->configure already set");
1626 return 0;
1627 }
1628
1629 ws->drag_surface = surface;
1630
1631 weston_surface_set_position(ws->drag_surface,
1632 wl_fixed_to_double(seat->pointer->x),
1633 wl_fixed_to_double(seat->pointer->y));
1634
1635 surface->configure = drag_surface_configure;
1636
1637 wl_signal_add(&surface->surface.resource.destroy_signal,
1638 &ws->drag_surface_destroy_listener);
1639
1640 return 1;
1641}
1642
1643static void
1644device_release_drag_surface(struct weston_seat *seat)
1645{
1646 if (weston_surface_is_mapped(seat->drag_surface))
1647 weston_surface_unmap(seat->drag_surface);
1648
1649 seat->drag_surface->configure = NULL;
1650 empty_region(&seat->drag_surface->pending.input);
1651 wl_list_remove(&seat->drag_surface_destroy_listener.link);
1652 seat->drag_surface = NULL;
1653}
1654
1655static void
1656device_map_drag_surface(struct weston_seat *seat)
1657{
1658 struct wl_list *list;
1659
1660 if (weston_surface_is_mapped(seat->drag_surface) ||
1661 !seat->drag_surface->buffer_ref.buffer)
1662 return;
1663
1664 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
1665 list = &seat->sprite->layer_link;
1666 else
1667 list = &seat->compositor->cursor_layer.surface_list;
1668
1669 wl_list_insert(list, &seat->drag_surface->layer_link);
1670 weston_surface_update_transform(seat->drag_surface);
1671 empty_region(&seat->drag_surface->input);
1672}
1673
1674static void
1675weston_seat_update_drag_surface(struct weston_seat *seat,
1676 int dx, int dy)
1677{
1678 int surface_changed = 0;
1679
1680 if (!seat->drag_surface && !seat->seat.drag_surface)
1681 return;
1682
1683 if (seat->drag_surface && seat->seat.drag_surface &&
1684 (&seat->drag_surface->surface.resource !=
1685 &seat->seat.drag_surface->resource))
1686 /* between calls to this funcion we got a new drag_surface */
1687 surface_changed = 1;
1688
1689 if (!seat->seat.drag_surface || surface_changed) {
1690 device_release_drag_surface(seat);
1691 if (!surface_changed)
1692 return;
1693 }
1694
1695 if (!seat->drag_surface || surface_changed) {
1696 struct weston_surface *surface = (struct weston_surface *)
1697 seat->seat.drag_surface;
1698 if (!device_setup_new_drag_surface(seat, surface))
1699 return;
1700 }
1701
1702 /* the client may not have attached a buffer to the drag surface
1703 * when we setup it up, so check if map is needed on every update */
1704 device_map_drag_surface(seat);
1705
1706 if (!dx && !dy)
1707 return;
1708
1709 weston_surface_set_position(seat->drag_surface,
1710 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
1711 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
1712}
1713
1714WL_EXPORT void
1715weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
1716{
1717 struct weston_seat *seat;
1718
1719 wl_list_for_each(seat, &compositor->seat_list, link)
1720 weston_seat_update_drag_surface(seat, 0, 0);
1721}