blob: 9c30460aa2c608391d3081b8efaa8b9967af2777 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010023#include "config.h"
24
Kristian Høgsberg2158a882013-04-18 15:07:39 -040025#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040028#include <sys/mman.h>
29#include <assert.h>
30#include <unistd.h>
Matt Roper01a92732013-06-24 16:52:44 +010031#include <fcntl.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040033#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040034#include "compositor.h"
35
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040036static void
37empty_region(pixman_region32_t *region)
38{
39 pixman_region32_fini(region);
40 pixman_region32_init(region);
41}
42
43static void unbind_resource(struct wl_resource *resource)
44{
Jason Ekstrand44a38632013-06-14 10:08:00 -050045 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040046}
47
48void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040049weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050{
Kristian Høgsbergda751b82013-07-04 00:58:07 -040051 const struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052
Kristian Høgsbergda751b82013-07-04 00:58:07 -040053 if (pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040054 return;
55
Kristian Høgsbergda751b82013-07-04 00:58:07 -040056 pointer->grab->interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040057}
58
59static void
60weston_compositor_idle_inhibit(struct weston_compositor *compositor)
61{
62 weston_compositor_wake(compositor);
63 compositor->idle_inhibit++;
64}
65
66static void
67weston_compositor_idle_release(struct weston_compositor *compositor)
68{
69 compositor->idle_inhibit--;
70 weston_compositor_wake(compositor);
71}
72
Kristian Høgsberg2158a882013-04-18 15:07:39 -040073static void
74lose_pointer_focus(struct wl_listener *listener, void *data)
75{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040076 struct weston_pointer *pointer =
77 container_of(listener, struct weston_pointer, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078
79 pointer->focus_resource = NULL;
80}
81
82static void
83lose_keyboard_focus(struct wl_listener *listener, void *data)
84{
Kristian Høgsberg29139d42013-04-18 15:25:39 -040085 struct weston_keyboard *keyboard =
86 container_of(listener, struct weston_keyboard, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040087
88 keyboard->focus_resource = NULL;
89}
90
91static void
92lose_touch_focus(struct wl_listener *listener, void *data)
93{
Kristian Høgsberge329f362013-05-06 22:19:57 -040094 struct weston_touch *touch =
95 container_of(listener, struct weston_touch, focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040096
97 touch->focus_resource = NULL;
98}
99
100static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400101default_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400102{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400103 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400104 struct weston_surface *surface;
105 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400106
107 if (pointer->button_count > 0)
108 return;
109
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400110 surface = weston_compositor_pick_surface(pointer->seat->compositor,
111 pointer->x, pointer->y,
112 &sx, &sy);
113
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400114 if (pointer->focus != surface)
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400115 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116}
117
118static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400119default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400120{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400121 struct weston_pointer *pointer = grab->pointer;
122 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400123
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400124 if (pointer->focus_resource) {
125 weston_surface_from_global_fixed(pointer->focus,
126 pointer->x, pointer->y,
127 &sx, &sy);
128 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
129 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400130}
131
132static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400133default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400134 uint32_t time, uint32_t button, uint32_t state_w)
135{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400136 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400137 struct weston_compositor *compositor = pointer->seat->compositor;
138 struct weston_surface *surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400139 struct wl_resource *resource;
140 uint32_t serial;
141 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100142 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400143 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400144
145 resource = pointer->focus_resource;
146 if (resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400147 serial = wl_display_next_serial(display);
148 wl_pointer_send_button(resource, serial, time, button, state_w);
149 }
150
151 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400152 state == WL_POINTER_BUTTON_STATE_RELEASED) {
153 surface = weston_compositor_pick_surface(compositor,
154 pointer->x,
155 pointer->y,
156 &sx, &sy);
157
158 weston_pointer_set_focus(pointer, surface, sx, sy);
159 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400160}
161
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400162static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163 default_pointer_grab_interface = {
164 default_grab_focus,
165 default_grab_motion,
166 default_grab_button
167};
168
Kristian Høgsberge329f362013-05-06 22:19:57 -0400169static void
170default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
171 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400172{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400173 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100174 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400175 uint32_t serial;
176
177 if (touch->focus_resource && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400178 serial = wl_display_next_serial(display);
179 wl_touch_send_down(touch->focus_resource, serial, time,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500180 touch->focus->resource,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400181 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400182 }
183}
184
Kristian Høgsberge329f362013-05-06 22:19:57 -0400185static void
186default_grab_touch_up(struct weston_touch_grab *grab,
187 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400188{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400189 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100190 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 uint32_t serial;
192
193 if (touch->focus_resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194 serial = wl_display_next_serial(display);
195 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
196 }
197}
198
Kristian Høgsberge329f362013-05-06 22:19:57 -0400199static void
200default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
201 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400202{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400203 struct weston_touch *touch = grab->touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400204
205 if (touch->focus_resource) {
206 wl_touch_send_motion(touch->focus_resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400207 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208 }
209}
210
Kristian Høgsberge329f362013-05-06 22:19:57 -0400211static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212 default_grab_touch_down,
213 default_grab_touch_up,
214 default_grab_touch_motion
215};
216
217static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400218default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400219 uint32_t time, uint32_t key, uint32_t state)
220{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400221 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400222 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100223 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224 uint32_t serial;
225
226 resource = keyboard->focus_resource;
227 if (resource) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228 serial = wl_display_next_serial(display);
229 wl_keyboard_send_key(resource, serial, time, key, state);
230 }
231}
232
233static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400234find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400235{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400236 if (!surface)
237 return NULL;
238
Jason Ekstrand44a38632013-06-14 10:08:00 -0500239 if (!surface->resource)
240 return NULL;
241
242 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243}
244
245static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400246default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247 uint32_t mods_depressed, uint32_t mods_latched,
248 uint32_t mods_locked, uint32_t group)
249{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400250 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400251 struct weston_pointer *pointer = keyboard->seat->pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252 struct wl_resource *resource, *pr;
253
254 resource = keyboard->focus_resource;
255 if (!resource)
256 return;
257
258 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
259 mods_latched, mods_locked, group);
260
261 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
262 pr = find_resource_for_surface(&keyboard->resource_list,
263 pointer->focus);
264 if (pr) {
265 wl_keyboard_send_modifiers(pr,
266 serial,
267 keyboard->modifiers.mods_depressed,
268 keyboard->modifiers.mods_latched,
269 keyboard->modifiers.mods_locked,
270 keyboard->modifiers.group);
271 }
272 }
273}
274
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400275static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400276 default_keyboard_grab_interface = {
277 default_grab_key,
278 default_grab_modifiers,
279};
280
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400281static void
282pointer_unmap_sprite(struct weston_pointer *pointer)
283{
284 if (weston_surface_is_mapped(pointer->sprite))
285 weston_surface_unmap(pointer->sprite);
286
287 wl_list_remove(&pointer->sprite_destroy_listener.link);
288 pointer->sprite->configure = NULL;
289 pointer->sprite->configure_private = NULL;
290 pointer->sprite = NULL;
291}
292
293static void
294pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
295{
296 struct weston_pointer *pointer =
297 container_of(listener, struct weston_pointer,
298 sprite_destroy_listener);
299
300 pointer->sprite = NULL;
301}
302
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400303WL_EXPORT struct weston_pointer *
304weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400306 struct weston_pointer *pointer;
307
Peter Huttererf3d62272013-08-08 11:57:05 +1000308 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400309 if (pointer == NULL)
310 return NULL;
311
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400312 wl_list_init(&pointer->resource_list);
313 pointer->focus_listener.notify = lose_pointer_focus;
314 pointer->default_grab.interface = &default_pointer_grab_interface;
315 pointer->default_grab.pointer = pointer;
316 pointer->grab = &pointer->default_grab;
317 wl_signal_init(&pointer->focus_signal);
318
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400319 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
320
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321 /* FIXME: Pick better co-ords. */
322 pointer->x = wl_fixed_from_int(100);
323 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400324
325 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326}
327
328WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400329weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400331 if (pointer->sprite)
332 pointer_unmap_sprite(pointer);
333
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400334 /* XXX: What about pointer->resource_list? */
335 if (pointer->focus_resource)
336 wl_list_remove(&pointer->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400337 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400338}
339
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400340WL_EXPORT struct weston_keyboard *
341weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400343 struct weston_keyboard *keyboard;
344
Peter Huttererf3d62272013-08-08 11:57:05 +1000345 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400346 if (keyboard == NULL)
347 return NULL;
348
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400349 wl_list_init(&keyboard->resource_list);
350 wl_array_init(&keyboard->keys);
351 keyboard->focus_listener.notify = lose_keyboard_focus;
352 keyboard->default_grab.interface = &default_keyboard_grab_interface;
353 keyboard->default_grab.keyboard = keyboard;
354 keyboard->grab = &keyboard->default_grab;
355 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400356
357 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358}
359
360WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400361weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400362{
363 /* XXX: What about keyboard->resource_list? */
364 if (keyboard->focus_resource)
365 wl_list_remove(&keyboard->focus_listener.link);
366 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400367 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400368}
369
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400370WL_EXPORT struct weston_touch *
371weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400373 struct weston_touch *touch;
374
Peter Huttererf3d62272013-08-08 11:57:05 +1000375 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400376 if (touch == NULL)
377 return NULL;
378
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379 wl_list_init(&touch->resource_list);
380 touch->focus_listener.notify = lose_touch_focus;
381 touch->default_grab.interface = &default_touch_grab_interface;
382 touch->default_grab.touch = touch;
383 touch->grab = &touch->default_grab;
384 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400385
386 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400387}
388
389WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400390weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391{
392 /* XXX: What about touch->resource_list? */
393 if (touch->focus_resource)
394 wl_list_remove(&touch->focus_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400395 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396}
397
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400399seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100402 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403
404 if (seat->pointer)
405 caps |= WL_SEAT_CAPABILITY_POINTER;
406 if (seat->keyboard)
407 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
408 if (seat->touch)
409 caps |= WL_SEAT_CAPABILITY_TOUCH;
410
Rob Bradford6e737f52013-09-06 17:48:19 +0100411 wl_resource_for_each(resource, &seat->base_resource_list) {
412 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500413 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414}
415
416WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400417weston_pointer_set_focus(struct weston_pointer *pointer,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400418 struct weston_surface *surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400419 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400420{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400421 struct weston_keyboard *kbd = pointer->seat->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422 struct wl_resource *resource, *kr;
Rob Bradford880ebc72013-07-22 17:31:38 +0100423 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424 uint32_t serial;
425
426 resource = pointer->focus_resource;
427 if (resource && pointer->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428 serial = wl_display_next_serial(display);
429 wl_pointer_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500430 pointer->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400431 wl_list_remove(&pointer->focus_listener.link);
432 }
433
434 resource = find_resource_for_surface(&pointer->resource_list,
435 surface);
436 if (resource &&
437 (pointer->focus != surface ||
438 pointer->focus_resource != resource)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439 serial = wl_display_next_serial(display);
440 if (kbd) {
441 kr = find_resource_for_surface(&kbd->resource_list,
442 surface);
443 if (kr) {
444 wl_keyboard_send_modifiers(kr,
445 serial,
446 kbd->modifiers.mods_depressed,
447 kbd->modifiers.mods_latched,
448 kbd->modifiers.mods_locked,
449 kbd->modifiers.group);
450 }
451 }
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500452 wl_pointer_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400453 sx, sy);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500454 wl_resource_add_destroy_listener(resource,
455 &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456 pointer->focus_serial = serial;
457 }
458
459 pointer->focus_resource = resource;
460 pointer->focus = surface;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400461 wl_signal_emit(&pointer->focus_signal, pointer);
462}
463
464WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400465weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400466 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467{
468 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100469 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 uint32_t serial;
471
472 if (keyboard->focus_resource && keyboard->focus != surface) {
473 resource = keyboard->focus_resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400474 serial = wl_display_next_serial(display);
475 wl_keyboard_send_leave(resource, serial,
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500476 keyboard->focus->resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400477 wl_list_remove(&keyboard->focus_listener.link);
478 }
479
480 resource = find_resource_for_surface(&keyboard->resource_list,
481 surface);
482 if (resource &&
483 (keyboard->focus != surface ||
484 keyboard->focus_resource != resource)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400485 serial = wl_display_next_serial(display);
486 wl_keyboard_send_modifiers(resource, serial,
487 keyboard->modifiers.mods_depressed,
488 keyboard->modifiers.mods_latched,
489 keyboard->modifiers.mods_locked,
490 keyboard->modifiers.group);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500491 wl_keyboard_send_enter(resource, serial, surface->resource,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400492 &keyboard->keys);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500493 wl_resource_add_destroy_listener(resource,
494 &keyboard->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495 keyboard->focus_serial = serial;
496 }
497
498 keyboard->focus_resource = resource;
499 keyboard->focus = surface;
500 wl_signal_emit(&keyboard->focus_signal, keyboard);
501}
502
503WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400504weston_keyboard_start_grab(struct weston_keyboard *keyboard,
505 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506{
507 keyboard->grab = grab;
508 grab->keyboard = keyboard;
509
510 /* XXX focus? */
511}
512
513WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400514weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400515{
516 keyboard->grab = &keyboard->default_grab;
517}
518
519WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400520weston_pointer_start_grab(struct weston_pointer *pointer,
521 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400523 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400525 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526}
527
528WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400529weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400530{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400532 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533}
534
535WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400536weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400537{
538 touch->grab = grab;
539 grab->touch = touch;
540}
541
542WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400543weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544{
545 touch->grab = &touch->default_grab;
546}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400547
Rob Bradford806d8c02013-06-25 18:56:41 +0100548WL_EXPORT void
549weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400550{
Rob Bradford806d8c02013-06-25 18:56:41 +0100551 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400552 struct weston_output *output, *prev = NULL;
553 int x, y, old_x, old_y, valid = 0;
554
555 x = wl_fixed_to_int(*fx);
556 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100557 old_x = wl_fixed_to_int(pointer->x);
558 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400559
560 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100561 if (pointer->seat->output && pointer->seat->output != output)
562 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400563 if (pixman_region32_contains_point(&output->region,
564 x, y, NULL))
565 valid = 1;
566 if (pixman_region32_contains_point(&output->region,
567 old_x, old_y, NULL))
568 prev = output;
569 }
570
Rob Bradford66bd9f52013-06-25 18:56:42 +0100571 if (!prev)
572 prev = pointer->seat->output;
573
574 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400575 if (x < prev->x)
576 *fx = wl_fixed_from_int(prev->x);
577 else if (x >= prev->x + prev->width)
578 *fx = wl_fixed_from_int(prev->x +
579 prev->width - 1);
580 if (y < prev->y)
581 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200582 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400583 *fy = wl_fixed_from_int(prev->y +
584 prev->height - 1);
585 }
586}
587
588/* Takes absolute values */
589static void
590move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
591{
592 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400593 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400594 struct weston_output *output;
595 int32_t ix, iy;
596
Rob Bradford806d8c02013-06-25 18:56:41 +0100597 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400598
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400599 pointer->x = x;
600 pointer->y = y;
601
602 ix = wl_fixed_to_int(x);
603 iy = wl_fixed_to_int(y);
604
605 wl_list_for_each(output, &ec->output_list, link)
606 if (output->zoom.active &&
607 pixman_region32_contains_point(&output->region,
608 ix, iy, NULL))
609 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
610
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400611 if (pointer->sprite) {
612 weston_surface_set_position(pointer->sprite,
613 ix - pointer->hotspot_x,
614 iy - pointer->hotspot_y);
615 weston_surface_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400616 }
617}
618
619WL_EXPORT void
620notify_motion(struct weston_seat *seat,
621 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
622{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400623 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400624 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400625
626 weston_compositor_wake(ec);
627
628 move_pointer(seat, pointer->x + dx, pointer->y + dy);
629
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400630 pointer->grab->interface->focus(pointer->grab);
631 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400632}
633
634WL_EXPORT void
635notify_motion_absolute(struct weston_seat *seat,
636 uint32_t time, wl_fixed_t x, wl_fixed_t y)
637{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400638 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400639 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400640
641 weston_compositor_wake(ec);
642
643 move_pointer(seat, x, y);
644
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400645 pointer->grab->interface->focus(pointer->grab);
646 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400647}
648
649WL_EXPORT void
650weston_surface_activate(struct weston_surface *surface,
651 struct weston_seat *seat)
652{
653 struct weston_compositor *compositor = seat->compositor;
654
Kristian Høgsberge3148752013-05-06 23:19:49 -0400655 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400656 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400657 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400658 }
659
660 wl_signal_emit(&compositor->activate_signal, surface);
661}
662
663WL_EXPORT void
664notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
665 enum wl_pointer_button_state state)
666{
667 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400668 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400669 struct weston_surface *focus =
670 (struct weston_surface *) pointer->focus;
671 uint32_t serial = wl_display_next_serial(compositor->wl_display);
672
673 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
674 if (compositor->ping_handler && focus)
675 compositor->ping_handler(focus, serial);
676 weston_compositor_idle_inhibit(compositor);
677 if (pointer->button_count == 0) {
678 pointer->grab_button = button;
679 pointer->grab_time = time;
680 pointer->grab_x = pointer->x;
681 pointer->grab_y = pointer->y;
682 }
683 pointer->button_count++;
684 } else {
685 weston_compositor_idle_release(compositor);
686 pointer->button_count--;
687 }
688
689 weston_compositor_run_button_binding(compositor, seat, time, button,
690 state);
691
692 pointer->grab->interface->button(pointer->grab, time, button, state);
693
694 if (pointer->button_count == 1)
695 pointer->grab_serial =
696 wl_display_get_serial(compositor->wl_display);
697}
698
699WL_EXPORT void
700notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
701 wl_fixed_t value)
702{
703 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400704 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400705 struct weston_surface *focus =
706 (struct weston_surface *) pointer->focus;
707 uint32_t serial = wl_display_next_serial(compositor->wl_display);
708
709 if (compositor->ping_handler && focus)
710 compositor->ping_handler(focus, serial);
711
712 weston_compositor_wake(compositor);
713
714 if (!value)
715 return;
716
717 if (weston_compositor_run_axis_binding(compositor, seat,
718 time, axis, value))
719 return;
720
721 if (pointer->focus_resource)
722 wl_pointer_send_axis(pointer->focus_resource, time, axis,
723 value);
724}
725
Rob Bradford382ff462013-06-24 16:52:45 +0100726#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400727WL_EXPORT void
728notify_modifiers(struct weston_seat *seat, uint32_t serial)
729{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400730 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400731 struct weston_keyboard_grab *grab = keyboard->grab;
732 uint32_t mods_depressed, mods_latched, mods_locked, group;
733 uint32_t mods_lookup;
734 enum weston_led leds = 0;
735 int changed = 0;
736
737 /* Serialize and update our internal state, checking to see if it's
738 * different to the previous state. */
739 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
740 XKB_STATE_DEPRESSED);
741 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
742 XKB_STATE_LATCHED);
743 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
744 XKB_STATE_LOCKED);
745 group = xkb_state_serialize_group(seat->xkb_state.state,
746 XKB_STATE_EFFECTIVE);
747
Kristian Høgsberge3148752013-05-06 23:19:49 -0400748 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
749 mods_latched != seat->keyboard->modifiers.mods_latched ||
750 mods_locked != seat->keyboard->modifiers.mods_locked ||
751 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400752 changed = 1;
753
Kristian Høgsberge3148752013-05-06 23:19:49 -0400754 seat->keyboard->modifiers.mods_depressed = mods_depressed;
755 seat->keyboard->modifiers.mods_latched = mods_latched;
756 seat->keyboard->modifiers.mods_locked = mods_locked;
757 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400758
759 /* And update the modifier_state for bindings. */
760 mods_lookup = mods_depressed | mods_latched;
761 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000762 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400763 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000764 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400765 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000766 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400767 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000768 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400769 seat->modifier_state |= MODIFIER_SHIFT;
770
771 /* Finally, notify the compositor that LEDs have changed. */
772 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000773 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400774 leds |= LED_NUM_LOCK;
775 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000776 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400777 leds |= LED_CAPS_LOCK;
778 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000779 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400780 leds |= LED_SCROLL_LOCK;
781 if (leds != seat->xkb_state.leds && seat->led_update)
782 seat->led_update(seat, leds);
783 seat->xkb_state.leds = leds;
784
785 if (changed) {
786 grab->interface->modifiers(grab,
787 serial,
788 keyboard->modifiers.mods_depressed,
789 keyboard->modifiers.mods_latched,
790 keyboard->modifiers.mods_locked,
791 keyboard->modifiers.group);
792 }
793}
794
795static void
796update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
797 enum wl_keyboard_key_state state)
798{
799 enum xkb_key_direction direction;
800
Matt Roper01a92732013-06-24 16:52:44 +0100801 /* Keyboard modifiers don't exist in raw keyboard mode */
802 if (!seat->compositor->use_xkbcommon)
803 return;
804
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400805 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
806 direction = XKB_KEY_DOWN;
807 else
808 direction = XKB_KEY_UP;
809
810 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
811 * broken keycode system, which starts at 8. */
812 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
813
814 notify_modifiers(seat, serial);
815}
Rob Bradford382ff462013-06-24 16:52:45 +0100816#else
817WL_EXPORT void
818notify_modifiers(struct weston_seat *seat, uint32_t serial)
819{
820}
821
822static void
823update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
824 enum wl_keyboard_key_state state)
825{
826}
827#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400828
829WL_EXPORT void
830notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
831 enum wl_keyboard_key_state state,
832 enum weston_key_state_update update_state)
833{
834 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400835 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400836 struct weston_surface *focus =
837 (struct weston_surface *) keyboard->focus;
838 struct weston_keyboard_grab *grab = keyboard->grab;
839 uint32_t serial = wl_display_next_serial(compositor->wl_display);
840 uint32_t *k, *end;
841
842 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
843 if (compositor->ping_handler && focus)
844 compositor->ping_handler(focus, serial);
845
846 weston_compositor_idle_inhibit(compositor);
847 keyboard->grab_key = key;
848 keyboard->grab_time = time;
849 } else {
850 weston_compositor_idle_release(compositor);
851 }
852
853 end = keyboard->keys.data + keyboard->keys.size;
854 for (k = keyboard->keys.data; k < end; k++) {
855 if (*k == key) {
856 /* Ignore server-generated repeats. */
857 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
858 return;
859 *k = *--end;
860 }
861 }
862 keyboard->keys.size = (void *) end - keyboard->keys.data;
863 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
864 k = wl_array_add(&keyboard->keys, sizeof *k);
865 *k = key;
866 }
867
868 if (grab == &keyboard->default_grab ||
869 grab == &keyboard->input_method_grab) {
870 weston_compositor_run_key_binding(compositor, seat, time, key,
871 state);
872 grab = keyboard->grab;
873 }
874
875 grab->interface->key(grab, time, key, state);
876
877 if (update_state == STATE_UPDATE_AUTOMATIC) {
878 update_modifier_state(seat,
879 wl_display_get_serial(compositor->wl_display),
880 key,
881 state);
882 }
883}
884
885WL_EXPORT void
886notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
887 wl_fixed_t x, wl_fixed_t y)
888{
889 struct weston_compositor *compositor = seat->compositor;
890
891 if (output) {
892 move_pointer(seat, x, y);
893 compositor->focus = 1;
894 } else {
895 compositor->focus = 0;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400896 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400897 * NULL) here, but somehow that breaks re-entry... */
898 }
899}
900
901static void
902destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
903{
904 struct weston_seat *ws;
905
906 ws = container_of(listener, struct weston_seat,
907 saved_kbd_focus_listener);
908
909 ws->saved_kbd_focus = NULL;
910}
911
912WL_EXPORT void
913notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
914 enum weston_key_state_update update_state)
915{
916 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400917 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400918 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400919 uint32_t *k, serial;
920
921 serial = wl_display_next_serial(compositor->wl_display);
922 wl_array_copy(&keyboard->keys, keys);
923 wl_array_for_each(k, &keyboard->keys) {
924 weston_compositor_idle_inhibit(compositor);
925 if (update_state == STATE_UPDATE_AUTOMATIC)
926 update_modifier_state(seat, serial, *k,
927 WL_KEYBOARD_KEY_STATE_PRESSED);
928 }
929
930 /* Run key bindings after we've updated the state. */
931 wl_array_for_each(k, &keyboard->keys) {
932 weston_compositor_run_key_binding(compositor, seat, 0, *k,
933 WL_KEYBOARD_KEY_STATE_PRESSED);
934 }
935
936 surface = seat->saved_kbd_focus;
937
938 if (surface) {
939 wl_list_remove(&seat->saved_kbd_focus_listener.link);
940 weston_keyboard_set_focus(keyboard, surface);
941 seat->saved_kbd_focus = NULL;
942 }
943}
944
945WL_EXPORT void
946notify_keyboard_focus_out(struct weston_seat *seat)
947{
948 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400949 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400950 uint32_t *k, serial;
951
952 serial = wl_display_next_serial(compositor->wl_display);
953 wl_array_for_each(k, &keyboard->keys) {
954 weston_compositor_idle_release(compositor);
955 update_modifier_state(seat, serial, *k,
956 WL_KEYBOARD_KEY_STATE_RELEASED);
957 }
958
959 seat->modifier_state = 0;
960
961 if (keyboard->focus) {
962 seat->saved_kbd_focus = keyboard->focus;
963 seat->saved_kbd_focus_listener.notify =
964 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500965 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400966 &seat->saved_kbd_focus_listener);
967 }
968
969 weston_keyboard_set_focus(keyboard, NULL);
970 /* FIXME: We really need keyboard grab cancel here to
971 * let the grab shut down properly. As it is we leak
972 * the grab data. */
973 weston_keyboard_end_grab(keyboard);
974}
975
Michael Fua2bb7912013-07-23 15:51:06 +0800976WL_EXPORT void
977weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400978{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400979 struct wl_resource *resource;
980
981 if (seat->touch->focus == surface)
982 return;
983
984 if (seat->touch->focus_resource)
985 wl_list_remove(&seat->touch->focus_listener.link);
986 seat->touch->focus = NULL;
987 seat->touch->focus_resource = NULL;
988
989 if (surface) {
990 resource =
Kristian Høgsberg80fb82d2013-05-06 21:49:55 -0400991 find_resource_for_surface(&seat->touch->resource_list,
992 surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400993 if (!resource) {
994 weston_log("couldn't find resource\n");
995 return;
996 }
997
998 seat->touch->focus = surface;
999 seat->touch->focus_resource = resource;
Jason Ekstrand44a38632013-06-14 10:08:00 -05001000 wl_resource_add_destroy_listener(resource,
1001 &seat->touch->focus_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001002 }
1003}
1004
1005/**
1006 * notify_touch - emulates button touches and notifies surfaces accordingly.
1007 *
1008 * It assumes always the correct cycle sequence until it gets here: touch_down
1009 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1010 * for sending along such order.
1011 *
1012 */
1013WL_EXPORT void
1014notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1015 wl_fixed_t x, wl_fixed_t y, int touch_type)
1016{
1017 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001018 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001019 struct weston_touch_grab *grab = touch->grab;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001020 struct weston_surface *es;
1021 wl_fixed_t sx, sy;
1022
1023 /* Update grab's global coordinates. */
1024 touch->grab_x = x;
1025 touch->grab_y = y;
1026
1027 switch (touch_type) {
1028 case WL_TOUCH_DOWN:
1029 weston_compositor_idle_inhibit(ec);
1030
1031 seat->num_tp++;
1032
1033 /* the first finger down picks the surface, and all further go
1034 * to that surface for the remainder of the touch session i.e.
1035 * until all touch points are up again. */
1036 if (seat->num_tp == 1) {
1037 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Michael Fua2bb7912013-07-23 15:51:06 +08001038 weston_touch_set_focus(seat, es);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001039 } else if (touch->focus) {
1040 es = (struct weston_surface *) touch->focus;
1041 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1042 } else {
1043 /* Unexpected condition: We have non-initial touch but
1044 * there is no focused surface.
1045 */
1046 weston_log("touch event received with %d points down"
1047 "but no surface focused\n", seat->num_tp);
1048 return;
1049 }
1050
1051 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001052 if (seat->num_tp == 1) {
1053 touch->grab_serial =
1054 wl_display_get_serial(ec->wl_display);
1055 touch->grab_time = time;
1056 touch->grab_x = x;
1057 touch->grab_y = y;
1058 }
1059
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001060 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)
Michael Fua2bb7912013-07-23 15:51:06 +08001075 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001076 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
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001162static void
1163pointer_release(struct wl_client *client, struct wl_resource *resource)
1164{
1165 wl_resource_destroy(resource);
1166}
1167
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001169 pointer_set_cursor,
1170 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001171};
1172
1173static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001174seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1175 uint32_t id)
1176{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001177 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001178 struct wl_resource *cr;
1179
Kristian Høgsberge3148752013-05-06 23:19:49 -04001180 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001181 return;
1182
Jason Ekstranda85118c2013-06-27 20:17:02 -05001183 cr = wl_resource_create(client, &wl_pointer_interface,
1184 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001185 if (cr == NULL) {
1186 wl_client_post_no_memory(client);
1187 return;
1188 }
1189
Jason Ekstrand44a38632013-06-14 10:08:00 -05001190 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001191 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1192 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193
Giulio Camuffo708b8af2013-07-07 17:38:50 +02001194 if (seat->pointer->focus && seat->pointer->focus->resource &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001195 wl_resource_get_client(seat->pointer->focus->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001196 struct weston_surface *surface;
1197 wl_fixed_t sx, sy;
1198
Kristian Høgsberge3148752013-05-06 23:19:49 -04001199 surface = (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001200 weston_surface_from_global_fixed(surface,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001201 seat->pointer->x,
1202 seat->pointer->y,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001203 &sx,
1204 &sy);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001205 weston_pointer_set_focus(seat->pointer,
1206 seat->pointer->focus,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001207 sx,
1208 sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 }
1210}
1211
1212static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001213keyboard_release(struct wl_client *client, struct wl_resource *resource)
1214{
1215 wl_resource_destroy(resource);
1216}
1217
1218static const struct wl_keyboard_interface keyboard_interface = {
1219 keyboard_release
1220};
1221
1222static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001223seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1224 uint32_t id)
1225{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001226 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001227 struct wl_resource *cr;
1228
Kristian Høgsberge3148752013-05-06 23:19:49 -04001229 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001230 return;
1231
Jason Ekstranda85118c2013-06-27 20:17:02 -05001232 cr = wl_resource_create(client, &wl_keyboard_interface,
1233 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001234 if (cr == NULL) {
1235 wl_client_post_no_memory(client);
1236 return;
1237 }
1238
Jason Ekstrand44a38632013-06-14 10:08:00 -05001239 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001240 wl_resource_set_implementation(cr, &keyboard_interface,
1241 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242
Matt Roper01a92732013-06-24 16:52:44 +01001243 if (seat->compositor->use_xkbcommon) {
1244 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001245 seat->xkb_info->keymap_fd,
1246 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001247 } else {
1248 int null_fd = open("/dev/null", O_RDONLY);
1249 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1250 null_fd,
1251 0);
1252 close(null_fd);
1253 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001254
Kristian Høgsberge3148752013-05-06 23:19:49 -04001255 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001256 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001257 weston_keyboard_set_focus(seat->keyboard,
1258 seat->keyboard->focus);
1259 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001260 }
1261}
1262
1263static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001264touch_release(struct wl_client *client, struct wl_resource *resource)
1265{
1266 wl_resource_destroy(resource);
1267}
1268
1269static const struct wl_touch_interface touch_interface = {
1270 touch_release
1271};
1272
1273static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1275 uint32_t id)
1276{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001277 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001278 struct wl_resource *cr;
1279
Kristian Høgsberge3148752013-05-06 23:19:49 -04001280 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001281 return;
1282
Jason Ekstranda85118c2013-06-27 20:17:02 -05001283 cr = wl_resource_create(client, &wl_touch_interface,
1284 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001285 if (cr == NULL) {
1286 wl_client_post_no_memory(client);
1287 return;
1288 }
1289
Jason Ekstrand44a38632013-06-14 10:08:00 -05001290 wl_list_insert(&seat->touch->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001291 wl_resource_set_implementation(cr, &touch_interface,
1292 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001293}
1294
1295static const struct wl_seat_interface seat_interface = {
1296 seat_get_pointer,
1297 seat_get_keyboard,
1298 seat_get_touch,
1299};
1300
1301static void
1302bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1303{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001304 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001305 struct wl_resource *resource;
1306 enum wl_seat_capability caps = 0;
1307
Jason Ekstranda85118c2013-06-27 20:17:02 -05001308 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001309 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001310 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001311 wl_resource_set_implementation(resource, &seat_interface, data,
1312 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001313
1314 if (seat->pointer)
1315 caps |= WL_SEAT_CAPABILITY_POINTER;
1316 if (seat->keyboard)
1317 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1318 if (seat->touch)
1319 caps |= WL_SEAT_CAPABILITY_TOUCH;
1320
1321 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001322 if (version >= 2)
1323 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001324}
1325
Rob Bradford382ff462013-06-24 16:52:45 +01001326#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001327int
1328weston_compositor_xkb_init(struct weston_compositor *ec,
1329 struct xkb_rule_names *names)
1330{
Rob Bradford382ff462013-06-24 16:52:45 +01001331 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001332
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333 if (ec->xkb_context == NULL) {
1334 ec->xkb_context = xkb_context_new(0);
1335 if (ec->xkb_context == NULL) {
1336 weston_log("failed to create XKB context\n");
1337 return -1;
1338 }
1339 }
1340
1341 if (names)
1342 ec->xkb_names = *names;
1343 if (!ec->xkb_names.rules)
1344 ec->xkb_names.rules = strdup("evdev");
1345 if (!ec->xkb_names.model)
1346 ec->xkb_names.model = strdup("pc105");
1347 if (!ec->xkb_names.layout)
1348 ec->xkb_names.layout = strdup("us");
1349
1350 return 0;
1351}
1352
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001353static void
1354weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001355{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001356 if (--xkb_info->ref_count > 0)
1357 return;
1358
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001359 if (xkb_info->keymap)
1360 xkb_map_unref(xkb_info->keymap);
1361
1362 if (xkb_info->keymap_area)
1363 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1364 if (xkb_info->keymap_fd >= 0)
1365 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001366 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001367}
1368
1369void
1370weston_compositor_xkb_destroy(struct weston_compositor *ec)
1371{
Matt Roper01a92732013-06-24 16:52:44 +01001372 /*
1373 * If we're operating in raw keyboard mode, we never initialized
1374 * libxkbcommon so there's no cleanup to do either.
1375 */
1376 if (!ec->use_xkbcommon)
1377 return;
1378
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001379 free((char *) ec->xkb_names.rules);
1380 free((char *) ec->xkb_names.model);
1381 free((char *) ec->xkb_names.layout);
1382 free((char *) ec->xkb_names.variant);
1383 free((char *) ec->xkb_names.options);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001384
1385 if (ec->xkb_info)
1386 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001387 xkb_context_unref(ec->xkb_context);
1388}
1389
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001390static struct weston_xkb_info *
1391weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001392{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001393 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1394 if (xkb_info == NULL)
1395 return NULL;
1396
1397 xkb_info->keymap = xkb_map_ref(keymap);
1398 xkb_info->ref_count = 1;
1399
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001400 char *keymap_str;
1401
1402 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1403 XKB_MOD_NAME_SHIFT);
1404 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1405 XKB_MOD_NAME_CAPS);
1406 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1407 XKB_MOD_NAME_CTRL);
1408 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1409 XKB_MOD_NAME_ALT);
1410 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1411 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1412 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1413 XKB_MOD_NAME_LOGO);
1414 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1415
1416 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1417 XKB_LED_NAME_NUM);
1418 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1419 XKB_LED_NAME_CAPS);
1420 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1421 XKB_LED_NAME_SCROLL);
1422
1423 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1424 if (keymap_str == NULL) {
1425 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001426 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001427 }
1428 xkb_info->keymap_size = strlen(keymap_str) + 1;
1429
1430 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1431 if (xkb_info->keymap_fd < 0) {
1432 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1433 (unsigned long) xkb_info->keymap_size);
1434 goto err_keymap_str;
1435 }
1436
1437 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1438 PROT_READ | PROT_WRITE,
1439 MAP_SHARED, xkb_info->keymap_fd, 0);
1440 if (xkb_info->keymap_area == MAP_FAILED) {
1441 weston_log("failed to mmap() %lu bytes\n",
1442 (unsigned long) xkb_info->keymap_size);
1443 goto err_dev_zero;
1444 }
1445 strcpy(xkb_info->keymap_area, keymap_str);
1446 free(keymap_str);
1447
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001448 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449
1450err_dev_zero:
1451 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001452err_keymap_str:
1453 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001454err_keymap:
1455 xkb_map_unref(xkb_info->keymap);
1456 free(xkb_info);
1457 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001458}
1459
1460static int
1461weston_compositor_build_global_keymap(struct weston_compositor *ec)
1462{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001463 struct xkb_keymap *keymap;
1464
1465 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466 return 0;
1467
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001468 keymap = xkb_map_new_from_names(ec->xkb_context,
1469 &ec->xkb_names,
1470 0);
1471 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001472 weston_log("failed to compile global XKB keymap\n");
1473 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1474 "options %s\n",
1475 ec->xkb_names.rules, ec->xkb_names.model,
1476 ec->xkb_names.layout, ec->xkb_names.variant,
1477 ec->xkb_names.options);
1478 return -1;
1479 }
1480
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001481 ec->xkb_info = weston_xkb_info_create(keymap);
1482 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483 return -1;
1484
1485 return 0;
1486}
Rob Bradford382ff462013-06-24 16:52:45 +01001487#else
1488int
1489weston_compositor_xkb_init(struct weston_compositor *ec,
1490 struct xkb_rule_names *names)
1491{
1492 return 0;
1493}
1494
1495void
1496weston_compositor_xkb_destroy(struct weston_compositor *ec)
1497{
1498}
1499#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500
1501WL_EXPORT int
1502weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1503{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001504 struct weston_keyboard *keyboard;
1505
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001506 if (seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001507 return 0;
1508
Rob Bradford382ff462013-06-24 16:52:45 +01001509#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001510 if (seat->compositor->use_xkbcommon) {
1511 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001512 seat->xkb_info = weston_xkb_info_create(keymap);
1513 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001514 return -1;
1515 } else {
1516 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1517 return -1;
1518 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001519 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001520 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001521
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001522 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001523 if (seat->xkb_state.state == NULL) {
1524 weston_log("failed to initialise XKB state\n");
1525 return -1;
1526 }
1527
1528 seat->xkb_state.leds = 0;
1529 }
Rob Bradford382ff462013-06-24 16:52:45 +01001530#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001531
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001532 keyboard = weston_keyboard_create();
1533 if (keyboard == NULL) {
1534 weston_log("failed to allocate weston keyboard struct\n");
1535 return -1;
1536 }
1537
1538 seat->keyboard = keyboard;
1539 keyboard->seat = seat;
1540
1541 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001542
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001543 return 0;
1544}
1545
1546WL_EXPORT void
1547weston_seat_init_pointer(struct weston_seat *seat)
1548{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001549 struct weston_pointer *pointer;
1550
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001551 if (seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001552 return;
1553
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001554 pointer = weston_pointer_create();
1555 if (pointer == NULL)
1556 return;
1557
1558 seat->pointer = pointer;
1559 pointer->seat = seat;
1560
1561 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001562}
1563
1564WL_EXPORT void
1565weston_seat_init_touch(struct weston_seat *seat)
1566{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001567 struct weston_touch *touch;
1568
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04001569 if (seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001570 return;
1571
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001572 touch = weston_touch_create();
1573 if (touch == NULL)
1574 return;
1575
1576 seat->touch = touch;
1577 touch->seat = seat;
1578
1579 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001580}
1581
1582WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001583weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1584 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001585{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001586 memset(seat, 0, sizeof *seat);
1587
Kristian Høgsberge3148752013-05-06 23:19:49 -04001588 seat->selection_data_source = NULL;
1589 wl_list_init(&seat->base_resource_list);
1590 wl_signal_init(&seat->selection_signal);
1591 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001592 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001593
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001594 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001595 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001597 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001598 seat->modifier_state = 0;
1599 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001600 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001601
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001602 wl_list_insert(ec->seat_list.prev, &seat->link);
1603
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001604 clipboard_create(seat);
1605
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001606 wl_signal_emit(&ec->seat_created_signal, seat);
1607}
1608
1609WL_EXPORT void
1610weston_seat_release(struct weston_seat *seat)
1611{
1612 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001613
Rob Bradford382ff462013-06-24 16:52:45 +01001614#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001615 if (seat->compositor->use_xkbcommon) {
1616 if (seat->xkb_state.state != NULL)
1617 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001618 if (seat->xkb_info)
1619 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001620 }
Rob Bradford382ff462013-06-24 16:52:45 +01001621#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001622
Kristian Høgsberge3148752013-05-06 23:19:49 -04001623 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001624 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001625 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001626 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001627 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001628 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001629
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001630 free (seat->seat_name);
1631
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001632 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001633
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001634 wl_signal_emit(&seat->destroy_signal, seat);
1635}