blob: 78b6eadb907b2dca9ee4a806d865059f7ca4f9c2 [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
74lose_pointer_focus(struct wl_listener *listener, void *data)
75{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040076 struct weston_pointer *pointer =
77 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078
79 pointer->focus_resource = NULL;
80}
81
82static void
83lose_keyboard_focus(struct wl_listener *listener, void *data)
84{
Kristian Høgsberg29139d42013-04-18 15:25:39 -040085 struct weston_keyboard *keyboard =
86 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040087
88 keyboard->focus_resource = NULL;
89}
90
91static void
92lose_touch_focus(struct wl_listener *listener, void *data)
93{
Kristian Høgsberge329f362013-05-06 22:19:57 -040094 struct weston_touch *touch =
95 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040096
97 touch->focus_resource = NULL;
98}
99
100static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400101default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400102{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400103 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400104 struct weston_surface *surface;
105 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400106
107 if (pointer->button_count > 0)
108 return;
109
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400110 surface = weston_compositor_pick_surface(pointer->seat->compositor,
111 pointer->x, pointer->y,
112 &sx, &sy);
113
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400114 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400115 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116}
117
118static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400119default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400120{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400121 struct weston_pointer *pointer = grab->pointer;
122 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400123
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400124 if (pointer->focus_resource) {
125 weston_surface_from_global_fixed(pointer->focus,
126 pointer->x, pointer->y,
127 &sx, &sy);
128 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
129 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400130}
131
132static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400133default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400134 uint32_t time, uint32_t button, uint32_t state_w)
135{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400136 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400137 struct weston_compositor *compositor = pointer->seat->compositor;
138 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139 struct wl_resource *resource;
140 uint32_t serial;
141 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100142 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400143 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400144
145 resource = pointer->focus_resource;
146 if (resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400147 serial = wl_display_next_serial(display);
148 wl_pointer_send_button(resource, serial, time, button, state_w);
149 }
150
151 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400152 state == WL_POINTER_BUTTON_STATE_RELEASED) {
153 surface = weston_compositor_pick_surface(compositor,
154 pointer->x,
155 pointer->y,
156 &sx, &sy);
157
158 weston_pointer_set_focus(pointer, surface, sx, sy);
159 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400160}
161
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400162static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163 default_pointer_grab_interface = {
164 default_grab_focus,
165 default_grab_motion,
166 default_grab_button
167};
168
Kristian Høgsberge329f362013-05-06 22:19:57 -0400169static void
170default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
171 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400172{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400173 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100174 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400175 uint32_t serial;
176
177 if (touch->focus_resource && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400178 serial = wl_display_next_serial(display);
179 wl_touch_send_down(touch->focus_resource, serial, time,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500180 touch->focus->resource,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400181 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400182 }
183}
184
Kristian Høgsberge329f362013-05-06 22:19:57 -0400185static void
186default_grab_touch_up(struct weston_touch_grab *grab,
187 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400188{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400189 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100190 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 uint32_t serial;
192
193 if (touch->focus_resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194 serial = wl_display_next_serial(display);
195 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
196 }
197}
198
Kristian Høgsberge329f362013-05-06 22:19:57 -0400199static void
200default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
201 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400202{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400203 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400204
205 if (touch->focus_resource) {
206 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400207 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208 }
209}
210
Kristian Høgsberge329f362013-05-06 22:19:57 -0400211static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212 default_grab_touch_down,
213 default_grab_touch_up,
214 default_grab_touch_motion
215};
216
217static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400218default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400219 uint32_t time, uint32_t key, uint32_t state)
220{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400221 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400222 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100223 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224 uint32_t serial;
225
226 resource = keyboard->focus_resource;
227 if (resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228 serial = wl_display_next_serial(display);
229 wl_keyboard_send_key(resource, serial, time, key, state);
230 }
231}
232
233static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400234find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400235{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400236 if (!surface)
237 return NULL;
238
Jason Ekstrand44a38632013-06-14 10:08:00 -0500239 if (!surface->resource)
240 return NULL;
241
242 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243}
244
245static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400246default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247 uint32_t mods_depressed, uint32_t mods_latched,
248 uint32_t mods_locked, uint32_t group)
249{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400250 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400251 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252 struct wl_resource *resource, *pr;
253
254 resource = keyboard->focus_resource;
255 if (!resource)
256 return;
257
258 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
259 mods_latched, mods_locked, group);
260
261 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
262 pr = find_resource_for_surface(&keyboard->resource_list,
263 pointer->focus);
264 if (pr) {
265 wl_keyboard_send_modifiers(pr,
266 serial,
267 keyboard->modifiers.mods_depressed,
268 keyboard->modifiers.mods_latched,
269 keyboard->modifiers.mods_locked,
270 keyboard->modifiers.group);
271 }
272 }
273}
274
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400275static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400276 default_keyboard_grab_interface = {
277 default_grab_key,
278 default_grab_modifiers,
279};
280
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400281static void
282pointer_unmap_sprite(struct weston_pointer *pointer)
283{
284 if (weston_surface_is_mapped(pointer->sprite))
285 weston_surface_unmap(pointer->sprite);
286
287 wl_list_remove(&pointer->sprite_destroy_listener.link);
288 pointer->sprite->configure = NULL;
289 pointer->sprite->configure_private = NULL;
290 pointer->sprite = NULL;
291}
292
293static void
294pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
295{
296 struct weston_pointer *pointer =
297 container_of(listener, struct weston_pointer,
298 sprite_destroy_listener);
299
300 pointer->sprite = NULL;
301}
302
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400303WL_EXPORT struct weston_pointer *
304weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400306 struct weston_pointer *pointer;
307
Peter Huttererf3d62272013-08-08 11:57:05 +1000308 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400309 if (pointer == NULL)
310 return NULL;
311
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312 wl_list_init(&pointer->resource_list);
313 pointer->focus_listener.notify = lose_pointer_focus;
314 pointer->default_grab.interface = &default_pointer_grab_interface;
315 pointer->default_grab.pointer = pointer;
316 pointer->grab = &pointer->default_grab;
317 wl_signal_init(&pointer->focus_signal);
318
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400319 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
320
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 /* FIXME: Pick better co-ords. */
322 pointer->x = wl_fixed_from_int(100);
323 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400324
325 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326}
327
328WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400329weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400331 if (pointer->sprite)
332 pointer_unmap_sprite(pointer);
333
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334 /* XXX: What about pointer->resource_list? */
335 if (pointer->focus_resource)
336 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400337 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338}
339
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400340WL_EXPORT struct weston_keyboard *
341weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400343 struct weston_keyboard *keyboard;
344
Peter Huttererf3d62272013-08-08 11:57:05 +1000345 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400346 if (keyboard == NULL)
347 return NULL;
348
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400349 wl_list_init(&keyboard->resource_list);
350 wl_array_init(&keyboard->keys);
351 keyboard->focus_listener.notify = lose_keyboard_focus;
352 keyboard->default_grab.interface = &default_keyboard_grab_interface;
353 keyboard->default_grab.keyboard = keyboard;
354 keyboard->grab = &keyboard->default_grab;
355 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400356
357 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358}
359
360WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400361weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400362{
363 /* XXX: What about keyboard->resource_list? */
364 if (keyboard->focus_resource)
365 wl_list_remove(&keyboard->focus_listener.link);
366 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400367 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400368}
369
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400370WL_EXPORT struct weston_touch *
371weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400373 struct weston_touch *touch;
374
Peter Huttererf3d62272013-08-08 11:57:05 +1000375 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400376 if (touch == NULL)
377 return NULL;
378
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379 wl_list_init(&touch->resource_list);
380 touch->focus_listener.notify = lose_touch_focus;
381 touch->default_grab.interface = &default_touch_grab_interface;
382 touch->default_grab.touch = touch;
383 touch->grab = &touch->default_grab;
384 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400385
386 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400387}
388
389WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400390weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391{
392 /* XXX: What about touch->resource_list? */
393 if (touch->focus_resource)
394 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400395 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396}
397
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400399seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500401 struct wl_list *link;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 enum wl_seat_capability caps = 0;
403
404 if (seat->pointer)
405 caps |= WL_SEAT_CAPABILITY_POINTER;
406 if (seat->keyboard)
407 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
408 if (seat->touch)
409 caps |= WL_SEAT_CAPABILITY_TOUCH;
410
Jason Ekstrand44a38632013-06-14 10:08:00 -0500411 for (link = seat->base_resource_list.next;
412 link != &seat->base_resource_list; link = link->next) {
413 wl_seat_send_capabilities(wl_resource_from_link(link), caps);
414 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
417WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400418weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400419 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400420 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400422 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400423 struct wl_resource *resource, *kr;
Rob Bradford880ebc72013-07-22 17:31:38 +0100424 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400425 uint32_t serial;
426
427 resource = pointer->focus_resource;
428 if (resource && pointer->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429 serial = wl_display_next_serial(display);
430 wl_pointer_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500431 pointer->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400432 wl_list_remove(&pointer->focus_listener.link);
433 }
434
435 resource = find_resource_for_surface(&pointer->resource_list,
436 surface);
437 if (resource &&
438 (pointer->focus != surface ||
439 pointer->focus_resource != resource)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400440 serial = wl_display_next_serial(display);
441 if (kbd) {
442 kr = find_resource_for_surface(&kbd->resource_list,
443 surface);
444 if (kr) {
445 wl_keyboard_send_modifiers(kr,
446 serial,
447 kbd->modifiers.mods_depressed,
448 kbd->modifiers.mods_latched,
449 kbd->modifiers.mods_locked,
450 kbd->modifiers.group);
451 }
452 }
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500453 wl_pointer_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454 sx, sy);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500455 wl_resource_add_destroy_listener(resource,
456 &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457 pointer->focus_serial = serial;
458 }
459
460 pointer->focus_resource = resource;
461 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462 wl_signal_emit(&pointer->focus_signal, pointer);
463}
464
465WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400466weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400467 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400468{
469 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100470 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400471 uint32_t serial;
472
473 if (keyboard->focus_resource && keyboard->focus != surface) {
474 resource = keyboard->focus_resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475 serial = wl_display_next_serial(display);
476 wl_keyboard_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500477 keyboard->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478 wl_list_remove(&keyboard->focus_listener.link);
479 }
480
481 resource = find_resource_for_surface(&keyboard->resource_list,
482 surface);
483 if (resource &&
484 (keyboard->focus != surface ||
485 keyboard->focus_resource != resource)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486 serial = wl_display_next_serial(display);
487 wl_keyboard_send_modifiers(resource, serial,
488 keyboard->modifiers.mods_depressed,
489 keyboard->modifiers.mods_latched,
490 keyboard->modifiers.mods_locked,
491 keyboard->modifiers.group);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500492 wl_keyboard_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493 &keyboard->keys);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500494 wl_resource_add_destroy_listener(resource,
495 &keyboard->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496 keyboard->focus_serial = serial;
497 }
498
499 keyboard->focus_resource = resource;
500 keyboard->focus = surface;
501 wl_signal_emit(&keyboard->focus_signal, keyboard);
502}
503
504WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400505weston_keyboard_start_grab(struct weston_keyboard *keyboard,
506 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400507{
508 keyboard->grab = grab;
509 grab->keyboard = keyboard;
510
511 /* XXX focus? */
512}
513
514WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400515weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400516{
517 keyboard->grab = &keyboard->default_grab;
518}
519
520WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400521weston_pointer_start_grab(struct weston_pointer *pointer,
522 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400523{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400526 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400527}
528
529WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400530weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400533 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400534}
535
536WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400537weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538{
539 touch->grab = grab;
540 grab->touch = touch;
541}
542
543WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400544weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400545{
546 touch->grab = &touch->default_grab;
547}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400548
Rob Bradford806d8c02013-06-25 18:56:41 +0100549WL_EXPORT void
550weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400551{
Rob Bradford806d8c02013-06-25 18:56:41 +0100552 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400553 struct weston_output *output, *prev = NULL;
554 int x, y, old_x, old_y, valid = 0;
555
556 x = wl_fixed_to_int(*fx);
557 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100558 old_x = wl_fixed_to_int(pointer->x);
559 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400560
561 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100562 if (pointer->seat->output && pointer->seat->output != output)
563 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400564 if (pixman_region32_contains_point(&output->region,
565 x, y, NULL))
566 valid = 1;
567 if (pixman_region32_contains_point(&output->region,
568 old_x, old_y, NULL))
569 prev = output;
570 }
571
Rob Bradford66bd9f52013-06-25 18:56:42 +0100572 if (!prev)
573 prev = pointer->seat->output;
574
575 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400576 if (x < prev->x)
577 *fx = wl_fixed_from_int(prev->x);
578 else if (x >= prev->x + prev->width)
579 *fx = wl_fixed_from_int(prev->x +
580 prev->width - 1);
581 if (y < prev->y)
582 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200583 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400584 *fy = wl_fixed_from_int(prev->y +
585 prev->height - 1);
586 }
587}
588
589/* Takes absolute values */
590static void
591move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
592{
593 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400594 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400595 struct weston_output *output;
596 int32_t ix, iy;
597
Rob Bradford806d8c02013-06-25 18:56:41 +0100598 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400599
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400600 pointer->x = x;
601 pointer->y = y;
602
603 ix = wl_fixed_to_int(x);
604 iy = wl_fixed_to_int(y);
605
606 wl_list_for_each(output, &ec->output_list, link)
607 if (output->zoom.active &&
608 pixman_region32_contains_point(&output->region,
609 ix, iy, NULL))
610 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
611
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400612 if (pointer->sprite) {
613 weston_surface_set_position(pointer->sprite,
614 ix - pointer->hotspot_x,
615 iy - pointer->hotspot_y);
616 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400617 }
618}
619
620WL_EXPORT void
621notify_motion(struct weston_seat *seat,
622 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
623{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400624 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400625 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400626
627 weston_compositor_wake(ec);
628
629 move_pointer(seat, pointer->x + dx, pointer->y + dy);
630
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400631 pointer->grab->interface->focus(pointer->grab);
632 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400633}
634
635WL_EXPORT void
636notify_motion_absolute(struct weston_seat *seat,
637 uint32_t time, wl_fixed_t x, wl_fixed_t y)
638{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400639 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400640 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400641
642 weston_compositor_wake(ec);
643
644 move_pointer(seat, x, y);
645
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400646 pointer->grab->interface->focus(pointer->grab);
647 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648}
649
650WL_EXPORT void
651weston_surface_activate(struct weston_surface *surface,
652 struct weston_seat *seat)
653{
654 struct weston_compositor *compositor = seat->compositor;
655
Kristian Høgsberge3148752013-05-06 23:19:49 -0400656 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400657 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400658 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400659 }
660
661 wl_signal_emit(&compositor->activate_signal, surface);
662}
663
664WL_EXPORT void
665notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
666 enum wl_pointer_button_state state)
667{
668 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400669 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400670 struct weston_surface *focus =
671 (struct weston_surface *) pointer->focus;
672 uint32_t serial = wl_display_next_serial(compositor->wl_display);
673
674 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
675 if (compositor->ping_handler && focus)
676 compositor->ping_handler(focus, serial);
677 weston_compositor_idle_inhibit(compositor);
678 if (pointer->button_count == 0) {
679 pointer->grab_button = button;
680 pointer->grab_time = time;
681 pointer->grab_x = pointer->x;
682 pointer->grab_y = pointer->y;
683 }
684 pointer->button_count++;
685 } else {
686 weston_compositor_idle_release(compositor);
687 pointer->button_count--;
688 }
689
690 weston_compositor_run_button_binding(compositor, seat, time, button,
691 state);
692
693 pointer->grab->interface->button(pointer->grab, time, button, state);
694
695 if (pointer->button_count == 1)
696 pointer->grab_serial =
697 wl_display_get_serial(compositor->wl_display);
698}
699
700WL_EXPORT void
701notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
702 wl_fixed_t value)
703{
704 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400705 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400706 struct weston_surface *focus =
707 (struct weston_surface *) pointer->focus;
708 uint32_t serial = wl_display_next_serial(compositor->wl_display);
709
710 if (compositor->ping_handler && focus)
711 compositor->ping_handler(focus, serial);
712
713 weston_compositor_wake(compositor);
714
715 if (!value)
716 return;
717
718 if (weston_compositor_run_axis_binding(compositor, seat,
719 time, axis, value))
720 return;
721
722 if (pointer->focus_resource)
723 wl_pointer_send_axis(pointer->focus_resource, time, axis,
724 value);
725}
726
Rob Bradford382ff462013-06-24 16:52:45 +0100727#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400728WL_EXPORT void
729notify_modifiers(struct weston_seat *seat, uint32_t serial)
730{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400731 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400732 struct weston_keyboard_grab *grab = keyboard->grab;
733 uint32_t mods_depressed, mods_latched, mods_locked, group;
734 uint32_t mods_lookup;
735 enum weston_led leds = 0;
736 int changed = 0;
737
738 /* Serialize and update our internal state, checking to see if it's
739 * different to the previous state. */
740 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
741 XKB_STATE_DEPRESSED);
742 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
743 XKB_STATE_LATCHED);
744 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
745 XKB_STATE_LOCKED);
746 group = xkb_state_serialize_group(seat->xkb_state.state,
747 XKB_STATE_EFFECTIVE);
748
Kristian Høgsberge3148752013-05-06 23:19:49 -0400749 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
750 mods_latched != seat->keyboard->modifiers.mods_latched ||
751 mods_locked != seat->keyboard->modifiers.mods_locked ||
752 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400753 changed = 1;
754
Kristian Høgsberge3148752013-05-06 23:19:49 -0400755 seat->keyboard->modifiers.mods_depressed = mods_depressed;
756 seat->keyboard->modifiers.mods_latched = mods_latched;
757 seat->keyboard->modifiers.mods_locked = mods_locked;
758 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400759
760 /* And update the modifier_state for bindings. */
761 mods_lookup = mods_depressed | mods_latched;
762 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000763 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400764 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000765 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400766 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000767 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400768 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000769 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400770 seat->modifier_state |= MODIFIER_SHIFT;
771
772 /* Finally, notify the compositor that LEDs have changed. */
773 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000774 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400775 leds |= LED_NUM_LOCK;
776 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000777 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400778 leds |= LED_CAPS_LOCK;
779 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000780 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400781 leds |= LED_SCROLL_LOCK;
782 if (leds != seat->xkb_state.leds && seat->led_update)
783 seat->led_update(seat, leds);
784 seat->xkb_state.leds = leds;
785
786 if (changed) {
787 grab->interface->modifiers(grab,
788 serial,
789 keyboard->modifiers.mods_depressed,
790 keyboard->modifiers.mods_latched,
791 keyboard->modifiers.mods_locked,
792 keyboard->modifiers.group);
793 }
794}
795
796static void
797update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
798 enum wl_keyboard_key_state state)
799{
800 enum xkb_key_direction direction;
801
Matt Roper01a92732013-06-24 16:52:44 +0100802 /* Keyboard modifiers don't exist in raw keyboard mode */
803 if (!seat->compositor->use_xkbcommon)
804 return;
805
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400806 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
807 direction = XKB_KEY_DOWN;
808 else
809 direction = XKB_KEY_UP;
810
811 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
812 * broken keycode system, which starts at 8. */
813 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
814
815 notify_modifiers(seat, serial);
816}
Rob Bradford382ff462013-06-24 16:52:45 +0100817#else
818WL_EXPORT void
819notify_modifiers(struct weston_seat *seat, uint32_t serial)
820{
821}
822
823static void
824update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
825 enum wl_keyboard_key_state state)
826{
827}
828#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400829
830WL_EXPORT void
831notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
832 enum wl_keyboard_key_state state,
833 enum weston_key_state_update update_state)
834{
835 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400836 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400837 struct weston_surface *focus =
838 (struct weston_surface *) keyboard->focus;
839 struct weston_keyboard_grab *grab = keyboard->grab;
840 uint32_t serial = wl_display_next_serial(compositor->wl_display);
841 uint32_t *k, *end;
842
843 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
844 if (compositor->ping_handler && focus)
845 compositor->ping_handler(focus, serial);
846
847 weston_compositor_idle_inhibit(compositor);
848 keyboard->grab_key = key;
849 keyboard->grab_time = time;
850 } else {
851 weston_compositor_idle_release(compositor);
852 }
853
854 end = keyboard->keys.data + keyboard->keys.size;
855 for (k = keyboard->keys.data; k < end; k++) {
856 if (*k == key) {
857 /* Ignore server-generated repeats. */
858 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
859 return;
860 *k = *--end;
861 }
862 }
863 keyboard->keys.size = (void *) end - keyboard->keys.data;
864 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
865 k = wl_array_add(&keyboard->keys, sizeof *k);
866 *k = key;
867 }
868
869 if (grab == &keyboard->default_grab ||
870 grab == &keyboard->input_method_grab) {
871 weston_compositor_run_key_binding(compositor, seat, time, key,
872 state);
873 grab = keyboard->grab;
874 }
875
876 grab->interface->key(grab, time, key, state);
877
878 if (update_state == STATE_UPDATE_AUTOMATIC) {
879 update_modifier_state(seat,
880 wl_display_get_serial(compositor->wl_display),
881 key,
882 state);
883 }
884}
885
886WL_EXPORT void
887notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
888 wl_fixed_t x, wl_fixed_t y)
889{
890 struct weston_compositor *compositor = seat->compositor;
891
892 if (output) {
893 move_pointer(seat, x, y);
894 compositor->focus = 1;
895 } else {
896 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400897 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400898 * NULL) here, but somehow that breaks re-entry... */
899 }
900}
901
902static void
903destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
904{
905 struct weston_seat *ws;
906
907 ws = container_of(listener, struct weston_seat,
908 saved_kbd_focus_listener);
909
910 ws->saved_kbd_focus = NULL;
911}
912
913WL_EXPORT void
914notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
915 enum weston_key_state_update update_state)
916{
917 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400918 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400919 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400920 uint32_t *k, serial;
921
922 serial = wl_display_next_serial(compositor->wl_display);
923 wl_array_copy(&keyboard->keys, keys);
924 wl_array_for_each(k, &keyboard->keys) {
925 weston_compositor_idle_inhibit(compositor);
926 if (update_state == STATE_UPDATE_AUTOMATIC)
927 update_modifier_state(seat, serial, *k,
928 WL_KEYBOARD_KEY_STATE_PRESSED);
929 }
930
931 /* Run key bindings after we've updated the state. */
932 wl_array_for_each(k, &keyboard->keys) {
933 weston_compositor_run_key_binding(compositor, seat, 0, *k,
934 WL_KEYBOARD_KEY_STATE_PRESSED);
935 }
936
937 surface = seat->saved_kbd_focus;
938
939 if (surface) {
940 wl_list_remove(&seat->saved_kbd_focus_listener.link);
941 weston_keyboard_set_focus(keyboard, surface);
942 seat->saved_kbd_focus = NULL;
943 }
944}
945
946WL_EXPORT void
947notify_keyboard_focus_out(struct weston_seat *seat)
948{
949 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400950 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400951 uint32_t *k, serial;
952
953 serial = wl_display_next_serial(compositor->wl_display);
954 wl_array_for_each(k, &keyboard->keys) {
955 weston_compositor_idle_release(compositor);
956 update_modifier_state(seat, serial, *k,
957 WL_KEYBOARD_KEY_STATE_RELEASED);
958 }
959
960 seat->modifier_state = 0;
961
962 if (keyboard->focus) {
963 seat->saved_kbd_focus = keyboard->focus;
964 seat->saved_kbd_focus_listener.notify =
965 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500966 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400967 &seat->saved_kbd_focus_listener);
968 }
969
970 weston_keyboard_set_focus(keyboard, NULL);
971 /* FIXME: We really need keyboard grab cancel here to
972 * let the grab shut down properly. As it is we leak
973 * the grab data. */
974 weston_keyboard_end_grab(keyboard);
975}
976
Michael Fua2bb7912013-07-23 15:51:06 +0800977WL_EXPORT void
978weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400979{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400980 struct wl_resource *resource;
981
982 if (seat->touch->focus == surface)
983 return;
984
985 if (seat->touch->focus_resource)
986 wl_list_remove(&seat->touch->focus_listener.link);
987 seat->touch->focus = NULL;
988 seat->touch->focus_resource = NULL;
989
990 if (surface) {
991 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -0400992 find_resource_for_surface(&seat->touch->resource_list,
993 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400994 if (!resource) {
995 weston_log("couldn't find resource\n");
996 return;
997 }
998
999 seat->touch->focus = surface;
1000 seat->touch->focus_resource = resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -05001001 wl_resource_add_destroy_listener(resource,
1002 &seat->touch->focus_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001003 }
1004}
1005
1006/**
1007 * notify_touch - emulates button touches and notifies surfaces accordingly.
1008 *
1009 * It assumes always the correct cycle sequence until it gets here: touch_down
1010 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1011 * for sending along such order.
1012 *
1013 */
1014WL_EXPORT void
1015notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1016 wl_fixed_t x, wl_fixed_t y, int touch_type)
1017{
1018 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001019 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001020 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001021 struct weston_surface *es;
1022 wl_fixed_t sx, sy;
1023
1024 /* Update grab's global coordinates. */
1025 touch->grab_x = x;
1026 touch->grab_y = y;
1027
1028 switch (touch_type) {
1029 case WL_TOUCH_DOWN:
1030 weston_compositor_idle_inhibit(ec);
1031
1032 seat->num_tp++;
1033
1034 /* the first finger down picks the surface, and all further go
1035 * to that surface for the remainder of the touch session i.e.
1036 * until all touch points are up again. */
1037 if (seat->num_tp == 1) {
1038 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001039 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001040 } else if (touch->focus) {
1041 es = (struct weston_surface *) touch->focus;
1042 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1043 } else {
1044 /* Unexpected condition: We have non-initial touch but
1045 * there is no focused surface.
1046 */
1047 weston_log("touch event received with %d points down"
1048 "but no surface focused\n", seat->num_tp);
1049 return;
1050 }
1051
1052 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001053 if (seat->num_tp == 1) {
1054 touch->grab_serial =
1055 wl_display_get_serial(ec->wl_display);
1056 touch->grab_time = time;
1057 touch->grab_x = x;
1058 touch->grab_y = y;
1059 }
1060
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001061 break;
1062 case WL_TOUCH_MOTION:
1063 es = (struct weston_surface *) touch->focus;
1064 if (!es)
1065 break;
1066
1067 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1068 grab->interface->motion(grab, time, touch_id, sx, sy);
1069 break;
1070 case WL_TOUCH_UP:
1071 weston_compositor_idle_release(ec);
1072 seat->num_tp--;
1073
1074 grab->interface->up(grab, time, touch_id);
1075 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001076 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001077 break;
1078 }
1079}
1080
1081static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001082pointer_cursor_surface_configure(struct weston_surface *es,
1083 int32_t dx, int32_t dy, int32_t width, int32_t height)
1084{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001085 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001086 int x, y;
1087
1088 if (width == 0)
1089 return;
1090
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001091 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001092
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001093 pointer->hotspot_x -= dx;
1094 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001095
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001096 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1097 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001098
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001099 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001100
1101 empty_region(&es->pending.input);
1102
1103 if (!weston_surface_is_mapped(es)) {
1104 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1105 &es->layer_link);
1106 weston_surface_update_transform(es);
1107 }
1108}
1109
1110static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001111pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1112 uint32_t serial, struct wl_resource *surface_resource,
1113 int32_t x, int32_t y)
1114{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001115 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001116 struct weston_surface *surface = NULL;
1117
1118 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001119 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001120
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001121 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001122 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001123 /* pointer->focus->resource can be NULL. Surfaces like the
1124 black_surface used in shell.c for fullscreen don't have
1125 a resource, but can still have focus */
1126 if (pointer->focus->resource == NULL)
1127 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001128 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001129 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001130 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001131 return;
1132
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001133 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001134 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001135 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001136 WL_DISPLAY_ERROR_INVALID_OBJECT,
1137 "surface->configure already "
1138 "set");
1139 return;
1140 }
1141 }
1142
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001143 if (pointer->sprite)
1144 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001145
1146 if (!surface)
1147 return;
1148
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001149 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001150 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001151
1152 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001153 surface->configure_private = pointer;
1154 pointer->sprite = surface;
1155 pointer->hotspot_x = x;
1156 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001157
1158 if (surface->buffer_ref.buffer)
1159 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1160 weston_surface_buffer_height(surface));
1161}
1162
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001163static void
1164pointer_release(struct wl_client *client, struct wl_resource *resource)
1165{
1166 wl_resource_destroy(resource);
1167}
1168
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001170 pointer_set_cursor,
1171 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001172};
1173
1174static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001175seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1176 uint32_t id)
1177{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001178 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001179 struct wl_resource *cr;
1180
Kristian Høgsberge3148752013-05-06 23:19:49 -04001181 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182 return;
1183
Jason Ekstranda85118c2013-06-27 20:17:02 -05001184 cr = wl_resource_create(client, &wl_pointer_interface,
1185 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001186 if (cr == NULL) {
1187 wl_client_post_no_memory(client);
1188 return;
1189 }
1190
Jason Ekstrand44a38632013-06-14 10:08:00 -05001191 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001192 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1193 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001195 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001196 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001197 struct weston_surface *surface;
1198 wl_fixed_t sx, sy;
1199
Kristian Høgsberge3148752013-05-06 23:19:49 -04001200 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001201 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001202 seat->pointer->x,
1203 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 &sx,
1205 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001206 weston_pointer_set_focus(seat->pointer,
1207 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001208 sx,
1209 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001210 }
1211}
1212
1213static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001214keyboard_release(struct wl_client *client, struct wl_resource *resource)
1215{
1216 wl_resource_destroy(resource);
1217}
1218
1219static const struct wl_keyboard_interface keyboard_interface = {
1220 keyboard_release
1221};
1222
1223static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001224seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1225 uint32_t id)
1226{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001227 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228 struct wl_resource *cr;
1229
Kristian Høgsberge3148752013-05-06 23:19:49 -04001230 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001231 return;
1232
Jason Ekstranda85118c2013-06-27 20:17:02 -05001233 cr = wl_resource_create(client, &wl_keyboard_interface,
1234 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001235 if (cr == NULL) {
1236 wl_client_post_no_memory(client);
1237 return;
1238 }
1239
Jason Ekstrand44a38632013-06-14 10:08:00 -05001240 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001241 wl_resource_set_implementation(cr, &keyboard_interface,
1242 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001243
Matt Roper01a92732013-06-24 16:52:44 +01001244 if (seat->compositor->use_xkbcommon) {
1245 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001246 seat->xkb_info->keymap_fd,
1247 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001248 } else {
1249 int null_fd = open("/dev/null", O_RDONLY);
1250 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1251 null_fd,
1252 0);
1253 close(null_fd);
1254 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001255
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001257 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001258 weston_keyboard_set_focus(seat->keyboard,
1259 seat->keyboard->focus);
1260 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001261 }
1262}
1263
1264static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001265touch_release(struct wl_client *client, struct wl_resource *resource)
1266{
1267 wl_resource_destroy(resource);
1268}
1269
1270static const struct wl_touch_interface touch_interface = {
1271 touch_release
1272};
1273
1274static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1276 uint32_t id)
1277{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001278 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001279 struct wl_resource *cr;
1280
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001282 return;
1283
Jason Ekstranda85118c2013-06-27 20:17:02 -05001284 cr = wl_resource_create(client, &wl_touch_interface,
1285 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001286 if (cr == NULL) {
1287 wl_client_post_no_memory(client);
1288 return;
1289 }
1290
Jason Ekstrand44a38632013-06-14 10:08:00 -05001291 wl_list_insert(&seat->touch->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001292 wl_resource_set_implementation(cr, &touch_interface,
1293 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001294}
1295
1296static const struct wl_seat_interface seat_interface = {
1297 seat_get_pointer,
1298 seat_get_keyboard,
1299 seat_get_touch,
1300};
1301
1302static void
1303bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1304{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001305 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001306 struct wl_resource *resource;
1307 enum wl_seat_capability caps = 0;
1308
Jason Ekstranda85118c2013-06-27 20:17:02 -05001309 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001310 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001311 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001312 wl_resource_set_implementation(resource, &seat_interface, data,
1313 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001314
1315 if (seat->pointer)
1316 caps |= WL_SEAT_CAPABILITY_POINTER;
1317 if (seat->keyboard)
1318 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1319 if (seat->touch)
1320 caps |= WL_SEAT_CAPABILITY_TOUCH;
1321
1322 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001323 if (version >= 2)
1324 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001325}
1326
Rob Bradford382ff462013-06-24 16:52:45 +01001327#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328int
1329weston_compositor_xkb_init(struct weston_compositor *ec,
1330 struct xkb_rule_names *names)
1331{
Rob Bradford382ff462013-06-24 16:52:45 +01001332 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001333
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001334 if (ec->xkb_context == NULL) {
1335 ec->xkb_context = xkb_context_new(0);
1336 if (ec->xkb_context == NULL) {
1337 weston_log("failed to create XKB context\n");
1338 return -1;
1339 }
1340 }
1341
1342 if (names)
1343 ec->xkb_names = *names;
1344 if (!ec->xkb_names.rules)
1345 ec->xkb_names.rules = strdup("evdev");
1346 if (!ec->xkb_names.model)
1347 ec->xkb_names.model = strdup("pc105");
1348 if (!ec->xkb_names.layout)
1349 ec->xkb_names.layout = strdup("us");
1350
1351 return 0;
1352}
1353
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001354static void
1355weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001356{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001357 if (--xkb_info->ref_count > 0)
1358 return;
1359
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001360 if (xkb_info->keymap)
1361 xkb_map_unref(xkb_info->keymap);
1362
1363 if (xkb_info->keymap_area)
1364 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1365 if (xkb_info->keymap_fd >= 0)
1366 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001367 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001368}
1369
1370void
1371weston_compositor_xkb_destroy(struct weston_compositor *ec)
1372{
Matt Roper01a92732013-06-24 16:52:44 +01001373 /*
1374 * If we're operating in raw keyboard mode, we never initialized
1375 * libxkbcommon so there's no cleanup to do either.
1376 */
1377 if (!ec->use_xkbcommon)
1378 return;
1379
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001380 free((char *) ec->xkb_names.rules);
1381 free((char *) ec->xkb_names.model);
1382 free((char *) ec->xkb_names.layout);
1383 free((char *) ec->xkb_names.variant);
1384 free((char *) ec->xkb_names.options);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001385
1386 if (ec->xkb_info)
1387 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001388 xkb_context_unref(ec->xkb_context);
1389}
1390
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001391static struct weston_xkb_info *
1392weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001393{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001394 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1395 if (xkb_info == NULL)
1396 return NULL;
1397
1398 xkb_info->keymap = xkb_map_ref(keymap);
1399 xkb_info->ref_count = 1;
1400
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001401 char *keymap_str;
1402
1403 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1404 XKB_MOD_NAME_SHIFT);
1405 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1406 XKB_MOD_NAME_CAPS);
1407 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1408 XKB_MOD_NAME_CTRL);
1409 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1410 XKB_MOD_NAME_ALT);
1411 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1412 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1413 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1414 XKB_MOD_NAME_LOGO);
1415 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1416
1417 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1418 XKB_LED_NAME_NUM);
1419 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1420 XKB_LED_NAME_CAPS);
1421 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1422 XKB_LED_NAME_SCROLL);
1423
1424 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1425 if (keymap_str == NULL) {
1426 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001427 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001428 }
1429 xkb_info->keymap_size = strlen(keymap_str) + 1;
1430
1431 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1432 if (xkb_info->keymap_fd < 0) {
1433 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1434 (unsigned long) xkb_info->keymap_size);
1435 goto err_keymap_str;
1436 }
1437
1438 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1439 PROT_READ | PROT_WRITE,
1440 MAP_SHARED, xkb_info->keymap_fd, 0);
1441 if (xkb_info->keymap_area == MAP_FAILED) {
1442 weston_log("failed to mmap() %lu bytes\n",
1443 (unsigned long) xkb_info->keymap_size);
1444 goto err_dev_zero;
1445 }
1446 strcpy(xkb_info->keymap_area, keymap_str);
1447 free(keymap_str);
1448
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001449 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001450
1451err_dev_zero:
1452 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001453err_keymap_str:
1454 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001455err_keymap:
1456 xkb_map_unref(xkb_info->keymap);
1457 free(xkb_info);
1458 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001459}
1460
1461static int
1462weston_compositor_build_global_keymap(struct weston_compositor *ec)
1463{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001464 struct xkb_keymap *keymap;
1465
1466 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001467 return 0;
1468
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001469 keymap = xkb_map_new_from_names(ec->xkb_context,
1470 &ec->xkb_names,
1471 0);
1472 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001473 weston_log("failed to compile global XKB keymap\n");
1474 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1475 "options %s\n",
1476 ec->xkb_names.rules, ec->xkb_names.model,
1477 ec->xkb_names.layout, ec->xkb_names.variant,
1478 ec->xkb_names.options);
1479 return -1;
1480 }
1481
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001482 ec->xkb_info = weston_xkb_info_create(keymap);
1483 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001484 return -1;
1485
1486 return 0;
1487}
Rob Bradford382ff462013-06-24 16:52:45 +01001488#else
1489int
1490weston_compositor_xkb_init(struct weston_compositor *ec,
1491 struct xkb_rule_names *names)
1492{
1493 return 0;
1494}
1495
1496void
1497weston_compositor_xkb_destroy(struct weston_compositor *ec)
1498{
1499}
1500#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001501
1502WL_EXPORT int
1503weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1504{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001505 struct weston_keyboard *keyboard;
1506
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001507 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001508 return 0;
1509
Rob Bradford382ff462013-06-24 16:52:45 +01001510#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001511 if (seat->compositor->use_xkbcommon) {
1512 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001513 seat->xkb_info = weston_xkb_info_create(keymap);
1514 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001515 return -1;
1516 } else {
1517 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1518 return -1;
1519 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001520 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001521 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001522
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001523 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001524 if (seat->xkb_state.state == NULL) {
1525 weston_log("failed to initialise XKB state\n");
1526 return -1;
1527 }
1528
1529 seat->xkb_state.leds = 0;
1530 }
Rob Bradford382ff462013-06-24 16:52:45 +01001531#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001532
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001533 keyboard = weston_keyboard_create();
1534 if (keyboard == NULL) {
1535 weston_log("failed to allocate weston keyboard struct\n");
1536 return -1;
1537 }
1538
1539 seat->keyboard = keyboard;
1540 keyboard->seat = seat;
1541
1542 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001543
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001544 return 0;
1545}
1546
1547WL_EXPORT void
1548weston_seat_init_pointer(struct weston_seat *seat)
1549{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001550 struct weston_pointer *pointer;
1551
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001552 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553 return;
1554
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001555 pointer = weston_pointer_create();
1556 if (pointer == NULL)
1557 return;
1558
1559 seat->pointer = pointer;
1560 pointer->seat = seat;
1561
1562 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001563}
1564
1565WL_EXPORT void
1566weston_seat_init_touch(struct weston_seat *seat)
1567{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001568 struct weston_touch *touch;
1569
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001570 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001571 return;
1572
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001573 touch = weston_touch_create();
1574 if (touch == NULL)
1575 return;
1576
1577 seat->touch = touch;
1578 touch->seat = seat;
1579
1580 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581}
1582
1583WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001584weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1585 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001586{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001587 memset(seat, 0, sizeof *seat);
1588
Kristian Høgsberge3148752013-05-06 23:19:49 -04001589 seat->selection_data_source = NULL;
1590 wl_list_init(&seat->base_resource_list);
1591 wl_signal_init(&seat->selection_signal);
1592 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001593 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001594
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001595 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001596 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001597
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001598 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001599 seat->modifier_state = 0;
1600 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001601 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001602
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001603 wl_list_insert(ec->seat_list.prev, &seat->link);
1604
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605 clipboard_create(seat);
1606
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001607 wl_signal_emit(&ec->seat_created_signal, seat);
1608}
1609
1610WL_EXPORT void
1611weston_seat_release(struct weston_seat *seat)
1612{
1613 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614
Rob Bradford382ff462013-06-24 16:52:45 +01001615#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001616 if (seat->compositor->use_xkbcommon) {
1617 if (seat->xkb_state.state != NULL)
1618 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001619 if (seat->xkb_info)
1620 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001621 }
Rob Bradford382ff462013-06-24 16:52:45 +01001622#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001623
Kristian Høgsberge3148752013-05-06 23:19:49 -04001624 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001625 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001626 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001627 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001628 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001629 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001630
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001631 free (seat->seat_name);
1632
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001633 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001634
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001635 wl_signal_emit(&seat->destroy_signal, seat);
1636}