blob: e285c593ac2dc73106b02ba9a55c4705bb30f1b2 [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 Ekstrand42133d42013-11-14 20:06:16 -0600338 if (pointer && pointer->focus && pointer->focus->surface->resource &&
339 pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100340 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100342 send_modifiers_to_client_in_list(pointer_client,
343 &keyboard->resource_list,
344 serial,
345 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400346 }
347}
348
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200349static void
350default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
351{
352}
353
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400354static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400355 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700356 default_grab_keyboard_key,
357 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200358 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400359};
360
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400361static void
362pointer_unmap_sprite(struct weston_pointer *pointer)
363{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500364 if (weston_surface_is_mapped(pointer->sprite->surface))
365 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400366
367 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368 pointer->sprite->surface->configure = NULL;
369 pointer->sprite->surface->configure_private = NULL;
370 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400371 pointer->sprite = NULL;
372}
373
374static void
375pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
376{
377 struct weston_pointer *pointer =
378 container_of(listener, struct weston_pointer,
379 sprite_destroy_listener);
380
381 pointer->sprite = NULL;
382}
383
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400384WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100385weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400386{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400387 struct weston_pointer *pointer;
388
Peter Huttererf3d62272013-08-08 11:57:05 +1000389 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400390 if (pointer == NULL)
391 return NULL;
392
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400393 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100394 wl_list_init(&pointer->focus_resource_list);
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100395 weston_pointer_set_default_grab(pointer,
396 seat->compositor->default_pointer_grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400397 pointer->default_grab.pointer = pointer;
398 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100399 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100400 wl_signal_init(&pointer->focus_signal);
401 wl_list_init(&pointer->focus_listener.link);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400403 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
404
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400405 /* FIXME: Pick better co-ords. */
406 pointer->x = wl_fixed_from_int(100);
407 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400408
409 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400410}
411
412WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400413weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400415 if (pointer->sprite)
416 pointer_unmap_sprite(pointer);
417
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100419
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400420 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400421}
422
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100423void
424weston_pointer_set_default_grab(struct weston_pointer *pointer,
425 const struct weston_pointer_grab_interface *interface)
426{
427 if (interface)
428 pointer->default_grab.interface = interface;
429 else
430 pointer->default_grab.interface =
431 &default_pointer_grab_interface;
432}
433
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400434WL_EXPORT struct weston_keyboard *
435weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400436{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400437 struct weston_keyboard *keyboard;
438
Peter Huttererf3d62272013-08-08 11:57:05 +1000439 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400440 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100441 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400442
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100444 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400445 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400446 keyboard->default_grab.interface = &default_keyboard_grab_interface;
447 keyboard->default_grab.keyboard = keyboard;
448 keyboard->grab = &keyboard->default_grab;
449 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400450
451 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400452}
453
454WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400455weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456{
457 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100458
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400459 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400460 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400461}
462
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400463WL_EXPORT struct weston_touch *
464weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400465{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400466 struct weston_touch *touch;
467
Peter Huttererf3d62272013-08-08 11:57:05 +1000468 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400469 if (touch == NULL)
470 return NULL;
471
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100473 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400474 touch->default_grab.interface = &default_touch_grab_interface;
475 touch->default_grab.touch = touch;
476 touch->grab = &touch->default_grab;
477 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400478
479 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400480}
481
482WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400483weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400484{
485 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100486
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400487 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488}
489
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400490static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400491seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400492{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100494 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200496 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200498 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400499 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200500 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400501 caps |= WL_SEAT_CAPABILITY_TOUCH;
502
Rob Bradford6e737f52013-09-06 17:48:19 +0100503 wl_resource_for_each(resource, &seat->base_resource_list) {
504 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500505 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506}
507
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100508static void
509destroy_pointer_focus(struct wl_listener *listener, void *data)
510{
511 struct weston_pointer *pointer;
512
513 pointer = container_of(listener, struct weston_pointer,
514 focus_listener);
515
516 pointer->focus = NULL;
517 move_resources(&pointer->resource_list, &pointer->focus_resource_list);
518
519 wl_list_remove(&pointer->focus_listener.link);
520 wl_list_init(&pointer->focus_listener.link);
521
522 wl_signal_emit(&pointer->focus_signal, pointer);
523}
524
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400526weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400528 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400529{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400530 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100531 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100532 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100534 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535 int different_surface = 0;
536
537 if ((!pointer->focus && view) ||
538 (pointer->focus && !view) ||
539 (pointer->focus && pointer->focus->surface != view->surface))
540 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541
Neil Roberts96d790e2013-09-19 17:32:00 +0100542 focus_resource_list = &pointer->focus_resource_list;
543
Jason Ekstranda7af7042013-10-12 22:38:11 -0500544 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400545 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100546 wl_resource_for_each(resource, focus_resource_list) {
547 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500548 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100549 }
550
551 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400552 }
553
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 if (find_resource_for_view(&pointer->resource_list, view) &&
555 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100556 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100558
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400559 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100560
Jason Ekstranda7af7042013-10-12 22:38:11 -0500561 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700562 send_modifiers_to_client_in_list(surface_client,
563 &kbd->resource_list,
564 serial,
565 kbd);
566
Neil Roberts96d790e2013-09-19 17:32:00 +0100567 move_resources_for_client(focus_resource_list,
568 &pointer->resource_list,
569 surface_client);
570
571 wl_resource_for_each(resource, focus_resource_list) {
572 wl_pointer_send_enter(resource,
573 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500574 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100575 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400576 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100577
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578 pointer->focus_serial = serial;
579 }
580
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100581 if (!wl_list_empty(&pointer->focus_listener.link)) {
582 wl_list_remove(&pointer->focus_listener.link);
583 wl_list_init(&pointer->focus_listener.link);
584 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585 pointer->focus = view;
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +0100586 pointer->focus_listener.notify = destroy_pointer_focus;
587 if (view)
588 wl_signal_add(&view->destroy_signal, &pointer->focus_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400589 wl_signal_emit(&pointer->focus_signal, pointer);
590}
591
Neil Roberts96d790e2013-09-19 17:32:00 +0100592static void
593send_enter_to_resource_list(struct wl_list *list,
594 struct weston_keyboard *keyboard,
595 struct weston_surface *surface,
596 uint32_t serial)
597{
598 struct wl_resource *resource;
599
600 wl_resource_for_each(resource, list) {
601 send_modifiers_to_resource(keyboard, resource, serial);
602 wl_keyboard_send_enter(resource, serial,
603 surface->resource,
604 &keyboard->keys);
605 }
606}
607
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400608WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400609weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400610 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400611{
612 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100613 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100615 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616
Neil Roberts96d790e2013-09-19 17:32:00 +0100617 focus_resource_list = &keyboard->focus_resource_list;
618
619 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100621 wl_resource_for_each(resource, focus_resource_list) {
622 wl_keyboard_send_leave(resource, serial,
623 keyboard->focus->resource);
624 }
625 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400626 }
627
Neil Roberts96d790e2013-09-19 17:32:00 +0100628 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
629 keyboard->focus != surface) {
630 struct wl_client *surface_client =
631 wl_resource_get_client(surface->resource);
632
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100634
635 move_resources_for_client(focus_resource_list,
636 &keyboard->resource_list,
637 surface_client);
638 send_enter_to_resource_list(focus_resource_list,
639 keyboard,
640 surface,
641 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400642 keyboard->focus_serial = serial;
643 }
644
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645 keyboard->focus = surface;
646 wl_signal_emit(&keyboard->focus_signal, keyboard);
647}
648
649WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400650weston_keyboard_start_grab(struct weston_keyboard *keyboard,
651 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652{
653 keyboard->grab = grab;
654 grab->keyboard = keyboard;
655
656 /* XXX focus? */
657}
658
659WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400660weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400661{
662 keyboard->grab = &keyboard->default_grab;
663}
664
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200665static void
666weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
667{
668 keyboard->grab->interface->cancel(keyboard->grab);
669}
670
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400671WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400672weston_pointer_start_grab(struct weston_pointer *pointer,
673 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400674{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400675 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400676 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400677 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400678}
679
680WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400681weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400682{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400683 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400684 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400685}
686
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200687static void
688weston_pointer_cancel_grab(struct weston_pointer *pointer)
689{
690 pointer->grab->interface->cancel(pointer->grab);
691}
692
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400693WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400694weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400695{
696 touch->grab = grab;
697 grab->touch = touch;
698}
699
700WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400701weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400702{
703 touch->grab = &touch->default_grab;
704}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400705
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200706static void
707weston_touch_cancel_grab(struct weston_touch *touch)
708{
709 touch->grab->interface->cancel(touch->grab);
710}
711
Rob Bradford806d8c02013-06-25 18:56:41 +0100712WL_EXPORT void
713weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400714{
Rob Bradford806d8c02013-06-25 18:56:41 +0100715 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400716 struct weston_output *output, *prev = NULL;
717 int x, y, old_x, old_y, valid = 0;
718
719 x = wl_fixed_to_int(*fx);
720 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100721 old_x = wl_fixed_to_int(pointer->x);
722 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400723
724 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100725 if (pointer->seat->output && pointer->seat->output != output)
726 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400727 if (pixman_region32_contains_point(&output->region,
728 x, y, NULL))
729 valid = 1;
730 if (pixman_region32_contains_point(&output->region,
731 old_x, old_y, NULL))
732 prev = output;
733 }
734
Rob Bradford66bd9f52013-06-25 18:56:42 +0100735 if (!prev)
736 prev = pointer->seat->output;
737
738 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400739 if (x < prev->x)
740 *fx = wl_fixed_from_int(prev->x);
741 else if (x >= prev->x + prev->width)
742 *fx = wl_fixed_from_int(prev->x +
743 prev->width - 1);
744 if (y < prev->y)
745 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200746 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400747 *fy = wl_fixed_from_int(prev->y +
748 prev->height - 1);
749 }
750}
751
752/* Takes absolute values */
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100753WL_EXPORT void
754weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400755{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400756 int32_t ix, iy;
757
Rob Bradford806d8c02013-06-25 18:56:41 +0100758 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400759
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400760 pointer->x = x;
761 pointer->y = y;
762
763 ix = wl_fixed_to_int(x);
764 iy = wl_fixed_to_int(y);
765
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400766 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 weston_view_set_position(pointer->sprite,
768 ix - pointer->hotspot_x,
769 iy - pointer->hotspot_y);
770 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400771 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100772
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100773 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100774 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400775}
776
777WL_EXPORT void
778notify_motion(struct weston_seat *seat,
779 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
780{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400781 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400782 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400783
784 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100785 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400786}
787
Daniel Stone96d47c02013-11-19 11:37:12 +0100788static void
789run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
790{
791 struct weston_compositor *compositor = seat->compositor;
792 uint32_t diff;
793 unsigned int i;
794 struct {
795 uint32_t xkb;
796 enum weston_keyboard_modifier weston;
797 } mods[] = {
798 { seat->xkb_info->ctrl_mod, MODIFIER_CTRL },
799 { seat->xkb_info->alt_mod, MODIFIER_ALT },
800 { seat->xkb_info->super_mod, MODIFIER_SUPER },
801 { seat->xkb_info->shift_mod, MODIFIER_SHIFT },
802 };
803
804 diff = new & ~old;
805 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
806 if (diff & (1 << mods[i].xkb))
807 weston_compositor_run_modifier_binding(compositor,
808 seat,
809 mods[i].weston,
810 WL_KEYBOARD_KEY_STATE_PRESSED);
811 }
812
813 diff = old & ~new;
814 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
815 if (diff & (1 << mods[i].xkb))
816 weston_compositor_run_modifier_binding(compositor,
817 seat,
818 mods[i].weston,
819 WL_KEYBOARD_KEY_STATE_RELEASED);
820 }
821}
822
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400823WL_EXPORT void
824notify_motion_absolute(struct weston_seat *seat,
825 uint32_t time, wl_fixed_t x, wl_fixed_t y)
826{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400827 struct weston_compositor *ec = 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
830 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100831 pointer->grab->interface->motion(pointer->grab, time, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400832}
833
834WL_EXPORT void
835weston_surface_activate(struct weston_surface *surface,
836 struct weston_seat *seat)
837{
838 struct weston_compositor *compositor = seat->compositor;
839
Kristian Høgsberge3148752013-05-06 23:19:49 -0400840 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400841 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400842 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400843 }
844
845 wl_signal_emit(&compositor->activate_signal, surface);
846}
847
848WL_EXPORT void
849notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
850 enum wl_pointer_button_state state)
851{
852 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400853 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400854 struct weston_surface *focus =
855 (struct weston_surface *) pointer->focus;
856 uint32_t serial = wl_display_next_serial(compositor->wl_display);
857
858 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
859 if (compositor->ping_handler && focus)
860 compositor->ping_handler(focus, serial);
861 weston_compositor_idle_inhibit(compositor);
862 if (pointer->button_count == 0) {
863 pointer->grab_button = button;
864 pointer->grab_time = time;
865 pointer->grab_x = pointer->x;
866 pointer->grab_y = pointer->y;
867 }
868 pointer->button_count++;
869 } else {
870 weston_compositor_idle_release(compositor);
871 pointer->button_count--;
872 }
873
874 weston_compositor_run_button_binding(compositor, seat, time, button,
875 state);
876
877 pointer->grab->interface->button(pointer->grab, time, button, state);
878
879 if (pointer->button_count == 1)
880 pointer->grab_serial =
881 wl_display_get_serial(compositor->wl_display);
882}
883
884WL_EXPORT void
885notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
886 wl_fixed_t value)
887{
888 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400889 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400890 struct weston_surface *focus =
891 (struct weston_surface *) pointer->focus;
892 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100893 struct wl_resource *resource;
894 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400895
896 if (compositor->ping_handler && focus)
897 compositor->ping_handler(focus, serial);
898
899 weston_compositor_wake(compositor);
900
901 if (!value)
902 return;
903
904 if (weston_compositor_run_axis_binding(compositor, seat,
905 time, axis, value))
906 return;
907
Neil Roberts96d790e2013-09-19 17:32:00 +0100908 resource_list = &pointer->focus_resource_list;
909 wl_resource_for_each(resource, resource_list)
910 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400911 value);
912}
913
Rob Bradford382ff462013-06-24 16:52:45 +0100914#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400915WL_EXPORT void
916notify_modifiers(struct weston_seat *seat, uint32_t serial)
917{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400918 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400919 struct weston_keyboard_grab *grab = keyboard->grab;
920 uint32_t mods_depressed, mods_latched, mods_locked, group;
921 uint32_t mods_lookup;
922 enum weston_led leds = 0;
923 int changed = 0;
924
925 /* Serialize and update our internal state, checking to see if it's
926 * different to the previous state. */
927 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
928 XKB_STATE_DEPRESSED);
929 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
930 XKB_STATE_LATCHED);
931 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
932 XKB_STATE_LOCKED);
933 group = xkb_state_serialize_group(seat->xkb_state.state,
934 XKB_STATE_EFFECTIVE);
935
Kristian Høgsberge3148752013-05-06 23:19:49 -0400936 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
937 mods_latched != seat->keyboard->modifiers.mods_latched ||
938 mods_locked != seat->keyboard->modifiers.mods_locked ||
939 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400940 changed = 1;
941
Daniel Stone96d47c02013-11-19 11:37:12 +0100942 run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
943 mods_depressed);
944
Kristian Høgsberge3148752013-05-06 23:19:49 -0400945 seat->keyboard->modifiers.mods_depressed = mods_depressed;
946 seat->keyboard->modifiers.mods_latched = mods_latched;
947 seat->keyboard->modifiers.mods_locked = mods_locked;
948 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400949
950 /* And update the modifier_state for bindings. */
951 mods_lookup = mods_depressed | mods_latched;
952 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000953 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400954 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000955 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400956 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000957 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400958 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000959 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400960 seat->modifier_state |= MODIFIER_SHIFT;
961
962 /* Finally, notify the compositor that LEDs have changed. */
963 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000964 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400965 leds |= LED_NUM_LOCK;
966 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000967 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400968 leds |= LED_CAPS_LOCK;
969 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000970 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400971 leds |= LED_SCROLL_LOCK;
972 if (leds != seat->xkb_state.leds && seat->led_update)
973 seat->led_update(seat, leds);
974 seat->xkb_state.leds = leds;
975
976 if (changed) {
977 grab->interface->modifiers(grab,
978 serial,
979 keyboard->modifiers.mods_depressed,
980 keyboard->modifiers.mods_latched,
981 keyboard->modifiers.mods_locked,
982 keyboard->modifiers.group);
983 }
984}
985
986static void
987update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
988 enum wl_keyboard_key_state state)
989{
990 enum xkb_key_direction direction;
991
Matt Roper01a92732013-06-24 16:52:44 +0100992 /* Keyboard modifiers don't exist in raw keyboard mode */
993 if (!seat->compositor->use_xkbcommon)
994 return;
995
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400996 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
997 direction = XKB_KEY_DOWN;
998 else
999 direction = XKB_KEY_UP;
1000
1001 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1002 * broken keycode system, which starts at 8. */
1003 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1004
1005 notify_modifiers(seat, serial);
1006}
Rui Matos65196bc2013-10-10 19:44:19 +02001007
1008static void
1009send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1010{
1011 wl_keyboard_send_keymap(resource,
1012 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1013 xkb_info->keymap_fd,
1014 xkb_info->keymap_size);
1015}
1016
1017static void
1018send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1019{
1020 wl_keyboard_send_modifiers(resource, serial,
1021 keyboard->modifiers.mods_depressed,
1022 keyboard->modifiers.mods_latched,
1023 keyboard->modifiers.mods_locked,
1024 keyboard->modifiers.group);
1025}
1026
1027static struct weston_xkb_info *
1028weston_xkb_info_create(struct xkb_keymap *keymap);
1029static void
1030weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1031
1032static void
1033update_keymap(struct weston_seat *seat)
1034{
1035 struct wl_resource *resource;
1036 struct weston_xkb_info *xkb_info;
1037 struct xkb_state *state;
1038 xkb_mod_mask_t latched_mods;
1039 xkb_mod_mask_t locked_mods;
1040
1041 xkb_info = weston_xkb_info_create(seat->pending_keymap);
1042
1043 xkb_keymap_unref(seat->pending_keymap);
1044 seat->pending_keymap = NULL;
1045
1046 if (!xkb_info) {
1047 weston_log("failed to create XKB info\n");
1048 return;
1049 }
1050
1051 state = xkb_state_new(xkb_info->keymap);
1052 if (!state) {
1053 weston_log("failed to initialise XKB state\n");
1054 weston_xkb_info_destroy(xkb_info);
1055 return;
1056 }
1057
1058 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
1059 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
1060 xkb_state_update_mask(state,
1061 0, /* depressed */
1062 latched_mods,
1063 locked_mods,
1064 0, 0, 0);
1065
1066 weston_xkb_info_destroy(seat->xkb_info);
1067 seat->xkb_info = xkb_info;
1068
1069 xkb_state_unref(seat->xkb_state.state);
1070 seat->xkb_state.state = state;
1071
1072 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1073 send_keymap(resource, xkb_info);
1074 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1075 send_keymap(resource, xkb_info);
1076
1077 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1078
1079 if (!latched_mods && !locked_mods)
1080 return;
1081
1082 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1083 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1084 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1085 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1086}
Rob Bradford382ff462013-06-24 16:52:45 +01001087#else
1088WL_EXPORT void
1089notify_modifiers(struct weston_seat *seat, uint32_t serial)
1090{
1091}
1092
1093static void
1094update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1095 enum wl_keyboard_key_state state)
1096{
1097}
Rui Matos65196bc2013-10-10 19:44:19 +02001098
1099static void
1100update_keymap(struct weston_seat *seat)
1101{
1102}
Rob Bradford382ff462013-06-24 16:52:45 +01001103#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001104
1105WL_EXPORT void
1106notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1107 enum wl_keyboard_key_state state,
1108 enum weston_key_state_update update_state)
1109{
1110 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001111 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001113 struct weston_keyboard_grab *grab = keyboard->grab;
1114 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1115 uint32_t *k, *end;
1116
1117 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1118 if (compositor->ping_handler && focus)
1119 compositor->ping_handler(focus, serial);
1120
1121 weston_compositor_idle_inhibit(compositor);
1122 keyboard->grab_key = key;
1123 keyboard->grab_time = time;
1124 } else {
1125 weston_compositor_idle_release(compositor);
1126 }
1127
1128 end = keyboard->keys.data + keyboard->keys.size;
1129 for (k = keyboard->keys.data; k < end; k++) {
1130 if (*k == key) {
1131 /* Ignore server-generated repeats. */
1132 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1133 return;
1134 *k = *--end;
1135 }
1136 }
1137 keyboard->keys.size = (void *) end - keyboard->keys.data;
1138 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1139 k = wl_array_add(&keyboard->keys, sizeof *k);
1140 *k = key;
1141 }
1142
1143 if (grab == &keyboard->default_grab ||
1144 grab == &keyboard->input_method_grab) {
1145 weston_compositor_run_key_binding(compositor, seat, time, key,
1146 state);
1147 grab = keyboard->grab;
1148 }
1149
1150 grab->interface->key(grab, time, key, state);
1151
Rui Matos65196bc2013-10-10 19:44:19 +02001152 if (seat->pending_keymap &&
1153 keyboard->keys.size == 0)
1154 update_keymap(seat);
1155
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001156 if (update_state == STATE_UPDATE_AUTOMATIC) {
1157 update_modifier_state(seat,
1158 wl_display_get_serial(compositor->wl_display),
1159 key,
1160 state);
1161 }
1162}
1163
1164WL_EXPORT void
1165notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1166 wl_fixed_t x, wl_fixed_t y)
1167{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168 if (output) {
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001169 weston_pointer_move(seat->pointer, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001170 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001171 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001172 * NULL) here, but somehow that breaks re-entry... */
1173 }
1174}
1175
1176static void
1177destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1178{
1179 struct weston_seat *ws;
1180
1181 ws = container_of(listener, struct weston_seat,
1182 saved_kbd_focus_listener);
1183
1184 ws->saved_kbd_focus = NULL;
1185}
1186
1187WL_EXPORT void
1188notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1189 enum weston_key_state_update update_state)
1190{
1191 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001192 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001193 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194 uint32_t *k, serial;
1195
1196 serial = wl_display_next_serial(compositor->wl_display);
1197 wl_array_copy(&keyboard->keys, keys);
1198 wl_array_for_each(k, &keyboard->keys) {
1199 weston_compositor_idle_inhibit(compositor);
1200 if (update_state == STATE_UPDATE_AUTOMATIC)
1201 update_modifier_state(seat, serial, *k,
1202 WL_KEYBOARD_KEY_STATE_PRESSED);
1203 }
1204
1205 /* Run key bindings after we've updated the state. */
1206 wl_array_for_each(k, &keyboard->keys) {
1207 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1208 WL_KEYBOARD_KEY_STATE_PRESSED);
1209 }
1210
1211 surface = seat->saved_kbd_focus;
1212
1213 if (surface) {
1214 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1215 weston_keyboard_set_focus(keyboard, surface);
1216 seat->saved_kbd_focus = NULL;
1217 }
1218}
1219
1220WL_EXPORT void
1221notify_keyboard_focus_out(struct weston_seat *seat)
1222{
1223 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001224 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001225 uint32_t *k, serial;
1226
1227 serial = wl_display_next_serial(compositor->wl_display);
1228 wl_array_for_each(k, &keyboard->keys) {
1229 weston_compositor_idle_release(compositor);
1230 update_modifier_state(seat, serial, *k,
1231 WL_KEYBOARD_KEY_STATE_RELEASED);
1232 }
1233
1234 seat->modifier_state = 0;
1235
1236 if (keyboard->focus) {
1237 seat->saved_kbd_focus = keyboard->focus;
1238 seat->saved_kbd_focus_listener.notify =
1239 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001240 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001241 &seat->saved_kbd_focus_listener);
1242 }
1243
1244 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001245 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001246}
1247
Michael Fua2bb7912013-07-23 15:51:06 +08001248WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001250{
Neil Roberts96d790e2013-09-19 17:32:00 +01001251 struct wl_list *focus_resource_list;
1252
1253 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001254
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001255 if (view && seat->touch->focus &&
1256 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001258 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001260
Neil Roberts96d790e2013-09-19 17:32:00 +01001261 if (!wl_list_empty(focus_resource_list)) {
1262 move_resources(&seat->touch->resource_list,
1263 focus_resource_list);
1264 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001267 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001269 move_resources_for_client(focus_resource_list,
1270 &seat->touch->resource_list,
1271 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001272 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274}
1275
1276/**
1277 * notify_touch - emulates button touches and notifies surfaces accordingly.
1278 *
1279 * It assumes always the correct cycle sequence until it gets here: touch_down
1280 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1281 * for sending along such order.
1282 *
1283 */
1284WL_EXPORT void
1285notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1286 wl_fixed_t x, wl_fixed_t y, int touch_type)
1287{
1288 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001289 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001290 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001292 wl_fixed_t sx, sy;
1293
1294 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001295 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1296 touch->grab_x = x;
1297 touch->grab_y = y;
1298 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001299
1300 switch (touch_type) {
1301 case WL_TOUCH_DOWN:
1302 weston_compositor_idle_inhibit(ec);
1303
1304 seat->num_tp++;
1305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 /* the first finger down picks the view, and all further go
1307 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001308 * until all touch points are up again. */
1309 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1311 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001312 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 ev = touch->focus;
1314 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001315 } else {
1316 /* Unexpected condition: We have non-initial touch but
1317 * there is no focused surface.
1318 */
1319 weston_log("touch event received with %d points down"
1320 "but no surface focused\n", seat->num_tp);
1321 return;
1322 }
1323
1324 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001325 if (seat->num_tp == 1) {
1326 touch->grab_serial =
1327 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001328 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001329 touch->grab_time = time;
1330 touch->grab_x = x;
1331 touch->grab_y = y;
1332 }
1333
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001334 break;
1335 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 ev = touch->focus;
1337 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001338 break;
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001341 grab->interface->motion(grab, time, touch_id, sx, sy);
1342 break;
1343 case WL_TOUCH_UP:
1344 weston_compositor_idle_release(ec);
1345 seat->num_tp--;
1346
1347 grab->interface->up(grab, time, touch_id);
1348 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001349 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001350 break;
1351 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001352
1353 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001354}
1355
1356static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001357pointer_cursor_surface_configure(struct weston_surface *es,
1358 int32_t dx, int32_t dy, int32_t width, int32_t height)
1359{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001360 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001361 int x, y;
1362
1363 if (width == 0)
1364 return;
1365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001367
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001368 pointer->hotspot_x -= dx;
1369 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001370
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001371 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1372 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001373
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001375
1376 empty_region(&es->pending.input);
1377
1378 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 wl_list_insert(&es->compositor->cursor_layer.view_list,
1380 &pointer->sprite->layer_link);
1381 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001382 }
1383}
1384
1385static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001386pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1387 uint32_t serial, struct wl_resource *surface_resource,
1388 int32_t x, int32_t y)
1389{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001390 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001391 struct weston_surface *surface = NULL;
1392
1393 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001394 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001395
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001396 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001397 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001399 black_surface used in shell.c for fullscreen don't have
1400 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001402 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001404 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001405 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001406 return;
1407
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001409 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001410 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001411 WL_DISPLAY_ERROR_INVALID_OBJECT,
1412 "surface->configure already "
1413 "set");
1414 return;
1415 }
1416 }
1417
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001418 if (pointer->sprite)
1419 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001420
1421 if (!surface)
1422 return;
1423
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001424 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001425 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001426
1427 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001428 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001430 pointer->hotspot_x = x;
1431 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001432
1433 if (surface->buffer_ref.buffer)
1434 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1435 weston_surface_buffer_height(surface));
1436}
1437
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001438static void
1439pointer_release(struct wl_client *client, struct wl_resource *resource)
1440{
1441 wl_resource_destroy(resource);
1442}
1443
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001444static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001445 pointer_set_cursor,
1446 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001447};
1448
1449static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001450seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1451 uint32_t id)
1452{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001453 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001454 struct wl_resource *cr;
1455
Kristian Høgsberge3148752013-05-06 23:19:49 -04001456 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001457 return;
1458
Jason Ekstranda85118c2013-06-27 20:17:02 -05001459 cr = wl_resource_create(client, &wl_pointer_interface,
1460 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001461 if (cr == NULL) {
1462 wl_client_post_no_memory(client);
1463 return;
1464 }
1465
Neil Roberts96d790e2013-09-19 17:32:00 +01001466 /* May be moved to focused list later by either
1467 * weston_pointer_set_focus or directly if this client is already
1468 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001469 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001470 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1471 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001472
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1474 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001475 wl_fixed_t sx, sy;
1476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 weston_view_from_global_fixed(seat->pointer->focus,
1478 seat->pointer->x,
1479 seat->pointer->y,
1480 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001481
1482 wl_list_remove(wl_resource_get_link(cr));
1483 wl_list_insert(&seat->pointer->focus_resource_list,
1484 wl_resource_get_link(cr));
1485 wl_pointer_send_enter(cr,
1486 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001488 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001489 }
1490}
1491
1492static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001493keyboard_release(struct wl_client *client, struct wl_resource *resource)
1494{
1495 wl_resource_destroy(resource);
1496}
1497
1498static const struct wl_keyboard_interface keyboard_interface = {
1499 keyboard_release
1500};
1501
Neil Roberts96d790e2013-09-19 17:32:00 +01001502static int
1503should_send_modifiers_to_client(struct weston_seat *seat,
1504 struct wl_client *client)
1505{
1506 if (seat->keyboard &&
1507 seat->keyboard->focus &&
Jason Ekstrand42133d42013-11-14 20:06:16 -06001508 seat->keyboard->focus->resource &&
Neil Roberts96d790e2013-09-19 17:32:00 +01001509 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1510 return 1;
1511
1512 if (seat->pointer &&
1513 seat->pointer->focus &&
Jason Ekstrand42133d42013-11-14 20:06:16 -06001514 seat->pointer->focus->surface->resource &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001516 return 1;
1517
1518 return 0;
1519}
1520
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001521static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001522seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1523 uint32_t id)
1524{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001525 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001526 struct wl_resource *cr;
1527
Kristian Høgsberge3148752013-05-06 23:19:49 -04001528 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001529 return;
1530
Jason Ekstranda85118c2013-06-27 20:17:02 -05001531 cr = wl_resource_create(client, &wl_keyboard_interface,
1532 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001533 if (cr == NULL) {
1534 wl_client_post_no_memory(client);
1535 return;
1536 }
1537
Neil Roberts96d790e2013-09-19 17:32:00 +01001538 /* May be moved to focused list later by either
1539 * weston_keyboard_set_focus or directly if this client is already
1540 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001541 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001542 wl_resource_set_implementation(cr, &keyboard_interface,
1543 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001544
Matt Roper01a92732013-06-24 16:52:44 +01001545 if (seat->compositor->use_xkbcommon) {
1546 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001547 seat->xkb_info->keymap_fd,
1548 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001549 } else {
1550 int null_fd = open("/dev/null", O_RDONLY);
1551 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1552 null_fd,
1553 0);
1554 close(null_fd);
1555 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001556
Neil Roberts96d790e2013-09-19 17:32:00 +01001557 if (should_send_modifiers_to_client(seat, client)) {
1558 send_modifiers_to_resource(seat->keyboard,
1559 cr,
1560 seat->keyboard->focus_serial);
1561 }
1562
Kristian Høgsberge3148752013-05-06 23:19:49 -04001563 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001564 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001565 struct weston_surface *surface =
1566 (struct weston_surface *) seat->keyboard->focus;
1567
1568 wl_list_remove(wl_resource_get_link(cr));
1569 wl_list_insert(&seat->keyboard->focus_resource_list,
1570 wl_resource_get_link(cr));
1571 wl_keyboard_send_enter(cr,
1572 seat->keyboard->focus_serial,
1573 surface->resource,
1574 &seat->keyboard->keys);
1575
1576 /* If this is the first keyboard resource for this
1577 * client... */
1578 if (seat->keyboard->focus_resource_list.prev ==
1579 wl_resource_get_link(cr))
1580 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581 }
1582}
1583
1584static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001585touch_release(struct wl_client *client, struct wl_resource *resource)
1586{
1587 wl_resource_destroy(resource);
1588}
1589
1590static const struct wl_touch_interface touch_interface = {
1591 touch_release
1592};
1593
1594static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001595seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1596 uint32_t id)
1597{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001598 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001599 struct wl_resource *cr;
1600
Kristian Høgsberge3148752013-05-06 23:19:49 -04001601 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001602 return;
1603
Jason Ekstranda85118c2013-06-27 20:17:02 -05001604 cr = wl_resource_create(client, &wl_touch_interface,
1605 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001606 if (cr == NULL) {
1607 wl_client_post_no_memory(client);
1608 return;
1609 }
1610
Neil Roberts96d790e2013-09-19 17:32:00 +01001611 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001613 wl_list_insert(&seat->touch->resource_list,
1614 wl_resource_get_link(cr));
1615 } else {
1616 wl_list_insert(&seat->touch->focus_resource_list,
1617 wl_resource_get_link(cr));
1618 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001619 wl_resource_set_implementation(cr, &touch_interface,
1620 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001621}
1622
1623static const struct wl_seat_interface seat_interface = {
1624 seat_get_pointer,
1625 seat_get_keyboard,
1626 seat_get_touch,
1627};
1628
1629static void
1630bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1631{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001632 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001633 struct wl_resource *resource;
1634 enum wl_seat_capability caps = 0;
1635
Jason Ekstranda85118c2013-06-27 20:17:02 -05001636 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001637 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001638 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001639 wl_resource_set_implementation(resource, &seat_interface, data,
1640 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001641
1642 if (seat->pointer)
1643 caps |= WL_SEAT_CAPABILITY_POINTER;
1644 if (seat->keyboard)
1645 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1646 if (seat->touch)
1647 caps |= WL_SEAT_CAPABILITY_TOUCH;
1648
1649 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001650 if (version >= 2)
1651 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001652}
1653
Rob Bradford382ff462013-06-24 16:52:45 +01001654#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001655int
1656weston_compositor_xkb_init(struct weston_compositor *ec,
1657 struct xkb_rule_names *names)
1658{
Rob Bradford382ff462013-06-24 16:52:45 +01001659 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001660
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001661 if (ec->xkb_context == NULL) {
1662 ec->xkb_context = xkb_context_new(0);
1663 if (ec->xkb_context == NULL) {
1664 weston_log("failed to create XKB context\n");
1665 return -1;
1666 }
1667 }
1668
1669 if (names)
1670 ec->xkb_names = *names;
1671 if (!ec->xkb_names.rules)
1672 ec->xkb_names.rules = strdup("evdev");
1673 if (!ec->xkb_names.model)
1674 ec->xkb_names.model = strdup("pc105");
1675 if (!ec->xkb_names.layout)
1676 ec->xkb_names.layout = strdup("us");
1677
1678 return 0;
1679}
1680
Stefan Schmidtfda26522013-09-17 10:54:09 +01001681static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001682weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001683{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001684 if (--xkb_info->ref_count > 0)
1685 return;
1686
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001687 if (xkb_info->keymap)
1688 xkb_map_unref(xkb_info->keymap);
1689
1690 if (xkb_info->keymap_area)
1691 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1692 if (xkb_info->keymap_fd >= 0)
1693 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001694 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001695}
1696
1697void
1698weston_compositor_xkb_destroy(struct weston_compositor *ec)
1699{
Matt Roper01a92732013-06-24 16:52:44 +01001700 /*
1701 * If we're operating in raw keyboard mode, we never initialized
1702 * libxkbcommon so there's no cleanup to do either.
1703 */
1704 if (!ec->use_xkbcommon)
1705 return;
1706
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001707 free((char *) ec->xkb_names.rules);
1708 free((char *) ec->xkb_names.model);
1709 free((char *) ec->xkb_names.layout);
1710 free((char *) ec->xkb_names.variant);
1711 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001712
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001713 if (ec->xkb_info)
1714 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001715 xkb_context_unref(ec->xkb_context);
1716}
1717
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001718static struct weston_xkb_info *
1719weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001721 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1722 if (xkb_info == NULL)
1723 return NULL;
1724
1725 xkb_info->keymap = xkb_map_ref(keymap);
1726 xkb_info->ref_count = 1;
1727
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001728 char *keymap_str;
1729
1730 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1731 XKB_MOD_NAME_SHIFT);
1732 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1733 XKB_MOD_NAME_CAPS);
1734 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1735 XKB_MOD_NAME_CTRL);
1736 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1737 XKB_MOD_NAME_ALT);
1738 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1739 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1740 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1741 XKB_MOD_NAME_LOGO);
1742 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1743
1744 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1745 XKB_LED_NAME_NUM);
1746 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1747 XKB_LED_NAME_CAPS);
1748 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1749 XKB_LED_NAME_SCROLL);
1750
1751 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1752 if (keymap_str == NULL) {
1753 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001754 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001755 }
1756 xkb_info->keymap_size = strlen(keymap_str) + 1;
1757
1758 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1759 if (xkb_info->keymap_fd < 0) {
1760 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1761 (unsigned long) xkb_info->keymap_size);
1762 goto err_keymap_str;
1763 }
1764
1765 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1766 PROT_READ | PROT_WRITE,
1767 MAP_SHARED, xkb_info->keymap_fd, 0);
1768 if (xkb_info->keymap_area == MAP_FAILED) {
1769 weston_log("failed to mmap() %lu bytes\n",
1770 (unsigned long) xkb_info->keymap_size);
1771 goto err_dev_zero;
1772 }
1773 strcpy(xkb_info->keymap_area, keymap_str);
1774 free(keymap_str);
1775
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001776 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001777
1778err_dev_zero:
1779 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001780err_keymap_str:
1781 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001782err_keymap:
1783 xkb_map_unref(xkb_info->keymap);
1784 free(xkb_info);
1785 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001786}
1787
1788static int
1789weston_compositor_build_global_keymap(struct weston_compositor *ec)
1790{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001791 struct xkb_keymap *keymap;
1792
1793 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001794 return 0;
1795
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001796 keymap = xkb_map_new_from_names(ec->xkb_context,
1797 &ec->xkb_names,
1798 0);
1799 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001800 weston_log("failed to compile global XKB keymap\n");
1801 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1802 "options %s\n",
1803 ec->xkb_names.rules, ec->xkb_names.model,
1804 ec->xkb_names.layout, ec->xkb_names.variant,
1805 ec->xkb_names.options);
1806 return -1;
1807 }
1808
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001809 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001810 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001811 return -1;
1812
1813 return 0;
1814}
Rob Bradford382ff462013-06-24 16:52:45 +01001815#else
1816int
1817weston_compositor_xkb_init(struct weston_compositor *ec,
1818 struct xkb_rule_names *names)
1819{
1820 return 0;
1821}
1822
1823void
1824weston_compositor_xkb_destroy(struct weston_compositor *ec)
1825{
1826}
1827#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828
Rui Matos65196bc2013-10-10 19:44:19 +02001829WL_EXPORT void
1830weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1831{
1832 if (!seat->keyboard || !keymap)
1833 return;
1834
1835#ifdef ENABLE_XKBCOMMON
1836 if (!seat->compositor->use_xkbcommon)
1837 return;
1838
1839 xkb_keymap_unref(seat->pending_keymap);
1840 seat->pending_keymap = xkb_keymap_ref(keymap);
1841
1842 if (seat->keyboard->keys.size == 0)
1843 update_keymap(seat);
1844#endif
1845}
1846
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001847WL_EXPORT int
1848weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1849{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001850 struct weston_keyboard *keyboard;
1851
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001852 if (seat->keyboard) {
1853 seat->keyboard_device_count += 1;
1854 if (seat->keyboard_device_count == 1)
1855 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001856 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001857 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001858
Rob Bradford382ff462013-06-24 16:52:45 +01001859#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001860 if (seat->compositor->use_xkbcommon) {
1861 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001862 seat->xkb_info = weston_xkb_info_create(keymap);
1863 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001864 return -1;
1865 } else {
1866 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1867 return -1;
1868 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001869 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001870 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001871
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001872 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001873 if (seat->xkb_state.state == NULL) {
1874 weston_log("failed to initialise XKB state\n");
1875 return -1;
1876 }
1877
1878 seat->xkb_state.leds = 0;
1879 }
Rob Bradford382ff462013-06-24 16:52:45 +01001880#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001882 keyboard = weston_keyboard_create();
1883 if (keyboard == NULL) {
1884 weston_log("failed to allocate weston keyboard struct\n");
1885 return -1;
1886 }
1887
1888 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001889 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001890 keyboard->seat = seat;
1891
1892 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001894 return 0;
1895}
1896
1897WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001898weston_seat_release_keyboard(struct weston_seat *seat)
1899{
1900 seat->keyboard_device_count--;
1901 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001902 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001903 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001904 seat_send_updated_caps(seat);
1905 }
1906}
1907
1908WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001909weston_seat_init_pointer(struct weston_seat *seat)
1910{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001911 struct weston_pointer *pointer;
1912
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001913 if (seat->pointer) {
1914 seat->pointer_device_count += 1;
1915 if (seat->pointer_device_count == 1)
1916 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001917 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001918 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001919
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001920 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001921 if (pointer == NULL)
1922 return;
1923
1924 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001925 seat->pointer_device_count = 1;
1926 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001927
1928 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001929}
1930
1931WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001932weston_seat_release_pointer(struct weston_seat *seat)
1933{
1934 struct weston_pointer *pointer = seat->pointer;
1935
1936 seat->pointer_device_count--;
1937 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001938 weston_pointer_set_focus(pointer, NULL,
1939 wl_fixed_from_int(0),
1940 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001941 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001942
Jonas Ådahla4932742013-10-17 23:04:07 +02001943 if (pointer->sprite)
1944 pointer_unmap_sprite(pointer);
1945
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001946 seat_send_updated_caps(seat);
1947 }
1948}
1949
1950WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001951weston_seat_init_touch(struct weston_seat *seat)
1952{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001953 struct weston_touch *touch;
1954
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001955 if (seat->touch) {
1956 seat->touch_device_count += 1;
1957 if (seat->touch_device_count == 1)
1958 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001959 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001960 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001961
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001962 touch = weston_touch_create();
1963 if (touch == NULL)
1964 return;
1965
1966 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001967 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001968 touch->seat = seat;
1969
1970 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001971}
1972
1973WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001974weston_seat_release_touch(struct weston_seat *seat)
1975{
1976 seat->touch_device_count--;
1977 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001978 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001979 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001980 seat_send_updated_caps(seat);
1981 }
1982}
1983
1984WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001985weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1986 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001987{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001988 memset(seat, 0, sizeof *seat);
1989
Kristian Høgsberge3148752013-05-06 23:19:49 -04001990 seat->selection_data_source = NULL;
1991 wl_list_init(&seat->base_resource_list);
1992 wl_signal_init(&seat->selection_signal);
1993 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001994 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001995
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001996 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001997 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001998
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001999 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002000 seat->modifier_state = 0;
2001 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002002 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002003
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002004 wl_list_insert(ec->seat_list.prev, &seat->link);
2005
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002006 clipboard_create(seat);
2007
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002008 wl_signal_emit(&ec->seat_created_signal, seat);
2009}
2010
2011WL_EXPORT void
2012weston_seat_release(struct weston_seat *seat)
2013{
2014 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002015
Rob Bradford382ff462013-06-24 16:52:45 +01002016#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01002017 if (seat->compositor->use_xkbcommon) {
2018 if (seat->xkb_state.state != NULL)
2019 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002020 if (seat->xkb_info)
2021 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02002022 if (seat->pending_keymap)
2023 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01002024 }
Rob Bradford382ff462013-06-24 16:52:45 +01002025#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002026
Kristian Høgsberge3148752013-05-06 23:19:49 -04002027 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002028 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002029 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002030 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002031 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002032 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04002033
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002034 free (seat->seat_name);
2035
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002036 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04002037
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 wl_signal_emit(&seat->destroy_signal, seat);
2039}