blob: d1acd926f0f938ec8fd33a7c197abac4be1f1a54 [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
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010023#include "config.h"
24
Kristian Høgsberg2158a882013-04-18 15:07:39 -040025#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040028#include <sys/mman.h>
29#include <assert.h>
30#include <unistd.h>
Matt Roper01a92732013-06-24 16:52:44 +010031#include <fcntl.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040033#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040034#include "compositor.h"
35
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040036static void
37empty_region(pixman_region32_t *region)
38{
39 pixman_region32_fini(region);
40 pixman_region32_init(region);
41}
42
43static void unbind_resource(struct wl_resource *resource)
44{
Jason Ekstrand44a38632013-06-14 10:08:00 -050045 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040046}
47
48void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040049weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050{
Kristian Høgsbergda751b82013-07-04 00:58:07 -040051 const struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052
Kristian Høgsbergda751b82013-07-04 00:58:07 -040053 if (pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040054 return;
55
Kristian Høgsbergda751b82013-07-04 00:58:07 -040056 pointer->grab->interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040057}
58
59static void
60weston_compositor_idle_inhibit(struct weston_compositor *compositor)
61{
62 weston_compositor_wake(compositor);
63 compositor->idle_inhibit++;
64}
65
66static void
67weston_compositor_idle_release(struct weston_compositor *compositor)
68{
69 compositor->idle_inhibit--;
70 weston_compositor_wake(compositor);
71}
72
Kristian Høgsberg2158a882013-04-18 15:07:39 -040073static void
Neil Roberts96d790e2013-09-19 17:32:00 +010074move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040075{
Neil Roberts96d790e2013-09-19 17:32:00 +010076 wl_list_insert_list(destination, source);
77 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078}
79
80static void
Neil Roberts96d790e2013-09-19 17:32:00 +010081move_resources_for_client(struct wl_list *destination,
82 struct wl_list *source,
83 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040084{
Neil Roberts96d790e2013-09-19 17:32:00 +010085 struct wl_resource *resource, *tmp;
86 wl_resource_for_each_safe(resource, tmp, source) {
87 if (wl_resource_get_client(resource) == client) {
88 wl_list_remove(wl_resource_get_link(resource));
89 wl_list_insert(destination,
90 wl_resource_get_link(resource));
91 }
92 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -040093}
94
95static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -040096default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040097{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040098 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -040099 struct weston_surface *surface;
100 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (pointer->button_count > 0)
103 return;
104
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400105 surface = weston_compositor_pick_surface(pointer->seat->compositor,
106 pointer->x, pointer->y,
107 &sx, &sy);
108
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400109 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400110 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400111}
112
113static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400114default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400116 struct weston_pointer *pointer = grab->pointer;
117 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100118 struct wl_list *resource_list;
119 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400120
Neil Roberts96d790e2013-09-19 17:32:00 +0100121 resource_list = &pointer->focus_resource_list;
122 wl_resource_for_each(resource, resource_list) {
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400123 weston_surface_from_global_fixed(pointer->focus,
124 pointer->x, pointer->y,
125 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +0100126 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400127 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400128}
129
130static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400131default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400132 uint32_t time, uint32_t button, uint32_t state_w)
133{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400134 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400135 struct weston_compositor *compositor = pointer->seat->compositor;
136 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137 struct wl_resource *resource;
138 uint32_t serial;
139 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100140 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400141 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100142 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143
Neil Roberts96d790e2013-09-19 17:32:00 +0100144 resource_list = &pointer->focus_resource_list;
145 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100147 wl_resource_for_each(resource, resource_list)
148 wl_pointer_send_button(resource,
149 serial,
150 time,
151 button,
152 state_w);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400153 }
154
155 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400156 state == WL_POINTER_BUTTON_STATE_RELEASED) {
157 surface = weston_compositor_pick_surface(compositor,
158 pointer->x,
159 pointer->y,
160 &sx, &sy);
161
162 weston_pointer_set_focus(pointer, surface, sx, sy);
163 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400164}
165
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400166static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400167 default_pointer_grab_interface = {
168 default_grab_focus,
169 default_grab_motion,
170 default_grab_button
171};
172
Kristian Høgsberge329f362013-05-06 22:19:57 -0400173static void
174default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
175 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400176{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400177 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100178 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100180 struct wl_resource *resource;
181 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400182
Neil Roberts96d790e2013-09-19 17:32:00 +0100183 resource_list = &touch->focus_resource_list;
184
185 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400186 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100187 wl_resource_for_each(resource, resource_list)
188 wl_touch_send_down(resource, serial, time,
189 touch->focus->resource,
190 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 }
192}
193
Kristian Høgsberge329f362013-05-06 22:19:57 -0400194static void
195default_grab_touch_up(struct weston_touch_grab *grab,
196 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400197{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400198 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100199 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400200 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100201 struct wl_resource *resource;
202 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400203
Neil Roberts96d790e2013-09-19 17:32:00 +0100204 resource_list = &touch->focus_resource_list;
205
206 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400207 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100208 wl_resource_for_each(resource, resource_list)
209 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400210 }
211}
212
Kristian Høgsberge329f362013-05-06 22:19:57 -0400213static void
214default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
215 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400216{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400217 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100218 struct wl_resource *resource;
219 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400220
Neil Roberts96d790e2013-09-19 17:32:00 +0100221 resource_list = &touch->focus_resource_list;
222
223 wl_resource_for_each(resource, resource_list) {
224 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400225 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400226 }
227}
228
Kristian Høgsberge329f362013-05-06 22:19:57 -0400229static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400230 default_grab_touch_down,
231 default_grab_touch_up,
232 default_grab_touch_motion
233};
234
235static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400236default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400237 uint32_t time, uint32_t key, uint32_t state)
238{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400239 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400240 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100241 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400242 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100243 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400244
Neil Roberts96d790e2013-09-19 17:32:00 +0100245 resource_list = &keyboard->focus_resource_list;
246 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100248 wl_resource_for_each(resource, resource_list)
249 wl_keyboard_send_key(resource,
250 serial,
251 time,
252 key,
253 state);
254 }
255}
256
257static void
258send_modifiers_to_resource(struct weston_keyboard *keyboard,
259 struct wl_resource *resource,
260 uint32_t serial)
261{
262 wl_keyboard_send_modifiers(resource,
263 serial,
264 keyboard->modifiers.mods_depressed,
265 keyboard->modifiers.mods_latched,
266 keyboard->modifiers.mods_locked,
267 keyboard->modifiers.group);
268}
269
270static void
271send_modifiers_to_client_in_list(struct wl_client *client,
272 struct wl_list *list,
273 uint32_t serial,
274 struct weston_keyboard *keyboard)
275{
276 struct wl_resource *resource;
277
278 wl_resource_for_each(resource, list) {
279 if (wl_resource_get_client(resource) == client)
280 send_modifiers_to_resource(keyboard,
281 resource,
282 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400283 }
284}
285
286static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400287find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400288{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400289 if (!surface)
290 return NULL;
291
Jason Ekstrand44a38632013-06-14 10:08:00 -0500292 if (!surface->resource)
293 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100294
Jason Ekstrand44a38632013-06-14 10:08:00 -0500295 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400296}
297
298static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400299default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300 uint32_t mods_depressed, uint32_t mods_latched,
301 uint32_t mods_locked, uint32_t group)
302{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400303 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100304 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
305 struct wl_resource *resource;
306 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307
Neil Roberts96d790e2013-09-19 17:32:00 +0100308 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400309
Neil Roberts96d790e2013-09-19 17:32:00 +0100310 wl_resource_for_each(resource, resource_list) {
311 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
312 mods_latched, mods_locked, group);
313 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100315 struct wl_client *pointer_client =
316 wl_resource_get_client(pointer->focus->resource);
317 send_modifiers_to_client_in_list(pointer_client,
318 &keyboard->resource_list,
319 serial,
320 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 }
322}
323
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400324static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 default_keyboard_grab_interface = {
326 default_grab_key,
327 default_grab_modifiers,
328};
329
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400330static void
331pointer_unmap_sprite(struct weston_pointer *pointer)
332{
333 if (weston_surface_is_mapped(pointer->sprite))
334 weston_surface_unmap(pointer->sprite);
335
336 wl_list_remove(&pointer->sprite_destroy_listener.link);
337 pointer->sprite->configure = NULL;
338 pointer->sprite->configure_private = NULL;
339 pointer->sprite = NULL;
340}
341
342static void
343pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
344{
345 struct weston_pointer *pointer =
346 container_of(listener, struct weston_pointer,
347 sprite_destroy_listener);
348
349 pointer->sprite = NULL;
350}
351
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400352WL_EXPORT struct weston_pointer *
353weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400355 struct weston_pointer *pointer;
356
Peter Huttererf3d62272013-08-08 11:57:05 +1000357 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400358 if (pointer == NULL)
359 return NULL;
360
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400361 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100362 wl_list_init(&pointer->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363 pointer->default_grab.interface = &default_pointer_grab_interface;
364 pointer->default_grab.pointer = pointer;
365 pointer->grab = &pointer->default_grab;
366 wl_signal_init(&pointer->focus_signal);
367
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400368 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
369
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400370 /* FIXME: Pick better co-ords. */
371 pointer->x = wl_fixed_from_int(100);
372 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400373
374 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400375}
376
377WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400378weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400380 if (pointer->sprite)
381 pointer_unmap_sprite(pointer);
382
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400383 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100384
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400385 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400386}
387
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400388WL_EXPORT struct weston_keyboard *
389weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400390{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400391 struct weston_keyboard *keyboard;
392
Peter Huttererf3d62272013-08-08 11:57:05 +1000393 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400394 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100395 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400396
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100398 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400 keyboard->default_grab.interface = &default_keyboard_grab_interface;
401 keyboard->default_grab.keyboard = keyboard;
402 keyboard->grab = &keyboard->default_grab;
403 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400404
405 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406}
407
408WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400409weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410{
411 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100412
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400413 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400414 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400417WL_EXPORT struct weston_touch *
418weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400419{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400420 struct weston_touch *touch;
421
Peter Huttererf3d62272013-08-08 11:57:05 +1000422 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400423 if (touch == NULL)
424 return NULL;
425
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100427 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428 touch->default_grab.interface = &default_touch_grab_interface;
429 touch->default_grab.touch = touch;
430 touch->grab = &touch->default_grab;
431 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400432
433 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434}
435
436WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400437weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400438{
439 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100440
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400441 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442}
443
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400445seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400446{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100448 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400449
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200450 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200452 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400453 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200454 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455 caps |= WL_SEAT_CAPABILITY_TOUCH;
456
Rob Bradford6e737f52013-09-06 17:48:19 +0100457 wl_resource_for_each(resource, &seat->base_resource_list) {
458 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500459 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460}
461
462WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400463weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400464 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400465 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400466{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400467 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100468 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100469 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100471 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472
Neil Roberts96d790e2013-09-19 17:32:00 +0100473 focus_resource_list = &pointer->focus_resource_list;
474
475 if (!wl_list_empty(focus_resource_list) && pointer->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100477 wl_resource_for_each(resource, focus_resource_list) {
478 wl_pointer_send_leave(resource, serial,
479 pointer->focus->resource);
480 }
481
482 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483 }
484
Neil Roberts96d790e2013-09-19 17:32:00 +0100485 if (find_resource_for_surface(&pointer->resource_list, surface) &&
486 pointer->focus != surface) {
487 struct wl_client *surface_client =
488 wl_resource_get_client(surface->resource);
489
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400490 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100491
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700492 if (kbd && kbd->focus != pointer->focus)
493 send_modifiers_to_client_in_list(surface_client,
494 &kbd->resource_list,
495 serial,
496 kbd);
497
Neil Roberts96d790e2013-09-19 17:32:00 +0100498 move_resources_for_client(focus_resource_list,
499 &pointer->resource_list,
500 surface_client);
501
502 wl_resource_for_each(resource, focus_resource_list) {
503 wl_pointer_send_enter(resource,
504 serial,
505 surface->resource,
506 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400507 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100508
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400509 pointer->focus_serial = serial;
510 }
511
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400512 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400513 wl_signal_emit(&pointer->focus_signal, pointer);
514}
515
Neil Roberts96d790e2013-09-19 17:32:00 +0100516static void
517send_enter_to_resource_list(struct wl_list *list,
518 struct weston_keyboard *keyboard,
519 struct weston_surface *surface,
520 uint32_t serial)
521{
522 struct wl_resource *resource;
523
524 wl_resource_for_each(resource, list) {
525 send_modifiers_to_resource(keyboard, resource, serial);
526 wl_keyboard_send_enter(resource, serial,
527 surface->resource,
528 &keyboard->keys);
529 }
530}
531
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400533weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400534 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400535{
536 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100537 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100539 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540
Neil Roberts96d790e2013-09-19 17:32:00 +0100541 focus_resource_list = &keyboard->focus_resource_list;
542
543 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100545 wl_resource_for_each(resource, focus_resource_list) {
546 wl_keyboard_send_leave(resource, serial,
547 keyboard->focus->resource);
548 }
549 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550 }
551
Neil Roberts96d790e2013-09-19 17:32:00 +0100552 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
553 keyboard->focus != surface) {
554 struct wl_client *surface_client =
555 wl_resource_get_client(surface->resource);
556
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100558
559 move_resources_for_client(focus_resource_list,
560 &keyboard->resource_list,
561 surface_client);
562 send_enter_to_resource_list(focus_resource_list,
563 keyboard,
564 surface,
565 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400566 keyboard->focus_serial = serial;
567 }
568
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569 keyboard->focus = surface;
570 wl_signal_emit(&keyboard->focus_signal, keyboard);
571}
572
573WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400574weston_keyboard_start_grab(struct weston_keyboard *keyboard,
575 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400576{
577 keyboard->grab = grab;
578 grab->keyboard = keyboard;
579
580 /* XXX focus? */
581}
582
583WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400584weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585{
586 keyboard->grab = &keyboard->default_grab;
587}
588
589WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400590weston_pointer_start_grab(struct weston_pointer *pointer,
591 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400592{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400593 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400595 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400596}
597
598WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400599weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400601 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400602 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400603}
604
605WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400606weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607{
608 touch->grab = grab;
609 grab->touch = touch;
610}
611
612WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400613weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614{
615 touch->grab = &touch->default_grab;
616}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400617
Rob Bradford806d8c02013-06-25 18:56:41 +0100618WL_EXPORT void
619weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400620{
Rob Bradford806d8c02013-06-25 18:56:41 +0100621 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400622 struct weston_output *output, *prev = NULL;
623 int x, y, old_x, old_y, valid = 0;
624
625 x = wl_fixed_to_int(*fx);
626 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100627 old_x = wl_fixed_to_int(pointer->x);
628 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400629
630 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100631 if (pointer->seat->output && pointer->seat->output != output)
632 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400633 if (pixman_region32_contains_point(&output->region,
634 x, y, NULL))
635 valid = 1;
636 if (pixman_region32_contains_point(&output->region,
637 old_x, old_y, NULL))
638 prev = output;
639 }
640
Rob Bradford66bd9f52013-06-25 18:56:42 +0100641 if (!prev)
642 prev = pointer->seat->output;
643
644 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400645 if (x < prev->x)
646 *fx = wl_fixed_from_int(prev->x);
647 else if (x >= prev->x + prev->width)
648 *fx = wl_fixed_from_int(prev->x +
649 prev->width - 1);
650 if (y < prev->y)
651 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200652 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400653 *fy = wl_fixed_from_int(prev->y +
654 prev->height - 1);
655 }
656}
657
658/* Takes absolute values */
659static void
660move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
661{
662 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400663 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400664 struct weston_output *output;
665 int32_t ix, iy;
666
Rob Bradford806d8c02013-06-25 18:56:41 +0100667 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400668
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400669 pointer->x = x;
670 pointer->y = y;
671
672 ix = wl_fixed_to_int(x);
673 iy = wl_fixed_to_int(y);
674
675 wl_list_for_each(output, &ec->output_list, link)
676 if (output->zoom.active &&
677 pixman_region32_contains_point(&output->region,
678 ix, iy, NULL))
679 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
680
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400681 if (pointer->sprite) {
682 weston_surface_set_position(pointer->sprite,
683 ix - pointer->hotspot_x,
684 iy - pointer->hotspot_y);
685 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400686 }
687}
688
689WL_EXPORT void
690notify_motion(struct weston_seat *seat,
691 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
692{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400693 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400694 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400695
696 weston_compositor_wake(ec);
697
698 move_pointer(seat, pointer->x + dx, pointer->y + dy);
699
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400700 pointer->grab->interface->focus(pointer->grab);
701 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400702}
703
704WL_EXPORT void
705notify_motion_absolute(struct weston_seat *seat,
706 uint32_t time, wl_fixed_t x, wl_fixed_t y)
707{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400708 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400709 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400710
711 weston_compositor_wake(ec);
712
713 move_pointer(seat, x, y);
714
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400715 pointer->grab->interface->focus(pointer->grab);
716 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400717}
718
719WL_EXPORT void
720weston_surface_activate(struct weston_surface *surface,
721 struct weston_seat *seat)
722{
723 struct weston_compositor *compositor = seat->compositor;
724
Kristian Høgsberge3148752013-05-06 23:19:49 -0400725 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400726 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400727 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400728 }
729
730 wl_signal_emit(&compositor->activate_signal, surface);
731}
732
733WL_EXPORT void
734notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
735 enum wl_pointer_button_state state)
736{
737 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400738 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400739 struct weston_surface *focus =
740 (struct weston_surface *) pointer->focus;
741 uint32_t serial = wl_display_next_serial(compositor->wl_display);
742
743 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
744 if (compositor->ping_handler && focus)
745 compositor->ping_handler(focus, serial);
746 weston_compositor_idle_inhibit(compositor);
747 if (pointer->button_count == 0) {
748 pointer->grab_button = button;
749 pointer->grab_time = time;
750 pointer->grab_x = pointer->x;
751 pointer->grab_y = pointer->y;
752 }
753 pointer->button_count++;
754 } else {
755 weston_compositor_idle_release(compositor);
756 pointer->button_count--;
757 }
758
759 weston_compositor_run_button_binding(compositor, seat, time, button,
760 state);
761
762 pointer->grab->interface->button(pointer->grab, time, button, state);
763
764 if (pointer->button_count == 1)
765 pointer->grab_serial =
766 wl_display_get_serial(compositor->wl_display);
767}
768
769WL_EXPORT void
770notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
771 wl_fixed_t value)
772{
773 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400774 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400775 struct weston_surface *focus =
776 (struct weston_surface *) pointer->focus;
777 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100778 struct wl_resource *resource;
779 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400780
781 if (compositor->ping_handler && focus)
782 compositor->ping_handler(focus, serial);
783
784 weston_compositor_wake(compositor);
785
786 if (!value)
787 return;
788
789 if (weston_compositor_run_axis_binding(compositor, seat,
790 time, axis, value))
791 return;
792
Neil Roberts96d790e2013-09-19 17:32:00 +0100793 resource_list = &pointer->focus_resource_list;
794 wl_resource_for_each(resource, resource_list)
795 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400796 value);
797}
798
Rob Bradford382ff462013-06-24 16:52:45 +0100799#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400800WL_EXPORT void
801notify_modifiers(struct weston_seat *seat, uint32_t serial)
802{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400803 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400804 struct weston_keyboard_grab *grab = keyboard->grab;
805 uint32_t mods_depressed, mods_latched, mods_locked, group;
806 uint32_t mods_lookup;
807 enum weston_led leds = 0;
808 int changed = 0;
809
810 /* Serialize and update our internal state, checking to see if it's
811 * different to the previous state. */
812 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
813 XKB_STATE_DEPRESSED);
814 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
815 XKB_STATE_LATCHED);
816 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
817 XKB_STATE_LOCKED);
818 group = xkb_state_serialize_group(seat->xkb_state.state,
819 XKB_STATE_EFFECTIVE);
820
Kristian Høgsberge3148752013-05-06 23:19:49 -0400821 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
822 mods_latched != seat->keyboard->modifiers.mods_latched ||
823 mods_locked != seat->keyboard->modifiers.mods_locked ||
824 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400825 changed = 1;
826
Kristian Høgsberge3148752013-05-06 23:19:49 -0400827 seat->keyboard->modifiers.mods_depressed = mods_depressed;
828 seat->keyboard->modifiers.mods_latched = mods_latched;
829 seat->keyboard->modifiers.mods_locked = mods_locked;
830 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400831
832 /* And update the modifier_state for bindings. */
833 mods_lookup = mods_depressed | mods_latched;
834 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000835 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400836 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000837 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400838 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000839 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400840 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000841 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400842 seat->modifier_state |= MODIFIER_SHIFT;
843
844 /* Finally, notify the compositor that LEDs have changed. */
845 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000846 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400847 leds |= LED_NUM_LOCK;
848 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000849 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400850 leds |= LED_CAPS_LOCK;
851 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000852 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400853 leds |= LED_SCROLL_LOCK;
854 if (leds != seat->xkb_state.leds && seat->led_update)
855 seat->led_update(seat, leds);
856 seat->xkb_state.leds = leds;
857
858 if (changed) {
859 grab->interface->modifiers(grab,
860 serial,
861 keyboard->modifiers.mods_depressed,
862 keyboard->modifiers.mods_latched,
863 keyboard->modifiers.mods_locked,
864 keyboard->modifiers.group);
865 }
866}
867
868static void
869update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
870 enum wl_keyboard_key_state state)
871{
872 enum xkb_key_direction direction;
873
Matt Roper01a92732013-06-24 16:52:44 +0100874 /* Keyboard modifiers don't exist in raw keyboard mode */
875 if (!seat->compositor->use_xkbcommon)
876 return;
877
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400878 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
879 direction = XKB_KEY_DOWN;
880 else
881 direction = XKB_KEY_UP;
882
883 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
884 * broken keycode system, which starts at 8. */
885 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
886
887 notify_modifiers(seat, serial);
888}
Rob Bradford382ff462013-06-24 16:52:45 +0100889#else
890WL_EXPORT void
891notify_modifiers(struct weston_seat *seat, uint32_t serial)
892{
893}
894
895static void
896update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
897 enum wl_keyboard_key_state state)
898{
899}
900#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400901
902WL_EXPORT void
903notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
904 enum wl_keyboard_key_state state,
905 enum weston_key_state_update update_state)
906{
907 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400908 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400909 struct weston_surface *focus =
910 (struct weston_surface *) keyboard->focus;
911 struct weston_keyboard_grab *grab = keyboard->grab;
912 uint32_t serial = wl_display_next_serial(compositor->wl_display);
913 uint32_t *k, *end;
914
915 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
916 if (compositor->ping_handler && focus)
917 compositor->ping_handler(focus, serial);
918
919 weston_compositor_idle_inhibit(compositor);
920 keyboard->grab_key = key;
921 keyboard->grab_time = time;
922 } else {
923 weston_compositor_idle_release(compositor);
924 }
925
926 end = keyboard->keys.data + keyboard->keys.size;
927 for (k = keyboard->keys.data; k < end; k++) {
928 if (*k == key) {
929 /* Ignore server-generated repeats. */
930 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
931 return;
932 *k = *--end;
933 }
934 }
935 keyboard->keys.size = (void *) end - keyboard->keys.data;
936 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
937 k = wl_array_add(&keyboard->keys, sizeof *k);
938 *k = key;
939 }
940
941 if (grab == &keyboard->default_grab ||
942 grab == &keyboard->input_method_grab) {
943 weston_compositor_run_key_binding(compositor, seat, time, key,
944 state);
945 grab = keyboard->grab;
946 }
947
948 grab->interface->key(grab, time, key, state);
949
950 if (update_state == STATE_UPDATE_AUTOMATIC) {
951 update_modifier_state(seat,
952 wl_display_get_serial(compositor->wl_display),
953 key,
954 state);
955 }
956}
957
958WL_EXPORT void
959notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
960 wl_fixed_t x, wl_fixed_t y)
961{
962 struct weston_compositor *compositor = seat->compositor;
963
964 if (output) {
965 move_pointer(seat, x, y);
966 compositor->focus = 1;
967 } else {
968 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400969 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400970 * NULL) here, but somehow that breaks re-entry... */
971 }
972}
973
974static void
975destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
976{
977 struct weston_seat *ws;
978
979 ws = container_of(listener, struct weston_seat,
980 saved_kbd_focus_listener);
981
982 ws->saved_kbd_focus = NULL;
983}
984
985WL_EXPORT void
986notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
987 enum weston_key_state_update update_state)
988{
989 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400990 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400991 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400992 uint32_t *k, serial;
993
994 serial = wl_display_next_serial(compositor->wl_display);
995 wl_array_copy(&keyboard->keys, keys);
996 wl_array_for_each(k, &keyboard->keys) {
997 weston_compositor_idle_inhibit(compositor);
998 if (update_state == STATE_UPDATE_AUTOMATIC)
999 update_modifier_state(seat, serial, *k,
1000 WL_KEYBOARD_KEY_STATE_PRESSED);
1001 }
1002
1003 /* Run key bindings after we've updated the state. */
1004 wl_array_for_each(k, &keyboard->keys) {
1005 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1006 WL_KEYBOARD_KEY_STATE_PRESSED);
1007 }
1008
1009 surface = seat->saved_kbd_focus;
1010
1011 if (surface) {
1012 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1013 weston_keyboard_set_focus(keyboard, surface);
1014 seat->saved_kbd_focus = NULL;
1015 }
1016}
1017
1018WL_EXPORT void
1019notify_keyboard_focus_out(struct weston_seat *seat)
1020{
1021 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001022 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001023 uint32_t *k, serial;
1024
1025 serial = wl_display_next_serial(compositor->wl_display);
1026 wl_array_for_each(k, &keyboard->keys) {
1027 weston_compositor_idle_release(compositor);
1028 update_modifier_state(seat, serial, *k,
1029 WL_KEYBOARD_KEY_STATE_RELEASED);
1030 }
1031
1032 seat->modifier_state = 0;
1033
1034 if (keyboard->focus) {
1035 seat->saved_kbd_focus = keyboard->focus;
1036 seat->saved_kbd_focus_listener.notify =
1037 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001038 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001039 &seat->saved_kbd_focus_listener);
1040 }
1041
1042 weston_keyboard_set_focus(keyboard, NULL);
1043 /* FIXME: We really need keyboard grab cancel here to
1044 * let the grab shut down properly. As it is we leak
1045 * the grab data. */
1046 weston_keyboard_end_grab(keyboard);
1047}
1048
Michael Fua2bb7912013-07-23 15:51:06 +08001049WL_EXPORT void
1050weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001051{
Neil Roberts96d790e2013-09-19 17:32:00 +01001052 struct wl_list *focus_resource_list;
1053
1054 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001055
1056 if (seat->touch->focus == surface)
1057 return;
1058
Neil Roberts96d790e2013-09-19 17:32:00 +01001059 if (!wl_list_empty(focus_resource_list)) {
1060 move_resources(&seat->touch->resource_list,
1061 focus_resource_list);
1062 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001063
1064 if (surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001065 struct wl_client *surface_client =
1066 wl_resource_get_client(surface->resource);
1067 move_resources_for_client(focus_resource_list,
1068 &seat->touch->resource_list,
1069 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001070 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001071 seat->touch->focus = surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001072}
1073
1074/**
1075 * notify_touch - emulates button touches and notifies surfaces accordingly.
1076 *
1077 * It assumes always the correct cycle sequence until it gets here: touch_down
1078 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1079 * for sending along such order.
1080 *
1081 */
1082WL_EXPORT void
1083notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1084 wl_fixed_t x, wl_fixed_t y, int touch_type)
1085{
1086 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001087 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001088 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001089 struct weston_surface *es;
1090 wl_fixed_t sx, sy;
1091
1092 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001093 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1094 touch->grab_x = x;
1095 touch->grab_y = y;
1096 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001097
1098 switch (touch_type) {
1099 case WL_TOUCH_DOWN:
1100 weston_compositor_idle_inhibit(ec);
1101
1102 seat->num_tp++;
1103
1104 /* the first finger down picks the surface, and all further go
1105 * to that surface for the remainder of the touch session i.e.
1106 * until all touch points are up again. */
1107 if (seat->num_tp == 1) {
1108 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001109 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001110 } else if (touch->focus) {
1111 es = (struct weston_surface *) touch->focus;
1112 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1113 } else {
1114 /* Unexpected condition: We have non-initial touch but
1115 * there is no focused surface.
1116 */
1117 weston_log("touch event received with %d points down"
1118 "but no surface focused\n", seat->num_tp);
1119 return;
1120 }
1121
1122 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001123 if (seat->num_tp == 1) {
1124 touch->grab_serial =
1125 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001126 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001127 touch->grab_time = time;
1128 touch->grab_x = x;
1129 touch->grab_y = y;
1130 }
1131
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001132 break;
1133 case WL_TOUCH_MOTION:
1134 es = (struct weston_surface *) touch->focus;
1135 if (!es)
1136 break;
1137
1138 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1139 grab->interface->motion(grab, time, touch_id, sx, sy);
1140 break;
1141 case WL_TOUCH_UP:
1142 weston_compositor_idle_release(ec);
1143 seat->num_tp--;
1144
1145 grab->interface->up(grab, time, touch_id);
1146 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001147 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001148 break;
1149 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001150
1151 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001152}
1153
1154static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001155pointer_cursor_surface_configure(struct weston_surface *es,
1156 int32_t dx, int32_t dy, int32_t width, int32_t height)
1157{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001158 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001159 int x, y;
1160
1161 if (width == 0)
1162 return;
1163
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001164 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001165
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001166 pointer->hotspot_x -= dx;
1167 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001169 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1170 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001171
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001172 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001173
1174 empty_region(&es->pending.input);
1175
1176 if (!weston_surface_is_mapped(es)) {
1177 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1178 &es->layer_link);
1179 weston_surface_update_transform(es);
1180 }
1181}
1182
1183static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001184pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1185 uint32_t serial, struct wl_resource *surface_resource,
1186 int32_t x, int32_t y)
1187{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001188 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001189 struct weston_surface *surface = NULL;
1190
1191 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001192 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001194 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001195 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001196 /* pointer->focus->resource can be NULL. Surfaces like the
1197 black_surface used in shell.c for fullscreen don't have
1198 a resource, but can still have focus */
1199 if (pointer->focus->resource == NULL)
1200 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001201 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001202 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001203 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 return;
1205
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001206 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001208 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 WL_DISPLAY_ERROR_INVALID_OBJECT,
1210 "surface->configure already "
1211 "set");
1212 return;
1213 }
1214 }
1215
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001216 if (pointer->sprite)
1217 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001218
1219 if (!surface)
1220 return;
1221
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001222 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001223 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001224
1225 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001226 surface->configure_private = pointer;
1227 pointer->sprite = surface;
1228 pointer->hotspot_x = x;
1229 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001230
1231 if (surface->buffer_ref.buffer)
1232 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1233 weston_surface_buffer_height(surface));
1234}
1235
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001236static void
1237pointer_release(struct wl_client *client, struct wl_resource *resource)
1238{
1239 wl_resource_destroy(resource);
1240}
1241
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001243 pointer_set_cursor,
1244 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245};
1246
1247static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001248seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1249 uint32_t id)
1250{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001251 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001252 struct wl_resource *cr;
1253
Kristian Høgsberge3148752013-05-06 23:19:49 -04001254 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001255 return;
1256
Jason Ekstranda85118c2013-06-27 20:17:02 -05001257 cr = wl_resource_create(client, &wl_pointer_interface,
1258 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001259 if (cr == NULL) {
1260 wl_client_post_no_memory(client);
1261 return;
1262 }
1263
Neil Roberts96d790e2013-09-19 17:32:00 +01001264 /* May be moved to focused list later by either
1265 * weston_pointer_set_focus or directly if this client is already
1266 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001267 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001268 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1269 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001270
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001271 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001272 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001273 struct weston_surface *surface;
1274 wl_fixed_t sx, sy;
1275
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001277 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001278 seat->pointer->x,
1279 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001280 &sx,
1281 &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001282
1283 wl_list_remove(wl_resource_get_link(cr));
1284 wl_list_insert(&seat->pointer->focus_resource_list,
1285 wl_resource_get_link(cr));
1286 wl_pointer_send_enter(cr,
1287 seat->pointer->focus_serial,
1288 surface->resource,
1289 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001290 }
1291}
1292
1293static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001294keyboard_release(struct wl_client *client, struct wl_resource *resource)
1295{
1296 wl_resource_destroy(resource);
1297}
1298
1299static const struct wl_keyboard_interface keyboard_interface = {
1300 keyboard_release
1301};
1302
Neil Roberts96d790e2013-09-19 17:32:00 +01001303static int
1304should_send_modifiers_to_client(struct weston_seat *seat,
1305 struct wl_client *client)
1306{
1307 if (seat->keyboard &&
1308 seat->keyboard->focus &&
1309 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1310 return 1;
1311
1312 if (seat->pointer &&
1313 seat->pointer->focus &&
1314 wl_resource_get_client(seat->pointer->focus->resource) == client)
1315 return 1;
1316
1317 return 0;
1318}
1319
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001320static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001321seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1322 uint32_t id)
1323{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001324 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001325 struct wl_resource *cr;
1326
Kristian Høgsberge3148752013-05-06 23:19:49 -04001327 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328 return;
1329
Jason Ekstranda85118c2013-06-27 20:17:02 -05001330 cr = wl_resource_create(client, &wl_keyboard_interface,
1331 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001332 if (cr == NULL) {
1333 wl_client_post_no_memory(client);
1334 return;
1335 }
1336
Neil Roberts96d790e2013-09-19 17:32:00 +01001337 /* May be moved to focused list later by either
1338 * weston_keyboard_set_focus or directly if this client is already
1339 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001340 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001341 wl_resource_set_implementation(cr, &keyboard_interface,
1342 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001343
Matt Roper01a92732013-06-24 16:52:44 +01001344 if (seat->compositor->use_xkbcommon) {
1345 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001346 seat->xkb_info->keymap_fd,
1347 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001348 } else {
1349 int null_fd = open("/dev/null", O_RDONLY);
1350 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1351 null_fd,
1352 0);
1353 close(null_fd);
1354 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001355
Neil Roberts96d790e2013-09-19 17:32:00 +01001356 if (should_send_modifiers_to_client(seat, client)) {
1357 send_modifiers_to_resource(seat->keyboard,
1358 cr,
1359 seat->keyboard->focus_serial);
1360 }
1361
Kristian Høgsberge3148752013-05-06 23:19:49 -04001362 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001363 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001364 struct weston_surface *surface =
1365 (struct weston_surface *) seat->keyboard->focus;
1366
1367 wl_list_remove(wl_resource_get_link(cr));
1368 wl_list_insert(&seat->keyboard->focus_resource_list,
1369 wl_resource_get_link(cr));
1370 wl_keyboard_send_enter(cr,
1371 seat->keyboard->focus_serial,
1372 surface->resource,
1373 &seat->keyboard->keys);
1374
1375 /* If this is the first keyboard resource for this
1376 * client... */
1377 if (seat->keyboard->focus_resource_list.prev ==
1378 wl_resource_get_link(cr))
1379 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001380 }
1381}
1382
1383static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001384touch_release(struct wl_client *client, struct wl_resource *resource)
1385{
1386 wl_resource_destroy(resource);
1387}
1388
1389static const struct wl_touch_interface touch_interface = {
1390 touch_release
1391};
1392
1393static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001394seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1395 uint32_t id)
1396{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001397 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001398 struct wl_resource *cr;
1399
Kristian Høgsberge3148752013-05-06 23:19:49 -04001400 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001401 return;
1402
Jason Ekstranda85118c2013-06-27 20:17:02 -05001403 cr = wl_resource_create(client, &wl_touch_interface,
1404 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001405 if (cr == NULL) {
1406 wl_client_post_no_memory(client);
1407 return;
1408 }
1409
Neil Roberts96d790e2013-09-19 17:32:00 +01001410 if (seat->touch->focus &&
1411 wl_resource_get_client(seat->touch->focus->resource) == client) {
1412 wl_list_insert(&seat->touch->resource_list,
1413 wl_resource_get_link(cr));
1414 } else {
1415 wl_list_insert(&seat->touch->focus_resource_list,
1416 wl_resource_get_link(cr));
1417 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001418 wl_resource_set_implementation(cr, &touch_interface,
1419 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001420}
1421
1422static const struct wl_seat_interface seat_interface = {
1423 seat_get_pointer,
1424 seat_get_keyboard,
1425 seat_get_touch,
1426};
1427
1428static void
1429bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1430{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001431 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001432 struct wl_resource *resource;
1433 enum wl_seat_capability caps = 0;
1434
Jason Ekstranda85118c2013-06-27 20:17:02 -05001435 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001436 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001437 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001438 wl_resource_set_implementation(resource, &seat_interface, data,
1439 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001440
1441 if (seat->pointer)
1442 caps |= WL_SEAT_CAPABILITY_POINTER;
1443 if (seat->keyboard)
1444 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1445 if (seat->touch)
1446 caps |= WL_SEAT_CAPABILITY_TOUCH;
1447
1448 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001449 if (version >= 2)
1450 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001451}
1452
Rob Bradford382ff462013-06-24 16:52:45 +01001453#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001454int
1455weston_compositor_xkb_init(struct weston_compositor *ec,
1456 struct xkb_rule_names *names)
1457{
Rob Bradford382ff462013-06-24 16:52:45 +01001458 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001459
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001460 if (ec->xkb_context == NULL) {
1461 ec->xkb_context = xkb_context_new(0);
1462 if (ec->xkb_context == NULL) {
1463 weston_log("failed to create XKB context\n");
1464 return -1;
1465 }
1466 }
1467
1468 if (names)
1469 ec->xkb_names = *names;
1470 if (!ec->xkb_names.rules)
1471 ec->xkb_names.rules = strdup("evdev");
1472 if (!ec->xkb_names.model)
1473 ec->xkb_names.model = strdup("pc105");
1474 if (!ec->xkb_names.layout)
1475 ec->xkb_names.layout = strdup("us");
1476
1477 return 0;
1478}
1479
Stefan Schmidtfda26522013-09-17 10:54:09 +01001480static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001481weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001482{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001483 if (--xkb_info->ref_count > 0)
1484 return;
1485
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001486 if (xkb_info->keymap)
1487 xkb_map_unref(xkb_info->keymap);
1488
1489 if (xkb_info->keymap_area)
1490 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1491 if (xkb_info->keymap_fd >= 0)
1492 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001493 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001494}
1495
1496void
1497weston_compositor_xkb_destroy(struct weston_compositor *ec)
1498{
Matt Roper01a92732013-06-24 16:52:44 +01001499 /*
1500 * If we're operating in raw keyboard mode, we never initialized
1501 * libxkbcommon so there's no cleanup to do either.
1502 */
1503 if (!ec->use_xkbcommon)
1504 return;
1505
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001506 free((char *) ec->xkb_names.rules);
1507 free((char *) ec->xkb_names.model);
1508 free((char *) ec->xkb_names.layout);
1509 free((char *) ec->xkb_names.variant);
1510 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001511
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001512 if (ec->xkb_info)
1513 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001514 xkb_context_unref(ec->xkb_context);
1515}
1516
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001517static struct weston_xkb_info *
1518weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001520 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1521 if (xkb_info == NULL)
1522 return NULL;
1523
1524 xkb_info->keymap = xkb_map_ref(keymap);
1525 xkb_info->ref_count = 1;
1526
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001527 char *keymap_str;
1528
1529 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1530 XKB_MOD_NAME_SHIFT);
1531 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1532 XKB_MOD_NAME_CAPS);
1533 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1534 XKB_MOD_NAME_CTRL);
1535 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1536 XKB_MOD_NAME_ALT);
1537 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1538 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1539 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1540 XKB_MOD_NAME_LOGO);
1541 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1542
1543 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1544 XKB_LED_NAME_NUM);
1545 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1546 XKB_LED_NAME_CAPS);
1547 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1548 XKB_LED_NAME_SCROLL);
1549
1550 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1551 if (keymap_str == NULL) {
1552 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001553 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001554 }
1555 xkb_info->keymap_size = strlen(keymap_str) + 1;
1556
1557 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1558 if (xkb_info->keymap_fd < 0) {
1559 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1560 (unsigned long) xkb_info->keymap_size);
1561 goto err_keymap_str;
1562 }
1563
1564 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1565 PROT_READ | PROT_WRITE,
1566 MAP_SHARED, xkb_info->keymap_fd, 0);
1567 if (xkb_info->keymap_area == MAP_FAILED) {
1568 weston_log("failed to mmap() %lu bytes\n",
1569 (unsigned long) xkb_info->keymap_size);
1570 goto err_dev_zero;
1571 }
1572 strcpy(xkb_info->keymap_area, keymap_str);
1573 free(keymap_str);
1574
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001575 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001576
1577err_dev_zero:
1578 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001579err_keymap_str:
1580 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001581err_keymap:
1582 xkb_map_unref(xkb_info->keymap);
1583 free(xkb_info);
1584 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001585}
1586
1587static int
1588weston_compositor_build_global_keymap(struct weston_compositor *ec)
1589{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001590 struct xkb_keymap *keymap;
1591
1592 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001593 return 0;
1594
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001595 keymap = xkb_map_new_from_names(ec->xkb_context,
1596 &ec->xkb_names,
1597 0);
1598 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001599 weston_log("failed to compile global XKB keymap\n");
1600 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1601 "options %s\n",
1602 ec->xkb_names.rules, ec->xkb_names.model,
1603 ec->xkb_names.layout, ec->xkb_names.variant,
1604 ec->xkb_names.options);
1605 return -1;
1606 }
1607
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001608 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001609 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001610 return -1;
1611
1612 return 0;
1613}
Rob Bradford382ff462013-06-24 16:52:45 +01001614#else
1615int
1616weston_compositor_xkb_init(struct weston_compositor *ec,
1617 struct xkb_rule_names *names)
1618{
1619 return 0;
1620}
1621
1622void
1623weston_compositor_xkb_destroy(struct weston_compositor *ec)
1624{
1625}
1626#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627
1628WL_EXPORT int
1629weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1630{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001631 struct weston_keyboard *keyboard;
1632
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001633 if (seat->keyboard) {
1634 seat->keyboard_device_count += 1;
1635 if (seat->keyboard_device_count == 1)
1636 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001637 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001638 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001639
Rob Bradford382ff462013-06-24 16:52:45 +01001640#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001641 if (seat->compositor->use_xkbcommon) {
1642 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001643 seat->xkb_info = weston_xkb_info_create(keymap);
1644 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001645 return -1;
1646 } else {
1647 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1648 return -1;
1649 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001650 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001651 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001652
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001653 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001654 if (seat->xkb_state.state == NULL) {
1655 weston_log("failed to initialise XKB state\n");
1656 return -1;
1657 }
1658
1659 seat->xkb_state.leds = 0;
1660 }
Rob Bradford382ff462013-06-24 16:52:45 +01001661#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001662
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001663 keyboard = weston_keyboard_create();
1664 if (keyboard == NULL) {
1665 weston_log("failed to allocate weston keyboard struct\n");
1666 return -1;
1667 }
1668
1669 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001670 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001671 keyboard->seat = seat;
1672
1673 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001674
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675 return 0;
1676}
1677
1678WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001679weston_seat_release_keyboard(struct weston_seat *seat)
1680{
1681 seat->keyboard_device_count--;
1682 if (seat->keyboard_device_count == 0) {
1683 seat_send_updated_caps(seat);
1684 }
1685}
1686
1687WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001688weston_seat_init_pointer(struct weston_seat *seat)
1689{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001690 struct weston_pointer *pointer;
1691
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001692 if (seat->pointer) {
1693 seat->pointer_device_count += 1;
1694 if (seat->pointer_device_count == 1)
1695 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001696 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001697 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001698
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001699 pointer = weston_pointer_create();
1700 if (pointer == NULL)
1701 return;
1702
1703 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001704 seat->pointer_device_count = 1;
1705 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001706
1707 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708}
1709
1710WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001711weston_seat_release_pointer(struct weston_seat *seat)
1712{
1713 struct weston_pointer *pointer = seat->pointer;
1714
1715 seat->pointer_device_count--;
1716 if (seat->pointer_device_count == 0) {
1717 seat_send_updated_caps(seat);
1718 }
1719}
1720
1721WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722weston_seat_init_touch(struct weston_seat *seat)
1723{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001724 struct weston_touch *touch;
1725
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001726 if (seat->touch) {
1727 seat->touch_device_count += 1;
1728 if (seat->touch_device_count == 1)
1729 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001730 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001731 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001732
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001733 touch = weston_touch_create();
1734 if (touch == NULL)
1735 return;
1736
1737 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001738 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001739 touch->seat = seat;
1740
1741 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001742}
1743
1744WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001745weston_seat_release_touch(struct weston_seat *seat)
1746{
1747 seat->touch_device_count--;
1748 if (seat->touch_device_count == 0) {
1749 seat_send_updated_caps(seat);
1750 }
1751}
1752
1753WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001754weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1755 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001756{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001757 memset(seat, 0, sizeof *seat);
1758
Kristian Høgsberge3148752013-05-06 23:19:49 -04001759 seat->selection_data_source = NULL;
1760 wl_list_init(&seat->base_resource_list);
1761 wl_signal_init(&seat->selection_signal);
1762 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001763 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001764
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001765 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001766 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001767
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001768 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001769 seat->modifier_state = 0;
1770 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001771 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001772
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001773 wl_list_insert(ec->seat_list.prev, &seat->link);
1774
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001775 clipboard_create(seat);
1776
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001777 wl_signal_emit(&ec->seat_created_signal, seat);
1778}
1779
1780WL_EXPORT void
1781weston_seat_release(struct weston_seat *seat)
1782{
1783 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001784
Rob Bradford382ff462013-06-24 16:52:45 +01001785#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001786 if (seat->compositor->use_xkbcommon) {
1787 if (seat->xkb_state.state != NULL)
1788 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001789 if (seat->xkb_info)
1790 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001791 }
Rob Bradford382ff462013-06-24 16:52:45 +01001792#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793
Kristian Høgsberge3148752013-05-06 23:19:49 -04001794 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001795 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001796 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001797 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001798 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001799 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001800
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001801 free (seat->seat_name);
1802
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001803 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001804
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001805 wl_signal_emit(&seat->destroy_signal, seat);
1806}