blob: fc93f884d0d4f95e2d2328b4e63b2ce7e9560711 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010023#include "config.h"
24
Kristian Høgsberg2158a882013-04-18 15:07:39 -040025#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040028#include <sys/mman.h>
29#include <assert.h>
30#include <unistd.h>
Matt Roper01a92732013-06-24 16:52:44 +010031#include <fcntl.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040032
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040033#include "../shared/os-compatibility.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040034#include "compositor.h"
35
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040036static void
37empty_region(pixman_region32_t *region)
38{
39 pixman_region32_fini(region);
40 pixman_region32_init(region);
41}
42
43static void unbind_resource(struct wl_resource *resource)
44{
Jason Ekstrand44a38632013-06-14 10:08:00 -050045 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040046}
47
Jonas Ådahl3042ffe2013-10-17 23:04:08 +020048WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -040049weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040050{
Kristian Høgsbergda751b82013-07-04 00:58:07 -040051 const struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040052
Kristian Høgsbergda751b82013-07-04 00:58:07 -040053 if (pointer == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040054 return;
55
Kristian Høgsbergda751b82013-07-04 00:58:07 -040056 pointer->grab->interface->focus(seat->pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040057}
58
59static void
60weston_compositor_idle_inhibit(struct weston_compositor *compositor)
61{
62 weston_compositor_wake(compositor);
63 compositor->idle_inhibit++;
64}
65
66static void
67weston_compositor_idle_release(struct weston_compositor *compositor)
68{
69 compositor->idle_inhibit--;
70 weston_compositor_wake(compositor);
71}
72
Kristian Høgsberg2158a882013-04-18 15:07:39 -040073static void
Neil Roberts96d790e2013-09-19 17:32:00 +010074move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040075{
Neil Roberts96d790e2013-09-19 17:32:00 +010076 wl_list_insert_list(destination, source);
77 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078}
79
80static void
Neil Roberts96d790e2013-09-19 17:32:00 +010081move_resources_for_client(struct wl_list *destination,
82 struct wl_list *source,
83 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040084{
Neil Roberts96d790e2013-09-19 17:32:00 +010085 struct wl_resource *resource, *tmp;
86 wl_resource_for_each_safe(resource, tmp, source) {
87 if (wl_resource_get_client(resource) == client) {
88 wl_list_remove(wl_resource_get_link(resource));
89 wl_list_insert(destination,
90 wl_resource_get_link(resource));
91 }
92 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -040093}
94
95static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -070096default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -040097{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -040098 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -050099 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400100 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400101
102 if (pointer->button_count > 0)
103 return;
104
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 view = weston_compositor_pick_view(pointer->seat->compositor,
106 pointer->x, pointer->y,
107 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400108
Jason Ekstranda7af7042013-10-12 22:38:11 -0500109 if (pointer->focus != view)
110 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400111}
112
113static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100114default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
115 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400116{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400117 struct weston_pointer *pointer = grab->pointer;
118 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100119 struct wl_list *resource_list;
120 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400121
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100122 weston_pointer_move(pointer, x, y);
123
Neil Roberts96d790e2013-09-19 17:32:00 +0100124 resource_list = &pointer->focus_resource_list;
125 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500126 weston_view_from_global_fixed(pointer->focus,
127 pointer->x, pointer->y,
128 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +0100129 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400130 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400131}
132
133static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700134default_grab_pointer_button(struct weston_pointer_grab *grab,
135 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400136{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400137 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400138 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500139 struct weston_view *view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400140 struct wl_resource *resource;
141 uint32_t serial;
142 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100143 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400144 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100145 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146
Neil Roberts96d790e2013-09-19 17:32:00 +0100147 resource_list = &pointer->focus_resource_list;
148 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400149 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100150 wl_resource_for_each(resource, resource_list)
151 wl_pointer_send_button(resource,
152 serial,
153 time,
154 button,
155 state_w);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400156 }
157
158 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400159 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500160 view = weston_compositor_pick_view(compositor,
161 pointer->x, pointer->y,
162 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400163
Jason Ekstranda7af7042013-10-12 22:38:11 -0500164 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400165 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400166}
167
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200168static void
169default_grab_pointer_cancel(struct weston_pointer_grab *grab)
170{
171}
172
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400173static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400174 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700175 default_grab_pointer_focus,
176 default_grab_pointer_motion,
177 default_grab_pointer_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200178 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400179};
180
Kristian Høgsberge329f362013-05-06 22:19:57 -0400181static void
182default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
183 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400184{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400185 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100186 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400187 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100188 struct wl_resource *resource;
189 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400190
Neil Roberts96d790e2013-09-19 17:32:00 +0100191 resource_list = &touch->focus_resource_list;
192
193 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400194 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100195 wl_resource_for_each(resource, resource_list)
196 wl_touch_send_down(resource, serial, time,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500197 touch->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100198 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400199 }
200}
201
Kristian Høgsberge329f362013-05-06 22:19:57 -0400202static void
203default_grab_touch_up(struct weston_touch_grab *grab,
204 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400206 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100207 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100209 struct wl_resource *resource;
210 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400211
Neil Roberts96d790e2013-09-19 17:32:00 +0100212 resource_list = &touch->focus_resource_list;
213
214 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100216 wl_resource_for_each(resource, resource_list)
217 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400218 }
219}
220
Kristian Høgsberge329f362013-05-06 22:19:57 -0400221static void
222default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
223 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400224{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400225 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100226 struct wl_resource *resource;
227 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400228
Neil Roberts96d790e2013-09-19 17:32:00 +0100229 resource_list = &touch->focus_resource_list;
230
231 wl_resource_for_each(resource, resource_list) {
232 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400233 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400234 }
235}
236
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200237static void
238default_grab_touch_cancel(struct weston_touch_grab *grab)
239{
240}
241
Kristian Høgsberge329f362013-05-06 22:19:57 -0400242static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243 default_grab_touch_down,
244 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200245 default_grab_touch_motion,
246 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400247};
248
249static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700250default_grab_keyboard_key(struct weston_keyboard_grab *grab,
251 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400253 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400254 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100255 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400256 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100257 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258
Neil Roberts96d790e2013-09-19 17:32:00 +0100259 resource_list = &keyboard->focus_resource_list;
260 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400261 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100262 wl_resource_for_each(resource, resource_list)
263 wl_keyboard_send_key(resource,
264 serial,
265 time,
266 key,
267 state);
268 }
269}
270
271static void
272send_modifiers_to_resource(struct weston_keyboard *keyboard,
273 struct wl_resource *resource,
274 uint32_t serial)
275{
276 wl_keyboard_send_modifiers(resource,
277 serial,
278 keyboard->modifiers.mods_depressed,
279 keyboard->modifiers.mods_latched,
280 keyboard->modifiers.mods_locked,
281 keyboard->modifiers.group);
282}
283
284static void
285send_modifiers_to_client_in_list(struct wl_client *client,
286 struct wl_list *list,
287 uint32_t serial,
288 struct weston_keyboard *keyboard)
289{
290 struct wl_resource *resource;
291
292 wl_resource_for_each(resource, list) {
293 if (wl_resource_get_client(resource) == client)
294 send_modifiers_to_resource(keyboard,
295 resource,
296 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400297 }
298}
299
300static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400301find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400302{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400303 if (!surface)
304 return NULL;
305
Jason Ekstrand44a38632013-06-14 10:08:00 -0500306 if (!surface->resource)
307 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100308
Jason Ekstrand44a38632013-06-14 10:08:00 -0500309 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400310}
311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312static struct wl_resource *
313find_resource_for_view(struct wl_list *list, struct weston_view *view)
314{
315 if (!view)
316 return NULL;
317
318 return find_resource_for_surface(list, view->surface);
319}
320
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400321static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700322default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
323 uint32_t serial, uint32_t mods_depressed,
324 uint32_t mods_latched,
325 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400327 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100328 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
329 struct wl_resource *resource;
330 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331
Neil Roberts96d790e2013-09-19 17:32:00 +0100332 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333
Neil Roberts96d790e2013-09-19 17:32:00 +0100334 wl_resource_for_each(resource, resource_list) {
335 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
336 mods_latched, mods_locked, group);
337 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 if (pointer && pointer->focus && pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100339 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100341 send_modifiers_to_client_in_list(pointer_client,
342 &keyboard->resource_list,
343 serial,
344 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400345 }
346}
347
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200348static void
349default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
350{
351}
352
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400353static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400354 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700355 default_grab_keyboard_key,
356 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200357 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400358};
359
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400360static void
361pointer_unmap_sprite(struct weston_pointer *pointer)
362{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 if (weston_surface_is_mapped(pointer->sprite->surface))
364 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400365
366 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367 pointer->sprite->surface->configure = NULL;
368 pointer->sprite->surface->configure_private = NULL;
369 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400370 pointer->sprite = NULL;
371}
372
373static void
374pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
375{
376 struct weston_pointer *pointer =
377 container_of(listener, struct weston_pointer,
378 sprite_destroy_listener);
379
380 pointer->sprite = NULL;
381}
382
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400383WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100384weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400385{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400386 struct weston_pointer *pointer;
387
Peter Huttererf3d62272013-08-08 11:57:05 +1000388 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400389 if (pointer == NULL)
390 return NULL;
391
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100393 wl_list_init(&pointer->focus_resource_list);
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100394 weston_pointer_set_default_grab(pointer,
395 seat->compositor->default_pointer_grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396 pointer->default_grab.pointer = pointer;
397 pointer->grab = &pointer->default_grab;
398 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100399 wl_signal_init(&pointer->motion_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400400
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400401 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
402
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403 /* FIXME: Pick better co-ords. */
404 pointer->x = wl_fixed_from_int(100);
405 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400406
407 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408}
409
410WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400411weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400412{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400413 if (pointer->sprite)
414 pointer_unmap_sprite(pointer);
415
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400416 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100417
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400418 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400419}
420
Giulio Camuffocdb4d292013-11-14 23:42:53 +0100421void
422weston_pointer_set_default_grab(struct weston_pointer *pointer,
423 const struct weston_pointer_grab_interface *interface)
424{
425 if (interface)
426 pointer->default_grab.interface = interface;
427 else
428 pointer->default_grab.interface =
429 &default_pointer_grab_interface;
430}
431
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400432WL_EXPORT struct weston_keyboard *
433weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400435 struct weston_keyboard *keyboard;
436
Peter Huttererf3d62272013-08-08 11:57:05 +1000437 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400438 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100439 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400440
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400441 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100442 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444 keyboard->default_grab.interface = &default_keyboard_grab_interface;
445 keyboard->default_grab.keyboard = keyboard;
446 keyboard->grab = &keyboard->default_grab;
447 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400448
449 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400450}
451
452WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400453weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454{
455 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100456
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400458 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400459}
460
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400461WL_EXPORT struct weston_touch *
462weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400463{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400464 struct weston_touch *touch;
465
Peter Huttererf3d62272013-08-08 11:57:05 +1000466 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400467 if (touch == NULL)
468 return NULL;
469
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100471 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472 touch->default_grab.interface = &default_touch_grab_interface;
473 touch->default_grab.touch = touch;
474 touch->grab = &touch->default_grab;
475 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400476
477 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478}
479
480WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400481weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482{
483 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100484
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400485 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486}
487
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400489seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400490{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100492 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200494 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200496 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400497 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200498 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400499 caps |= WL_SEAT_CAPABILITY_TOUCH;
500
Rob Bradford6e737f52013-09-06 17:48:19 +0100501 wl_resource_for_each(resource, &seat->base_resource_list) {
502 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500503 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400504}
505
506WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400507weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400509 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400511 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100512 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100513 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400514 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100515 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516 int different_surface = 0;
517
518 if ((!pointer->focus && view) ||
519 (pointer->focus && !view) ||
520 (pointer->focus && pointer->focus->surface != view->surface))
521 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522
Neil Roberts96d790e2013-09-19 17:32:00 +0100523 focus_resource_list = &pointer->focus_resource_list;
524
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400526 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100527 wl_resource_for_each(resource, focus_resource_list) {
528 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500529 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100530 }
531
532 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533 }
534
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535 if (find_resource_for_view(&pointer->resource_list, view) &&
536 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100537 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100539
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100541
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700543 send_modifiers_to_client_in_list(surface_client,
544 &kbd->resource_list,
545 serial,
546 kbd);
547
Neil Roberts96d790e2013-09-19 17:32:00 +0100548 move_resources_for_client(focus_resource_list,
549 &pointer->resource_list,
550 surface_client);
551
552 wl_resource_for_each(resource, focus_resource_list) {
553 wl_pointer_send_enter(resource,
554 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100556 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400557 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100558
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400559 pointer->focus_serial = serial;
560 }
561
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400563 wl_signal_emit(&pointer->focus_signal, pointer);
564}
565
Neil Roberts96d790e2013-09-19 17:32:00 +0100566static void
567send_enter_to_resource_list(struct wl_list *list,
568 struct weston_keyboard *keyboard,
569 struct weston_surface *surface,
570 uint32_t serial)
571{
572 struct wl_resource *resource;
573
574 wl_resource_for_each(resource, list) {
575 send_modifiers_to_resource(keyboard, resource, serial);
576 wl_keyboard_send_enter(resource, serial,
577 surface->resource,
578 &keyboard->keys);
579 }
580}
581
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400583weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400584 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585{
586 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100587 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100589 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400590
Neil Roberts96d790e2013-09-19 17:32:00 +0100591 focus_resource_list = &keyboard->focus_resource_list;
592
593 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400594 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100595 wl_resource_for_each(resource, focus_resource_list) {
596 wl_keyboard_send_leave(resource, serial,
597 keyboard->focus->resource);
598 }
599 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600 }
601
Neil Roberts96d790e2013-09-19 17:32:00 +0100602 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
603 keyboard->focus != surface) {
604 struct wl_client *surface_client =
605 wl_resource_get_client(surface->resource);
606
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100608
609 move_resources_for_client(focus_resource_list,
610 &keyboard->resource_list,
611 surface_client);
612 send_enter_to_resource_list(focus_resource_list,
613 keyboard,
614 surface,
615 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616 keyboard->focus_serial = serial;
617 }
618
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619 keyboard->focus = surface;
620 wl_signal_emit(&keyboard->focus_signal, keyboard);
621}
622
623WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400624weston_keyboard_start_grab(struct weston_keyboard *keyboard,
625 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400626{
627 keyboard->grab = grab;
628 grab->keyboard = keyboard;
629
630 /* XXX focus? */
631}
632
633WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400634weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635{
636 keyboard->grab = &keyboard->default_grab;
637}
638
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200639static void
640weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
641{
642 keyboard->grab->interface->cancel(keyboard->grab);
643}
644
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400646weston_pointer_start_grab(struct weston_pointer *pointer,
647 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400648{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400649 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400650 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400651 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652}
653
654WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400655weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400656{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400657 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400658 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400659}
660
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200661static void
662weston_pointer_cancel_grab(struct weston_pointer *pointer)
663{
664 pointer->grab->interface->cancel(pointer->grab);
665}
666
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400667WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400668weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400669{
670 touch->grab = grab;
671 grab->touch = touch;
672}
673
674WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400675weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400676{
677 touch->grab = &touch->default_grab;
678}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400679
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200680static void
681weston_touch_cancel_grab(struct weston_touch *touch)
682{
683 touch->grab->interface->cancel(touch->grab);
684}
685
Rob Bradford806d8c02013-06-25 18:56:41 +0100686WL_EXPORT void
687weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400688{
Rob Bradford806d8c02013-06-25 18:56:41 +0100689 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400690 struct weston_output *output, *prev = NULL;
691 int x, y, old_x, old_y, valid = 0;
692
693 x = wl_fixed_to_int(*fx);
694 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100695 old_x = wl_fixed_to_int(pointer->x);
696 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400697
698 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100699 if (pointer->seat->output && pointer->seat->output != output)
700 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400701 if (pixman_region32_contains_point(&output->region,
702 x, y, NULL))
703 valid = 1;
704 if (pixman_region32_contains_point(&output->region,
705 old_x, old_y, NULL))
706 prev = output;
707 }
708
Rob Bradford66bd9f52013-06-25 18:56:42 +0100709 if (!prev)
710 prev = pointer->seat->output;
711
712 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400713 if (x < prev->x)
714 *fx = wl_fixed_from_int(prev->x);
715 else if (x >= prev->x + prev->width)
716 *fx = wl_fixed_from_int(prev->x +
717 prev->width - 1);
718 if (y < prev->y)
719 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200720 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400721 *fy = wl_fixed_from_int(prev->y +
722 prev->height - 1);
723 }
724}
725
726/* Takes absolute values */
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100727WL_EXPORT void
728weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400729{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400730 int32_t ix, iy;
731
Rob Bradford806d8c02013-06-25 18:56:41 +0100732 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400733
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400734 pointer->x = x;
735 pointer->y = y;
736
737 ix = wl_fixed_to_int(x);
738 iy = wl_fixed_to_int(y);
739
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400740 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500741 weston_view_set_position(pointer->sprite,
742 ix - pointer->hotspot_x,
743 iy - pointer->hotspot_y);
744 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400745 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100746
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100747 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100748 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400749}
750
751WL_EXPORT void
752notify_motion(struct weston_seat *seat,
753 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
754{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400755 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400756 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400757
758 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100759 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400760}
761
Daniel Stone96d47c02013-11-19 11:37:12 +0100762static void
763run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
764{
765 struct weston_compositor *compositor = seat->compositor;
766 uint32_t diff;
767 unsigned int i;
768 struct {
769 uint32_t xkb;
770 enum weston_keyboard_modifier weston;
771 } mods[] = {
772 { seat->xkb_info->ctrl_mod, MODIFIER_CTRL },
773 { seat->xkb_info->alt_mod, MODIFIER_ALT },
774 { seat->xkb_info->super_mod, MODIFIER_SUPER },
775 { seat->xkb_info->shift_mod, MODIFIER_SHIFT },
776 };
777
778 diff = new & ~old;
779 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
780 if (diff & (1 << mods[i].xkb))
781 weston_compositor_run_modifier_binding(compositor,
782 seat,
783 mods[i].weston,
784 WL_KEYBOARD_KEY_STATE_PRESSED);
785 }
786
787 diff = old & ~new;
788 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
789 if (diff & (1 << mods[i].xkb))
790 weston_compositor_run_modifier_binding(compositor,
791 seat,
792 mods[i].weston,
793 WL_KEYBOARD_KEY_STATE_RELEASED);
794 }
795}
796
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400797WL_EXPORT void
798notify_motion_absolute(struct weston_seat *seat,
799 uint32_t time, wl_fixed_t x, wl_fixed_t y)
800{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400801 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400802 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400803
804 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100805 pointer->grab->interface->motion(pointer->grab, time, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400806}
807
808WL_EXPORT void
809weston_surface_activate(struct weston_surface *surface,
810 struct weston_seat *seat)
811{
812 struct weston_compositor *compositor = seat->compositor;
813
Kristian Høgsberge3148752013-05-06 23:19:49 -0400814 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400815 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400816 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400817 }
818
819 wl_signal_emit(&compositor->activate_signal, surface);
820}
821
822WL_EXPORT void
823notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
824 enum wl_pointer_button_state state)
825{
826 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400827 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400828 struct weston_surface *focus =
829 (struct weston_surface *) pointer->focus;
830 uint32_t serial = wl_display_next_serial(compositor->wl_display);
831
832 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
833 if (compositor->ping_handler && focus)
834 compositor->ping_handler(focus, serial);
835 weston_compositor_idle_inhibit(compositor);
836 if (pointer->button_count == 0) {
837 pointer->grab_button = button;
838 pointer->grab_time = time;
839 pointer->grab_x = pointer->x;
840 pointer->grab_y = pointer->y;
841 }
842 pointer->button_count++;
843 } else {
844 weston_compositor_idle_release(compositor);
845 pointer->button_count--;
846 }
847
848 weston_compositor_run_button_binding(compositor, seat, time, button,
849 state);
850
851 pointer->grab->interface->button(pointer->grab, time, button, state);
852
853 if (pointer->button_count == 1)
854 pointer->grab_serial =
855 wl_display_get_serial(compositor->wl_display);
856}
857
858WL_EXPORT void
859notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
860 wl_fixed_t value)
861{
862 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400863 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400864 struct weston_surface *focus =
865 (struct weston_surface *) pointer->focus;
866 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100867 struct wl_resource *resource;
868 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400869
870 if (compositor->ping_handler && focus)
871 compositor->ping_handler(focus, serial);
872
873 weston_compositor_wake(compositor);
874
875 if (!value)
876 return;
877
878 if (weston_compositor_run_axis_binding(compositor, seat,
879 time, axis, value))
880 return;
881
Neil Roberts96d790e2013-09-19 17:32:00 +0100882 resource_list = &pointer->focus_resource_list;
883 wl_resource_for_each(resource, resource_list)
884 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400885 value);
886}
887
Rob Bradford382ff462013-06-24 16:52:45 +0100888#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400889WL_EXPORT void
890notify_modifiers(struct weston_seat *seat, uint32_t serial)
891{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400892 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400893 struct weston_keyboard_grab *grab = keyboard->grab;
894 uint32_t mods_depressed, mods_latched, mods_locked, group;
895 uint32_t mods_lookup;
896 enum weston_led leds = 0;
897 int changed = 0;
898
899 /* Serialize and update our internal state, checking to see if it's
900 * different to the previous state. */
901 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
902 XKB_STATE_DEPRESSED);
903 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
904 XKB_STATE_LATCHED);
905 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
906 XKB_STATE_LOCKED);
907 group = xkb_state_serialize_group(seat->xkb_state.state,
908 XKB_STATE_EFFECTIVE);
909
Kristian Høgsberge3148752013-05-06 23:19:49 -0400910 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
911 mods_latched != seat->keyboard->modifiers.mods_latched ||
912 mods_locked != seat->keyboard->modifiers.mods_locked ||
913 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400914 changed = 1;
915
Daniel Stone96d47c02013-11-19 11:37:12 +0100916 run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
917 mods_depressed);
918
Kristian Høgsberge3148752013-05-06 23:19:49 -0400919 seat->keyboard->modifiers.mods_depressed = mods_depressed;
920 seat->keyboard->modifiers.mods_latched = mods_latched;
921 seat->keyboard->modifiers.mods_locked = mods_locked;
922 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400923
924 /* And update the modifier_state for bindings. */
925 mods_lookup = mods_depressed | mods_latched;
926 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000927 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400928 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000929 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400930 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000931 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400932 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000933 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400934 seat->modifier_state |= MODIFIER_SHIFT;
935
936 /* Finally, notify the compositor that LEDs have changed. */
937 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000938 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400939 leds |= LED_NUM_LOCK;
940 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000941 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400942 leds |= LED_CAPS_LOCK;
943 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000944 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400945 leds |= LED_SCROLL_LOCK;
946 if (leds != seat->xkb_state.leds && seat->led_update)
947 seat->led_update(seat, leds);
948 seat->xkb_state.leds = leds;
949
950 if (changed) {
951 grab->interface->modifiers(grab,
952 serial,
953 keyboard->modifiers.mods_depressed,
954 keyboard->modifiers.mods_latched,
955 keyboard->modifiers.mods_locked,
956 keyboard->modifiers.group);
957 }
958}
959
960static void
961update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
962 enum wl_keyboard_key_state state)
963{
964 enum xkb_key_direction direction;
965
Matt Roper01a92732013-06-24 16:52:44 +0100966 /* Keyboard modifiers don't exist in raw keyboard mode */
967 if (!seat->compositor->use_xkbcommon)
968 return;
969
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400970 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
971 direction = XKB_KEY_DOWN;
972 else
973 direction = XKB_KEY_UP;
974
975 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
976 * broken keycode system, which starts at 8. */
977 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
978
979 notify_modifiers(seat, serial);
980}
Rui Matos65196bc2013-10-10 19:44:19 +0200981
982static void
983send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
984{
985 wl_keyboard_send_keymap(resource,
986 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
987 xkb_info->keymap_fd,
988 xkb_info->keymap_size);
989}
990
991static void
992send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
993{
994 wl_keyboard_send_modifiers(resource, serial,
995 keyboard->modifiers.mods_depressed,
996 keyboard->modifiers.mods_latched,
997 keyboard->modifiers.mods_locked,
998 keyboard->modifiers.group);
999}
1000
1001static struct weston_xkb_info *
1002weston_xkb_info_create(struct xkb_keymap *keymap);
1003static void
1004weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1005
1006static void
1007update_keymap(struct weston_seat *seat)
1008{
1009 struct wl_resource *resource;
1010 struct weston_xkb_info *xkb_info;
1011 struct xkb_state *state;
1012 xkb_mod_mask_t latched_mods;
1013 xkb_mod_mask_t locked_mods;
1014
1015 xkb_info = weston_xkb_info_create(seat->pending_keymap);
1016
1017 xkb_keymap_unref(seat->pending_keymap);
1018 seat->pending_keymap = NULL;
1019
1020 if (!xkb_info) {
1021 weston_log("failed to create XKB info\n");
1022 return;
1023 }
1024
1025 state = xkb_state_new(xkb_info->keymap);
1026 if (!state) {
1027 weston_log("failed to initialise XKB state\n");
1028 weston_xkb_info_destroy(xkb_info);
1029 return;
1030 }
1031
1032 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
1033 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
1034 xkb_state_update_mask(state,
1035 0, /* depressed */
1036 latched_mods,
1037 locked_mods,
1038 0, 0, 0);
1039
1040 weston_xkb_info_destroy(seat->xkb_info);
1041 seat->xkb_info = xkb_info;
1042
1043 xkb_state_unref(seat->xkb_state.state);
1044 seat->xkb_state.state = state;
1045
1046 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1047 send_keymap(resource, xkb_info);
1048 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1049 send_keymap(resource, xkb_info);
1050
1051 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1052
1053 if (!latched_mods && !locked_mods)
1054 return;
1055
1056 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1057 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1058 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1059 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1060}
Rob Bradford382ff462013-06-24 16:52:45 +01001061#else
1062WL_EXPORT void
1063notify_modifiers(struct weston_seat *seat, uint32_t serial)
1064{
1065}
1066
1067static void
1068update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1069 enum wl_keyboard_key_state state)
1070{
1071}
Rui Matos65196bc2013-10-10 19:44:19 +02001072
1073static void
1074update_keymap(struct weston_seat *seat)
1075{
1076}
Rob Bradford382ff462013-06-24 16:52:45 +01001077#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001078
1079WL_EXPORT void
1080notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1081 enum wl_keyboard_key_state state,
1082 enum weston_key_state_update update_state)
1083{
1084 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001087 struct weston_keyboard_grab *grab = keyboard->grab;
1088 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1089 uint32_t *k, *end;
1090
1091 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1092 if (compositor->ping_handler && focus)
1093 compositor->ping_handler(focus, serial);
1094
1095 weston_compositor_idle_inhibit(compositor);
1096 keyboard->grab_key = key;
1097 keyboard->grab_time = time;
1098 } else {
1099 weston_compositor_idle_release(compositor);
1100 }
1101
1102 end = keyboard->keys.data + keyboard->keys.size;
1103 for (k = keyboard->keys.data; k < end; k++) {
1104 if (*k == key) {
1105 /* Ignore server-generated repeats. */
1106 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1107 return;
1108 *k = *--end;
1109 }
1110 }
1111 keyboard->keys.size = (void *) end - keyboard->keys.data;
1112 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1113 k = wl_array_add(&keyboard->keys, sizeof *k);
1114 *k = key;
1115 }
1116
1117 if (grab == &keyboard->default_grab ||
1118 grab == &keyboard->input_method_grab) {
1119 weston_compositor_run_key_binding(compositor, seat, time, key,
1120 state);
1121 grab = keyboard->grab;
1122 }
1123
1124 grab->interface->key(grab, time, key, state);
1125
Rui Matos65196bc2013-10-10 19:44:19 +02001126 if (seat->pending_keymap &&
1127 keyboard->keys.size == 0)
1128 update_keymap(seat);
1129
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001130 if (update_state == STATE_UPDATE_AUTOMATIC) {
1131 update_modifier_state(seat,
1132 wl_display_get_serial(compositor->wl_display),
1133 key,
1134 state);
1135 }
1136}
1137
1138WL_EXPORT void
1139notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1140 wl_fixed_t x, wl_fixed_t y)
1141{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001142 if (output) {
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001143 weston_pointer_move(seat->pointer, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001144 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001145 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001146 * NULL) here, but somehow that breaks re-entry... */
1147 }
1148}
1149
1150static void
1151destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1152{
1153 struct weston_seat *ws;
1154
1155 ws = container_of(listener, struct weston_seat,
1156 saved_kbd_focus_listener);
1157
1158 ws->saved_kbd_focus = NULL;
1159}
1160
1161WL_EXPORT void
1162notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1163 enum weston_key_state_update update_state)
1164{
1165 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001166 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001167 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001168 uint32_t *k, serial;
1169
1170 serial = wl_display_next_serial(compositor->wl_display);
1171 wl_array_copy(&keyboard->keys, keys);
1172 wl_array_for_each(k, &keyboard->keys) {
1173 weston_compositor_idle_inhibit(compositor);
1174 if (update_state == STATE_UPDATE_AUTOMATIC)
1175 update_modifier_state(seat, serial, *k,
1176 WL_KEYBOARD_KEY_STATE_PRESSED);
1177 }
1178
1179 /* Run key bindings after we've updated the state. */
1180 wl_array_for_each(k, &keyboard->keys) {
1181 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1182 WL_KEYBOARD_KEY_STATE_PRESSED);
1183 }
1184
1185 surface = seat->saved_kbd_focus;
1186
1187 if (surface) {
1188 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1189 weston_keyboard_set_focus(keyboard, surface);
1190 seat->saved_kbd_focus = NULL;
1191 }
1192}
1193
1194WL_EXPORT void
1195notify_keyboard_focus_out(struct weston_seat *seat)
1196{
1197 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001198 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199 uint32_t *k, serial;
1200
1201 serial = wl_display_next_serial(compositor->wl_display);
1202 wl_array_for_each(k, &keyboard->keys) {
1203 weston_compositor_idle_release(compositor);
1204 update_modifier_state(seat, serial, *k,
1205 WL_KEYBOARD_KEY_STATE_RELEASED);
1206 }
1207
1208 seat->modifier_state = 0;
1209
1210 if (keyboard->focus) {
1211 seat->saved_kbd_focus = keyboard->focus;
1212 seat->saved_kbd_focus_listener.notify =
1213 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001214 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001215 &seat->saved_kbd_focus_listener);
1216 }
1217
1218 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001219 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001220}
1221
Michael Fua2bb7912013-07-23 15:51:06 +08001222WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001224{
Neil Roberts96d790e2013-09-19 17:32:00 +01001225 struct wl_list *focus_resource_list;
1226
1227 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001229 if (view && seat->touch->focus &&
1230 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001234
Neil Roberts96d790e2013-09-19 17:32:00 +01001235 if (!wl_list_empty(focus_resource_list)) {
1236 move_resources(&seat->touch->resource_list,
1237 focus_resource_list);
1238 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001241 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001243 move_resources_for_client(focus_resource_list,
1244 &seat->touch->resource_list,
1245 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001246 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001248}
1249
1250/**
1251 * notify_touch - emulates button touches and notifies surfaces accordingly.
1252 *
1253 * It assumes always the correct cycle sequence until it gets here: touch_down
1254 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1255 * for sending along such order.
1256 *
1257 */
1258WL_EXPORT void
1259notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1260 wl_fixed_t x, wl_fixed_t y, int touch_type)
1261{
1262 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001263 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001264 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001266 wl_fixed_t sx, sy;
1267
1268 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001269 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1270 touch->grab_x = x;
1271 touch->grab_y = y;
1272 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001273
1274 switch (touch_type) {
1275 case WL_TOUCH_DOWN:
1276 weston_compositor_idle_inhibit(ec);
1277
1278 seat->num_tp++;
1279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 /* the first finger down picks the view, and all further go
1281 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001282 * until all touch points are up again. */
1283 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1285 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001286 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 ev = touch->focus;
1288 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001289 } else {
1290 /* Unexpected condition: We have non-initial touch but
1291 * there is no focused surface.
1292 */
1293 weston_log("touch event received with %d points down"
1294 "but no surface focused\n", seat->num_tp);
1295 return;
1296 }
1297
1298 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001299 if (seat->num_tp == 1) {
1300 touch->grab_serial =
1301 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001302 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001303 touch->grab_time = time;
1304 touch->grab_x = x;
1305 touch->grab_y = y;
1306 }
1307
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001308 break;
1309 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 ev = touch->focus;
1311 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001312 break;
1313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001315 grab->interface->motion(grab, time, touch_id, sx, sy);
1316 break;
1317 case WL_TOUCH_UP:
1318 weston_compositor_idle_release(ec);
1319 seat->num_tp--;
1320
1321 grab->interface->up(grab, time, touch_id);
1322 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001323 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001324 break;
1325 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001326
1327 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328}
1329
1330static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001331pointer_cursor_surface_configure(struct weston_surface *es,
1332 int32_t dx, int32_t dy, int32_t width, int32_t height)
1333{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001334 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001335 int x, y;
1336
1337 if (width == 0)
1338 return;
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001341
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001342 pointer->hotspot_x -= dx;
1343 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001344
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001345 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1346 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001349
1350 empty_region(&es->pending.input);
1351
1352 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 wl_list_insert(&es->compositor->cursor_layer.view_list,
1354 &pointer->sprite->layer_link);
1355 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001356 }
1357}
1358
1359static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001360pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1361 uint32_t serial, struct wl_resource *surface_resource,
1362 int32_t x, int32_t y)
1363{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001364 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001365 struct weston_surface *surface = NULL;
1366
1367 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001368 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001369
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001370 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001371 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001373 black_surface used in shell.c for fullscreen don't have
1374 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001376 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001378 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001379 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001380 return;
1381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001383 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001384 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001385 WL_DISPLAY_ERROR_INVALID_OBJECT,
1386 "surface->configure already "
1387 "set");
1388 return;
1389 }
1390 }
1391
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001392 if (pointer->sprite)
1393 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001394
1395 if (!surface)
1396 return;
1397
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001398 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001399 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001400
1401 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001402 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001404 pointer->hotspot_x = x;
1405 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001406
1407 if (surface->buffer_ref.buffer)
1408 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1409 weston_surface_buffer_height(surface));
1410}
1411
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001412static void
1413pointer_release(struct wl_client *client, struct wl_resource *resource)
1414{
1415 wl_resource_destroy(resource);
1416}
1417
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001418static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001419 pointer_set_cursor,
1420 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001421};
1422
1423static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001424seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1425 uint32_t id)
1426{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001427 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001428 struct wl_resource *cr;
1429
Kristian Høgsberge3148752013-05-06 23:19:49 -04001430 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001431 return;
1432
Jason Ekstranda85118c2013-06-27 20:17:02 -05001433 cr = wl_resource_create(client, &wl_pointer_interface,
1434 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001435 if (cr == NULL) {
1436 wl_client_post_no_memory(client);
1437 return;
1438 }
1439
Neil Roberts96d790e2013-09-19 17:32:00 +01001440 /* May be moved to focused list later by either
1441 * weston_pointer_set_focus or directly if this client is already
1442 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001443 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001444 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1445 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1448 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449 wl_fixed_t sx, sy;
1450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_view_from_global_fixed(seat->pointer->focus,
1452 seat->pointer->x,
1453 seat->pointer->y,
1454 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001455
1456 wl_list_remove(wl_resource_get_link(cr));
1457 wl_list_insert(&seat->pointer->focus_resource_list,
1458 wl_resource_get_link(cr));
1459 wl_pointer_send_enter(cr,
1460 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001462 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001463 }
1464}
1465
1466static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001467keyboard_release(struct wl_client *client, struct wl_resource *resource)
1468{
1469 wl_resource_destroy(resource);
1470}
1471
1472static const struct wl_keyboard_interface keyboard_interface = {
1473 keyboard_release
1474};
1475
Neil Roberts96d790e2013-09-19 17:32:00 +01001476static int
1477should_send_modifiers_to_client(struct weston_seat *seat,
1478 struct wl_client *client)
1479{
1480 if (seat->keyboard &&
1481 seat->keyboard->focus &&
1482 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1483 return 1;
1484
1485 if (seat->pointer &&
1486 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001488 return 1;
1489
1490 return 0;
1491}
1492
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001493static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001494seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1495 uint32_t id)
1496{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001497 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001498 struct wl_resource *cr;
1499
Kristian Høgsberge3148752013-05-06 23:19:49 -04001500 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001501 return;
1502
Jason Ekstranda85118c2013-06-27 20:17:02 -05001503 cr = wl_resource_create(client, &wl_keyboard_interface,
1504 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001505 if (cr == NULL) {
1506 wl_client_post_no_memory(client);
1507 return;
1508 }
1509
Neil Roberts96d790e2013-09-19 17:32:00 +01001510 /* May be moved to focused list later by either
1511 * weston_keyboard_set_focus or directly if this client is already
1512 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001513 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001514 wl_resource_set_implementation(cr, &keyboard_interface,
1515 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001516
Matt Roper01a92732013-06-24 16:52:44 +01001517 if (seat->compositor->use_xkbcommon) {
1518 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001519 seat->xkb_info->keymap_fd,
1520 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001521 } else {
1522 int null_fd = open("/dev/null", O_RDONLY);
1523 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1524 null_fd,
1525 0);
1526 close(null_fd);
1527 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001528
Neil Roberts96d790e2013-09-19 17:32:00 +01001529 if (should_send_modifiers_to_client(seat, client)) {
1530 send_modifiers_to_resource(seat->keyboard,
1531 cr,
1532 seat->keyboard->focus_serial);
1533 }
1534
Kristian Høgsberge3148752013-05-06 23:19:49 -04001535 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001536 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001537 struct weston_surface *surface =
1538 (struct weston_surface *) seat->keyboard->focus;
1539
1540 wl_list_remove(wl_resource_get_link(cr));
1541 wl_list_insert(&seat->keyboard->focus_resource_list,
1542 wl_resource_get_link(cr));
1543 wl_keyboard_send_enter(cr,
1544 seat->keyboard->focus_serial,
1545 surface->resource,
1546 &seat->keyboard->keys);
1547
1548 /* If this is the first keyboard resource for this
1549 * client... */
1550 if (seat->keyboard->focus_resource_list.prev ==
1551 wl_resource_get_link(cr))
1552 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553 }
1554}
1555
1556static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001557touch_release(struct wl_client *client, struct wl_resource *resource)
1558{
1559 wl_resource_destroy(resource);
1560}
1561
1562static const struct wl_touch_interface touch_interface = {
1563 touch_release
1564};
1565
1566static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001567seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1568 uint32_t id)
1569{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001570 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001571 struct wl_resource *cr;
1572
Kristian Høgsberge3148752013-05-06 23:19:49 -04001573 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001574 return;
1575
Jason Ekstranda85118c2013-06-27 20:17:02 -05001576 cr = wl_resource_create(client, &wl_touch_interface,
1577 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001578 if (cr == NULL) {
1579 wl_client_post_no_memory(client);
1580 return;
1581 }
1582
Neil Roberts96d790e2013-09-19 17:32:00 +01001583 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001585 wl_list_insert(&seat->touch->resource_list,
1586 wl_resource_get_link(cr));
1587 } else {
1588 wl_list_insert(&seat->touch->focus_resource_list,
1589 wl_resource_get_link(cr));
1590 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001591 wl_resource_set_implementation(cr, &touch_interface,
1592 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001593}
1594
1595static const struct wl_seat_interface seat_interface = {
1596 seat_get_pointer,
1597 seat_get_keyboard,
1598 seat_get_touch,
1599};
1600
1601static void
1602bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1603{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001604 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605 struct wl_resource *resource;
1606 enum wl_seat_capability caps = 0;
1607
Jason Ekstranda85118c2013-06-27 20:17:02 -05001608 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001609 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001610 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001611 wl_resource_set_implementation(resource, &seat_interface, data,
1612 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001613
1614 if (seat->pointer)
1615 caps |= WL_SEAT_CAPABILITY_POINTER;
1616 if (seat->keyboard)
1617 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1618 if (seat->touch)
1619 caps |= WL_SEAT_CAPABILITY_TOUCH;
1620
1621 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001622 if (version >= 2)
1623 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001624}
1625
Rob Bradford382ff462013-06-24 16:52:45 +01001626#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627int
1628weston_compositor_xkb_init(struct weston_compositor *ec,
1629 struct xkb_rule_names *names)
1630{
Rob Bradford382ff462013-06-24 16:52:45 +01001631 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001632
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001633 if (ec->xkb_context == NULL) {
1634 ec->xkb_context = xkb_context_new(0);
1635 if (ec->xkb_context == NULL) {
1636 weston_log("failed to create XKB context\n");
1637 return -1;
1638 }
1639 }
1640
1641 if (names)
1642 ec->xkb_names = *names;
1643 if (!ec->xkb_names.rules)
1644 ec->xkb_names.rules = strdup("evdev");
1645 if (!ec->xkb_names.model)
1646 ec->xkb_names.model = strdup("pc105");
1647 if (!ec->xkb_names.layout)
1648 ec->xkb_names.layout = strdup("us");
1649
1650 return 0;
1651}
1652
Stefan Schmidtfda26522013-09-17 10:54:09 +01001653static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001654weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001655{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001656 if (--xkb_info->ref_count > 0)
1657 return;
1658
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001659 if (xkb_info->keymap)
1660 xkb_map_unref(xkb_info->keymap);
1661
1662 if (xkb_info->keymap_area)
1663 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1664 if (xkb_info->keymap_fd >= 0)
1665 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001666 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001667}
1668
1669void
1670weston_compositor_xkb_destroy(struct weston_compositor *ec)
1671{
Matt Roper01a92732013-06-24 16:52:44 +01001672 /*
1673 * If we're operating in raw keyboard mode, we never initialized
1674 * libxkbcommon so there's no cleanup to do either.
1675 */
1676 if (!ec->use_xkbcommon)
1677 return;
1678
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001679 free((char *) ec->xkb_names.rules);
1680 free((char *) ec->xkb_names.model);
1681 free((char *) ec->xkb_names.layout);
1682 free((char *) ec->xkb_names.variant);
1683 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001684
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001685 if (ec->xkb_info)
1686 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001687 xkb_context_unref(ec->xkb_context);
1688}
1689
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001690static struct weston_xkb_info *
1691weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001692{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001693 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1694 if (xkb_info == NULL)
1695 return NULL;
1696
1697 xkb_info->keymap = xkb_map_ref(keymap);
1698 xkb_info->ref_count = 1;
1699
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700 char *keymap_str;
1701
1702 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1703 XKB_MOD_NAME_SHIFT);
1704 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1705 XKB_MOD_NAME_CAPS);
1706 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1707 XKB_MOD_NAME_CTRL);
1708 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1709 XKB_MOD_NAME_ALT);
1710 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1711 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1712 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1713 XKB_MOD_NAME_LOGO);
1714 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1715
1716 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1717 XKB_LED_NAME_NUM);
1718 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1719 XKB_LED_NAME_CAPS);
1720 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1721 XKB_LED_NAME_SCROLL);
1722
1723 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1724 if (keymap_str == NULL) {
1725 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001726 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001727 }
1728 xkb_info->keymap_size = strlen(keymap_str) + 1;
1729
1730 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1731 if (xkb_info->keymap_fd < 0) {
1732 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1733 (unsigned long) xkb_info->keymap_size);
1734 goto err_keymap_str;
1735 }
1736
1737 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1738 PROT_READ | PROT_WRITE,
1739 MAP_SHARED, xkb_info->keymap_fd, 0);
1740 if (xkb_info->keymap_area == MAP_FAILED) {
1741 weston_log("failed to mmap() %lu bytes\n",
1742 (unsigned long) xkb_info->keymap_size);
1743 goto err_dev_zero;
1744 }
1745 strcpy(xkb_info->keymap_area, keymap_str);
1746 free(keymap_str);
1747
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001748 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001749
1750err_dev_zero:
1751 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001752err_keymap_str:
1753 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001754err_keymap:
1755 xkb_map_unref(xkb_info->keymap);
1756 free(xkb_info);
1757 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001758}
1759
1760static int
1761weston_compositor_build_global_keymap(struct weston_compositor *ec)
1762{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001763 struct xkb_keymap *keymap;
1764
1765 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001766 return 0;
1767
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001768 keymap = xkb_map_new_from_names(ec->xkb_context,
1769 &ec->xkb_names,
1770 0);
1771 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001772 weston_log("failed to compile global XKB keymap\n");
1773 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1774 "options %s\n",
1775 ec->xkb_names.rules, ec->xkb_names.model,
1776 ec->xkb_names.layout, ec->xkb_names.variant,
1777 ec->xkb_names.options);
1778 return -1;
1779 }
1780
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001781 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001782 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001783 return -1;
1784
1785 return 0;
1786}
Rob Bradford382ff462013-06-24 16:52:45 +01001787#else
1788int
1789weston_compositor_xkb_init(struct weston_compositor *ec,
1790 struct xkb_rule_names *names)
1791{
1792 return 0;
1793}
1794
1795void
1796weston_compositor_xkb_destroy(struct weston_compositor *ec)
1797{
1798}
1799#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001800
Rui Matos65196bc2013-10-10 19:44:19 +02001801WL_EXPORT void
1802weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1803{
1804 if (!seat->keyboard || !keymap)
1805 return;
1806
1807#ifdef ENABLE_XKBCOMMON
1808 if (!seat->compositor->use_xkbcommon)
1809 return;
1810
1811 xkb_keymap_unref(seat->pending_keymap);
1812 seat->pending_keymap = xkb_keymap_ref(keymap);
1813
1814 if (seat->keyboard->keys.size == 0)
1815 update_keymap(seat);
1816#endif
1817}
1818
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001819WL_EXPORT int
1820weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1821{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001822 struct weston_keyboard *keyboard;
1823
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001824 if (seat->keyboard) {
1825 seat->keyboard_device_count += 1;
1826 if (seat->keyboard_device_count == 1)
1827 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001829 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001830
Rob Bradford382ff462013-06-24 16:52:45 +01001831#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001832 if (seat->compositor->use_xkbcommon) {
1833 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001834 seat->xkb_info = weston_xkb_info_create(keymap);
1835 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001836 return -1;
1837 } else {
1838 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1839 return -1;
1840 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001841 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001842 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001843
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001844 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001845 if (seat->xkb_state.state == NULL) {
1846 weston_log("failed to initialise XKB state\n");
1847 return -1;
1848 }
1849
1850 seat->xkb_state.leds = 0;
1851 }
Rob Bradford382ff462013-06-24 16:52:45 +01001852#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001853
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001854 keyboard = weston_keyboard_create();
1855 if (keyboard == NULL) {
1856 weston_log("failed to allocate weston keyboard struct\n");
1857 return -1;
1858 }
1859
1860 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001861 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001862 keyboard->seat = seat;
1863
1864 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001865
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001866 return 0;
1867}
1868
1869WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001870weston_seat_release_keyboard(struct weston_seat *seat)
1871{
1872 seat->keyboard_device_count--;
1873 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001874 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001875 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001876 seat_send_updated_caps(seat);
1877 }
1878}
1879
1880WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881weston_seat_init_pointer(struct weston_seat *seat)
1882{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001883 struct weston_pointer *pointer;
1884
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001885 if (seat->pointer) {
1886 seat->pointer_device_count += 1;
1887 if (seat->pointer_device_count == 1)
1888 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001889 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001890 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001891
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001892 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001893 if (pointer == NULL)
1894 return;
1895
1896 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001897 seat->pointer_device_count = 1;
1898 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001899
1900 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001901}
1902
1903WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001904weston_seat_release_pointer(struct weston_seat *seat)
1905{
1906 struct weston_pointer *pointer = seat->pointer;
1907
1908 seat->pointer_device_count--;
1909 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001910 weston_pointer_set_focus(pointer, NULL,
1911 wl_fixed_from_int(0),
1912 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001913 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001914
Jonas Ådahla4932742013-10-17 23:04:07 +02001915 if (pointer->sprite)
1916 pointer_unmap_sprite(pointer);
1917
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001918 seat_send_updated_caps(seat);
1919 }
1920}
1921
1922WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001923weston_seat_init_touch(struct weston_seat *seat)
1924{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001925 struct weston_touch *touch;
1926
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001927 if (seat->touch) {
1928 seat->touch_device_count += 1;
1929 if (seat->touch_device_count == 1)
1930 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001931 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001932 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001934 touch = weston_touch_create();
1935 if (touch == NULL)
1936 return;
1937
1938 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001939 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001940 touch->seat = seat;
1941
1942 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001943}
1944
1945WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001946weston_seat_release_touch(struct weston_seat *seat)
1947{
1948 seat->touch_device_count--;
1949 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001950 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001951 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001952 seat_send_updated_caps(seat);
1953 }
1954}
1955
1956WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001957weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1958 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001959{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001960 memset(seat, 0, sizeof *seat);
1961
Kristian Høgsberge3148752013-05-06 23:19:49 -04001962 seat->selection_data_source = NULL;
1963 wl_list_init(&seat->base_resource_list);
1964 wl_signal_init(&seat->selection_signal);
1965 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001966 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001967
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001968 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001969 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001970
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001971 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001972 seat->modifier_state = 0;
1973 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001974 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001975
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001976 wl_list_insert(ec->seat_list.prev, &seat->link);
1977
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001978 clipboard_create(seat);
1979
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001980 wl_signal_emit(&ec->seat_created_signal, seat);
1981}
1982
1983WL_EXPORT void
1984weston_seat_release(struct weston_seat *seat)
1985{
1986 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001987
Rob Bradford382ff462013-06-24 16:52:45 +01001988#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001989 if (seat->compositor->use_xkbcommon) {
1990 if (seat->xkb_state.state != NULL)
1991 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001992 if (seat->xkb_info)
1993 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001994 if (seat->pending_keymap)
1995 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001996 }
Rob Bradford382ff462013-06-24 16:52:45 +01001997#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001998
Kristian Høgsberge3148752013-05-06 23:19:49 -04001999 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002000 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002001 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002002 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002003 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002004 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04002005
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002006 free (seat->seat_name);
2007
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002008 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04002009
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002010 wl_signal_emit(&seat->destroy_signal, seat);
2011}