blob: 153bcb6febaf541fc916aebeb9ee07dcdf1f7f95 [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);
395
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400396 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
397
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400398 /* FIXME: Pick better co-ords. */
399 pointer->x = wl_fixed_from_int(100);
400 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400401
402 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403}
404
405WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400406weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400407{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400408 if (pointer->sprite)
409 pointer_unmap_sprite(pointer);
410
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400411 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100412
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400413 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400414}
415
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400416WL_EXPORT struct weston_keyboard *
417weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400419 struct weston_keyboard *keyboard;
420
Peter Huttererf3d62272013-08-08 11:57:05 +1000421 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400422 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100423 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400424
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400425 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100426 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400427 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428 keyboard->default_grab.interface = &default_keyboard_grab_interface;
429 keyboard->default_grab.keyboard = keyboard;
430 keyboard->grab = &keyboard->default_grab;
431 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400432
433 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434}
435
436WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400437weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400438{
439 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100440
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400441 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400442 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443}
444
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400445WL_EXPORT struct weston_touch *
446weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400448 struct weston_touch *touch;
449
Peter Huttererf3d62272013-08-08 11:57:05 +1000450 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400451 if (touch == NULL)
452 return NULL;
453
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400454 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100455 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456 touch->default_grab.interface = &default_touch_grab_interface;
457 touch->default_grab.touch = touch;
458 touch->grab = &touch->default_grab;
459 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400460
461 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462}
463
464WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400465weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400466{
467 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100468
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400469 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400470}
471
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400472static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400473seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400474{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100476 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400477
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200478 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200480 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400481 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200482 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483 caps |= WL_SEAT_CAPABILITY_TOUCH;
484
Rob Bradford6e737f52013-09-06 17:48:19 +0100485 wl_resource_for_each(resource, &seat->base_resource_list) {
486 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500487 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400488}
489
490WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400491weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500492 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400493 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400495 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100496 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100497 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400498 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100499 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 int different_surface = 0;
501
502 if ((!pointer->focus && view) ||
503 (pointer->focus && !view) ||
504 (pointer->focus && pointer->focus->surface != view->surface))
505 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400506
Neil Roberts96d790e2013-09-19 17:32:00 +0100507 focus_resource_list = &pointer->focus_resource_list;
508
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400510 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100511 wl_resource_for_each(resource, focus_resource_list) {
512 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100514 }
515
516 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400517 }
518
Jason Ekstranda7af7042013-10-12 22:38:11 -0500519 if (find_resource_for_view(&pointer->resource_list, view) &&
520 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100521 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100523
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100525
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700527 send_modifiers_to_client_in_list(surface_client,
528 &kbd->resource_list,
529 serial,
530 kbd);
531
Neil Roberts96d790e2013-09-19 17:32:00 +0100532 move_resources_for_client(focus_resource_list,
533 &pointer->resource_list,
534 surface_client);
535
536 wl_resource_for_each(resource, focus_resource_list) {
537 wl_pointer_send_enter(resource,
538 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100540 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400541 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100542
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400543 pointer->focus_serial = serial;
544 }
545
Jason Ekstranda7af7042013-10-12 22:38:11 -0500546 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400547 wl_signal_emit(&pointer->focus_signal, pointer);
548}
549
Neil Roberts96d790e2013-09-19 17:32:00 +0100550static void
551send_enter_to_resource_list(struct wl_list *list,
552 struct weston_keyboard *keyboard,
553 struct weston_surface *surface,
554 uint32_t serial)
555{
556 struct wl_resource *resource;
557
558 wl_resource_for_each(resource, list) {
559 send_modifiers_to_resource(keyboard, resource, serial);
560 wl_keyboard_send_enter(resource, serial,
561 surface->resource,
562 &keyboard->keys);
563 }
564}
565
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400566WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400567weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400568 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400569{
570 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100571 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100573 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400574
Neil Roberts96d790e2013-09-19 17:32:00 +0100575 focus_resource_list = &keyboard->focus_resource_list;
576
577 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400578 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100579 wl_resource_for_each(resource, focus_resource_list) {
580 wl_keyboard_send_leave(resource, serial,
581 keyboard->focus->resource);
582 }
583 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400584 }
585
Neil Roberts96d790e2013-09-19 17:32:00 +0100586 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
587 keyboard->focus != surface) {
588 struct wl_client *surface_client =
589 wl_resource_get_client(surface->resource);
590
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100592
593 move_resources_for_client(focus_resource_list,
594 &keyboard->resource_list,
595 surface_client);
596 send_enter_to_resource_list(focus_resource_list,
597 keyboard,
598 surface,
599 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600 keyboard->focus_serial = serial;
601 }
602
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400603 keyboard->focus = surface;
604 wl_signal_emit(&keyboard->focus_signal, keyboard);
605}
606
607WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400608weston_keyboard_start_grab(struct weston_keyboard *keyboard,
609 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400610{
611 keyboard->grab = grab;
612 grab->keyboard = keyboard;
613
614 /* XXX focus? */
615}
616
617WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400618weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619{
620 keyboard->grab = &keyboard->default_grab;
621}
622
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200623static void
624weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
625{
626 keyboard->grab->interface->cancel(keyboard->grab);
627}
628
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400630weston_pointer_start_grab(struct weston_pointer *pointer,
631 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400632{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400634 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400635 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636}
637
638WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400639weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400642 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400643}
644
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200645static void
646weston_pointer_cancel_grab(struct weston_pointer *pointer)
647{
648 pointer->grab->interface->cancel(pointer->grab);
649}
650
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400651WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400652weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400653{
654 touch->grab = grab;
655 grab->touch = touch;
656}
657
658WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400659weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400660{
661 touch->grab = &touch->default_grab;
662}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400663
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200664static void
665weston_touch_cancel_grab(struct weston_touch *touch)
666{
667 touch->grab->interface->cancel(touch->grab);
668}
669
Rob Bradford806d8c02013-06-25 18:56:41 +0100670WL_EXPORT void
671weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400672{
Rob Bradford806d8c02013-06-25 18:56:41 +0100673 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400674 struct weston_output *output, *prev = NULL;
675 int x, y, old_x, old_y, valid = 0;
676
677 x = wl_fixed_to_int(*fx);
678 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100679 old_x = wl_fixed_to_int(pointer->x);
680 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400681
682 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100683 if (pointer->seat->output && pointer->seat->output != output)
684 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400685 if (pixman_region32_contains_point(&output->region,
686 x, y, NULL))
687 valid = 1;
688 if (pixman_region32_contains_point(&output->region,
689 old_x, old_y, NULL))
690 prev = output;
691 }
692
Rob Bradford66bd9f52013-06-25 18:56:42 +0100693 if (!prev)
694 prev = pointer->seat->output;
695
696 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400697 if (x < prev->x)
698 *fx = wl_fixed_from_int(prev->x);
699 else if (x >= prev->x + prev->width)
700 *fx = wl_fixed_from_int(prev->x +
701 prev->width - 1);
702 if (y < prev->y)
703 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200704 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400705 *fy = wl_fixed_from_int(prev->y +
706 prev->height - 1);
707 }
708}
709
710/* Takes absolute values */
711static void
712move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
713{
714 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400715 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400716 struct weston_output *output;
717 int32_t ix, iy;
718
Rob Bradford806d8c02013-06-25 18:56:41 +0100719 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400720
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400721 pointer->x = x;
722 pointer->y = y;
723
724 ix = wl_fixed_to_int(x);
725 iy = wl_fixed_to_int(y);
726
727 wl_list_for_each(output, &ec->output_list, link)
728 if (output->zoom.active &&
729 pixman_region32_contains_point(&output->region,
730 ix, iy, NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 weston_output_update_zoom(output);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400732
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400733 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500734 weston_view_set_position(pointer->sprite,
735 ix - pointer->hotspot_x,
736 iy - pointer->hotspot_y);
737 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400738 }
739}
740
741WL_EXPORT void
742notify_motion(struct weston_seat *seat,
743 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
744{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400745 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400746 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400747
748 weston_compositor_wake(ec);
749
750 move_pointer(seat, pointer->x + dx, pointer->y + dy);
751
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400752 pointer->grab->interface->focus(pointer->grab);
753 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400754}
755
756WL_EXPORT void
757notify_motion_absolute(struct weston_seat *seat,
758 uint32_t time, wl_fixed_t x, wl_fixed_t y)
759{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400760 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400761 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400762
763 weston_compositor_wake(ec);
764
765 move_pointer(seat, x, y);
766
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400767 pointer->grab->interface->focus(pointer->grab);
768 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400769}
770
771WL_EXPORT void
772weston_surface_activate(struct weston_surface *surface,
773 struct weston_seat *seat)
774{
775 struct weston_compositor *compositor = seat->compositor;
776
Kristian Høgsberge3148752013-05-06 23:19:49 -0400777 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400778 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400779 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400780 }
781
782 wl_signal_emit(&compositor->activate_signal, surface);
783}
784
785WL_EXPORT void
786notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
787 enum wl_pointer_button_state state)
788{
789 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400790 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400791 struct weston_surface *focus =
792 (struct weston_surface *) pointer->focus;
793 uint32_t serial = wl_display_next_serial(compositor->wl_display);
794
795 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
796 if (compositor->ping_handler && focus)
797 compositor->ping_handler(focus, serial);
798 weston_compositor_idle_inhibit(compositor);
799 if (pointer->button_count == 0) {
800 pointer->grab_button = button;
801 pointer->grab_time = time;
802 pointer->grab_x = pointer->x;
803 pointer->grab_y = pointer->y;
804 }
805 pointer->button_count++;
806 } else {
807 weston_compositor_idle_release(compositor);
808 pointer->button_count--;
809 }
810
811 weston_compositor_run_button_binding(compositor, seat, time, button,
812 state);
813
814 pointer->grab->interface->button(pointer->grab, time, button, state);
815
816 if (pointer->button_count == 1)
817 pointer->grab_serial =
818 wl_display_get_serial(compositor->wl_display);
819}
820
821WL_EXPORT void
822notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
823 wl_fixed_t value)
824{
825 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400826 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400827 struct weston_surface *focus =
828 (struct weston_surface *) pointer->focus;
829 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100830 struct wl_resource *resource;
831 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400832
833 if (compositor->ping_handler && focus)
834 compositor->ping_handler(focus, serial);
835
836 weston_compositor_wake(compositor);
837
838 if (!value)
839 return;
840
841 if (weston_compositor_run_axis_binding(compositor, seat,
842 time, axis, value))
843 return;
844
Neil Roberts96d790e2013-09-19 17:32:00 +0100845 resource_list = &pointer->focus_resource_list;
846 wl_resource_for_each(resource, resource_list)
847 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400848 value);
849}
850
Rob Bradford382ff462013-06-24 16:52:45 +0100851#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400852WL_EXPORT void
853notify_modifiers(struct weston_seat *seat, uint32_t serial)
854{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400855 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400856 struct weston_keyboard_grab *grab = keyboard->grab;
857 uint32_t mods_depressed, mods_latched, mods_locked, group;
858 uint32_t mods_lookup;
859 enum weston_led leds = 0;
860 int changed = 0;
861
862 /* Serialize and update our internal state, checking to see if it's
863 * different to the previous state. */
864 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
865 XKB_STATE_DEPRESSED);
866 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
867 XKB_STATE_LATCHED);
868 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
869 XKB_STATE_LOCKED);
870 group = xkb_state_serialize_group(seat->xkb_state.state,
871 XKB_STATE_EFFECTIVE);
872
Kristian Høgsberge3148752013-05-06 23:19:49 -0400873 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
874 mods_latched != seat->keyboard->modifiers.mods_latched ||
875 mods_locked != seat->keyboard->modifiers.mods_locked ||
876 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400877 changed = 1;
878
Kristian Høgsberge3148752013-05-06 23:19:49 -0400879 seat->keyboard->modifiers.mods_depressed = mods_depressed;
880 seat->keyboard->modifiers.mods_latched = mods_latched;
881 seat->keyboard->modifiers.mods_locked = mods_locked;
882 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400883
884 /* And update the modifier_state for bindings. */
885 mods_lookup = mods_depressed | mods_latched;
886 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000887 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400888 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000889 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400890 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000891 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400892 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000893 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400894 seat->modifier_state |= MODIFIER_SHIFT;
895
896 /* Finally, notify the compositor that LEDs have changed. */
897 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000898 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400899 leds |= LED_NUM_LOCK;
900 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000901 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400902 leds |= LED_CAPS_LOCK;
903 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000904 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400905 leds |= LED_SCROLL_LOCK;
906 if (leds != seat->xkb_state.leds && seat->led_update)
907 seat->led_update(seat, leds);
908 seat->xkb_state.leds = leds;
909
910 if (changed) {
911 grab->interface->modifiers(grab,
912 serial,
913 keyboard->modifiers.mods_depressed,
914 keyboard->modifiers.mods_latched,
915 keyboard->modifiers.mods_locked,
916 keyboard->modifiers.group);
917 }
918}
919
920static void
921update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
922 enum wl_keyboard_key_state state)
923{
924 enum xkb_key_direction direction;
925
Matt Roper01a92732013-06-24 16:52:44 +0100926 /* Keyboard modifiers don't exist in raw keyboard mode */
927 if (!seat->compositor->use_xkbcommon)
928 return;
929
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400930 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
931 direction = XKB_KEY_DOWN;
932 else
933 direction = XKB_KEY_UP;
934
935 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
936 * broken keycode system, which starts at 8. */
937 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
938
939 notify_modifiers(seat, serial);
940}
Rui Matos65196bc2013-10-10 19:44:19 +0200941
942static void
943send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
944{
945 wl_keyboard_send_keymap(resource,
946 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
947 xkb_info->keymap_fd,
948 xkb_info->keymap_size);
949}
950
951static void
952send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
953{
954 wl_keyboard_send_modifiers(resource, serial,
955 keyboard->modifiers.mods_depressed,
956 keyboard->modifiers.mods_latched,
957 keyboard->modifiers.mods_locked,
958 keyboard->modifiers.group);
959}
960
961static struct weston_xkb_info *
962weston_xkb_info_create(struct xkb_keymap *keymap);
963static void
964weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
965
966static void
967update_keymap(struct weston_seat *seat)
968{
969 struct wl_resource *resource;
970 struct weston_xkb_info *xkb_info;
971 struct xkb_state *state;
972 xkb_mod_mask_t latched_mods;
973 xkb_mod_mask_t locked_mods;
974
975 xkb_info = weston_xkb_info_create(seat->pending_keymap);
976
977 xkb_keymap_unref(seat->pending_keymap);
978 seat->pending_keymap = NULL;
979
980 if (!xkb_info) {
981 weston_log("failed to create XKB info\n");
982 return;
983 }
984
985 state = xkb_state_new(xkb_info->keymap);
986 if (!state) {
987 weston_log("failed to initialise XKB state\n");
988 weston_xkb_info_destroy(xkb_info);
989 return;
990 }
991
992 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
993 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
994 xkb_state_update_mask(state,
995 0, /* depressed */
996 latched_mods,
997 locked_mods,
998 0, 0, 0);
999
1000 weston_xkb_info_destroy(seat->xkb_info);
1001 seat->xkb_info = xkb_info;
1002
1003 xkb_state_unref(seat->xkb_state.state);
1004 seat->xkb_state.state = state;
1005
1006 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1007 send_keymap(resource, xkb_info);
1008 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1009 send_keymap(resource, xkb_info);
1010
1011 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1012
1013 if (!latched_mods && !locked_mods)
1014 return;
1015
1016 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1017 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1018 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1019 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1020}
Rob Bradford382ff462013-06-24 16:52:45 +01001021#else
1022WL_EXPORT void
1023notify_modifiers(struct weston_seat *seat, uint32_t serial)
1024{
1025}
1026
1027static void
1028update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1029 enum wl_keyboard_key_state state)
1030{
1031}
Rui Matos65196bc2013-10-10 19:44:19 +02001032
1033static void
1034update_keymap(struct weston_seat *seat)
1035{
1036}
Rob Bradford382ff462013-06-24 16:52:45 +01001037#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001038
1039WL_EXPORT void
1040notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1041 enum wl_keyboard_key_state state,
1042 enum weston_key_state_update update_state)
1043{
1044 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001045 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001047 struct weston_keyboard_grab *grab = keyboard->grab;
1048 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1049 uint32_t *k, *end;
1050
1051 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1052 if (compositor->ping_handler && focus)
1053 compositor->ping_handler(focus, serial);
1054
1055 weston_compositor_idle_inhibit(compositor);
1056 keyboard->grab_key = key;
1057 keyboard->grab_time = time;
1058 } else {
1059 weston_compositor_idle_release(compositor);
1060 }
1061
1062 end = keyboard->keys.data + keyboard->keys.size;
1063 for (k = keyboard->keys.data; k < end; k++) {
1064 if (*k == key) {
1065 /* Ignore server-generated repeats. */
1066 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1067 return;
1068 *k = *--end;
1069 }
1070 }
1071 keyboard->keys.size = (void *) end - keyboard->keys.data;
1072 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1073 k = wl_array_add(&keyboard->keys, sizeof *k);
1074 *k = key;
1075 }
1076
1077 if (grab == &keyboard->default_grab ||
1078 grab == &keyboard->input_method_grab) {
1079 weston_compositor_run_key_binding(compositor, seat, time, key,
1080 state);
1081 grab = keyboard->grab;
1082 }
1083
1084 grab->interface->key(grab, time, key, state);
1085
Rui Matos65196bc2013-10-10 19:44:19 +02001086 if (seat->pending_keymap &&
1087 keyboard->keys.size == 0)
1088 update_keymap(seat);
1089
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001090 if (update_state == STATE_UPDATE_AUTOMATIC) {
1091 update_modifier_state(seat,
1092 wl_display_get_serial(compositor->wl_display),
1093 key,
1094 state);
1095 }
1096}
1097
1098WL_EXPORT void
1099notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1100 wl_fixed_t x, wl_fixed_t y)
1101{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001102 if (output) {
1103 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001104 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001105 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001106 * NULL) here, but somehow that breaks re-entry... */
1107 }
1108}
1109
1110static void
1111destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1112{
1113 struct weston_seat *ws;
1114
1115 ws = container_of(listener, struct weston_seat,
1116 saved_kbd_focus_listener);
1117
1118 ws->saved_kbd_focus = NULL;
1119}
1120
1121WL_EXPORT void
1122notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1123 enum weston_key_state_update update_state)
1124{
1125 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001126 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001127 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001128 uint32_t *k, serial;
1129
1130 serial = wl_display_next_serial(compositor->wl_display);
1131 wl_array_copy(&keyboard->keys, keys);
1132 wl_array_for_each(k, &keyboard->keys) {
1133 weston_compositor_idle_inhibit(compositor);
1134 if (update_state == STATE_UPDATE_AUTOMATIC)
1135 update_modifier_state(seat, serial, *k,
1136 WL_KEYBOARD_KEY_STATE_PRESSED);
1137 }
1138
1139 /* Run key bindings after we've updated the state. */
1140 wl_array_for_each(k, &keyboard->keys) {
1141 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1142 WL_KEYBOARD_KEY_STATE_PRESSED);
1143 }
1144
1145 surface = seat->saved_kbd_focus;
1146
1147 if (surface) {
1148 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1149 weston_keyboard_set_focus(keyboard, surface);
1150 seat->saved_kbd_focus = NULL;
1151 }
1152}
1153
1154WL_EXPORT void
1155notify_keyboard_focus_out(struct weston_seat *seat)
1156{
1157 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001158 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001159 uint32_t *k, serial;
1160
1161 serial = wl_display_next_serial(compositor->wl_display);
1162 wl_array_for_each(k, &keyboard->keys) {
1163 weston_compositor_idle_release(compositor);
1164 update_modifier_state(seat, serial, *k,
1165 WL_KEYBOARD_KEY_STATE_RELEASED);
1166 }
1167
1168 seat->modifier_state = 0;
1169
1170 if (keyboard->focus) {
1171 seat->saved_kbd_focus = keyboard->focus;
1172 seat->saved_kbd_focus_listener.notify =
1173 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001174 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001175 &seat->saved_kbd_focus_listener);
1176 }
1177
1178 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001179 weston_keyboard_cancel_grab(keyboard);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001180}
1181
Michael Fua2bb7912013-07-23 15:51:06 +08001182WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001184{
Neil Roberts96d790e2013-09-19 17:32:00 +01001185 struct wl_list *focus_resource_list;
1186
1187 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001188
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001189 if (view && seat->touch->focus &&
1190 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001192 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194
Neil Roberts96d790e2013-09-19 17:32:00 +01001195 if (!wl_list_empty(focus_resource_list)) {
1196 move_resources(&seat->touch->resource_list,
1197 focus_resource_list);
1198 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001201 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001203 move_resources_for_client(focus_resource_list,
1204 &seat->touch->resource_list,
1205 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001206 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001208}
1209
1210/**
1211 * notify_touch - emulates button touches and notifies surfaces accordingly.
1212 *
1213 * It assumes always the correct cycle sequence until it gets here: touch_down
1214 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1215 * for sending along such order.
1216 *
1217 */
1218WL_EXPORT void
1219notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1220 wl_fixed_t x, wl_fixed_t y, int touch_type)
1221{
1222 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001223 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001224 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001226 wl_fixed_t sx, sy;
1227
1228 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001229 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1230 touch->grab_x = x;
1231 touch->grab_y = y;
1232 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001233
1234 switch (touch_type) {
1235 case WL_TOUCH_DOWN:
1236 weston_compositor_idle_inhibit(ec);
1237
1238 seat->num_tp++;
1239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 /* the first finger down picks the view, and all further go
1241 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242 * until all touch points are up again. */
1243 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1245 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001246 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 ev = touch->focus;
1248 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001249 } else {
1250 /* Unexpected condition: We have non-initial touch but
1251 * there is no focused surface.
1252 */
1253 weston_log("touch event received with %d points down"
1254 "but no surface focused\n", seat->num_tp);
1255 return;
1256 }
1257
1258 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001259 if (seat->num_tp == 1) {
1260 touch->grab_serial =
1261 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001262 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001263 touch->grab_time = time;
1264 touch->grab_x = x;
1265 touch->grab_y = y;
1266 }
1267
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001268 break;
1269 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 ev = touch->focus;
1271 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001272 break;
1273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275 grab->interface->motion(grab, time, touch_id, sx, sy);
1276 break;
1277 case WL_TOUCH_UP:
1278 weston_compositor_idle_release(ec);
1279 seat->num_tp--;
1280
1281 grab->interface->up(grab, time, touch_id);
1282 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001283 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001284 break;
1285 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001286
1287 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001288}
1289
1290static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291pointer_cursor_surface_configure(struct weston_surface *es,
1292 int32_t dx, int32_t dy, int32_t width, int32_t height)
1293{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001294 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001295 int x, y;
1296
1297 if (width == 0)
1298 return;
1299
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001301
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001302 pointer->hotspot_x -= dx;
1303 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001304
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001305 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1306 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001309
1310 empty_region(&es->pending.input);
1311
1312 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 wl_list_insert(&es->compositor->cursor_layer.view_list,
1314 &pointer->sprite->layer_link);
1315 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001316 }
1317}
1318
1319static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001320pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1321 uint32_t serial, struct wl_resource *surface_resource,
1322 int32_t x, int32_t y)
1323{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001324 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001325 struct weston_surface *surface = NULL;
1326
1327 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001328 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001329
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001330 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001331 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001333 black_surface used in shell.c for fullscreen don't have
1334 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001336 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001338 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001339 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001340 return;
1341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001343 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001344 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001345 WL_DISPLAY_ERROR_INVALID_OBJECT,
1346 "surface->configure already "
1347 "set");
1348 return;
1349 }
1350 }
1351
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001352 if (pointer->sprite)
1353 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001354
1355 if (!surface)
1356 return;
1357
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001358 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001359 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001360
1361 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001362 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001364 pointer->hotspot_x = x;
1365 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001366
1367 if (surface->buffer_ref.buffer)
1368 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1369 weston_surface_buffer_height(surface));
1370}
1371
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001372static void
1373pointer_release(struct wl_client *client, struct wl_resource *resource)
1374{
1375 wl_resource_destroy(resource);
1376}
1377
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001378static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001379 pointer_set_cursor,
1380 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001381};
1382
1383static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001384seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1385 uint32_t id)
1386{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001387 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001388 struct wl_resource *cr;
1389
Kristian Høgsberge3148752013-05-06 23:19:49 -04001390 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001391 return;
1392
Jason Ekstranda85118c2013-06-27 20:17:02 -05001393 cr = wl_resource_create(client, &wl_pointer_interface,
1394 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001395 if (cr == NULL) {
1396 wl_client_post_no_memory(client);
1397 return;
1398 }
1399
Neil Roberts96d790e2013-09-19 17:32:00 +01001400 /* May be moved to focused list later by either
1401 * weston_pointer_set_focus or directly if this client is already
1402 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001403 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001404 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1405 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1408 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001409 wl_fixed_t sx, sy;
1410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 weston_view_from_global_fixed(seat->pointer->focus,
1412 seat->pointer->x,
1413 seat->pointer->y,
1414 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001415
1416 wl_list_remove(wl_resource_get_link(cr));
1417 wl_list_insert(&seat->pointer->focus_resource_list,
1418 wl_resource_get_link(cr));
1419 wl_pointer_send_enter(cr,
1420 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001422 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001423 }
1424}
1425
1426static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001427keyboard_release(struct wl_client *client, struct wl_resource *resource)
1428{
1429 wl_resource_destroy(resource);
1430}
1431
1432static const struct wl_keyboard_interface keyboard_interface = {
1433 keyboard_release
1434};
1435
Neil Roberts96d790e2013-09-19 17:32:00 +01001436static int
1437should_send_modifiers_to_client(struct weston_seat *seat,
1438 struct wl_client *client)
1439{
1440 if (seat->keyboard &&
1441 seat->keyboard->focus &&
1442 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1443 return 1;
1444
1445 if (seat->pointer &&
1446 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001448 return 1;
1449
1450 return 0;
1451}
1452
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001453static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001454seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1455 uint32_t id)
1456{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001457 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001458 struct wl_resource *cr;
1459
Kristian Høgsberge3148752013-05-06 23:19:49 -04001460 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001461 return;
1462
Jason Ekstranda85118c2013-06-27 20:17:02 -05001463 cr = wl_resource_create(client, &wl_keyboard_interface,
1464 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001465 if (cr == NULL) {
1466 wl_client_post_no_memory(client);
1467 return;
1468 }
1469
Neil Roberts96d790e2013-09-19 17:32:00 +01001470 /* May be moved to focused list later by either
1471 * weston_keyboard_set_focus or directly if this client is already
1472 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001473 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001474 wl_resource_set_implementation(cr, &keyboard_interface,
1475 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001476
Matt Roper01a92732013-06-24 16:52:44 +01001477 if (seat->compositor->use_xkbcommon) {
1478 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001479 seat->xkb_info->keymap_fd,
1480 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001481 } else {
1482 int null_fd = open("/dev/null", O_RDONLY);
1483 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1484 null_fd,
1485 0);
1486 close(null_fd);
1487 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001488
Neil Roberts96d790e2013-09-19 17:32:00 +01001489 if (should_send_modifiers_to_client(seat, client)) {
1490 send_modifiers_to_resource(seat->keyboard,
1491 cr,
1492 seat->keyboard->focus_serial);
1493 }
1494
Kristian Høgsberge3148752013-05-06 23:19:49 -04001495 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001496 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001497 struct weston_surface *surface =
1498 (struct weston_surface *) seat->keyboard->focus;
1499
1500 wl_list_remove(wl_resource_get_link(cr));
1501 wl_list_insert(&seat->keyboard->focus_resource_list,
1502 wl_resource_get_link(cr));
1503 wl_keyboard_send_enter(cr,
1504 seat->keyboard->focus_serial,
1505 surface->resource,
1506 &seat->keyboard->keys);
1507
1508 /* If this is the first keyboard resource for this
1509 * client... */
1510 if (seat->keyboard->focus_resource_list.prev ==
1511 wl_resource_get_link(cr))
1512 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001513 }
1514}
1515
1516static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001517touch_release(struct wl_client *client, struct wl_resource *resource)
1518{
1519 wl_resource_destroy(resource);
1520}
1521
1522static const struct wl_touch_interface touch_interface = {
1523 touch_release
1524};
1525
1526static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001527seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1528 uint32_t id)
1529{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001530 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001531 struct wl_resource *cr;
1532
Kristian Høgsberge3148752013-05-06 23:19:49 -04001533 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001534 return;
1535
Jason Ekstranda85118c2013-06-27 20:17:02 -05001536 cr = wl_resource_create(client, &wl_touch_interface,
1537 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001538 if (cr == NULL) {
1539 wl_client_post_no_memory(client);
1540 return;
1541 }
1542
Neil Roberts96d790e2013-09-19 17:32:00 +01001543 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001545 wl_list_insert(&seat->touch->resource_list,
1546 wl_resource_get_link(cr));
1547 } else {
1548 wl_list_insert(&seat->touch->focus_resource_list,
1549 wl_resource_get_link(cr));
1550 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001551 wl_resource_set_implementation(cr, &touch_interface,
1552 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553}
1554
1555static const struct wl_seat_interface seat_interface = {
1556 seat_get_pointer,
1557 seat_get_keyboard,
1558 seat_get_touch,
1559};
1560
1561static void
1562bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1563{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001564 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001565 struct wl_resource *resource;
1566 enum wl_seat_capability caps = 0;
1567
Jason Ekstranda85118c2013-06-27 20:17:02 -05001568 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001569 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001570 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001571 wl_resource_set_implementation(resource, &seat_interface, data,
1572 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001573
1574 if (seat->pointer)
1575 caps |= WL_SEAT_CAPABILITY_POINTER;
1576 if (seat->keyboard)
1577 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1578 if (seat->touch)
1579 caps |= WL_SEAT_CAPABILITY_TOUCH;
1580
1581 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001582 if (version >= 2)
1583 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001584}
1585
Rob Bradford382ff462013-06-24 16:52:45 +01001586#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001587int
1588weston_compositor_xkb_init(struct weston_compositor *ec,
1589 struct xkb_rule_names *names)
1590{
Rob Bradford382ff462013-06-24 16:52:45 +01001591 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001592
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001593 if (ec->xkb_context == NULL) {
1594 ec->xkb_context = xkb_context_new(0);
1595 if (ec->xkb_context == NULL) {
1596 weston_log("failed to create XKB context\n");
1597 return -1;
1598 }
1599 }
1600
1601 if (names)
1602 ec->xkb_names = *names;
1603 if (!ec->xkb_names.rules)
1604 ec->xkb_names.rules = strdup("evdev");
1605 if (!ec->xkb_names.model)
1606 ec->xkb_names.model = strdup("pc105");
1607 if (!ec->xkb_names.layout)
1608 ec->xkb_names.layout = strdup("us");
1609
1610 return 0;
1611}
1612
Stefan Schmidtfda26522013-09-17 10:54:09 +01001613static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001614weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001615{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001616 if (--xkb_info->ref_count > 0)
1617 return;
1618
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001619 if (xkb_info->keymap)
1620 xkb_map_unref(xkb_info->keymap);
1621
1622 if (xkb_info->keymap_area)
1623 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1624 if (xkb_info->keymap_fd >= 0)
1625 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001626 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627}
1628
1629void
1630weston_compositor_xkb_destroy(struct weston_compositor *ec)
1631{
Matt Roper01a92732013-06-24 16:52:44 +01001632 /*
1633 * If we're operating in raw keyboard mode, we never initialized
1634 * libxkbcommon so there's no cleanup to do either.
1635 */
1636 if (!ec->use_xkbcommon)
1637 return;
1638
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001639 free((char *) ec->xkb_names.rules);
1640 free((char *) ec->xkb_names.model);
1641 free((char *) ec->xkb_names.layout);
1642 free((char *) ec->xkb_names.variant);
1643 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001644
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001645 if (ec->xkb_info)
1646 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001647 xkb_context_unref(ec->xkb_context);
1648}
1649
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001650static struct weston_xkb_info *
1651weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001652{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001653 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1654 if (xkb_info == NULL)
1655 return NULL;
1656
1657 xkb_info->keymap = xkb_map_ref(keymap);
1658 xkb_info->ref_count = 1;
1659
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001660 char *keymap_str;
1661
1662 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1663 XKB_MOD_NAME_SHIFT);
1664 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1665 XKB_MOD_NAME_CAPS);
1666 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1667 XKB_MOD_NAME_CTRL);
1668 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1669 XKB_MOD_NAME_ALT);
1670 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1671 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1672 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1673 XKB_MOD_NAME_LOGO);
1674 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1675
1676 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1677 XKB_LED_NAME_NUM);
1678 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1679 XKB_LED_NAME_CAPS);
1680 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1681 XKB_LED_NAME_SCROLL);
1682
1683 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1684 if (keymap_str == NULL) {
1685 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001686 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001687 }
1688 xkb_info->keymap_size = strlen(keymap_str) + 1;
1689
1690 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1691 if (xkb_info->keymap_fd < 0) {
1692 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1693 (unsigned long) xkb_info->keymap_size);
1694 goto err_keymap_str;
1695 }
1696
1697 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1698 PROT_READ | PROT_WRITE,
1699 MAP_SHARED, xkb_info->keymap_fd, 0);
1700 if (xkb_info->keymap_area == MAP_FAILED) {
1701 weston_log("failed to mmap() %lu bytes\n",
1702 (unsigned long) xkb_info->keymap_size);
1703 goto err_dev_zero;
1704 }
1705 strcpy(xkb_info->keymap_area, keymap_str);
1706 free(keymap_str);
1707
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001708 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709
1710err_dev_zero:
1711 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001712err_keymap_str:
1713 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001714err_keymap:
1715 xkb_map_unref(xkb_info->keymap);
1716 free(xkb_info);
1717 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001718}
1719
1720static int
1721weston_compositor_build_global_keymap(struct weston_compositor *ec)
1722{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001723 struct xkb_keymap *keymap;
1724
1725 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726 return 0;
1727
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001728 keymap = xkb_map_new_from_names(ec->xkb_context,
1729 &ec->xkb_names,
1730 0);
1731 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001732 weston_log("failed to compile global XKB keymap\n");
1733 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1734 "options %s\n",
1735 ec->xkb_names.rules, ec->xkb_names.model,
1736 ec->xkb_names.layout, ec->xkb_names.variant,
1737 ec->xkb_names.options);
1738 return -1;
1739 }
1740
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001741 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001742 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001743 return -1;
1744
1745 return 0;
1746}
Rob Bradford382ff462013-06-24 16:52:45 +01001747#else
1748int
1749weston_compositor_xkb_init(struct weston_compositor *ec,
1750 struct xkb_rule_names *names)
1751{
1752 return 0;
1753}
1754
1755void
1756weston_compositor_xkb_destroy(struct weston_compositor *ec)
1757{
1758}
1759#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001760
Rui Matos65196bc2013-10-10 19:44:19 +02001761WL_EXPORT void
1762weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1763{
1764 if (!seat->keyboard || !keymap)
1765 return;
1766
1767#ifdef ENABLE_XKBCOMMON
1768 if (!seat->compositor->use_xkbcommon)
1769 return;
1770
1771 xkb_keymap_unref(seat->pending_keymap);
1772 seat->pending_keymap = xkb_keymap_ref(keymap);
1773
1774 if (seat->keyboard->keys.size == 0)
1775 update_keymap(seat);
1776#endif
1777}
1778
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001779WL_EXPORT int
1780weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1781{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001782 struct weston_keyboard *keyboard;
1783
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001784 if (seat->keyboard) {
1785 seat->keyboard_device_count += 1;
1786 if (seat->keyboard_device_count == 1)
1787 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001788 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001789 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001790
Rob Bradford382ff462013-06-24 16:52:45 +01001791#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001792 if (seat->compositor->use_xkbcommon) {
1793 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001794 seat->xkb_info = weston_xkb_info_create(keymap);
1795 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001796 return -1;
1797 } else {
1798 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1799 return -1;
1800 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001801 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001802 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001803
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001804 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001805 if (seat->xkb_state.state == NULL) {
1806 weston_log("failed to initialise XKB state\n");
1807 return -1;
1808 }
1809
1810 seat->xkb_state.leds = 0;
1811 }
Rob Bradford382ff462013-06-24 16:52:45 +01001812#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001813
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001814 keyboard = weston_keyboard_create();
1815 if (keyboard == NULL) {
1816 weston_log("failed to allocate weston keyboard struct\n");
1817 return -1;
1818 }
1819
1820 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001821 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001822 keyboard->seat = seat;
1823
1824 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001826 return 0;
1827}
1828
1829WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001830weston_seat_release_keyboard(struct weston_seat *seat)
1831{
1832 seat->keyboard_device_count--;
1833 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001834 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001835 weston_keyboard_cancel_grab(seat->keyboard);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001836 seat_send_updated_caps(seat);
1837 }
1838}
1839
1840WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001841weston_seat_init_pointer(struct weston_seat *seat)
1842{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001843 struct weston_pointer *pointer;
1844
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001845 if (seat->pointer) {
1846 seat->pointer_device_count += 1;
1847 if (seat->pointer_device_count == 1)
1848 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001849 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001850 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001851
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001852 pointer = weston_pointer_create();
1853 if (pointer == NULL)
1854 return;
1855
1856 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001857 seat->pointer_device_count = 1;
1858 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001859
1860 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001861}
1862
1863WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001864weston_seat_release_pointer(struct weston_seat *seat)
1865{
1866 struct weston_pointer *pointer = seat->pointer;
1867
1868 seat->pointer_device_count--;
1869 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001870 weston_pointer_set_focus(pointer, NULL,
1871 wl_fixed_from_int(0),
1872 wl_fixed_from_int(0));
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001873 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02001874
Jonas Ådahla4932742013-10-17 23:04:07 +02001875 if (pointer->sprite)
1876 pointer_unmap_sprite(pointer);
1877
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001878 seat_send_updated_caps(seat);
1879 }
1880}
1881
1882WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001883weston_seat_init_touch(struct weston_seat *seat)
1884{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001885 struct weston_touch *touch;
1886
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001887 if (seat->touch) {
1888 seat->touch_device_count += 1;
1889 if (seat->touch_device_count == 1)
1890 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001891 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001892 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001894 touch = weston_touch_create();
1895 if (touch == NULL)
1896 return;
1897
1898 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001899 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001900 touch->seat = seat;
1901
1902 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001903}
1904
1905WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001906weston_seat_release_touch(struct weston_seat *seat)
1907{
1908 seat->touch_device_count--;
1909 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001910 weston_touch_set_focus(seat, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001911 weston_touch_cancel_grab(seat->touch);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001912 seat_send_updated_caps(seat);
1913 }
1914}
1915
1916WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001917weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1918 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001919{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001920 memset(seat, 0, sizeof *seat);
1921
Kristian Høgsberge3148752013-05-06 23:19:49 -04001922 seat->selection_data_source = NULL;
1923 wl_list_init(&seat->base_resource_list);
1924 wl_signal_init(&seat->selection_signal);
1925 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001926 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001927
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001928 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001929 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001930
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001931 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001932 seat->modifier_state = 0;
1933 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001934 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001935
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001936 wl_list_insert(ec->seat_list.prev, &seat->link);
1937
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001938 clipboard_create(seat);
1939
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001940 wl_signal_emit(&ec->seat_created_signal, seat);
1941}
1942
1943WL_EXPORT void
1944weston_seat_release(struct weston_seat *seat)
1945{
1946 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001947
Rob Bradford382ff462013-06-24 16:52:45 +01001948#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001949 if (seat->compositor->use_xkbcommon) {
1950 if (seat->xkb_state.state != NULL)
1951 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001952 if (seat->xkb_info)
1953 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001954 if (seat->pending_keymap)
1955 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001956 }
Rob Bradford382ff462013-06-24 16:52:45 +01001957#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001958
Kristian Høgsberge3148752013-05-06 23:19:49 -04001959 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001960 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001961 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001962 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001963 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001964 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001965
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001966 free (seat->seat_name);
1967
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001968 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001969
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001970 wl_signal_emit(&seat->destroy_signal, seat);
1971}