blob: a86bb7e586cd572f978c65315206abf8662e8fe2 [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øgsberg02bbabb2013-05-06 22:15:05 -040049 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050
Kristian Høgsberg6848c252013-05-08 22:02:59 -040051 if (seat->pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052 return;
53
Kristian Høgsberg6848c252013-05-08 22:02:59 -040054 interface = seat->pointer->grab->interface;
55 interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040056}
57
58static void
59weston_compositor_idle_inhibit(struct weston_compositor *compositor)
60{
61 weston_compositor_wake(compositor);
62 compositor->idle_inhibit++;
63}
64
65static void
66weston_compositor_idle_release(struct weston_compositor *compositor)
67{
68 compositor->idle_inhibit--;
69 weston_compositor_wake(compositor);
70}
71
Kristian Høgsberg2158a882013-04-18 15:07:39 -040072static void
73lose_pointer_focus(struct wl_listener *listener, void *data)
74{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040075 struct weston_pointer *pointer =
76 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040077
78 pointer->focus_resource = NULL;
79}
80
81static void
82lose_keyboard_focus(struct wl_listener *listener, void *data)
83{
Kristian Høgsberg29139d42013-04-18 15:25:39 -040084 struct weston_keyboard *keyboard =
85 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040086
87 keyboard->focus_resource = NULL;
88}
89
90static void
91lose_touch_focus(struct wl_listener *listener, void *data)
92{
Kristian Høgsberge329f362013-05-06 22:19:57 -040093 struct weston_touch *touch =
94 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040095
96 touch->focus_resource = NULL;
97}
98
99static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400100default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400102 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400103 struct weston_surface *surface;
104 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400105
106 if (pointer->button_count > 0)
107 return;
108
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400109 surface = weston_compositor_pick_surface(pointer->seat->compositor,
110 pointer->x, pointer->y,
111 &sx, &sy);
112
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400113 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400114 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115}
116
117static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400118default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400119{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400120 struct weston_pointer *pointer = grab->pointer;
121 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400122
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400123 if (pointer->focus_resource) {
124 weston_surface_from_global_fixed(pointer->focus,
125 pointer->x, pointer->y,
126 &sx, &sy);
127 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
128 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400129}
130
131static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400132default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400133 uint32_t time, uint32_t button, uint32_t state_w)
134{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400135 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400136 struct weston_compositor *compositor = pointer->seat->compositor;
137 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400138 struct wl_resource *resource;
139 uint32_t serial;
140 enum wl_pointer_button_state state = state_w;
141 struct wl_display *display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400142 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143
144 resource = pointer->focus_resource;
145 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500146 display = wl_client_get_display(wl_resource_get_client(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;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400174 struct wl_display *display;
175 uint32_t serial;
176
177 if (touch->focus_resource && touch->focus) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500178 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179 serial = wl_display_next_serial(display);
180 wl_touch_send_down(touch->focus_resource, serial, time,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500181 touch->focus->resource,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400182 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400183 }
184}
185
Kristian Høgsberge329f362013-05-06 22:19:57 -0400186static void
187default_grab_touch_up(struct weston_touch_grab *grab,
188 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400189{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400190 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 struct wl_display *display;
192 uint32_t serial;
193
194 if (touch->focus_resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500195 display = wl_client_get_display(wl_resource_get_client(touch->focus_resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196 serial = wl_display_next_serial(display);
197 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
198 }
199}
200
Kristian Høgsberge329f362013-05-06 22:19:57 -0400201static void
202default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
203 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400204{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400205 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400206
207 if (touch->focus_resource) {
208 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400209 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400210 }
211}
212
Kristian Høgsberge329f362013-05-06 22:19:57 -0400213static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400214 default_grab_touch_down,
215 default_grab_touch_up,
216 default_grab_touch_motion
217};
218
219static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400220default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400221 uint32_t time, uint32_t key, uint32_t state)
222{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400223 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224 struct wl_resource *resource;
225 struct wl_display *display;
226 uint32_t serial;
227
228 resource = keyboard->focus_resource;
229 if (resource) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500230 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400231 serial = wl_display_next_serial(display);
232 wl_keyboard_send_key(resource, serial, time, key, state);
233 }
234}
235
236static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400237find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400238{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400239 if (!surface)
240 return NULL;
241
Jason Ekstrand44a38632013-06-14 10:08:00 -0500242 if (!surface->resource)
243 return NULL;
244
245 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246}
247
248static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400249default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400250 uint32_t mods_depressed, uint32_t mods_latched,
251 uint32_t mods_locked, uint32_t group)
252{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400253 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400254 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255 struct wl_resource *resource, *pr;
256
257 resource = keyboard->focus_resource;
258 if (!resource)
259 return;
260
261 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
262 mods_latched, mods_locked, group);
263
264 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
265 pr = find_resource_for_surface(&keyboard->resource_list,
266 pointer->focus);
267 if (pr) {
268 wl_keyboard_send_modifiers(pr,
269 serial,
270 keyboard->modifiers.mods_depressed,
271 keyboard->modifiers.mods_latched,
272 keyboard->modifiers.mods_locked,
273 keyboard->modifiers.group);
274 }
275 }
276}
277
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400278static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400279 default_keyboard_grab_interface = {
280 default_grab_key,
281 default_grab_modifiers,
282};
283
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400284static void
285pointer_unmap_sprite(struct weston_pointer *pointer)
286{
287 if (weston_surface_is_mapped(pointer->sprite))
288 weston_surface_unmap(pointer->sprite);
289
290 wl_list_remove(&pointer->sprite_destroy_listener.link);
291 pointer->sprite->configure = NULL;
292 pointer->sprite->configure_private = NULL;
293 pointer->sprite = NULL;
294}
295
296static void
297pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
298{
299 struct weston_pointer *pointer =
300 container_of(listener, struct weston_pointer,
301 sprite_destroy_listener);
302
303 pointer->sprite = NULL;
304}
305
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400306WL_EXPORT struct weston_pointer *
307weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400309 struct weston_pointer *pointer;
310
311 pointer = malloc(sizeof *pointer);
312 if (pointer == NULL)
313 return NULL;
314
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400315 memset(pointer, 0, sizeof *pointer);
316 wl_list_init(&pointer->resource_list);
317 pointer->focus_listener.notify = lose_pointer_focus;
318 pointer->default_grab.interface = &default_pointer_grab_interface;
319 pointer->default_grab.pointer = pointer;
320 pointer->grab = &pointer->default_grab;
321 wl_signal_init(&pointer->focus_signal);
322
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400323 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
324
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400325 /* FIXME: Pick better co-ords. */
326 pointer->x = wl_fixed_from_int(100);
327 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400328
329 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330}
331
332WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400333weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400335 if (pointer->sprite)
336 pointer_unmap_sprite(pointer);
337
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338 /* XXX: What about pointer->resource_list? */
339 if (pointer->focus_resource)
340 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400341 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342}
343
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400344WL_EXPORT struct weston_keyboard *
345weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400346{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400347 struct weston_keyboard *keyboard;
348
349 keyboard = malloc(sizeof *keyboard);
350 if (keyboard == NULL)
351 return NULL;
352
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400353 memset(keyboard, 0, sizeof *keyboard);
354 wl_list_init(&keyboard->resource_list);
355 wl_array_init(&keyboard->keys);
356 keyboard->focus_listener.notify = lose_keyboard_focus;
357 keyboard->default_grab.interface = &default_keyboard_grab_interface;
358 keyboard->default_grab.keyboard = keyboard;
359 keyboard->grab = &keyboard->default_grab;
360 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400361
362 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363}
364
365WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400366weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400367{
368 /* XXX: What about keyboard->resource_list? */
369 if (keyboard->focus_resource)
370 wl_list_remove(&keyboard->focus_listener.link);
371 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400372 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400373}
374
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400375WL_EXPORT struct weston_touch *
376weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400377{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400378 struct weston_touch *touch;
379
380 touch = malloc(sizeof *touch);
381 if (touch == NULL)
382 return NULL;
383
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400384 memset(touch, 0, sizeof *touch);
385 wl_list_init(&touch->resource_list);
386 touch->focus_listener.notify = lose_touch_focus;
387 touch->default_grab.interface = &default_touch_grab_interface;
388 touch->default_grab.touch = touch;
389 touch->grab = &touch->default_grab;
390 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400391
392 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393}
394
395WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400396weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397{
398 /* XXX: What about touch->resource_list? */
399 if (touch->focus_resource)
400 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400401 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402}
403
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400405seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500407 struct wl_list *link;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 enum wl_seat_capability caps = 0;
409
410 if (seat->pointer)
411 caps |= WL_SEAT_CAPABILITY_POINTER;
412 if (seat->keyboard)
413 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
414 if (seat->touch)
415 caps |= WL_SEAT_CAPABILITY_TOUCH;
416
Jason Ekstrand44a38632013-06-14 10:08:00 -0500417 for (link = seat->base_resource_list.next;
418 link != &seat->base_resource_list; link = link->next) {
419 wl_seat_send_capabilities(wl_resource_from_link(link), caps);
420 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421}
422
423WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400424weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400425 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400426 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400427{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400428 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429 struct wl_resource *resource, *kr;
430 struct wl_display *display;
431 uint32_t serial;
432
433 resource = pointer->focus_resource;
434 if (resource && pointer->focus != surface) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500435 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400436 serial = wl_display_next_serial(display);
437 wl_pointer_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500438 pointer->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439 wl_list_remove(&pointer->focus_listener.link);
440 }
441
442 resource = find_resource_for_surface(&pointer->resource_list,
443 surface);
444 if (resource &&
445 (pointer->focus != surface ||
446 pointer->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500447 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400448 serial = wl_display_next_serial(display);
449 if (kbd) {
450 kr = find_resource_for_surface(&kbd->resource_list,
451 surface);
452 if (kr) {
453 wl_keyboard_send_modifiers(kr,
454 serial,
455 kbd->modifiers.mods_depressed,
456 kbd->modifiers.mods_latched,
457 kbd->modifiers.mods_locked,
458 kbd->modifiers.group);
459 }
460 }
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500461 wl_pointer_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462 sx, sy);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500463 wl_resource_add_destroy_listener(resource,
464 &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465 pointer->focus_serial = serial;
466 }
467
468 pointer->focus_resource = resource;
469 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 wl_signal_emit(&pointer->focus_signal, pointer);
471}
472
473WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400474weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400475 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476{
477 struct wl_resource *resource;
478 struct wl_display *display;
479 uint32_t serial;
480
481 if (keyboard->focus_resource && keyboard->focus != surface) {
482 resource = keyboard->focus_resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -0500483 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400484 serial = wl_display_next_serial(display);
485 wl_keyboard_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500486 keyboard->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487 wl_list_remove(&keyboard->focus_listener.link);
488 }
489
490 resource = find_resource_for_surface(&keyboard->resource_list,
491 surface);
492 if (resource &&
493 (keyboard->focus != surface ||
494 keyboard->focus_resource != resource)) {
Jason Ekstrand44a38632013-06-14 10:08:00 -0500495 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496 serial = wl_display_next_serial(display);
497 wl_keyboard_send_modifiers(resource, serial,
498 keyboard->modifiers.mods_depressed,
499 keyboard->modifiers.mods_latched,
500 keyboard->modifiers.mods_locked,
501 keyboard->modifiers.group);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500502 wl_keyboard_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400503 &keyboard->keys);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500504 wl_resource_add_destroy_listener(resource,
505 &keyboard->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506 keyboard->focus_serial = serial;
507 }
508
509 keyboard->focus_resource = resource;
510 keyboard->focus = surface;
511 wl_signal_emit(&keyboard->focus_signal, keyboard);
512}
513
514WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400515weston_keyboard_start_grab(struct weston_keyboard *keyboard,
516 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517{
518 keyboard->grab = grab;
519 grab->keyboard = keyboard;
520
521 /* XXX focus? */
522}
523
524WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400525weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526{
527 keyboard->grab = &keyboard->default_grab;
528}
529
530WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400531weston_pointer_start_grab(struct weston_pointer *pointer,
532 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400534 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400535
536 pointer->grab = grab;
537 interface = pointer->grab->interface;
538 grab->pointer = pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400539 interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540}
541
542WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400543weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400545 const struct weston_pointer_grab_interface *interface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546
547 pointer->grab = &pointer->default_grab;
548 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400549 interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550}
551
552WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400553weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554{
555 touch->grab = grab;
556 grab->touch = touch;
557}
558
559WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400560weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400561{
562 touch->grab = &touch->default_grab;
563}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400564
Rob Bradford806d8c02013-06-25 18:56:41 +0100565WL_EXPORT void
566weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400567{
Rob Bradford806d8c02013-06-25 18:56:41 +0100568 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400569 struct weston_output *output, *prev = NULL;
570 int x, y, old_x, old_y, valid = 0;
571
572 x = wl_fixed_to_int(*fx);
573 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100574 old_x = wl_fixed_to_int(pointer->x);
575 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400576
577 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100578 if (pointer->seat->output && pointer->seat->output != output)
579 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400580 if (pixman_region32_contains_point(&output->region,
581 x, y, NULL))
582 valid = 1;
583 if (pixman_region32_contains_point(&output->region,
584 old_x, old_y, NULL))
585 prev = output;
586 }
587
Rob Bradford66bd9f52013-06-25 18:56:42 +0100588 if (!prev)
589 prev = pointer->seat->output;
590
591 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400592 if (x < prev->x)
593 *fx = wl_fixed_from_int(prev->x);
594 else if (x >= prev->x + prev->width)
595 *fx = wl_fixed_from_int(prev->x +
596 prev->width - 1);
597 if (y < prev->y)
598 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200599 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400600 *fy = wl_fixed_from_int(prev->y +
601 prev->height - 1);
602 }
603}
604
605/* Takes absolute values */
606static void
607move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
608{
609 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400610 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400611 struct weston_output *output;
612 int32_t ix, iy;
613
Rob Bradford806d8c02013-06-25 18:56:41 +0100614 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400615
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400616 pointer->x = x;
617 pointer->y = y;
618
619 ix = wl_fixed_to_int(x);
620 iy = wl_fixed_to_int(y);
621
622 wl_list_for_each(output, &ec->output_list, link)
623 if (output->zoom.active &&
624 pixman_region32_contains_point(&output->region,
625 ix, iy, NULL))
626 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
627
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400628 if (pointer->sprite) {
629 weston_surface_set_position(pointer->sprite,
630 ix - pointer->hotspot_x,
631 iy - pointer->hotspot_y);
632 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400633 }
634}
635
636WL_EXPORT void
637notify_motion(struct weston_seat *seat,
638 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
639{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400640 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400641 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400642 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400643
644 weston_compositor_wake(ec);
645
646 move_pointer(seat, pointer->x + dx, pointer->y + dy);
647
648 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400649 interface->focus(pointer->grab);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400650 interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400651}
652
653WL_EXPORT void
654notify_motion_absolute(struct weston_seat *seat,
655 uint32_t time, wl_fixed_t x, wl_fixed_t y)
656{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400657 const struct weston_pointer_grab_interface *interface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400658 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400659 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400660
661 weston_compositor_wake(ec);
662
663 move_pointer(seat, x, y);
664
665 interface = pointer->grab->interface;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400666 interface->focus(pointer->grab);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400667 interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400668}
669
670WL_EXPORT void
671weston_surface_activate(struct weston_surface *surface,
672 struct weston_seat *seat)
673{
674 struct weston_compositor *compositor = seat->compositor;
675
Kristian Høgsberge3148752013-05-06 23:19:49 -0400676 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400677 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400678 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400679 }
680
681 wl_signal_emit(&compositor->activate_signal, surface);
682}
683
684WL_EXPORT void
685notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
686 enum wl_pointer_button_state state)
687{
688 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400689 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400690 struct weston_surface *focus =
691 (struct weston_surface *) pointer->focus;
692 uint32_t serial = wl_display_next_serial(compositor->wl_display);
693
694 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
695 if (compositor->ping_handler && focus)
696 compositor->ping_handler(focus, serial);
697 weston_compositor_idle_inhibit(compositor);
698 if (pointer->button_count == 0) {
699 pointer->grab_button = button;
700 pointer->grab_time = time;
701 pointer->grab_x = pointer->x;
702 pointer->grab_y = pointer->y;
703 }
704 pointer->button_count++;
705 } else {
706 weston_compositor_idle_release(compositor);
707 pointer->button_count--;
708 }
709
710 weston_compositor_run_button_binding(compositor, seat, time, button,
711 state);
712
713 pointer->grab->interface->button(pointer->grab, time, button, state);
714
715 if (pointer->button_count == 1)
716 pointer->grab_serial =
717 wl_display_get_serial(compositor->wl_display);
718}
719
720WL_EXPORT void
721notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
722 wl_fixed_t value)
723{
724 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400725 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400726 struct weston_surface *focus =
727 (struct weston_surface *) pointer->focus;
728 uint32_t serial = wl_display_next_serial(compositor->wl_display);
729
730 if (compositor->ping_handler && focus)
731 compositor->ping_handler(focus, serial);
732
733 weston_compositor_wake(compositor);
734
735 if (!value)
736 return;
737
738 if (weston_compositor_run_axis_binding(compositor, seat,
739 time, axis, value))
740 return;
741
742 if (pointer->focus_resource)
743 wl_pointer_send_axis(pointer->focus_resource, time, axis,
744 value);
745}
746
747WL_EXPORT void
748notify_modifiers(struct weston_seat *seat, uint32_t serial)
749{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400750 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400751 struct weston_keyboard_grab *grab = keyboard->grab;
752 uint32_t mods_depressed, mods_latched, mods_locked, group;
753 uint32_t mods_lookup;
754 enum weston_led leds = 0;
755 int changed = 0;
756
757 /* Serialize and update our internal state, checking to see if it's
758 * different to the previous state. */
759 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
760 XKB_STATE_DEPRESSED);
761 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
762 XKB_STATE_LATCHED);
763 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
764 XKB_STATE_LOCKED);
765 group = xkb_state_serialize_group(seat->xkb_state.state,
766 XKB_STATE_EFFECTIVE);
767
Kristian Høgsberge3148752013-05-06 23:19:49 -0400768 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
769 mods_latched != seat->keyboard->modifiers.mods_latched ||
770 mods_locked != seat->keyboard->modifiers.mods_locked ||
771 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400772 changed = 1;
773
Kristian Høgsberge3148752013-05-06 23:19:49 -0400774 seat->keyboard->modifiers.mods_depressed = mods_depressed;
775 seat->keyboard->modifiers.mods_latched = mods_latched;
776 seat->keyboard->modifiers.mods_locked = mods_locked;
777 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400778
779 /* And update the modifier_state for bindings. */
780 mods_lookup = mods_depressed | mods_latched;
781 seat->modifier_state = 0;
782 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
783 seat->modifier_state |= MODIFIER_CTRL;
784 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
785 seat->modifier_state |= MODIFIER_ALT;
786 if (mods_lookup & (1 << seat->xkb_info.super_mod))
787 seat->modifier_state |= MODIFIER_SUPER;
788 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
789 seat->modifier_state |= MODIFIER_SHIFT;
790
791 /* Finally, notify the compositor that LEDs have changed. */
792 if (xkb_state_led_index_is_active(seat->xkb_state.state,
793 seat->xkb_info.num_led))
794 leds |= LED_NUM_LOCK;
795 if (xkb_state_led_index_is_active(seat->xkb_state.state,
796 seat->xkb_info.caps_led))
797 leds |= LED_CAPS_LOCK;
798 if (xkb_state_led_index_is_active(seat->xkb_state.state,
799 seat->xkb_info.scroll_led))
800 leds |= LED_SCROLL_LOCK;
801 if (leds != seat->xkb_state.leds && seat->led_update)
802 seat->led_update(seat, leds);
803 seat->xkb_state.leds = leds;
804
805 if (changed) {
806 grab->interface->modifiers(grab,
807 serial,
808 keyboard->modifiers.mods_depressed,
809 keyboard->modifiers.mods_latched,
810 keyboard->modifiers.mods_locked,
811 keyboard->modifiers.group);
812 }
813}
814
815static void
816update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
817 enum wl_keyboard_key_state state)
818{
819 enum xkb_key_direction direction;
820
Matt Roper01a92732013-06-24 16:52:44 +0100821 /* Keyboard modifiers don't exist in raw keyboard mode */
822 if (!seat->compositor->use_xkbcommon)
823 return;
824
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400825 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
826 direction = XKB_KEY_DOWN;
827 else
828 direction = XKB_KEY_UP;
829
830 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
831 * broken keycode system, which starts at 8. */
832 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
833
834 notify_modifiers(seat, serial);
835}
836
837WL_EXPORT void
838notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
839 enum wl_keyboard_key_state state,
840 enum weston_key_state_update update_state)
841{
842 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400843 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400844 struct weston_surface *focus =
845 (struct weston_surface *) keyboard->focus;
846 struct weston_keyboard_grab *grab = keyboard->grab;
847 uint32_t serial = wl_display_next_serial(compositor->wl_display);
848 uint32_t *k, *end;
849
850 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
851 if (compositor->ping_handler && focus)
852 compositor->ping_handler(focus, serial);
853
854 weston_compositor_idle_inhibit(compositor);
855 keyboard->grab_key = key;
856 keyboard->grab_time = time;
857 } else {
858 weston_compositor_idle_release(compositor);
859 }
860
861 end = keyboard->keys.data + keyboard->keys.size;
862 for (k = keyboard->keys.data; k < end; k++) {
863 if (*k == key) {
864 /* Ignore server-generated repeats. */
865 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
866 return;
867 *k = *--end;
868 }
869 }
870 keyboard->keys.size = (void *) end - keyboard->keys.data;
871 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
872 k = wl_array_add(&keyboard->keys, sizeof *k);
873 *k = key;
874 }
875
876 if (grab == &keyboard->default_grab ||
877 grab == &keyboard->input_method_grab) {
878 weston_compositor_run_key_binding(compositor, seat, time, key,
879 state);
880 grab = keyboard->grab;
881 }
882
883 grab->interface->key(grab, time, key, state);
884
885 if (update_state == STATE_UPDATE_AUTOMATIC) {
886 update_modifier_state(seat,
887 wl_display_get_serial(compositor->wl_display),
888 key,
889 state);
890 }
891}
892
893WL_EXPORT void
894notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
895 wl_fixed_t x, wl_fixed_t y)
896{
897 struct weston_compositor *compositor = seat->compositor;
898
899 if (output) {
900 move_pointer(seat, x, y);
901 compositor->focus = 1;
902 } else {
903 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400904 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400905 * NULL) here, but somehow that breaks re-entry... */
906 }
907}
908
909static void
910destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
911{
912 struct weston_seat *ws;
913
914 ws = container_of(listener, struct weston_seat,
915 saved_kbd_focus_listener);
916
917 ws->saved_kbd_focus = NULL;
918}
919
920WL_EXPORT void
921notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
922 enum weston_key_state_update update_state)
923{
924 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400925 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400926 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400927 uint32_t *k, serial;
928
929 serial = wl_display_next_serial(compositor->wl_display);
930 wl_array_copy(&keyboard->keys, keys);
931 wl_array_for_each(k, &keyboard->keys) {
932 weston_compositor_idle_inhibit(compositor);
933 if (update_state == STATE_UPDATE_AUTOMATIC)
934 update_modifier_state(seat, serial, *k,
935 WL_KEYBOARD_KEY_STATE_PRESSED);
936 }
937
938 /* Run key bindings after we've updated the state. */
939 wl_array_for_each(k, &keyboard->keys) {
940 weston_compositor_run_key_binding(compositor, seat, 0, *k,
941 WL_KEYBOARD_KEY_STATE_PRESSED);
942 }
943
944 surface = seat->saved_kbd_focus;
945
946 if (surface) {
947 wl_list_remove(&seat->saved_kbd_focus_listener.link);
948 weston_keyboard_set_focus(keyboard, surface);
949 seat->saved_kbd_focus = NULL;
950 }
951}
952
953WL_EXPORT void
954notify_keyboard_focus_out(struct weston_seat *seat)
955{
956 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400957 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400958 uint32_t *k, serial;
959
960 serial = wl_display_next_serial(compositor->wl_display);
961 wl_array_for_each(k, &keyboard->keys) {
962 weston_compositor_idle_release(compositor);
963 update_modifier_state(seat, serial, *k,
964 WL_KEYBOARD_KEY_STATE_RELEASED);
965 }
966
967 seat->modifier_state = 0;
968
969 if (keyboard->focus) {
970 seat->saved_kbd_focus = keyboard->focus;
971 seat->saved_kbd_focus_listener.notify =
972 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500973 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400974 &seat->saved_kbd_focus_listener);
975 }
976
977 weston_keyboard_set_focus(keyboard, NULL);
978 /* FIXME: We really need keyboard grab cancel here to
979 * let the grab shut down properly. As it is we leak
980 * the grab data. */
981 weston_keyboard_end_grab(keyboard);
982}
983
984static void
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400985touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400986{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400987 struct wl_resource *resource;
988
989 if (seat->touch->focus == surface)
990 return;
991
992 if (seat->touch->focus_resource)
993 wl_list_remove(&seat->touch->focus_listener.link);
994 seat->touch->focus = NULL;
995 seat->touch->focus_resource = NULL;
996
997 if (surface) {
998 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -0400999 find_resource_for_surface(&seat->touch->resource_list,
1000 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001001 if (!resource) {
1002 weston_log("couldn't find resource\n");
1003 return;
1004 }
1005
1006 seat->touch->focus = surface;
1007 seat->touch->focus_resource = resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -05001008 wl_resource_add_destroy_listener(resource,
1009 &seat->touch->focus_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001010 }
1011}
1012
1013/**
1014 * notify_touch - emulates button touches and notifies surfaces accordingly.
1015 *
1016 * It assumes always the correct cycle sequence until it gets here: touch_down
1017 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1018 * for sending along such order.
1019 *
1020 */
1021WL_EXPORT void
1022notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1023 wl_fixed_t x, wl_fixed_t y, int touch_type)
1024{
1025 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001026 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001027 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001028 struct weston_surface *es;
1029 wl_fixed_t sx, sy;
1030
1031 /* Update grab's global coordinates. */
1032 touch->grab_x = x;
1033 touch->grab_y = y;
1034
1035 switch (touch_type) {
1036 case WL_TOUCH_DOWN:
1037 weston_compositor_idle_inhibit(ec);
1038
1039 seat->num_tp++;
1040
1041 /* the first finger down picks the surface, and all further go
1042 * to that surface for the remainder of the touch session i.e.
1043 * until all touch points are up again. */
1044 if (seat->num_tp == 1) {
1045 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001046 touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001047 } else if (touch->focus) {
1048 es = (struct weston_surface *) touch->focus;
1049 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1050 } else {
1051 /* Unexpected condition: We have non-initial touch but
1052 * there is no focused surface.
1053 */
1054 weston_log("touch event received with %d points down"
1055 "but no surface focused\n", seat->num_tp);
1056 return;
1057 }
1058
1059 grab->interface->down(grab, time, touch_id, sx, sy);
1060 break;
1061 case WL_TOUCH_MOTION:
1062 es = (struct weston_surface *) touch->focus;
1063 if (!es)
1064 break;
1065
1066 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1067 grab->interface->motion(grab, time, touch_id, sx, sy);
1068 break;
1069 case WL_TOUCH_UP:
1070 weston_compositor_idle_release(ec);
1071 seat->num_tp--;
1072
1073 grab->interface->up(grab, time, touch_id);
1074 if (seat->num_tp == 0)
1075 touch_set_focus(seat, NULL);
1076 break;
1077 }
1078}
1079
1080static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001081pointer_cursor_surface_configure(struct weston_surface *es,
1082 int32_t dx, int32_t dy, int32_t width, int32_t height)
1083{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001084 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001085 int x, y;
1086
1087 if (width == 0)
1088 return;
1089
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001090 assert(es == pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001091
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001092 pointer->hotspot_x -= dx;
1093 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001094
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001095 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1096 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001097
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001098 weston_surface_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001099
1100 empty_region(&es->pending.input);
1101
1102 if (!weston_surface_is_mapped(es)) {
1103 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1104 &es->layer_link);
1105 weston_surface_update_transform(es);
1106 }
1107}
1108
1109static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001110pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1111 uint32_t serial, struct wl_resource *surface_resource,
1112 int32_t x, int32_t y)
1113{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001114 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001115 struct weston_surface *surface = NULL;
1116
1117 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001118 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001119
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001120 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001121 return;
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001122 /* pointer->focus->resource can be NULL. Surfaces like the
1123 black_surface used in shell.c for fullscreen don't have
1124 a resource, but can still have focus */
1125 if (pointer->focus->resource == NULL)
1126 return;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001127 if (wl_resource_get_client(pointer->focus->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001128 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001129 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001130 return;
1131
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001132 if (surface && surface != pointer->sprite) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001133 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001134 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001135 WL_DISPLAY_ERROR_INVALID_OBJECT,
1136 "surface->configure already "
1137 "set");
1138 return;
1139 }
1140 }
1141
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001142 if (pointer->sprite)
1143 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001144
1145 if (!surface)
1146 return;
1147
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001148 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001149 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001150
1151 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001152 surface->configure_private = pointer;
1153 pointer->sprite = surface;
1154 pointer->hotspot_x = x;
1155 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001156
1157 if (surface->buffer_ref.buffer)
1158 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1159 weston_surface_buffer_height(surface));
1160}
1161
1162static const struct wl_pointer_interface pointer_interface = {
1163 pointer_set_cursor
1164};
1165
1166static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001167seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1168 uint32_t id)
1169{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001170 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001171 struct wl_resource *cr;
1172
Kristian Høgsberge3148752013-05-06 23:19:49 -04001173 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001174 return;
1175
1176 cr = wl_client_add_object(client, &wl_pointer_interface,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001177 &pointer_interface, id, seat->pointer);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001178 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1179 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001180
Kristian Høgsberge3148752013-05-06 23:19:49 -04001181 if (seat->pointer->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001182 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001183 struct weston_surface *surface;
1184 wl_fixed_t sx, sy;
1185
Kristian Høgsberge3148752013-05-06 23:19:49 -04001186 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001188 seat->pointer->x,
1189 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190 &sx,
1191 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001192 weston_pointer_set_focus(seat->pointer,
1193 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001194 sx,
1195 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001196 }
1197}
1198
1199static void
1200seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1201 uint32_t id)
1202{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001203 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001204 struct wl_resource *cr;
1205
Kristian Høgsberge3148752013-05-06 23:19:49 -04001206 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207 return;
1208
1209 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
1210 seat);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001211 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1212 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001213
Matt Roper01a92732013-06-24 16:52:44 +01001214 if (seat->compositor->use_xkbcommon) {
1215 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1216 seat->xkb_info.keymap_fd,
1217 seat->xkb_info.keymap_size);
1218 } else {
1219 int null_fd = open("/dev/null", O_RDONLY);
1220 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1221 null_fd,
1222 0);
1223 close(null_fd);
1224 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001225
Kristian Høgsberge3148752013-05-06 23:19:49 -04001226 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001227 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001228 weston_keyboard_set_focus(seat->keyboard,
1229 seat->keyboard->focus);
1230 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001231 }
1232}
1233
1234static void
1235seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1236 uint32_t id)
1237{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001238 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001239 struct wl_resource *cr;
1240
Kristian Høgsberge3148752013-05-06 23:19:49 -04001241 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242 return;
1243
1244 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001245 wl_list_insert(&seat->touch->resource_list, wl_resource_get_link(cr));
1246 wl_resource_set_destructor(cr, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001247}
1248
1249static const struct wl_seat_interface seat_interface = {
1250 seat_get_pointer,
1251 seat_get_keyboard,
1252 seat_get_touch,
1253};
1254
1255static void
1256bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1257{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001258 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001259 struct wl_resource *resource;
1260 enum wl_seat_capability caps = 0;
1261
1262 resource = wl_client_add_object(client, &wl_seat_interface,
1263 &seat_interface, id, data);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001264 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1265 wl_resource_set_destructor(resource, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001266
1267 if (seat->pointer)
1268 caps |= WL_SEAT_CAPABILITY_POINTER;
1269 if (seat->keyboard)
1270 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1271 if (seat->touch)
1272 caps |= WL_SEAT_CAPABILITY_TOUCH;
1273
1274 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001275 if (version >= 2)
1276 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001277}
1278
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001279int
1280weston_compositor_xkb_init(struct weston_compositor *ec,
1281 struct xkb_rule_names *names)
1282{
Matt Roper01a92732013-06-24 16:52:44 +01001283 /*
1284 * If we're operating in raw keyboard mode, libxkbcommon isn't used and
1285 * shouldn't be initialized.
1286 */
1287 if (!ec->use_xkbcommon)
1288 return 0;
1289
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001290 if (ec->xkb_context == NULL) {
1291 ec->xkb_context = xkb_context_new(0);
1292 if (ec->xkb_context == NULL) {
1293 weston_log("failed to create XKB context\n");
1294 return -1;
1295 }
1296 }
1297
1298 if (names)
1299 ec->xkb_names = *names;
1300 if (!ec->xkb_names.rules)
1301 ec->xkb_names.rules = strdup("evdev");
1302 if (!ec->xkb_names.model)
1303 ec->xkb_names.model = strdup("pc105");
1304 if (!ec->xkb_names.layout)
1305 ec->xkb_names.layout = strdup("us");
1306
1307 return 0;
1308}
1309
1310static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
1311{
1312 if (xkb_info->keymap)
1313 xkb_map_unref(xkb_info->keymap);
1314
1315 if (xkb_info->keymap_area)
1316 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1317 if (xkb_info->keymap_fd >= 0)
1318 close(xkb_info->keymap_fd);
1319}
1320
1321void
1322weston_compositor_xkb_destroy(struct weston_compositor *ec)
1323{
Matt Roper01a92732013-06-24 16:52:44 +01001324 /*
1325 * If we're operating in raw keyboard mode, we never initialized
1326 * libxkbcommon so there's no cleanup to do either.
1327 */
1328 if (!ec->use_xkbcommon)
1329 return;
1330
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001331 free((char *) ec->xkb_names.rules);
1332 free((char *) ec->xkb_names.model);
1333 free((char *) ec->xkb_names.layout);
1334 free((char *) ec->xkb_names.variant);
1335 free((char *) ec->xkb_names.options);
1336
1337 xkb_info_destroy(&ec->xkb_info);
1338 xkb_context_unref(ec->xkb_context);
1339}
1340
1341static int
1342weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
1343{
1344 char *keymap_str;
1345
1346 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1347 XKB_MOD_NAME_SHIFT);
1348 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1349 XKB_MOD_NAME_CAPS);
1350 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1351 XKB_MOD_NAME_CTRL);
1352 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1353 XKB_MOD_NAME_ALT);
1354 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1355 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1356 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1357 XKB_MOD_NAME_LOGO);
1358 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1359
1360 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1361 XKB_LED_NAME_NUM);
1362 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1363 XKB_LED_NAME_CAPS);
1364 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1365 XKB_LED_NAME_SCROLL);
1366
1367 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1368 if (keymap_str == NULL) {
1369 weston_log("failed to get string version of keymap\n");
1370 return -1;
1371 }
1372 xkb_info->keymap_size = strlen(keymap_str) + 1;
1373
1374 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1375 if (xkb_info->keymap_fd < 0) {
1376 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1377 (unsigned long) xkb_info->keymap_size);
1378 goto err_keymap_str;
1379 }
1380
1381 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1382 PROT_READ | PROT_WRITE,
1383 MAP_SHARED, xkb_info->keymap_fd, 0);
1384 if (xkb_info->keymap_area == MAP_FAILED) {
1385 weston_log("failed to mmap() %lu bytes\n",
1386 (unsigned long) xkb_info->keymap_size);
1387 goto err_dev_zero;
1388 }
1389 strcpy(xkb_info->keymap_area, keymap_str);
1390 free(keymap_str);
1391
1392 return 0;
1393
1394err_dev_zero:
1395 close(xkb_info->keymap_fd);
1396 xkb_info->keymap_fd = -1;
1397err_keymap_str:
1398 free(keymap_str);
1399 return -1;
1400}
1401
1402static int
1403weston_compositor_build_global_keymap(struct weston_compositor *ec)
1404{
1405 if (ec->xkb_info.keymap != NULL)
1406 return 0;
1407
1408 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
1409 &ec->xkb_names,
1410 0);
1411 if (ec->xkb_info.keymap == NULL) {
1412 weston_log("failed to compile global XKB keymap\n");
1413 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1414 "options %s\n",
1415 ec->xkb_names.rules, ec->xkb_names.model,
1416 ec->xkb_names.layout, ec->xkb_names.variant,
1417 ec->xkb_names.options);
1418 return -1;
1419 }
1420
1421 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
1422 return -1;
1423
1424 return 0;
1425}
1426
1427WL_EXPORT int
1428weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1429{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001430 struct weston_keyboard *keyboard;
1431
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001432 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001433 return 0;
1434
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001435
Matt Roper01a92732013-06-24 16:52:44 +01001436 if (seat->compositor->use_xkbcommon) {
1437 if (keymap != NULL) {
1438 seat->xkb_info.keymap = xkb_map_ref(keymap);
1439 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
1440 return -1;
1441 } else {
1442 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1443 return -1;
1444 seat->xkb_info = seat->compositor->xkb_info;
1445 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
1446 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001447
Matt Roper01a92732013-06-24 16:52:44 +01001448 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
1449 if (seat->xkb_state.state == NULL) {
1450 weston_log("failed to initialise XKB state\n");
1451 return -1;
1452 }
1453
1454 seat->xkb_state.leds = 0;
1455 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001456
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001457 keyboard = weston_keyboard_create();
1458 if (keyboard == NULL) {
1459 weston_log("failed to allocate weston keyboard struct\n");
1460 return -1;
1461 }
1462
1463 seat->keyboard = keyboard;
1464 keyboard->seat = seat;
1465
1466 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001467
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001468 return 0;
1469}
1470
1471WL_EXPORT void
1472weston_seat_init_pointer(struct weston_seat *seat)
1473{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001474 struct weston_pointer *pointer;
1475
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001476 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001477 return;
1478
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001479 pointer = weston_pointer_create();
1480 if (pointer == NULL)
1481 return;
1482
1483 seat->pointer = pointer;
1484 pointer->seat = seat;
1485
1486 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487}
1488
1489WL_EXPORT void
1490weston_seat_init_touch(struct weston_seat *seat)
1491{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001492 struct weston_touch *touch;
1493
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001494 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001495 return;
1496
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001497 touch = weston_touch_create();
1498 if (touch == NULL)
1499 return;
1500
1501 seat->touch = touch;
1502 touch->seat = seat;
1503
1504 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001505}
1506
1507WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001508weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1509 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001510{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001511 memset(seat, 0, sizeof *seat);
1512
Kristian Høgsberge3148752013-05-06 23:19:49 -04001513 seat->selection_data_source = NULL;
1514 wl_list_init(&seat->base_resource_list);
1515 wl_signal_init(&seat->selection_signal);
1516 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001517 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001518
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
1520 bind_seat);
1521
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001522 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001523 seat->modifier_state = 0;
1524 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001525 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001526
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001527 wl_list_insert(ec->seat_list.prev, &seat->link);
1528
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001529 clipboard_create(seat);
1530
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001531 wl_signal_emit(&ec->seat_created_signal, seat);
1532}
1533
1534WL_EXPORT void
1535weston_seat_release(struct weston_seat *seat)
1536{
1537 wl_list_remove(&seat->link);
1538 /* The global object is destroyed at wl_display_destroy() time. */
1539
Matt Roper01a92732013-06-24 16:52:44 +01001540 if (seat->compositor->use_xkbcommon) {
1541 if (seat->xkb_state.state != NULL)
1542 xkb_state_unref(seat->xkb_state.state);
1543 xkb_info_destroy(&seat->xkb_info);
1544 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001545
Kristian Høgsberge3148752013-05-06 23:19:49 -04001546 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001547 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001548 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001549 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001550 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001551 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001552
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001553 free (seat->seat_name);
1554
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001555 wl_signal_emit(&seat->destroy_signal, seat);
1556}