blob: 704ec0d8a285bcd4d997a6be7d46fb5affe6e97b [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
23#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040026#include <sys/mman.h>
27#include <assert.h>
28#include <unistd.h>
Matt Roper01a92732013-06-24 16:52:44 +010029#include <fcntl.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040030
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040031#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032#include "compositor.h"
33
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040034static void
35empty_region(pixman_region32_t *region)
36{
37 pixman_region32_fini(region);
38 pixman_region32_init(region);
39}
40
41static void unbind_resource(struct wl_resource *resource)
42{
Jason Ekstrand44a38632013-06-14 10:08:00 -050043 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040044}
45
46void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040047weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040048{
Kristian Høgsbergda751b82013-07-04 00:58:07 -040049 const struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050
Kristian Høgsbergda751b82013-07-04 00:58:07 -040051 if (pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052 return;
53
Kristian Høgsbergda751b82013-07-04 00:58:07 -040054 pointer->grab->interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040055}
56
57static void
58weston_compositor_idle_inhibit(struct weston_compositor *compositor)
59{
60 weston_compositor_wake(compositor);
61 compositor->idle_inhibit++;
62}
63
64static void
65weston_compositor_idle_release(struct weston_compositor *compositor)
66{
67 compositor->idle_inhibit--;
68 weston_compositor_wake(compositor);
69}
70
Kristian Høgsberg2158a882013-04-18 15:07:39 -040071static void
72lose_pointer_focus(struct wl_listener *listener, void *data)
73{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040074 struct weston_pointer *pointer =
75 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040076
77 pointer->focus_resource = NULL;
78}
79
80static void
81lose_keyboard_focus(struct wl_listener *listener, void *data)
82{
Kristian Høgsberg29139d42013-04-18 15:25:39 -040083 struct weston_keyboard *keyboard =
84 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040085
86 keyboard->focus_resource = NULL;
87}
88
89static void
90lose_touch_focus(struct wl_listener *listener, void *data)
91{
Kristian Høgsberge329f362013-05-06 22:19:57 -040092 struct weston_touch *touch =
93 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040094
95 touch->focus_resource = NULL;
96}
97
98static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -040099default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400100{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400101 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400102 struct weston_surface *surface;
103 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400104
105 if (pointer->button_count > 0)
106 return;
107
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400108 surface = weston_compositor_pick_surface(pointer->seat->compositor,
109 pointer->x, pointer->y,
110 &sx, &sy);
111
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400112 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400113 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400114}
115
116static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400117default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400118{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400119 struct weston_pointer *pointer = grab->pointer;
120 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400121
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400122 if (pointer->focus_resource) {
123 weston_surface_from_global_fixed(pointer->focus,
124 pointer->x, pointer->y,
125 &sx, &sy);
126 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
127 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400128}
129
130static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400131default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400132 uint32_t time, uint32_t button, uint32_t state_w)
133{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400134 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400135 struct weston_compositor *compositor = pointer->seat->compositor;
136 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137 struct wl_resource *resource;
138 uint32_t serial;
139 enum wl_pointer_button_state state = state_w;
140 struct wl_display *display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400141 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400142
143 resource = pointer->focus_resource;
144 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500145 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146 serial = wl_display_next_serial(display);
147 wl_pointer_send_button(resource, serial, time, button, state_w);
148 }
149
150 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400151 state == WL_POINTER_BUTTON_STATE_RELEASED) {
152 surface = weston_compositor_pick_surface(compositor,
153 pointer->x,
154 pointer->y,
155 &sx, &sy);
156
157 weston_pointer_set_focus(pointer, surface, sx, sy);
158 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400159}
160
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400161static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400162 default_pointer_grab_interface = {
163 default_grab_focus,
164 default_grab_motion,
165 default_grab_button
166};
167
Kristian Høgsberge329f362013-05-06 22:19:57 -0400168static void
169default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
170 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400171{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400172 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400173 struct wl_display *display;
174 uint32_t serial;
175
176 if (touch->focus_resource && touch->focus) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500177 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
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;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400190 struct wl_display *display;
191 uint32_t serial;
192
193 if (touch->focus_resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500194 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400195 serial = wl_display_next_serial(display);
196 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
197 }
198}
199
Kristian Høgsberge329f362013-05-06 22:19:57 -0400200static void
201default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
202 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400203{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400204 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205
206 if (touch->focus_resource) {
207 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400208 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400209 }
210}
211
Kristian Høgsberge329f362013-05-06 22:19:57 -0400212static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400213 default_grab_touch_down,
214 default_grab_touch_up,
215 default_grab_touch_motion
216};
217
218static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400219default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400220 uint32_t time, uint32_t key, uint32_t state)
221{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400222 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400223 struct wl_resource *resource;
224 struct wl_display *display;
225 uint32_t serial;
226
227 resource = keyboard->focus_resource;
228 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500229 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400230 serial = wl_display_next_serial(display);
231 wl_keyboard_send_key(resource, serial, time, key, state);
232 }
233}
234
235static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400236find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400237{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400238 if (!surface)
239 return NULL;
240
Jason Ekstrand44a38632013-06-14 10:08:00 -0500241 if (!surface->resource)
242 return NULL;
243
244 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400245}
246
247static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400248default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400249 uint32_t mods_depressed, uint32_t mods_latched,
250 uint32_t mods_locked, uint32_t group)
251{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400252 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400253 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254 struct wl_resource *resource, *pr;
255
256 resource = keyboard->focus_resource;
257 if (!resource)
258 return;
259
260 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
261 mods_latched, mods_locked, group);
262
263 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
264 pr = find_resource_for_surface(&keyboard->resource_list,
265 pointer->focus);
266 if (pr) {
267 wl_keyboard_send_modifiers(pr,
268 serial,
269 keyboard->modifiers.mods_depressed,
270 keyboard->modifiers.mods_latched,
271 keyboard->modifiers.mods_locked,
272 keyboard->modifiers.group);
273 }
274 }
275}
276
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400277static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400278 default_keyboard_grab_interface = {
279 default_grab_key,
280 default_grab_modifiers,
281};
282
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400283static void
284pointer_unmap_sprite(struct weston_pointer *pointer)
285{
286 if (weston_surface_is_mapped(pointer->sprite))
287 weston_surface_unmap(pointer->sprite);
288
289 wl_list_remove(&pointer->sprite_destroy_listener.link);
290 pointer->sprite->configure = NULL;
291 pointer->sprite->configure_private = NULL;
292 pointer->sprite = NULL;
293}
294
295static void
296pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
297{
298 struct weston_pointer *pointer =
299 container_of(listener, struct weston_pointer,
300 sprite_destroy_listener);
301
302 pointer->sprite = NULL;
303}
304
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400305WL_EXPORT struct weston_pointer *
306weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400308 struct weston_pointer *pointer;
309
310 pointer = malloc(sizeof *pointer);
311 if (pointer == NULL)
312 return NULL;
313
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314 memset(pointer, 0, sizeof *pointer);
315 wl_list_init(&pointer->resource_list);
316 pointer->focus_listener.notify = lose_pointer_focus;
317 pointer->default_grab.interface = &default_pointer_grab_interface;
318 pointer->default_grab.pointer = pointer;
319 pointer->grab = &pointer->default_grab;
320 wl_signal_init(&pointer->focus_signal);
321
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400322 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
323
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400324 /* FIXME: Pick better co-ords. */
325 pointer->x = wl_fixed_from_int(100);
326 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400327
328 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400329}
330
331WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400332weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400334 if (pointer->sprite)
335 pointer_unmap_sprite(pointer);
336
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400337 /* XXX: What about pointer->resource_list? */
338 if (pointer->focus_resource)
339 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400340 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341}
342
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400343WL_EXPORT struct weston_keyboard *
344weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400345{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400346 struct weston_keyboard *keyboard;
347
348 keyboard = malloc(sizeof *keyboard);
349 if (keyboard == NULL)
350 return NULL;
351
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400352 memset(keyboard, 0, sizeof *keyboard);
353 wl_list_init(&keyboard->resource_list);
354 wl_array_init(&keyboard->keys);
355 keyboard->focus_listener.notify = lose_keyboard_focus;
356 keyboard->default_grab.interface = &default_keyboard_grab_interface;
357 keyboard->default_grab.keyboard = keyboard;
358 keyboard->grab = &keyboard->default_grab;
359 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400360
361 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400362}
363
364WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400365weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400366{
367 /* XXX: What about keyboard->resource_list? */
368 if (keyboard->focus_resource)
369 wl_list_remove(&keyboard->focus_listener.link);
370 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400371 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372}
373
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400374WL_EXPORT struct weston_touch *
375weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400376{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400377 struct weston_touch *touch;
378
379 touch = malloc(sizeof *touch);
380 if (touch == NULL)
381 return NULL;
382
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400383 memset(touch, 0, sizeof *touch);
384 wl_list_init(&touch->resource_list);
385 touch->focus_listener.notify = lose_touch_focus;
386 touch->default_grab.interface = &default_touch_grab_interface;
387 touch->default_grab.touch = touch;
388 touch->grab = &touch->default_grab;
389 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400390
391 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392}
393
394WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400395weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396{
397 /* XXX: What about touch->resource_list? */
398 if (touch->focus_resource)
399 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400400 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401}
402
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400404seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500406 struct wl_list *link;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400407 enum wl_seat_capability caps = 0;
408
409 if (seat->pointer)
410 caps |= WL_SEAT_CAPABILITY_POINTER;
411 if (seat->keyboard)
412 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
413 if (seat->touch)
414 caps |= WL_SEAT_CAPABILITY_TOUCH;
415
Jason Ekstrand44a38632013-06-14 10:08:00 -0500416 for (link = seat->base_resource_list.next;
417 link != &seat->base_resource_list; link = link->next) {
418 wl_seat_send_capabilities(wl_resource_from_link(link), caps);
419 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400420}
421
422WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400423weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400424 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400425 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400427 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428 struct wl_resource *resource, *kr;
429 struct wl_display *display;
430 uint32_t serial;
431
432 resource = pointer->focus_resource;
433 if (resource && pointer->focus != surface) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500434 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435 serial = wl_display_next_serial(display);
436 wl_pointer_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500437 pointer->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400438 wl_list_remove(&pointer->focus_listener.link);
439 }
440
441 resource = find_resource_for_surface(&pointer->resource_list,
442 surface);
443 if (resource &&
444 (pointer->focus != surface ||
445 pointer->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500446 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447 serial = wl_display_next_serial(display);
448 if (kbd) {
449 kr = find_resource_for_surface(&kbd->resource_list,
450 surface);
451 if (kr) {
452 wl_keyboard_send_modifiers(kr,
453 serial,
454 kbd->modifiers.mods_depressed,
455 kbd->modifiers.mods_latched,
456 kbd->modifiers.mods_locked,
457 kbd->modifiers.group);
458 }
459 }
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500460 wl_pointer_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400461 sx, sy);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500462 wl_resource_add_destroy_listener(resource,
463 &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400464 pointer->focus_serial = serial;
465 }
466
467 pointer->focus_resource = resource;
468 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400469 wl_signal_emit(&pointer->focus_signal, pointer);
470}
471
472WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400473weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400474 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475{
476 struct wl_resource *resource;
477 struct wl_display *display;
478 uint32_t serial;
479
480 if (keyboard->focus_resource && keyboard->focus != surface) {
481 resource = keyboard->focus_resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -0500482 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483 serial = wl_display_next_serial(display);
484 wl_keyboard_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500485 keyboard->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486 wl_list_remove(&keyboard->focus_listener.link);
487 }
488
489 resource = find_resource_for_surface(&keyboard->resource_list,
490 surface);
491 if (resource &&
492 (keyboard->focus != surface ||
493 keyboard->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500494 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495 serial = wl_display_next_serial(display);
496 wl_keyboard_send_modifiers(resource, serial,
497 keyboard->modifiers.mods_depressed,
498 keyboard->modifiers.mods_latched,
499 keyboard->modifiers.mods_locked,
500 keyboard->modifiers.group);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500501 wl_keyboard_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400502 &keyboard->keys);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500503 wl_resource_add_destroy_listener(resource,
504 &keyboard->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505 keyboard->focus_serial = serial;
506 }
507
508 keyboard->focus_resource = resource;
509 keyboard->focus = surface;
510 wl_signal_emit(&keyboard->focus_signal, keyboard);
511}
512
513WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400514weston_keyboard_start_grab(struct weston_keyboard *keyboard,
515 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400516{
517 keyboard->grab = grab;
518 grab->keyboard = keyboard;
519
520 /* XXX focus? */
521}
522
523WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400524weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525{
526 keyboard->grab = &keyboard->default_grab;
527}
528
529WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400530weston_pointer_start_grab(struct weston_pointer *pointer,
531 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400534 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400535 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400536}
537
538WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400539weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400542 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543}
544
545WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400546weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400547{
548 touch->grab = grab;
549 grab->touch = touch;
550}
551
552WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400553weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554{
555 touch->grab = &touch->default_grab;
556}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400557
Rob Bradford806d8c02013-06-25 18:56:41 +0100558WL_EXPORT void
559weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400560{
Rob Bradford806d8c02013-06-25 18:56:41 +0100561 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400562 struct weston_output *output, *prev = NULL;
563 int x, y, old_x, old_y, valid = 0;
564
565 x = wl_fixed_to_int(*fx);
566 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100567 old_x = wl_fixed_to_int(pointer->x);
568 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400569
570 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100571 if (pointer->seat->output && pointer->seat->output != output)
572 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400573 if (pixman_region32_contains_point(&output->region,
574 x, y, NULL))
575 valid = 1;
576 if (pixman_region32_contains_point(&output->region,
577 old_x, old_y, NULL))
578 prev = output;
579 }
580
Rob Bradford66bd9f52013-06-25 18:56:42 +0100581 if (!prev)
582 prev = pointer->seat->output;
583
584 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400585 if (x < prev->x)
586 *fx = wl_fixed_from_int(prev->x);
587 else if (x >= prev->x + prev->width)
588 *fx = wl_fixed_from_int(prev->x +
589 prev->width - 1);
590 if (y < prev->y)
591 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200592 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400593 *fy = wl_fixed_from_int(prev->y +
594 prev->height - 1);
595 }
596}
597
598/* Takes absolute values */
599static void
600move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
601{
602 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400603 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400604 struct weston_output *output;
605 int32_t ix, iy;
606
Rob Bradford806d8c02013-06-25 18:56:41 +0100607 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400608
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400609 pointer->x = x;
610 pointer->y = y;
611
612 ix = wl_fixed_to_int(x);
613 iy = wl_fixed_to_int(y);
614
615 wl_list_for_each(output, &ec->output_list, link)
616 if (output->zoom.active &&
617 pixman_region32_contains_point(&output->region,
618 ix, iy, NULL))
619 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
620
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400621 if (pointer->sprite) {
622 weston_surface_set_position(pointer->sprite,
623 ix - pointer->hotspot_x,
624 iy - pointer->hotspot_y);
625 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400626 }
627}
628
629WL_EXPORT void
630notify_motion(struct weston_seat *seat,
631 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
632{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400633 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400634 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400635
636 weston_compositor_wake(ec);
637
638 move_pointer(seat, pointer->x + dx, pointer->y + dy);
639
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400640 pointer->grab->interface->focus(pointer->grab);
641 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400642}
643
644WL_EXPORT void
645notify_motion_absolute(struct weston_seat *seat,
646 uint32_t time, wl_fixed_t x, wl_fixed_t y)
647{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400649 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400650
651 weston_compositor_wake(ec);
652
653 move_pointer(seat, x, y);
654
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400655 pointer->grab->interface->focus(pointer->grab);
656 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400657}
658
659WL_EXPORT void
660weston_surface_activate(struct weston_surface *surface,
661 struct weston_seat *seat)
662{
663 struct weston_compositor *compositor = seat->compositor;
664
Kristian Høgsberge3148752013-05-06 23:19:49 -0400665 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400666 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400667 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400668 }
669
670 wl_signal_emit(&compositor->activate_signal, surface);
671}
672
673WL_EXPORT void
674notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
675 enum wl_pointer_button_state state)
676{
677 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400678 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400679 struct weston_surface *focus =
680 (struct weston_surface *) pointer->focus;
681 uint32_t serial = wl_display_next_serial(compositor->wl_display);
682
683 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
684 if (compositor->ping_handler && focus)
685 compositor->ping_handler(focus, serial);
686 weston_compositor_idle_inhibit(compositor);
687 if (pointer->button_count == 0) {
688 pointer->grab_button = button;
689 pointer->grab_time = time;
690 pointer->grab_x = pointer->x;
691 pointer->grab_y = pointer->y;
692 }
693 pointer->button_count++;
694 } else {
695 weston_compositor_idle_release(compositor);
696 pointer->button_count--;
697 }
698
699 weston_compositor_run_button_binding(compositor, seat, time, button,
700 state);
701
702 pointer->grab->interface->button(pointer->grab, time, button, state);
703
704 if (pointer->button_count == 1)
705 pointer->grab_serial =
706 wl_display_get_serial(compositor->wl_display);
707}
708
709WL_EXPORT void
710notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
711 wl_fixed_t value)
712{
713 struct weston_compositor *compositor = 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_surface *focus =
716 (struct weston_surface *) pointer->focus;
717 uint32_t serial = wl_display_next_serial(compositor->wl_display);
718
719 if (compositor->ping_handler && focus)
720 compositor->ping_handler(focus, serial);
721
722 weston_compositor_wake(compositor);
723
724 if (!value)
725 return;
726
727 if (weston_compositor_run_axis_binding(compositor, seat,
728 time, axis, value))
729 return;
730
731 if (pointer->focus_resource)
732 wl_pointer_send_axis(pointer->focus_resource, time, axis,
733 value);
734}
735
Rob Bradford382ff462013-06-24 16:52:45 +0100736#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400737WL_EXPORT void
738notify_modifiers(struct weston_seat *seat, uint32_t serial)
739{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400740 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400741 struct weston_keyboard_grab *grab = keyboard->grab;
742 uint32_t mods_depressed, mods_latched, mods_locked, group;
743 uint32_t mods_lookup;
744 enum weston_led leds = 0;
745 int changed = 0;
746
747 /* Serialize and update our internal state, checking to see if it's
748 * different to the previous state. */
749 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
750 XKB_STATE_DEPRESSED);
751 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
752 XKB_STATE_LATCHED);
753 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
754 XKB_STATE_LOCKED);
755 group = xkb_state_serialize_group(seat->xkb_state.state,
756 XKB_STATE_EFFECTIVE);
757
Kristian Høgsberge3148752013-05-06 23:19:49 -0400758 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
759 mods_latched != seat->keyboard->modifiers.mods_latched ||
760 mods_locked != seat->keyboard->modifiers.mods_locked ||
761 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400762 changed = 1;
763
Kristian Høgsberge3148752013-05-06 23:19:49 -0400764 seat->keyboard->modifiers.mods_depressed = mods_depressed;
765 seat->keyboard->modifiers.mods_latched = mods_latched;
766 seat->keyboard->modifiers.mods_locked = mods_locked;
767 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400768
769 /* And update the modifier_state for bindings. */
770 mods_lookup = mods_depressed | mods_latched;
771 seat->modifier_state = 0;
772 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
773 seat->modifier_state |= MODIFIER_CTRL;
774 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
775 seat->modifier_state |= MODIFIER_ALT;
776 if (mods_lookup & (1 << seat->xkb_info.super_mod))
777 seat->modifier_state |= MODIFIER_SUPER;
778 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
779 seat->modifier_state |= MODIFIER_SHIFT;
780
781 /* Finally, notify the compositor that LEDs have changed. */
782 if (xkb_state_led_index_is_active(seat->xkb_state.state,
783 seat->xkb_info.num_led))
784 leds |= LED_NUM_LOCK;
785 if (xkb_state_led_index_is_active(seat->xkb_state.state,
786 seat->xkb_info.caps_led))
787 leds |= LED_CAPS_LOCK;
788 if (xkb_state_led_index_is_active(seat->xkb_state.state,
789 seat->xkb_info.scroll_led))
790 leds |= LED_SCROLL_LOCK;
791 if (leds != seat->xkb_state.leds && seat->led_update)
792 seat->led_update(seat, leds);
793 seat->xkb_state.leds = leds;
794
795 if (changed) {
796 grab->interface->modifiers(grab,
797 serial,
798 keyboard->modifiers.mods_depressed,
799 keyboard->modifiers.mods_latched,
800 keyboard->modifiers.mods_locked,
801 keyboard->modifiers.group);
802 }
803}
804
805static void
806update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
807 enum wl_keyboard_key_state state)
808{
809 enum xkb_key_direction direction;
810
Matt Roper01a92732013-06-24 16:52:44 +0100811 /* Keyboard modifiers don't exist in raw keyboard mode */
812 if (!seat->compositor->use_xkbcommon)
813 return;
814
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400815 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
816 direction = XKB_KEY_DOWN;
817 else
818 direction = XKB_KEY_UP;
819
820 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
821 * broken keycode system, which starts at 8. */
822 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
823
824 notify_modifiers(seat, serial);
825}
Rob Bradford382ff462013-06-24 16:52:45 +0100826#else
827WL_EXPORT void
828notify_modifiers(struct weston_seat *seat, uint32_t serial)
829{
830}
831
832static void
833update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
834 enum wl_keyboard_key_state state)
835{
836}
837#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400838
839WL_EXPORT void
840notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
841 enum wl_keyboard_key_state state,
842 enum weston_key_state_update update_state)
843{
844 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400845 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400846 struct weston_surface *focus =
847 (struct weston_surface *) keyboard->focus;
848 struct weston_keyboard_grab *grab = keyboard->grab;
849 uint32_t serial = wl_display_next_serial(compositor->wl_display);
850 uint32_t *k, *end;
851
852 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
853 if (compositor->ping_handler && focus)
854 compositor->ping_handler(focus, serial);
855
856 weston_compositor_idle_inhibit(compositor);
857 keyboard->grab_key = key;
858 keyboard->grab_time = time;
859 } else {
860 weston_compositor_idle_release(compositor);
861 }
862
863 end = keyboard->keys.data + keyboard->keys.size;
864 for (k = keyboard->keys.data; k < end; k++) {
865 if (*k == key) {
866 /* Ignore server-generated repeats. */
867 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
868 return;
869 *k = *--end;
870 }
871 }
872 keyboard->keys.size = (void *) end - keyboard->keys.data;
873 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
874 k = wl_array_add(&keyboard->keys, sizeof *k);
875 *k = key;
876 }
877
878 if (grab == &keyboard->default_grab ||
879 grab == &keyboard->input_method_grab) {
880 weston_compositor_run_key_binding(compositor, seat, time, key,
881 state);
882 grab = keyboard->grab;
883 }
884
885 grab->interface->key(grab, time, key, state);
886
887 if (update_state == STATE_UPDATE_AUTOMATIC) {
888 update_modifier_state(seat,
889 wl_display_get_serial(compositor->wl_display),
890 key,
891 state);
892 }
893}
894
895WL_EXPORT void
896notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
897 wl_fixed_t x, wl_fixed_t y)
898{
899 struct weston_compositor *compositor = seat->compositor;
900
901 if (output) {
902 move_pointer(seat, x, y);
903 compositor->focus = 1;
904 } else {
905 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400906 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400907 * NULL) here, but somehow that breaks re-entry... */
908 }
909}
910
911static void
912destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
913{
914 struct weston_seat *ws;
915
916 ws = container_of(listener, struct weston_seat,
917 saved_kbd_focus_listener);
918
919 ws->saved_kbd_focus = NULL;
920}
921
922WL_EXPORT void
923notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
924 enum weston_key_state_update update_state)
925{
926 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400927 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400928 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400929 uint32_t *k, serial;
930
931 serial = wl_display_next_serial(compositor->wl_display);
932 wl_array_copy(&keyboard->keys, keys);
933 wl_array_for_each(k, &keyboard->keys) {
934 weston_compositor_idle_inhibit(compositor);
935 if (update_state == STATE_UPDATE_AUTOMATIC)
936 update_modifier_state(seat, serial, *k,
937 WL_KEYBOARD_KEY_STATE_PRESSED);
938 }
939
940 /* Run key bindings after we've updated the state. */
941 wl_array_for_each(k, &keyboard->keys) {
942 weston_compositor_run_key_binding(compositor, seat, 0, *k,
943 WL_KEYBOARD_KEY_STATE_PRESSED);
944 }
945
946 surface = seat->saved_kbd_focus;
947
948 if (surface) {
949 wl_list_remove(&seat->saved_kbd_focus_listener.link);
950 weston_keyboard_set_focus(keyboard, surface);
951 seat->saved_kbd_focus = NULL;
952 }
953}
954
955WL_EXPORT void
956notify_keyboard_focus_out(struct weston_seat *seat)
957{
958 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400959 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400960 uint32_t *k, serial;
961
962 serial = wl_display_next_serial(compositor->wl_display);
963 wl_array_for_each(k, &keyboard->keys) {
964 weston_compositor_idle_release(compositor);
965 update_modifier_state(seat, serial, *k,
966 WL_KEYBOARD_KEY_STATE_RELEASED);
967 }
968
969 seat->modifier_state = 0;
970
971 if (keyboard->focus) {
972 seat->saved_kbd_focus = keyboard->focus;
973 seat->saved_kbd_focus_listener.notify =
974 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500975 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400976 &seat->saved_kbd_focus_listener);
977 }
978
979 weston_keyboard_set_focus(keyboard, NULL);
980 /* FIXME: We really need keyboard grab cancel here to
981 * let the grab shut down properly. As it is we leak
982 * the grab data. */
983 weston_keyboard_end_grab(keyboard);
984}
985
Michael Fua2bb7912013-07-23 15:51:06 +0800986WL_EXPORT void
987weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400988{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400989 struct wl_resource *resource;
990
991 if (seat->touch->focus == surface)
992 return;
993
994 if (seat->touch->focus_resource)
995 wl_list_remove(&seat->touch->focus_listener.link);
996 seat->touch->focus = NULL;
997 seat->touch->focus_resource = NULL;
998
999 if (surface) {
1000 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -04001001 find_resource_for_surface(&seat->touch->resource_list,
1002 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001003 if (!resource) {
1004 weston_log("couldn't find resource\n");
1005 return;
1006 }
1007
1008 seat->touch->focus = surface;
1009 seat->touch->focus_resource = resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -05001010 wl_resource_add_destroy_listener(resource,
1011 &seat->touch->focus_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001012 }
1013}
1014
1015/**
1016 * notify_touch - emulates button touches and notifies surfaces accordingly.
1017 *
1018 * It assumes always the correct cycle sequence until it gets here: touch_down
1019 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1020 * for sending along such order.
1021 *
1022 */
1023WL_EXPORT void
1024notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1025 wl_fixed_t x, wl_fixed_t y, int touch_type)
1026{
1027 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001028 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001029 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001030 struct weston_surface *es;
1031 wl_fixed_t sx, sy;
1032
1033 /* Update grab's global coordinates. */
1034 touch->grab_x = x;
1035 touch->grab_y = y;
1036
1037 switch (touch_type) {
1038 case WL_TOUCH_DOWN:
1039 weston_compositor_idle_inhibit(ec);
1040
1041 seat->num_tp++;
1042
1043 /* the first finger down picks the surface, and all further go
1044 * to that surface for the remainder of the touch session i.e.
1045 * until all touch points are up again. */
1046 if (seat->num_tp == 1) {
1047 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001048 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001049 } else if (touch->focus) {
1050 es = (struct weston_surface *) touch->focus;
1051 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1052 } else {
1053 /* Unexpected condition: We have non-initial touch but
1054 * there is no focused surface.
1055 */
1056 weston_log("touch event received with %d points down"
1057 "but no surface focused\n", seat->num_tp);
1058 return;
1059 }
1060
1061 grab->interface->down(grab, time, touch_id, sx, sy);
1062 break;
1063 case WL_TOUCH_MOTION:
1064 es = (struct weston_surface *) touch->focus;
1065 if (!es)
1066 break;
1067
1068 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1069 grab->interface->motion(grab, time, touch_id, sx, sy);
1070 break;
1071 case WL_TOUCH_UP:
1072 weston_compositor_idle_release(ec);
1073 seat->num_tp--;
1074
1075 grab->interface->up(grab, time, touch_id);
1076 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001077 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001078 break;
1079 }
1080}
1081
1082static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001083pointer_cursor_surface_configure(struct weston_surface *es,
1084 int32_t dx, int32_t dy, int32_t width, int32_t height)
1085{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001086 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001087 int x, y;
1088
1089 if (width == 0)
1090 return;
1091
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001092 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001093
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001094 pointer->hotspot_x -= dx;
1095 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001096
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001097 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1098 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001099
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001100 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001101
1102 empty_region(&es->pending.input);
1103
1104 if (!weston_surface_is_mapped(es)) {
1105 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1106 &es->layer_link);
1107 weston_surface_update_transform(es);
1108 }
1109}
1110
1111static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001112pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1113 uint32_t serial, struct wl_resource *surface_resource,
1114 int32_t x, int32_t y)
1115{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001116 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001117 struct weston_surface *surface = NULL;
1118
1119 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001120 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001121
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001122 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001123 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001124 /* pointer->focus->resource can be NULL. Surfaces like the
1125 black_surface used in shell.c for fullscreen don't have
1126 a resource, but can still have focus */
1127 if (pointer->focus->resource == NULL)
1128 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001129 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001130 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001131 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001132 return;
1133
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001134 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001135 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001136 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001137 WL_DISPLAY_ERROR_INVALID_OBJECT,
1138 "surface->configure already "
1139 "set");
1140 return;
1141 }
1142 }
1143
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001144 if (pointer->sprite)
1145 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001146
1147 if (!surface)
1148 return;
1149
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001150 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001151 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001152
1153 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001154 surface->configure_private = pointer;
1155 pointer->sprite = surface;
1156 pointer->hotspot_x = x;
1157 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001158
1159 if (surface->buffer_ref.buffer)
1160 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1161 weston_surface_buffer_height(surface));
1162}
1163
1164static const struct wl_pointer_interface pointer_interface = {
1165 pointer_set_cursor
1166};
1167
1168static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1170 uint32_t id)
1171{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001172 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001173 struct wl_resource *cr;
1174
Kristian Høgsberge3148752013-05-06 23:19:49 -04001175 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001176 return;
1177
Jason Ekstranda85118c2013-06-27 20:17:02 -05001178 cr = wl_resource_create(client, &wl_pointer_interface,
1179 wl_resource_get_version(resource), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001180 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001181 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1182 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001183
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001184 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001185 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001186 struct weston_surface *surface;
1187 wl_fixed_t sx, sy;
1188
Kristian Høgsberge3148752013-05-06 23:19:49 -04001189 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001191 seat->pointer->x,
1192 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193 &sx,
1194 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001195 weston_pointer_set_focus(seat->pointer,
1196 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001197 sx,
1198 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 }
1200}
1201
1202static void
1203seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1204 uint32_t id)
1205{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001206 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207 struct wl_resource *cr;
1208
Kristian Høgsberge3148752013-05-06 23:19:49 -04001209 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001210 return;
1211
Jason Ekstranda85118c2013-06-27 20:17:02 -05001212 cr = wl_resource_create(client, &wl_keyboard_interface,
1213 wl_resource_get_version(resource), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001214 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001215 wl_resource_set_implementation(cr, NULL, seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216
Matt Roper01a92732013-06-24 16:52:44 +01001217 if (seat->compositor->use_xkbcommon) {
1218 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1219 seat->xkb_info.keymap_fd,
1220 seat->xkb_info.keymap_size);
1221 } else {
1222 int null_fd = open("/dev/null", O_RDONLY);
1223 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1224 null_fd,
1225 0);
1226 close(null_fd);
1227 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228
Kristian Høgsberge3148752013-05-06 23:19:49 -04001229 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001230 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001231 weston_keyboard_set_focus(seat->keyboard,
1232 seat->keyboard->focus);
1233 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001234 }
1235}
1236
1237static void
1238seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1239 uint32_t id)
1240{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001241 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242 struct wl_resource *cr;
1243
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245 return;
1246
Jason Ekstranda85118c2013-06-27 20:17:02 -05001247 cr = wl_resource_create(client, &wl_touch_interface,
1248 wl_resource_get_version(resource), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001249 wl_list_insert(&seat->touch->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001250 wl_resource_set_implementation(cr, NULL, seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001251}
1252
1253static const struct wl_seat_interface seat_interface = {
1254 seat_get_pointer,
1255 seat_get_keyboard,
1256 seat_get_touch,
1257};
1258
1259static void
1260bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1261{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001263 struct wl_resource *resource;
1264 enum wl_seat_capability caps = 0;
1265
Jason Ekstranda85118c2013-06-27 20:17:02 -05001266 resource = wl_resource_create(client,
1267 &wl_seat_interface, MIN(version, 2), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001268 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001269 wl_resource_set_implementation(resource, &seat_interface, data,
1270 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271
1272 if (seat->pointer)
1273 caps |= WL_SEAT_CAPABILITY_POINTER;
1274 if (seat->keyboard)
1275 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1276 if (seat->touch)
1277 caps |= WL_SEAT_CAPABILITY_TOUCH;
1278
1279 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001280 if (version >= 2)
1281 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001282}
1283
Rob Bradford382ff462013-06-24 16:52:45 +01001284#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001285int
1286weston_compositor_xkb_init(struct weston_compositor *ec,
1287 struct xkb_rule_names *names)
1288{
Rob Bradford382ff462013-06-24 16:52:45 +01001289 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001290
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291 if (ec->xkb_context == NULL) {
1292 ec->xkb_context = xkb_context_new(0);
1293 if (ec->xkb_context == NULL) {
1294 weston_log("failed to create XKB context\n");
1295 return -1;
1296 }
1297 }
1298
1299 if (names)
1300 ec->xkb_names = *names;
1301 if (!ec->xkb_names.rules)
1302 ec->xkb_names.rules = strdup("evdev");
1303 if (!ec->xkb_names.model)
1304 ec->xkb_names.model = strdup("pc105");
1305 if (!ec->xkb_names.layout)
1306 ec->xkb_names.layout = strdup("us");
1307
1308 return 0;
1309}
1310
1311static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1312{
1313 if (xkb_info->keymap)
1314 xkb_map_unref(xkb_info->keymap);
1315
1316 if (xkb_info->keymap_area)
1317 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1318 if (xkb_info->keymap_fd >= 0)
1319 close(xkb_info->keymap_fd);
1320}
1321
1322void
1323weston_compositor_xkb_destroy(struct weston_compositor *ec)
1324{
Matt Roper01a92732013-06-24 16:52:44 +01001325 /*
1326 * If we're operating in raw keyboard mode, we never initialized
1327 * libxkbcommon so there's no cleanup to do either.
1328 */
1329 if (!ec->use_xkbcommon)
1330 return;
1331
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001332 free((char *) ec->xkb_names.rules);
1333 free((char *) ec->xkb_names.model);
1334 free((char *) ec->xkb_names.layout);
1335 free((char *) ec->xkb_names.variant);
1336 free((char *) ec->xkb_names.options);
1337
1338 xkb_info_destroy(&ec->xkb_info);
1339 xkb_context_unref(ec->xkb_context);
1340}
1341
1342static int
1343weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1344{
1345 char *keymap_str;
1346
1347 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1348 XKB_MOD_NAME_SHIFT);
1349 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1350 XKB_MOD_NAME_CAPS);
1351 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1352 XKB_MOD_NAME_CTRL);
1353 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1354 XKB_MOD_NAME_ALT);
1355 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1356 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1357 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1358 XKB_MOD_NAME_LOGO);
1359 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1360
1361 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1362 XKB_LED_NAME_NUM);
1363 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1364 XKB_LED_NAME_CAPS);
1365 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1366 XKB_LED_NAME_SCROLL);
1367
1368 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1369 if (keymap_str == NULL) {
1370 weston_log("failed to get string version of keymap\n");
1371 return -1;
1372 }
1373 xkb_info->keymap_size = strlen(keymap_str) + 1;
1374
1375 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1376 if (xkb_info->keymap_fd < 0) {
1377 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1378 (unsigned long) xkb_info->keymap_size);
1379 goto err_keymap_str;
1380 }
1381
1382 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1383 PROT_READ | PROT_WRITE,
1384 MAP_SHARED, xkb_info->keymap_fd, 0);
1385 if (xkb_info->keymap_area == MAP_FAILED) {
1386 weston_log("failed to mmap() %lu bytes\n",
1387 (unsigned long) xkb_info->keymap_size);
1388 goto err_dev_zero;
1389 }
1390 strcpy(xkb_info->keymap_area, keymap_str);
1391 free(keymap_str);
1392
1393 return 0;
1394
1395err_dev_zero:
1396 close(xkb_info->keymap_fd);
1397 xkb_info->keymap_fd = -1;
1398err_keymap_str:
1399 free(keymap_str);
1400 return -1;
1401}
1402
1403static int
1404weston_compositor_build_global_keymap(struct weston_compositor *ec)
1405{
1406 if (ec->xkb_info.keymap != NULL)
1407 return 0;
1408
1409 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1410 &ec->xkb_names,
1411 0);
1412 if (ec->xkb_info.keymap == NULL) {
1413 weston_log("failed to compile global XKB keymap\n");
1414 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1415 "options %s\n",
1416 ec->xkb_names.rules, ec->xkb_names.model,
1417 ec->xkb_names.layout, ec->xkb_names.variant,
1418 ec->xkb_names.options);
1419 return -1;
1420 }
1421
1422 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1423 return -1;
1424
1425 return 0;
1426}
Rob Bradford382ff462013-06-24 16:52:45 +01001427#else
1428int
1429weston_compositor_xkb_init(struct weston_compositor *ec,
1430 struct xkb_rule_names *names)
1431{
1432 return 0;
1433}
1434
1435void
1436weston_compositor_xkb_destroy(struct weston_compositor *ec)
1437{
1438}
1439#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001440
1441WL_EXPORT int
1442weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1443{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001444 struct weston_keyboard *keyboard;
1445
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001446 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001447 return 0;
1448
Rob Bradford382ff462013-06-24 16:52:45 +01001449#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001450 if (seat->compositor->use_xkbcommon) {
1451 if (keymap != NULL) {
1452 seat->xkb_info.keymap = xkb_map_ref(keymap);
1453 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1454 return -1;
1455 } else {
1456 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1457 return -1;
1458 seat->xkb_info = seat->compositor->xkb_info;
1459 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1460 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001461
Matt Roper01a92732013-06-24 16:52:44 +01001462 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1463 if (seat->xkb_state.state == NULL) {
1464 weston_log("failed to initialise XKB state\n");
1465 return -1;
1466 }
1467
1468 seat->xkb_state.leds = 0;
1469 }
Rob Bradford382ff462013-06-24 16:52:45 +01001470#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001471
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001472 keyboard = weston_keyboard_create();
1473 if (keyboard == NULL) {
1474 weston_log("failed to allocate weston keyboard struct\n");
1475 return -1;
1476 }
1477
1478 seat->keyboard = keyboard;
1479 keyboard->seat = seat;
1480
1481 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001482
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483 return 0;
1484}
1485
1486WL_EXPORT void
1487weston_seat_init_pointer(struct weston_seat *seat)
1488{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001489 struct weston_pointer *pointer;
1490
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001491 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001492 return;
1493
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001494 pointer = weston_pointer_create();
1495 if (pointer == NULL)
1496 return;
1497
1498 seat->pointer = pointer;
1499 pointer->seat = seat;
1500
1501 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001502}
1503
1504WL_EXPORT void
1505weston_seat_init_touch(struct weston_seat *seat)
1506{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001507 struct weston_touch *touch;
1508
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001509 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001510 return;
1511
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001512 touch = weston_touch_create();
1513 if (touch == NULL)
1514 return;
1515
1516 seat->touch = touch;
1517 touch->seat = seat;
1518
1519 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001520}
1521
1522WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001523weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1524 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001525{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001526 memset(seat, 0, sizeof *seat);
1527
Kristian Høgsberge3148752013-05-06 23:19:49 -04001528 seat->selection_data_source = NULL;
1529 wl_list_init(&seat->base_resource_list);
1530 wl_signal_init(&seat->selection_signal);
1531 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001532 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001533
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001534 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 2,
1535 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001536
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001537 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001538 seat->modifier_state = 0;
1539 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001540 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001541
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001542 wl_list_insert(ec->seat_list.prev, &seat->link);
1543
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001544 clipboard_create(seat);
1545
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001546 wl_signal_emit(&ec->seat_created_signal, seat);
1547}
1548
1549WL_EXPORT void
1550weston_seat_release(struct weston_seat *seat)
1551{
1552 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553
Rob Bradford382ff462013-06-24 16:52:45 +01001554#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001555 if (seat->compositor->use_xkbcommon) {
1556 if (seat->xkb_state.state != NULL)
1557 xkb_state_unref(seat->xkb_state.state);
1558 xkb_info_destroy(&seat->xkb_info);
1559 }
Rob Bradford382ff462013-06-24 16:52:45 +01001560#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001561
Kristian Høgsberge3148752013-05-06 23:19:49 -04001562 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001563 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001564 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001565 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001566 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001567 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001568
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001569 free (seat->seat_name);
1570
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001571 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001572
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001573 wl_signal_emit(&seat->destroy_signal, seat);
1574}