blob: e6b070d781b2c80b3a172c5850847c2ab5405593 [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
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700114default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400115{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400116 struct weston_pointer *pointer = grab->pointer;
117 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100118 struct wl_list *resource_list;
119 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400120
Neil Roberts96d790e2013-09-19 17:32:00 +0100121 resource_list = &pointer->focus_resource_list;
122 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 weston_view_from_global_fixed(pointer->focus,
124 pointer->x, pointer->y,
125 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +0100126 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400127 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400128}
129
130static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700131default_grab_pointer_button(struct weston_pointer_grab *grab,
132 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400133{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400134 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400135 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500136 struct weston_view *view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400137 struct wl_resource *resource;
138 uint32_t serial;
139 enum wl_pointer_button_state state = state_w;
Rob Bradford880ebc72013-07-22 17:31:38 +0100140 struct wl_display *display = compositor->wl_display;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400141 wl_fixed_t sx, sy;
Neil Roberts96d790e2013-09-19 17:32:00 +0100142 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400143
Neil Roberts96d790e2013-09-19 17:32:00 +0100144 resource_list = &pointer->focus_resource_list;
145 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400146 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100147 wl_resource_for_each(resource, resource_list)
148 wl_pointer_send_button(resource,
149 serial,
150 time,
151 button,
152 state_w);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400153 }
154
155 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400156 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157 view = weston_compositor_pick_view(compositor,
158 pointer->x, pointer->y,
159 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400160
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400162 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400163}
164
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200165static void
166default_grab_pointer_cancel(struct weston_pointer_grab *grab)
167{
168}
169
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400170static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400171 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700172 default_grab_pointer_focus,
173 default_grab_pointer_motion,
174 default_grab_pointer_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200175 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400176};
177
Kristian Høgsberge329f362013-05-06 22:19:57 -0400178static void
179default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
180 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400181{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400182 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100183 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400184 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100185 struct wl_resource *resource;
186 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400187
Neil Roberts96d790e2013-09-19 17:32:00 +0100188 resource_list = &touch->focus_resource_list;
189
190 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400191 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100192 wl_resource_for_each(resource, resource_list)
193 wl_touch_send_down(resource, serial, time,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500194 touch->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100195 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196 }
197}
198
Kristian Høgsberge329f362013-05-06 22:19:57 -0400199static void
200default_grab_touch_up(struct weston_touch_grab *grab,
201 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400202{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400203 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100204 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400205 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100206 struct wl_resource *resource;
207 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400208
Neil Roberts96d790e2013-09-19 17:32:00 +0100209 resource_list = &touch->focus_resource_list;
210
211 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400212 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100213 wl_resource_for_each(resource, resource_list)
214 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215 }
216}
217
Kristian Høgsberge329f362013-05-06 22:19:57 -0400218static void
219default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
220 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400221{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400222 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100223 struct wl_resource *resource;
224 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400225
Neil Roberts96d790e2013-09-19 17:32:00 +0100226 resource_list = &touch->focus_resource_list;
227
228 wl_resource_for_each(resource, resource_list) {
229 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400230 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400231 }
232}
233
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200234static void
235default_grab_touch_cancel(struct weston_touch_grab *grab)
236{
237}
238
Kristian Høgsberge329f362013-05-06 22:19:57 -0400239static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400240 default_grab_touch_down,
241 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200242 default_grab_touch_motion,
243 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400244};
245
246static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700247default_grab_keyboard_key(struct weston_keyboard_grab *grab,
248 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400249{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400250 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400251 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100252 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100254 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400255
Neil Roberts96d790e2013-09-19 17:32:00 +0100256 resource_list = &keyboard->focus_resource_list;
257 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400258 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100259 wl_resource_for_each(resource, resource_list)
260 wl_keyboard_send_key(resource,
261 serial,
262 time,
263 key,
264 state);
265 }
266}
267
268static void
269send_modifiers_to_resource(struct weston_keyboard *keyboard,
270 struct wl_resource *resource,
271 uint32_t serial)
272{
273 wl_keyboard_send_modifiers(resource,
274 serial,
275 keyboard->modifiers.mods_depressed,
276 keyboard->modifiers.mods_latched,
277 keyboard->modifiers.mods_locked,
278 keyboard->modifiers.group);
279}
280
281static void
282send_modifiers_to_client_in_list(struct wl_client *client,
283 struct wl_list *list,
284 uint32_t serial,
285 struct weston_keyboard *keyboard)
286{
287 struct wl_resource *resource;
288
289 wl_resource_for_each(resource, list) {
290 if (wl_resource_get_client(resource) == client)
291 send_modifiers_to_resource(keyboard,
292 resource,
293 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400294 }
295}
296
297static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400298find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400299{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400300 if (!surface)
301 return NULL;
302
Jason Ekstrand44a38632013-06-14 10:08:00 -0500303 if (!surface->resource)
304 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100305
Jason Ekstrand44a38632013-06-14 10:08:00 -0500306 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307}
308
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309static struct wl_resource *
310find_resource_for_view(struct wl_list *list, struct weston_view *view)
311{
312 if (!view)
313 return NULL;
314
315 return find_resource_for_surface(list, view->surface);
316}
317
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400318static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700319default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
320 uint32_t serial, uint32_t mods_depressed,
321 uint32_t mods_latched,
322 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400324 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100325 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
326 struct wl_resource *resource;
327 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400328
Neil Roberts96d790e2013-09-19 17:32:00 +0100329 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330
Neil Roberts96d790e2013-09-19 17:32:00 +0100331 wl_resource_for_each(resource, resource_list) {
332 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
333 mods_latched, mods_locked, group);
334 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500335 if (pointer && pointer->focus && pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100336 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100338 send_modifiers_to_client_in_list(pointer_client,
339 &keyboard->resource_list,
340 serial,
341 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400342 }
343}
344
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200345static void
346default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
347{
348}
349
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400350static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400351 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700352 default_grab_keyboard_key,
353 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200354 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400355};
356
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400357static void
358pointer_unmap_sprite(struct weston_pointer *pointer)
359{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 if (weston_surface_is_mapped(pointer->sprite->surface))
361 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400362
363 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500364 pointer->sprite->surface->configure = NULL;
365 pointer->sprite->surface->configure_private = NULL;
366 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400367 pointer->sprite = NULL;
368}
369
370static void
371pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
372{
373 struct weston_pointer *pointer =
374 container_of(listener, struct weston_pointer,
375 sprite_destroy_listener);
376
377 pointer->sprite = NULL;
378}
379
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400380WL_EXPORT struct weston_pointer *
381weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400383 struct weston_pointer *pointer;
384
Peter Huttererf3d62272013-08-08 11:57:05 +1000385 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400386 if (pointer == NULL)
387 return NULL;
388
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400389 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100390 wl_list_init(&pointer->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400391 pointer->default_grab.interface = &default_pointer_grab_interface;
392 pointer->default_grab.pointer = pointer;
393 pointer->grab = &pointer->default_grab;
394 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100395 wl_signal_init(&pointer->motion_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400397 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
398
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399 /* FIXME: Pick better co-ords. */
400 pointer->x = wl_fixed_from_int(100);
401 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400402
403 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404}
405
406WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400407weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400409 if (pointer->sprite)
410 pointer_unmap_sprite(pointer);
411
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400412 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100413
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400414 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400417WL_EXPORT struct weston_keyboard *
418weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400419{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400420 struct weston_keyboard *keyboard;
421
Peter Huttererf3d62272013-08-08 11:57:05 +1000422 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400423 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100424 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400425
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400426 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100427 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400429 keyboard->default_grab.interface = &default_keyboard_grab_interface;
430 keyboard->default_grab.keyboard = keyboard;
431 keyboard->grab = &keyboard->default_grab;
432 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400433
434 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435}
436
437WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400438weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439{
440 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100441
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400443 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400444}
445
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400446WL_EXPORT struct weston_touch *
447weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400448{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400449 struct weston_touch *touch;
450
Peter Huttererf3d62272013-08-08 11:57:05 +1000451 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400452 if (touch == NULL)
453 return NULL;
454
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100456 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457 touch->default_grab.interface = &default_touch_grab_interface;
458 touch->default_grab.touch = touch;
459 touch->grab = &touch->default_grab;
460 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400461
462 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400463}
464
465WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400466weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467{
468 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100469
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400470 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400471}
472
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400473static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400474seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400476 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100477 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200479 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400480 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200481 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200483 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400484 caps |= WL_SEAT_CAPABILITY_TOUCH;
485
Rob Bradford6e737f52013-09-06 17:48:19 +0100486 wl_resource_for_each(resource, &seat->base_resource_list) {
487 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500488 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400489}
490
491WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400492weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500493 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400494 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400495{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400496 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100497 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100498 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400499 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100500 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 int different_surface = 0;
502
503 if ((!pointer->focus && view) ||
504 (pointer->focus && !view) ||
505 (pointer->focus && pointer->focus->surface != view->surface))
506 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400507
Neil Roberts96d790e2013-09-19 17:32:00 +0100508 focus_resource_list = &pointer->focus_resource_list;
509
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400511 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100512 wl_resource_for_each(resource, focus_resource_list) {
513 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100515 }
516
517 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400518 }
519
Jason Ekstranda7af7042013-10-12 22:38:11 -0500520 if (find_resource_for_view(&pointer->resource_list, view) &&
521 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100522 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100524
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400525 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100526
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700528 send_modifiers_to_client_in_list(surface_client,
529 &kbd->resource_list,
530 serial,
531 kbd);
532
Neil Roberts96d790e2013-09-19 17:32:00 +0100533 move_resources_for_client(focus_resource_list,
534 &pointer->resource_list,
535 surface_client);
536
537 wl_resource_for_each(resource, focus_resource_list) {
538 wl_pointer_send_enter(resource,
539 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500540 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100541 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400542 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100543
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400544 pointer->focus_serial = serial;
545 }
546
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400548 wl_signal_emit(&pointer->focus_signal, pointer);
549}
550
Neil Roberts96d790e2013-09-19 17:32:00 +0100551static void
552send_enter_to_resource_list(struct wl_list *list,
553 struct weston_keyboard *keyboard,
554 struct weston_surface *surface,
555 uint32_t serial)
556{
557 struct wl_resource *resource;
558
559 wl_resource_for_each(resource, list) {
560 send_modifiers_to_resource(keyboard, resource, serial);
561 wl_keyboard_send_enter(resource, serial,
562 surface->resource,
563 &keyboard->keys);
564 }
565}
566
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400567WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400568weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400569 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400570{
571 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100572 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400573 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100574 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400575
Neil Roberts96d790e2013-09-19 17:32:00 +0100576 focus_resource_list = &keyboard->focus_resource_list;
577
578 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400579 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100580 wl_resource_for_each(resource, focus_resource_list) {
581 wl_keyboard_send_leave(resource, serial,
582 keyboard->focus->resource);
583 }
584 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400585 }
586
Neil Roberts96d790e2013-09-19 17:32:00 +0100587 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
588 keyboard->focus != surface) {
589 struct wl_client *surface_client =
590 wl_resource_get_client(surface->resource);
591
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400592 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100593
594 move_resources_for_client(focus_resource_list,
595 &keyboard->resource_list,
596 surface_client);
597 send_enter_to_resource_list(focus_resource_list,
598 keyboard,
599 surface,
600 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400601 keyboard->focus_serial = serial;
602 }
603
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400604 keyboard->focus = surface;
605 wl_signal_emit(&keyboard->focus_signal, keyboard);
606}
607
608WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400609weston_keyboard_start_grab(struct weston_keyboard *keyboard,
610 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400611{
612 keyboard->grab = grab;
613 grab->keyboard = keyboard;
614
615 /* XXX focus? */
616}
617
618WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400619weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620{
621 keyboard->grab = &keyboard->default_grab;
622}
623
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200624static void
625weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
626{
627 keyboard->grab->interface->cancel(keyboard->grab);
628}
629
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400630WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400631weston_pointer_start_grab(struct weston_pointer *pointer,
632 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400636 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400637}
638
639WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400640weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400642 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400643 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400644}
645
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200646static void
647weston_pointer_cancel_grab(struct weston_pointer *pointer)
648{
649 pointer->grab->interface->cancel(pointer->grab);
650}
651
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400652WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400653weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400654{
655 touch->grab = grab;
656 grab->touch = touch;
657}
658
659WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400660weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400661{
662 touch->grab = &touch->default_grab;
663}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400664
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200665static void
666weston_touch_cancel_grab(struct weston_touch *touch)
667{
668 touch->grab->interface->cancel(touch->grab);
669}
670
Rob Bradford806d8c02013-06-25 18:56:41 +0100671WL_EXPORT void
672weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400673{
Rob Bradford806d8c02013-06-25 18:56:41 +0100674 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400675 struct weston_output *output, *prev = NULL;
676 int x, y, old_x, old_y, valid = 0;
677
678 x = wl_fixed_to_int(*fx);
679 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100680 old_x = wl_fixed_to_int(pointer->x);
681 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400682
683 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100684 if (pointer->seat->output && pointer->seat->output != output)
685 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400686 if (pixman_region32_contains_point(&output->region,
687 x, y, NULL))
688 valid = 1;
689 if (pixman_region32_contains_point(&output->region,
690 old_x, old_y, NULL))
691 prev = output;
692 }
693
Rob Bradford66bd9f52013-06-25 18:56:42 +0100694 if (!prev)
695 prev = pointer->seat->output;
696
697 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400698 if (x < prev->x)
699 *fx = wl_fixed_from_int(prev->x);
700 else if (x >= prev->x + prev->width)
701 *fx = wl_fixed_from_int(prev->x +
702 prev->width - 1);
703 if (y < prev->y)
704 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200705 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400706 *fy = wl_fixed_from_int(prev->y +
707 prev->height - 1);
708 }
709}
710
711/* Takes absolute values */
712static void
713move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
714{
715 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400716 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400717 struct weston_output *output;
718 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
728 wl_list_for_each(output, &ec->output_list, link)
729 if (output->zoom.active &&
730 pixman_region32_contains_point(&output->region,
731 ix, iy, NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500732 weston_output_update_zoom(output);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400733
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400734 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 weston_view_set_position(pointer->sprite,
736 ix - pointer->hotspot_x,
737 iy - pointer->hotspot_y);
738 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400739 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100740
741 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400742}
743
744WL_EXPORT void
745notify_motion(struct weston_seat *seat,
746 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
747{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400748 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400749 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400750
751 weston_compositor_wake(ec);
752
753 move_pointer(seat, pointer->x + dx, pointer->y + dy);
754
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400755 pointer->grab->interface->focus(pointer->grab);
756 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400757}
758
759WL_EXPORT void
760notify_motion_absolute(struct weston_seat *seat,
761 uint32_t time, wl_fixed_t x, wl_fixed_t y)
762{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400763 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400764 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400765
766 weston_compositor_wake(ec);
767
768 move_pointer(seat, x, y);
769
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400770 pointer->grab->interface->focus(pointer->grab);
771 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400772}
773
774WL_EXPORT void
775weston_surface_activate(struct weston_surface *surface,
776 struct weston_seat *seat)
777{
778 struct weston_compositor *compositor = seat->compositor;
779
Kristian Høgsberge3148752013-05-06 23:19:49 -0400780 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400781 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400782 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400783 }
784
785 wl_signal_emit(&compositor->activate_signal, surface);
786}
787
788WL_EXPORT void
789notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
790 enum wl_pointer_button_state state)
791{
792 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400793 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400794 struct weston_surface *focus =
795 (struct weston_surface *) pointer->focus;
796 uint32_t serial = wl_display_next_serial(compositor->wl_display);
797
798 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
799 if (compositor->ping_handler && focus)
800 compositor->ping_handler(focus, serial);
801 weston_compositor_idle_inhibit(compositor);
802 if (pointer->button_count == 0) {
803 pointer->grab_button = button;
804 pointer->grab_time = time;
805 pointer->grab_x = pointer->x;
806 pointer->grab_y = pointer->y;
807 }
808 pointer->button_count++;
809 } else {
810 weston_compositor_idle_release(compositor);
811 pointer->button_count--;
812 }
813
814 weston_compositor_run_button_binding(compositor, seat, time, button,
815 state);
816
817 pointer->grab->interface->button(pointer->grab, time, button, state);
818
819 if (pointer->button_count == 1)
820 pointer->grab_serial =
821 wl_display_get_serial(compositor->wl_display);
822}
823
824WL_EXPORT void
825notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
826 wl_fixed_t value)
827{
828 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400829 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400830 struct weston_surface *focus =
831 (struct weston_surface *) pointer->focus;
832 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100833 struct wl_resource *resource;
834 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400835
836 if (compositor->ping_handler && focus)
837 compositor->ping_handler(focus, serial);
838
839 weston_compositor_wake(compositor);
840
841 if (!value)
842 return;
843
844 if (weston_compositor_run_axis_binding(compositor, seat,
845 time, axis, value))
846 return;
847
Neil Roberts96d790e2013-09-19 17:32:00 +0100848 resource_list = &pointer->focus_resource_list;
849 wl_resource_for_each(resource, resource_list)
850 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400851 value);
852}
853
Rob Bradford382ff462013-06-24 16:52:45 +0100854#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400855WL_EXPORT void
856notify_modifiers(struct weston_seat *seat, uint32_t serial)
857{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400858 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400859 struct weston_keyboard_grab *grab = keyboard->grab;
860 uint32_t mods_depressed, mods_latched, mods_locked, group;
861 uint32_t mods_lookup;
862 enum weston_led leds = 0;
863 int changed = 0;
864
865 /* Serialize and update our internal state, checking to see if it's
866 * different to the previous state. */
867 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
868 XKB_STATE_DEPRESSED);
869 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
870 XKB_STATE_LATCHED);
871 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
872 XKB_STATE_LOCKED);
873 group = xkb_state_serialize_group(seat->xkb_state.state,
874 XKB_STATE_EFFECTIVE);
875
Kristian Høgsberge3148752013-05-06 23:19:49 -0400876 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
877 mods_latched != seat->keyboard->modifiers.mods_latched ||
878 mods_locked != seat->keyboard->modifiers.mods_locked ||
879 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400880 changed = 1;
881
Kristian Høgsberge3148752013-05-06 23:19:49 -0400882 seat->keyboard->modifiers.mods_depressed = mods_depressed;
883 seat->keyboard->modifiers.mods_latched = mods_latched;
884 seat->keyboard->modifiers.mods_locked = mods_locked;
885 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400886
887 /* And update the modifier_state for bindings. */
888 mods_lookup = mods_depressed | mods_latched;
889 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000890 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400891 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000892 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400893 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000894 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400895 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000896 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400897 seat->modifier_state |= MODIFIER_SHIFT;
898
899 /* Finally, notify the compositor that LEDs have changed. */
900 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000901 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400902 leds |= LED_NUM_LOCK;
903 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000904 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400905 leds |= LED_CAPS_LOCK;
906 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000907 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400908 leds |= LED_SCROLL_LOCK;
909 if (leds != seat->xkb_state.leds && seat->led_update)
910 seat->led_update(seat, leds);
911 seat->xkb_state.leds = leds;
912
913 if (changed) {
914 grab->interface->modifiers(grab,
915 serial,
916 keyboard->modifiers.mods_depressed,
917 keyboard->modifiers.mods_latched,
918 keyboard->modifiers.mods_locked,
919 keyboard->modifiers.group);
920 }
921}
922
923static void
924update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
925 enum wl_keyboard_key_state state)
926{
927 enum xkb_key_direction direction;
928
Matt Roper01a92732013-06-24 16:52:44 +0100929 /* Keyboard modifiers don't exist in raw keyboard mode */
930 if (!seat->compositor->use_xkbcommon)
931 return;
932
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400933 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
934 direction = XKB_KEY_DOWN;
935 else
936 direction = XKB_KEY_UP;
937
938 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
939 * broken keycode system, which starts at 8. */
940 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
941
942 notify_modifiers(seat, serial);
943}
Rui Matos65196bc2013-10-10 19:44:19 +0200944
945static void
946send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
947{
948 wl_keyboard_send_keymap(resource,
949 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
950 xkb_info->keymap_fd,
951 xkb_info->keymap_size);
952}
953
954static void
955send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
956{
957 wl_keyboard_send_modifiers(resource, serial,
958 keyboard->modifiers.mods_depressed,
959 keyboard->modifiers.mods_latched,
960 keyboard->modifiers.mods_locked,
961 keyboard->modifiers.group);
962}
963
964static struct weston_xkb_info *
965weston_xkb_info_create(struct xkb_keymap *keymap);
966static void
967weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
968
969static void
970update_keymap(struct weston_seat *seat)
971{
972 struct wl_resource *resource;
973 struct weston_xkb_info *xkb_info;
974 struct xkb_state *state;
975 xkb_mod_mask_t latched_mods;
976 xkb_mod_mask_t locked_mods;
977
978 xkb_info = weston_xkb_info_create(seat->pending_keymap);
979
980 xkb_keymap_unref(seat->pending_keymap);
981 seat->pending_keymap = NULL;
982
983 if (!xkb_info) {
984 weston_log("failed to create XKB info\n");
985 return;
986 }
987
988 state = xkb_state_new(xkb_info->keymap);
989 if (!state) {
990 weston_log("failed to initialise XKB state\n");
991 weston_xkb_info_destroy(xkb_info);
992 return;
993 }
994
995 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
996 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
997 xkb_state_update_mask(state,
998 0, /* depressed */
999 latched_mods,
1000 locked_mods,
1001 0, 0, 0);
1002
1003 weston_xkb_info_destroy(seat->xkb_info);
1004 seat->xkb_info = xkb_info;
1005
1006 xkb_state_unref(seat->xkb_state.state);
1007 seat->xkb_state.state = state;
1008
1009 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1010 send_keymap(resource, xkb_info);
1011 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1012 send_keymap(resource, xkb_info);
1013
1014 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1015
1016 if (!latched_mods && !locked_mods)
1017 return;
1018
1019 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1020 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1021 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1022 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1023}
Rob Bradford382ff462013-06-24 16:52:45 +01001024#else
1025WL_EXPORT void
1026notify_modifiers(struct weston_seat *seat, uint32_t serial)
1027{
1028}
1029
1030static void
1031update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1032 enum wl_keyboard_key_state state)
1033{
1034}
Rui Matos65196bc2013-10-10 19:44:19 +02001035
1036static void
1037update_keymap(struct weston_seat *seat)
1038{
1039}
Rob Bradford382ff462013-06-24 16:52:45 +01001040#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001041
1042WL_EXPORT void
1043notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1044 enum wl_keyboard_key_state state,
1045 enum weston_key_state_update update_state)
1046{
1047 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001048 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001050 struct weston_keyboard_grab *grab = keyboard->grab;
1051 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1052 uint32_t *k, *end;
1053
1054 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1055 if (compositor->ping_handler && focus)
1056 compositor->ping_handler(focus, serial);
1057
1058 weston_compositor_idle_inhibit(compositor);
1059 keyboard->grab_key = key;
1060 keyboard->grab_time = time;
1061 } else {
1062 weston_compositor_idle_release(compositor);
1063 }
1064
1065 end = keyboard->keys.data + keyboard->keys.size;
1066 for (k = keyboard->keys.data; k < end; k++) {
1067 if (*k == key) {
1068 /* Ignore server-generated repeats. */
1069 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1070 return;
1071 *k = *--end;
1072 }
1073 }
1074 keyboard->keys.size = (void *) end - keyboard->keys.data;
1075 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1076 k = wl_array_add(&keyboard->keys, sizeof *k);
1077 *k = key;
1078 }
1079
1080 if (grab == &keyboard->default_grab ||
1081 grab == &keyboard->input_method_grab) {
1082 weston_compositor_run_key_binding(compositor, seat, time, key,
1083 state);
1084 grab = keyboard->grab;
1085 }
1086
1087 grab->interface->key(grab, time, key, state);
1088
Rui Matos65196bc2013-10-10 19:44:19 +02001089 if (seat->pending_keymap &&
1090 keyboard->keys.size == 0)
1091 update_keymap(seat);
1092
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001093 if (update_state == STATE_UPDATE_AUTOMATIC) {
1094 update_modifier_state(seat,
1095 wl_display_get_serial(compositor->wl_display),
1096 key,
1097 state);
1098 }
1099}
1100
1101WL_EXPORT void
1102notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1103 wl_fixed_t x, wl_fixed_t y)
1104{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001105 if (output) {
1106 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001107 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001108 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001109 * NULL) here, but somehow that breaks re-entry... */
1110 }
1111}
1112
1113static void
1114destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1115{
1116 struct weston_seat *ws;
1117
1118 ws = container_of(listener, struct weston_seat,
1119 saved_kbd_focus_listener);
1120
1121 ws->saved_kbd_focus = NULL;
1122}
1123
1124WL_EXPORT void
1125notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1126 enum weston_key_state_update update_state)
1127{
1128 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001129 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001130 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001131 uint32_t *k, serial;
1132
1133 serial = wl_display_next_serial(compositor->wl_display);
1134 wl_array_copy(&keyboard->keys, keys);
1135 wl_array_for_each(k, &keyboard->keys) {
1136 weston_compositor_idle_inhibit(compositor);
1137 if (update_state == STATE_UPDATE_AUTOMATIC)
1138 update_modifier_state(seat, serial, *k,
1139 WL_KEYBOARD_KEY_STATE_PRESSED);
1140 }
1141
1142 /* Run key bindings after we've updated the state. */
1143 wl_array_for_each(k, &keyboard->keys) {
1144 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1145 WL_KEYBOARD_KEY_STATE_PRESSED);
1146 }
1147
1148 surface = seat->saved_kbd_focus;
1149
1150 if (surface) {
1151 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1152 weston_keyboard_set_focus(keyboard, surface);
1153 seat->saved_kbd_focus = NULL;
1154 }
1155}
1156
1157WL_EXPORT void
1158notify_keyboard_focus_out(struct weston_seat *seat)
1159{
1160 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001161 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001162 uint32_t *k, serial;
1163
1164 serial = wl_display_next_serial(compositor->wl_display);
1165 wl_array_for_each(k, &keyboard->keys) {
1166 weston_compositor_idle_release(compositor);
1167 update_modifier_state(seat, serial, *k,
1168 WL_KEYBOARD_KEY_STATE_RELEASED);
1169 }
1170
1171 seat->modifier_state = 0;
1172
1173 if (keyboard->focus) {
1174 seat->saved_kbd_focus = keyboard->focus;
1175 seat->saved_kbd_focus_listener.notify =
1176 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001177 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001178 &seat->saved_kbd_focus_listener);
1179 }
1180
1181 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001182 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001183}
1184
Michael Fua2bb7912013-07-23 15:51:06 +08001185WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187{
Neil Roberts96d790e2013-09-19 17:32:00 +01001188 struct wl_list *focus_resource_list;
1189
1190 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001191
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001192 if (view && seat->touch->focus &&
1193 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001195 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001197
Neil Roberts96d790e2013-09-19 17:32:00 +01001198 if (!wl_list_empty(focus_resource_list)) {
1199 move_resources(&seat->touch->resource_list,
1200 focus_resource_list);
1201 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001204 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001206 move_resources_for_client(focus_resource_list,
1207 &seat->touch->resource_list,
1208 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001211}
1212
1213/**
1214 * notify_touch - emulates button touches and notifies surfaces accordingly.
1215 *
1216 * It assumes always the correct cycle sequence until it gets here: touch_down
1217 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1218 * for sending along such order.
1219 *
1220 */
1221WL_EXPORT void
1222notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1223 wl_fixed_t x, wl_fixed_t y, int touch_type)
1224{
1225 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001226 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001227 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001229 wl_fixed_t sx, sy;
1230
1231 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001232 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1233 touch->grab_x = x;
1234 touch->grab_y = y;
1235 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001236
1237 switch (touch_type) {
1238 case WL_TOUCH_DOWN:
1239 weston_compositor_idle_inhibit(ec);
1240
1241 seat->num_tp++;
1242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 /* the first finger down picks the view, and all further go
1244 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001245 * until all touch points are up again. */
1246 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1248 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001249 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 ev = touch->focus;
1251 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001252 } else {
1253 /* Unexpected condition: We have non-initial touch but
1254 * there is no focused surface.
1255 */
1256 weston_log("touch event received with %d points down"
1257 "but no surface focused\n", seat->num_tp);
1258 return;
1259 }
1260
1261 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001262 if (seat->num_tp == 1) {
1263 touch->grab_serial =
1264 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001265 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001266 touch->grab_time = time;
1267 touch->grab_x = x;
1268 touch->grab_y = y;
1269 }
1270
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271 break;
1272 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 ev = touch->focus;
1274 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275 break;
1276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001278 grab->interface->motion(grab, time, touch_id, sx, sy);
1279 break;
1280 case WL_TOUCH_UP:
1281 weston_compositor_idle_release(ec);
1282 seat->num_tp--;
1283
1284 grab->interface->up(grab, time, touch_id);
1285 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001286 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001287 break;
1288 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001289
1290 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291}
1292
1293static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001294pointer_cursor_surface_configure(struct weston_surface *es,
1295 int32_t dx, int32_t dy, int32_t width, int32_t height)
1296{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001297 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001298 int x, y;
1299
1300 if (width == 0)
1301 return;
1302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001304
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001305 pointer->hotspot_x -= dx;
1306 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001307
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001308 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1309 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001312
1313 empty_region(&es->pending.input);
1314
1315 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 wl_list_insert(&es->compositor->cursor_layer.view_list,
1317 &pointer->sprite->layer_link);
1318 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001319 }
1320}
1321
1322static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001323pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1324 uint32_t serial, struct wl_resource *surface_resource,
1325 int32_t x, int32_t y)
1326{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001327 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001328 struct weston_surface *surface = NULL;
1329
1330 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001331 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001332
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001333 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001334 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001336 black_surface used in shell.c for fullscreen don't have
1337 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001339 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001341 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001342 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001343 return;
1344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001346 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001347 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001348 WL_DISPLAY_ERROR_INVALID_OBJECT,
1349 "surface->configure already "
1350 "set");
1351 return;
1352 }
1353 }
1354
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001355 if (pointer->sprite)
1356 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001357
1358 if (!surface)
1359 return;
1360
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001361 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001362 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001363
1364 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001365 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001367 pointer->hotspot_x = x;
1368 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001369
1370 if (surface->buffer_ref.buffer)
1371 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1372 weston_surface_buffer_height(surface));
1373}
1374
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001375static void
1376pointer_release(struct wl_client *client, struct wl_resource *resource)
1377{
1378 wl_resource_destroy(resource);
1379}
1380
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001381static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001382 pointer_set_cursor,
1383 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001384};
1385
1386static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001387seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1388 uint32_t id)
1389{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001390 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001391 struct wl_resource *cr;
1392
Kristian Høgsberge3148752013-05-06 23:19:49 -04001393 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001394 return;
1395
Jason Ekstranda85118c2013-06-27 20:17:02 -05001396 cr = wl_resource_create(client, &wl_pointer_interface,
1397 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001398 if (cr == NULL) {
1399 wl_client_post_no_memory(client);
1400 return;
1401 }
1402
Neil Roberts96d790e2013-09-19 17:32:00 +01001403 /* May be moved to focused list later by either
1404 * weston_pointer_set_focus or directly if this client is already
1405 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001406 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001407 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1408 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001409
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1411 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001412 wl_fixed_t sx, sy;
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_from_global_fixed(seat->pointer->focus,
1415 seat->pointer->x,
1416 seat->pointer->y,
1417 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001418
1419 wl_list_remove(wl_resource_get_link(cr));
1420 wl_list_insert(&seat->pointer->focus_resource_list,
1421 wl_resource_get_link(cr));
1422 wl_pointer_send_enter(cr,
1423 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001425 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001426 }
1427}
1428
1429static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001430keyboard_release(struct wl_client *client, struct wl_resource *resource)
1431{
1432 wl_resource_destroy(resource);
1433}
1434
1435static const struct wl_keyboard_interface keyboard_interface = {
1436 keyboard_release
1437};
1438
Neil Roberts96d790e2013-09-19 17:32:00 +01001439static int
1440should_send_modifiers_to_client(struct weston_seat *seat,
1441 struct wl_client *client)
1442{
1443 if (seat->keyboard &&
1444 seat->keyboard->focus &&
1445 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1446 return 1;
1447
1448 if (seat->pointer &&
1449 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001451 return 1;
1452
1453 return 0;
1454}
1455
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001456static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001457seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1458 uint32_t id)
1459{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001460 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001461 struct wl_resource *cr;
1462
Kristian Høgsberge3148752013-05-06 23:19:49 -04001463 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001464 return;
1465
Jason Ekstranda85118c2013-06-27 20:17:02 -05001466 cr = wl_resource_create(client, &wl_keyboard_interface,
1467 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001468 if (cr == NULL) {
1469 wl_client_post_no_memory(client);
1470 return;
1471 }
1472
Neil Roberts96d790e2013-09-19 17:32:00 +01001473 /* May be moved to focused list later by either
1474 * weston_keyboard_set_focus or directly if this client is already
1475 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001476 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001477 wl_resource_set_implementation(cr, &keyboard_interface,
1478 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001479
Matt Roper01a92732013-06-24 16:52:44 +01001480 if (seat->compositor->use_xkbcommon) {
1481 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001482 seat->xkb_info->keymap_fd,
1483 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001484 } else {
1485 int null_fd = open("/dev/null", O_RDONLY);
1486 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1487 null_fd,
1488 0);
1489 close(null_fd);
1490 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001491
Neil Roberts96d790e2013-09-19 17:32:00 +01001492 if (should_send_modifiers_to_client(seat, client)) {
1493 send_modifiers_to_resource(seat->keyboard,
1494 cr,
1495 seat->keyboard->focus_serial);
1496 }
1497
Kristian Høgsberge3148752013-05-06 23:19:49 -04001498 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001499 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001500 struct weston_surface *surface =
1501 (struct weston_surface *) seat->keyboard->focus;
1502
1503 wl_list_remove(wl_resource_get_link(cr));
1504 wl_list_insert(&seat->keyboard->focus_resource_list,
1505 wl_resource_get_link(cr));
1506 wl_keyboard_send_enter(cr,
1507 seat->keyboard->focus_serial,
1508 surface->resource,
1509 &seat->keyboard->keys);
1510
1511 /* If this is the first keyboard resource for this
1512 * client... */
1513 if (seat->keyboard->focus_resource_list.prev ==
1514 wl_resource_get_link(cr))
1515 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001516 }
1517}
1518
1519static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001520touch_release(struct wl_client *client, struct wl_resource *resource)
1521{
1522 wl_resource_destroy(resource);
1523}
1524
1525static const struct wl_touch_interface touch_interface = {
1526 touch_release
1527};
1528
1529static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001530seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1531 uint32_t id)
1532{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001533 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001534 struct wl_resource *cr;
1535
Kristian Høgsberge3148752013-05-06 23:19:49 -04001536 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001537 return;
1538
Jason Ekstranda85118c2013-06-27 20:17:02 -05001539 cr = wl_resource_create(client, &wl_touch_interface,
1540 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001541 if (cr == NULL) {
1542 wl_client_post_no_memory(client);
1543 return;
1544 }
1545
Neil Roberts96d790e2013-09-19 17:32:00 +01001546 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001548 wl_list_insert(&seat->touch->resource_list,
1549 wl_resource_get_link(cr));
1550 } else {
1551 wl_list_insert(&seat->touch->focus_resource_list,
1552 wl_resource_get_link(cr));
1553 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001554 wl_resource_set_implementation(cr, &touch_interface,
1555 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001556}
1557
1558static const struct wl_seat_interface seat_interface = {
1559 seat_get_pointer,
1560 seat_get_keyboard,
1561 seat_get_touch,
1562};
1563
1564static void
1565bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1566{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001567 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001568 struct wl_resource *resource;
1569 enum wl_seat_capability caps = 0;
1570
Jason Ekstranda85118c2013-06-27 20:17:02 -05001571 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001572 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001573 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001574 wl_resource_set_implementation(resource, &seat_interface, data,
1575 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001576
1577 if (seat->pointer)
1578 caps |= WL_SEAT_CAPABILITY_POINTER;
1579 if (seat->keyboard)
1580 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1581 if (seat->touch)
1582 caps |= WL_SEAT_CAPABILITY_TOUCH;
1583
1584 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001585 if (version >= 2)
1586 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001587}
1588
Rob Bradford382ff462013-06-24 16:52:45 +01001589#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001590int
1591weston_compositor_xkb_init(struct weston_compositor *ec,
1592 struct xkb_rule_names *names)
1593{
Rob Bradford382ff462013-06-24 16:52:45 +01001594 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001595
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596 if (ec->xkb_context == NULL) {
1597 ec->xkb_context = xkb_context_new(0);
1598 if (ec->xkb_context == NULL) {
1599 weston_log("failed to create XKB context\n");
1600 return -1;
1601 }
1602 }
1603
1604 if (names)
1605 ec->xkb_names = *names;
1606 if (!ec->xkb_names.rules)
1607 ec->xkb_names.rules = strdup("evdev");
1608 if (!ec->xkb_names.model)
1609 ec->xkb_names.model = strdup("pc105");
1610 if (!ec->xkb_names.layout)
1611 ec->xkb_names.layout = strdup("us");
1612
1613 return 0;
1614}
1615
Stefan Schmidtfda26522013-09-17 10:54:09 +01001616static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001617weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001618{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001619 if (--xkb_info->ref_count > 0)
1620 return;
1621
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001622 if (xkb_info->keymap)
1623 xkb_map_unref(xkb_info->keymap);
1624
1625 if (xkb_info->keymap_area)
1626 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1627 if (xkb_info->keymap_fd >= 0)
1628 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001629 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001630}
1631
1632void
1633weston_compositor_xkb_destroy(struct weston_compositor *ec)
1634{
Matt Roper01a92732013-06-24 16:52:44 +01001635 /*
1636 * If we're operating in raw keyboard mode, we never initialized
1637 * libxkbcommon so there's no cleanup to do either.
1638 */
1639 if (!ec->use_xkbcommon)
1640 return;
1641
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001642 free((char *) ec->xkb_names.rules);
1643 free((char *) ec->xkb_names.model);
1644 free((char *) ec->xkb_names.layout);
1645 free((char *) ec->xkb_names.variant);
1646 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001647
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001648 if (ec->xkb_info)
1649 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001650 xkb_context_unref(ec->xkb_context);
1651}
1652
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001653static struct weston_xkb_info *
1654weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001655{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001656 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1657 if (xkb_info == NULL)
1658 return NULL;
1659
1660 xkb_info->keymap = xkb_map_ref(keymap);
1661 xkb_info->ref_count = 1;
1662
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001663 char *keymap_str;
1664
1665 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1666 XKB_MOD_NAME_SHIFT);
1667 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1668 XKB_MOD_NAME_CAPS);
1669 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1670 XKB_MOD_NAME_CTRL);
1671 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1672 XKB_MOD_NAME_ALT);
1673 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1674 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1675 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1676 XKB_MOD_NAME_LOGO);
1677 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1678
1679 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1680 XKB_LED_NAME_NUM);
1681 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1682 XKB_LED_NAME_CAPS);
1683 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1684 XKB_LED_NAME_SCROLL);
1685
1686 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1687 if (keymap_str == NULL) {
1688 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001689 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001690 }
1691 xkb_info->keymap_size = strlen(keymap_str) + 1;
1692
1693 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1694 if (xkb_info->keymap_fd < 0) {
1695 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1696 (unsigned long) xkb_info->keymap_size);
1697 goto err_keymap_str;
1698 }
1699
1700 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1701 PROT_READ | PROT_WRITE,
1702 MAP_SHARED, xkb_info->keymap_fd, 0);
1703 if (xkb_info->keymap_area == MAP_FAILED) {
1704 weston_log("failed to mmap() %lu bytes\n",
1705 (unsigned long) xkb_info->keymap_size);
1706 goto err_dev_zero;
1707 }
1708 strcpy(xkb_info->keymap_area, keymap_str);
1709 free(keymap_str);
1710
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001711 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001712
1713err_dev_zero:
1714 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001715err_keymap_str:
1716 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001717err_keymap:
1718 xkb_map_unref(xkb_info->keymap);
1719 free(xkb_info);
1720 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001721}
1722
1723static int
1724weston_compositor_build_global_keymap(struct weston_compositor *ec)
1725{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001726 struct xkb_keymap *keymap;
1727
1728 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001729 return 0;
1730
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001731 keymap = xkb_map_new_from_names(ec->xkb_context,
1732 &ec->xkb_names,
1733 0);
1734 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001735 weston_log("failed to compile global XKB keymap\n");
1736 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1737 "options %s\n",
1738 ec->xkb_names.rules, ec->xkb_names.model,
1739 ec->xkb_names.layout, ec->xkb_names.variant,
1740 ec->xkb_names.options);
1741 return -1;
1742 }
1743
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001744 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001745 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001746 return -1;
1747
1748 return 0;
1749}
Rob Bradford382ff462013-06-24 16:52:45 +01001750#else
1751int
1752weston_compositor_xkb_init(struct weston_compositor *ec,
1753 struct xkb_rule_names *names)
1754{
1755 return 0;
1756}
1757
1758void
1759weston_compositor_xkb_destroy(struct weston_compositor *ec)
1760{
1761}
1762#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001763
Rui Matos65196bc2013-10-10 19:44:19 +02001764WL_EXPORT void
1765weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1766{
1767 if (!seat->keyboard || !keymap)
1768 return;
1769
1770#ifdef ENABLE_XKBCOMMON
1771 if (!seat->compositor->use_xkbcommon)
1772 return;
1773
1774 xkb_keymap_unref(seat->pending_keymap);
1775 seat->pending_keymap = xkb_keymap_ref(keymap);
1776
1777 if (seat->keyboard->keys.size == 0)
1778 update_keymap(seat);
1779#endif
1780}
1781
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001782WL_EXPORT int
1783weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1784{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001785 struct weston_keyboard *keyboard;
1786
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001787 if (seat->keyboard) {
1788 seat->keyboard_device_count += 1;
1789 if (seat->keyboard_device_count == 1)
1790 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001791 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001792 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793
Rob Bradford382ff462013-06-24 16:52:45 +01001794#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001795 if (seat->compositor->use_xkbcommon) {
1796 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001797 seat->xkb_info = weston_xkb_info_create(keymap);
1798 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001799 return -1;
1800 } else {
1801 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1802 return -1;
1803 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001804 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001805 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001806
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001807 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001808 if (seat->xkb_state.state == NULL) {
1809 weston_log("failed to initialise XKB state\n");
1810 return -1;
1811 }
1812
1813 seat->xkb_state.leds = 0;
1814 }
Rob Bradford382ff462013-06-24 16:52:45 +01001815#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001817 keyboard = weston_keyboard_create();
1818 if (keyboard == NULL) {
1819 weston_log("failed to allocate weston keyboard struct\n");
1820 return -1;
1821 }
1822
1823 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001824 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001825 keyboard->seat = seat;
1826
1827 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001829 return 0;
1830}
1831
1832WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001833weston_seat_release_keyboard(struct weston_seat *seat)
1834{
1835 seat->keyboard_device_count--;
1836 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001837 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001838 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001839 seat_send_updated_caps(seat);
1840 }
1841}
1842
1843WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844weston_seat_init_pointer(struct weston_seat *seat)
1845{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001846 struct weston_pointer *pointer;
1847
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001848 if (seat->pointer) {
1849 seat->pointer_device_count += 1;
1850 if (seat->pointer_device_count == 1)
1851 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001852 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001853 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001854
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001855 pointer = weston_pointer_create();
1856 if (pointer == NULL)
1857 return;
1858
1859 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001860 seat->pointer_device_count = 1;
1861 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001862
1863 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001864}
1865
1866WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001867weston_seat_release_pointer(struct weston_seat *seat)
1868{
1869 struct weston_pointer *pointer = seat->pointer;
1870
1871 seat->pointer_device_count--;
1872 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001873 weston_pointer_set_focus(pointer, NULL,
1874 wl_fixed_from_int(0),
1875 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001876 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001877
Jonas Ådahla4932742013-10-17 23:04:07 +02001878 if (pointer->sprite)
1879 pointer_unmap_sprite(pointer);
1880
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001881 seat_send_updated_caps(seat);
1882 }
1883}
1884
1885WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886weston_seat_init_touch(struct weston_seat *seat)
1887{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001888 struct weston_touch *touch;
1889
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001890 if (seat->touch) {
1891 seat->touch_device_count += 1;
1892 if (seat->touch_device_count == 1)
1893 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001894 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001895 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001896
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001897 touch = weston_touch_create();
1898 if (touch == NULL)
1899 return;
1900
1901 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001902 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001903 touch->seat = seat;
1904
1905 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001906}
1907
1908WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001909weston_seat_release_touch(struct weston_seat *seat)
1910{
1911 seat->touch_device_count--;
1912 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001913 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001914 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001915 seat_send_updated_caps(seat);
1916 }
1917}
1918
1919WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001920weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1921 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001922{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001923 memset(seat, 0, sizeof *seat);
1924
Kristian Høgsberge3148752013-05-06 23:19:49 -04001925 seat->selection_data_source = NULL;
1926 wl_list_init(&seat->base_resource_list);
1927 wl_signal_init(&seat->selection_signal);
1928 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001929 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001930
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001931 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001932 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001934 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001935 seat->modifier_state = 0;
1936 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001937 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001938
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001939 wl_list_insert(ec->seat_list.prev, &seat->link);
1940
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001941 clipboard_create(seat);
1942
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001943 wl_signal_emit(&ec->seat_created_signal, seat);
1944}
1945
1946WL_EXPORT void
1947weston_seat_release(struct weston_seat *seat)
1948{
1949 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001950
Rob Bradford382ff462013-06-24 16:52:45 +01001951#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001952 if (seat->compositor->use_xkbcommon) {
1953 if (seat->xkb_state.state != NULL)
1954 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001955 if (seat->xkb_info)
1956 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001957 if (seat->pending_keymap)
1958 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001959 }
Rob Bradford382ff462013-06-24 16:52:45 +01001960#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001961
Kristian Høgsberge3148752013-05-06 23:19:49 -04001962 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001963 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001964 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001965 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001966 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001967 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001968
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001969 free (seat->seat_name);
1970
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001971 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001972
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001973 wl_signal_emit(&seat->destroy_signal, seat);
1974}