blob: d7b4b139b059df9250616bbc33f0f8ed622a69a3 [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 *
384weston_pointer_create(void)
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);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400394 pointer->default_grab.interface = &default_pointer_grab_interface;
395 pointer->default_grab.pointer = pointer;
396 pointer->grab = &pointer->default_grab;
397 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100398 wl_signal_init(&pointer->motion_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400400 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
401
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400402 /* FIXME: Pick better co-ords. */
403 pointer->x = wl_fixed_from_int(100);
404 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400405
406 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400407}
408
409WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400410weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400411{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400412 if (pointer->sprite)
413 pointer_unmap_sprite(pointer);
414
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100416
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400417 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418}
419
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400420WL_EXPORT struct weston_keyboard *
421weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400423 struct weston_keyboard *keyboard;
424
Peter Huttererf3d62272013-08-08 11:57:05 +1000425 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400426 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100427 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400428
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100430 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400431 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400432 keyboard->default_grab.interface = &default_keyboard_grab_interface;
433 keyboard->default_grab.keyboard = keyboard;
434 keyboard->grab = &keyboard->default_grab;
435 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400436
437 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400438}
439
440WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400441weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442{
443 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100444
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400445 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400446 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447}
448
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400449WL_EXPORT struct weston_touch *
450weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400452 struct weston_touch *touch;
453
Peter Huttererf3d62272013-08-08 11:57:05 +1000454 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400455 if (touch == NULL)
456 return NULL;
457
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400458 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100459 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460 touch->default_grab.interface = &default_touch_grab_interface;
461 touch->default_grab.touch = touch;
462 touch->grab = &touch->default_grab;
463 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400464
465 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400466}
467
468WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400469weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470{
471 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100472
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400473 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400474}
475
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400477seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100480 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400481
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200482 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200484 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400485 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200486 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487 caps |= WL_SEAT_CAPABILITY_TOUCH;
488
Rob Bradford6e737f52013-09-06 17:48:19 +0100489 wl_resource_for_each(resource, &seat->base_resource_list) {
490 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500491 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400492}
493
494WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400495weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500496 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400497 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400498{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400499 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100500 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100501 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400502 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100503 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 int different_surface = 0;
505
506 if ((!pointer->focus && view) ||
507 (pointer->focus && !view) ||
508 (pointer->focus && pointer->focus->surface != view->surface))
509 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510
Neil Roberts96d790e2013-09-19 17:32:00 +0100511 focus_resource_list = &pointer->focus_resource_list;
512
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400514 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100515 wl_resource_for_each(resource, focus_resource_list) {
516 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500517 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100518 }
519
520 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400521 }
522
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 if (find_resource_for_view(&pointer->resource_list, view) &&
524 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100525 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100527
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100529
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700531 send_modifiers_to_client_in_list(surface_client,
532 &kbd->resource_list,
533 serial,
534 kbd);
535
Neil Roberts96d790e2013-09-19 17:32:00 +0100536 move_resources_for_client(focus_resource_list,
537 &pointer->resource_list,
538 surface_client);
539
540 wl_resource_for_each(resource, focus_resource_list) {
541 wl_pointer_send_enter(resource,
542 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500543 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100544 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400545 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100546
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400547 pointer->focus_serial = serial;
548 }
549
Jason Ekstranda7af7042013-10-12 22:38:11 -0500550 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400551 wl_signal_emit(&pointer->focus_signal, pointer);
552}
553
Neil Roberts96d790e2013-09-19 17:32:00 +0100554static void
555send_enter_to_resource_list(struct wl_list *list,
556 struct weston_keyboard *keyboard,
557 struct weston_surface *surface,
558 uint32_t serial)
559{
560 struct wl_resource *resource;
561
562 wl_resource_for_each(resource, list) {
563 send_modifiers_to_resource(keyboard, resource, serial);
564 wl_keyboard_send_enter(resource, serial,
565 surface->resource,
566 &keyboard->keys);
567 }
568}
569
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400570WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400571weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400572 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573{
574 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100575 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400576 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100577 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578
Neil Roberts96d790e2013-09-19 17:32:00 +0100579 focus_resource_list = &keyboard->focus_resource_list;
580
581 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400582 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100583 wl_resource_for_each(resource, focus_resource_list) {
584 wl_keyboard_send_leave(resource, serial,
585 keyboard->focus->resource);
586 }
587 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400588 }
589
Neil Roberts96d790e2013-09-19 17:32:00 +0100590 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
591 keyboard->focus != surface) {
592 struct wl_client *surface_client =
593 wl_resource_get_client(surface->resource);
594
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400595 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100596
597 move_resources_for_client(focus_resource_list,
598 &keyboard->resource_list,
599 surface_client);
600 send_enter_to_resource_list(focus_resource_list,
601 keyboard,
602 surface,
603 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400604 keyboard->focus_serial = serial;
605 }
606
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607 keyboard->focus = surface;
608 wl_signal_emit(&keyboard->focus_signal, keyboard);
609}
610
611WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400612weston_keyboard_start_grab(struct weston_keyboard *keyboard,
613 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400614{
615 keyboard->grab = grab;
616 grab->keyboard = keyboard;
617
618 /* XXX focus? */
619}
620
621WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400622weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400623{
624 keyboard->grab = &keyboard->default_grab;
625}
626
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200627static void
628weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
629{
630 keyboard->grab->interface->cancel(keyboard->grab);
631}
632
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400634weston_pointer_start_grab(struct weston_pointer *pointer,
635 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400637 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400638 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400639 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640}
641
642WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400643weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400644{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400645 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400646 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400647}
648
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200649static void
650weston_pointer_cancel_grab(struct weston_pointer *pointer)
651{
652 pointer->grab->interface->cancel(pointer->grab);
653}
654
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400655WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400656weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400657{
658 touch->grab = grab;
659 grab->touch = touch;
660}
661
662WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400663weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400664{
665 touch->grab = &touch->default_grab;
666}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400667
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200668static void
669weston_touch_cancel_grab(struct weston_touch *touch)
670{
671 touch->grab->interface->cancel(touch->grab);
672}
673
Rob Bradford806d8c02013-06-25 18:56:41 +0100674WL_EXPORT void
675weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400676{
Rob Bradford806d8c02013-06-25 18:56:41 +0100677 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400678 struct weston_output *output, *prev = NULL;
679 int x, y, old_x, old_y, valid = 0;
680
681 x = wl_fixed_to_int(*fx);
682 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100683 old_x = wl_fixed_to_int(pointer->x);
684 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400685
686 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100687 if (pointer->seat->output && pointer->seat->output != output)
688 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400689 if (pixman_region32_contains_point(&output->region,
690 x, y, NULL))
691 valid = 1;
692 if (pixman_region32_contains_point(&output->region,
693 old_x, old_y, NULL))
694 prev = output;
695 }
696
Rob Bradford66bd9f52013-06-25 18:56:42 +0100697 if (!prev)
698 prev = pointer->seat->output;
699
700 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400701 if (x < prev->x)
702 *fx = wl_fixed_from_int(prev->x);
703 else if (x >= prev->x + prev->width)
704 *fx = wl_fixed_from_int(prev->x +
705 prev->width - 1);
706 if (y < prev->y)
707 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200708 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400709 *fy = wl_fixed_from_int(prev->y +
710 prev->height - 1);
711 }
712}
713
714/* Takes absolute values */
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100715WL_EXPORT void
716weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400717{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400718 int32_t ix, iy;
719
Rob Bradford806d8c02013-06-25 18:56:41 +0100720 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400721
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400722 pointer->x = x;
723 pointer->y = y;
724
725 ix = wl_fixed_to_int(x);
726 iy = wl_fixed_to_int(y);
727
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400728 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500729 weston_view_set_position(pointer->sprite,
730 ix - pointer->hotspot_x,
731 iy - pointer->hotspot_y);
732 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400733 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100734
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100735 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100736 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400737}
738
739WL_EXPORT void
740notify_motion(struct weston_seat *seat,
741 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
742{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400743 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400744 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400745
746 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100747 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400748}
749
750WL_EXPORT void
751notify_motion_absolute(struct weston_seat *seat,
752 uint32_t time, wl_fixed_t x, wl_fixed_t y)
753{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400754 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400755 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400756
757 weston_compositor_wake(ec);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100758 pointer->grab->interface->motion(pointer->grab, time, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400759}
760
761WL_EXPORT void
762weston_surface_activate(struct weston_surface *surface,
763 struct weston_seat *seat)
764{
765 struct weston_compositor *compositor = seat->compositor;
766
Kristian Høgsberge3148752013-05-06 23:19:49 -0400767 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400768 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400769 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400770 }
771
772 wl_signal_emit(&compositor->activate_signal, surface);
773}
774
775WL_EXPORT void
776notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
777 enum wl_pointer_button_state state)
778{
779 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400780 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400781 struct weston_surface *focus =
782 (struct weston_surface *) pointer->focus;
783 uint32_t serial = wl_display_next_serial(compositor->wl_display);
784
785 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
786 if (compositor->ping_handler && focus)
787 compositor->ping_handler(focus, serial);
788 weston_compositor_idle_inhibit(compositor);
789 if (pointer->button_count == 0) {
790 pointer->grab_button = button;
791 pointer->grab_time = time;
792 pointer->grab_x = pointer->x;
793 pointer->grab_y = pointer->y;
794 }
795 pointer->button_count++;
796 } else {
797 weston_compositor_idle_release(compositor);
798 pointer->button_count--;
799 }
800
801 weston_compositor_run_button_binding(compositor, seat, time, button,
802 state);
803
804 pointer->grab->interface->button(pointer->grab, time, button, state);
805
806 if (pointer->button_count == 1)
807 pointer->grab_serial =
808 wl_display_get_serial(compositor->wl_display);
809}
810
811WL_EXPORT void
812notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
813 wl_fixed_t value)
814{
815 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400816 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400817 struct weston_surface *focus =
818 (struct weston_surface *) pointer->focus;
819 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100820 struct wl_resource *resource;
821 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400822
823 if (compositor->ping_handler && focus)
824 compositor->ping_handler(focus, serial);
825
826 weston_compositor_wake(compositor);
827
828 if (!value)
829 return;
830
831 if (weston_compositor_run_axis_binding(compositor, seat,
832 time, axis, value))
833 return;
834
Neil Roberts96d790e2013-09-19 17:32:00 +0100835 resource_list = &pointer->focus_resource_list;
836 wl_resource_for_each(resource, resource_list)
837 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400838 value);
839}
840
Rob Bradford382ff462013-06-24 16:52:45 +0100841#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400842WL_EXPORT void
843notify_modifiers(struct weston_seat *seat, uint32_t serial)
844{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400845 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400846 struct weston_keyboard_grab *grab = keyboard->grab;
847 uint32_t mods_depressed, mods_latched, mods_locked, group;
848 uint32_t mods_lookup;
849 enum weston_led leds = 0;
850 int changed = 0;
851
852 /* Serialize and update our internal state, checking to see if it's
853 * different to the previous state. */
854 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
855 XKB_STATE_DEPRESSED);
856 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
857 XKB_STATE_LATCHED);
858 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
859 XKB_STATE_LOCKED);
860 group = xkb_state_serialize_group(seat->xkb_state.state,
861 XKB_STATE_EFFECTIVE);
862
Kristian Høgsberge3148752013-05-06 23:19:49 -0400863 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
864 mods_latched != seat->keyboard->modifiers.mods_latched ||
865 mods_locked != seat->keyboard->modifiers.mods_locked ||
866 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400867 changed = 1;
868
Kristian Høgsberge3148752013-05-06 23:19:49 -0400869 seat->keyboard->modifiers.mods_depressed = mods_depressed;
870 seat->keyboard->modifiers.mods_latched = mods_latched;
871 seat->keyboard->modifiers.mods_locked = mods_locked;
872 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400873
874 /* And update the modifier_state for bindings. */
875 mods_lookup = mods_depressed | mods_latched;
876 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000877 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400878 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000879 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400880 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000881 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400882 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000883 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400884 seat->modifier_state |= MODIFIER_SHIFT;
885
886 /* Finally, notify the compositor that LEDs have changed. */
887 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000888 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400889 leds |= LED_NUM_LOCK;
890 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000891 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400892 leds |= LED_CAPS_LOCK;
893 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000894 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400895 leds |= LED_SCROLL_LOCK;
896 if (leds != seat->xkb_state.leds && seat->led_update)
897 seat->led_update(seat, leds);
898 seat->xkb_state.leds = leds;
899
900 if (changed) {
901 grab->interface->modifiers(grab,
902 serial,
903 keyboard->modifiers.mods_depressed,
904 keyboard->modifiers.mods_latched,
905 keyboard->modifiers.mods_locked,
906 keyboard->modifiers.group);
907 }
908}
909
910static void
911update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
912 enum wl_keyboard_key_state state)
913{
914 enum xkb_key_direction direction;
915
Matt Roper01a92732013-06-24 16:52:44 +0100916 /* Keyboard modifiers don't exist in raw keyboard mode */
917 if (!seat->compositor->use_xkbcommon)
918 return;
919
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400920 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
921 direction = XKB_KEY_DOWN;
922 else
923 direction = XKB_KEY_UP;
924
925 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
926 * broken keycode system, which starts at 8. */
927 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
928
929 notify_modifiers(seat, serial);
930}
Rui Matos65196bc2013-10-10 19:44:19 +0200931
932static void
933send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
934{
935 wl_keyboard_send_keymap(resource,
936 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
937 xkb_info->keymap_fd,
938 xkb_info->keymap_size);
939}
940
941static void
942send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
943{
944 wl_keyboard_send_modifiers(resource, serial,
945 keyboard->modifiers.mods_depressed,
946 keyboard->modifiers.mods_latched,
947 keyboard->modifiers.mods_locked,
948 keyboard->modifiers.group);
949}
950
951static struct weston_xkb_info *
952weston_xkb_info_create(struct xkb_keymap *keymap);
953static void
954weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
955
956static void
957update_keymap(struct weston_seat *seat)
958{
959 struct wl_resource *resource;
960 struct weston_xkb_info *xkb_info;
961 struct xkb_state *state;
962 xkb_mod_mask_t latched_mods;
963 xkb_mod_mask_t locked_mods;
964
965 xkb_info = weston_xkb_info_create(seat->pending_keymap);
966
967 xkb_keymap_unref(seat->pending_keymap);
968 seat->pending_keymap = NULL;
969
970 if (!xkb_info) {
971 weston_log("failed to create XKB info\n");
972 return;
973 }
974
975 state = xkb_state_new(xkb_info->keymap);
976 if (!state) {
977 weston_log("failed to initialise XKB state\n");
978 weston_xkb_info_destroy(xkb_info);
979 return;
980 }
981
982 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
983 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
984 xkb_state_update_mask(state,
985 0, /* depressed */
986 latched_mods,
987 locked_mods,
988 0, 0, 0);
989
990 weston_xkb_info_destroy(seat->xkb_info);
991 seat->xkb_info = xkb_info;
992
993 xkb_state_unref(seat->xkb_state.state);
994 seat->xkb_state.state = state;
995
996 wl_resource_for_each(resource, &seat->keyboard->resource_list)
997 send_keymap(resource, xkb_info);
998 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
999 send_keymap(resource, xkb_info);
1000
1001 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1002
1003 if (!latched_mods && !locked_mods)
1004 return;
1005
1006 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1007 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1008 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1009 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1010}
Rob Bradford382ff462013-06-24 16:52:45 +01001011#else
1012WL_EXPORT void
1013notify_modifiers(struct weston_seat *seat, uint32_t serial)
1014{
1015}
1016
1017static void
1018update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1019 enum wl_keyboard_key_state state)
1020{
1021}
Rui Matos65196bc2013-10-10 19:44:19 +02001022
1023static void
1024update_keymap(struct weston_seat *seat)
1025{
1026}
Rob Bradford382ff462013-06-24 16:52:45 +01001027#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001028
1029WL_EXPORT void
1030notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1031 enum wl_keyboard_key_state state,
1032 enum weston_key_state_update update_state)
1033{
1034 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001035 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001037 struct weston_keyboard_grab *grab = keyboard->grab;
1038 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1039 uint32_t *k, *end;
1040
1041 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1042 if (compositor->ping_handler && focus)
1043 compositor->ping_handler(focus, serial);
1044
1045 weston_compositor_idle_inhibit(compositor);
1046 keyboard->grab_key = key;
1047 keyboard->grab_time = time;
1048 } else {
1049 weston_compositor_idle_release(compositor);
1050 }
1051
1052 end = keyboard->keys.data + keyboard->keys.size;
1053 for (k = keyboard->keys.data; k < end; k++) {
1054 if (*k == key) {
1055 /* Ignore server-generated repeats. */
1056 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1057 return;
1058 *k = *--end;
1059 }
1060 }
1061 keyboard->keys.size = (void *) end - keyboard->keys.data;
1062 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1063 k = wl_array_add(&keyboard->keys, sizeof *k);
1064 *k = key;
1065 }
1066
1067 if (grab == &keyboard->default_grab ||
1068 grab == &keyboard->input_method_grab) {
1069 weston_compositor_run_key_binding(compositor, seat, time, key,
1070 state);
1071 grab = keyboard->grab;
1072 }
1073
1074 grab->interface->key(grab, time, key, state);
1075
Rui Matos65196bc2013-10-10 19:44:19 +02001076 if (seat->pending_keymap &&
1077 keyboard->keys.size == 0)
1078 update_keymap(seat);
1079
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001080 if (update_state == STATE_UPDATE_AUTOMATIC) {
1081 update_modifier_state(seat,
1082 wl_display_get_serial(compositor->wl_display),
1083 key,
1084 state);
1085 }
1086}
1087
1088WL_EXPORT void
1089notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1090 wl_fixed_t x, wl_fixed_t y)
1091{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001092 if (output) {
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001093 weston_pointer_move(seat->pointer, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001094 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001095 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001096 * NULL) here, but somehow that breaks re-entry... */
1097 }
1098}
1099
1100static void
1101destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1102{
1103 struct weston_seat *ws;
1104
1105 ws = container_of(listener, struct weston_seat,
1106 saved_kbd_focus_listener);
1107
1108 ws->saved_kbd_focus = NULL;
1109}
1110
1111WL_EXPORT void
1112notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1113 enum weston_key_state_update update_state)
1114{
1115 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001116 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001117 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001118 uint32_t *k, serial;
1119
1120 serial = wl_display_next_serial(compositor->wl_display);
1121 wl_array_copy(&keyboard->keys, keys);
1122 wl_array_for_each(k, &keyboard->keys) {
1123 weston_compositor_idle_inhibit(compositor);
1124 if (update_state == STATE_UPDATE_AUTOMATIC)
1125 update_modifier_state(seat, serial, *k,
1126 WL_KEYBOARD_KEY_STATE_PRESSED);
1127 }
1128
1129 /* Run key bindings after we've updated the state. */
1130 wl_array_for_each(k, &keyboard->keys) {
1131 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1132 WL_KEYBOARD_KEY_STATE_PRESSED);
1133 }
1134
1135 surface = seat->saved_kbd_focus;
1136
1137 if (surface) {
1138 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1139 weston_keyboard_set_focus(keyboard, surface);
1140 seat->saved_kbd_focus = NULL;
1141 }
1142}
1143
1144WL_EXPORT void
1145notify_keyboard_focus_out(struct weston_seat *seat)
1146{
1147 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001148 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001149 uint32_t *k, serial;
1150
1151 serial = wl_display_next_serial(compositor->wl_display);
1152 wl_array_for_each(k, &keyboard->keys) {
1153 weston_compositor_idle_release(compositor);
1154 update_modifier_state(seat, serial, *k,
1155 WL_KEYBOARD_KEY_STATE_RELEASED);
1156 }
1157
1158 seat->modifier_state = 0;
1159
1160 if (keyboard->focus) {
1161 seat->saved_kbd_focus = keyboard->focus;
1162 seat->saved_kbd_focus_listener.notify =
1163 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001164 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001165 &seat->saved_kbd_focus_listener);
1166 }
1167
1168 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001169 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001170}
1171
Michael Fua2bb7912013-07-23 15:51:06 +08001172WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001174{
Neil Roberts96d790e2013-09-19 17:32:00 +01001175 struct wl_list *focus_resource_list;
1176
1177 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001178
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001179 if (view && seat->touch->focus &&
1180 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001184
Neil Roberts96d790e2013-09-19 17:32:00 +01001185 if (!wl_list_empty(focus_resource_list)) {
1186 move_resources(&seat->touch->resource_list,
1187 focus_resource_list);
1188 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001189
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001191 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001193 move_resources_for_client(focus_resource_list,
1194 &seat->touch->resource_list,
1195 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001196 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001198}
1199
1200/**
1201 * notify_touch - emulates button touches and notifies surfaces accordingly.
1202 *
1203 * It assumes always the correct cycle sequence until it gets here: touch_down
1204 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1205 * for sending along such order.
1206 *
1207 */
1208WL_EXPORT void
1209notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1210 wl_fixed_t x, wl_fixed_t y, int touch_type)
1211{
1212 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001213 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001214 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216 wl_fixed_t sx, sy;
1217
1218 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001219 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1220 touch->grab_x = x;
1221 touch->grab_y = y;
1222 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001223
1224 switch (touch_type) {
1225 case WL_TOUCH_DOWN:
1226 weston_compositor_idle_inhibit(ec);
1227
1228 seat->num_tp++;
1229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 /* the first finger down picks the view, and all further go
1231 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232 * until all touch points are up again. */
1233 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1235 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001236 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 ev = touch->focus;
1238 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001239 } else {
1240 /* Unexpected condition: We have non-initial touch but
1241 * there is no focused surface.
1242 */
1243 weston_log("touch event received with %d points down"
1244 "but no surface focused\n", seat->num_tp);
1245 return;
1246 }
1247
1248 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001249 if (seat->num_tp == 1) {
1250 touch->grab_serial =
1251 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001252 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001253 touch->grab_time = time;
1254 touch->grab_x = x;
1255 touch->grab_y = y;
1256 }
1257
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001258 break;
1259 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 ev = touch->focus;
1261 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001262 break;
1263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001265 grab->interface->motion(grab, time, touch_id, sx, sy);
1266 break;
1267 case WL_TOUCH_UP:
1268 weston_compositor_idle_release(ec);
1269 seat->num_tp--;
1270
1271 grab->interface->up(grab, time, touch_id);
1272 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001273 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274 break;
1275 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001276
1277 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001278}
1279
1280static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001281pointer_cursor_surface_configure(struct weston_surface *es,
1282 int32_t dx, int32_t dy, int32_t width, int32_t height)
1283{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001284 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001285 int x, y;
1286
1287 if (width == 0)
1288 return;
1289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001292 pointer->hotspot_x -= dx;
1293 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001294
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001295 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1296 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001299
1300 empty_region(&es->pending.input);
1301
1302 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 wl_list_insert(&es->compositor->cursor_layer.view_list,
1304 &pointer->sprite->layer_link);
1305 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001306 }
1307}
1308
1309static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001310pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1311 uint32_t serial, struct wl_resource *surface_resource,
1312 int32_t x, int32_t y)
1313{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001314 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001315 struct weston_surface *surface = NULL;
1316
1317 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001318 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001319
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001320 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001321 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001323 black_surface used in shell.c for fullscreen don't have
1324 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001326 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001329 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001330 return;
1331
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001334 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001335 WL_DISPLAY_ERROR_INVALID_OBJECT,
1336 "surface->configure already "
1337 "set");
1338 return;
1339 }
1340 }
1341
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001342 if (pointer->sprite)
1343 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001344
1345 if (!surface)
1346 return;
1347
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001348 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001349 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001350
1351 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001352 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001354 pointer->hotspot_x = x;
1355 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001356
1357 if (surface->buffer_ref.buffer)
1358 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1359 weston_surface_buffer_height(surface));
1360}
1361
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001362static void
1363pointer_release(struct wl_client *client, struct wl_resource *resource)
1364{
1365 wl_resource_destroy(resource);
1366}
1367
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001368static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001369 pointer_set_cursor,
1370 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001371};
1372
1373static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001374seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1375 uint32_t id)
1376{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001377 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001378 struct wl_resource *cr;
1379
Kristian Høgsberge3148752013-05-06 23:19:49 -04001380 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001381 return;
1382
Jason Ekstranda85118c2013-06-27 20:17:02 -05001383 cr = wl_resource_create(client, &wl_pointer_interface,
1384 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001385 if (cr == NULL) {
1386 wl_client_post_no_memory(client);
1387 return;
1388 }
1389
Neil Roberts96d790e2013-09-19 17:32:00 +01001390 /* May be moved to focused list later by either
1391 * weston_pointer_set_focus or directly if this client is already
1392 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001393 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001394 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1395 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001396
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1398 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001399 wl_fixed_t sx, sy;
1400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_from_global_fixed(seat->pointer->focus,
1402 seat->pointer->x,
1403 seat->pointer->y,
1404 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001405
1406 wl_list_remove(wl_resource_get_link(cr));
1407 wl_list_insert(&seat->pointer->focus_resource_list,
1408 wl_resource_get_link(cr));
1409 wl_pointer_send_enter(cr,
1410 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001412 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001413 }
1414}
1415
1416static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001417keyboard_release(struct wl_client *client, struct wl_resource *resource)
1418{
1419 wl_resource_destroy(resource);
1420}
1421
1422static const struct wl_keyboard_interface keyboard_interface = {
1423 keyboard_release
1424};
1425
Neil Roberts96d790e2013-09-19 17:32:00 +01001426static int
1427should_send_modifiers_to_client(struct weston_seat *seat,
1428 struct wl_client *client)
1429{
1430 if (seat->keyboard &&
1431 seat->keyboard->focus &&
1432 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1433 return 1;
1434
1435 if (seat->pointer &&
1436 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001438 return 1;
1439
1440 return 0;
1441}
1442
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001443static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001444seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1445 uint32_t id)
1446{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001447 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001448 struct wl_resource *cr;
1449
Kristian Høgsberge3148752013-05-06 23:19:49 -04001450 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001451 return;
1452
Jason Ekstranda85118c2013-06-27 20:17:02 -05001453 cr = wl_resource_create(client, &wl_keyboard_interface,
1454 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001455 if (cr == NULL) {
1456 wl_client_post_no_memory(client);
1457 return;
1458 }
1459
Neil Roberts96d790e2013-09-19 17:32:00 +01001460 /* May be moved to focused list later by either
1461 * weston_keyboard_set_focus or directly if this client is already
1462 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001463 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001464 wl_resource_set_implementation(cr, &keyboard_interface,
1465 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466
Matt Roper01a92732013-06-24 16:52:44 +01001467 if (seat->compositor->use_xkbcommon) {
1468 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001469 seat->xkb_info->keymap_fd,
1470 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001471 } else {
1472 int null_fd = open("/dev/null", O_RDONLY);
1473 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1474 null_fd,
1475 0);
1476 close(null_fd);
1477 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001478
Neil Roberts96d790e2013-09-19 17:32:00 +01001479 if (should_send_modifiers_to_client(seat, client)) {
1480 send_modifiers_to_resource(seat->keyboard,
1481 cr,
1482 seat->keyboard->focus_serial);
1483 }
1484
Kristian Høgsberge3148752013-05-06 23:19:49 -04001485 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001486 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001487 struct weston_surface *surface =
1488 (struct weston_surface *) seat->keyboard->focus;
1489
1490 wl_list_remove(wl_resource_get_link(cr));
1491 wl_list_insert(&seat->keyboard->focus_resource_list,
1492 wl_resource_get_link(cr));
1493 wl_keyboard_send_enter(cr,
1494 seat->keyboard->focus_serial,
1495 surface->resource,
1496 &seat->keyboard->keys);
1497
1498 /* If this is the first keyboard resource for this
1499 * client... */
1500 if (seat->keyboard->focus_resource_list.prev ==
1501 wl_resource_get_link(cr))
1502 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001503 }
1504}
1505
1506static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001507touch_release(struct wl_client *client, struct wl_resource *resource)
1508{
1509 wl_resource_destroy(resource);
1510}
1511
1512static const struct wl_touch_interface touch_interface = {
1513 touch_release
1514};
1515
1516static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001517seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1518 uint32_t id)
1519{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001520 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001521 struct wl_resource *cr;
1522
Kristian Høgsberge3148752013-05-06 23:19:49 -04001523 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001524 return;
1525
Jason Ekstranda85118c2013-06-27 20:17:02 -05001526 cr = wl_resource_create(client, &wl_touch_interface,
1527 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001528 if (cr == NULL) {
1529 wl_client_post_no_memory(client);
1530 return;
1531 }
1532
Neil Roberts96d790e2013-09-19 17:32:00 +01001533 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001535 wl_list_insert(&seat->touch->resource_list,
1536 wl_resource_get_link(cr));
1537 } else {
1538 wl_list_insert(&seat->touch->focus_resource_list,
1539 wl_resource_get_link(cr));
1540 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001541 wl_resource_set_implementation(cr, &touch_interface,
1542 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001543}
1544
1545static const struct wl_seat_interface seat_interface = {
1546 seat_get_pointer,
1547 seat_get_keyboard,
1548 seat_get_touch,
1549};
1550
1551static void
1552bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1553{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001554 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001555 struct wl_resource *resource;
1556 enum wl_seat_capability caps = 0;
1557
Jason Ekstranda85118c2013-06-27 20:17:02 -05001558 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001559 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001560 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001561 wl_resource_set_implementation(resource, &seat_interface, data,
1562 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001563
1564 if (seat->pointer)
1565 caps |= WL_SEAT_CAPABILITY_POINTER;
1566 if (seat->keyboard)
1567 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1568 if (seat->touch)
1569 caps |= WL_SEAT_CAPABILITY_TOUCH;
1570
1571 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001572 if (version >= 2)
1573 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001574}
1575
Rob Bradford382ff462013-06-24 16:52:45 +01001576#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001577int
1578weston_compositor_xkb_init(struct weston_compositor *ec,
1579 struct xkb_rule_names *names)
1580{
Rob Bradford382ff462013-06-24 16:52:45 +01001581 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001582
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001583 if (ec->xkb_context == NULL) {
1584 ec->xkb_context = xkb_context_new(0);
1585 if (ec->xkb_context == NULL) {
1586 weston_log("failed to create XKB context\n");
1587 return -1;
1588 }
1589 }
1590
1591 if (names)
1592 ec->xkb_names = *names;
1593 if (!ec->xkb_names.rules)
1594 ec->xkb_names.rules = strdup("evdev");
1595 if (!ec->xkb_names.model)
1596 ec->xkb_names.model = strdup("pc105");
1597 if (!ec->xkb_names.layout)
1598 ec->xkb_names.layout = strdup("us");
1599
1600 return 0;
1601}
1602
Stefan Schmidtfda26522013-09-17 10:54:09 +01001603static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001604weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001606 if (--xkb_info->ref_count > 0)
1607 return;
1608
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001609 if (xkb_info->keymap)
1610 xkb_map_unref(xkb_info->keymap);
1611
1612 if (xkb_info->keymap_area)
1613 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1614 if (xkb_info->keymap_fd >= 0)
1615 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001616 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001617}
1618
1619void
1620weston_compositor_xkb_destroy(struct weston_compositor *ec)
1621{
Matt Roper01a92732013-06-24 16:52:44 +01001622 /*
1623 * If we're operating in raw keyboard mode, we never initialized
1624 * libxkbcommon so there's no cleanup to do either.
1625 */
1626 if (!ec->use_xkbcommon)
1627 return;
1628
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001629 free((char *) ec->xkb_names.rules);
1630 free((char *) ec->xkb_names.model);
1631 free((char *) ec->xkb_names.layout);
1632 free((char *) ec->xkb_names.variant);
1633 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001634
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001635 if (ec->xkb_info)
1636 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001637 xkb_context_unref(ec->xkb_context);
1638}
1639
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001640static struct weston_xkb_info *
1641weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001642{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001643 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1644 if (xkb_info == NULL)
1645 return NULL;
1646
1647 xkb_info->keymap = xkb_map_ref(keymap);
1648 xkb_info->ref_count = 1;
1649
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001650 char *keymap_str;
1651
1652 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1653 XKB_MOD_NAME_SHIFT);
1654 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1655 XKB_MOD_NAME_CAPS);
1656 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1657 XKB_MOD_NAME_CTRL);
1658 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1659 XKB_MOD_NAME_ALT);
1660 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1661 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1662 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1663 XKB_MOD_NAME_LOGO);
1664 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1665
1666 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1667 XKB_LED_NAME_NUM);
1668 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1669 XKB_LED_NAME_CAPS);
1670 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1671 XKB_LED_NAME_SCROLL);
1672
1673 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1674 if (keymap_str == NULL) {
1675 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001676 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677 }
1678 xkb_info->keymap_size = strlen(keymap_str) + 1;
1679
1680 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1681 if (xkb_info->keymap_fd < 0) {
1682 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1683 (unsigned long) xkb_info->keymap_size);
1684 goto err_keymap_str;
1685 }
1686
1687 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1688 PROT_READ | PROT_WRITE,
1689 MAP_SHARED, xkb_info->keymap_fd, 0);
1690 if (xkb_info->keymap_area == MAP_FAILED) {
1691 weston_log("failed to mmap() %lu bytes\n",
1692 (unsigned long) xkb_info->keymap_size);
1693 goto err_dev_zero;
1694 }
1695 strcpy(xkb_info->keymap_area, keymap_str);
1696 free(keymap_str);
1697
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001698 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699
1700err_dev_zero:
1701 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001702err_keymap_str:
1703 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001704err_keymap:
1705 xkb_map_unref(xkb_info->keymap);
1706 free(xkb_info);
1707 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708}
1709
1710static int
1711weston_compositor_build_global_keymap(struct weston_compositor *ec)
1712{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001713 struct xkb_keymap *keymap;
1714
1715 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001716 return 0;
1717
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001718 keymap = xkb_map_new_from_names(ec->xkb_context,
1719 &ec->xkb_names,
1720 0);
1721 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722 weston_log("failed to compile global XKB keymap\n");
1723 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1724 "options %s\n",
1725 ec->xkb_names.rules, ec->xkb_names.model,
1726 ec->xkb_names.layout, ec->xkb_names.variant,
1727 ec->xkb_names.options);
1728 return -1;
1729 }
1730
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001731 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001732 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001733 return -1;
1734
1735 return 0;
1736}
Rob Bradford382ff462013-06-24 16:52:45 +01001737#else
1738int
1739weston_compositor_xkb_init(struct weston_compositor *ec,
1740 struct xkb_rule_names *names)
1741{
1742 return 0;
1743}
1744
1745void
1746weston_compositor_xkb_destroy(struct weston_compositor *ec)
1747{
1748}
1749#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001750
Rui Matos65196bc2013-10-10 19:44:19 +02001751WL_EXPORT void
1752weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1753{
1754 if (!seat->keyboard || !keymap)
1755 return;
1756
1757#ifdef ENABLE_XKBCOMMON
1758 if (!seat->compositor->use_xkbcommon)
1759 return;
1760
1761 xkb_keymap_unref(seat->pending_keymap);
1762 seat->pending_keymap = xkb_keymap_ref(keymap);
1763
1764 if (seat->keyboard->keys.size == 0)
1765 update_keymap(seat);
1766#endif
1767}
1768
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001769WL_EXPORT int
1770weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1771{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001772 struct weston_keyboard *keyboard;
1773
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001774 if (seat->keyboard) {
1775 seat->keyboard_device_count += 1;
1776 if (seat->keyboard_device_count == 1)
1777 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001779 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001780
Rob Bradford382ff462013-06-24 16:52:45 +01001781#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001782 if (seat->compositor->use_xkbcommon) {
1783 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001784 seat->xkb_info = weston_xkb_info_create(keymap);
1785 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001786 return -1;
1787 } else {
1788 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1789 return -1;
1790 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001791 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001792 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001794 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001795 if (seat->xkb_state.state == NULL) {
1796 weston_log("failed to initialise XKB state\n");
1797 return -1;
1798 }
1799
1800 seat->xkb_state.leds = 0;
1801 }
Rob Bradford382ff462013-06-24 16:52:45 +01001802#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001803
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001804 keyboard = weston_keyboard_create();
1805 if (keyboard == NULL) {
1806 weston_log("failed to allocate weston keyboard struct\n");
1807 return -1;
1808 }
1809
1810 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001811 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001812 keyboard->seat = seat;
1813
1814 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001815
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816 return 0;
1817}
1818
1819WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001820weston_seat_release_keyboard(struct weston_seat *seat)
1821{
1822 seat->keyboard_device_count--;
1823 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001824 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001825 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001826 seat_send_updated_caps(seat);
1827 }
1828}
1829
1830WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831weston_seat_init_pointer(struct weston_seat *seat)
1832{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001833 struct weston_pointer *pointer;
1834
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001835 if (seat->pointer) {
1836 seat->pointer_device_count += 1;
1837 if (seat->pointer_device_count == 1)
1838 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001839 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001840 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001841
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001842 pointer = weston_pointer_create();
1843 if (pointer == NULL)
1844 return;
1845
1846 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001847 seat->pointer_device_count = 1;
1848 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001849
1850 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001851}
1852
1853WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001854weston_seat_release_pointer(struct weston_seat *seat)
1855{
1856 struct weston_pointer *pointer = seat->pointer;
1857
1858 seat->pointer_device_count--;
1859 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001860 weston_pointer_set_focus(pointer, NULL,
1861 wl_fixed_from_int(0),
1862 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001863 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001864
Jonas Ådahla4932742013-10-17 23:04:07 +02001865 if (pointer->sprite)
1866 pointer_unmap_sprite(pointer);
1867
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001868 seat_send_updated_caps(seat);
1869 }
1870}
1871
1872WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001873weston_seat_init_touch(struct weston_seat *seat)
1874{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001875 struct weston_touch *touch;
1876
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001877 if (seat->touch) {
1878 seat->touch_device_count += 1;
1879 if (seat->touch_device_count == 1)
1880 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001882 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001883
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001884 touch = weston_touch_create();
1885 if (touch == NULL)
1886 return;
1887
1888 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001889 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001890 touch->seat = seat;
1891
1892 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893}
1894
1895WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001896weston_seat_release_touch(struct weston_seat *seat)
1897{
1898 seat->touch_device_count--;
1899 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001900 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001901 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001902 seat_send_updated_caps(seat);
1903 }
1904}
1905
1906WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001907weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1908 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001909{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001910 memset(seat, 0, sizeof *seat);
1911
Kristian Høgsberge3148752013-05-06 23:19:49 -04001912 seat->selection_data_source = NULL;
1913 wl_list_init(&seat->base_resource_list);
1914 wl_signal_init(&seat->selection_signal);
1915 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001916 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001917
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001918 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001919 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001920
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001921 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001922 seat->modifier_state = 0;
1923 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001924 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001925
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001926 wl_list_insert(ec->seat_list.prev, &seat->link);
1927
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001928 clipboard_create(seat);
1929
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001930 wl_signal_emit(&ec->seat_created_signal, seat);
1931}
1932
1933WL_EXPORT void
1934weston_seat_release(struct weston_seat *seat)
1935{
1936 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001937
Rob Bradford382ff462013-06-24 16:52:45 +01001938#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001939 if (seat->compositor->use_xkbcommon) {
1940 if (seat->xkb_state.state != NULL)
1941 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001942 if (seat->xkb_info)
1943 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001944 if (seat->pending_keymap)
1945 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001946 }
Rob Bradford382ff462013-06-24 16:52:45 +01001947#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001948
Kristian Høgsberge3148752013-05-06 23:19:49 -04001949 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001950 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001951 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001952 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001953 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001954 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001955
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001956 free (seat->seat_name);
1957
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001958 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001959
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001960 wl_signal_emit(&seat->destroy_signal, seat);
1961}