blob: 436b36c98be77952915d21792f95eb03838e9469 [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 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001147
1148 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001149}
1150
1151static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001152pointer_cursor_surface_configure(struct weston_surface *es,
1153 int32_t dx, int32_t dy, int32_t width, int32_t height)
1154{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001155 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001156 int x, y;
1157
1158 if (width == 0)
1159 return;
1160
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001161 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001162
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001163 pointer->hotspot_x -= dx;
1164 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001165
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001166 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1167 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001169 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001170
1171 empty_region(&es->pending.input);
1172
1173 if (!weston_surface_is_mapped(es)) {
1174 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1175 &es->layer_link);
1176 weston_surface_update_transform(es);
1177 }
1178}
1179
1180static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001181pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1182 uint32_t serial, struct wl_resource *surface_resource,
1183 int32_t x, int32_t y)
1184{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001185 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001186 struct weston_surface *surface = NULL;
1187
1188 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001189 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001191 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001192 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001193 /* pointer->focus->resource can be NULL. Surfaces like the
1194 black_surface used in shell.c for fullscreen don't have
1195 a resource, but can still have focus */
1196 if (pointer->focus->resource == NULL)
1197 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001198 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001200 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001201 return;
1202
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001203 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001205 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001206 WL_DISPLAY_ERROR_INVALID_OBJECT,
1207 "surface->configure already "
1208 "set");
1209 return;
1210 }
1211 }
1212
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001213 if (pointer->sprite)
1214 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001215
1216 if (!surface)
1217 return;
1218
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001219 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001220 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001221
1222 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001223 surface->configure_private = pointer;
1224 pointer->sprite = surface;
1225 pointer->hotspot_x = x;
1226 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001227
1228 if (surface->buffer_ref.buffer)
1229 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1230 weston_surface_buffer_height(surface));
1231}
1232
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001233static void
1234pointer_release(struct wl_client *client, struct wl_resource *resource)
1235{
1236 wl_resource_destroy(resource);
1237}
1238
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001239static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001240 pointer_set_cursor,
1241 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242};
1243
1244static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1246 uint32_t id)
1247{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001248 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001249 struct wl_resource *cr;
1250
Kristian Høgsberge3148752013-05-06 23:19:49 -04001251 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001252 return;
1253
Jason Ekstranda85118c2013-06-27 20:17:02 -05001254 cr = wl_resource_create(client, &wl_pointer_interface,
1255 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001256 if (cr == NULL) {
1257 wl_client_post_no_memory(client);
1258 return;
1259 }
1260
Neil Roberts96d790e2013-09-19 17:32:00 +01001261 /* May be moved to focused list later by either
1262 * weston_pointer_set_focus or directly if this client is already
1263 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001264 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001265 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1266 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001267
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001268 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001269 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001270 struct weston_surface *surface;
1271 wl_fixed_t sx, sy;
1272
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001275 seat->pointer->x,
1276 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001277 &sx,
1278 &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001279
1280 wl_list_remove(wl_resource_get_link(cr));
1281 wl_list_insert(&seat->pointer->focus_resource_list,
1282 wl_resource_get_link(cr));
1283 wl_pointer_send_enter(cr,
1284 seat->pointer->focus_serial,
1285 surface->resource,
1286 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001287 }
1288}
1289
1290static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001291keyboard_release(struct wl_client *client, struct wl_resource *resource)
1292{
1293 wl_resource_destroy(resource);
1294}
1295
1296static const struct wl_keyboard_interface keyboard_interface = {
1297 keyboard_release
1298};
1299
Neil Roberts96d790e2013-09-19 17:32:00 +01001300static int
1301should_send_modifiers_to_client(struct weston_seat *seat,
1302 struct wl_client *client)
1303{
1304 if (seat->keyboard &&
1305 seat->keyboard->focus &&
1306 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1307 return 1;
1308
1309 if (seat->pointer &&
1310 seat->pointer->focus &&
1311 wl_resource_get_client(seat->pointer->focus->resource) == client)
1312 return 1;
1313
1314 return 0;
1315}
1316
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001317static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001318seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1319 uint32_t id)
1320{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001321 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001322 struct wl_resource *cr;
1323
Kristian Høgsberge3148752013-05-06 23:19:49 -04001324 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001325 return;
1326
Jason Ekstranda85118c2013-06-27 20:17:02 -05001327 cr = wl_resource_create(client, &wl_keyboard_interface,
1328 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001329 if (cr == NULL) {
1330 wl_client_post_no_memory(client);
1331 return;
1332 }
1333
Neil Roberts96d790e2013-09-19 17:32:00 +01001334 /* May be moved to focused list later by either
1335 * weston_keyboard_set_focus or directly if this client is already
1336 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001337 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001338 wl_resource_set_implementation(cr, &keyboard_interface,
1339 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001340
Matt Roper01a92732013-06-24 16:52:44 +01001341 if (seat->compositor->use_xkbcommon) {
1342 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001343 seat->xkb_info->keymap_fd,
1344 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001345 } else {
1346 int null_fd = open("/dev/null", O_RDONLY);
1347 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1348 null_fd,
1349 0);
1350 close(null_fd);
1351 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001352
Neil Roberts96d790e2013-09-19 17:32:00 +01001353 if (should_send_modifiers_to_client(seat, client)) {
1354 send_modifiers_to_resource(seat->keyboard,
1355 cr,
1356 seat->keyboard->focus_serial);
1357 }
1358
Kristian Høgsberge3148752013-05-06 23:19:49 -04001359 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001360 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001361 struct weston_surface *surface =
1362 (struct weston_surface *) seat->keyboard->focus;
1363
1364 wl_list_remove(wl_resource_get_link(cr));
1365 wl_list_insert(&seat->keyboard->focus_resource_list,
1366 wl_resource_get_link(cr));
1367 wl_keyboard_send_enter(cr,
1368 seat->keyboard->focus_serial,
1369 surface->resource,
1370 &seat->keyboard->keys);
1371
1372 /* If this is the first keyboard resource for this
1373 * client... */
1374 if (seat->keyboard->focus_resource_list.prev ==
1375 wl_resource_get_link(cr))
1376 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001377 }
1378}
1379
1380static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001381touch_release(struct wl_client *client, struct wl_resource *resource)
1382{
1383 wl_resource_destroy(resource);
1384}
1385
1386static const struct wl_touch_interface touch_interface = {
1387 touch_release
1388};
1389
1390static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001391seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1392 uint32_t id)
1393{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001394 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001395 struct wl_resource *cr;
1396
Kristian Høgsberge3148752013-05-06 23:19:49 -04001397 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001398 return;
1399
Jason Ekstranda85118c2013-06-27 20:17:02 -05001400 cr = wl_resource_create(client, &wl_touch_interface,
1401 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001402 if (cr == NULL) {
1403 wl_client_post_no_memory(client);
1404 return;
1405 }
1406
Neil Roberts96d790e2013-09-19 17:32:00 +01001407 if (seat->touch->focus &&
1408 wl_resource_get_client(seat->touch->focus->resource) == client) {
1409 wl_list_insert(&seat->touch->resource_list,
1410 wl_resource_get_link(cr));
1411 } else {
1412 wl_list_insert(&seat->touch->focus_resource_list,
1413 wl_resource_get_link(cr));
1414 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001415 wl_resource_set_implementation(cr, &touch_interface,
1416 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001417}
1418
1419static const struct wl_seat_interface seat_interface = {
1420 seat_get_pointer,
1421 seat_get_keyboard,
1422 seat_get_touch,
1423};
1424
1425static void
1426bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1427{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001428 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001429 struct wl_resource *resource;
1430 enum wl_seat_capability caps = 0;
1431
Jason Ekstranda85118c2013-06-27 20:17:02 -05001432 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001433 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001434 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001435 wl_resource_set_implementation(resource, &seat_interface, data,
1436 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001437
1438 if (seat->pointer)
1439 caps |= WL_SEAT_CAPABILITY_POINTER;
1440 if (seat->keyboard)
1441 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1442 if (seat->touch)
1443 caps |= WL_SEAT_CAPABILITY_TOUCH;
1444
1445 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001446 if (version >= 2)
1447 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001448}
1449
Rob Bradford382ff462013-06-24 16:52:45 +01001450#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001451int
1452weston_compositor_xkb_init(struct weston_compositor *ec,
1453 struct xkb_rule_names *names)
1454{
Rob Bradford382ff462013-06-24 16:52:45 +01001455 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001456
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001457 if (ec->xkb_context == NULL) {
1458 ec->xkb_context = xkb_context_new(0);
1459 if (ec->xkb_context == NULL) {
1460 weston_log("failed to create XKB context\n");
1461 return -1;
1462 }
1463 }
1464
1465 if (names)
1466 ec->xkb_names = *names;
1467 if (!ec->xkb_names.rules)
1468 ec->xkb_names.rules = strdup("evdev");
1469 if (!ec->xkb_names.model)
1470 ec->xkb_names.model = strdup("pc105");
1471 if (!ec->xkb_names.layout)
1472 ec->xkb_names.layout = strdup("us");
1473
1474 return 0;
1475}
1476
Stefan Schmidtfda26522013-09-17 10:54:09 +01001477static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001478weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001479{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001480 if (--xkb_info->ref_count > 0)
1481 return;
1482
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483 if (xkb_info->keymap)
1484 xkb_map_unref(xkb_info->keymap);
1485
1486 if (xkb_info->keymap_area)
1487 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1488 if (xkb_info->keymap_fd >= 0)
1489 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001490 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001491}
1492
1493void
1494weston_compositor_xkb_destroy(struct weston_compositor *ec)
1495{
Matt Roper01a92732013-06-24 16:52:44 +01001496 /*
1497 * If we're operating in raw keyboard mode, we never initialized
1498 * libxkbcommon so there's no cleanup to do either.
1499 */
1500 if (!ec->use_xkbcommon)
1501 return;
1502
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001503 free((char *) ec->xkb_names.rules);
1504 free((char *) ec->xkb_names.model);
1505 free((char *) ec->xkb_names.layout);
1506 free((char *) ec->xkb_names.variant);
1507 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001508
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001509 if (ec->xkb_info)
1510 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001511 xkb_context_unref(ec->xkb_context);
1512}
1513
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001514static struct weston_xkb_info *
1515weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001516{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001517 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1518 if (xkb_info == NULL)
1519 return NULL;
1520
1521 xkb_info->keymap = xkb_map_ref(keymap);
1522 xkb_info->ref_count = 1;
1523
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001524 char *keymap_str;
1525
1526 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1527 XKB_MOD_NAME_SHIFT);
1528 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1529 XKB_MOD_NAME_CAPS);
1530 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1531 XKB_MOD_NAME_CTRL);
1532 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1533 XKB_MOD_NAME_ALT);
1534 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1535 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1536 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1537 XKB_MOD_NAME_LOGO);
1538 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1539
1540 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1541 XKB_LED_NAME_NUM);
1542 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1543 XKB_LED_NAME_CAPS);
1544 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1545 XKB_LED_NAME_SCROLL);
1546
1547 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1548 if (keymap_str == NULL) {
1549 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001550 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001551 }
1552 xkb_info->keymap_size = strlen(keymap_str) + 1;
1553
1554 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1555 if (xkb_info->keymap_fd < 0) {
1556 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1557 (unsigned long) xkb_info->keymap_size);
1558 goto err_keymap_str;
1559 }
1560
1561 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1562 PROT_READ | PROT_WRITE,
1563 MAP_SHARED, xkb_info->keymap_fd, 0);
1564 if (xkb_info->keymap_area == MAP_FAILED) {
1565 weston_log("failed to mmap() %lu bytes\n",
1566 (unsigned long) xkb_info->keymap_size);
1567 goto err_dev_zero;
1568 }
1569 strcpy(xkb_info->keymap_area, keymap_str);
1570 free(keymap_str);
1571
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001572 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001573
1574err_dev_zero:
1575 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001576err_keymap_str:
1577 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001578err_keymap:
1579 xkb_map_unref(xkb_info->keymap);
1580 free(xkb_info);
1581 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001582}
1583
1584static int
1585weston_compositor_build_global_keymap(struct weston_compositor *ec)
1586{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001587 struct xkb_keymap *keymap;
1588
1589 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001590 return 0;
1591
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001592 keymap = xkb_map_new_from_names(ec->xkb_context,
1593 &ec->xkb_names,
1594 0);
1595 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596 weston_log("failed to compile global XKB keymap\n");
1597 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1598 "options %s\n",
1599 ec->xkb_names.rules, ec->xkb_names.model,
1600 ec->xkb_names.layout, ec->xkb_names.variant,
1601 ec->xkb_names.options);
1602 return -1;
1603 }
1604
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001605 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001606 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001607 return -1;
1608
1609 return 0;
1610}
Rob Bradford382ff462013-06-24 16:52:45 +01001611#else
1612int
1613weston_compositor_xkb_init(struct weston_compositor *ec,
1614 struct xkb_rule_names *names)
1615{
1616 return 0;
1617}
1618
1619void
1620weston_compositor_xkb_destroy(struct weston_compositor *ec)
1621{
1622}
1623#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001624
1625WL_EXPORT int
1626weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1627{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001628 struct weston_keyboard *keyboard;
1629
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001630 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001631 return 0;
1632
Rob Bradford382ff462013-06-24 16:52:45 +01001633#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001634 if (seat->compositor->use_xkbcommon) {
1635 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001636 seat->xkb_info = weston_xkb_info_create(keymap);
1637 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001638 return -1;
1639 } else {
1640 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1641 return -1;
1642 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001643 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001644 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001645
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001646 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001647 if (seat->xkb_state.state == NULL) {
1648 weston_log("failed to initialise XKB state\n");
1649 return -1;
1650 }
1651
1652 seat->xkb_state.leds = 0;
1653 }
Rob Bradford382ff462013-06-24 16:52:45 +01001654#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001655
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001656 keyboard = weston_keyboard_create();
1657 if (keyboard == NULL) {
1658 weston_log("failed to allocate weston keyboard struct\n");
1659 return -1;
1660 }
1661
1662 seat->keyboard = keyboard;
1663 keyboard->seat = seat;
1664
1665 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001666
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001667 return 0;
1668}
1669
1670WL_EXPORT void
1671weston_seat_init_pointer(struct weston_seat *seat)
1672{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001673 struct weston_pointer *pointer;
1674
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001675 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001676 return;
1677
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001678 pointer = weston_pointer_create();
1679 if (pointer == NULL)
1680 return;
1681
1682 seat->pointer = pointer;
1683 pointer->seat = seat;
1684
1685 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686}
1687
1688WL_EXPORT void
1689weston_seat_init_touch(struct weston_seat *seat)
1690{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001691 struct weston_touch *touch;
1692
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001693 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001694 return;
1695
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001696 touch = weston_touch_create();
1697 if (touch == NULL)
1698 return;
1699
1700 seat->touch = touch;
1701 touch->seat = seat;
1702
1703 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704}
1705
1706WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001707weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1708 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001710 memset(seat, 0, sizeof *seat);
1711
Kristian Høgsberge3148752013-05-06 23:19:49 -04001712 seat->selection_data_source = NULL;
1713 wl_list_init(&seat->base_resource_list);
1714 wl_signal_init(&seat->selection_signal);
1715 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001716 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001717
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001718 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001719 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001721 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722 seat->modifier_state = 0;
1723 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001724 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001725
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726 wl_list_insert(ec->seat_list.prev, &seat->link);
1727
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001728 clipboard_create(seat);
1729
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001730 wl_signal_emit(&ec->seat_created_signal, seat);
1731}
1732
1733WL_EXPORT void
1734weston_seat_release(struct weston_seat *seat)
1735{
1736 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001737
Rob Bradford382ff462013-06-24 16:52:45 +01001738#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001739 if (seat->compositor->use_xkbcommon) {
1740 if (seat->xkb_state.state != NULL)
1741 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001742 if (seat->xkb_info)
1743 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001744 }
Rob Bradford382ff462013-06-24 16:52:45 +01001745#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001746
Kristian Høgsberge3148752013-05-06 23:19:49 -04001747 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001748 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001749 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001750 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001751 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001752 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001753
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001754 free (seat->seat_name);
1755
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001756 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001757
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001758 wl_signal_emit(&seat->destroy_signal, seat);
1759}