blob: e265efd394546af2e1efdb6cf6dcfb29fe3b83ec [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
Jonas Ådahl3042ffe2013-10-17 23:04:08 +020048WL_EXPORT void
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;
Jason Ekstranda7af7042013-10-12 22:38:11 -050099 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400100 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (pointer->button_count > 0)
103 return;
104
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 view = weston_compositor_pick_view(pointer->seat->compositor,
106 pointer->x, pointer->y,
107 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400108
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 if (pointer->focus != view)
110 weston_pointer_set_focus(pointer, view, 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) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 weston_view_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;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500136 struct weston_view *view;
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) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157 view = weston_compositor_pick_view(compositor,
158 pointer->x, pointer->y,
159 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400160
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400162 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163}
164
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200165static void
166default_grab_pointer_cancel(struct weston_pointer_grab *grab)
167{
168}
169
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400170static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400171 default_pointer_grab_interface = {
172 default_grab_focus,
173 default_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200174 default_grab_button,
175 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400176};
177
Kristian Høgsberge329f362013-05-06 22:19:57 -0400178static void
179default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
180 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400181{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400182 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100183 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400184 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100185 struct wl_resource *resource;
186 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400187
Neil Roberts96d790e2013-09-19 17:32:00 +0100188 resource_list = &touch->focus_resource_list;
189
190 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100192 wl_resource_for_each(resource, resource_list)
193 wl_touch_send_down(resource, serial, time,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500194 touch->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100195 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196 }
197}
198
Kristian Høgsberge329f362013-05-06 22:19:57 -0400199static void
200default_grab_touch_up(struct weston_touch_grab *grab,
201 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400202{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400203 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100204 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100206 struct wl_resource *resource;
207 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208
Neil Roberts96d790e2013-09-19 17:32:00 +0100209 resource_list = &touch->focus_resource_list;
210
211 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100213 wl_resource_for_each(resource, resource_list)
214 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215 }
216}
217
Kristian Høgsberge329f362013-05-06 22:19:57 -0400218static void
219default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
220 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400221{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400222 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100223 struct wl_resource *resource;
224 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400225
Neil Roberts96d790e2013-09-19 17:32:00 +0100226 resource_list = &touch->focus_resource_list;
227
228 wl_resource_for_each(resource, resource_list) {
229 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400230 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400231 }
232}
233
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200234static void
235default_grab_touch_cancel(struct weston_touch_grab *grab)
236{
237}
238
Kristian Høgsberge329f362013-05-06 22:19:57 -0400239static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400240 default_grab_touch_down,
241 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200242 default_grab_touch_motion,
243 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400244};
245
246static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400247default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400248 uint32_t time, uint32_t key, uint32_t state)
249{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400250 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400251 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100252 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100254 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255
Neil Roberts96d790e2013-09-19 17:32:00 +0100256 resource_list = &keyboard->focus_resource_list;
257 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100259 wl_resource_for_each(resource, resource_list)
260 wl_keyboard_send_key(resource,
261 serial,
262 time,
263 key,
264 state);
265 }
266}
267
268static void
269send_modifiers_to_resource(struct weston_keyboard *keyboard,
270 struct wl_resource *resource,
271 uint32_t serial)
272{
273 wl_keyboard_send_modifiers(resource,
274 serial,
275 keyboard->modifiers.mods_depressed,
276 keyboard->modifiers.mods_latched,
277 keyboard->modifiers.mods_locked,
278 keyboard->modifiers.group);
279}
280
281static void
282send_modifiers_to_client_in_list(struct wl_client *client,
283 struct wl_list *list,
284 uint32_t serial,
285 struct weston_keyboard *keyboard)
286{
287 struct wl_resource *resource;
288
289 wl_resource_for_each(resource, list) {
290 if (wl_resource_get_client(resource) == client)
291 send_modifiers_to_resource(keyboard,
292 resource,
293 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294 }
295}
296
297static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400298find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400299{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300 if (!surface)
301 return NULL;
302
Jason Ekstrand44a38632013-06-14 10:08:00 -0500303 if (!surface->resource)
304 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100305
Jason Ekstrand44a38632013-06-14 10:08:00 -0500306 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307}
308
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309static struct wl_resource *
310find_resource_for_view(struct wl_list *list, struct weston_view *view)
311{
312 if (!view)
313 return NULL;
314
315 return find_resource_for_surface(list, view->surface);
316}
317
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400318static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400319default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400320 uint32_t mods_depressed, uint32_t mods_latched,
321 uint32_t mods_locked, uint32_t group)
322{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400323 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100324 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
325 struct wl_resource *resource;
326 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327
Neil Roberts96d790e2013-09-19 17:32:00 +0100328 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400329
Neil Roberts96d790e2013-09-19 17:32:00 +0100330 wl_resource_for_each(resource, resource_list) {
331 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
332 mods_latched, mods_locked, group);
333 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 if (pointer && pointer->focus && pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100335 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100337 send_modifiers_to_client_in_list(pointer_client,
338 &keyboard->resource_list,
339 serial,
340 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341 }
342}
343
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200344static void
345default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
346{
347}
348
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400349static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400350 default_keyboard_grab_interface = {
351 default_grab_key,
352 default_grab_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200353 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354};
355
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400356static void
357pointer_unmap_sprite(struct weston_pointer *pointer)
358{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359 if (weston_surface_is_mapped(pointer->sprite->surface))
360 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400361
362 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 pointer->sprite->surface->configure = NULL;
364 pointer->sprite->surface->configure_private = NULL;
365 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400366 pointer->sprite = NULL;
367}
368
369static void
370pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
371{
372 struct weston_pointer *pointer =
373 container_of(listener, struct weston_pointer,
374 sprite_destroy_listener);
375
376 pointer->sprite = NULL;
377}
378
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400379WL_EXPORT struct weston_pointer *
380weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400381{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400382 struct weston_pointer *pointer;
383
Peter Huttererf3d62272013-08-08 11:57:05 +1000384 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400385 if (pointer == NULL)
386 return NULL;
387
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400388 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100389 wl_list_init(&pointer->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400390 pointer->default_grab.interface = &default_pointer_grab_interface;
391 pointer->default_grab.pointer = pointer;
392 pointer->grab = &pointer->default_grab;
393 wl_signal_init(&pointer->focus_signal);
394
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400395 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
396
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397 /* FIXME: Pick better co-ords. */
398 pointer->x = wl_fixed_from_int(100);
399 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400400
401 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402}
403
404WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400405weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400407 if (pointer->sprite)
408 pointer_unmap_sprite(pointer);
409
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100411
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400412 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400413}
414
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400415WL_EXPORT struct weston_keyboard *
416weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400417{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400418 struct weston_keyboard *keyboard;
419
Peter Huttererf3d62272013-08-08 11:57:05 +1000420 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400421 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100422 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400423
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100425 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400427 keyboard->default_grab.interface = &default_keyboard_grab_interface;
428 keyboard->default_grab.keyboard = keyboard;
429 keyboard->grab = &keyboard->default_grab;
430 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400431
432 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433}
434
435WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400436weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400437{
438 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100439
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400440 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400441 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442}
443
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400444WL_EXPORT struct weston_touch *
445weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400446{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400447 struct weston_touch *touch;
448
Peter Huttererf3d62272013-08-08 11:57:05 +1000449 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400450 if (touch == NULL)
451 return NULL;
452
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400453 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100454 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455 touch->default_grab.interface = &default_touch_grab_interface;
456 touch->default_grab.touch = touch;
457 touch->grab = &touch->default_grab;
458 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400459
460 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400461}
462
463WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400464weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465{
466 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100467
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400468 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400469}
470
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400471static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400472seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400473{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400474 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100475 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200477 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200479 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400480 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200481 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482 caps |= WL_SEAT_CAPABILITY_TOUCH;
483
Rob Bradford6e737f52013-09-06 17:48:19 +0100484 wl_resource_for_each(resource, &seat->base_resource_list) {
485 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500486 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487}
488
489WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400490weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400492 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400494 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100495 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100496 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100498 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500499 int different_surface = 0;
500
501 if ((!pointer->focus && view) ||
502 (pointer->focus && !view) ||
503 (pointer->focus && pointer->focus->surface != view->surface))
504 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505
Neil Roberts96d790e2013-09-19 17:32:00 +0100506 focus_resource_list = &pointer->focus_resource_list;
507
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400509 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100510 wl_resource_for_each(resource, focus_resource_list) {
511 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500512 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100513 }
514
515 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400516 }
517
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 if (find_resource_for_view(&pointer->resource_list, view) &&
519 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100520 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100522
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400523 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100524
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700526 send_modifiers_to_client_in_list(surface_client,
527 &kbd->resource_list,
528 serial,
529 kbd);
530
Neil Roberts96d790e2013-09-19 17:32:00 +0100531 move_resources_for_client(focus_resource_list,
532 &pointer->resource_list,
533 surface_client);
534
535 wl_resource_for_each(resource, focus_resource_list) {
536 wl_pointer_send_enter(resource,
537 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100539 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100541
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400542 pointer->focus_serial = serial;
543 }
544
Jason Ekstranda7af7042013-10-12 22:38:11 -0500545 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546 wl_signal_emit(&pointer->focus_signal, pointer);
547}
548
Neil Roberts96d790e2013-09-19 17:32:00 +0100549static void
550send_enter_to_resource_list(struct wl_list *list,
551 struct weston_keyboard *keyboard,
552 struct weston_surface *surface,
553 uint32_t serial)
554{
555 struct wl_resource *resource;
556
557 wl_resource_for_each(resource, list) {
558 send_modifiers_to_resource(keyboard, resource, serial);
559 wl_keyboard_send_enter(resource, serial,
560 surface->resource,
561 &keyboard->keys);
562 }
563}
564
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400565WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400566weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400567 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400568{
569 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100570 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400571 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100572 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573
Neil Roberts96d790e2013-09-19 17:32:00 +0100574 focus_resource_list = &keyboard->focus_resource_list;
575
576 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100578 wl_resource_for_each(resource, focus_resource_list) {
579 wl_keyboard_send_leave(resource, serial,
580 keyboard->focus->resource);
581 }
582 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400583 }
584
Neil Roberts96d790e2013-09-19 17:32:00 +0100585 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
586 keyboard->focus != surface) {
587 struct wl_client *surface_client =
588 wl_resource_get_client(surface->resource);
589
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400590 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100591
592 move_resources_for_client(focus_resource_list,
593 &keyboard->resource_list,
594 surface_client);
595 send_enter_to_resource_list(focus_resource_list,
596 keyboard,
597 surface,
598 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400599 keyboard->focus_serial = serial;
600 }
601
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400602 keyboard->focus = surface;
603 wl_signal_emit(&keyboard->focus_signal, keyboard);
604}
605
606WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400607weston_keyboard_start_grab(struct weston_keyboard *keyboard,
608 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400609{
610 keyboard->grab = grab;
611 grab->keyboard = keyboard;
612
613 /* XXX focus? */
614}
615
616WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400617weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400618{
619 keyboard->grab = &keyboard->default_grab;
620}
621
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200622static void
623weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
624{
625 keyboard->grab->interface->cancel(keyboard->grab);
626}
627
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400628WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400629weston_pointer_start_grab(struct weston_pointer *pointer,
630 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400631{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400632 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400634 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635}
636
637WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400638weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400639{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400641 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400642}
643
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200644static void
645weston_pointer_cancel_grab(struct weston_pointer *pointer)
646{
647 pointer->grab->interface->cancel(pointer->grab);
648}
649
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400650WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400651weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652{
653 touch->grab = grab;
654 grab->touch = touch;
655}
656
657WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400658weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400659{
660 touch->grab = &touch->default_grab;
661}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400662
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200663static void
664weston_touch_cancel_grab(struct weston_touch *touch)
665{
666 touch->grab->interface->cancel(touch->grab);
667}
668
Rob Bradford806d8c02013-06-25 18:56:41 +0100669WL_EXPORT void
670weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400671{
Rob Bradford806d8c02013-06-25 18:56:41 +0100672 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400673 struct weston_output *output, *prev = NULL;
674 int x, y, old_x, old_y, valid = 0;
675
676 x = wl_fixed_to_int(*fx);
677 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100678 old_x = wl_fixed_to_int(pointer->x);
679 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400680
681 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100682 if (pointer->seat->output && pointer->seat->output != output)
683 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400684 if (pixman_region32_contains_point(&output->region,
685 x, y, NULL))
686 valid = 1;
687 if (pixman_region32_contains_point(&output->region,
688 old_x, old_y, NULL))
689 prev = output;
690 }
691
Rob Bradford66bd9f52013-06-25 18:56:42 +0100692 if (!prev)
693 prev = pointer->seat->output;
694
695 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400696 if (x < prev->x)
697 *fx = wl_fixed_from_int(prev->x);
698 else if (x >= prev->x + prev->width)
699 *fx = wl_fixed_from_int(prev->x +
700 prev->width - 1);
701 if (y < prev->y)
702 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200703 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400704 *fy = wl_fixed_from_int(prev->y +
705 prev->height - 1);
706 }
707}
708
709/* Takes absolute values */
710static void
711move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
712{
713 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400714 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400715 struct weston_output *output;
716 int32_t ix, iy;
717
Rob Bradford806d8c02013-06-25 18:56:41 +0100718 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400719
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400720 pointer->x = x;
721 pointer->y = y;
722
723 ix = wl_fixed_to_int(x);
724 iy = wl_fixed_to_int(y);
725
726 wl_list_for_each(output, &ec->output_list, link)
727 if (output->zoom.active &&
728 pixman_region32_contains_point(&output->region,
729 ix, iy, NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500730 weston_output_update_zoom(output);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400731
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400732 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 weston_view_set_position(pointer->sprite,
734 ix - pointer->hotspot_x,
735 iy - pointer->hotspot_y);
736 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400737 }
738}
739
740WL_EXPORT void
741notify_motion(struct weston_seat *seat,
742 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
743{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400744 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400745 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400746
747 weston_compositor_wake(ec);
748
749 move_pointer(seat, pointer->x + dx, pointer->y + dy);
750
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400751 pointer->grab->interface->focus(pointer->grab);
752 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400753}
754
755WL_EXPORT void
756notify_motion_absolute(struct weston_seat *seat,
757 uint32_t time, wl_fixed_t x, wl_fixed_t y)
758{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400759 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400760 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400761
762 weston_compositor_wake(ec);
763
764 move_pointer(seat, x, y);
765
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400766 pointer->grab->interface->focus(pointer->grab);
767 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400768}
769
770WL_EXPORT void
771weston_surface_activate(struct weston_surface *surface,
772 struct weston_seat *seat)
773{
774 struct weston_compositor *compositor = seat->compositor;
775
Kristian Høgsberge3148752013-05-06 23:19:49 -0400776 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400777 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400778 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400779 }
780
781 wl_signal_emit(&compositor->activate_signal, surface);
782}
783
784WL_EXPORT void
785notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
786 enum wl_pointer_button_state state)
787{
788 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400789 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400790 struct weston_surface *focus =
791 (struct weston_surface *) pointer->focus;
792 uint32_t serial = wl_display_next_serial(compositor->wl_display);
793
794 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
795 if (compositor->ping_handler && focus)
796 compositor->ping_handler(focus, serial);
797 weston_compositor_idle_inhibit(compositor);
798 if (pointer->button_count == 0) {
799 pointer->grab_button = button;
800 pointer->grab_time = time;
801 pointer->grab_x = pointer->x;
802 pointer->grab_y = pointer->y;
803 }
804 pointer->button_count++;
805 } else {
806 weston_compositor_idle_release(compositor);
807 pointer->button_count--;
808 }
809
810 weston_compositor_run_button_binding(compositor, seat, time, button,
811 state);
812
813 pointer->grab->interface->button(pointer->grab, time, button, state);
814
815 if (pointer->button_count == 1)
816 pointer->grab_serial =
817 wl_display_get_serial(compositor->wl_display);
818}
819
820WL_EXPORT void
821notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
822 wl_fixed_t value)
823{
824 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400825 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400826 struct weston_surface *focus =
827 (struct weston_surface *) pointer->focus;
828 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100829 struct wl_resource *resource;
830 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400831
832 if (compositor->ping_handler && focus)
833 compositor->ping_handler(focus, serial);
834
835 weston_compositor_wake(compositor);
836
837 if (!value)
838 return;
839
840 if (weston_compositor_run_axis_binding(compositor, seat,
841 time, axis, value))
842 return;
843
Neil Roberts96d790e2013-09-19 17:32:00 +0100844 resource_list = &pointer->focus_resource_list;
845 wl_resource_for_each(resource, resource_list)
846 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400847 value);
848}
849
Rob Bradford382ff462013-06-24 16:52:45 +0100850#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400851WL_EXPORT void
852notify_modifiers(struct weston_seat *seat, uint32_t serial)
853{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400854 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400855 struct weston_keyboard_grab *grab = keyboard->grab;
856 uint32_t mods_depressed, mods_latched, mods_locked, group;
857 uint32_t mods_lookup;
858 enum weston_led leds = 0;
859 int changed = 0;
860
861 /* Serialize and update our internal state, checking to see if it's
862 * different to the previous state. */
863 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
864 XKB_STATE_DEPRESSED);
865 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
866 XKB_STATE_LATCHED);
867 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
868 XKB_STATE_LOCKED);
869 group = xkb_state_serialize_group(seat->xkb_state.state,
870 XKB_STATE_EFFECTIVE);
871
Kristian Høgsberge3148752013-05-06 23:19:49 -0400872 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
873 mods_latched != seat->keyboard->modifiers.mods_latched ||
874 mods_locked != seat->keyboard->modifiers.mods_locked ||
875 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400876 changed = 1;
877
Kristian Høgsberge3148752013-05-06 23:19:49 -0400878 seat->keyboard->modifiers.mods_depressed = mods_depressed;
879 seat->keyboard->modifiers.mods_latched = mods_latched;
880 seat->keyboard->modifiers.mods_locked = mods_locked;
881 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400882
883 /* And update the modifier_state for bindings. */
884 mods_lookup = mods_depressed | mods_latched;
885 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000886 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400887 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000888 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400889 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000890 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400891 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000892 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400893 seat->modifier_state |= MODIFIER_SHIFT;
894
895 /* Finally, notify the compositor that LEDs have changed. */
896 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000897 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400898 leds |= LED_NUM_LOCK;
899 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000900 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400901 leds |= LED_CAPS_LOCK;
902 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000903 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400904 leds |= LED_SCROLL_LOCK;
905 if (leds != seat->xkb_state.leds && seat->led_update)
906 seat->led_update(seat, leds);
907 seat->xkb_state.leds = leds;
908
909 if (changed) {
910 grab->interface->modifiers(grab,
911 serial,
912 keyboard->modifiers.mods_depressed,
913 keyboard->modifiers.mods_latched,
914 keyboard->modifiers.mods_locked,
915 keyboard->modifiers.group);
916 }
917}
918
919static void
920update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
921 enum wl_keyboard_key_state state)
922{
923 enum xkb_key_direction direction;
924
Matt Roper01a92732013-06-24 16:52:44 +0100925 /* Keyboard modifiers don't exist in raw keyboard mode */
926 if (!seat->compositor->use_xkbcommon)
927 return;
928
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400929 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
930 direction = XKB_KEY_DOWN;
931 else
932 direction = XKB_KEY_UP;
933
934 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
935 * broken keycode system, which starts at 8. */
936 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
937
938 notify_modifiers(seat, serial);
939}
Rui Matos65196bc2013-10-10 19:44:19 +0200940
941static void
942send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
943{
944 wl_keyboard_send_keymap(resource,
945 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
946 xkb_info->keymap_fd,
947 xkb_info->keymap_size);
948}
949
950static void
951send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
952{
953 wl_keyboard_send_modifiers(resource, serial,
954 keyboard->modifiers.mods_depressed,
955 keyboard->modifiers.mods_latched,
956 keyboard->modifiers.mods_locked,
957 keyboard->modifiers.group);
958}
959
960static struct weston_xkb_info *
961weston_xkb_info_create(struct xkb_keymap *keymap);
962static void
963weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
964
965static void
966update_keymap(struct weston_seat *seat)
967{
968 struct wl_resource *resource;
969 struct weston_xkb_info *xkb_info;
970 struct xkb_state *state;
971 xkb_mod_mask_t latched_mods;
972 xkb_mod_mask_t locked_mods;
973
974 xkb_info = weston_xkb_info_create(seat->pending_keymap);
975
976 xkb_keymap_unref(seat->pending_keymap);
977 seat->pending_keymap = NULL;
978
979 if (!xkb_info) {
980 weston_log("failed to create XKB info\n");
981 return;
982 }
983
984 state = xkb_state_new(xkb_info->keymap);
985 if (!state) {
986 weston_log("failed to initialise XKB state\n");
987 weston_xkb_info_destroy(xkb_info);
988 return;
989 }
990
991 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
992 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
993 xkb_state_update_mask(state,
994 0, /* depressed */
995 latched_mods,
996 locked_mods,
997 0, 0, 0);
998
999 weston_xkb_info_destroy(seat->xkb_info);
1000 seat->xkb_info = xkb_info;
1001
1002 xkb_state_unref(seat->xkb_state.state);
1003 seat->xkb_state.state = state;
1004
1005 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1006 send_keymap(resource, xkb_info);
1007 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1008 send_keymap(resource, xkb_info);
1009
1010 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1011
1012 if (!latched_mods && !locked_mods)
1013 return;
1014
1015 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1016 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1017 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1018 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1019}
Rob Bradford382ff462013-06-24 16:52:45 +01001020#else
1021WL_EXPORT void
1022notify_modifiers(struct weston_seat *seat, uint32_t serial)
1023{
1024}
1025
1026static void
1027update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1028 enum wl_keyboard_key_state state)
1029{
1030}
Rui Matos65196bc2013-10-10 19:44:19 +02001031
1032static void
1033update_keymap(struct weston_seat *seat)
1034{
1035}
Rob Bradford382ff462013-06-24 16:52:45 +01001036#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001037
1038WL_EXPORT void
1039notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1040 enum wl_keyboard_key_state state,
1041 enum weston_key_state_update update_state)
1042{
1043 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001044 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001046 struct weston_keyboard_grab *grab = keyboard->grab;
1047 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1048 uint32_t *k, *end;
1049
1050 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1051 if (compositor->ping_handler && focus)
1052 compositor->ping_handler(focus, serial);
1053
1054 weston_compositor_idle_inhibit(compositor);
1055 keyboard->grab_key = key;
1056 keyboard->grab_time = time;
1057 } else {
1058 weston_compositor_idle_release(compositor);
1059 }
1060
1061 end = keyboard->keys.data + keyboard->keys.size;
1062 for (k = keyboard->keys.data; k < end; k++) {
1063 if (*k == key) {
1064 /* Ignore server-generated repeats. */
1065 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1066 return;
1067 *k = *--end;
1068 }
1069 }
1070 keyboard->keys.size = (void *) end - keyboard->keys.data;
1071 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1072 k = wl_array_add(&keyboard->keys, sizeof *k);
1073 *k = key;
1074 }
1075
1076 if (grab == &keyboard->default_grab ||
1077 grab == &keyboard->input_method_grab) {
1078 weston_compositor_run_key_binding(compositor, seat, time, key,
1079 state);
1080 grab = keyboard->grab;
1081 }
1082
1083 grab->interface->key(grab, time, key, state);
1084
Rui Matos65196bc2013-10-10 19:44:19 +02001085 if (seat->pending_keymap &&
1086 keyboard->keys.size == 0)
1087 update_keymap(seat);
1088
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001089 if (update_state == STATE_UPDATE_AUTOMATIC) {
1090 update_modifier_state(seat,
1091 wl_display_get_serial(compositor->wl_display),
1092 key,
1093 state);
1094 }
1095}
1096
1097WL_EXPORT void
1098notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1099 wl_fixed_t x, wl_fixed_t y)
1100{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001101 if (output) {
1102 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001103 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001104 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001105 * NULL) here, but somehow that breaks re-entry... */
1106 }
1107}
1108
1109static void
1110destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1111{
1112 struct weston_seat *ws;
1113
1114 ws = container_of(listener, struct weston_seat,
1115 saved_kbd_focus_listener);
1116
1117 ws->saved_kbd_focus = NULL;
1118}
1119
1120WL_EXPORT void
1121notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1122 enum weston_key_state_update update_state)
1123{
1124 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001125 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001126 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001127 uint32_t *k, serial;
1128
1129 serial = wl_display_next_serial(compositor->wl_display);
1130 wl_array_copy(&keyboard->keys, keys);
1131 wl_array_for_each(k, &keyboard->keys) {
1132 weston_compositor_idle_inhibit(compositor);
1133 if (update_state == STATE_UPDATE_AUTOMATIC)
1134 update_modifier_state(seat, serial, *k,
1135 WL_KEYBOARD_KEY_STATE_PRESSED);
1136 }
1137
1138 /* Run key bindings after we've updated the state. */
1139 wl_array_for_each(k, &keyboard->keys) {
1140 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1141 WL_KEYBOARD_KEY_STATE_PRESSED);
1142 }
1143
1144 surface = seat->saved_kbd_focus;
1145
1146 if (surface) {
1147 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1148 weston_keyboard_set_focus(keyboard, surface);
1149 seat->saved_kbd_focus = NULL;
1150 }
1151}
1152
1153WL_EXPORT void
1154notify_keyboard_focus_out(struct weston_seat *seat)
1155{
1156 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001157 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001158 uint32_t *k, serial;
1159
1160 serial = wl_display_next_serial(compositor->wl_display);
1161 wl_array_for_each(k, &keyboard->keys) {
1162 weston_compositor_idle_release(compositor);
1163 update_modifier_state(seat, serial, *k,
1164 WL_KEYBOARD_KEY_STATE_RELEASED);
1165 }
1166
1167 seat->modifier_state = 0;
1168
1169 if (keyboard->focus) {
1170 seat->saved_kbd_focus = keyboard->focus;
1171 seat->saved_kbd_focus_listener.notify =
1172 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001173 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001174 &seat->saved_kbd_focus_listener);
1175 }
1176
1177 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001178 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001179}
1180
Michael Fua2bb7912013-07-23 15:51:06 +08001181WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001183{
Neil Roberts96d790e2013-09-19 17:32:00 +01001184 struct wl_list *focus_resource_list;
1185
1186 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001188 if (view && seat->touch->focus &&
1189 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001191 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193
Neil Roberts96d790e2013-09-19 17:32:00 +01001194 if (!wl_list_empty(focus_resource_list)) {
1195 move_resources(&seat->touch->resource_list,
1196 focus_resource_list);
1197 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001200 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001202 move_resources_for_client(focus_resource_list,
1203 &seat->touch->resource_list,
1204 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001205 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207}
1208
1209/**
1210 * notify_touch - emulates button touches and notifies surfaces accordingly.
1211 *
1212 * It assumes always the correct cycle sequence until it gets here: touch_down
1213 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1214 * for sending along such order.
1215 *
1216 */
1217WL_EXPORT void
1218notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1219 wl_fixed_t x, wl_fixed_t y, int touch_type)
1220{
1221 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001222 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001223 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001225 wl_fixed_t sx, sy;
1226
1227 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001228 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1229 touch->grab_x = x;
1230 touch->grab_y = y;
1231 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232
1233 switch (touch_type) {
1234 case WL_TOUCH_DOWN:
1235 weston_compositor_idle_inhibit(ec);
1236
1237 seat->num_tp++;
1238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 /* the first finger down picks the view, and all further go
1240 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001241 * until all touch points are up again. */
1242 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1244 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 ev = touch->focus;
1247 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001248 } else {
1249 /* Unexpected condition: We have non-initial touch but
1250 * there is no focused surface.
1251 */
1252 weston_log("touch event received with %d points down"
1253 "but no surface focused\n", seat->num_tp);
1254 return;
1255 }
1256
1257 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001258 if (seat->num_tp == 1) {
1259 touch->grab_serial =
1260 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001261 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001262 touch->grab_time = time;
1263 touch->grab_x = x;
1264 touch->grab_y = y;
1265 }
1266
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001267 break;
1268 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 ev = touch->focus;
1270 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271 break;
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274 grab->interface->motion(grab, time, touch_id, sx, sy);
1275 break;
1276 case WL_TOUCH_UP:
1277 weston_compositor_idle_release(ec);
1278 seat->num_tp--;
1279
1280 grab->interface->up(grab, time, touch_id);
1281 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001282 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001283 break;
1284 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001285
1286 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001287}
1288
1289static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001290pointer_cursor_surface_configure(struct weston_surface *es,
1291 int32_t dx, int32_t dy, int32_t width, int32_t height)
1292{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001293 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001294 int x, y;
1295
1296 if (width == 0)
1297 return;
1298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001300
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001301 pointer->hotspot_x -= dx;
1302 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001303
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001304 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1305 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001308
1309 empty_region(&es->pending.input);
1310
1311 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 wl_list_insert(&es->compositor->cursor_layer.view_list,
1313 &pointer->sprite->layer_link);
1314 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001315 }
1316}
1317
1318static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001319pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1320 uint32_t serial, struct wl_resource *surface_resource,
1321 int32_t x, int32_t y)
1322{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001323 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001324 struct weston_surface *surface = NULL;
1325
1326 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001327 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001329 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001330 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001332 black_surface used in shell.c for fullscreen don't have
1333 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001335 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001337 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001338 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001339 return;
1340
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001342 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001343 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001344 WL_DISPLAY_ERROR_INVALID_OBJECT,
1345 "surface->configure already "
1346 "set");
1347 return;
1348 }
1349 }
1350
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001351 if (pointer->sprite)
1352 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001353
1354 if (!surface)
1355 return;
1356
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001357 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001358 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001359
1360 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001361 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001363 pointer->hotspot_x = x;
1364 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001365
1366 if (surface->buffer_ref.buffer)
1367 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1368 weston_surface_buffer_height(surface));
1369}
1370
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001371static void
1372pointer_release(struct wl_client *client, struct wl_resource *resource)
1373{
1374 wl_resource_destroy(resource);
1375}
1376
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001377static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001378 pointer_set_cursor,
1379 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001380};
1381
1382static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001383seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1384 uint32_t id)
1385{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001386 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001387 struct wl_resource *cr;
1388
Kristian Høgsberge3148752013-05-06 23:19:49 -04001389 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001390 return;
1391
Jason Ekstranda85118c2013-06-27 20:17:02 -05001392 cr = wl_resource_create(client, &wl_pointer_interface,
1393 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001394 if (cr == NULL) {
1395 wl_client_post_no_memory(client);
1396 return;
1397 }
1398
Neil Roberts96d790e2013-09-19 17:32:00 +01001399 /* May be moved to focused list later by either
1400 * weston_pointer_set_focus or directly if this client is already
1401 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001402 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001403 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1404 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1407 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001408 wl_fixed_t sx, sy;
1409
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 weston_view_from_global_fixed(seat->pointer->focus,
1411 seat->pointer->x,
1412 seat->pointer->y,
1413 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001414
1415 wl_list_remove(wl_resource_get_link(cr));
1416 wl_list_insert(&seat->pointer->focus_resource_list,
1417 wl_resource_get_link(cr));
1418 wl_pointer_send_enter(cr,
1419 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001421 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001422 }
1423}
1424
1425static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001426keyboard_release(struct wl_client *client, struct wl_resource *resource)
1427{
1428 wl_resource_destroy(resource);
1429}
1430
1431static const struct wl_keyboard_interface keyboard_interface = {
1432 keyboard_release
1433};
1434
Neil Roberts96d790e2013-09-19 17:32:00 +01001435static int
1436should_send_modifiers_to_client(struct weston_seat *seat,
1437 struct wl_client *client)
1438{
1439 if (seat->keyboard &&
1440 seat->keyboard->focus &&
1441 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1442 return 1;
1443
1444 if (seat->pointer &&
1445 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001447 return 1;
1448
1449 return 0;
1450}
1451
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001452static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001453seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1454 uint32_t id)
1455{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001456 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001457 struct wl_resource *cr;
1458
Kristian Høgsberge3148752013-05-06 23:19:49 -04001459 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001460 return;
1461
Jason Ekstranda85118c2013-06-27 20:17:02 -05001462 cr = wl_resource_create(client, &wl_keyboard_interface,
1463 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001464 if (cr == NULL) {
1465 wl_client_post_no_memory(client);
1466 return;
1467 }
1468
Neil Roberts96d790e2013-09-19 17:32:00 +01001469 /* May be moved to focused list later by either
1470 * weston_keyboard_set_focus or directly if this client is already
1471 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001472 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001473 wl_resource_set_implementation(cr, &keyboard_interface,
1474 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001475
Matt Roper01a92732013-06-24 16:52:44 +01001476 if (seat->compositor->use_xkbcommon) {
1477 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001478 seat->xkb_info->keymap_fd,
1479 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001480 } else {
1481 int null_fd = open("/dev/null", O_RDONLY);
1482 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1483 null_fd,
1484 0);
1485 close(null_fd);
1486 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487
Neil Roberts96d790e2013-09-19 17:32:00 +01001488 if (should_send_modifiers_to_client(seat, client)) {
1489 send_modifiers_to_resource(seat->keyboard,
1490 cr,
1491 seat->keyboard->focus_serial);
1492 }
1493
Kristian Høgsberge3148752013-05-06 23:19:49 -04001494 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001495 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001496 struct weston_surface *surface =
1497 (struct weston_surface *) seat->keyboard->focus;
1498
1499 wl_list_remove(wl_resource_get_link(cr));
1500 wl_list_insert(&seat->keyboard->focus_resource_list,
1501 wl_resource_get_link(cr));
1502 wl_keyboard_send_enter(cr,
1503 seat->keyboard->focus_serial,
1504 surface->resource,
1505 &seat->keyboard->keys);
1506
1507 /* If this is the first keyboard resource for this
1508 * client... */
1509 if (seat->keyboard->focus_resource_list.prev ==
1510 wl_resource_get_link(cr))
1511 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001512 }
1513}
1514
1515static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001516touch_release(struct wl_client *client, struct wl_resource *resource)
1517{
1518 wl_resource_destroy(resource);
1519}
1520
1521static const struct wl_touch_interface touch_interface = {
1522 touch_release
1523};
1524
1525static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001526seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1527 uint32_t id)
1528{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001529 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001530 struct wl_resource *cr;
1531
Kristian Høgsberge3148752013-05-06 23:19:49 -04001532 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001533 return;
1534
Jason Ekstranda85118c2013-06-27 20:17:02 -05001535 cr = wl_resource_create(client, &wl_touch_interface,
1536 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001537 if (cr == NULL) {
1538 wl_client_post_no_memory(client);
1539 return;
1540 }
1541
Neil Roberts96d790e2013-09-19 17:32:00 +01001542 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001544 wl_list_insert(&seat->touch->resource_list,
1545 wl_resource_get_link(cr));
1546 } else {
1547 wl_list_insert(&seat->touch->focus_resource_list,
1548 wl_resource_get_link(cr));
1549 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001550 wl_resource_set_implementation(cr, &touch_interface,
1551 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001552}
1553
1554static const struct wl_seat_interface seat_interface = {
1555 seat_get_pointer,
1556 seat_get_keyboard,
1557 seat_get_touch,
1558};
1559
1560static void
1561bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1562{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001563 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001564 struct wl_resource *resource;
1565 enum wl_seat_capability caps = 0;
1566
Jason Ekstranda85118c2013-06-27 20:17:02 -05001567 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001568 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001569 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001570 wl_resource_set_implementation(resource, &seat_interface, data,
1571 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001572
1573 if (seat->pointer)
1574 caps |= WL_SEAT_CAPABILITY_POINTER;
1575 if (seat->keyboard)
1576 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1577 if (seat->touch)
1578 caps |= WL_SEAT_CAPABILITY_TOUCH;
1579
1580 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001581 if (version >= 2)
1582 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001583}
1584
Rob Bradford382ff462013-06-24 16:52:45 +01001585#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001586int
1587weston_compositor_xkb_init(struct weston_compositor *ec,
1588 struct xkb_rule_names *names)
1589{
Rob Bradford382ff462013-06-24 16:52:45 +01001590 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001591
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001592 if (ec->xkb_context == NULL) {
1593 ec->xkb_context = xkb_context_new(0);
1594 if (ec->xkb_context == NULL) {
1595 weston_log("failed to create XKB context\n");
1596 return -1;
1597 }
1598 }
1599
1600 if (names)
1601 ec->xkb_names = *names;
1602 if (!ec->xkb_names.rules)
1603 ec->xkb_names.rules = strdup("evdev");
1604 if (!ec->xkb_names.model)
1605 ec->xkb_names.model = strdup("pc105");
1606 if (!ec->xkb_names.layout)
1607 ec->xkb_names.layout = strdup("us");
1608
1609 return 0;
1610}
1611
Stefan Schmidtfda26522013-09-17 10:54:09 +01001612static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001613weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001615 if (--xkb_info->ref_count > 0)
1616 return;
1617
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001618 if (xkb_info->keymap)
1619 xkb_map_unref(xkb_info->keymap);
1620
1621 if (xkb_info->keymap_area)
1622 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1623 if (xkb_info->keymap_fd >= 0)
1624 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001625 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001626}
1627
1628void
1629weston_compositor_xkb_destroy(struct weston_compositor *ec)
1630{
Matt Roper01a92732013-06-24 16:52:44 +01001631 /*
1632 * If we're operating in raw keyboard mode, we never initialized
1633 * libxkbcommon so there's no cleanup to do either.
1634 */
1635 if (!ec->use_xkbcommon)
1636 return;
1637
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001638 free((char *) ec->xkb_names.rules);
1639 free((char *) ec->xkb_names.model);
1640 free((char *) ec->xkb_names.layout);
1641 free((char *) ec->xkb_names.variant);
1642 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001643
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001644 if (ec->xkb_info)
1645 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001646 xkb_context_unref(ec->xkb_context);
1647}
1648
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001649static struct weston_xkb_info *
1650weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001651{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001652 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1653 if (xkb_info == NULL)
1654 return NULL;
1655
1656 xkb_info->keymap = xkb_map_ref(keymap);
1657 xkb_info->ref_count = 1;
1658
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001659 char *keymap_str;
1660
1661 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1662 XKB_MOD_NAME_SHIFT);
1663 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1664 XKB_MOD_NAME_CAPS);
1665 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1666 XKB_MOD_NAME_CTRL);
1667 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1668 XKB_MOD_NAME_ALT);
1669 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1670 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1671 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1672 XKB_MOD_NAME_LOGO);
1673 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1674
1675 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1676 XKB_LED_NAME_NUM);
1677 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1678 XKB_LED_NAME_CAPS);
1679 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1680 XKB_LED_NAME_SCROLL);
1681
1682 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1683 if (keymap_str == NULL) {
1684 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001685 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686 }
1687 xkb_info->keymap_size = strlen(keymap_str) + 1;
1688
1689 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1690 if (xkb_info->keymap_fd < 0) {
1691 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1692 (unsigned long) xkb_info->keymap_size);
1693 goto err_keymap_str;
1694 }
1695
1696 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1697 PROT_READ | PROT_WRITE,
1698 MAP_SHARED, xkb_info->keymap_fd, 0);
1699 if (xkb_info->keymap_area == MAP_FAILED) {
1700 weston_log("failed to mmap() %lu bytes\n",
1701 (unsigned long) xkb_info->keymap_size);
1702 goto err_dev_zero;
1703 }
1704 strcpy(xkb_info->keymap_area, keymap_str);
1705 free(keymap_str);
1706
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001707 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708
1709err_dev_zero:
1710 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001711err_keymap_str:
1712 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001713err_keymap:
1714 xkb_map_unref(xkb_info->keymap);
1715 free(xkb_info);
1716 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001717}
1718
1719static int
1720weston_compositor_build_global_keymap(struct weston_compositor *ec)
1721{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001722 struct xkb_keymap *keymap;
1723
1724 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001725 return 0;
1726
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001727 keymap = xkb_map_new_from_names(ec->xkb_context,
1728 &ec->xkb_names,
1729 0);
1730 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001731 weston_log("failed to compile global XKB keymap\n");
1732 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1733 "options %s\n",
1734 ec->xkb_names.rules, ec->xkb_names.model,
1735 ec->xkb_names.layout, ec->xkb_names.variant,
1736 ec->xkb_names.options);
1737 return -1;
1738 }
1739
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001740 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001741 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001742 return -1;
1743
1744 return 0;
1745}
Rob Bradford382ff462013-06-24 16:52:45 +01001746#else
1747int
1748weston_compositor_xkb_init(struct weston_compositor *ec,
1749 struct xkb_rule_names *names)
1750{
1751 return 0;
1752}
1753
1754void
1755weston_compositor_xkb_destroy(struct weston_compositor *ec)
1756{
1757}
1758#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001759
Rui Matos65196bc2013-10-10 19:44:19 +02001760WL_EXPORT void
1761weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1762{
1763 if (!seat->keyboard || !keymap)
1764 return;
1765
1766#ifdef ENABLE_XKBCOMMON
1767 if (!seat->compositor->use_xkbcommon)
1768 return;
1769
1770 xkb_keymap_unref(seat->pending_keymap);
1771 seat->pending_keymap = xkb_keymap_ref(keymap);
1772
1773 if (seat->keyboard->keys.size == 0)
1774 update_keymap(seat);
1775#endif
1776}
1777
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778WL_EXPORT int
1779weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1780{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001781 struct weston_keyboard *keyboard;
1782
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001783 if (seat->keyboard) {
1784 seat->keyboard_device_count += 1;
1785 if (seat->keyboard_device_count == 1)
1786 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001788 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001789
Rob Bradford382ff462013-06-24 16:52:45 +01001790#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001791 if (seat->compositor->use_xkbcommon) {
1792 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001793 seat->xkb_info = weston_xkb_info_create(keymap);
1794 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001795 return -1;
1796 } else {
1797 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1798 return -1;
1799 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001800 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001801 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001802
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001803 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001804 if (seat->xkb_state.state == NULL) {
1805 weston_log("failed to initialise XKB state\n");
1806 return -1;
1807 }
1808
1809 seat->xkb_state.leds = 0;
1810 }
Rob Bradford382ff462013-06-24 16:52:45 +01001811#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001812
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001813 keyboard = weston_keyboard_create();
1814 if (keyboard == NULL) {
1815 weston_log("failed to allocate weston keyboard struct\n");
1816 return -1;
1817 }
1818
1819 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001820 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001821 keyboard->seat = seat;
1822
1823 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001824
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825 return 0;
1826}
1827
1828WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001829weston_seat_release_keyboard(struct weston_seat *seat)
1830{
1831 seat->keyboard_device_count--;
1832 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001833 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001834 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001835 seat_send_updated_caps(seat);
1836 }
1837}
1838
1839WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001840weston_seat_init_pointer(struct weston_seat *seat)
1841{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001842 struct weston_pointer *pointer;
1843
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001844 if (seat->pointer) {
1845 seat->pointer_device_count += 1;
1846 if (seat->pointer_device_count == 1)
1847 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001848 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001849 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001850
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001851 pointer = weston_pointer_create();
1852 if (pointer == NULL)
1853 return;
1854
1855 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001856 seat->pointer_device_count = 1;
1857 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001858
1859 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001860}
1861
1862WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001863weston_seat_release_pointer(struct weston_seat *seat)
1864{
1865 struct weston_pointer *pointer = seat->pointer;
1866
1867 seat->pointer_device_count--;
1868 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001869 weston_pointer_set_focus(pointer, NULL,
1870 wl_fixed_from_int(0),
1871 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001872 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001873
Jonas Ådahla4932742013-10-17 23:04:07 +02001874 if (pointer->sprite)
1875 pointer_unmap_sprite(pointer);
1876
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001877 seat_send_updated_caps(seat);
1878 }
1879}
1880
1881WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882weston_seat_init_touch(struct weston_seat *seat)
1883{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001884 struct weston_touch *touch;
1885
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001886 if (seat->touch) {
1887 seat->touch_device_count += 1;
1888 if (seat->touch_device_count == 1)
1889 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001890 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001891 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001892
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001893 touch = weston_touch_create();
1894 if (touch == NULL)
1895 return;
1896
1897 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001898 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001899 touch->seat = seat;
1900
1901 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001902}
1903
1904WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001905weston_seat_release_touch(struct weston_seat *seat)
1906{
1907 seat->touch_device_count--;
1908 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001909 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001910 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001911 seat_send_updated_caps(seat);
1912 }
1913}
1914
1915WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001916weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1917 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001918{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001919 memset(seat, 0, sizeof *seat);
1920
Kristian Høgsberge3148752013-05-06 23:19:49 -04001921 seat->selection_data_source = NULL;
1922 wl_list_init(&seat->base_resource_list);
1923 wl_signal_init(&seat->selection_signal);
1924 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001925 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001926
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001927 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001928 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001929
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001930 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001931 seat->modifier_state = 0;
1932 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001933 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001934
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001935 wl_list_insert(ec->seat_list.prev, &seat->link);
1936
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001937 clipboard_create(seat);
1938
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001939 wl_signal_emit(&ec->seat_created_signal, seat);
1940}
1941
1942WL_EXPORT void
1943weston_seat_release(struct weston_seat *seat)
1944{
1945 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001946
Rob Bradford382ff462013-06-24 16:52:45 +01001947#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001948 if (seat->compositor->use_xkbcommon) {
1949 if (seat->xkb_state.state != NULL)
1950 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001951 if (seat->xkb_info)
1952 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001953 if (seat->pending_keymap)
1954 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001955 }
Rob Bradford382ff462013-06-24 16:52:45 +01001956#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001957
Kristian Høgsberge3148752013-05-06 23:19:49 -04001958 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001959 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001960 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001961 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001962 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001963 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001964
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001965 free (seat->seat_name);
1966
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001967 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001968
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001969 wl_signal_emit(&seat->destroy_signal, seat);
1970}