blob: a994fb95531705f7b612022ffdcf9bed2497dd34 [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
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. */
1093 touch->grab_x = x;
1094 touch->grab_y = y;
1095
1096 switch (touch_type) {
1097 case WL_TOUCH_DOWN:
1098 weston_compositor_idle_inhibit(ec);
1099
1100 seat->num_tp++;
1101
1102 /* the first finger down picks the surface, and all further go
1103 * to that surface for the remainder of the touch session i.e.
1104 * until all touch points are up again. */
1105 if (seat->num_tp == 1) {
1106 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001107 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001108 } else if (touch->focus) {
1109 es = (struct weston_surface *) touch->focus;
1110 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1111 } else {
1112 /* Unexpected condition: We have non-initial touch but
1113 * there is no focused surface.
1114 */
1115 weston_log("touch event received with %d points down"
1116 "but no surface focused\n", seat->num_tp);
1117 return;
1118 }
1119
1120 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001121 if (seat->num_tp == 1) {
1122 touch->grab_serial =
1123 wl_display_get_serial(ec->wl_display);
1124 touch->grab_time = time;
1125 touch->grab_x = x;
1126 touch->grab_y = y;
1127 }
1128
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001129 break;
1130 case WL_TOUCH_MOTION:
1131 es = (struct weston_surface *) touch->focus;
1132 if (!es)
1133 break;
1134
1135 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1136 grab->interface->motion(grab, time, touch_id, sx, sy);
1137 break;
1138 case WL_TOUCH_UP:
1139 weston_compositor_idle_release(ec);
1140 seat->num_tp--;
1141
1142 grab->interface->up(grab, time, touch_id);
1143 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001144 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001145 break;
1146 }
1147}
1148
1149static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001150pointer_cursor_surface_configure(struct weston_surface *es,
1151 int32_t dx, int32_t dy, int32_t width, int32_t height)
1152{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001153 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001154 int x, y;
1155
1156 if (width == 0)
1157 return;
1158
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001159 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001160
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001161 pointer->hotspot_x -= dx;
1162 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001163
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001164 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1165 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001166
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001167 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168
1169 empty_region(&es->pending.input);
1170
1171 if (!weston_surface_is_mapped(es)) {
1172 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1173 &es->layer_link);
1174 weston_surface_update_transform(es);
1175 }
1176}
1177
1178static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001179pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1180 uint32_t serial, struct wl_resource *surface_resource,
1181 int32_t x, int32_t y)
1182{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001183 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001184 struct weston_surface *surface = NULL;
1185
1186 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001187 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001188
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001189 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001191 /* pointer->focus->resource can be NULL. Surfaces like the
1192 black_surface used in shell.c for fullscreen don't have
1193 a resource, but can still have focus */
1194 if (pointer->focus->resource == NULL)
1195 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001196 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001197 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001198 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 return;
1200
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001201 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001202 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001203 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 WL_DISPLAY_ERROR_INVALID_OBJECT,
1205 "surface->configure already "
1206 "set");
1207 return;
1208 }
1209 }
1210
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001211 if (pointer->sprite)
1212 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001213
1214 if (!surface)
1215 return;
1216
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001217 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001218 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001219
1220 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001221 surface->configure_private = pointer;
1222 pointer->sprite = surface;
1223 pointer->hotspot_x = x;
1224 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001225
1226 if (surface->buffer_ref.buffer)
1227 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1228 weston_surface_buffer_height(surface));
1229}
1230
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001231static void
1232pointer_release(struct wl_client *client, struct wl_resource *resource)
1233{
1234 wl_resource_destroy(resource);
1235}
1236
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001237static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001238 pointer_set_cursor,
1239 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001240};
1241
1242static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001243seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1244 uint32_t id)
1245{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001246 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001247 struct wl_resource *cr;
1248
Kristian Høgsberge3148752013-05-06 23:19:49 -04001249 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001250 return;
1251
Jason Ekstranda85118c2013-06-27 20:17:02 -05001252 cr = wl_resource_create(client, &wl_pointer_interface,
1253 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001254 if (cr == NULL) {
1255 wl_client_post_no_memory(client);
1256 return;
1257 }
1258
Neil Roberts96d790e2013-09-19 17:32:00 +01001259 /* May be moved to focused list later by either
1260 * weston_pointer_set_focus or directly if this client is already
1261 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001262 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001263 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1264 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001265
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001266 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001267 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001268 struct weston_surface *surface;
1269 wl_fixed_t sx, sy;
1270
Kristian Høgsberge3148752013-05-06 23:19:49 -04001271 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001272 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 seat->pointer->x,
1274 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275 &sx,
1276 &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001277
1278 wl_list_remove(wl_resource_get_link(cr));
1279 wl_list_insert(&seat->pointer->focus_resource_list,
1280 wl_resource_get_link(cr));
1281 wl_pointer_send_enter(cr,
1282 seat->pointer->focus_serial,
1283 surface->resource,
1284 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001285 }
1286}
1287
1288static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001289keyboard_release(struct wl_client *client, struct wl_resource *resource)
1290{
1291 wl_resource_destroy(resource);
1292}
1293
1294static const struct wl_keyboard_interface keyboard_interface = {
1295 keyboard_release
1296};
1297
Neil Roberts96d790e2013-09-19 17:32:00 +01001298static int
1299should_send_modifiers_to_client(struct weston_seat *seat,
1300 struct wl_client *client)
1301{
1302 if (seat->keyboard &&
1303 seat->keyboard->focus &&
1304 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1305 return 1;
1306
1307 if (seat->pointer &&
1308 seat->pointer->focus &&
1309 wl_resource_get_client(seat->pointer->focus->resource) == client)
1310 return 1;
1311
1312 return 0;
1313}
1314
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001315static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001316seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1317 uint32_t id)
1318{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001319 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001320 struct wl_resource *cr;
1321
Kristian Høgsberge3148752013-05-06 23:19:49 -04001322 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001323 return;
1324
Jason Ekstranda85118c2013-06-27 20:17:02 -05001325 cr = wl_resource_create(client, &wl_keyboard_interface,
1326 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001327 if (cr == NULL) {
1328 wl_client_post_no_memory(client);
1329 return;
1330 }
1331
Neil Roberts96d790e2013-09-19 17:32:00 +01001332 /* May be moved to focused list later by either
1333 * weston_keyboard_set_focus or directly if this client is already
1334 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001335 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001336 wl_resource_set_implementation(cr, &keyboard_interface,
1337 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001338
Matt Roper01a92732013-06-24 16:52:44 +01001339 if (seat->compositor->use_xkbcommon) {
1340 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001341 seat->xkb_info->keymap_fd,
1342 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001343 } else {
1344 int null_fd = open("/dev/null", O_RDONLY);
1345 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1346 null_fd,
1347 0);
1348 close(null_fd);
1349 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001350
Neil Roberts96d790e2013-09-19 17:32:00 +01001351 if (should_send_modifiers_to_client(seat, client)) {
1352 send_modifiers_to_resource(seat->keyboard,
1353 cr,
1354 seat->keyboard->focus_serial);
1355 }
1356
Kristian Høgsberge3148752013-05-06 23:19:49 -04001357 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001358 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001359 struct weston_surface *surface =
1360 (struct weston_surface *) seat->keyboard->focus;
1361
1362 wl_list_remove(wl_resource_get_link(cr));
1363 wl_list_insert(&seat->keyboard->focus_resource_list,
1364 wl_resource_get_link(cr));
1365 wl_keyboard_send_enter(cr,
1366 seat->keyboard->focus_serial,
1367 surface->resource,
1368 &seat->keyboard->keys);
1369
1370 /* If this is the first keyboard resource for this
1371 * client... */
1372 if (seat->keyboard->focus_resource_list.prev ==
1373 wl_resource_get_link(cr))
1374 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001375 }
1376}
1377
1378static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001379touch_release(struct wl_client *client, struct wl_resource *resource)
1380{
1381 wl_resource_destroy(resource);
1382}
1383
1384static const struct wl_touch_interface touch_interface = {
1385 touch_release
1386};
1387
1388static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001389seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1390 uint32_t id)
1391{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001392 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001393 struct wl_resource *cr;
1394
Kristian Høgsberge3148752013-05-06 23:19:49 -04001395 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001396 return;
1397
Jason Ekstranda85118c2013-06-27 20:17:02 -05001398 cr = wl_resource_create(client, &wl_touch_interface,
1399 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001400 if (cr == NULL) {
1401 wl_client_post_no_memory(client);
1402 return;
1403 }
1404
Neil Roberts96d790e2013-09-19 17:32:00 +01001405 if (seat->touch->focus &&
1406 wl_resource_get_client(seat->touch->focus->resource) == client) {
1407 wl_list_insert(&seat->touch->resource_list,
1408 wl_resource_get_link(cr));
1409 } else {
1410 wl_list_insert(&seat->touch->focus_resource_list,
1411 wl_resource_get_link(cr));
1412 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001413 wl_resource_set_implementation(cr, &touch_interface,
1414 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001415}
1416
1417static const struct wl_seat_interface seat_interface = {
1418 seat_get_pointer,
1419 seat_get_keyboard,
1420 seat_get_touch,
1421};
1422
1423static void
1424bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1425{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001426 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001427 struct wl_resource *resource;
1428 enum wl_seat_capability caps = 0;
1429
Jason Ekstranda85118c2013-06-27 20:17:02 -05001430 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001431 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001432 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001433 wl_resource_set_implementation(resource, &seat_interface, data,
1434 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001435
1436 if (seat->pointer)
1437 caps |= WL_SEAT_CAPABILITY_POINTER;
1438 if (seat->keyboard)
1439 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1440 if (seat->touch)
1441 caps |= WL_SEAT_CAPABILITY_TOUCH;
1442
1443 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001444 if (version >= 2)
1445 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001446}
1447
Rob Bradford382ff462013-06-24 16:52:45 +01001448#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449int
1450weston_compositor_xkb_init(struct weston_compositor *ec,
1451 struct xkb_rule_names *names)
1452{
Rob Bradford382ff462013-06-24 16:52:45 +01001453 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001454
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001455 if (ec->xkb_context == NULL) {
1456 ec->xkb_context = xkb_context_new(0);
1457 if (ec->xkb_context == NULL) {
1458 weston_log("failed to create XKB context\n");
1459 return -1;
1460 }
1461 }
1462
1463 if (names)
1464 ec->xkb_names = *names;
1465 if (!ec->xkb_names.rules)
1466 ec->xkb_names.rules = strdup("evdev");
1467 if (!ec->xkb_names.model)
1468 ec->xkb_names.model = strdup("pc105");
1469 if (!ec->xkb_names.layout)
1470 ec->xkb_names.layout = strdup("us");
1471
1472 return 0;
1473}
1474
Stefan Schmidtfda26522013-09-17 10:54:09 +01001475static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001476weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001477{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001478 if (--xkb_info->ref_count > 0)
1479 return;
1480
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001481 if (xkb_info->keymap)
1482 xkb_map_unref(xkb_info->keymap);
1483
1484 if (xkb_info->keymap_area)
1485 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1486 if (xkb_info->keymap_fd >= 0)
1487 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001488 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001489}
1490
1491void
1492weston_compositor_xkb_destroy(struct weston_compositor *ec)
1493{
Matt Roper01a92732013-06-24 16:52:44 +01001494 /*
1495 * If we're operating in raw keyboard mode, we never initialized
1496 * libxkbcommon so there's no cleanup to do either.
1497 */
1498 if (!ec->use_xkbcommon)
1499 return;
1500
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001501 free((char *) ec->xkb_names.rules);
1502 free((char *) ec->xkb_names.model);
1503 free((char *) ec->xkb_names.layout);
1504 free((char *) ec->xkb_names.variant);
1505 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001506
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001507 if (ec->xkb_info)
1508 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001509 xkb_context_unref(ec->xkb_context);
1510}
1511
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001512static struct weston_xkb_info *
1513weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001514{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001515 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1516 if (xkb_info == NULL)
1517 return NULL;
1518
1519 xkb_info->keymap = xkb_map_ref(keymap);
1520 xkb_info->ref_count = 1;
1521
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001522 char *keymap_str;
1523
1524 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1525 XKB_MOD_NAME_SHIFT);
1526 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1527 XKB_MOD_NAME_CAPS);
1528 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1529 XKB_MOD_NAME_CTRL);
1530 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1531 XKB_MOD_NAME_ALT);
1532 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1533 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1534 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1535 XKB_MOD_NAME_LOGO);
1536 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1537
1538 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1539 XKB_LED_NAME_NUM);
1540 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1541 XKB_LED_NAME_CAPS);
1542 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1543 XKB_LED_NAME_SCROLL);
1544
1545 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1546 if (keymap_str == NULL) {
1547 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001548 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001549 }
1550 xkb_info->keymap_size = strlen(keymap_str) + 1;
1551
1552 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1553 if (xkb_info->keymap_fd < 0) {
1554 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1555 (unsigned long) xkb_info->keymap_size);
1556 goto err_keymap_str;
1557 }
1558
1559 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1560 PROT_READ | PROT_WRITE,
1561 MAP_SHARED, xkb_info->keymap_fd, 0);
1562 if (xkb_info->keymap_area == MAP_FAILED) {
1563 weston_log("failed to mmap() %lu bytes\n",
1564 (unsigned long) xkb_info->keymap_size);
1565 goto err_dev_zero;
1566 }
1567 strcpy(xkb_info->keymap_area, keymap_str);
1568 free(keymap_str);
1569
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001570 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001571
1572err_dev_zero:
1573 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001574err_keymap_str:
1575 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001576err_keymap:
1577 xkb_map_unref(xkb_info->keymap);
1578 free(xkb_info);
1579 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001580}
1581
1582static int
1583weston_compositor_build_global_keymap(struct weston_compositor *ec)
1584{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001585 struct xkb_keymap *keymap;
1586
1587 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001588 return 0;
1589
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001590 keymap = xkb_map_new_from_names(ec->xkb_context,
1591 &ec->xkb_names,
1592 0);
1593 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001594 weston_log("failed to compile global XKB keymap\n");
1595 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1596 "options %s\n",
1597 ec->xkb_names.rules, ec->xkb_names.model,
1598 ec->xkb_names.layout, ec->xkb_names.variant,
1599 ec->xkb_names.options);
1600 return -1;
1601 }
1602
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001603 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001604 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605 return -1;
1606
1607 return 0;
1608}
Rob Bradford382ff462013-06-24 16:52:45 +01001609#else
1610int
1611weston_compositor_xkb_init(struct weston_compositor *ec,
1612 struct xkb_rule_names *names)
1613{
1614 return 0;
1615}
1616
1617void
1618weston_compositor_xkb_destroy(struct weston_compositor *ec)
1619{
1620}
1621#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001622
1623WL_EXPORT int
1624weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1625{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001626 struct weston_keyboard *keyboard;
1627
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001628 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001629 return 0;
1630
Rob Bradford382ff462013-06-24 16:52:45 +01001631#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001632 if (seat->compositor->use_xkbcommon) {
1633 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001634 seat->xkb_info = weston_xkb_info_create(keymap);
1635 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001636 return -1;
1637 } else {
1638 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1639 return -1;
1640 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001641 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001642 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001643
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001644 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001645 if (seat->xkb_state.state == NULL) {
1646 weston_log("failed to initialise XKB state\n");
1647 return -1;
1648 }
1649
1650 seat->xkb_state.leds = 0;
1651 }
Rob Bradford382ff462013-06-24 16:52:45 +01001652#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001653
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001654 keyboard = weston_keyboard_create();
1655 if (keyboard == NULL) {
1656 weston_log("failed to allocate weston keyboard struct\n");
1657 return -1;
1658 }
1659
1660 seat->keyboard = keyboard;
1661 keyboard->seat = seat;
1662
1663 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001664
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001665 return 0;
1666}
1667
1668WL_EXPORT void
1669weston_seat_init_pointer(struct weston_seat *seat)
1670{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001671 struct weston_pointer *pointer;
1672
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001673 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001674 return;
1675
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001676 pointer = weston_pointer_create();
1677 if (pointer == NULL)
1678 return;
1679
1680 seat->pointer = pointer;
1681 pointer->seat = seat;
1682
1683 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001684}
1685
1686WL_EXPORT void
1687weston_seat_init_touch(struct weston_seat *seat)
1688{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001689 struct weston_touch *touch;
1690
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001691 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001692 return;
1693
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001694 touch = weston_touch_create();
1695 if (touch == NULL)
1696 return;
1697
1698 seat->touch = touch;
1699 touch->seat = seat;
1700
1701 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001702}
1703
1704WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001705weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1706 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001707{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001708 memset(seat, 0, sizeof *seat);
1709
Kristian Høgsberge3148752013-05-06 23:19:49 -04001710 seat->selection_data_source = NULL;
1711 wl_list_init(&seat->base_resource_list);
1712 wl_signal_init(&seat->selection_signal);
1713 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001714 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001715
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001716 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001717 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001718
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001719 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720 seat->modifier_state = 0;
1721 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001722 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001723
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001724 wl_list_insert(ec->seat_list.prev, &seat->link);
1725
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726 clipboard_create(seat);
1727
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001728 wl_signal_emit(&ec->seat_created_signal, seat);
1729}
1730
1731WL_EXPORT void
1732weston_seat_release(struct weston_seat *seat)
1733{
1734 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001735
Rob Bradford382ff462013-06-24 16:52:45 +01001736#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001737 if (seat->compositor->use_xkbcommon) {
1738 if (seat->xkb_state.state != NULL)
1739 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001740 if (seat->xkb_info)
1741 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001742 }
Rob Bradford382ff462013-06-24 16:52:45 +01001743#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001744
Kristian Høgsberge3148752013-05-06 23:19:49 -04001745 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001746 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001747 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001748 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001749 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001750 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001751
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001752 free (seat->seat_name);
1753
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001754 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001755
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001756 wl_signal_emit(&seat->destroy_signal, seat);
1757}