blob: 01ec059e3bea8cd91ed9fd30f9ecd5be9b4e3611 [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
450 if (seat->pointer)
451 caps |= WL_SEAT_CAPABILITY_POINTER;
452 if (seat->keyboard)
453 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
454 if (seat->touch)
455 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
492 move_resources_for_client(focus_resource_list,
493 &pointer->resource_list,
494 surface_client);
495
496 wl_resource_for_each(resource, focus_resource_list) {
497 wl_pointer_send_enter(resource,
498 serial,
499 surface->resource,
500 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400501 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100502
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400503 pointer->focus_serial = serial;
504 }
505
Neil Roberts96d790e2013-09-19 17:32:00 +0100506 if (kbd && surface && kbd->focus != pointer->focus) {
507 struct wl_client *surface_client =
508 wl_resource_get_client(surface->resource);
509 send_modifiers_to_client_in_list(surface_client,
510 &kbd->resource_list,
511 serial,
512 kbd);
513 }
514
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400516 wl_signal_emit(&pointer->focus_signal, pointer);
517}
518
Neil Roberts96d790e2013-09-19 17:32:00 +0100519static void
520send_enter_to_resource_list(struct wl_list *list,
521 struct weston_keyboard *keyboard,
522 struct weston_surface *surface,
523 uint32_t serial)
524{
525 struct wl_resource *resource;
526
527 wl_resource_for_each(resource, list) {
528 send_modifiers_to_resource(keyboard, resource, serial);
529 wl_keyboard_send_enter(resource, serial,
530 surface->resource,
531 &keyboard->keys);
532 }
533}
534
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400535WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400536weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400537 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538{
539 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100540 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100542 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543
Neil Roberts96d790e2013-09-19 17:32:00 +0100544 focus_resource_list = &keyboard->focus_resource_list;
545
546 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400547 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100548 wl_resource_for_each(resource, focus_resource_list) {
549 wl_keyboard_send_leave(resource, serial,
550 keyboard->focus->resource);
551 }
552 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553 }
554
Neil Roberts96d790e2013-09-19 17:32:00 +0100555 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
556 keyboard->focus != surface) {
557 struct wl_client *surface_client =
558 wl_resource_get_client(surface->resource);
559
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400560 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100561
562 move_resources_for_client(focus_resource_list,
563 &keyboard->resource_list,
564 surface_client);
565 send_enter_to_resource_list(focus_resource_list,
566 keyboard,
567 surface,
568 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569 keyboard->focus_serial = serial;
570 }
571
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572 keyboard->focus = surface;
573 wl_signal_emit(&keyboard->focus_signal, keyboard);
574}
575
576WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400577weston_keyboard_start_grab(struct weston_keyboard *keyboard,
578 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400579{
580 keyboard->grab = grab;
581 grab->keyboard = keyboard;
582
583 /* XXX focus? */
584}
585
586WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400587weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588{
589 keyboard->grab = &keyboard->default_grab;
590}
591
592WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400593weston_pointer_start_grab(struct weston_pointer *pointer,
594 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400595{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400596 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400597 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400598 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400599}
600
601WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400602weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400603{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400604 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400605 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400606}
607
608WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400609weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400610{
611 touch->grab = grab;
612 grab->touch = touch;
613}
614
615WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400616weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400617{
618 touch->grab = &touch->default_grab;
619}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400620
Rob Bradford806d8c02013-06-25 18:56:41 +0100621WL_EXPORT void
622weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400623{
Rob Bradford806d8c02013-06-25 18:56:41 +0100624 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400625 struct weston_output *output, *prev = NULL;
626 int x, y, old_x, old_y, valid = 0;
627
628 x = wl_fixed_to_int(*fx);
629 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100630 old_x = wl_fixed_to_int(pointer->x);
631 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400632
633 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100634 if (pointer->seat->output && pointer->seat->output != output)
635 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400636 if (pixman_region32_contains_point(&output->region,
637 x, y, NULL))
638 valid = 1;
639 if (pixman_region32_contains_point(&output->region,
640 old_x, old_y, NULL))
641 prev = output;
642 }
643
Rob Bradford66bd9f52013-06-25 18:56:42 +0100644 if (!prev)
645 prev = pointer->seat->output;
646
647 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648 if (x < prev->x)
649 *fx = wl_fixed_from_int(prev->x);
650 else if (x >= prev->x + prev->width)
651 *fx = wl_fixed_from_int(prev->x +
652 prev->width - 1);
653 if (y < prev->y)
654 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200655 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400656 *fy = wl_fixed_from_int(prev->y +
657 prev->height - 1);
658 }
659}
660
661/* Takes absolute values */
662static void
663move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
664{
665 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400666 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400667 struct weston_output *output;
668 int32_t ix, iy;
669
Rob Bradford806d8c02013-06-25 18:56:41 +0100670 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400671
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400672 pointer->x = x;
673 pointer->y = y;
674
675 ix = wl_fixed_to_int(x);
676 iy = wl_fixed_to_int(y);
677
678 wl_list_for_each(output, &ec->output_list, link)
679 if (output->zoom.active &&
680 pixman_region32_contains_point(&output->region,
681 ix, iy, NULL))
682 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
683
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400684 if (pointer->sprite) {
685 weston_surface_set_position(pointer->sprite,
686 ix - pointer->hotspot_x,
687 iy - pointer->hotspot_y);
688 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400689 }
690}
691
692WL_EXPORT void
693notify_motion(struct weston_seat *seat,
694 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
695{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400696 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400697 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400698
699 weston_compositor_wake(ec);
700
701 move_pointer(seat, pointer->x + dx, pointer->y + dy);
702
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400703 pointer->grab->interface->focus(pointer->grab);
704 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400705}
706
707WL_EXPORT void
708notify_motion_absolute(struct weston_seat *seat,
709 uint32_t time, wl_fixed_t x, wl_fixed_t y)
710{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400711 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400712 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400713
714 weston_compositor_wake(ec);
715
716 move_pointer(seat, x, y);
717
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400718 pointer->grab->interface->focus(pointer->grab);
719 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400720}
721
722WL_EXPORT void
723weston_surface_activate(struct weston_surface *surface,
724 struct weston_seat *seat)
725{
726 struct weston_compositor *compositor = seat->compositor;
727
Kristian Høgsberge3148752013-05-06 23:19:49 -0400728 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400729 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400730 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400731 }
732
733 wl_signal_emit(&compositor->activate_signal, surface);
734}
735
736WL_EXPORT void
737notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
738 enum wl_pointer_button_state state)
739{
740 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400741 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400742 struct weston_surface *focus =
743 (struct weston_surface *) pointer->focus;
744 uint32_t serial = wl_display_next_serial(compositor->wl_display);
745
746 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
747 if (compositor->ping_handler && focus)
748 compositor->ping_handler(focus, serial);
749 weston_compositor_idle_inhibit(compositor);
750 if (pointer->button_count == 0) {
751 pointer->grab_button = button;
752 pointer->grab_time = time;
753 pointer->grab_x = pointer->x;
754 pointer->grab_y = pointer->y;
755 }
756 pointer->button_count++;
757 } else {
758 weston_compositor_idle_release(compositor);
759 pointer->button_count--;
760 }
761
762 weston_compositor_run_button_binding(compositor, seat, time, button,
763 state);
764
765 pointer->grab->interface->button(pointer->grab, time, button, state);
766
767 if (pointer->button_count == 1)
768 pointer->grab_serial =
769 wl_display_get_serial(compositor->wl_display);
770}
771
772WL_EXPORT void
773notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
774 wl_fixed_t value)
775{
776 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400777 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400778 struct weston_surface *focus =
779 (struct weston_surface *) pointer->focus;
780 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100781 struct wl_resource *resource;
782 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400783
784 if (compositor->ping_handler && focus)
785 compositor->ping_handler(focus, serial);
786
787 weston_compositor_wake(compositor);
788
789 if (!value)
790 return;
791
792 if (weston_compositor_run_axis_binding(compositor, seat,
793 time, axis, value))
794 return;
795
Neil Roberts96d790e2013-09-19 17:32:00 +0100796 resource_list = &pointer->focus_resource_list;
797 wl_resource_for_each(resource, resource_list)
798 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400799 value);
800}
801
Rob Bradford382ff462013-06-24 16:52:45 +0100802#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400803WL_EXPORT void
804notify_modifiers(struct weston_seat *seat, uint32_t serial)
805{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400806 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400807 struct weston_keyboard_grab *grab = keyboard->grab;
808 uint32_t mods_depressed, mods_latched, mods_locked, group;
809 uint32_t mods_lookup;
810 enum weston_led leds = 0;
811 int changed = 0;
812
813 /* Serialize and update our internal state, checking to see if it's
814 * different to the previous state. */
815 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
816 XKB_STATE_DEPRESSED);
817 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
818 XKB_STATE_LATCHED);
819 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
820 XKB_STATE_LOCKED);
821 group = xkb_state_serialize_group(seat->xkb_state.state,
822 XKB_STATE_EFFECTIVE);
823
Kristian Høgsberge3148752013-05-06 23:19:49 -0400824 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
825 mods_latched != seat->keyboard->modifiers.mods_latched ||
826 mods_locked != seat->keyboard->modifiers.mods_locked ||
827 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400828 changed = 1;
829
Kristian Høgsberge3148752013-05-06 23:19:49 -0400830 seat->keyboard->modifiers.mods_depressed = mods_depressed;
831 seat->keyboard->modifiers.mods_latched = mods_latched;
832 seat->keyboard->modifiers.mods_locked = mods_locked;
833 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400834
835 /* And update the modifier_state for bindings. */
836 mods_lookup = mods_depressed | mods_latched;
837 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000838 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400839 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000840 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400841 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000842 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400843 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000844 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400845 seat->modifier_state |= MODIFIER_SHIFT;
846
847 /* Finally, notify the compositor that LEDs have changed. */
848 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000849 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400850 leds |= LED_NUM_LOCK;
851 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000852 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400853 leds |= LED_CAPS_LOCK;
854 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000855 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400856 leds |= LED_SCROLL_LOCK;
857 if (leds != seat->xkb_state.leds && seat->led_update)
858 seat->led_update(seat, leds);
859 seat->xkb_state.leds = leds;
860
861 if (changed) {
862 grab->interface->modifiers(grab,
863 serial,
864 keyboard->modifiers.mods_depressed,
865 keyboard->modifiers.mods_latched,
866 keyboard->modifiers.mods_locked,
867 keyboard->modifiers.group);
868 }
869}
870
871static void
872update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
873 enum wl_keyboard_key_state state)
874{
875 enum xkb_key_direction direction;
876
Matt Roper01a92732013-06-24 16:52:44 +0100877 /* Keyboard modifiers don't exist in raw keyboard mode */
878 if (!seat->compositor->use_xkbcommon)
879 return;
880
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400881 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
882 direction = XKB_KEY_DOWN;
883 else
884 direction = XKB_KEY_UP;
885
886 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
887 * broken keycode system, which starts at 8. */
888 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
889
890 notify_modifiers(seat, serial);
891}
Rob Bradford382ff462013-06-24 16:52:45 +0100892#else
893WL_EXPORT void
894notify_modifiers(struct weston_seat *seat, uint32_t serial)
895{
896}
897
898static void
899update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
900 enum wl_keyboard_key_state state)
901{
902}
903#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400904
905WL_EXPORT void
906notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
907 enum wl_keyboard_key_state state,
908 enum weston_key_state_update update_state)
909{
910 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400911 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400912 struct weston_surface *focus =
913 (struct weston_surface *) keyboard->focus;
914 struct weston_keyboard_grab *grab = keyboard->grab;
915 uint32_t serial = wl_display_next_serial(compositor->wl_display);
916 uint32_t *k, *end;
917
918 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
919 if (compositor->ping_handler && focus)
920 compositor->ping_handler(focus, serial);
921
922 weston_compositor_idle_inhibit(compositor);
923 keyboard->grab_key = key;
924 keyboard->grab_time = time;
925 } else {
926 weston_compositor_idle_release(compositor);
927 }
928
929 end = keyboard->keys.data + keyboard->keys.size;
930 for (k = keyboard->keys.data; k < end; k++) {
931 if (*k == key) {
932 /* Ignore server-generated repeats. */
933 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
934 return;
935 *k = *--end;
936 }
937 }
938 keyboard->keys.size = (void *) end - keyboard->keys.data;
939 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
940 k = wl_array_add(&keyboard->keys, sizeof *k);
941 *k = key;
942 }
943
944 if (grab == &keyboard->default_grab ||
945 grab == &keyboard->input_method_grab) {
946 weston_compositor_run_key_binding(compositor, seat, time, key,
947 state);
948 grab = keyboard->grab;
949 }
950
951 grab->interface->key(grab, time, key, state);
952
953 if (update_state == STATE_UPDATE_AUTOMATIC) {
954 update_modifier_state(seat,
955 wl_display_get_serial(compositor->wl_display),
956 key,
957 state);
958 }
959}
960
961WL_EXPORT void
962notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
963 wl_fixed_t x, wl_fixed_t y)
964{
965 struct weston_compositor *compositor = seat->compositor;
966
967 if (output) {
968 move_pointer(seat, x, y);
969 compositor->focus = 1;
970 } else {
971 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400972 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400973 * NULL) here, but somehow that breaks re-entry... */
974 }
975}
976
977static void
978destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
979{
980 struct weston_seat *ws;
981
982 ws = container_of(listener, struct weston_seat,
983 saved_kbd_focus_listener);
984
985 ws->saved_kbd_focus = NULL;
986}
987
988WL_EXPORT void
989notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
990 enum weston_key_state_update update_state)
991{
992 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400993 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400994 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400995 uint32_t *k, serial;
996
997 serial = wl_display_next_serial(compositor->wl_display);
998 wl_array_copy(&keyboard->keys, keys);
999 wl_array_for_each(k, &keyboard->keys) {
1000 weston_compositor_idle_inhibit(compositor);
1001 if (update_state == STATE_UPDATE_AUTOMATIC)
1002 update_modifier_state(seat, serial, *k,
1003 WL_KEYBOARD_KEY_STATE_PRESSED);
1004 }
1005
1006 /* Run key bindings after we've updated the state. */
1007 wl_array_for_each(k, &keyboard->keys) {
1008 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1009 WL_KEYBOARD_KEY_STATE_PRESSED);
1010 }
1011
1012 surface = seat->saved_kbd_focus;
1013
1014 if (surface) {
1015 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1016 weston_keyboard_set_focus(keyboard, surface);
1017 seat->saved_kbd_focus = NULL;
1018 }
1019}
1020
1021WL_EXPORT void
1022notify_keyboard_focus_out(struct weston_seat *seat)
1023{
1024 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001025 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001026 uint32_t *k, serial;
1027
1028 serial = wl_display_next_serial(compositor->wl_display);
1029 wl_array_for_each(k, &keyboard->keys) {
1030 weston_compositor_idle_release(compositor);
1031 update_modifier_state(seat, serial, *k,
1032 WL_KEYBOARD_KEY_STATE_RELEASED);
1033 }
1034
1035 seat->modifier_state = 0;
1036
1037 if (keyboard->focus) {
1038 seat->saved_kbd_focus = keyboard->focus;
1039 seat->saved_kbd_focus_listener.notify =
1040 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001041 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001042 &seat->saved_kbd_focus_listener);
1043 }
1044
1045 weston_keyboard_set_focus(keyboard, NULL);
1046 /* FIXME: We really need keyboard grab cancel here to
1047 * let the grab shut down properly. As it is we leak
1048 * the grab data. */
1049 weston_keyboard_end_grab(keyboard);
1050}
1051
Michael Fua2bb7912013-07-23 15:51:06 +08001052WL_EXPORT void
1053weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001054{
Neil Roberts96d790e2013-09-19 17:32:00 +01001055 struct wl_list *focus_resource_list;
1056
1057 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001058
1059 if (seat->touch->focus == surface)
1060 return;
1061
Neil Roberts96d790e2013-09-19 17:32:00 +01001062 if (!wl_list_empty(focus_resource_list)) {
1063 move_resources(&seat->touch->resource_list,
1064 focus_resource_list);
1065 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001066
1067 if (surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001068 struct wl_client *surface_client =
1069 wl_resource_get_client(surface->resource);
1070 move_resources_for_client(focus_resource_list,
1071 &seat->touch->resource_list,
1072 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001073 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001074 seat->touch->focus = surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001075}
1076
1077/**
1078 * notify_touch - emulates button touches and notifies surfaces accordingly.
1079 *
1080 * It assumes always the correct cycle sequence until it gets here: touch_down
1081 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1082 * for sending along such order.
1083 *
1084 */
1085WL_EXPORT void
1086notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1087 wl_fixed_t x, wl_fixed_t y, int touch_type)
1088{
1089 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001090 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001091 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001092 struct weston_surface *es;
1093 wl_fixed_t sx, sy;
1094
1095 /* Update grab's global coordinates. */
1096 touch->grab_x = x;
1097 touch->grab_y = y;
1098
1099 switch (touch_type) {
1100 case WL_TOUCH_DOWN:
1101 weston_compositor_idle_inhibit(ec);
1102
1103 seat->num_tp++;
1104
1105 /* the first finger down picks the surface, and all further go
1106 * to that surface for the remainder of the touch session i.e.
1107 * until all touch points are up again. */
1108 if (seat->num_tp == 1) {
1109 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001110 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001111 } else if (touch->focus) {
1112 es = (struct weston_surface *) touch->focus;
1113 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1114 } else {
1115 /* Unexpected condition: We have non-initial touch but
1116 * there is no focused surface.
1117 */
1118 weston_log("touch event received with %d points down"
1119 "but no surface focused\n", seat->num_tp);
1120 return;
1121 }
1122
1123 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001124 if (seat->num_tp == 1) {
1125 touch->grab_serial =
1126 wl_display_get_serial(ec->wl_display);
1127 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 }
1150}
1151
1152static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001153pointer_cursor_surface_configure(struct weston_surface *es,
1154 int32_t dx, int32_t dy, int32_t width, int32_t height)
1155{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001156 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001157 int x, y;
1158
1159 if (width == 0)
1160 return;
1161
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001162 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001163
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001164 pointer->hotspot_x -= dx;
1165 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001166
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001167 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1168 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001170 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001171
1172 empty_region(&es->pending.input);
1173
1174 if (!weston_surface_is_mapped(es)) {
1175 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1176 &es->layer_link);
1177 weston_surface_update_transform(es);
1178 }
1179}
1180
1181static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1183 uint32_t serial, struct wl_resource *surface_resource,
1184 int32_t x, int32_t y)
1185{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001186 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187 struct weston_surface *surface = NULL;
1188
1189 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001190 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001191
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001192 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001194 /* pointer->focus->resource can be NULL. Surfaces like the
1195 black_surface used in shell.c for fullscreen don't have
1196 a resource, but can still have focus */
1197 if (pointer->focus->resource == NULL)
1198 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001199 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001200 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001201 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001202 return;
1203
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001204 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001205 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001206 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207 WL_DISPLAY_ERROR_INVALID_OBJECT,
1208 "surface->configure already "
1209 "set");
1210 return;
1211 }
1212 }
1213
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001214 if (pointer->sprite)
1215 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216
1217 if (!surface)
1218 return;
1219
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001220 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001221 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001222
1223 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001224 surface->configure_private = pointer;
1225 pointer->sprite = surface;
1226 pointer->hotspot_x = x;
1227 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228
1229 if (surface->buffer_ref.buffer)
1230 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1231 weston_surface_buffer_height(surface));
1232}
1233
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001234static void
1235pointer_release(struct wl_client *client, struct wl_resource *resource)
1236{
1237 wl_resource_destroy(resource);
1238}
1239
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001240static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001241 pointer_set_cursor,
1242 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001243};
1244
1245static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001246seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1247 uint32_t id)
1248{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001249 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001250 struct wl_resource *cr;
1251
Kristian Høgsberge3148752013-05-06 23:19:49 -04001252 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001253 return;
1254
Jason Ekstranda85118c2013-06-27 20:17:02 -05001255 cr = wl_resource_create(client, &wl_pointer_interface,
1256 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001257 if (cr == NULL) {
1258 wl_client_post_no_memory(client);
1259 return;
1260 }
1261
Neil Roberts96d790e2013-09-19 17:32:00 +01001262 /* May be moved to focused list later by either
1263 * weston_pointer_set_focus or directly if this client is already
1264 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001265 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001266 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1267 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001268
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001269 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001270 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271 struct weston_surface *surface;
1272 wl_fixed_t sx, sy;
1273
Kristian Høgsberge3148752013-05-06 23:19:49 -04001274 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 seat->pointer->x,
1277 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001278 &sx,
1279 &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001280
1281 wl_list_remove(wl_resource_get_link(cr));
1282 wl_list_insert(&seat->pointer->focus_resource_list,
1283 wl_resource_get_link(cr));
1284 wl_pointer_send_enter(cr,
1285 seat->pointer->focus_serial,
1286 surface->resource,
1287 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001288 }
1289}
1290
1291static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001292keyboard_release(struct wl_client *client, struct wl_resource *resource)
1293{
1294 wl_resource_destroy(resource);
1295}
1296
1297static const struct wl_keyboard_interface keyboard_interface = {
1298 keyboard_release
1299};
1300
Neil Roberts96d790e2013-09-19 17:32:00 +01001301static int
1302should_send_modifiers_to_client(struct weston_seat *seat,
1303 struct wl_client *client)
1304{
1305 if (seat->keyboard &&
1306 seat->keyboard->focus &&
1307 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1308 return 1;
1309
1310 if (seat->pointer &&
1311 seat->pointer->focus &&
1312 wl_resource_get_client(seat->pointer->focus->resource) == client)
1313 return 1;
1314
1315 return 0;
1316}
1317
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001318static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001319seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1320 uint32_t id)
1321{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001322 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001323 struct wl_resource *cr;
1324
Kristian Høgsberge3148752013-05-06 23:19:49 -04001325 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001326 return;
1327
Jason Ekstranda85118c2013-06-27 20:17:02 -05001328 cr = wl_resource_create(client, &wl_keyboard_interface,
1329 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001330 if (cr == NULL) {
1331 wl_client_post_no_memory(client);
1332 return;
1333 }
1334
Neil Roberts96d790e2013-09-19 17:32:00 +01001335 /* May be moved to focused list later by either
1336 * weston_keyboard_set_focus or directly if this client is already
1337 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001338 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001339 wl_resource_set_implementation(cr, &keyboard_interface,
1340 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001341
Matt Roper01a92732013-06-24 16:52:44 +01001342 if (seat->compositor->use_xkbcommon) {
1343 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001344 seat->xkb_info->keymap_fd,
1345 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001346 } else {
1347 int null_fd = open("/dev/null", O_RDONLY);
1348 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1349 null_fd,
1350 0);
1351 close(null_fd);
1352 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001353
Neil Roberts96d790e2013-09-19 17:32:00 +01001354 if (should_send_modifiers_to_client(seat, client)) {
1355 send_modifiers_to_resource(seat->keyboard,
1356 cr,
1357 seat->keyboard->focus_serial);
1358 }
1359
Kristian Høgsberge3148752013-05-06 23:19:49 -04001360 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001361 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001362 struct weston_surface *surface =
1363 (struct weston_surface *) seat->keyboard->focus;
1364
1365 wl_list_remove(wl_resource_get_link(cr));
1366 wl_list_insert(&seat->keyboard->focus_resource_list,
1367 wl_resource_get_link(cr));
1368 wl_keyboard_send_enter(cr,
1369 seat->keyboard->focus_serial,
1370 surface->resource,
1371 &seat->keyboard->keys);
1372
1373 /* If this is the first keyboard resource for this
1374 * client... */
1375 if (seat->keyboard->focus_resource_list.prev ==
1376 wl_resource_get_link(cr))
1377 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001378 }
1379}
1380
1381static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001382touch_release(struct wl_client *client, struct wl_resource *resource)
1383{
1384 wl_resource_destroy(resource);
1385}
1386
1387static const struct wl_touch_interface touch_interface = {
1388 touch_release
1389};
1390
1391static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001392seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1393 uint32_t id)
1394{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001395 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001396 struct wl_resource *cr;
1397
Kristian Høgsberge3148752013-05-06 23:19:49 -04001398 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001399 return;
1400
Jason Ekstranda85118c2013-06-27 20:17:02 -05001401 cr = wl_resource_create(client, &wl_touch_interface,
1402 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001403 if (cr == NULL) {
1404 wl_client_post_no_memory(client);
1405 return;
1406 }
1407
Neil Roberts96d790e2013-09-19 17:32:00 +01001408 if (seat->touch->focus &&
1409 wl_resource_get_client(seat->touch->focus->resource) == client) {
1410 wl_list_insert(&seat->touch->resource_list,
1411 wl_resource_get_link(cr));
1412 } else {
1413 wl_list_insert(&seat->touch->focus_resource_list,
1414 wl_resource_get_link(cr));
1415 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001416 wl_resource_set_implementation(cr, &touch_interface,
1417 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001418}
1419
1420static const struct wl_seat_interface seat_interface = {
1421 seat_get_pointer,
1422 seat_get_keyboard,
1423 seat_get_touch,
1424};
1425
1426static void
1427bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1428{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001429 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001430 struct wl_resource *resource;
1431 enum wl_seat_capability caps = 0;
1432
Jason Ekstranda85118c2013-06-27 20:17:02 -05001433 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001434 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001435 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001436 wl_resource_set_implementation(resource, &seat_interface, data,
1437 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001438
1439 if (seat->pointer)
1440 caps |= WL_SEAT_CAPABILITY_POINTER;
1441 if (seat->keyboard)
1442 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1443 if (seat->touch)
1444 caps |= WL_SEAT_CAPABILITY_TOUCH;
1445
1446 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001447 if (version >= 2)
1448 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449}
1450
Rob Bradford382ff462013-06-24 16:52:45 +01001451#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001452int
1453weston_compositor_xkb_init(struct weston_compositor *ec,
1454 struct xkb_rule_names *names)
1455{
Rob Bradford382ff462013-06-24 16:52:45 +01001456 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001457
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001458 if (ec->xkb_context == NULL) {
1459 ec->xkb_context = xkb_context_new(0);
1460 if (ec->xkb_context == NULL) {
1461 weston_log("failed to create XKB context\n");
1462 return -1;
1463 }
1464 }
1465
1466 if (names)
1467 ec->xkb_names = *names;
1468 if (!ec->xkb_names.rules)
1469 ec->xkb_names.rules = strdup("evdev");
1470 if (!ec->xkb_names.model)
1471 ec->xkb_names.model = strdup("pc105");
1472 if (!ec->xkb_names.layout)
1473 ec->xkb_names.layout = strdup("us");
1474
1475 return 0;
1476}
1477
Stefan Schmidtfda26522013-09-17 10:54:09 +01001478static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001479weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001480{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001481 if (--xkb_info->ref_count > 0)
1482 return;
1483
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001484 if (xkb_info->keymap)
1485 xkb_map_unref(xkb_info->keymap);
1486
1487 if (xkb_info->keymap_area)
1488 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1489 if (xkb_info->keymap_fd >= 0)
1490 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001491 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001492}
1493
1494void
1495weston_compositor_xkb_destroy(struct weston_compositor *ec)
1496{
Matt Roper01a92732013-06-24 16:52:44 +01001497 /*
1498 * If we're operating in raw keyboard mode, we never initialized
1499 * libxkbcommon so there's no cleanup to do either.
1500 */
1501 if (!ec->use_xkbcommon)
1502 return;
1503
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001504 free((char *) ec->xkb_names.rules);
1505 free((char *) ec->xkb_names.model);
1506 free((char *) ec->xkb_names.layout);
1507 free((char *) ec->xkb_names.variant);
1508 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001509
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001510 if (ec->xkb_info)
1511 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001512 xkb_context_unref(ec->xkb_context);
1513}
1514
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001515static struct weston_xkb_info *
1516weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001517{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001518 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1519 if (xkb_info == NULL)
1520 return NULL;
1521
1522 xkb_info->keymap = xkb_map_ref(keymap);
1523 xkb_info->ref_count = 1;
1524
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001525 char *keymap_str;
1526
1527 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1528 XKB_MOD_NAME_SHIFT);
1529 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1530 XKB_MOD_NAME_CAPS);
1531 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1532 XKB_MOD_NAME_CTRL);
1533 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1534 XKB_MOD_NAME_ALT);
1535 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1536 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1537 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1538 XKB_MOD_NAME_LOGO);
1539 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1540
1541 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1542 XKB_LED_NAME_NUM);
1543 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1544 XKB_LED_NAME_CAPS);
1545 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1546 XKB_LED_NAME_SCROLL);
1547
1548 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1549 if (keymap_str == NULL) {
1550 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001551 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001552 }
1553 xkb_info->keymap_size = strlen(keymap_str) + 1;
1554
1555 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1556 if (xkb_info->keymap_fd < 0) {
1557 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1558 (unsigned long) xkb_info->keymap_size);
1559 goto err_keymap_str;
1560 }
1561
1562 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1563 PROT_READ | PROT_WRITE,
1564 MAP_SHARED, xkb_info->keymap_fd, 0);
1565 if (xkb_info->keymap_area == MAP_FAILED) {
1566 weston_log("failed to mmap() %lu bytes\n",
1567 (unsigned long) xkb_info->keymap_size);
1568 goto err_dev_zero;
1569 }
1570 strcpy(xkb_info->keymap_area, keymap_str);
1571 free(keymap_str);
1572
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001573 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001574
1575err_dev_zero:
1576 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001577err_keymap_str:
1578 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001579err_keymap:
1580 xkb_map_unref(xkb_info->keymap);
1581 free(xkb_info);
1582 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001583}
1584
1585static int
1586weston_compositor_build_global_keymap(struct weston_compositor *ec)
1587{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001588 struct xkb_keymap *keymap;
1589
1590 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001591 return 0;
1592
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001593 keymap = xkb_map_new_from_names(ec->xkb_context,
1594 &ec->xkb_names,
1595 0);
1596 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001597 weston_log("failed to compile global XKB keymap\n");
1598 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1599 "options %s\n",
1600 ec->xkb_names.rules, ec->xkb_names.model,
1601 ec->xkb_names.layout, ec->xkb_names.variant,
1602 ec->xkb_names.options);
1603 return -1;
1604 }
1605
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001606 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001607 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001608 return -1;
1609
1610 return 0;
1611}
Rob Bradford382ff462013-06-24 16:52:45 +01001612#else
1613int
1614weston_compositor_xkb_init(struct weston_compositor *ec,
1615 struct xkb_rule_names *names)
1616{
1617 return 0;
1618}
1619
1620void
1621weston_compositor_xkb_destroy(struct weston_compositor *ec)
1622{
1623}
1624#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001625
1626WL_EXPORT int
1627weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1628{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001629 struct weston_keyboard *keyboard;
1630
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001631 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001632 return 0;
1633
Rob Bradford382ff462013-06-24 16:52:45 +01001634#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001635 if (seat->compositor->use_xkbcommon) {
1636 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001637 seat->xkb_info = weston_xkb_info_create(keymap);
1638 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001639 return -1;
1640 } else {
1641 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1642 return -1;
1643 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001644 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001645 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001646
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001647 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001648 if (seat->xkb_state.state == NULL) {
1649 weston_log("failed to initialise XKB state\n");
1650 return -1;
1651 }
1652
1653 seat->xkb_state.leds = 0;
1654 }
Rob Bradford382ff462013-06-24 16:52:45 +01001655#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001656
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001657 keyboard = weston_keyboard_create();
1658 if (keyboard == NULL) {
1659 weston_log("failed to allocate weston keyboard struct\n");
1660 return -1;
1661 }
1662
1663 seat->keyboard = keyboard;
1664 keyboard->seat = seat;
1665
1666 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001667
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001668 return 0;
1669}
1670
1671WL_EXPORT void
1672weston_seat_init_pointer(struct weston_seat *seat)
1673{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001674 struct weston_pointer *pointer;
1675
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001676 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677 return;
1678
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001679 pointer = weston_pointer_create();
1680 if (pointer == NULL)
1681 return;
1682
1683 seat->pointer = pointer;
1684 pointer->seat = seat;
1685
1686 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001687}
1688
1689WL_EXPORT void
1690weston_seat_init_touch(struct weston_seat *seat)
1691{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001692 struct weston_touch *touch;
1693
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001694 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001695 return;
1696
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001697 touch = weston_touch_create();
1698 if (touch == NULL)
1699 return;
1700
1701 seat->touch = touch;
1702 touch->seat = seat;
1703
1704 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001705}
1706
1707WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001708weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1709 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001711 memset(seat, 0, sizeof *seat);
1712
Kristian Høgsberge3148752013-05-06 23:19:49 -04001713 seat->selection_data_source = NULL;
1714 wl_list_init(&seat->base_resource_list);
1715 wl_signal_init(&seat->selection_signal);
1716 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001718
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001719 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001720 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001721
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001723 seat->modifier_state = 0;
1724 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001725 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001727 wl_list_insert(ec->seat_list.prev, &seat->link);
1728
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001729 clipboard_create(seat);
1730
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001731 wl_signal_emit(&ec->seat_created_signal, seat);
1732}
1733
1734WL_EXPORT void
1735weston_seat_release(struct weston_seat *seat)
1736{
1737 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001738
Rob Bradford382ff462013-06-24 16:52:45 +01001739#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001740 if (seat->compositor->use_xkbcommon) {
1741 if (seat->xkb_state.state != NULL)
1742 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001743 if (seat->xkb_info)
1744 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001745 }
Rob Bradford382ff462013-06-24 16:52:45 +01001746#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001747
Kristian Høgsberge3148752013-05-06 23:19:49 -04001748 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001749 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001750 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001751 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001752 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001753 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001754
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001755 free (seat->seat_name);
1756
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001757 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001758
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001759 wl_signal_emit(&seat->destroy_signal, seat);
1760}