blob: f1cf93853da1e55053b8734c15dc71137cbc3380 [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{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400715 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400716 int32_t ix, iy;
717
Rob Bradford806d8c02013-06-25 18:56:41 +0100718 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400719
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400720 pointer->x = x;
721 pointer->y = y;
722
723 ix = wl_fixed_to_int(x);
724 iy = wl_fixed_to_int(y);
725
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400726 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 weston_view_set_position(pointer->sprite,
728 ix - pointer->hotspot_x,
729 iy - pointer->hotspot_y);
730 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400731 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +0100732
733 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400734}
735
736WL_EXPORT void
737notify_motion(struct weston_seat *seat,
738 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
739{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400740 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400741 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400742
743 weston_compositor_wake(ec);
744
745 move_pointer(seat, pointer->x + dx, pointer->y + dy);
746
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400747 pointer->grab->interface->focus(pointer->grab);
748 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400749}
750
751WL_EXPORT void
752notify_motion_absolute(struct weston_seat *seat,
753 uint32_t time, wl_fixed_t x, wl_fixed_t y)
754{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400755 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400756 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400757
758 weston_compositor_wake(ec);
759
760 move_pointer(seat, x, y);
761
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400762 pointer->grab->interface->focus(pointer->grab);
763 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400764}
765
766WL_EXPORT void
767weston_surface_activate(struct weston_surface *surface,
768 struct weston_seat *seat)
769{
770 struct weston_compositor *compositor = seat->compositor;
771
Kristian Høgsberge3148752013-05-06 23:19:49 -0400772 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400773 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400774 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400775 }
776
777 wl_signal_emit(&compositor->activate_signal, surface);
778}
779
780WL_EXPORT void
781notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
782 enum wl_pointer_button_state state)
783{
784 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400785 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400786 struct weston_surface *focus =
787 (struct weston_surface *) pointer->focus;
788 uint32_t serial = wl_display_next_serial(compositor->wl_display);
789
790 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
791 if (compositor->ping_handler && focus)
792 compositor->ping_handler(focus, serial);
793 weston_compositor_idle_inhibit(compositor);
794 if (pointer->button_count == 0) {
795 pointer->grab_button = button;
796 pointer->grab_time = time;
797 pointer->grab_x = pointer->x;
798 pointer->grab_y = pointer->y;
799 }
800 pointer->button_count++;
801 } else {
802 weston_compositor_idle_release(compositor);
803 pointer->button_count--;
804 }
805
806 weston_compositor_run_button_binding(compositor, seat, time, button,
807 state);
808
809 pointer->grab->interface->button(pointer->grab, time, button, state);
810
811 if (pointer->button_count == 1)
812 pointer->grab_serial =
813 wl_display_get_serial(compositor->wl_display);
814}
815
816WL_EXPORT void
817notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
818 wl_fixed_t value)
819{
820 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400821 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400822 struct weston_surface *focus =
823 (struct weston_surface *) pointer->focus;
824 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100825 struct wl_resource *resource;
826 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400827
828 if (compositor->ping_handler && focus)
829 compositor->ping_handler(focus, serial);
830
831 weston_compositor_wake(compositor);
832
833 if (!value)
834 return;
835
836 if (weston_compositor_run_axis_binding(compositor, seat,
837 time, axis, value))
838 return;
839
Neil Roberts96d790e2013-09-19 17:32:00 +0100840 resource_list = &pointer->focus_resource_list;
841 wl_resource_for_each(resource, resource_list)
842 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400843 value);
844}
845
Rob Bradford382ff462013-06-24 16:52:45 +0100846#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400847WL_EXPORT void
848notify_modifiers(struct weston_seat *seat, uint32_t serial)
849{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400850 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400851 struct weston_keyboard_grab *grab = keyboard->grab;
852 uint32_t mods_depressed, mods_latched, mods_locked, group;
853 uint32_t mods_lookup;
854 enum weston_led leds = 0;
855 int changed = 0;
856
857 /* Serialize and update our internal state, checking to see if it's
858 * different to the previous state. */
859 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
860 XKB_STATE_DEPRESSED);
861 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
862 XKB_STATE_LATCHED);
863 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
864 XKB_STATE_LOCKED);
865 group = xkb_state_serialize_group(seat->xkb_state.state,
866 XKB_STATE_EFFECTIVE);
867
Kristian Høgsberge3148752013-05-06 23:19:49 -0400868 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
869 mods_latched != seat->keyboard->modifiers.mods_latched ||
870 mods_locked != seat->keyboard->modifiers.mods_locked ||
871 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400872 changed = 1;
873
Kristian Høgsberge3148752013-05-06 23:19:49 -0400874 seat->keyboard->modifiers.mods_depressed = mods_depressed;
875 seat->keyboard->modifiers.mods_latched = mods_latched;
876 seat->keyboard->modifiers.mods_locked = mods_locked;
877 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400878
879 /* And update the modifier_state for bindings. */
880 mods_lookup = mods_depressed | mods_latched;
881 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000882 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400883 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000884 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400885 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000886 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400887 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000888 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400889 seat->modifier_state |= MODIFIER_SHIFT;
890
891 /* Finally, notify the compositor that LEDs have changed. */
892 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000893 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400894 leds |= LED_NUM_LOCK;
895 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000896 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400897 leds |= LED_CAPS_LOCK;
898 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000899 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400900 leds |= LED_SCROLL_LOCK;
901 if (leds != seat->xkb_state.leds && seat->led_update)
902 seat->led_update(seat, leds);
903 seat->xkb_state.leds = leds;
904
905 if (changed) {
906 grab->interface->modifiers(grab,
907 serial,
908 keyboard->modifiers.mods_depressed,
909 keyboard->modifiers.mods_latched,
910 keyboard->modifiers.mods_locked,
911 keyboard->modifiers.group);
912 }
913}
914
915static void
916update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
917 enum wl_keyboard_key_state state)
918{
919 enum xkb_key_direction direction;
920
Matt Roper01a92732013-06-24 16:52:44 +0100921 /* Keyboard modifiers don't exist in raw keyboard mode */
922 if (!seat->compositor->use_xkbcommon)
923 return;
924
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400925 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
926 direction = XKB_KEY_DOWN;
927 else
928 direction = XKB_KEY_UP;
929
930 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
931 * broken keycode system, which starts at 8. */
932 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
933
934 notify_modifiers(seat, serial);
935}
Rui Matos65196bc2013-10-10 19:44:19 +0200936
937static void
938send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
939{
940 wl_keyboard_send_keymap(resource,
941 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
942 xkb_info->keymap_fd,
943 xkb_info->keymap_size);
944}
945
946static void
947send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
948{
949 wl_keyboard_send_modifiers(resource, serial,
950 keyboard->modifiers.mods_depressed,
951 keyboard->modifiers.mods_latched,
952 keyboard->modifiers.mods_locked,
953 keyboard->modifiers.group);
954}
955
956static struct weston_xkb_info *
957weston_xkb_info_create(struct xkb_keymap *keymap);
958static void
959weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
960
961static void
962update_keymap(struct weston_seat *seat)
963{
964 struct wl_resource *resource;
965 struct weston_xkb_info *xkb_info;
966 struct xkb_state *state;
967 xkb_mod_mask_t latched_mods;
968 xkb_mod_mask_t locked_mods;
969
970 xkb_info = weston_xkb_info_create(seat->pending_keymap);
971
972 xkb_keymap_unref(seat->pending_keymap);
973 seat->pending_keymap = NULL;
974
975 if (!xkb_info) {
976 weston_log("failed to create XKB info\n");
977 return;
978 }
979
980 state = xkb_state_new(xkb_info->keymap);
981 if (!state) {
982 weston_log("failed to initialise XKB state\n");
983 weston_xkb_info_destroy(xkb_info);
984 return;
985 }
986
987 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
988 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
989 xkb_state_update_mask(state,
990 0, /* depressed */
991 latched_mods,
992 locked_mods,
993 0, 0, 0);
994
995 weston_xkb_info_destroy(seat->xkb_info);
996 seat->xkb_info = xkb_info;
997
998 xkb_state_unref(seat->xkb_state.state);
999 seat->xkb_state.state = state;
1000
1001 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1002 send_keymap(resource, xkb_info);
1003 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1004 send_keymap(resource, xkb_info);
1005
1006 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1007
1008 if (!latched_mods && !locked_mods)
1009 return;
1010
1011 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1012 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1013 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1014 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1015}
Rob Bradford382ff462013-06-24 16:52:45 +01001016#else
1017WL_EXPORT void
1018notify_modifiers(struct weston_seat *seat, uint32_t serial)
1019{
1020}
1021
1022static void
1023update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1024 enum wl_keyboard_key_state state)
1025{
1026}
Rui Matos65196bc2013-10-10 19:44:19 +02001027
1028static void
1029update_keymap(struct weston_seat *seat)
1030{
1031}
Rob Bradford382ff462013-06-24 16:52:45 +01001032#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001033
1034WL_EXPORT void
1035notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1036 enum wl_keyboard_key_state state,
1037 enum weston_key_state_update update_state)
1038{
1039 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001040 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001042 struct weston_keyboard_grab *grab = keyboard->grab;
1043 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1044 uint32_t *k, *end;
1045
1046 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1047 if (compositor->ping_handler && focus)
1048 compositor->ping_handler(focus, serial);
1049
1050 weston_compositor_idle_inhibit(compositor);
1051 keyboard->grab_key = key;
1052 keyboard->grab_time = time;
1053 } else {
1054 weston_compositor_idle_release(compositor);
1055 }
1056
1057 end = keyboard->keys.data + keyboard->keys.size;
1058 for (k = keyboard->keys.data; k < end; k++) {
1059 if (*k == key) {
1060 /* Ignore server-generated repeats. */
1061 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1062 return;
1063 *k = *--end;
1064 }
1065 }
1066 keyboard->keys.size = (void *) end - keyboard->keys.data;
1067 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1068 k = wl_array_add(&keyboard->keys, sizeof *k);
1069 *k = key;
1070 }
1071
1072 if (grab == &keyboard->default_grab ||
1073 grab == &keyboard->input_method_grab) {
1074 weston_compositor_run_key_binding(compositor, seat, time, key,
1075 state);
1076 grab = keyboard->grab;
1077 }
1078
1079 grab->interface->key(grab, time, key, state);
1080
Rui Matos65196bc2013-10-10 19:44:19 +02001081 if (seat->pending_keymap &&
1082 keyboard->keys.size == 0)
1083 update_keymap(seat);
1084
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001085 if (update_state == STATE_UPDATE_AUTOMATIC) {
1086 update_modifier_state(seat,
1087 wl_display_get_serial(compositor->wl_display),
1088 key,
1089 state);
1090 }
1091}
1092
1093WL_EXPORT void
1094notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1095 wl_fixed_t x, wl_fixed_t y)
1096{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001097 if (output) {
1098 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001099 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001100 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001101 * NULL) here, but somehow that breaks re-entry... */
1102 }
1103}
1104
1105static void
1106destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1107{
1108 struct weston_seat *ws;
1109
1110 ws = container_of(listener, struct weston_seat,
1111 saved_kbd_focus_listener);
1112
1113 ws->saved_kbd_focus = NULL;
1114}
1115
1116WL_EXPORT void
1117notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1118 enum weston_key_state_update update_state)
1119{
1120 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001121 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001122 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001123 uint32_t *k, serial;
1124
1125 serial = wl_display_next_serial(compositor->wl_display);
1126 wl_array_copy(&keyboard->keys, keys);
1127 wl_array_for_each(k, &keyboard->keys) {
1128 weston_compositor_idle_inhibit(compositor);
1129 if (update_state == STATE_UPDATE_AUTOMATIC)
1130 update_modifier_state(seat, serial, *k,
1131 WL_KEYBOARD_KEY_STATE_PRESSED);
1132 }
1133
1134 /* Run key bindings after we've updated the state. */
1135 wl_array_for_each(k, &keyboard->keys) {
1136 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1137 WL_KEYBOARD_KEY_STATE_PRESSED);
1138 }
1139
1140 surface = seat->saved_kbd_focus;
1141
1142 if (surface) {
1143 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1144 weston_keyboard_set_focus(keyboard, surface);
1145 seat->saved_kbd_focus = NULL;
1146 }
1147}
1148
1149WL_EXPORT void
1150notify_keyboard_focus_out(struct weston_seat *seat)
1151{
1152 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001153 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001154 uint32_t *k, serial;
1155
1156 serial = wl_display_next_serial(compositor->wl_display);
1157 wl_array_for_each(k, &keyboard->keys) {
1158 weston_compositor_idle_release(compositor);
1159 update_modifier_state(seat, serial, *k,
1160 WL_KEYBOARD_KEY_STATE_RELEASED);
1161 }
1162
1163 seat->modifier_state = 0;
1164
1165 if (keyboard->focus) {
1166 seat->saved_kbd_focus = keyboard->focus;
1167 seat->saved_kbd_focus_listener.notify =
1168 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001169 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001170 &seat->saved_kbd_focus_listener);
1171 }
1172
1173 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001174 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001175}
1176
Michael Fua2bb7912013-07-23 15:51:06 +08001177WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001179{
Neil Roberts96d790e2013-09-19 17:32:00 +01001180 struct wl_list *focus_resource_list;
1181
1182 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001183
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001184 if (view && seat->touch->focus &&
1185 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001189
Neil Roberts96d790e2013-09-19 17:32:00 +01001190 if (!wl_list_empty(focus_resource_list)) {
1191 move_resources(&seat->touch->resource_list,
1192 focus_resource_list);
1193 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001196 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001198 move_resources_for_client(focus_resource_list,
1199 &seat->touch->resource_list,
1200 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001201 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001203}
1204
1205/**
1206 * notify_touch - emulates button touches and notifies surfaces accordingly.
1207 *
1208 * It assumes always the correct cycle sequence until it gets here: touch_down
1209 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1210 * for sending along such order.
1211 *
1212 */
1213WL_EXPORT void
1214notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1215 wl_fixed_t x, wl_fixed_t y, int touch_type)
1216{
1217 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001218 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001219 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001221 wl_fixed_t sx, sy;
1222
1223 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001224 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1225 touch->grab_x = x;
1226 touch->grab_y = y;
1227 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001228
1229 switch (touch_type) {
1230 case WL_TOUCH_DOWN:
1231 weston_compositor_idle_inhibit(ec);
1232
1233 seat->num_tp++;
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 /* the first finger down picks the view, and all further go
1236 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001237 * until all touch points are up again. */
1238 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1240 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001241 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 ev = touch->focus;
1243 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001244 } else {
1245 /* Unexpected condition: We have non-initial touch but
1246 * there is no focused surface.
1247 */
1248 weston_log("touch event received with %d points down"
1249 "but no surface focused\n", seat->num_tp);
1250 return;
1251 }
1252
1253 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001254 if (seat->num_tp == 1) {
1255 touch->grab_serial =
1256 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001257 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001258 touch->grab_time = time;
1259 touch->grab_x = x;
1260 touch->grab_y = y;
1261 }
1262
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001263 break;
1264 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 ev = touch->focus;
1266 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001267 break;
1268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001270 grab->interface->motion(grab, time, touch_id, sx, sy);
1271 break;
1272 case WL_TOUCH_UP:
1273 weston_compositor_idle_release(ec);
1274 seat->num_tp--;
1275
1276 grab->interface->up(grab, time, touch_id);
1277 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001278 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001279 break;
1280 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001281
1282 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001283}
1284
1285static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001286pointer_cursor_surface_configure(struct weston_surface *es,
1287 int32_t dx, int32_t dy, int32_t width, int32_t height)
1288{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001289 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001290 int x, y;
1291
1292 if (width == 0)
1293 return;
1294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001296
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001297 pointer->hotspot_x -= dx;
1298 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001299
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001300 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1301 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001304
1305 empty_region(&es->pending.input);
1306
1307 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 wl_list_insert(&es->compositor->cursor_layer.view_list,
1309 &pointer->sprite->layer_link);
1310 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001311 }
1312}
1313
1314static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001315pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1316 uint32_t serial, struct wl_resource *surface_resource,
1317 int32_t x, int32_t y)
1318{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001319 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001320 struct weston_surface *surface = NULL;
1321
1322 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001323 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001324
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001325 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001326 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001328 black_surface used in shell.c for fullscreen don't have
1329 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001331 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001334 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001335 return;
1336
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001338 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001339 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001340 WL_DISPLAY_ERROR_INVALID_OBJECT,
1341 "surface->configure already "
1342 "set");
1343 return;
1344 }
1345 }
1346
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001347 if (pointer->sprite)
1348 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001349
1350 if (!surface)
1351 return;
1352
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001353 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001354 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001355
1356 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001357 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001359 pointer->hotspot_x = x;
1360 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001361
1362 if (surface->buffer_ref.buffer)
1363 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1364 weston_surface_buffer_height(surface));
1365}
1366
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001367static void
1368pointer_release(struct wl_client *client, struct wl_resource *resource)
1369{
1370 wl_resource_destroy(resource);
1371}
1372
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001373static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001374 pointer_set_cursor,
1375 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001376};
1377
1378static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001379seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1380 uint32_t id)
1381{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001382 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001383 struct wl_resource *cr;
1384
Kristian Høgsberge3148752013-05-06 23:19:49 -04001385 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001386 return;
1387
Jason Ekstranda85118c2013-06-27 20:17:02 -05001388 cr = wl_resource_create(client, &wl_pointer_interface,
1389 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001390 if (cr == NULL) {
1391 wl_client_post_no_memory(client);
1392 return;
1393 }
1394
Neil Roberts96d790e2013-09-19 17:32:00 +01001395 /* May be moved to focused list later by either
1396 * weston_pointer_set_focus or directly if this client is already
1397 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001398 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001399 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1400 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001401
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1403 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001404 wl_fixed_t sx, sy;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 weston_view_from_global_fixed(seat->pointer->focus,
1407 seat->pointer->x,
1408 seat->pointer->y,
1409 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001410
1411 wl_list_remove(wl_resource_get_link(cr));
1412 wl_list_insert(&seat->pointer->focus_resource_list,
1413 wl_resource_get_link(cr));
1414 wl_pointer_send_enter(cr,
1415 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001417 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001418 }
1419}
1420
1421static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001422keyboard_release(struct wl_client *client, struct wl_resource *resource)
1423{
1424 wl_resource_destroy(resource);
1425}
1426
1427static const struct wl_keyboard_interface keyboard_interface = {
1428 keyboard_release
1429};
1430
Neil Roberts96d790e2013-09-19 17:32:00 +01001431static int
1432should_send_modifiers_to_client(struct weston_seat *seat,
1433 struct wl_client *client)
1434{
1435 if (seat->keyboard &&
1436 seat->keyboard->focus &&
1437 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1438 return 1;
1439
1440 if (seat->pointer &&
1441 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001443 return 1;
1444
1445 return 0;
1446}
1447
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001448static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1450 uint32_t id)
1451{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001452 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001453 struct wl_resource *cr;
1454
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001456 return;
1457
Jason Ekstranda85118c2013-06-27 20:17:02 -05001458 cr = wl_resource_create(client, &wl_keyboard_interface,
1459 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001460 if (cr == NULL) {
1461 wl_client_post_no_memory(client);
1462 return;
1463 }
1464
Neil Roberts96d790e2013-09-19 17:32:00 +01001465 /* May be moved to focused list later by either
1466 * weston_keyboard_set_focus or directly if this client is already
1467 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001468 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001469 wl_resource_set_implementation(cr, &keyboard_interface,
1470 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001471
Matt Roper01a92732013-06-24 16:52:44 +01001472 if (seat->compositor->use_xkbcommon) {
1473 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001474 seat->xkb_info->keymap_fd,
1475 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001476 } else {
1477 int null_fd = open("/dev/null", O_RDONLY);
1478 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1479 null_fd,
1480 0);
1481 close(null_fd);
1482 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483
Neil Roberts96d790e2013-09-19 17:32:00 +01001484 if (should_send_modifiers_to_client(seat, client)) {
1485 send_modifiers_to_resource(seat->keyboard,
1486 cr,
1487 seat->keyboard->focus_serial);
1488 }
1489
Kristian Høgsberge3148752013-05-06 23:19:49 -04001490 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001491 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001492 struct weston_surface *surface =
1493 (struct weston_surface *) seat->keyboard->focus;
1494
1495 wl_list_remove(wl_resource_get_link(cr));
1496 wl_list_insert(&seat->keyboard->focus_resource_list,
1497 wl_resource_get_link(cr));
1498 wl_keyboard_send_enter(cr,
1499 seat->keyboard->focus_serial,
1500 surface->resource,
1501 &seat->keyboard->keys);
1502
1503 /* If this is the first keyboard resource for this
1504 * client... */
1505 if (seat->keyboard->focus_resource_list.prev ==
1506 wl_resource_get_link(cr))
1507 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001508 }
1509}
1510
1511static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001512touch_release(struct wl_client *client, struct wl_resource *resource)
1513{
1514 wl_resource_destroy(resource);
1515}
1516
1517static const struct wl_touch_interface touch_interface = {
1518 touch_release
1519};
1520
1521static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001522seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1523 uint32_t id)
1524{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001525 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001526 struct wl_resource *cr;
1527
Kristian Høgsberge3148752013-05-06 23:19:49 -04001528 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001529 return;
1530
Jason Ekstranda85118c2013-06-27 20:17:02 -05001531 cr = wl_resource_create(client, &wl_touch_interface,
1532 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001533 if (cr == NULL) {
1534 wl_client_post_no_memory(client);
1535 return;
1536 }
1537
Neil Roberts96d790e2013-09-19 17:32:00 +01001538 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001540 wl_list_insert(&seat->touch->resource_list,
1541 wl_resource_get_link(cr));
1542 } else {
1543 wl_list_insert(&seat->touch->focus_resource_list,
1544 wl_resource_get_link(cr));
1545 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001546 wl_resource_set_implementation(cr, &touch_interface,
1547 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001548}
1549
1550static const struct wl_seat_interface seat_interface = {
1551 seat_get_pointer,
1552 seat_get_keyboard,
1553 seat_get_touch,
1554};
1555
1556static void
1557bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1558{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001559 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001560 struct wl_resource *resource;
1561 enum wl_seat_capability caps = 0;
1562
Jason Ekstranda85118c2013-06-27 20:17:02 -05001563 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001564 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001565 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001566 wl_resource_set_implementation(resource, &seat_interface, data,
1567 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001568
1569 if (seat->pointer)
1570 caps |= WL_SEAT_CAPABILITY_POINTER;
1571 if (seat->keyboard)
1572 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1573 if (seat->touch)
1574 caps |= WL_SEAT_CAPABILITY_TOUCH;
1575
1576 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001577 if (version >= 2)
1578 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001579}
1580
Rob Bradford382ff462013-06-24 16:52:45 +01001581#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001582int
1583weston_compositor_xkb_init(struct weston_compositor *ec,
1584 struct xkb_rule_names *names)
1585{
Rob Bradford382ff462013-06-24 16:52:45 +01001586 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001587
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001588 if (ec->xkb_context == NULL) {
1589 ec->xkb_context = xkb_context_new(0);
1590 if (ec->xkb_context == NULL) {
1591 weston_log("failed to create XKB context\n");
1592 return -1;
1593 }
1594 }
1595
1596 if (names)
1597 ec->xkb_names = *names;
1598 if (!ec->xkb_names.rules)
1599 ec->xkb_names.rules = strdup("evdev");
1600 if (!ec->xkb_names.model)
1601 ec->xkb_names.model = strdup("pc105");
1602 if (!ec->xkb_names.layout)
1603 ec->xkb_names.layout = strdup("us");
1604
1605 return 0;
1606}
1607
Stefan Schmidtfda26522013-09-17 10:54:09 +01001608static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001609weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001610{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001611 if (--xkb_info->ref_count > 0)
1612 return;
1613
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614 if (xkb_info->keymap)
1615 xkb_map_unref(xkb_info->keymap);
1616
1617 if (xkb_info->keymap_area)
1618 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1619 if (xkb_info->keymap_fd >= 0)
1620 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001621 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001622}
1623
1624void
1625weston_compositor_xkb_destroy(struct weston_compositor *ec)
1626{
Matt Roper01a92732013-06-24 16:52:44 +01001627 /*
1628 * If we're operating in raw keyboard mode, we never initialized
1629 * libxkbcommon so there's no cleanup to do either.
1630 */
1631 if (!ec->use_xkbcommon)
1632 return;
1633
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001634 free((char *) ec->xkb_names.rules);
1635 free((char *) ec->xkb_names.model);
1636 free((char *) ec->xkb_names.layout);
1637 free((char *) ec->xkb_names.variant);
1638 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001639
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001640 if (ec->xkb_info)
1641 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001642 xkb_context_unref(ec->xkb_context);
1643}
1644
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001645static struct weston_xkb_info *
1646weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001647{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001648 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1649 if (xkb_info == NULL)
1650 return NULL;
1651
1652 xkb_info->keymap = xkb_map_ref(keymap);
1653 xkb_info->ref_count = 1;
1654
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001655 char *keymap_str;
1656
1657 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1658 XKB_MOD_NAME_SHIFT);
1659 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1660 XKB_MOD_NAME_CAPS);
1661 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1662 XKB_MOD_NAME_CTRL);
1663 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1664 XKB_MOD_NAME_ALT);
1665 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1666 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1667 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1668 XKB_MOD_NAME_LOGO);
1669 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1670
1671 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1672 XKB_LED_NAME_NUM);
1673 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1674 XKB_LED_NAME_CAPS);
1675 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1676 XKB_LED_NAME_SCROLL);
1677
1678 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1679 if (keymap_str == NULL) {
1680 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001681 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001682 }
1683 xkb_info->keymap_size = strlen(keymap_str) + 1;
1684
1685 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1686 if (xkb_info->keymap_fd < 0) {
1687 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1688 (unsigned long) xkb_info->keymap_size);
1689 goto err_keymap_str;
1690 }
1691
1692 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1693 PROT_READ | PROT_WRITE,
1694 MAP_SHARED, xkb_info->keymap_fd, 0);
1695 if (xkb_info->keymap_area == MAP_FAILED) {
1696 weston_log("failed to mmap() %lu bytes\n",
1697 (unsigned long) xkb_info->keymap_size);
1698 goto err_dev_zero;
1699 }
1700 strcpy(xkb_info->keymap_area, keymap_str);
1701 free(keymap_str);
1702
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001703 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704
1705err_dev_zero:
1706 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001707err_keymap_str:
1708 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001709err_keymap:
1710 xkb_map_unref(xkb_info->keymap);
1711 free(xkb_info);
1712 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001713}
1714
1715static int
1716weston_compositor_build_global_keymap(struct weston_compositor *ec)
1717{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001718 struct xkb_keymap *keymap;
1719
1720 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001721 return 0;
1722
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001723 keymap = xkb_map_new_from_names(ec->xkb_context,
1724 &ec->xkb_names,
1725 0);
1726 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001727 weston_log("failed to compile global XKB keymap\n");
1728 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1729 "options %s\n",
1730 ec->xkb_names.rules, ec->xkb_names.model,
1731 ec->xkb_names.layout, ec->xkb_names.variant,
1732 ec->xkb_names.options);
1733 return -1;
1734 }
1735
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001736 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001737 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001738 return -1;
1739
1740 return 0;
1741}
Rob Bradford382ff462013-06-24 16:52:45 +01001742#else
1743int
1744weston_compositor_xkb_init(struct weston_compositor *ec,
1745 struct xkb_rule_names *names)
1746{
1747 return 0;
1748}
1749
1750void
1751weston_compositor_xkb_destroy(struct weston_compositor *ec)
1752{
1753}
1754#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001755
Rui Matos65196bc2013-10-10 19:44:19 +02001756WL_EXPORT void
1757weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1758{
1759 if (!seat->keyboard || !keymap)
1760 return;
1761
1762#ifdef ENABLE_XKBCOMMON
1763 if (!seat->compositor->use_xkbcommon)
1764 return;
1765
1766 xkb_keymap_unref(seat->pending_keymap);
1767 seat->pending_keymap = xkb_keymap_ref(keymap);
1768
1769 if (seat->keyboard->keys.size == 0)
1770 update_keymap(seat);
1771#endif
1772}
1773
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001774WL_EXPORT int
1775weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1776{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001777 struct weston_keyboard *keyboard;
1778
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001779 if (seat->keyboard) {
1780 seat->keyboard_device_count += 1;
1781 if (seat->keyboard_device_count == 1)
1782 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001783 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001784 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001785
Rob Bradford382ff462013-06-24 16:52:45 +01001786#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001787 if (seat->compositor->use_xkbcommon) {
1788 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001789 seat->xkb_info = weston_xkb_info_create(keymap);
1790 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001791 return -1;
1792 } else {
1793 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1794 return -1;
1795 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001796 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001797 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001798
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001799 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001800 if (seat->xkb_state.state == NULL) {
1801 weston_log("failed to initialise XKB state\n");
1802 return -1;
1803 }
1804
1805 seat->xkb_state.leds = 0;
1806 }
Rob Bradford382ff462013-06-24 16:52:45 +01001807#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001808
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001809 keyboard = weston_keyboard_create();
1810 if (keyboard == NULL) {
1811 weston_log("failed to allocate weston keyboard struct\n");
1812 return -1;
1813 }
1814
1815 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001816 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001817 keyboard->seat = seat;
1818
1819 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001820
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001821 return 0;
1822}
1823
1824WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001825weston_seat_release_keyboard(struct weston_seat *seat)
1826{
1827 seat->keyboard_device_count--;
1828 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001829 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001830 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001831 seat_send_updated_caps(seat);
1832 }
1833}
1834
1835WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001836weston_seat_init_pointer(struct weston_seat *seat)
1837{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001838 struct weston_pointer *pointer;
1839
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001840 if (seat->pointer) {
1841 seat->pointer_device_count += 1;
1842 if (seat->pointer_device_count == 1)
1843 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001845 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001846
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001847 pointer = weston_pointer_create();
1848 if (pointer == NULL)
1849 return;
1850
1851 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001852 seat->pointer_device_count = 1;
1853 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001854
1855 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001856}
1857
1858WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001859weston_seat_release_pointer(struct weston_seat *seat)
1860{
1861 struct weston_pointer *pointer = seat->pointer;
1862
1863 seat->pointer_device_count--;
1864 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001865 weston_pointer_set_focus(pointer, NULL,
1866 wl_fixed_from_int(0),
1867 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001868 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001869
Jonas Ådahla4932742013-10-17 23:04:07 +02001870 if (pointer->sprite)
1871 pointer_unmap_sprite(pointer);
1872
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001873 seat_send_updated_caps(seat);
1874 }
1875}
1876
1877WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001878weston_seat_init_touch(struct weston_seat *seat)
1879{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001880 struct weston_touch *touch;
1881
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001882 if (seat->touch) {
1883 seat->touch_device_count += 1;
1884 if (seat->touch_device_count == 1)
1885 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001887 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001888
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001889 touch = weston_touch_create();
1890 if (touch == NULL)
1891 return;
1892
1893 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001894 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001895 touch->seat = seat;
1896
1897 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001898}
1899
1900WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001901weston_seat_release_touch(struct weston_seat *seat)
1902{
1903 seat->touch_device_count--;
1904 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001905 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001906 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001907 seat_send_updated_caps(seat);
1908 }
1909}
1910
1911WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001912weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1913 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001914{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001915 memset(seat, 0, sizeof *seat);
1916
Kristian Høgsberge3148752013-05-06 23:19:49 -04001917 seat->selection_data_source = NULL;
1918 wl_list_init(&seat->base_resource_list);
1919 wl_signal_init(&seat->selection_signal);
1920 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001921 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001922
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001923 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001924 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001925
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001926 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001927 seat->modifier_state = 0;
1928 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001929 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001930
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001931 wl_list_insert(ec->seat_list.prev, &seat->link);
1932
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933 clipboard_create(seat);
1934
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001935 wl_signal_emit(&ec->seat_created_signal, seat);
1936}
1937
1938WL_EXPORT void
1939weston_seat_release(struct weston_seat *seat)
1940{
1941 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001942
Rob Bradford382ff462013-06-24 16:52:45 +01001943#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001944 if (seat->compositor->use_xkbcommon) {
1945 if (seat->xkb_state.state != NULL)
1946 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001947 if (seat->xkb_info)
1948 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001949 if (seat->pending_keymap)
1950 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001951 }
Rob Bradford382ff462013-06-24 16:52:45 +01001952#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001953
Kristian Høgsberge3148752013-05-06 23:19:49 -04001954 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001955 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001956 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001957 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001958 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001959 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001960
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001961 free (seat->seat_name);
1962
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001963 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001964
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001965 wl_signal_emit(&seat->destroy_signal, seat);
1966}