blob: e670eb60dfd9b9ffc1c9d4f86b70e92e5d6cea7b [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{
Jason Ekstrand44a38632013-06-14 10:08:00 -050042 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040043 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
Kristian Høgsberg6848c252013-05-08 22:02:59 -040051 if (seat->pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052 return;
53
Kristian Høgsberg6848c252013-05-08 22:02:59 -040054 interface = seat->pointer->grab->interface;
55 interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040056}
57
58static void
59weston_compositor_idle_inhibit(struct weston_compositor *compositor)
60{
61 weston_compositor_wake(compositor);
62 compositor->idle_inhibit++;
63}
64
65static void
66weston_compositor_idle_release(struct weston_compositor *compositor)
67{
68 compositor->idle_inhibit--;
69 weston_compositor_wake(compositor);
70}
71
Kristian Høgsberg2158a882013-04-18 15:07:39 -040072static void
73lose_pointer_focus(struct wl_listener *listener, void *data)
74{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040075 struct weston_pointer *pointer =
76 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040077
78 pointer->focus_resource = NULL;
79}
80
81static void
82lose_keyboard_focus(struct wl_listener *listener, void *data)
83{
Kristian Høgsberg29139d42013-04-18 15:25:39 -040084 struct weston_keyboard *keyboard =
85 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040086
87 keyboard->focus_resource = NULL;
88}
89
90static void
91lose_touch_focus(struct wl_listener *listener, void *data)
92{
Kristian Høgsberge329f362013-05-06 22:19:57 -040093 struct weston_touch *touch =
94 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040095
96 touch->focus_resource = NULL;
97}
98
99static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400100default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400102 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400103 struct weston_surface *surface;
104 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400105
106 if (pointer->button_count > 0)
107 return;
108
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400109 surface = weston_compositor_pick_surface(pointer->seat->compositor,
110 pointer->x, pointer->y,
111 &sx, &sy);
112
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400113 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400114 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115}
116
117static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400118default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400119{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400120 struct weston_pointer *pointer = grab->pointer;
121 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400122
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400123 if (pointer->focus_resource) {
124 weston_surface_from_global_fixed(pointer->focus,
125 pointer->x, pointer->y,
126 &sx, &sy);
127 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
128 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400129}
130
131static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400132default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400133 uint32_t time, uint32_t button, uint32_t state_w)
134{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400135 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400136 struct weston_compositor *compositor = pointer->seat->compositor;
137 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400138 struct wl_resource *resource;
139 uint32_t serial;
140 enum wl_pointer_button_state state = state_w;
141 struct wl_display *display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400142 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143
144 resource = pointer->focus_resource;
145 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500146 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400147 serial = wl_display_next_serial(display);
148 wl_pointer_send_button(resource, serial, time, button, state_w);
149 }
150
151 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400152 state == WL_POINTER_BUTTON_STATE_RELEASED) {
153 surface = weston_compositor_pick_surface(compositor,
154 pointer->x,
155 pointer->y,
156 &sx, &sy);
157
158 weston_pointer_set_focus(pointer, surface, sx, sy);
159 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400160}
161
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400162static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163 default_pointer_grab_interface = {
164 default_grab_focus,
165 default_grab_motion,
166 default_grab_button
167};
168
Kristian Høgsberge329f362013-05-06 22:19:57 -0400169static void
170default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
171 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400172{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400173 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400174 struct wl_display *display;
175 uint32_t serial;
176
177 if (touch->focus_resource && touch->focus) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500178 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179 serial = wl_display_next_serial(display);
180 wl_touch_send_down(touch->focus_resource, serial, time,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500181 touch->focus->resource,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400182 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400183 }
184}
185
Kristian Høgsberge329f362013-05-06 22:19:57 -0400186static void
187default_grab_touch_up(struct weston_touch_grab *grab,
188 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400189{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400190 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 struct wl_display *display;
192 uint32_t serial;
193
194 if (touch->focus_resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500195 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196 serial = wl_display_next_serial(display);
197 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
198 }
199}
200
Kristian Høgsberge329f362013-05-06 22:19:57 -0400201static void
202default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
203 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400204{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400205 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400206
207 if (touch->focus_resource) {
208 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400209 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400210 }
211}
212
Kristian Høgsberge329f362013-05-06 22:19:57 -0400213static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400214 default_grab_touch_down,
215 default_grab_touch_up,
216 default_grab_touch_motion
217};
218
219static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400220default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400221 uint32_t time, uint32_t key, uint32_t state)
222{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400223 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224 struct wl_resource *resource;
225 struct wl_display *display;
226 uint32_t serial;
227
228 resource = keyboard->focus_resource;
229 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500230 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400231 serial = wl_display_next_serial(display);
232 wl_keyboard_send_key(resource, serial, time, key, state);
233 }
234}
235
236static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400237find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400238{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400239 if (!surface)
240 return NULL;
241
Jason Ekstrand44a38632013-06-14 10:08:00 -0500242 if (!surface->resource)
243 return NULL;
244
245 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246}
247
248static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400249default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400250 uint32_t mods_depressed, uint32_t mods_latched,
251 uint32_t mods_locked, uint32_t group)
252{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400253 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400254 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255 struct wl_resource *resource, *pr;
256
257 resource = keyboard->focus_resource;
258 if (!resource)
259 return;
260
261 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
262 mods_latched, mods_locked, group);
263
264 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
265 pr = find_resource_for_surface(&keyboard->resource_list,
266 pointer->focus);
267 if (pr) {
268 wl_keyboard_send_modifiers(pr,
269 serial,
270 keyboard->modifiers.mods_depressed,
271 keyboard->modifiers.mods_latched,
272 keyboard->modifiers.mods_locked,
273 keyboard->modifiers.group);
274 }
275 }
276}
277
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400278static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400279 default_keyboard_grab_interface = {
280 default_grab_key,
281 default_grab_modifiers,
282};
283
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400284static void
285pointer_unmap_sprite(struct weston_pointer *pointer)
286{
287 if (weston_surface_is_mapped(pointer->sprite))
288 weston_surface_unmap(pointer->sprite);
289
290 wl_list_remove(&pointer->sprite_destroy_listener.link);
291 pointer->sprite->configure = NULL;
292 pointer->sprite->configure_private = NULL;
293 pointer->sprite = NULL;
294}
295
296static void
297pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
298{
299 struct weston_pointer *pointer =
300 container_of(listener, struct weston_pointer,
301 sprite_destroy_listener);
302
303 pointer->sprite = NULL;
304}
305
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400306WL_EXPORT struct weston_pointer *
307weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400309 struct weston_pointer *pointer;
310
311 pointer = malloc(sizeof *pointer);
312 if (pointer == NULL)
313 return NULL;
314
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400315 memset(pointer, 0, sizeof *pointer);
316 wl_list_init(&pointer->resource_list);
317 pointer->focus_listener.notify = lose_pointer_focus;
318 pointer->default_grab.interface = &default_pointer_grab_interface;
319 pointer->default_grab.pointer = pointer;
320 pointer->grab = &pointer->default_grab;
321 wl_signal_init(&pointer->focus_signal);
322
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400323 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
324
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 /* FIXME: Pick better co-ords. */
326 pointer->x = wl_fixed_from_int(100);
327 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400328
329 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330}
331
332WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400333weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400335 if (pointer->sprite)
336 pointer_unmap_sprite(pointer);
337
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338 /* XXX: What about pointer->resource_list? */
339 if (pointer->focus_resource)
340 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400341 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342}
343
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400344WL_EXPORT struct weston_keyboard *
345weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400346{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400347 struct weston_keyboard *keyboard;
348
349 keyboard = malloc(sizeof *keyboard);
350 if (keyboard == NULL)
351 return NULL;
352
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353 memset(keyboard, 0, sizeof *keyboard);
354 wl_list_init(&keyboard->resource_list);
355 wl_array_init(&keyboard->keys);
356 keyboard->focus_listener.notify = lose_keyboard_focus;
357 keyboard->default_grab.interface = &default_keyboard_grab_interface;
358 keyboard->default_grab.keyboard = keyboard;
359 keyboard->grab = &keyboard->default_grab;
360 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400361
362 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363}
364
365WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400366weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400367{
368 /* XXX: What about keyboard->resource_list? */
369 if (keyboard->focus_resource)
370 wl_list_remove(&keyboard->focus_listener.link);
371 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400372 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400373}
374
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400375WL_EXPORT struct weston_touch *
376weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400378 struct weston_touch *touch;
379
380 touch = malloc(sizeof *touch);
381 if (touch == NULL)
382 return NULL;
383
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400384 memset(touch, 0, sizeof *touch);
385 wl_list_init(&touch->resource_list);
386 touch->focus_listener.notify = lose_touch_focus;
387 touch->default_grab.interface = &default_touch_grab_interface;
388 touch->default_grab.touch = touch;
389 touch->grab = &touch->default_grab;
390 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400391
392 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393}
394
395WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400396weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397{
398 /* XXX: What about touch->resource_list? */
399 if (touch->focus_resource)
400 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400401 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402}
403
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400405seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500407 struct wl_list *link;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 enum wl_seat_capability caps = 0;
409
410 if (seat->pointer)
411 caps |= WL_SEAT_CAPABILITY_POINTER;
412 if (seat->keyboard)
413 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
414 if (seat->touch)
415 caps |= WL_SEAT_CAPABILITY_TOUCH;
416
Jason Ekstrand44a38632013-06-14 10:08:00 -0500417 for (link = seat->base_resource_list.next;
418 link != &seat->base_resource_list; link = link->next) {
419 wl_seat_send_capabilities(wl_resource_from_link(link), caps);
420 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421}
422
423WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400424weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400425 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400426 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400427{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400428 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429 struct wl_resource *resource, *kr;
430 struct wl_display *display;
431 uint32_t serial;
432
433 resource = pointer->focus_resource;
434 if (resource && pointer->focus != surface) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500435 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400436 serial = wl_display_next_serial(display);
437 wl_pointer_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500438 pointer->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439 wl_list_remove(&pointer->focus_listener.link);
440 }
441
442 resource = find_resource_for_surface(&pointer->resource_list,
443 surface);
444 if (resource &&
445 (pointer->focus != surface ||
446 pointer->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500447 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400448 serial = wl_display_next_serial(display);
449 if (kbd) {
450 kr = find_resource_for_surface(&kbd->resource_list,
451 surface);
452 if (kr) {
453 wl_keyboard_send_modifiers(kr,
454 serial,
455 kbd->modifiers.mods_depressed,
456 kbd->modifiers.mods_latched,
457 kbd->modifiers.mods_locked,
458 kbd->modifiers.group);
459 }
460 }
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500461 wl_pointer_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462 sx, sy);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500463 wl_resource_add_destroy_listener(resource,
464 &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465 pointer->focus_serial = serial;
466 }
467
468 pointer->focus_resource = resource;
469 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 wl_signal_emit(&pointer->focus_signal, pointer);
471}
472
473WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400474weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400475 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476{
477 struct wl_resource *resource;
478 struct wl_display *display;
479 uint32_t serial;
480
481 if (keyboard->focus_resource && keyboard->focus != surface) {
482 resource = keyboard->focus_resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -0500483 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400484 serial = wl_display_next_serial(display);
485 wl_keyboard_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500486 keyboard->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487 wl_list_remove(&keyboard->focus_listener.link);
488 }
489
490 resource = find_resource_for_surface(&keyboard->resource_list,
491 surface);
492 if (resource &&
493 (keyboard->focus != surface ||
494 keyboard->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500495 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496 serial = wl_display_next_serial(display);
497 wl_keyboard_send_modifiers(resource, serial,
498 keyboard->modifiers.mods_depressed,
499 keyboard->modifiers.mods_latched,
500 keyboard->modifiers.mods_locked,
501 keyboard->modifiers.group);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500502 wl_keyboard_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400503 &keyboard->keys);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500504 wl_resource_add_destroy_listener(resource,
505 &keyboard->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506 keyboard->focus_serial = serial;
507 }
508
509 keyboard->focus_resource = resource;
510 keyboard->focus = surface;
511 wl_signal_emit(&keyboard->focus_signal, keyboard);
512}
513
514WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400515weston_keyboard_start_grab(struct weston_keyboard *keyboard,
516 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517{
518 keyboard->grab = grab;
519 grab->keyboard = keyboard;
520
521 /* XXX focus? */
522}
523
524WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400525weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526{
527 keyboard->grab = &keyboard->default_grab;
528}
529
530WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400531weston_pointer_start_grab(struct weston_pointer *pointer,
532 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400534 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400535
536 pointer->grab = grab;
537 interface = pointer->grab->interface;
538 grab->pointer = pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400539 interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540}
541
542WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400543weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400545 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546
547 pointer->grab = &pointer->default_grab;
548 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400549 interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550}
551
552WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400553weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554{
555 touch->grab = grab;
556 grab->touch = touch;
557}
558
559WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400560weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561{
562 touch->grab = &touch->default_grab;
563}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400564
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400565static void
566clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
567{
568 struct weston_compositor *ec = seat->compositor;
569 struct weston_output *output, *prev = NULL;
570 int x, y, old_x, old_y, valid = 0;
571
572 x = wl_fixed_to_int(*fx);
573 y = wl_fixed_to_int(*fy);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400574 old_x = wl_fixed_to_int(seat->pointer->x);
575 old_y = wl_fixed_to_int(seat->pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400576
577 wl_list_for_each(output, &ec->output_list, link) {
578 if (pixman_region32_contains_point(&output->region,
579 x, y, NULL))
580 valid = 1;
581 if (pixman_region32_contains_point(&output->region,
582 old_x, old_y, NULL))
583 prev = output;
584 }
585
586 if (!valid) {
587 if (x < prev->x)
588 *fx = wl_fixed_from_int(prev->x);
589 else if (x >= prev->x + prev->width)
590 *fx = wl_fixed_from_int(prev->x +
591 prev->width - 1);
592 if (y < prev->y)
593 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200594 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400595 *fy = wl_fixed_from_int(prev->y +
596 prev->height - 1);
597 }
598}
599
600/* Takes absolute values */
601static void
602move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
603{
604 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400605 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400606 struct weston_output *output;
607 int32_t ix, iy;
608
609 clip_pointer_motion(seat, &x, &y);
610
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400611 pointer->x = x;
612 pointer->y = y;
613
614 ix = wl_fixed_to_int(x);
615 iy = wl_fixed_to_int(y);
616
617 wl_list_for_each(output, &ec->output_list, link)
618 if (output->zoom.active &&
619 pixman_region32_contains_point(&output->region,
620 ix, iy, NULL))
621 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
622
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400623 if (pointer->sprite) {
624 weston_surface_set_position(pointer->sprite,
625 ix - pointer->hotspot_x,
626 iy - pointer->hotspot_y);
627 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400628 }
629}
630
631WL_EXPORT void
632notify_motion(struct weston_seat *seat,
633 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
634{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400635 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400636 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400637 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400638
639 weston_compositor_wake(ec);
640
641 move_pointer(seat, pointer->x + dx, pointer->y + dy);
642
643 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400644 interface->focus(pointer->grab);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400645 interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400646}
647
648WL_EXPORT void
649notify_motion_absolute(struct weston_seat *seat,
650 uint32_t time, wl_fixed_t x, wl_fixed_t y)
651{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400652 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400653 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400654 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400655
656 weston_compositor_wake(ec);
657
658 move_pointer(seat, x, y);
659
660 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400661 interface->focus(pointer->grab);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400662 interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400663}
664
665WL_EXPORT void
666weston_surface_activate(struct weston_surface *surface,
667 struct weston_seat *seat)
668{
669 struct weston_compositor *compositor = seat->compositor;
670
Kristian Høgsberge3148752013-05-06 23:19:49 -0400671 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400672 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400673 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400674 }
675
676 wl_signal_emit(&compositor->activate_signal, surface);
677}
678
679WL_EXPORT void
680notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
681 enum wl_pointer_button_state state)
682{
683 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400684 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400685 struct weston_surface *focus =
686 (struct weston_surface *) pointer->focus;
687 uint32_t serial = wl_display_next_serial(compositor->wl_display);
688
689 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
690 if (compositor->ping_handler && focus)
691 compositor->ping_handler(focus, serial);
692 weston_compositor_idle_inhibit(compositor);
693 if (pointer->button_count == 0) {
694 pointer->grab_button = button;
695 pointer->grab_time = time;
696 pointer->grab_x = pointer->x;
697 pointer->grab_y = pointer->y;
698 }
699 pointer->button_count++;
700 } else {
701 weston_compositor_idle_release(compositor);
702 pointer->button_count--;
703 }
704
705 weston_compositor_run_button_binding(compositor, seat, time, button,
706 state);
707
708 pointer->grab->interface->button(pointer->grab, time, button, state);
709
710 if (pointer->button_count == 1)
711 pointer->grab_serial =
712 wl_display_get_serial(compositor->wl_display);
713}
714
715WL_EXPORT void
716notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
717 wl_fixed_t value)
718{
719 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400720 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400721 struct weston_surface *focus =
722 (struct weston_surface *) pointer->focus;
723 uint32_t serial = wl_display_next_serial(compositor->wl_display);
724
725 if (compositor->ping_handler && focus)
726 compositor->ping_handler(focus, serial);
727
728 weston_compositor_wake(compositor);
729
730 if (!value)
731 return;
732
733 if (weston_compositor_run_axis_binding(compositor, seat,
734 time, axis, value))
735 return;
736
737 if (pointer->focus_resource)
738 wl_pointer_send_axis(pointer->focus_resource, time, axis,
739 value);
740}
741
742WL_EXPORT void
743notify_modifiers(struct weston_seat *seat, uint32_t serial)
744{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400745 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400746 struct weston_keyboard_grab *grab = keyboard->grab;
747 uint32_t mods_depressed, mods_latched, mods_locked, group;
748 uint32_t mods_lookup;
749 enum weston_led leds = 0;
750 int changed = 0;
751
752 /* Serialize and update our internal state, checking to see if it's
753 * different to the previous state. */
754 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
755 XKB_STATE_DEPRESSED);
756 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
757 XKB_STATE_LATCHED);
758 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
759 XKB_STATE_LOCKED);
760 group = xkb_state_serialize_group(seat->xkb_state.state,
761 XKB_STATE_EFFECTIVE);
762
Kristian Høgsberge3148752013-05-06 23:19:49 -0400763 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
764 mods_latched != seat->keyboard->modifiers.mods_latched ||
765 mods_locked != seat->keyboard->modifiers.mods_locked ||
766 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400767 changed = 1;
768
Kristian Høgsberge3148752013-05-06 23:19:49 -0400769 seat->keyboard->modifiers.mods_depressed = mods_depressed;
770 seat->keyboard->modifiers.mods_latched = mods_latched;
771 seat->keyboard->modifiers.mods_locked = mods_locked;
772 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400773
774 /* And update the modifier_state for bindings. */
775 mods_lookup = mods_depressed | mods_latched;
776 seat->modifier_state = 0;
777 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
778 seat->modifier_state |= MODIFIER_CTRL;
779 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
780 seat->modifier_state |= MODIFIER_ALT;
781 if (mods_lookup & (1 << seat->xkb_info.super_mod))
782 seat->modifier_state |= MODIFIER_SUPER;
783 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
784 seat->modifier_state |= MODIFIER_SHIFT;
785
786 /* Finally, notify the compositor that LEDs have changed. */
787 if (xkb_state_led_index_is_active(seat->xkb_state.state,
788 seat->xkb_info.num_led))
789 leds |= LED_NUM_LOCK;
790 if (xkb_state_led_index_is_active(seat->xkb_state.state,
791 seat->xkb_info.caps_led))
792 leds |= LED_CAPS_LOCK;
793 if (xkb_state_led_index_is_active(seat->xkb_state.state,
794 seat->xkb_info.scroll_led))
795 leds |= LED_SCROLL_LOCK;
796 if (leds != seat->xkb_state.leds && seat->led_update)
797 seat->led_update(seat, leds);
798 seat->xkb_state.leds = leds;
799
800 if (changed) {
801 grab->interface->modifiers(grab,
802 serial,
803 keyboard->modifiers.mods_depressed,
804 keyboard->modifiers.mods_latched,
805 keyboard->modifiers.mods_locked,
806 keyboard->modifiers.group);
807 }
808}
809
810static void
811update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
812 enum wl_keyboard_key_state state)
813{
814 enum xkb_key_direction direction;
815
816 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
817 direction = XKB_KEY_DOWN;
818 else
819 direction = XKB_KEY_UP;
820
821 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
822 * broken keycode system, which starts at 8. */
823 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
824
825 notify_modifiers(seat, serial);
826}
827
828WL_EXPORT void
829notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
830 enum wl_keyboard_key_state state,
831 enum weston_key_state_update update_state)
832{
833 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400834 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400835 struct weston_surface *focus =
836 (struct weston_surface *) keyboard->focus;
837 struct weston_keyboard_grab *grab = keyboard->grab;
838 uint32_t serial = wl_display_next_serial(compositor->wl_display);
839 uint32_t *k, *end;
840
841 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
842 if (compositor->ping_handler && focus)
843 compositor->ping_handler(focus, serial);
844
845 weston_compositor_idle_inhibit(compositor);
846 keyboard->grab_key = key;
847 keyboard->grab_time = time;
848 } else {
849 weston_compositor_idle_release(compositor);
850 }
851
852 end = keyboard->keys.data + keyboard->keys.size;
853 for (k = keyboard->keys.data; k < end; k++) {
854 if (*k == key) {
855 /* Ignore server-generated repeats. */
856 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
857 return;
858 *k = *--end;
859 }
860 }
861 keyboard->keys.size = (void *) end - keyboard->keys.data;
862 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
863 k = wl_array_add(&keyboard->keys, sizeof *k);
864 *k = key;
865 }
866
867 if (grab == &keyboard->default_grab ||
868 grab == &keyboard->input_method_grab) {
869 weston_compositor_run_key_binding(compositor, seat, time, key,
870 state);
871 grab = keyboard->grab;
872 }
873
874 grab->interface->key(grab, time, key, state);
875
876 if (update_state == STATE_UPDATE_AUTOMATIC) {
877 update_modifier_state(seat,
878 wl_display_get_serial(compositor->wl_display),
879 key,
880 state);
881 }
882}
883
884WL_EXPORT void
885notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
886 wl_fixed_t x, wl_fixed_t y)
887{
888 struct weston_compositor *compositor = seat->compositor;
889
890 if (output) {
891 move_pointer(seat, x, y);
892 compositor->focus = 1;
893 } else {
894 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400895 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400896 * NULL) here, but somehow that breaks re-entry... */
897 }
898}
899
900static void
901destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
902{
903 struct weston_seat *ws;
904
905 ws = container_of(listener, struct weston_seat,
906 saved_kbd_focus_listener);
907
908 ws->saved_kbd_focus = NULL;
909}
910
911WL_EXPORT void
912notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
913 enum weston_key_state_update update_state)
914{
915 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400916 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400917 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400918 uint32_t *k, serial;
919
920 serial = wl_display_next_serial(compositor->wl_display);
921 wl_array_copy(&keyboard->keys, keys);
922 wl_array_for_each(k, &keyboard->keys) {
923 weston_compositor_idle_inhibit(compositor);
924 if (update_state == STATE_UPDATE_AUTOMATIC)
925 update_modifier_state(seat, serial, *k,
926 WL_KEYBOARD_KEY_STATE_PRESSED);
927 }
928
929 /* Run key bindings after we've updated the state. */
930 wl_array_for_each(k, &keyboard->keys) {
931 weston_compositor_run_key_binding(compositor, seat, 0, *k,
932 WL_KEYBOARD_KEY_STATE_PRESSED);
933 }
934
935 surface = seat->saved_kbd_focus;
936
937 if (surface) {
938 wl_list_remove(&seat->saved_kbd_focus_listener.link);
939 weston_keyboard_set_focus(keyboard, surface);
940 seat->saved_kbd_focus = NULL;
941 }
942}
943
944WL_EXPORT void
945notify_keyboard_focus_out(struct weston_seat *seat)
946{
947 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400948 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400949 uint32_t *k, serial;
950
951 serial = wl_display_next_serial(compositor->wl_display);
952 wl_array_for_each(k, &keyboard->keys) {
953 weston_compositor_idle_release(compositor);
954 update_modifier_state(seat, serial, *k,
955 WL_KEYBOARD_KEY_STATE_RELEASED);
956 }
957
958 seat->modifier_state = 0;
959
960 if (keyboard->focus) {
961 seat->saved_kbd_focus = keyboard->focus;
962 seat->saved_kbd_focus_listener.notify =
963 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500964 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400965 &seat->saved_kbd_focus_listener);
966 }
967
968 weston_keyboard_set_focus(keyboard, NULL);
969 /* FIXME: We really need keyboard grab cancel here to
970 * let the grab shut down properly. As it is we leak
971 * the grab data. */
972 weston_keyboard_end_grab(keyboard);
973}
974
975static void
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400976touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400977{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400978 struct wl_resource *resource;
979
980 if (seat->touch->focus == surface)
981 return;
982
983 if (seat->touch->focus_resource)
984 wl_list_remove(&seat->touch->focus_listener.link);
985 seat->touch->focus = NULL;
986 seat->touch->focus_resource = NULL;
987
988 if (surface) {
989 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -0400990 find_resource_for_surface(&seat->touch->resource_list,
991 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400992 if (!resource) {
993 weston_log("couldn't find resource\n");
994 return;
995 }
996
997 seat->touch->focus = surface;
998 seat->touch->focus_resource = resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -0500999 wl_resource_add_destroy_listener(resource,
1000 &seat->touch->focus_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001001 }
1002}
1003
1004/**
1005 * notify_touch - emulates button touches and notifies surfaces accordingly.
1006 *
1007 * It assumes always the correct cycle sequence until it gets here: touch_down
1008 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1009 * for sending along such order.
1010 *
1011 */
1012WL_EXPORT void
1013notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1014 wl_fixed_t x, wl_fixed_t y, int touch_type)
1015{
1016 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001017 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001018 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001019 struct weston_surface *es;
1020 wl_fixed_t sx, sy;
1021
1022 /* Update grab's global coordinates. */
1023 touch->grab_x = x;
1024 touch->grab_y = y;
1025
1026 switch (touch_type) {
1027 case WL_TOUCH_DOWN:
1028 weston_compositor_idle_inhibit(ec);
1029
1030 seat->num_tp++;
1031
1032 /* the first finger down picks the surface, and all further go
1033 * to that surface for the remainder of the touch session i.e.
1034 * until all touch points are up again. */
1035 if (seat->num_tp == 1) {
1036 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001037 touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001038 } else if (touch->focus) {
1039 es = (struct weston_surface *) touch->focus;
1040 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1041 } else {
1042 /* Unexpected condition: We have non-initial touch but
1043 * there is no focused surface.
1044 */
1045 weston_log("touch event received with %d points down"
1046 "but no surface focused\n", seat->num_tp);
1047 return;
1048 }
1049
1050 grab->interface->down(grab, time, touch_id, sx, sy);
1051 break;
1052 case WL_TOUCH_MOTION:
1053 es = (struct weston_surface *) touch->focus;
1054 if (!es)
1055 break;
1056
1057 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1058 grab->interface->motion(grab, time, touch_id, sx, sy);
1059 break;
1060 case WL_TOUCH_UP:
1061 weston_compositor_idle_release(ec);
1062 seat->num_tp--;
1063
1064 grab->interface->up(grab, time, touch_id);
1065 if (seat->num_tp == 0)
1066 touch_set_focus(seat, NULL);
1067 break;
1068 }
1069}
1070
1071static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001072pointer_cursor_surface_configure(struct weston_surface *es,
1073 int32_t dx, int32_t dy, int32_t width, int32_t height)
1074{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001075 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001076 int x, y;
1077
1078 if (width == 0)
1079 return;
1080
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001081 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001082
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001083 pointer->hotspot_x -= dx;
1084 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001085
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001086 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1087 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001088
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001089 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001090
1091 empty_region(&es->pending.input);
1092
1093 if (!weston_surface_is_mapped(es)) {
1094 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1095 &es->layer_link);
1096 weston_surface_update_transform(es);
1097 }
1098}
1099
1100static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001101pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1102 uint32_t serial, struct wl_resource *surface_resource,
1103 int32_t x, int32_t y)
1104{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001105 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001106 struct weston_surface *surface = NULL;
1107
1108 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001109 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001110
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001111 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001112 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001113 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001114 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001115 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001116 return;
1117
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001118 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001119 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001120 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001121 WL_DISPLAY_ERROR_INVALID_OBJECT,
1122 "surface->configure already "
1123 "set");
1124 return;
1125 }
1126 }
1127
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001128 if (pointer->sprite)
1129 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001130
1131 if (!surface)
1132 return;
1133
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001134 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001135 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001136
1137 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001138 surface->configure_private = pointer;
1139 pointer->sprite = surface;
1140 pointer->hotspot_x = x;
1141 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001142
1143 if (surface->buffer_ref.buffer)
1144 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1145 weston_surface_buffer_height(surface));
1146}
1147
1148static const struct wl_pointer_interface pointer_interface = {
1149 pointer_set_cursor
1150};
1151
1152static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001153seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1154 uint32_t id)
1155{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001156 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001157 struct wl_resource *cr;
1158
Kristian Høgsberge3148752013-05-06 23:19:49 -04001159 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001160 return;
1161
1162 cr = wl_client_add_object(client, &wl_pointer_interface,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001163 &pointer_interface, id, seat->pointer);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001164 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1165 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001166
Kristian Høgsberge3148752013-05-06 23:19:49 -04001167 if (seat->pointer->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001168 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169 struct weston_surface *surface;
1170 wl_fixed_t sx, sy;
1171
Kristian Høgsberge3148752013-05-06 23:19:49 -04001172 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001173 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001174 seat->pointer->x,
1175 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001176 &sx,
1177 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001178 weston_pointer_set_focus(seat->pointer,
1179 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001180 sx,
1181 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182 }
1183}
1184
1185static void
1186seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1187 uint32_t id)
1188{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001189 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190 struct wl_resource *cr;
1191
Kristian Høgsberge3148752013-05-06 23:19:49 -04001192 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193 return;
1194
1195 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
1196 seat);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001197 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1198 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199
1200 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1201 seat->xkb_info.keymap_fd,
1202 seat->xkb_info.keymap_size);
1203
Kristian Høgsberge3148752013-05-06 23:19:49 -04001204 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001205 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001206 weston_keyboard_set_focus(seat->keyboard,
1207 seat->keyboard->focus);
1208 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 }
1210}
1211
1212static void
1213seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1214 uint32_t id)
1215{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001216 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001217 struct wl_resource *cr;
1218
Kristian Høgsberge3148752013-05-06 23:19:49 -04001219 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001220 return;
1221
1222 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001223 wl_list_insert(&seat->touch->resource_list, wl_resource_get_link(cr));
1224 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001225}
1226
1227static const struct wl_seat_interface seat_interface = {
1228 seat_get_pointer,
1229 seat_get_keyboard,
1230 seat_get_touch,
1231};
1232
1233static void
1234bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1235{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001236 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001237 struct wl_resource *resource;
1238 enum wl_seat_capability caps = 0;
1239
1240 resource = wl_client_add_object(client, &wl_seat_interface,
1241 &seat_interface, id, data);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001242 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1243 wl_resource_set_destructor(resource, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001244
1245 if (seat->pointer)
1246 caps |= WL_SEAT_CAPABILITY_POINTER;
1247 if (seat->keyboard)
1248 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1249 if (seat->touch)
1250 caps |= WL_SEAT_CAPABILITY_TOUCH;
1251
1252 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001253 if (version >= 2)
1254 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001255}
1256
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001257int
1258weston_compositor_xkb_init(struct weston_compositor *ec,
1259 struct xkb_rule_names *names)
1260{
1261 if (ec->xkb_context == NULL) {
1262 ec->xkb_context = xkb_context_new(0);
1263 if (ec->xkb_context == NULL) {
1264 weston_log("failed to create XKB context\n");
1265 return -1;
1266 }
1267 }
1268
1269 if (names)
1270 ec->xkb_names = *names;
1271 if (!ec->xkb_names.rules)
1272 ec->xkb_names.rules = strdup("evdev");
1273 if (!ec->xkb_names.model)
1274 ec->xkb_names.model = strdup("pc105");
1275 if (!ec->xkb_names.layout)
1276 ec->xkb_names.layout = strdup("us");
1277
1278 return 0;
1279}
1280
1281static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1282{
1283 if (xkb_info->keymap)
1284 xkb_map_unref(xkb_info->keymap);
1285
1286 if (xkb_info->keymap_area)
1287 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1288 if (xkb_info->keymap_fd >= 0)
1289 close(xkb_info->keymap_fd);
1290}
1291
1292void
1293weston_compositor_xkb_destroy(struct weston_compositor *ec)
1294{
1295 free((char *) ec->xkb_names.rules);
1296 free((char *) ec->xkb_names.model);
1297 free((char *) ec->xkb_names.layout);
1298 free((char *) ec->xkb_names.variant);
1299 free((char *) ec->xkb_names.options);
1300
1301 xkb_info_destroy(&ec->xkb_info);
1302 xkb_context_unref(ec->xkb_context);
1303}
1304
1305static int
1306weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1307{
1308 char *keymap_str;
1309
1310 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1311 XKB_MOD_NAME_SHIFT);
1312 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1313 XKB_MOD_NAME_CAPS);
1314 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1315 XKB_MOD_NAME_CTRL);
1316 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1317 XKB_MOD_NAME_ALT);
1318 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1319 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1320 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1321 XKB_MOD_NAME_LOGO);
1322 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1323
1324 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1325 XKB_LED_NAME_NUM);
1326 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1327 XKB_LED_NAME_CAPS);
1328 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1329 XKB_LED_NAME_SCROLL);
1330
1331 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1332 if (keymap_str == NULL) {
1333 weston_log("failed to get string version of keymap\n");
1334 return -1;
1335 }
1336 xkb_info->keymap_size = strlen(keymap_str) + 1;
1337
1338 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1339 if (xkb_info->keymap_fd < 0) {
1340 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1341 (unsigned long) xkb_info->keymap_size);
1342 goto err_keymap_str;
1343 }
1344
1345 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1346 PROT_READ | PROT_WRITE,
1347 MAP_SHARED, xkb_info->keymap_fd, 0);
1348 if (xkb_info->keymap_area == MAP_FAILED) {
1349 weston_log("failed to mmap() %lu bytes\n",
1350 (unsigned long) xkb_info->keymap_size);
1351 goto err_dev_zero;
1352 }
1353 strcpy(xkb_info->keymap_area, keymap_str);
1354 free(keymap_str);
1355
1356 return 0;
1357
1358err_dev_zero:
1359 close(xkb_info->keymap_fd);
1360 xkb_info->keymap_fd = -1;
1361err_keymap_str:
1362 free(keymap_str);
1363 return -1;
1364}
1365
1366static int
1367weston_compositor_build_global_keymap(struct weston_compositor *ec)
1368{
1369 if (ec->xkb_info.keymap != NULL)
1370 return 0;
1371
1372 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1373 &ec->xkb_names,
1374 0);
1375 if (ec->xkb_info.keymap == NULL) {
1376 weston_log("failed to compile global XKB keymap\n");
1377 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1378 "options %s\n",
1379 ec->xkb_names.rules, ec->xkb_names.model,
1380 ec->xkb_names.layout, ec->xkb_names.variant,
1381 ec->xkb_names.options);
1382 return -1;
1383 }
1384
1385 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1386 return -1;
1387
1388 return 0;
1389}
1390
1391WL_EXPORT int
1392weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1393{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001394 struct weston_keyboard *keyboard;
1395
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001396 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001397 return 0;
1398
1399 if (keymap != NULL) {
1400 seat->xkb_info.keymap = xkb_map_ref(keymap);
1401 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1402 return -1;
1403 } else {
1404 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1405 return -1;
1406 seat->xkb_info = seat->compositor->xkb_info;
1407 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1408 }
1409
1410 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1411 if (seat->xkb_state.state == NULL) {
1412 weston_log("failed to initialise XKB state\n");
1413 return -1;
1414 }
1415
1416 seat->xkb_state.leds = 0;
1417
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001418 keyboard = weston_keyboard_create();
1419 if (keyboard == NULL) {
1420 weston_log("failed to allocate weston keyboard struct\n");
1421 return -1;
1422 }
1423
1424 seat->keyboard = keyboard;
1425 keyboard->seat = seat;
1426
1427 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001428
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001429 return 0;
1430}
1431
1432WL_EXPORT void
1433weston_seat_init_pointer(struct weston_seat *seat)
1434{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001435 struct weston_pointer *pointer;
1436
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001437 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001438 return;
1439
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001440 pointer = weston_pointer_create();
1441 if (pointer == NULL)
1442 return;
1443
1444 seat->pointer = pointer;
1445 pointer->seat = seat;
1446
1447 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001448}
1449
1450WL_EXPORT void
1451weston_seat_init_touch(struct weston_seat *seat)
1452{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001453 struct weston_touch *touch;
1454
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001455 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001456 return;
1457
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001458 touch = weston_touch_create();
1459 if (touch == NULL)
1460 return;
1461
1462 seat->touch = touch;
1463 touch->seat = seat;
1464
1465 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466}
1467
1468WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001469weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1470 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001471{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001472 memset(seat, 0, sizeof *seat);
1473
Kristian Høgsberge3148752013-05-06 23:19:49 -04001474 seat->selection_data_source = NULL;
1475 wl_list_init(&seat->base_resource_list);
1476 wl_signal_init(&seat->selection_signal);
1477 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001478 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001479
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001480 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
1481 bind_seat);
1482
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001484 seat->modifier_state = 0;
1485 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001486 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001488 wl_list_insert(ec->seat_list.prev, &seat->link);
1489
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001490 clipboard_create(seat);
1491
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001492 wl_signal_emit(&ec->seat_created_signal, seat);
1493}
1494
1495WL_EXPORT void
1496weston_seat_release(struct weston_seat *seat)
1497{
1498 wl_list_remove(&seat->link);
1499 /* The global object is destroyed at wl_display_destroy() time. */
1500
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001501 if (seat->xkb_state.state != NULL)
1502 xkb_state_unref(seat->xkb_state.state);
1503 xkb_info_destroy(&seat->xkb_info);
1504
Kristian Høgsberge3148752013-05-06 23:19:49 -04001505 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001506 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001507 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001508 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001509 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001510 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001511
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001512 free (seat->seat_name);
1513
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001514 wl_signal_emit(&seat->destroy_signal, seat);
1515}