blob: 748037441ae2a6ccd0eb8531d0899dfd75a96834 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010023#include "config.h"
24
Kristian Høgsberg2158a882013-04-18 15:07:39 -040025#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040028#include <sys/mman.h>
29#include <assert.h>
30#include <unistd.h>
Matt Roper01a92732013-06-24 16:52:44 +010031#include <fcntl.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040033#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040034#include "compositor.h"
35
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040036static void
37empty_region(pixman_region32_t *region)
38{
39 pixman_region32_fini(region);
40 pixman_region32_init(region);
41}
42
43static void unbind_resource(struct wl_resource *resource)
44{
Jason Ekstrand44a38632013-06-14 10:08:00 -050045 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040046}
47
Jonas Ådahl3042ffe2013-10-17 23:04:08 +020048WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040049weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050{
Kristian Høgsbergda751b82013-07-04 00:58:07 -040051 const struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052
Kristian Høgsbergda751b82013-07-04 00:58:07 -040053 if (pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040054 return;
55
Kristian Høgsbergda751b82013-07-04 00:58:07 -040056 pointer->grab->interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040057}
58
59static void
60weston_compositor_idle_inhibit(struct weston_compositor *compositor)
61{
62 weston_compositor_wake(compositor);
63 compositor->idle_inhibit++;
64}
65
66static void
67weston_compositor_idle_release(struct weston_compositor *compositor)
68{
69 compositor->idle_inhibit--;
70 weston_compositor_wake(compositor);
71}
72
Kristian Høgsberg2158a882013-04-18 15:07:39 -040073static void
Neil Roberts96d790e2013-09-19 17:32:00 +010074move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040075{
Neil Roberts96d790e2013-09-19 17:32:00 +010076 wl_list_insert_list(destination, source);
77 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078}
79
80static void
Neil Roberts96d790e2013-09-19 17:32:00 +010081move_resources_for_client(struct wl_list *destination,
82 struct wl_list *source,
83 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040084{
Neil Roberts96d790e2013-09-19 17:32:00 +010085 struct wl_resource *resource, *tmp;
86 wl_resource_for_each_safe(resource, tmp, source) {
87 if (wl_resource_get_client(resource) == client) {
88 wl_list_remove(wl_resource_get_link(resource));
89 wl_list_insert(destination,
90 wl_resource_get_link(resource));
91 }
92 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -040093}
94
95static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -070096default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040097{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040098 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -050099 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400100 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (pointer->button_count > 0)
103 return;
104
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 view = weston_compositor_pick_view(pointer->seat->compositor,
106 pointer->x, pointer->y,
107 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400108
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 if (pointer->focus != view)
110 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400111}
112
113static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100114default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
115 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400117 struct weston_pointer *pointer = grab->pointer;
118 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100119 struct wl_list *resource_list;
120 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400121
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100122 weston_pointer_move(pointer, x, y);
123
Neil Roberts96d790e2013-09-19 17:32:00 +0100124 resource_list = &pointer->focus_resource_list;
125 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500126 weston_view_from_global_fixed(pointer->focus,
127 pointer->x, pointer->y,
128 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +0100129 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400130 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400131}
132
133static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700134default_grab_pointer_button(struct weston_pointer_grab *grab,
135 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400136{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400137 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400138 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500139 struct weston_view *view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400140 struct wl_resource *resource;
141 uint32_t serial;
142 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100143 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400144 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100145 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146
Neil Roberts96d790e2013-09-19 17:32:00 +0100147 resource_list = &pointer->focus_resource_list;
148 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400149 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100150 wl_resource_for_each(resource, resource_list)
151 wl_pointer_send_button(resource,
152 serial,
153 time,
154 button,
155 state_w);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400156 }
157
158 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400159 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500160 view = weston_compositor_pick_view(compositor,
161 pointer->x, pointer->y,
162 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400163
Jason Ekstranda7af7042013-10-12 22:38:11 -0500164 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400165 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400166}
167
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200168static void
169default_grab_pointer_cancel(struct weston_pointer_grab *grab)
170{
171}
172
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400173static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400174 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700175 default_grab_pointer_focus,
176 default_grab_pointer_motion,
177 default_grab_pointer_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200178 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179};
180
Kristian Høgsberge329f362013-05-06 22:19:57 -0400181static void
182default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
183 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400184{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400185 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100186 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400187 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100188 struct wl_resource *resource;
189 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400190
Neil Roberts96d790e2013-09-19 17:32:00 +0100191 resource_list = &touch->focus_resource_list;
192
193 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100195 wl_resource_for_each(resource, resource_list)
196 wl_touch_send_down(resource, serial, time,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500197 touch->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100198 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400199 }
200}
201
Kristian Høgsberge329f362013-05-06 22:19:57 -0400202static void
203default_grab_touch_up(struct weston_touch_grab *grab,
204 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400206 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100207 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100209 struct wl_resource *resource;
210 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400211
Neil Roberts96d790e2013-09-19 17:32:00 +0100212 resource_list = &touch->focus_resource_list;
213
214 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100216 wl_resource_for_each(resource, resource_list)
217 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400218 }
219}
220
Kristian Høgsberge329f362013-05-06 22:19:57 -0400221static void
222default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
223 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400225 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100226 struct wl_resource *resource;
227 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228
Neil Roberts96d790e2013-09-19 17:32:00 +0100229 resource_list = &touch->focus_resource_list;
230
231 wl_resource_for_each(resource, resource_list) {
232 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400233 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400234 }
235}
236
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200237static void
238default_grab_touch_cancel(struct weston_touch_grab *grab)
239{
240}
241
Kristian Høgsberge329f362013-05-06 22:19:57 -0400242static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243 default_grab_touch_down,
244 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200245 default_grab_touch_motion,
246 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247};
248
249static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700250default_grab_keyboard_key(struct weston_keyboard_grab *grab,
251 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400253 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100255 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400256 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100257 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258
Neil Roberts96d790e2013-09-19 17:32:00 +0100259 resource_list = &keyboard->focus_resource_list;
260 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400261 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100262 wl_resource_for_each(resource, resource_list)
263 wl_keyboard_send_key(resource,
264 serial,
265 time,
266 key,
267 state);
268 }
269}
270
271static void
272send_modifiers_to_resource(struct weston_keyboard *keyboard,
273 struct wl_resource *resource,
274 uint32_t serial)
275{
276 wl_keyboard_send_modifiers(resource,
277 serial,
278 keyboard->modifiers.mods_depressed,
279 keyboard->modifiers.mods_latched,
280 keyboard->modifiers.mods_locked,
281 keyboard->modifiers.group);
282}
283
284static void
285send_modifiers_to_client_in_list(struct wl_client *client,
286 struct wl_list *list,
287 uint32_t serial,
288 struct weston_keyboard *keyboard)
289{
290 struct wl_resource *resource;
291
292 wl_resource_for_each(resource, list) {
293 if (wl_resource_get_client(resource) == client)
294 send_modifiers_to_resource(keyboard,
295 resource,
296 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297 }
298}
299
300static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400301find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400302{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400303 if (!surface)
304 return NULL;
305
Jason Ekstrand44a38632013-06-14 10:08:00 -0500306 if (!surface->resource)
307 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100308
Jason Ekstrand44a38632013-06-14 10:08:00 -0500309 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400310}
311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312static struct wl_resource *
313find_resource_for_view(struct wl_list *list, struct weston_view *view)
314{
315 if (!view)
316 return NULL;
317
318 return find_resource_for_surface(list, view->surface);
319}
320
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700322default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
323 uint32_t serial, uint32_t mods_depressed,
324 uint32_t mods_latched,
325 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400327 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100328 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
329 struct wl_resource *resource;
330 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331
Neil Roberts96d790e2013-09-19 17:32:00 +0100332 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333
Neil Roberts96d790e2013-09-19 17:32:00 +0100334 wl_resource_for_each(resource, resource_list) {
335 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
336 mods_latched, mods_locked, group);
337 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 if (pointer && pointer->focus && pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100339 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100341 send_modifiers_to_client_in_list(pointer_client,
342 &keyboard->resource_list,
343 serial,
344 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400345 }
346}
347
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200348static void
349default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
350{
351}
352
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400353static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700355 default_grab_keyboard_key,
356 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200357 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358};
359
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400360static void
361pointer_unmap_sprite(struct weston_pointer *pointer)
362{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 if (weston_surface_is_mapped(pointer->sprite->surface))
364 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400365
366 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367 pointer->sprite->surface->configure = NULL;
368 pointer->sprite->surface->configure_private = NULL;
369 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400370 pointer->sprite = NULL;
371}
372
373static void
374pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
375{
376 struct weston_pointer *pointer =
377 container_of(listener, struct weston_pointer,
378 sprite_destroy_listener);
379
380 pointer->sprite = NULL;
381}
382
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400383WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100384weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400386 struct weston_pointer *pointer;
387
Peter Huttererf3d62272013-08-08 11:57:05 +1000388 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400389 if (pointer == NULL)
390 return NULL;
391
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100393 wl_list_init(&pointer->focus_resource_list);
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100394 weston_pointer_set_default_grab(pointer,
395 seat->compositor->default_pointer_grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396 pointer->default_grab.pointer = pointer;
397 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100398 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100399 wl_signal_init(&pointer->focus_signal);
400 wl_list_init(&pointer->focus_listener.link);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400401
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400402 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
403
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404 /* FIXME: Pick better co-ords. */
405 pointer->x = wl_fixed_from_int(100);
406 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400407
408 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400409}
410
411WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400412weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400413{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400414 if (pointer->sprite)
415 pointer_unmap_sprite(pointer);
416
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400417 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100418
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400419 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400420}
421
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100422void
423weston_pointer_set_default_grab(struct weston_pointer *pointer,
424 const struct weston_pointer_grab_interface *interface)
425{
426 if (interface)
427 pointer->default_grab.interface = interface;
428 else
429 pointer->default_grab.interface =
430 &default_pointer_grab_interface;
431}
432
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400433WL_EXPORT struct weston_keyboard *
434weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400436 struct weston_keyboard *keyboard;
437
Peter Huttererf3d62272013-08-08 11:57:05 +1000438 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400439 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100440 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400441
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100443 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400445 keyboard->default_grab.interface = &default_keyboard_grab_interface;
446 keyboard->default_grab.keyboard = keyboard;
447 keyboard->grab = &keyboard->default_grab;
448 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400449
450 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451}
452
453WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400454weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455{
456 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100457
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400458 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400459 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460}
461
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400462WL_EXPORT struct weston_touch *
463weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400464{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400465 struct weston_touch *touch;
466
Peter Huttererf3d62272013-08-08 11:57:05 +1000467 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400468 if (touch == NULL)
469 return NULL;
470
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400471 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100472 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400473 touch->default_grab.interface = &default_touch_grab_interface;
474 touch->default_grab.touch = touch;
475 touch->grab = &touch->default_grab;
476 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400477
478 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479}
480
481WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400482weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483{
484 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100485
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400486 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487}
488
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400489static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400490seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400492 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100493 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200495 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400496 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200497 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400498 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200499 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400500 caps |= WL_SEAT_CAPABILITY_TOUCH;
501
Rob Bradford6e737f52013-09-06 17:48:19 +0100502 wl_resource_for_each(resource, &seat->base_resource_list) {
503 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500504 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505}
506
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100507static void
508destroy_pointer_focus(struct wl_listener *listener, void *data)
509{
510 struct weston_pointer *pointer;
511
512 pointer = container_of(listener, struct weston_pointer,
513 focus_listener);
514
515 pointer->focus = NULL;
516 move_resources(&pointer->resource_list, &pointer->focus_resource_list);
517
518 wl_list_remove(&pointer->focus_listener.link);
519 wl_list_init(&pointer->focus_listener.link);
520
521 wl_signal_emit(&pointer->focus_signal, pointer);
522}
523
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400525weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400527 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400529 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100530 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100531 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400532 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100533 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534 int different_surface = 0;
535
536 if ((!pointer->focus && view) ||
537 (pointer->focus && !view) ||
538 (pointer->focus && pointer->focus->surface != view->surface))
539 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540
Neil Roberts96d790e2013-09-19 17:32:00 +0100541 focus_resource_list = &pointer->focus_resource_list;
542
Jason Ekstranda7af7042013-10-12 22:38:11 -0500543 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100545 wl_resource_for_each(resource, focus_resource_list) {
546 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100548 }
549
550 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551 }
552
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 if (find_resource_for_view(&pointer->resource_list, view) &&
554 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100555 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100557
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400558 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100559
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700561 send_modifiers_to_client_in_list(surface_client,
562 &kbd->resource_list,
563 serial,
564 kbd);
565
Neil Roberts96d790e2013-09-19 17:32:00 +0100566 move_resources_for_client(focus_resource_list,
567 &pointer->resource_list,
568 surface_client);
569
570 wl_resource_for_each(resource, focus_resource_list) {
571 wl_pointer_send_enter(resource,
572 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100574 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400575 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100576
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400577 pointer->focus_serial = serial;
578 }
579
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100580 if (!wl_list_empty(&pointer->focus_listener.link)) {
581 wl_list_remove(&pointer->focus_listener.link);
582 wl_list_init(&pointer->focus_listener.link);
583 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584 pointer->focus = view;
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100585 pointer->focus_listener.notify = destroy_pointer_focus;
586 if (view)
587 wl_signal_add(&view->destroy_signal, &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588 wl_signal_emit(&pointer->focus_signal, pointer);
589}
590
Neil Roberts96d790e2013-09-19 17:32:00 +0100591static void
592send_enter_to_resource_list(struct wl_list *list,
593 struct weston_keyboard *keyboard,
594 struct weston_surface *surface,
595 uint32_t serial)
596{
597 struct wl_resource *resource;
598
599 wl_resource_for_each(resource, list) {
600 send_modifiers_to_resource(keyboard, resource, serial);
601 wl_keyboard_send_enter(resource, serial,
602 surface->resource,
603 &keyboard->keys);
604 }
605}
606
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400608weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400609 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400610{
611 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100612 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400613 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100614 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400615
Neil Roberts96d790e2013-09-19 17:32:00 +0100616 focus_resource_list = &keyboard->focus_resource_list;
617
618 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100620 wl_resource_for_each(resource, focus_resource_list) {
621 wl_keyboard_send_leave(resource, serial,
622 keyboard->focus->resource);
623 }
624 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400625 }
626
Neil Roberts96d790e2013-09-19 17:32:00 +0100627 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
628 keyboard->focus != surface) {
629 struct wl_client *surface_client =
630 wl_resource_get_client(surface->resource);
631
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400632 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100633
634 move_resources_for_client(focus_resource_list,
635 &keyboard->resource_list,
636 surface_client);
637 send_enter_to_resource_list(focus_resource_list,
638 keyboard,
639 surface,
640 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641 keyboard->focus_serial = serial;
642 }
643
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400644 keyboard->focus = surface;
645 wl_signal_emit(&keyboard->focus_signal, keyboard);
646}
647
648WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400649weston_keyboard_start_grab(struct weston_keyboard *keyboard,
650 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400651{
652 keyboard->grab = grab;
653 grab->keyboard = keyboard;
654
655 /* XXX focus? */
656}
657
658WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400659weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400660{
661 keyboard->grab = &keyboard->default_grab;
662}
663
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200664static void
665weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
666{
667 keyboard->grab->interface->cancel(keyboard->grab);
668}
669
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400670WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400671weston_pointer_start_grab(struct weston_pointer *pointer,
672 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400673{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400674 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400675 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400676 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400677}
678
679WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400680weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400681{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400682 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400683 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400684}
685
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200686static void
687weston_pointer_cancel_grab(struct weston_pointer *pointer)
688{
689 pointer->grab->interface->cancel(pointer->grab);
690}
691
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400692WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400693weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400694{
695 touch->grab = grab;
696 grab->touch = touch;
697}
698
699WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400700weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400701{
702 touch->grab = &touch->default_grab;
703}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400704
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200705static void
706weston_touch_cancel_grab(struct weston_touch *touch)
707{
708 touch->grab->interface->cancel(touch->grab);
709}
710
Rob Bradford806d8c02013-06-25 18:56:41 +0100711WL_EXPORT void
712weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400713{
Rob Bradford806d8c02013-06-25 18:56:41 +0100714 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400715 struct weston_output *output, *prev = NULL;
716 int x, y, old_x, old_y, valid = 0;
717
718 x = wl_fixed_to_int(*fx);
719 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100720 old_x = wl_fixed_to_int(pointer->x);
721 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400722
723 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100724 if (pointer->seat->output && pointer->seat->output != output)
725 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400726 if (pixman_region32_contains_point(&output->region,
727 x, y, NULL))
728 valid = 1;
729 if (pixman_region32_contains_point(&output->region,
730 old_x, old_y, NULL))
731 prev = output;
732 }
733
Rob Bradford66bd9f52013-06-25 18:56:42 +0100734 if (!prev)
735 prev = pointer->seat->output;
736
737 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400738 if (x < prev->x)
739 *fx = wl_fixed_from_int(prev->x);
740 else if (x >= prev->x + prev->width)
741 *fx = wl_fixed_from_int(prev->x +
742 prev->width - 1);
743 if (y < prev->y)
744 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200745 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400746 *fy = wl_fixed_from_int(prev->y +
747 prev->height - 1);
748 }
749}
750
751/* Takes absolute values */
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100752WL_EXPORT void
753weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400754{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400755 int32_t ix, iy;
756
Rob Bradford806d8c02013-06-25 18:56:41 +0100757 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400758
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400759 pointer->x = x;
760 pointer->y = y;
761
762 ix = wl_fixed_to_int(x);
763 iy = wl_fixed_to_int(y);
764
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400765 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500766 weston_view_set_position(pointer->sprite,
767 ix - pointer->hotspot_x,
768 iy - pointer->hotspot_y);
769 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400770 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100771
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100772 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100773 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400774}
775
776WL_EXPORT void
777notify_motion(struct weston_seat *seat,
778 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
779{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400780 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400781 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400782
783 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100784 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400785}
786
Daniel Stone96d47c02013-11-19 11:37:12 +0100787static void
788run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
789{
790 struct weston_compositor *compositor = seat->compositor;
791 uint32_t diff;
792 unsigned int i;
793 struct {
794 uint32_t xkb;
795 enum weston_keyboard_modifier weston;
796 } mods[] = {
797 { seat->xkb_info->ctrl_mod, MODIFIER_CTRL },
798 { seat->xkb_info->alt_mod, MODIFIER_ALT },
799 { seat->xkb_info->super_mod, MODIFIER_SUPER },
800 { seat->xkb_info->shift_mod, MODIFIER_SHIFT },
801 };
802
803 diff = new & ~old;
804 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
805 if (diff & (1 << mods[i].xkb))
806 weston_compositor_run_modifier_binding(compositor,
807 seat,
808 mods[i].weston,
809 WL_KEYBOARD_KEY_STATE_PRESSED);
810 }
811
812 diff = old & ~new;
813 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
814 if (diff & (1 << mods[i].xkb))
815 weston_compositor_run_modifier_binding(compositor,
816 seat,
817 mods[i].weston,
818 WL_KEYBOARD_KEY_STATE_RELEASED);
819 }
820}
821
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400822WL_EXPORT void
823notify_motion_absolute(struct weston_seat *seat,
824 uint32_t time, wl_fixed_t x, wl_fixed_t y)
825{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400826 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400827 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400828
829 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100830 pointer->grab->interface->motion(pointer->grab, time, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400831}
832
833WL_EXPORT void
834weston_surface_activate(struct weston_surface *surface,
835 struct weston_seat *seat)
836{
837 struct weston_compositor *compositor = seat->compositor;
838
Kristian Høgsberge3148752013-05-06 23:19:49 -0400839 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400840 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400841 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400842 }
843
844 wl_signal_emit(&compositor->activate_signal, surface);
845}
846
847WL_EXPORT void
848notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
849 enum wl_pointer_button_state state)
850{
851 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400852 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400853 struct weston_surface *focus =
854 (struct weston_surface *) pointer->focus;
855 uint32_t serial = wl_display_next_serial(compositor->wl_display);
856
857 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
858 if (compositor->ping_handler && focus)
859 compositor->ping_handler(focus, serial);
860 weston_compositor_idle_inhibit(compositor);
861 if (pointer->button_count == 0) {
862 pointer->grab_button = button;
863 pointer->grab_time = time;
864 pointer->grab_x = pointer->x;
865 pointer->grab_y = pointer->y;
866 }
867 pointer->button_count++;
868 } else {
869 weston_compositor_idle_release(compositor);
870 pointer->button_count--;
871 }
872
873 weston_compositor_run_button_binding(compositor, seat, time, button,
874 state);
875
876 pointer->grab->interface->button(pointer->grab, time, button, state);
877
878 if (pointer->button_count == 1)
879 pointer->grab_serial =
880 wl_display_get_serial(compositor->wl_display);
881}
882
883WL_EXPORT void
884notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
885 wl_fixed_t value)
886{
887 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400888 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400889 struct weston_surface *focus =
890 (struct weston_surface *) pointer->focus;
891 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100892 struct wl_resource *resource;
893 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400894
895 if (compositor->ping_handler && focus)
896 compositor->ping_handler(focus, serial);
897
898 weston_compositor_wake(compositor);
899
900 if (!value)
901 return;
902
903 if (weston_compositor_run_axis_binding(compositor, seat,
904 time, axis, value))
905 return;
906
Neil Roberts96d790e2013-09-19 17:32:00 +0100907 resource_list = &pointer->focus_resource_list;
908 wl_resource_for_each(resource, resource_list)
909 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400910 value);
911}
912
Rob Bradford382ff462013-06-24 16:52:45 +0100913#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400914WL_EXPORT void
915notify_modifiers(struct weston_seat *seat, uint32_t serial)
916{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400917 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400918 struct weston_keyboard_grab *grab = keyboard->grab;
919 uint32_t mods_depressed, mods_latched, mods_locked, group;
920 uint32_t mods_lookup;
921 enum weston_led leds = 0;
922 int changed = 0;
923
924 /* Serialize and update our internal state, checking to see if it's
925 * different to the previous state. */
926 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
927 XKB_STATE_DEPRESSED);
928 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
929 XKB_STATE_LATCHED);
930 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
931 XKB_STATE_LOCKED);
932 group = xkb_state_serialize_group(seat->xkb_state.state,
933 XKB_STATE_EFFECTIVE);
934
Kristian Høgsberge3148752013-05-06 23:19:49 -0400935 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
936 mods_latched != seat->keyboard->modifiers.mods_latched ||
937 mods_locked != seat->keyboard->modifiers.mods_locked ||
938 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400939 changed = 1;
940
Daniel Stone96d47c02013-11-19 11:37:12 +0100941 run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
942 mods_depressed);
943
Kristian Høgsberge3148752013-05-06 23:19:49 -0400944 seat->keyboard->modifiers.mods_depressed = mods_depressed;
945 seat->keyboard->modifiers.mods_latched = mods_latched;
946 seat->keyboard->modifiers.mods_locked = mods_locked;
947 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400948
949 /* And update the modifier_state for bindings. */
950 mods_lookup = mods_depressed | mods_latched;
951 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000952 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400953 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000954 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400955 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000956 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400957 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000958 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400959 seat->modifier_state |= MODIFIER_SHIFT;
960
961 /* Finally, notify the compositor that LEDs have changed. */
962 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000963 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400964 leds |= LED_NUM_LOCK;
965 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000966 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400967 leds |= LED_CAPS_LOCK;
968 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000969 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400970 leds |= LED_SCROLL_LOCK;
971 if (leds != seat->xkb_state.leds && seat->led_update)
972 seat->led_update(seat, leds);
973 seat->xkb_state.leds = leds;
974
975 if (changed) {
976 grab->interface->modifiers(grab,
977 serial,
978 keyboard->modifiers.mods_depressed,
979 keyboard->modifiers.mods_latched,
980 keyboard->modifiers.mods_locked,
981 keyboard->modifiers.group);
982 }
983}
984
985static void
986update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
987 enum wl_keyboard_key_state state)
988{
989 enum xkb_key_direction direction;
990
Matt Roper01a92732013-06-24 16:52:44 +0100991 /* Keyboard modifiers don't exist in raw keyboard mode */
992 if (!seat->compositor->use_xkbcommon)
993 return;
994
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400995 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
996 direction = XKB_KEY_DOWN;
997 else
998 direction = XKB_KEY_UP;
999
1000 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1001 * broken keycode system, which starts at 8. */
1002 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1003
1004 notify_modifiers(seat, serial);
1005}
Rui Matos65196bc2013-10-10 19:44:19 +02001006
1007static void
1008send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1009{
1010 wl_keyboard_send_keymap(resource,
1011 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1012 xkb_info->keymap_fd,
1013 xkb_info->keymap_size);
1014}
1015
1016static void
1017send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1018{
1019 wl_keyboard_send_modifiers(resource, serial,
1020 keyboard->modifiers.mods_depressed,
1021 keyboard->modifiers.mods_latched,
1022 keyboard->modifiers.mods_locked,
1023 keyboard->modifiers.group);
1024}
1025
1026static struct weston_xkb_info *
1027weston_xkb_info_create(struct xkb_keymap *keymap);
1028static void
1029weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1030
1031static void
1032update_keymap(struct weston_seat *seat)
1033{
1034 struct wl_resource *resource;
1035 struct weston_xkb_info *xkb_info;
1036 struct xkb_state *state;
1037 xkb_mod_mask_t latched_mods;
1038 xkb_mod_mask_t locked_mods;
1039
1040 xkb_info = weston_xkb_info_create(seat->pending_keymap);
1041
1042 xkb_keymap_unref(seat->pending_keymap);
1043 seat->pending_keymap = NULL;
1044
1045 if (!xkb_info) {
1046 weston_log("failed to create XKB info\n");
1047 return;
1048 }
1049
1050 state = xkb_state_new(xkb_info->keymap);
1051 if (!state) {
1052 weston_log("failed to initialise XKB state\n");
1053 weston_xkb_info_destroy(xkb_info);
1054 return;
1055 }
1056
1057 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
1058 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
1059 xkb_state_update_mask(state,
1060 0, /* depressed */
1061 latched_mods,
1062 locked_mods,
1063 0, 0, 0);
1064
1065 weston_xkb_info_destroy(seat->xkb_info);
1066 seat->xkb_info = xkb_info;
1067
1068 xkb_state_unref(seat->xkb_state.state);
1069 seat->xkb_state.state = state;
1070
1071 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1072 send_keymap(resource, xkb_info);
1073 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1074 send_keymap(resource, xkb_info);
1075
1076 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1077
1078 if (!latched_mods && !locked_mods)
1079 return;
1080
1081 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1082 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1083 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1084 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1085}
Rob Bradford382ff462013-06-24 16:52:45 +01001086#else
1087WL_EXPORT void
1088notify_modifiers(struct weston_seat *seat, uint32_t serial)
1089{
1090}
1091
1092static void
1093update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1094 enum wl_keyboard_key_state state)
1095{
1096}
Rui Matos65196bc2013-10-10 19:44:19 +02001097
1098static void
1099update_keymap(struct weston_seat *seat)
1100{
1101}
Rob Bradford382ff462013-06-24 16:52:45 +01001102#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001103
1104WL_EXPORT void
1105notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1106 enum wl_keyboard_key_state state,
1107 enum weston_key_state_update update_state)
1108{
1109 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001110 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001112 struct weston_keyboard_grab *grab = keyboard->grab;
1113 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1114 uint32_t *k, *end;
1115
1116 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1117 if (compositor->ping_handler && focus)
1118 compositor->ping_handler(focus, serial);
1119
1120 weston_compositor_idle_inhibit(compositor);
1121 keyboard->grab_key = key;
1122 keyboard->grab_time = time;
1123 } else {
1124 weston_compositor_idle_release(compositor);
1125 }
1126
1127 end = keyboard->keys.data + keyboard->keys.size;
1128 for (k = keyboard->keys.data; k < end; k++) {
1129 if (*k == key) {
1130 /* Ignore server-generated repeats. */
1131 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1132 return;
1133 *k = *--end;
1134 }
1135 }
1136 keyboard->keys.size = (void *) end - keyboard->keys.data;
1137 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1138 k = wl_array_add(&keyboard->keys, sizeof *k);
1139 *k = key;
1140 }
1141
1142 if (grab == &keyboard->default_grab ||
1143 grab == &keyboard->input_method_grab) {
1144 weston_compositor_run_key_binding(compositor, seat, time, key,
1145 state);
1146 grab = keyboard->grab;
1147 }
1148
1149 grab->interface->key(grab, time, key, state);
1150
Rui Matos65196bc2013-10-10 19:44:19 +02001151 if (seat->pending_keymap &&
1152 keyboard->keys.size == 0)
1153 update_keymap(seat);
1154
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001155 if (update_state == STATE_UPDATE_AUTOMATIC) {
1156 update_modifier_state(seat,
1157 wl_display_get_serial(compositor->wl_display),
1158 key,
1159 state);
1160 }
1161}
1162
1163WL_EXPORT void
1164notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1165 wl_fixed_t x, wl_fixed_t y)
1166{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001167 if (output) {
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001168 weston_pointer_move(seat->pointer, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001170 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001171 * NULL) here, but somehow that breaks re-entry... */
1172 }
1173}
1174
1175static void
1176destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1177{
1178 struct weston_seat *ws;
1179
1180 ws = container_of(listener, struct weston_seat,
1181 saved_kbd_focus_listener);
1182
1183 ws->saved_kbd_focus = NULL;
1184}
1185
1186WL_EXPORT void
1187notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1188 enum weston_key_state_update update_state)
1189{
1190 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001191 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001192 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193 uint32_t *k, serial;
1194
1195 serial = wl_display_next_serial(compositor->wl_display);
1196 wl_array_copy(&keyboard->keys, keys);
1197 wl_array_for_each(k, &keyboard->keys) {
1198 weston_compositor_idle_inhibit(compositor);
1199 if (update_state == STATE_UPDATE_AUTOMATIC)
1200 update_modifier_state(seat, serial, *k,
1201 WL_KEYBOARD_KEY_STATE_PRESSED);
1202 }
1203
1204 /* Run key bindings after we've updated the state. */
1205 wl_array_for_each(k, &keyboard->keys) {
1206 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1207 WL_KEYBOARD_KEY_STATE_PRESSED);
1208 }
1209
1210 surface = seat->saved_kbd_focus;
1211
1212 if (surface) {
1213 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1214 weston_keyboard_set_focus(keyboard, surface);
1215 seat->saved_kbd_focus = NULL;
1216 }
1217}
1218
1219WL_EXPORT void
1220notify_keyboard_focus_out(struct weston_seat *seat)
1221{
1222 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001223 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001224 uint32_t *k, serial;
1225
1226 serial = wl_display_next_serial(compositor->wl_display);
1227 wl_array_for_each(k, &keyboard->keys) {
1228 weston_compositor_idle_release(compositor);
1229 update_modifier_state(seat, serial, *k,
1230 WL_KEYBOARD_KEY_STATE_RELEASED);
1231 }
1232
1233 seat->modifier_state = 0;
1234
1235 if (keyboard->focus) {
1236 seat->saved_kbd_focus = keyboard->focus;
1237 seat->saved_kbd_focus_listener.notify =
1238 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001239 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001240 &seat->saved_kbd_focus_listener);
1241 }
1242
1243 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001244 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245}
1246
Michael Fua2bb7912013-07-23 15:51:06 +08001247WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001249{
Neil Roberts96d790e2013-09-19 17:32:00 +01001250 struct wl_list *focus_resource_list;
1251
1252 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001253
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001254 if (view && seat->touch->focus &&
1255 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001257 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001259
Neil Roberts96d790e2013-09-19 17:32:00 +01001260 if (!wl_list_empty(focus_resource_list)) {
1261 move_resources(&seat->touch->resource_list,
1262 focus_resource_list);
1263 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001266 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001268 move_resources_for_client(focus_resource_list,
1269 &seat->touch->resource_list,
1270 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001273}
1274
1275/**
1276 * notify_touch - emulates button touches and notifies surfaces accordingly.
1277 *
1278 * It assumes always the correct cycle sequence until it gets here: touch_down
1279 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1280 * for sending along such order.
1281 *
1282 */
1283WL_EXPORT void
1284notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1285 wl_fixed_t x, wl_fixed_t y, int touch_type)
1286{
1287 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001288 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001289 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291 wl_fixed_t sx, sy;
1292
1293 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001294 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1295 touch->grab_x = x;
1296 touch->grab_y = y;
1297 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001298
1299 switch (touch_type) {
1300 case WL_TOUCH_DOWN:
1301 weston_compositor_idle_inhibit(ec);
1302
1303 seat->num_tp++;
1304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 /* the first finger down picks the view, and all further go
1306 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001307 * until all touch points are up again. */
1308 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1310 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001311 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 ev = touch->focus;
1313 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001314 } else {
1315 /* Unexpected condition: We have non-initial touch but
1316 * there is no focused surface.
1317 */
1318 weston_log("touch event received with %d points down"
1319 "but no surface focused\n", seat->num_tp);
1320 return;
1321 }
1322
1323 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001324 if (seat->num_tp == 1) {
1325 touch->grab_serial =
1326 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001327 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001328 touch->grab_time = time;
1329 touch->grab_x = x;
1330 touch->grab_y = y;
1331 }
1332
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333 break;
1334 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 ev = touch->focus;
1336 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001337 break;
1338
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001340 grab->interface->motion(grab, time, touch_id, sx, sy);
1341 break;
1342 case WL_TOUCH_UP:
1343 weston_compositor_idle_release(ec);
1344 seat->num_tp--;
1345
1346 grab->interface->up(grab, time, touch_id);
1347 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001348 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001349 break;
1350 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001351
1352 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001353}
1354
1355static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001356pointer_cursor_surface_configure(struct weston_surface *es,
1357 int32_t dx, int32_t dy, int32_t width, int32_t height)
1358{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001359 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001360 int x, y;
1361
1362 if (width == 0)
1363 return;
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001366
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001367 pointer->hotspot_x -= dx;
1368 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001369
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001370 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1371 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001372
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001374
1375 empty_region(&es->pending.input);
1376
1377 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 wl_list_insert(&es->compositor->cursor_layer.view_list,
1379 &pointer->sprite->layer_link);
1380 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001381 }
1382}
1383
1384static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001385pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1386 uint32_t serial, struct wl_resource *surface_resource,
1387 int32_t x, int32_t y)
1388{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001389 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001390 struct weston_surface *surface = NULL;
1391
1392 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001393 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001394
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001395 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001396 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001398 black_surface used in shell.c for fullscreen don't have
1399 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001401 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001403 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001404 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001405 return;
1406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001408 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001409 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001410 WL_DISPLAY_ERROR_INVALID_OBJECT,
1411 "surface->configure already "
1412 "set");
1413 return;
1414 }
1415 }
1416
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001417 if (pointer->sprite)
1418 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001419
1420 if (!surface)
1421 return;
1422
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001423 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001424 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001425
1426 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001427 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001429 pointer->hotspot_x = x;
1430 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001431
1432 if (surface->buffer_ref.buffer)
1433 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1434 weston_surface_buffer_height(surface));
1435}
1436
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001437static void
1438pointer_release(struct wl_client *client, struct wl_resource *resource)
1439{
1440 wl_resource_destroy(resource);
1441}
1442
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001443static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001444 pointer_set_cursor,
1445 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001446};
1447
1448static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1450 uint32_t id)
1451{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001452 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001453 struct wl_resource *cr;
1454
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001456 return;
1457
Jason Ekstranda85118c2013-06-27 20:17:02 -05001458 cr = wl_resource_create(client, &wl_pointer_interface,
1459 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001460 if (cr == NULL) {
1461 wl_client_post_no_memory(client);
1462 return;
1463 }
1464
Neil Roberts96d790e2013-09-19 17:32:00 +01001465 /* May be moved to focused list later by either
1466 * weston_pointer_set_focus or directly if this client is already
1467 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001468 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001469 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1470 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1473 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001474 wl_fixed_t sx, sy;
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_from_global_fixed(seat->pointer->focus,
1477 seat->pointer->x,
1478 seat->pointer->y,
1479 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001480
1481 wl_list_remove(wl_resource_get_link(cr));
1482 wl_list_insert(&seat->pointer->focus_resource_list,
1483 wl_resource_get_link(cr));
1484 wl_pointer_send_enter(cr,
1485 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001487 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001488 }
1489}
1490
1491static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001492keyboard_release(struct wl_client *client, struct wl_resource *resource)
1493{
1494 wl_resource_destroy(resource);
1495}
1496
1497static const struct wl_keyboard_interface keyboard_interface = {
1498 keyboard_release
1499};
1500
Neil Roberts96d790e2013-09-19 17:32:00 +01001501static int
1502should_send_modifiers_to_client(struct weston_seat *seat,
1503 struct wl_client *client)
1504{
1505 if (seat->keyboard &&
1506 seat->keyboard->focus &&
1507 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1508 return 1;
1509
1510 if (seat->pointer &&
1511 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001513 return 1;
1514
1515 return 0;
1516}
1517
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001518static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1520 uint32_t id)
1521{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001522 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001523 struct wl_resource *cr;
1524
Kristian Høgsberge3148752013-05-06 23:19:49 -04001525 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001526 return;
1527
Jason Ekstranda85118c2013-06-27 20:17:02 -05001528 cr = wl_resource_create(client, &wl_keyboard_interface,
1529 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001530 if (cr == NULL) {
1531 wl_client_post_no_memory(client);
1532 return;
1533 }
1534
Neil Roberts96d790e2013-09-19 17:32:00 +01001535 /* May be moved to focused list later by either
1536 * weston_keyboard_set_focus or directly if this client is already
1537 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001538 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001539 wl_resource_set_implementation(cr, &keyboard_interface,
1540 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001541
Matt Roper01a92732013-06-24 16:52:44 +01001542 if (seat->compositor->use_xkbcommon) {
1543 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001544 seat->xkb_info->keymap_fd,
1545 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001546 } else {
1547 int null_fd = open("/dev/null", O_RDONLY);
1548 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1549 null_fd,
1550 0);
1551 close(null_fd);
1552 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553
Neil Roberts96d790e2013-09-19 17:32:00 +01001554 if (should_send_modifiers_to_client(seat, client)) {
1555 send_modifiers_to_resource(seat->keyboard,
1556 cr,
1557 seat->keyboard->focus_serial);
1558 }
1559
Kristian Høgsberge3148752013-05-06 23:19:49 -04001560 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001561 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001562 struct weston_surface *surface =
1563 (struct weston_surface *) seat->keyboard->focus;
1564
1565 wl_list_remove(wl_resource_get_link(cr));
1566 wl_list_insert(&seat->keyboard->focus_resource_list,
1567 wl_resource_get_link(cr));
1568 wl_keyboard_send_enter(cr,
1569 seat->keyboard->focus_serial,
1570 surface->resource,
1571 &seat->keyboard->keys);
1572
1573 /* If this is the first keyboard resource for this
1574 * client... */
1575 if (seat->keyboard->focus_resource_list.prev ==
1576 wl_resource_get_link(cr))
1577 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001578 }
1579}
1580
1581static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001582touch_release(struct wl_client *client, struct wl_resource *resource)
1583{
1584 wl_resource_destroy(resource);
1585}
1586
1587static const struct wl_touch_interface touch_interface = {
1588 touch_release
1589};
1590
1591static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001592seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1593 uint32_t id)
1594{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001595 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596 struct wl_resource *cr;
1597
Kristian Høgsberge3148752013-05-06 23:19:49 -04001598 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001599 return;
1600
Jason Ekstranda85118c2013-06-27 20:17:02 -05001601 cr = wl_resource_create(client, &wl_touch_interface,
1602 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001603 if (cr == NULL) {
1604 wl_client_post_no_memory(client);
1605 return;
1606 }
1607
Neil Roberts96d790e2013-09-19 17:32:00 +01001608 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001610 wl_list_insert(&seat->touch->resource_list,
1611 wl_resource_get_link(cr));
1612 } else {
1613 wl_list_insert(&seat->touch->focus_resource_list,
1614 wl_resource_get_link(cr));
1615 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001616 wl_resource_set_implementation(cr, &touch_interface,
1617 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001618}
1619
1620static const struct wl_seat_interface seat_interface = {
1621 seat_get_pointer,
1622 seat_get_keyboard,
1623 seat_get_touch,
1624};
1625
1626static void
1627bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1628{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001629 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001630 struct wl_resource *resource;
1631 enum wl_seat_capability caps = 0;
1632
Jason Ekstranda85118c2013-06-27 20:17:02 -05001633 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001634 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001635 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001636 wl_resource_set_implementation(resource, &seat_interface, data,
1637 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001638
1639 if (seat->pointer)
1640 caps |= WL_SEAT_CAPABILITY_POINTER;
1641 if (seat->keyboard)
1642 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1643 if (seat->touch)
1644 caps |= WL_SEAT_CAPABILITY_TOUCH;
1645
1646 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001647 if (version >= 2)
1648 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001649}
1650
Rob Bradford382ff462013-06-24 16:52:45 +01001651#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001652int
1653weston_compositor_xkb_init(struct weston_compositor *ec,
1654 struct xkb_rule_names *names)
1655{
Rob Bradford382ff462013-06-24 16:52:45 +01001656 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001657
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001658 if (ec->xkb_context == NULL) {
1659 ec->xkb_context = xkb_context_new(0);
1660 if (ec->xkb_context == NULL) {
1661 weston_log("failed to create XKB context\n");
1662 return -1;
1663 }
1664 }
1665
1666 if (names)
1667 ec->xkb_names = *names;
1668 if (!ec->xkb_names.rules)
1669 ec->xkb_names.rules = strdup("evdev");
1670 if (!ec->xkb_names.model)
1671 ec->xkb_names.model = strdup("pc105");
1672 if (!ec->xkb_names.layout)
1673 ec->xkb_names.layout = strdup("us");
1674
1675 return 0;
1676}
1677
Stefan Schmidtfda26522013-09-17 10:54:09 +01001678static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001679weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001680{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001681 if (--xkb_info->ref_count > 0)
1682 return;
1683
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001684 if (xkb_info->keymap)
1685 xkb_map_unref(xkb_info->keymap);
1686
1687 if (xkb_info->keymap_area)
1688 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1689 if (xkb_info->keymap_fd >= 0)
1690 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001691 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001692}
1693
1694void
1695weston_compositor_xkb_destroy(struct weston_compositor *ec)
1696{
Matt Roper01a92732013-06-24 16:52:44 +01001697 /*
1698 * If we're operating in raw keyboard mode, we never initialized
1699 * libxkbcommon so there's no cleanup to do either.
1700 */
1701 if (!ec->use_xkbcommon)
1702 return;
1703
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704 free((char *) ec->xkb_names.rules);
1705 free((char *) ec->xkb_names.model);
1706 free((char *) ec->xkb_names.layout);
1707 free((char *) ec->xkb_names.variant);
1708 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001709
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001710 if (ec->xkb_info)
1711 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001712 xkb_context_unref(ec->xkb_context);
1713}
1714
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001715static struct weston_xkb_info *
1716weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001717{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001718 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1719 if (xkb_info == NULL)
1720 return NULL;
1721
1722 xkb_info->keymap = xkb_map_ref(keymap);
1723 xkb_info->ref_count = 1;
1724
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001725 char *keymap_str;
1726
1727 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1728 XKB_MOD_NAME_SHIFT);
1729 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1730 XKB_MOD_NAME_CAPS);
1731 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1732 XKB_MOD_NAME_CTRL);
1733 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1734 XKB_MOD_NAME_ALT);
1735 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1736 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1737 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1738 XKB_MOD_NAME_LOGO);
1739 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1740
1741 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1742 XKB_LED_NAME_NUM);
1743 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1744 XKB_LED_NAME_CAPS);
1745 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1746 XKB_LED_NAME_SCROLL);
1747
1748 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1749 if (keymap_str == NULL) {
1750 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001751 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001752 }
1753 xkb_info->keymap_size = strlen(keymap_str) + 1;
1754
1755 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1756 if (xkb_info->keymap_fd < 0) {
1757 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1758 (unsigned long) xkb_info->keymap_size);
1759 goto err_keymap_str;
1760 }
1761
1762 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1763 PROT_READ | PROT_WRITE,
1764 MAP_SHARED, xkb_info->keymap_fd, 0);
1765 if (xkb_info->keymap_area == MAP_FAILED) {
1766 weston_log("failed to mmap() %lu bytes\n",
1767 (unsigned long) xkb_info->keymap_size);
1768 goto err_dev_zero;
1769 }
1770 strcpy(xkb_info->keymap_area, keymap_str);
1771 free(keymap_str);
1772
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001773 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001774
1775err_dev_zero:
1776 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001777err_keymap_str:
1778 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001779err_keymap:
1780 xkb_map_unref(xkb_info->keymap);
1781 free(xkb_info);
1782 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001783}
1784
1785static int
1786weston_compositor_build_global_keymap(struct weston_compositor *ec)
1787{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001788 struct xkb_keymap *keymap;
1789
1790 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001791 return 0;
1792
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001793 keymap = xkb_map_new_from_names(ec->xkb_context,
1794 &ec->xkb_names,
1795 0);
1796 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001797 weston_log("failed to compile global XKB keymap\n");
1798 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1799 "options %s\n",
1800 ec->xkb_names.rules, ec->xkb_names.model,
1801 ec->xkb_names.layout, ec->xkb_names.variant,
1802 ec->xkb_names.options);
1803 return -1;
1804 }
1805
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001806 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001807 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001808 return -1;
1809
1810 return 0;
1811}
Rob Bradford382ff462013-06-24 16:52:45 +01001812#else
1813int
1814weston_compositor_xkb_init(struct weston_compositor *ec,
1815 struct xkb_rule_names *names)
1816{
1817 return 0;
1818}
1819
1820void
1821weston_compositor_xkb_destroy(struct weston_compositor *ec)
1822{
1823}
1824#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825
Rui Matos65196bc2013-10-10 19:44:19 +02001826WL_EXPORT void
1827weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1828{
1829 if (!seat->keyboard || !keymap)
1830 return;
1831
1832#ifdef ENABLE_XKBCOMMON
1833 if (!seat->compositor->use_xkbcommon)
1834 return;
1835
1836 xkb_keymap_unref(seat->pending_keymap);
1837 seat->pending_keymap = xkb_keymap_ref(keymap);
1838
1839 if (seat->keyboard->keys.size == 0)
1840 update_keymap(seat);
1841#endif
1842}
1843
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844WL_EXPORT int
1845weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1846{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001847 struct weston_keyboard *keyboard;
1848
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001849 if (seat->keyboard) {
1850 seat->keyboard_device_count += 1;
1851 if (seat->keyboard_device_count == 1)
1852 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001853 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001854 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001855
Rob Bradford382ff462013-06-24 16:52:45 +01001856#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001857 if (seat->compositor->use_xkbcommon) {
1858 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001859 seat->xkb_info = weston_xkb_info_create(keymap);
1860 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001861 return -1;
1862 } else {
1863 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1864 return -1;
1865 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001866 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001867 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001868
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001869 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001870 if (seat->xkb_state.state == NULL) {
1871 weston_log("failed to initialise XKB state\n");
1872 return -1;
1873 }
1874
1875 seat->xkb_state.leds = 0;
1876 }
Rob Bradford382ff462013-06-24 16:52:45 +01001877#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001878
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001879 keyboard = weston_keyboard_create();
1880 if (keyboard == NULL) {
1881 weston_log("failed to allocate weston keyboard struct\n");
1882 return -1;
1883 }
1884
1885 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001886 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001887 keyboard->seat = seat;
1888
1889 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001890
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001891 return 0;
1892}
1893
1894WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001895weston_seat_release_keyboard(struct weston_seat *seat)
1896{
1897 seat->keyboard_device_count--;
1898 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001899 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001900 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001901 seat_send_updated_caps(seat);
1902 }
1903}
1904
1905WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001906weston_seat_init_pointer(struct weston_seat *seat)
1907{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001908 struct weston_pointer *pointer;
1909
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001910 if (seat->pointer) {
1911 seat->pointer_device_count += 1;
1912 if (seat->pointer_device_count == 1)
1913 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001914 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001915 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001916
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001917 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001918 if (pointer == NULL)
1919 return;
1920
1921 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001922 seat->pointer_device_count = 1;
1923 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001924
1925 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001926}
1927
1928WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001929weston_seat_release_pointer(struct weston_seat *seat)
1930{
1931 struct weston_pointer *pointer = seat->pointer;
1932
1933 seat->pointer_device_count--;
1934 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001935 weston_pointer_set_focus(pointer, NULL,
1936 wl_fixed_from_int(0),
1937 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001938 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001939
Jonas Ådahla4932742013-10-17 23:04:07 +02001940 if (pointer->sprite)
1941 pointer_unmap_sprite(pointer);
1942
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001943 seat_send_updated_caps(seat);
1944 }
1945}
1946
1947WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001948weston_seat_init_touch(struct weston_seat *seat)
1949{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001950 struct weston_touch *touch;
1951
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001952 if (seat->touch) {
1953 seat->touch_device_count += 1;
1954 if (seat->touch_device_count == 1)
1955 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001956 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001957 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001958
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001959 touch = weston_touch_create();
1960 if (touch == NULL)
1961 return;
1962
1963 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001964 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001965 touch->seat = seat;
1966
1967 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001968}
1969
1970WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001971weston_seat_release_touch(struct weston_seat *seat)
1972{
1973 seat->touch_device_count--;
1974 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001975 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001976 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001977 seat_send_updated_caps(seat);
1978 }
1979}
1980
1981WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001982weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1983 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001984{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001985 memset(seat, 0, sizeof *seat);
1986
Kristian Høgsberge3148752013-05-06 23:19:49 -04001987 seat->selection_data_source = NULL;
1988 wl_list_init(&seat->base_resource_list);
1989 wl_signal_init(&seat->selection_signal);
1990 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001991 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001992
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001993 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001994 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001995
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001996 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001997 seat->modifier_state = 0;
1998 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001999 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002000
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002001 wl_list_insert(ec->seat_list.prev, &seat->link);
2002
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002003 clipboard_create(seat);
2004
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002005 wl_signal_emit(&ec->seat_created_signal, seat);
2006}
2007
2008WL_EXPORT void
2009weston_seat_release(struct weston_seat *seat)
2010{
2011 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002012
Rob Bradford382ff462013-06-24 16:52:45 +01002013#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01002014 if (seat->compositor->use_xkbcommon) {
2015 if (seat->xkb_state.state != NULL)
2016 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002017 if (seat->xkb_info)
2018 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02002019 if (seat->pending_keymap)
2020 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01002021 }
Rob Bradford382ff462013-06-24 16:52:45 +01002022#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002023
Kristian Høgsberge3148752013-05-06 23:19:49 -04002024 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002025 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002026 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002027 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002028 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002029 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04002030
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002031 free (seat->seat_name);
2032
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002033 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04002034
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002035 wl_signal_emit(&seat->destroy_signal, seat);
2036}