blob: 159049e63702bb57d09ccce6b0e1d99af8bfb3aa [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;
398 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100399 wl_signal_init(&pointer->motion_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400401 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
402
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403 /* FIXME: Pick better co-ords. */
404 pointer->x = wl_fixed_from_int(100);
405 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400406
407 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408}
409
410WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400411weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400412{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400413 if (pointer->sprite)
414 pointer_unmap_sprite(pointer);
415
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400416 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100417
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400418 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400419}
420
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100421void
422weston_pointer_set_default_grab(struct weston_pointer *pointer,
423 const struct weston_pointer_grab_interface *interface)
424{
425 if (interface)
426 pointer->default_grab.interface = interface;
427 else
428 pointer->default_grab.interface =
429 &default_pointer_grab_interface;
430}
431
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400432WL_EXPORT struct weston_keyboard *
433weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400435 struct weston_keyboard *keyboard;
436
Peter Huttererf3d62272013-08-08 11:57:05 +1000437 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400438 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100439 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400440
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400441 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100442 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444 keyboard->default_grab.interface = &default_keyboard_grab_interface;
445 keyboard->default_grab.keyboard = keyboard;
446 keyboard->grab = &keyboard->default_grab;
447 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400448
449 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400450}
451
452WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400453weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454{
455 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100456
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400458 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400459}
460
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400461WL_EXPORT struct weston_touch *
462weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400463{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400464 struct weston_touch *touch;
465
Peter Huttererf3d62272013-08-08 11:57:05 +1000466 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400467 if (touch == NULL)
468 return NULL;
469
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100471 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472 touch->default_grab.interface = &default_touch_grab_interface;
473 touch->default_grab.touch = touch;
474 touch->grab = &touch->default_grab;
475 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400476
477 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478}
479
480WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400481weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482{
483 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100484
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400485 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486}
487
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400489seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400490{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100492 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200494 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200496 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200498 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400499 caps |= WL_SEAT_CAPABILITY_TOUCH;
500
Rob Bradford6e737f52013-09-06 17:48:19 +0100501 wl_resource_for_each(resource, &seat->base_resource_list) {
502 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500503 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400504}
505
506WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400507weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400509 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400511 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100512 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100513 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400514 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100515 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516 int different_surface = 0;
517
518 if ((!pointer->focus && view) ||
519 (pointer->focus && !view) ||
520 (pointer->focus && pointer->focus->surface != view->surface))
521 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522
Neil Roberts96d790e2013-09-19 17:32:00 +0100523 focus_resource_list = &pointer->focus_resource_list;
524
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100527 wl_resource_for_each(resource, focus_resource_list) {
528 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500529 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100530 }
531
532 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533 }
534
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535 if (find_resource_for_view(&pointer->resource_list, view) &&
536 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100537 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100539
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100541
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700543 send_modifiers_to_client_in_list(surface_client,
544 &kbd->resource_list,
545 serial,
546 kbd);
547
Neil Roberts96d790e2013-09-19 17:32:00 +0100548 move_resources_for_client(focus_resource_list,
549 &pointer->resource_list,
550 surface_client);
551
552 wl_resource_for_each(resource, focus_resource_list) {
553 wl_pointer_send_enter(resource,
554 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100556 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100558
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400559 pointer->focus_serial = serial;
560 }
561
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563 wl_signal_emit(&pointer->focus_signal, pointer);
564}
565
Neil Roberts96d790e2013-09-19 17:32:00 +0100566static void
567send_enter_to_resource_list(struct wl_list *list,
568 struct weston_keyboard *keyboard,
569 struct weston_surface *surface,
570 uint32_t serial)
571{
572 struct wl_resource *resource;
573
574 wl_resource_for_each(resource, list) {
575 send_modifiers_to_resource(keyboard, resource, serial);
576 wl_keyboard_send_enter(resource, serial,
577 surface->resource,
578 &keyboard->keys);
579 }
580}
581
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400583weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400584 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585{
586 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100587 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100589 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400590
Neil Roberts96d790e2013-09-19 17:32:00 +0100591 focus_resource_list = &keyboard->focus_resource_list;
592
593 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100595 wl_resource_for_each(resource, focus_resource_list) {
596 wl_keyboard_send_leave(resource, serial,
597 keyboard->focus->resource);
598 }
599 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600 }
601
Neil Roberts96d790e2013-09-19 17:32:00 +0100602 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
603 keyboard->focus != surface) {
604 struct wl_client *surface_client =
605 wl_resource_get_client(surface->resource);
606
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100608
609 move_resources_for_client(focus_resource_list,
610 &keyboard->resource_list,
611 surface_client);
612 send_enter_to_resource_list(focus_resource_list,
613 keyboard,
614 surface,
615 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616 keyboard->focus_serial = serial;
617 }
618
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619 keyboard->focus = surface;
620 wl_signal_emit(&keyboard->focus_signal, keyboard);
621}
622
623WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400624weston_keyboard_start_grab(struct weston_keyboard *keyboard,
625 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400626{
627 keyboard->grab = grab;
628 grab->keyboard = keyboard;
629
630 /* XXX focus? */
631}
632
633WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400634weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635{
636 keyboard->grab = &keyboard->default_grab;
637}
638
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200639static void
640weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
641{
642 keyboard->grab->interface->cancel(keyboard->grab);
643}
644
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400646weston_pointer_start_grab(struct weston_pointer *pointer,
647 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400648{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400649 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400650 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400651 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652}
653
654WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400655weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400656{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400657 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400658 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400659}
660
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200661static void
662weston_pointer_cancel_grab(struct weston_pointer *pointer)
663{
664 pointer->grab->interface->cancel(pointer->grab);
665}
666
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400667WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400668weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400669{
670 touch->grab = grab;
671 grab->touch = touch;
672}
673
674WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400675weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400676{
677 touch->grab = &touch->default_grab;
678}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400679
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200680static void
681weston_touch_cancel_grab(struct weston_touch *touch)
682{
683 touch->grab->interface->cancel(touch->grab);
684}
685
Rob Bradford806d8c02013-06-25 18:56:41 +0100686WL_EXPORT void
687weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400688{
Rob Bradford806d8c02013-06-25 18:56:41 +0100689 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400690 struct weston_output *output, *prev = NULL;
691 int x, y, old_x, old_y, valid = 0;
692
693 x = wl_fixed_to_int(*fx);
694 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100695 old_x = wl_fixed_to_int(pointer->x);
696 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400697
698 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100699 if (pointer->seat->output && pointer->seat->output != output)
700 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400701 if (pixman_region32_contains_point(&output->region,
702 x, y, NULL))
703 valid = 1;
704 if (pixman_region32_contains_point(&output->region,
705 old_x, old_y, NULL))
706 prev = output;
707 }
708
Rob Bradford66bd9f52013-06-25 18:56:42 +0100709 if (!prev)
710 prev = pointer->seat->output;
711
712 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400713 if (x < prev->x)
714 *fx = wl_fixed_from_int(prev->x);
715 else if (x >= prev->x + prev->width)
716 *fx = wl_fixed_from_int(prev->x +
717 prev->width - 1);
718 if (y < prev->y)
719 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200720 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400721 *fy = wl_fixed_from_int(prev->y +
722 prev->height - 1);
723 }
724}
725
726/* Takes absolute values */
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100727WL_EXPORT void
728weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400729{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400730 int32_t ix, iy;
731
Rob Bradford806d8c02013-06-25 18:56:41 +0100732 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400733
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400734 pointer->x = x;
735 pointer->y = y;
736
737 ix = wl_fixed_to_int(x);
738 iy = wl_fixed_to_int(y);
739
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400740 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500741 weston_view_set_position(pointer->sprite,
742 ix - pointer->hotspot_x,
743 iy - pointer->hotspot_y);
744 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400745 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100746
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100747 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100748 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400749}
750
751WL_EXPORT void
752notify_motion(struct weston_seat *seat,
753 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
754{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400755 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400756 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400757
758 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100759 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400760}
761
762WL_EXPORT void
763notify_motion_absolute(struct weston_seat *seat,
764 uint32_t time, wl_fixed_t x, wl_fixed_t y)
765{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400766 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400767 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400768
769 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100770 pointer->grab->interface->motion(pointer->grab, time, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400771}
772
773WL_EXPORT void
774weston_surface_activate(struct weston_surface *surface,
775 struct weston_seat *seat)
776{
777 struct weston_compositor *compositor = seat->compositor;
778
Kristian Høgsberge3148752013-05-06 23:19:49 -0400779 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400780 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400781 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400782 }
783
784 wl_signal_emit(&compositor->activate_signal, surface);
785}
786
787WL_EXPORT void
788notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
789 enum wl_pointer_button_state state)
790{
791 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400792 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400793 struct weston_surface *focus =
794 (struct weston_surface *) pointer->focus;
795 uint32_t serial = wl_display_next_serial(compositor->wl_display);
796
797 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
798 if (compositor->ping_handler && focus)
799 compositor->ping_handler(focus, serial);
800 weston_compositor_idle_inhibit(compositor);
801 if (pointer->button_count == 0) {
802 pointer->grab_button = button;
803 pointer->grab_time = time;
804 pointer->grab_x = pointer->x;
805 pointer->grab_y = pointer->y;
806 }
807 pointer->button_count++;
808 } else {
809 weston_compositor_idle_release(compositor);
810 pointer->button_count--;
811 }
812
813 weston_compositor_run_button_binding(compositor, seat, time, button,
814 state);
815
816 pointer->grab->interface->button(pointer->grab, time, button, state);
817
818 if (pointer->button_count == 1)
819 pointer->grab_serial =
820 wl_display_get_serial(compositor->wl_display);
821}
822
823WL_EXPORT void
824notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
825 wl_fixed_t value)
826{
827 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400828 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400829 struct weston_surface *focus =
830 (struct weston_surface *) pointer->focus;
831 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100832 struct wl_resource *resource;
833 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400834
835 if (compositor->ping_handler && focus)
836 compositor->ping_handler(focus, serial);
837
838 weston_compositor_wake(compositor);
839
840 if (!value)
841 return;
842
843 if (weston_compositor_run_axis_binding(compositor, seat,
844 time, axis, value))
845 return;
846
Neil Roberts96d790e2013-09-19 17:32:00 +0100847 resource_list = &pointer->focus_resource_list;
848 wl_resource_for_each(resource, resource_list)
849 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400850 value);
851}
852
Rob Bradford382ff462013-06-24 16:52:45 +0100853#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400854WL_EXPORT void
855notify_modifiers(struct weston_seat *seat, uint32_t serial)
856{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400857 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400858 struct weston_keyboard_grab *grab = keyboard->grab;
859 uint32_t mods_depressed, mods_latched, mods_locked, group;
860 uint32_t mods_lookup;
861 enum weston_led leds = 0;
862 int changed = 0;
863
864 /* Serialize and update our internal state, checking to see if it's
865 * different to the previous state. */
866 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
867 XKB_STATE_DEPRESSED);
868 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
869 XKB_STATE_LATCHED);
870 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
871 XKB_STATE_LOCKED);
872 group = xkb_state_serialize_group(seat->xkb_state.state,
873 XKB_STATE_EFFECTIVE);
874
Kristian Høgsberge3148752013-05-06 23:19:49 -0400875 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
876 mods_latched != seat->keyboard->modifiers.mods_latched ||
877 mods_locked != seat->keyboard->modifiers.mods_locked ||
878 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400879 changed = 1;
880
Kristian Høgsberge3148752013-05-06 23:19:49 -0400881 seat->keyboard->modifiers.mods_depressed = mods_depressed;
882 seat->keyboard->modifiers.mods_latched = mods_latched;
883 seat->keyboard->modifiers.mods_locked = mods_locked;
884 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400885
886 /* And update the modifier_state for bindings. */
887 mods_lookup = mods_depressed | mods_latched;
888 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000889 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400890 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000891 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400892 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000893 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400894 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000895 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400896 seat->modifier_state |= MODIFIER_SHIFT;
897
898 /* Finally, notify the compositor that LEDs have changed. */
899 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000900 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400901 leds |= LED_NUM_LOCK;
902 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000903 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400904 leds |= LED_CAPS_LOCK;
905 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000906 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400907 leds |= LED_SCROLL_LOCK;
908 if (leds != seat->xkb_state.leds && seat->led_update)
909 seat->led_update(seat, leds);
910 seat->xkb_state.leds = leds;
911
912 if (changed) {
913 grab->interface->modifiers(grab,
914 serial,
915 keyboard->modifiers.mods_depressed,
916 keyboard->modifiers.mods_latched,
917 keyboard->modifiers.mods_locked,
918 keyboard->modifiers.group);
919 }
920}
921
922static void
923update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
924 enum wl_keyboard_key_state state)
925{
926 enum xkb_key_direction direction;
927
Matt Roper01a92732013-06-24 16:52:44 +0100928 /* Keyboard modifiers don't exist in raw keyboard mode */
929 if (!seat->compositor->use_xkbcommon)
930 return;
931
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400932 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
933 direction = XKB_KEY_DOWN;
934 else
935 direction = XKB_KEY_UP;
936
937 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
938 * broken keycode system, which starts at 8. */
939 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
940
941 notify_modifiers(seat, serial);
942}
Rui Matos65196bc2013-10-10 19:44:19 +0200943
944static void
945send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
946{
947 wl_keyboard_send_keymap(resource,
948 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
949 xkb_info->keymap_fd,
950 xkb_info->keymap_size);
951}
952
953static void
954send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
955{
956 wl_keyboard_send_modifiers(resource, serial,
957 keyboard->modifiers.mods_depressed,
958 keyboard->modifiers.mods_latched,
959 keyboard->modifiers.mods_locked,
960 keyboard->modifiers.group);
961}
962
963static struct weston_xkb_info *
964weston_xkb_info_create(struct xkb_keymap *keymap);
965static void
966weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
967
968static void
969update_keymap(struct weston_seat *seat)
970{
971 struct wl_resource *resource;
972 struct weston_xkb_info *xkb_info;
973 struct xkb_state *state;
974 xkb_mod_mask_t latched_mods;
975 xkb_mod_mask_t locked_mods;
976
977 xkb_info = weston_xkb_info_create(seat->pending_keymap);
978
979 xkb_keymap_unref(seat->pending_keymap);
980 seat->pending_keymap = NULL;
981
982 if (!xkb_info) {
983 weston_log("failed to create XKB info\n");
984 return;
985 }
986
987 state = xkb_state_new(xkb_info->keymap);
988 if (!state) {
989 weston_log("failed to initialise XKB state\n");
990 weston_xkb_info_destroy(xkb_info);
991 return;
992 }
993
994 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
995 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
996 xkb_state_update_mask(state,
997 0, /* depressed */
998 latched_mods,
999 locked_mods,
1000 0, 0, 0);
1001
1002 weston_xkb_info_destroy(seat->xkb_info);
1003 seat->xkb_info = xkb_info;
1004
1005 xkb_state_unref(seat->xkb_state.state);
1006 seat->xkb_state.state = state;
1007
1008 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1009 send_keymap(resource, xkb_info);
1010 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1011 send_keymap(resource, xkb_info);
1012
1013 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1014
1015 if (!latched_mods && !locked_mods)
1016 return;
1017
1018 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1019 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1020 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1021 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1022}
Rob Bradford382ff462013-06-24 16:52:45 +01001023#else
1024WL_EXPORT void
1025notify_modifiers(struct weston_seat *seat, uint32_t serial)
1026{
1027}
1028
1029static void
1030update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1031 enum wl_keyboard_key_state state)
1032{
1033}
Rui Matos65196bc2013-10-10 19:44:19 +02001034
1035static void
1036update_keymap(struct weston_seat *seat)
1037{
1038}
Rob Bradford382ff462013-06-24 16:52:45 +01001039#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001040
1041WL_EXPORT void
1042notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1043 enum wl_keyboard_key_state state,
1044 enum weston_key_state_update update_state)
1045{
1046 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001047 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001049 struct weston_keyboard_grab *grab = keyboard->grab;
1050 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1051 uint32_t *k, *end;
1052
1053 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1054 if (compositor->ping_handler && focus)
1055 compositor->ping_handler(focus, serial);
1056
1057 weston_compositor_idle_inhibit(compositor);
1058 keyboard->grab_key = key;
1059 keyboard->grab_time = time;
1060 } else {
1061 weston_compositor_idle_release(compositor);
1062 }
1063
1064 end = keyboard->keys.data + keyboard->keys.size;
1065 for (k = keyboard->keys.data; k < end; k++) {
1066 if (*k == key) {
1067 /* Ignore server-generated repeats. */
1068 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1069 return;
1070 *k = *--end;
1071 }
1072 }
1073 keyboard->keys.size = (void *) end - keyboard->keys.data;
1074 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1075 k = wl_array_add(&keyboard->keys, sizeof *k);
1076 *k = key;
1077 }
1078
1079 if (grab == &keyboard->default_grab ||
1080 grab == &keyboard->input_method_grab) {
1081 weston_compositor_run_key_binding(compositor, seat, time, key,
1082 state);
1083 grab = keyboard->grab;
1084 }
1085
1086 grab->interface->key(grab, time, key, state);
1087
Rui Matos65196bc2013-10-10 19:44:19 +02001088 if (seat->pending_keymap &&
1089 keyboard->keys.size == 0)
1090 update_keymap(seat);
1091
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001092 if (update_state == STATE_UPDATE_AUTOMATIC) {
1093 update_modifier_state(seat,
1094 wl_display_get_serial(compositor->wl_display),
1095 key,
1096 state);
1097 }
1098}
1099
1100WL_EXPORT void
1101notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1102 wl_fixed_t x, wl_fixed_t y)
1103{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001104 if (output) {
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001105 weston_pointer_move(seat->pointer, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001106 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001107 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001108 * NULL) here, but somehow that breaks re-entry... */
1109 }
1110}
1111
1112static void
1113destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1114{
1115 struct weston_seat *ws;
1116
1117 ws = container_of(listener, struct weston_seat,
1118 saved_kbd_focus_listener);
1119
1120 ws->saved_kbd_focus = NULL;
1121}
1122
1123WL_EXPORT void
1124notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1125 enum weston_key_state_update update_state)
1126{
1127 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001128 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001129 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001130 uint32_t *k, serial;
1131
1132 serial = wl_display_next_serial(compositor->wl_display);
1133 wl_array_copy(&keyboard->keys, keys);
1134 wl_array_for_each(k, &keyboard->keys) {
1135 weston_compositor_idle_inhibit(compositor);
1136 if (update_state == STATE_UPDATE_AUTOMATIC)
1137 update_modifier_state(seat, serial, *k,
1138 WL_KEYBOARD_KEY_STATE_PRESSED);
1139 }
1140
1141 /* Run key bindings after we've updated the state. */
1142 wl_array_for_each(k, &keyboard->keys) {
1143 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1144 WL_KEYBOARD_KEY_STATE_PRESSED);
1145 }
1146
1147 surface = seat->saved_kbd_focus;
1148
1149 if (surface) {
1150 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1151 weston_keyboard_set_focus(keyboard, surface);
1152 seat->saved_kbd_focus = NULL;
1153 }
1154}
1155
1156WL_EXPORT void
1157notify_keyboard_focus_out(struct weston_seat *seat)
1158{
1159 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001160 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001161 uint32_t *k, serial;
1162
1163 serial = wl_display_next_serial(compositor->wl_display);
1164 wl_array_for_each(k, &keyboard->keys) {
1165 weston_compositor_idle_release(compositor);
1166 update_modifier_state(seat, serial, *k,
1167 WL_KEYBOARD_KEY_STATE_RELEASED);
1168 }
1169
1170 seat->modifier_state = 0;
1171
1172 if (keyboard->focus) {
1173 seat->saved_kbd_focus = keyboard->focus;
1174 seat->saved_kbd_focus_listener.notify =
1175 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001176 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001177 &seat->saved_kbd_focus_listener);
1178 }
1179
1180 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001181 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182}
1183
Michael Fua2bb7912013-07-23 15:51:06 +08001184WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001186{
Neil Roberts96d790e2013-09-19 17:32:00 +01001187 struct wl_list *focus_resource_list;
1188
1189 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001190
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001191 if (view && seat->touch->focus &&
1192 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001196
Neil Roberts96d790e2013-09-19 17:32:00 +01001197 if (!wl_list_empty(focus_resource_list)) {
1198 move_resources(&seat->touch->resource_list,
1199 focus_resource_list);
1200 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001203 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001205 move_resources_for_client(focus_resource_list,
1206 &seat->touch->resource_list,
1207 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001210}
1211
1212/**
1213 * notify_touch - emulates button touches and notifies surfaces accordingly.
1214 *
1215 * It assumes always the correct cycle sequence until it gets here: touch_down
1216 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1217 * for sending along such order.
1218 *
1219 */
1220WL_EXPORT void
1221notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1222 wl_fixed_t x, wl_fixed_t y, int touch_type)
1223{
1224 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001225 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001226 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228 wl_fixed_t sx, sy;
1229
1230 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001231 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1232 touch->grab_x = x;
1233 touch->grab_y = y;
1234 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001235
1236 switch (touch_type) {
1237 case WL_TOUCH_DOWN:
1238 weston_compositor_idle_inhibit(ec);
1239
1240 seat->num_tp++;
1241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 /* the first finger down picks the view, and all further go
1243 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001244 * until all touch points are up again. */
1245 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1247 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001248 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 ev = touch->focus;
1250 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001251 } else {
1252 /* Unexpected condition: We have non-initial touch but
1253 * there is no focused surface.
1254 */
1255 weston_log("touch event received with %d points down"
1256 "but no surface focused\n", seat->num_tp);
1257 return;
1258 }
1259
1260 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001261 if (seat->num_tp == 1) {
1262 touch->grab_serial =
1263 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001264 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001265 touch->grab_time = time;
1266 touch->grab_x = x;
1267 touch->grab_y = y;
1268 }
1269
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001270 break;
1271 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 ev = touch->focus;
1273 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274 break;
1275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001277 grab->interface->motion(grab, time, touch_id, sx, sy);
1278 break;
1279 case WL_TOUCH_UP:
1280 weston_compositor_idle_release(ec);
1281 seat->num_tp--;
1282
1283 grab->interface->up(grab, time, touch_id);
1284 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001285 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001286 break;
1287 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001288
1289 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001290}
1291
1292static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001293pointer_cursor_surface_configure(struct weston_surface *es,
1294 int32_t dx, int32_t dy, int32_t width, int32_t height)
1295{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001296 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001297 int x, y;
1298
1299 if (width == 0)
1300 return;
1301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001303
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001304 pointer->hotspot_x -= dx;
1305 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001306
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001307 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1308 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001311
1312 empty_region(&es->pending.input);
1313
1314 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 wl_list_insert(&es->compositor->cursor_layer.view_list,
1316 &pointer->sprite->layer_link);
1317 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001318 }
1319}
1320
1321static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001322pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1323 uint32_t serial, struct wl_resource *surface_resource,
1324 int32_t x, int32_t y)
1325{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001326 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001327 struct weston_surface *surface = NULL;
1328
1329 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001330 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001331
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001332 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001335 black_surface used in shell.c for fullscreen don't have
1336 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001338 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001340 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001341 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001342 return;
1343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001345 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001346 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001347 WL_DISPLAY_ERROR_INVALID_OBJECT,
1348 "surface->configure already "
1349 "set");
1350 return;
1351 }
1352 }
1353
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001354 if (pointer->sprite)
1355 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001356
1357 if (!surface)
1358 return;
1359
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001360 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001361 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001362
1363 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001364 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001366 pointer->hotspot_x = x;
1367 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001368
1369 if (surface->buffer_ref.buffer)
1370 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1371 weston_surface_buffer_height(surface));
1372}
1373
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001374static void
1375pointer_release(struct wl_client *client, struct wl_resource *resource)
1376{
1377 wl_resource_destroy(resource);
1378}
1379
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001380static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001381 pointer_set_cursor,
1382 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001383};
1384
1385static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001386seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1387 uint32_t id)
1388{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001389 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001390 struct wl_resource *cr;
1391
Kristian Høgsberge3148752013-05-06 23:19:49 -04001392 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001393 return;
1394
Jason Ekstranda85118c2013-06-27 20:17:02 -05001395 cr = wl_resource_create(client, &wl_pointer_interface,
1396 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001397 if (cr == NULL) {
1398 wl_client_post_no_memory(client);
1399 return;
1400 }
1401
Neil Roberts96d790e2013-09-19 17:32:00 +01001402 /* May be moved to focused list later by either
1403 * weston_pointer_set_focus or directly if this client is already
1404 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001405 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001406 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1407 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001408
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1410 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001411 wl_fixed_t sx, sy;
1412
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 weston_view_from_global_fixed(seat->pointer->focus,
1414 seat->pointer->x,
1415 seat->pointer->y,
1416 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001417
1418 wl_list_remove(wl_resource_get_link(cr));
1419 wl_list_insert(&seat->pointer->focus_resource_list,
1420 wl_resource_get_link(cr));
1421 wl_pointer_send_enter(cr,
1422 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001424 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001425 }
1426}
1427
1428static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001429keyboard_release(struct wl_client *client, struct wl_resource *resource)
1430{
1431 wl_resource_destroy(resource);
1432}
1433
1434static const struct wl_keyboard_interface keyboard_interface = {
1435 keyboard_release
1436};
1437
Neil Roberts96d790e2013-09-19 17:32:00 +01001438static int
1439should_send_modifiers_to_client(struct weston_seat *seat,
1440 struct wl_client *client)
1441{
1442 if (seat->keyboard &&
1443 seat->keyboard->focus &&
1444 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1445 return 1;
1446
1447 if (seat->pointer &&
1448 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001450 return 1;
1451
1452 return 0;
1453}
1454
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001455static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001456seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1457 uint32_t id)
1458{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001459 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001460 struct wl_resource *cr;
1461
Kristian Høgsberge3148752013-05-06 23:19:49 -04001462 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001463 return;
1464
Jason Ekstranda85118c2013-06-27 20:17:02 -05001465 cr = wl_resource_create(client, &wl_keyboard_interface,
1466 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001467 if (cr == NULL) {
1468 wl_client_post_no_memory(client);
1469 return;
1470 }
1471
Neil Roberts96d790e2013-09-19 17:32:00 +01001472 /* May be moved to focused list later by either
1473 * weston_keyboard_set_focus or directly if this client is already
1474 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001475 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001476 wl_resource_set_implementation(cr, &keyboard_interface,
1477 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001478
Matt Roper01a92732013-06-24 16:52:44 +01001479 if (seat->compositor->use_xkbcommon) {
1480 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001481 seat->xkb_info->keymap_fd,
1482 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001483 } else {
1484 int null_fd = open("/dev/null", O_RDONLY);
1485 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1486 null_fd,
1487 0);
1488 close(null_fd);
1489 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001490
Neil Roberts96d790e2013-09-19 17:32:00 +01001491 if (should_send_modifiers_to_client(seat, client)) {
1492 send_modifiers_to_resource(seat->keyboard,
1493 cr,
1494 seat->keyboard->focus_serial);
1495 }
1496
Kristian Høgsberge3148752013-05-06 23:19:49 -04001497 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001498 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001499 struct weston_surface *surface =
1500 (struct weston_surface *) seat->keyboard->focus;
1501
1502 wl_list_remove(wl_resource_get_link(cr));
1503 wl_list_insert(&seat->keyboard->focus_resource_list,
1504 wl_resource_get_link(cr));
1505 wl_keyboard_send_enter(cr,
1506 seat->keyboard->focus_serial,
1507 surface->resource,
1508 &seat->keyboard->keys);
1509
1510 /* If this is the first keyboard resource for this
1511 * client... */
1512 if (seat->keyboard->focus_resource_list.prev ==
1513 wl_resource_get_link(cr))
1514 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001515 }
1516}
1517
1518static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001519touch_release(struct wl_client *client, struct wl_resource *resource)
1520{
1521 wl_resource_destroy(resource);
1522}
1523
1524static const struct wl_touch_interface touch_interface = {
1525 touch_release
1526};
1527
1528static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001529seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1530 uint32_t id)
1531{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001532 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001533 struct wl_resource *cr;
1534
Kristian Høgsberge3148752013-05-06 23:19:49 -04001535 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001536 return;
1537
Jason Ekstranda85118c2013-06-27 20:17:02 -05001538 cr = wl_resource_create(client, &wl_touch_interface,
1539 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001540 if (cr == NULL) {
1541 wl_client_post_no_memory(client);
1542 return;
1543 }
1544
Neil Roberts96d790e2013-09-19 17:32:00 +01001545 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001547 wl_list_insert(&seat->touch->resource_list,
1548 wl_resource_get_link(cr));
1549 } else {
1550 wl_list_insert(&seat->touch->focus_resource_list,
1551 wl_resource_get_link(cr));
1552 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001553 wl_resource_set_implementation(cr, &touch_interface,
1554 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001555}
1556
1557static const struct wl_seat_interface seat_interface = {
1558 seat_get_pointer,
1559 seat_get_keyboard,
1560 seat_get_touch,
1561};
1562
1563static void
1564bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1565{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001566 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001567 struct wl_resource *resource;
1568 enum wl_seat_capability caps = 0;
1569
Jason Ekstranda85118c2013-06-27 20:17:02 -05001570 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001571 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001572 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001573 wl_resource_set_implementation(resource, &seat_interface, data,
1574 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001575
1576 if (seat->pointer)
1577 caps |= WL_SEAT_CAPABILITY_POINTER;
1578 if (seat->keyboard)
1579 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1580 if (seat->touch)
1581 caps |= WL_SEAT_CAPABILITY_TOUCH;
1582
1583 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001584 if (version >= 2)
1585 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001586}
1587
Rob Bradford382ff462013-06-24 16:52:45 +01001588#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001589int
1590weston_compositor_xkb_init(struct weston_compositor *ec,
1591 struct xkb_rule_names *names)
1592{
Rob Bradford382ff462013-06-24 16:52:45 +01001593 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001594
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001595 if (ec->xkb_context == NULL) {
1596 ec->xkb_context = xkb_context_new(0);
1597 if (ec->xkb_context == NULL) {
1598 weston_log("failed to create XKB context\n");
1599 return -1;
1600 }
1601 }
1602
1603 if (names)
1604 ec->xkb_names = *names;
1605 if (!ec->xkb_names.rules)
1606 ec->xkb_names.rules = strdup("evdev");
1607 if (!ec->xkb_names.model)
1608 ec->xkb_names.model = strdup("pc105");
1609 if (!ec->xkb_names.layout)
1610 ec->xkb_names.layout = strdup("us");
1611
1612 return 0;
1613}
1614
Stefan Schmidtfda26522013-09-17 10:54:09 +01001615static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001616weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001617{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001618 if (--xkb_info->ref_count > 0)
1619 return;
1620
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001621 if (xkb_info->keymap)
1622 xkb_map_unref(xkb_info->keymap);
1623
1624 if (xkb_info->keymap_area)
1625 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1626 if (xkb_info->keymap_fd >= 0)
1627 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001628 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001629}
1630
1631void
1632weston_compositor_xkb_destroy(struct weston_compositor *ec)
1633{
Matt Roper01a92732013-06-24 16:52:44 +01001634 /*
1635 * If we're operating in raw keyboard mode, we never initialized
1636 * libxkbcommon so there's no cleanup to do either.
1637 */
1638 if (!ec->use_xkbcommon)
1639 return;
1640
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001641 free((char *) ec->xkb_names.rules);
1642 free((char *) ec->xkb_names.model);
1643 free((char *) ec->xkb_names.layout);
1644 free((char *) ec->xkb_names.variant);
1645 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001646
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001647 if (ec->xkb_info)
1648 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001649 xkb_context_unref(ec->xkb_context);
1650}
1651
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001652static struct weston_xkb_info *
1653weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001654{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001655 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1656 if (xkb_info == NULL)
1657 return NULL;
1658
1659 xkb_info->keymap = xkb_map_ref(keymap);
1660 xkb_info->ref_count = 1;
1661
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001662 char *keymap_str;
1663
1664 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1665 XKB_MOD_NAME_SHIFT);
1666 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1667 XKB_MOD_NAME_CAPS);
1668 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1669 XKB_MOD_NAME_CTRL);
1670 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1671 XKB_MOD_NAME_ALT);
1672 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1673 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1674 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1675 XKB_MOD_NAME_LOGO);
1676 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1677
1678 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1679 XKB_LED_NAME_NUM);
1680 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1681 XKB_LED_NAME_CAPS);
1682 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1683 XKB_LED_NAME_SCROLL);
1684
1685 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1686 if (keymap_str == NULL) {
1687 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001688 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001689 }
1690 xkb_info->keymap_size = strlen(keymap_str) + 1;
1691
1692 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1693 if (xkb_info->keymap_fd < 0) {
1694 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1695 (unsigned long) xkb_info->keymap_size);
1696 goto err_keymap_str;
1697 }
1698
1699 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1700 PROT_READ | PROT_WRITE,
1701 MAP_SHARED, xkb_info->keymap_fd, 0);
1702 if (xkb_info->keymap_area == MAP_FAILED) {
1703 weston_log("failed to mmap() %lu bytes\n",
1704 (unsigned long) xkb_info->keymap_size);
1705 goto err_dev_zero;
1706 }
1707 strcpy(xkb_info->keymap_area, keymap_str);
1708 free(keymap_str);
1709
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001710 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001711
1712err_dev_zero:
1713 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001714err_keymap_str:
1715 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001716err_keymap:
1717 xkb_map_unref(xkb_info->keymap);
1718 free(xkb_info);
1719 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720}
1721
1722static int
1723weston_compositor_build_global_keymap(struct weston_compositor *ec)
1724{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001725 struct xkb_keymap *keymap;
1726
1727 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001728 return 0;
1729
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001730 keymap = xkb_map_new_from_names(ec->xkb_context,
1731 &ec->xkb_names,
1732 0);
1733 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001734 weston_log("failed to compile global XKB keymap\n");
1735 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1736 "options %s\n",
1737 ec->xkb_names.rules, ec->xkb_names.model,
1738 ec->xkb_names.layout, ec->xkb_names.variant,
1739 ec->xkb_names.options);
1740 return -1;
1741 }
1742
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001743 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001744 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001745 return -1;
1746
1747 return 0;
1748}
Rob Bradford382ff462013-06-24 16:52:45 +01001749#else
1750int
1751weston_compositor_xkb_init(struct weston_compositor *ec,
1752 struct xkb_rule_names *names)
1753{
1754 return 0;
1755}
1756
1757void
1758weston_compositor_xkb_destroy(struct weston_compositor *ec)
1759{
1760}
1761#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001762
Rui Matos65196bc2013-10-10 19:44:19 +02001763WL_EXPORT void
1764weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1765{
1766 if (!seat->keyboard || !keymap)
1767 return;
1768
1769#ifdef ENABLE_XKBCOMMON
1770 if (!seat->compositor->use_xkbcommon)
1771 return;
1772
1773 xkb_keymap_unref(seat->pending_keymap);
1774 seat->pending_keymap = xkb_keymap_ref(keymap);
1775
1776 if (seat->keyboard->keys.size == 0)
1777 update_keymap(seat);
1778#endif
1779}
1780
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001781WL_EXPORT int
1782weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1783{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001784 struct weston_keyboard *keyboard;
1785
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001786 if (seat->keyboard) {
1787 seat->keyboard_device_count += 1;
1788 if (seat->keyboard_device_count == 1)
1789 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001790 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001791 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792
Rob Bradford382ff462013-06-24 16:52:45 +01001793#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001794 if (seat->compositor->use_xkbcommon) {
1795 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001796 seat->xkb_info = weston_xkb_info_create(keymap);
1797 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001798 return -1;
1799 } else {
1800 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1801 return -1;
1802 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001803 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001804 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001805
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001806 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001807 if (seat->xkb_state.state == NULL) {
1808 weston_log("failed to initialise XKB state\n");
1809 return -1;
1810 }
1811
1812 seat->xkb_state.leds = 0;
1813 }
Rob Bradford382ff462013-06-24 16:52:45 +01001814#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001815
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001816 keyboard = weston_keyboard_create();
1817 if (keyboard == NULL) {
1818 weston_log("failed to allocate weston keyboard struct\n");
1819 return -1;
1820 }
1821
1822 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001823 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001824 keyboard->seat = seat;
1825
1826 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828 return 0;
1829}
1830
1831WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001832weston_seat_release_keyboard(struct weston_seat *seat)
1833{
1834 seat->keyboard_device_count--;
1835 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001836 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001837 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001838 seat_send_updated_caps(seat);
1839 }
1840}
1841
1842WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001843weston_seat_init_pointer(struct weston_seat *seat)
1844{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001845 struct weston_pointer *pointer;
1846
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001847 if (seat->pointer) {
1848 seat->pointer_device_count += 1;
1849 if (seat->pointer_device_count == 1)
1850 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001851 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001852 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001853
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001854 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001855 if (pointer == NULL)
1856 return;
1857
1858 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001859 seat->pointer_device_count = 1;
1860 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001861
1862 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001863}
1864
1865WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001866weston_seat_release_pointer(struct weston_seat *seat)
1867{
1868 struct weston_pointer *pointer = seat->pointer;
1869
1870 seat->pointer_device_count--;
1871 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001872 weston_pointer_set_focus(pointer, NULL,
1873 wl_fixed_from_int(0),
1874 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001875 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001876
Jonas Ådahla4932742013-10-17 23:04:07 +02001877 if (pointer->sprite)
1878 pointer_unmap_sprite(pointer);
1879
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001880 seat_send_updated_caps(seat);
1881 }
1882}
1883
1884WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001885weston_seat_init_touch(struct weston_seat *seat)
1886{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001887 struct weston_touch *touch;
1888
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001889 if (seat->touch) {
1890 seat->touch_device_count += 1;
1891 if (seat->touch_device_count == 1)
1892 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001894 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001895
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001896 touch = weston_touch_create();
1897 if (touch == NULL)
1898 return;
1899
1900 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001901 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001902 touch->seat = seat;
1903
1904 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001905}
1906
1907WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001908weston_seat_release_touch(struct weston_seat *seat)
1909{
1910 seat->touch_device_count--;
1911 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001912 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001913 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001914 seat_send_updated_caps(seat);
1915 }
1916}
1917
1918WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001919weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1920 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001921{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001922 memset(seat, 0, sizeof *seat);
1923
Kristian Høgsberge3148752013-05-06 23:19:49 -04001924 seat->selection_data_source = NULL;
1925 wl_list_init(&seat->base_resource_list);
1926 wl_signal_init(&seat->selection_signal);
1927 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001928 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001929
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001930 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001931 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001932
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001934 seat->modifier_state = 0;
1935 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001936 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001937
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001938 wl_list_insert(ec->seat_list.prev, &seat->link);
1939
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001940 clipboard_create(seat);
1941
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001942 wl_signal_emit(&ec->seat_created_signal, seat);
1943}
1944
1945WL_EXPORT void
1946weston_seat_release(struct weston_seat *seat)
1947{
1948 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001949
Rob Bradford382ff462013-06-24 16:52:45 +01001950#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001951 if (seat->compositor->use_xkbcommon) {
1952 if (seat->xkb_state.state != NULL)
1953 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001954 if (seat->xkb_info)
1955 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001956 if (seat->pending_keymap)
1957 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001958 }
Rob Bradford382ff462013-06-24 16:52:45 +01001959#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001960
Kristian Høgsberge3148752013-05-06 23:19:49 -04001961 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001962 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001963 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001964 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001965 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001966 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001967
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001968 free (seat->seat_name);
1969
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001970 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001971
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001972 wl_signal_emit(&seat->destroy_signal, seat);
1973}