blob: 2fed718faba5c69f5fc940da2659390f6aeaf37d [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øgsberg6848c252013-05-08 22:02:59 -040096default_grab_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øgsbergbe6403e2013-05-08 21:03:21 -0400114default_grab_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øgsberg02bbabb2013-05-06 22:15:05 -0400131default_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400132 uint32_t time, uint32_t button, uint32_t state_w)
133{
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
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400165static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400166 default_pointer_grab_interface = {
167 default_grab_focus,
168 default_grab_motion,
169 default_grab_button
170};
171
Kristian Høgsberge329f362013-05-06 22:19:57 -0400172static void
173default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
174 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400175{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400176 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100177 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400178 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100179 struct wl_resource *resource;
180 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400181
Neil Roberts96d790e2013-09-19 17:32:00 +0100182 resource_list = &touch->focus_resource_list;
183
184 if (!wl_list_empty(resource_list) && touch->focus) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400185 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100186 wl_resource_for_each(resource, resource_list)
187 wl_touch_send_down(resource, serial, time,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500188 touch->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100189 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400190 }
191}
192
Kristian Høgsberge329f362013-05-06 22:19:57 -0400193static void
194default_grab_touch_up(struct weston_touch_grab *grab,
195 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400196{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400197 struct weston_touch *touch = grab->touch;
Rob Bradford880ebc72013-07-22 17:31:38 +0100198 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400199 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100200 struct wl_resource *resource;
201 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400202
Neil Roberts96d790e2013-09-19 17:32:00 +0100203 resource_list = &touch->focus_resource_list;
204
205 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400206 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100207 wl_resource_for_each(resource, resource_list)
208 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400209 }
210}
211
Kristian Høgsberge329f362013-05-06 22:19:57 -0400212static void
213default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
214 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400215{
Kristian Høgsberge329f362013-05-06 22:19:57 -0400216 struct weston_touch *touch = grab->touch;
Neil Roberts96d790e2013-09-19 17:32:00 +0100217 struct wl_resource *resource;
218 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400219
Neil Roberts96d790e2013-09-19 17:32:00 +0100220 resource_list = &touch->focus_resource_list;
221
222 wl_resource_for_each(resource, resource_list) {
223 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400224 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400225 }
226}
227
Kristian Høgsberge329f362013-05-06 22:19:57 -0400228static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400229 default_grab_touch_down,
230 default_grab_touch_up,
231 default_grab_touch_motion
232};
233
234static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400235default_grab_key(struct weston_keyboard_grab *grab,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400236 uint32_t time, uint32_t key, uint32_t state)
237{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400238 struct weston_keyboard *keyboard = grab->keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400239 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100240 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400241 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100242 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400243
Neil Roberts96d790e2013-09-19 17:32:00 +0100244 resource_list = &keyboard->focus_resource_list;
245 if (!wl_list_empty(resource_list)) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400246 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100247 wl_resource_for_each(resource, resource_list)
248 wl_keyboard_send_key(resource,
249 serial,
250 time,
251 key,
252 state);
253 }
254}
255
256static void
257send_modifiers_to_resource(struct weston_keyboard *keyboard,
258 struct wl_resource *resource,
259 uint32_t serial)
260{
261 wl_keyboard_send_modifiers(resource,
262 serial,
263 keyboard->modifiers.mods_depressed,
264 keyboard->modifiers.mods_latched,
265 keyboard->modifiers.mods_locked,
266 keyboard->modifiers.group);
267}
268
269static void
270send_modifiers_to_client_in_list(struct wl_client *client,
271 struct wl_list *list,
272 uint32_t serial,
273 struct weston_keyboard *keyboard)
274{
275 struct wl_resource *resource;
276
277 wl_resource_for_each(resource, list) {
278 if (wl_resource_get_client(resource) == client)
279 send_modifiers_to_resource(keyboard,
280 resource,
281 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400282 }
283}
284
285static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400286find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400287{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400288 if (!surface)
289 return NULL;
290
Jason Ekstrand44a38632013-06-14 10:08:00 -0500291 if (!surface->resource)
292 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100293
Jason Ekstrand44a38632013-06-14 10:08:00 -0500294 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400295}
296
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297static struct wl_resource *
298find_resource_for_view(struct wl_list *list, struct weston_view *view)
299{
300 if (!view)
301 return NULL;
302
303 return find_resource_for_surface(list, view->surface);
304}
305
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400306static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400307default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308 uint32_t mods_depressed, uint32_t mods_latched,
309 uint32_t mods_locked, uint32_t group)
310{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400311 struct weston_keyboard *keyboard = grab->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100312 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
313 struct wl_resource *resource;
314 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400315
Neil Roberts96d790e2013-09-19 17:32:00 +0100316 resource_list = &keyboard->focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400317
Neil Roberts96d790e2013-09-19 17:32:00 +0100318 wl_resource_for_each(resource, resource_list) {
319 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
320 mods_latched, mods_locked, group);
321 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322 if (pointer && pointer->focus && pointer->focus->surface != keyboard->focus) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100323 struct wl_client *pointer_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 wl_resource_get_client(pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100325 send_modifiers_to_client_in_list(pointer_client,
326 &keyboard->resource_list,
327 serial,
328 keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400329 }
330}
331
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400332static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400333 default_keyboard_grab_interface = {
334 default_grab_key,
335 default_grab_modifiers,
336};
337
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400338static void
339pointer_unmap_sprite(struct weston_pointer *pointer)
340{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 if (weston_surface_is_mapped(pointer->sprite->surface))
342 weston_surface_unmap(pointer->sprite->surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400343
344 wl_list_remove(&pointer->sprite_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500345 pointer->sprite->surface->configure = NULL;
346 pointer->sprite->surface->configure_private = NULL;
347 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400348 pointer->sprite = NULL;
349}
350
351static void
352pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
353{
354 struct weston_pointer *pointer =
355 container_of(listener, struct weston_pointer,
356 sprite_destroy_listener);
357
358 pointer->sprite = NULL;
359}
360
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400361WL_EXPORT struct weston_pointer *
362weston_pointer_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400364 struct weston_pointer *pointer;
365
Peter Huttererf3d62272013-08-08 11:57:05 +1000366 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400367 if (pointer == NULL)
368 return NULL;
369
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400370 wl_list_init(&pointer->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100371 wl_list_init(&pointer->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372 pointer->default_grab.interface = &default_pointer_grab_interface;
373 pointer->default_grab.pointer = pointer;
374 pointer->grab = &pointer->default_grab;
375 wl_signal_init(&pointer->focus_signal);
376
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400377 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
378
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379 /* FIXME: Pick better co-ords. */
380 pointer->x = wl_fixed_from_int(100);
381 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400382
383 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400384}
385
386WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400387weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400388{
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400389 if (pointer->sprite)
390 pointer_unmap_sprite(pointer);
391
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400392 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100393
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400394 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400395}
396
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400397WL_EXPORT struct weston_keyboard *
398weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400400 struct weston_keyboard *keyboard;
401
Peter Huttererf3d62272013-08-08 11:57:05 +1000402 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400403 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +0100404 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400405
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400406 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100407 wl_list_init(&keyboard->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400409 keyboard->default_grab.interface = &default_keyboard_grab_interface;
410 keyboard->default_grab.keyboard = keyboard;
411 keyboard->grab = &keyboard->default_grab;
412 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400413
414 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400415}
416
417WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400418weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400419{
420 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100421
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400422 wl_array_release(&keyboard->keys);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400423 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424}
425
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400426WL_EXPORT struct weston_touch *
427weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400428{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400429 struct weston_touch *touch;
430
Peter Huttererf3d62272013-08-08 11:57:05 +1000431 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400432 if (touch == NULL)
433 return NULL;
434
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400435 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +0100436 wl_list_init(&touch->focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400437 touch->default_grab.interface = &default_touch_grab_interface;
438 touch->default_grab.touch = touch;
439 touch->grab = &touch->default_grab;
440 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400441
442 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400443}
444
445WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400446weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400447{
448 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +0100449
Kristian Høgsberga4036bb2013-05-07 23:52:07 -0400450 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451}
452
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400453static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400454seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +0100457 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400458
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200459 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200461 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400462 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200463 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400464 caps |= WL_SEAT_CAPABILITY_TOUCH;
465
Rob Bradford6e737f52013-09-06 17:48:19 +0100466 wl_resource_for_each(resource, &seat->base_resource_list) {
467 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -0500468 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400469}
470
471WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400472weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400474 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400475{
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400476 struct weston_keyboard *kbd = pointer->seat->keyboard;
Neil Roberts96d790e2013-09-19 17:32:00 +0100477 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100478 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400479 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100480 struct wl_list *focus_resource_list;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 int different_surface = 0;
482
483 if ((!pointer->focus && view) ||
484 (pointer->focus && !view) ||
485 (pointer->focus && pointer->focus->surface != view->surface))
486 different_surface = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400487
Neil Roberts96d790e2013-09-19 17:32:00 +0100488 focus_resource_list = &pointer->focus_resource_list;
489
Jason Ekstranda7af7042013-10-12 22:38:11 -0500490 if (!wl_list_empty(focus_resource_list) && different_surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100492 wl_resource_for_each(resource, focus_resource_list) {
493 wl_pointer_send_leave(resource, serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494 pointer->focus->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100495 }
496
497 move_resources(&pointer->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400498 }
499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 if (find_resource_for_view(&pointer->resource_list, view) &&
501 different_surface) {
Neil Roberts96d790e2013-09-19 17:32:00 +0100502 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +0100504
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400505 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100506
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -0700508 send_modifiers_to_client_in_list(surface_client,
509 &kbd->resource_list,
510 serial,
511 kbd);
512
Neil Roberts96d790e2013-09-19 17:32:00 +0100513 move_resources_for_client(focus_resource_list,
514 &pointer->resource_list,
515 surface_client);
516
517 wl_resource_for_each(resource, focus_resource_list) {
518 wl_pointer_send_enter(resource,
519 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500520 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +0100521 sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400522 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100523
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400524 pointer->focus_serial = serial;
525 }
526
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 pointer->focus = view;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400528 wl_signal_emit(&pointer->focus_signal, pointer);
529}
530
Neil Roberts96d790e2013-09-19 17:32:00 +0100531static void
532send_enter_to_resource_list(struct wl_list *list,
533 struct weston_keyboard *keyboard,
534 struct weston_surface *surface,
535 uint32_t serial)
536{
537 struct wl_resource *resource;
538
539 wl_resource_for_each(resource, list) {
540 send_modifiers_to_resource(keyboard, resource, serial);
541 wl_keyboard_send_enter(resource, serial,
542 surface->resource,
543 &keyboard->keys);
544 }
545}
546
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400547WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400548weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400549 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400550{
551 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100552 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100554 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400555
Neil Roberts96d790e2013-09-19 17:32:00 +0100556 focus_resource_list = &keyboard->focus_resource_list;
557
558 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400559 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100560 wl_resource_for_each(resource, focus_resource_list) {
561 wl_keyboard_send_leave(resource, serial,
562 keyboard->focus->resource);
563 }
564 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400565 }
566
Neil Roberts96d790e2013-09-19 17:32:00 +0100567 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
568 keyboard->focus != surface) {
569 struct wl_client *surface_client =
570 wl_resource_get_client(surface->resource);
571
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400572 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100573
574 move_resources_for_client(focus_resource_list,
575 &keyboard->resource_list,
576 surface_client);
577 send_enter_to_resource_list(focus_resource_list,
578 keyboard,
579 surface,
580 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400581 keyboard->focus_serial = serial;
582 }
583
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400584 keyboard->focus = surface;
585 wl_signal_emit(&keyboard->focus_signal, keyboard);
586}
587
588WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400589weston_keyboard_start_grab(struct weston_keyboard *keyboard,
590 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400591{
592 keyboard->grab = grab;
593 grab->keyboard = keyboard;
594
595 /* XXX focus? */
596}
597
598WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400599weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400600{
601 keyboard->grab = &keyboard->default_grab;
602}
603
604WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400605weston_pointer_start_grab(struct weston_pointer *pointer,
606 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400607{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400608 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400609 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400610 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400611}
612
613WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400614weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400615{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400616 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400617 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400618}
619
620WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400621weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400622{
623 touch->grab = grab;
624 grab->touch = touch;
625}
626
627WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -0400628weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629{
630 touch->grab = &touch->default_grab;
631}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400632
Rob Bradford806d8c02013-06-25 18:56:41 +0100633WL_EXPORT void
634weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400635{
Rob Bradford806d8c02013-06-25 18:56:41 +0100636 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400637 struct weston_output *output, *prev = NULL;
638 int x, y, old_x, old_y, valid = 0;
639
640 x = wl_fixed_to_int(*fx);
641 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +0100642 old_x = wl_fixed_to_int(pointer->x);
643 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400644
645 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +0100646 if (pointer->seat->output && pointer->seat->output != output)
647 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400648 if (pixman_region32_contains_point(&output->region,
649 x, y, NULL))
650 valid = 1;
651 if (pixman_region32_contains_point(&output->region,
652 old_x, old_y, NULL))
653 prev = output;
654 }
655
Rob Bradford66bd9f52013-06-25 18:56:42 +0100656 if (!prev)
657 prev = pointer->seat->output;
658
659 if (prev && !valid) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400660 if (x < prev->x)
661 *fx = wl_fixed_from_int(prev->x);
662 else if (x >= prev->x + prev->width)
663 *fx = wl_fixed_from_int(prev->x +
664 prev->width - 1);
665 if (y < prev->y)
666 *fy = wl_fixed_from_int(prev->y);
Alexander Larssonbcd18d92013-05-28 16:23:33 +0200667 else if (y >= prev->y + prev->height)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400668 *fy = wl_fixed_from_int(prev->y +
669 prev->height - 1);
670 }
671}
672
673/* Takes absolute values */
674static void
675move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
676{
677 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400678 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400679 struct weston_output *output;
680 int32_t ix, iy;
681
Rob Bradford806d8c02013-06-25 18:56:41 +0100682 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400683
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400684 pointer->x = x;
685 pointer->y = y;
686
687 ix = wl_fixed_to_int(x);
688 iy = wl_fixed_to_int(y);
689
690 wl_list_for_each(output, &ec->output_list, link)
691 if (output->zoom.active &&
692 pixman_region32_contains_point(&output->region,
693 ix, iy, NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500694 weston_output_update_zoom(output);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400695
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400696 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500697 weston_view_set_position(pointer->sprite,
698 ix - pointer->hotspot_x,
699 iy - pointer->hotspot_y);
700 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400701 }
702}
703
704WL_EXPORT void
705notify_motion(struct weston_seat *seat,
706 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
707{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400708 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400709 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400710
711 weston_compositor_wake(ec);
712
713 move_pointer(seat, pointer->x + dx, pointer->y + dy);
714
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400715 pointer->grab->interface->focus(pointer->grab);
716 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400717}
718
719WL_EXPORT void
720notify_motion_absolute(struct weston_seat *seat,
721 uint32_t time, wl_fixed_t x, wl_fixed_t y)
722{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400723 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400724 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400725
726 weston_compositor_wake(ec);
727
728 move_pointer(seat, x, y);
729
Kristian Høgsbergda751b82013-07-04 00:58:07 -0400730 pointer->grab->interface->focus(pointer->grab);
731 pointer->grab->interface->motion(pointer->grab, time);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400732}
733
734WL_EXPORT void
735weston_surface_activate(struct weston_surface *surface,
736 struct weston_seat *seat)
737{
738 struct weston_compositor *compositor = seat->compositor;
739
Kristian Høgsberge3148752013-05-06 23:19:49 -0400740 if (seat->keyboard) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400741 weston_keyboard_set_focus(seat->keyboard, surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400742 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400743 }
744
745 wl_signal_emit(&compositor->activate_signal, surface);
746}
747
748WL_EXPORT void
749notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
750 enum wl_pointer_button_state state)
751{
752 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400753 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400754 struct weston_surface *focus =
755 (struct weston_surface *) pointer->focus;
756 uint32_t serial = wl_display_next_serial(compositor->wl_display);
757
758 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
759 if (compositor->ping_handler && focus)
760 compositor->ping_handler(focus, serial);
761 weston_compositor_idle_inhibit(compositor);
762 if (pointer->button_count == 0) {
763 pointer->grab_button = button;
764 pointer->grab_time = time;
765 pointer->grab_x = pointer->x;
766 pointer->grab_y = pointer->y;
767 }
768 pointer->button_count++;
769 } else {
770 weston_compositor_idle_release(compositor);
771 pointer->button_count--;
772 }
773
774 weston_compositor_run_button_binding(compositor, seat, time, button,
775 state);
776
777 pointer->grab->interface->button(pointer->grab, time, button, state);
778
779 if (pointer->button_count == 1)
780 pointer->grab_serial =
781 wl_display_get_serial(compositor->wl_display);
782}
783
784WL_EXPORT void
785notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
786 wl_fixed_t value)
787{
788 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400789 struct weston_pointer *pointer = seat->pointer;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400790 struct weston_surface *focus =
791 (struct weston_surface *) pointer->focus;
792 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100793 struct wl_resource *resource;
794 struct wl_list *resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400795
796 if (compositor->ping_handler && focus)
797 compositor->ping_handler(focus, serial);
798
799 weston_compositor_wake(compositor);
800
801 if (!value)
802 return;
803
804 if (weston_compositor_run_axis_binding(compositor, seat,
805 time, axis, value))
806 return;
807
Neil Roberts96d790e2013-09-19 17:32:00 +0100808 resource_list = &pointer->focus_resource_list;
809 wl_resource_for_each(resource, resource_list)
810 wl_pointer_send_axis(resource, time, axis,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400811 value);
812}
813
Rob Bradford382ff462013-06-24 16:52:45 +0100814#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400815WL_EXPORT void
816notify_modifiers(struct weston_seat *seat, uint32_t serial)
817{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400818 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400819 struct weston_keyboard_grab *grab = keyboard->grab;
820 uint32_t mods_depressed, mods_latched, mods_locked, group;
821 uint32_t mods_lookup;
822 enum weston_led leds = 0;
823 int changed = 0;
824
825 /* Serialize and update our internal state, checking to see if it's
826 * different to the previous state. */
827 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
828 XKB_STATE_DEPRESSED);
829 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
830 XKB_STATE_LATCHED);
831 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
832 XKB_STATE_LOCKED);
833 group = xkb_state_serialize_group(seat->xkb_state.state,
834 XKB_STATE_EFFECTIVE);
835
Kristian Høgsberge3148752013-05-06 23:19:49 -0400836 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
837 mods_latched != seat->keyboard->modifiers.mods_latched ||
838 mods_locked != seat->keyboard->modifiers.mods_locked ||
839 group != seat->keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400840 changed = 1;
841
Kristian Høgsberge3148752013-05-06 23:19:49 -0400842 seat->keyboard->modifiers.mods_depressed = mods_depressed;
843 seat->keyboard->modifiers.mods_latched = mods_latched;
844 seat->keyboard->modifiers.mods_locked = mods_locked;
845 seat->keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400846
847 /* And update the modifier_state for bindings. */
848 mods_lookup = mods_depressed | mods_latched;
849 seat->modifier_state = 0;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000850 if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400851 seat->modifier_state |= MODIFIER_CTRL;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000852 if (mods_lookup & (1 << seat->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400853 seat->modifier_state |= MODIFIER_ALT;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000854 if (mods_lookup & (1 << seat->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400855 seat->modifier_state |= MODIFIER_SUPER;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000856 if (mods_lookup & (1 << seat->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400857 seat->modifier_state |= MODIFIER_SHIFT;
858
859 /* Finally, notify the compositor that LEDs have changed. */
860 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000861 seat->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400862 leds |= LED_NUM_LOCK;
863 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000864 seat->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400865 leds |= LED_CAPS_LOCK;
866 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +0000867 seat->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400868 leds |= LED_SCROLL_LOCK;
869 if (leds != seat->xkb_state.leds && seat->led_update)
870 seat->led_update(seat, leds);
871 seat->xkb_state.leds = leds;
872
873 if (changed) {
874 grab->interface->modifiers(grab,
875 serial,
876 keyboard->modifiers.mods_depressed,
877 keyboard->modifiers.mods_latched,
878 keyboard->modifiers.mods_locked,
879 keyboard->modifiers.group);
880 }
881}
882
883static void
884update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
885 enum wl_keyboard_key_state state)
886{
887 enum xkb_key_direction direction;
888
Matt Roper01a92732013-06-24 16:52:44 +0100889 /* Keyboard modifiers don't exist in raw keyboard mode */
890 if (!seat->compositor->use_xkbcommon)
891 return;
892
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400893 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
894 direction = XKB_KEY_DOWN;
895 else
896 direction = XKB_KEY_UP;
897
898 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
899 * broken keycode system, which starts at 8. */
900 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
901
902 notify_modifiers(seat, serial);
903}
Rui Matos65196bc2013-10-10 19:44:19 +0200904
905static void
906send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
907{
908 wl_keyboard_send_keymap(resource,
909 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
910 xkb_info->keymap_fd,
911 xkb_info->keymap_size);
912}
913
914static void
915send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
916{
917 wl_keyboard_send_modifiers(resource, serial,
918 keyboard->modifiers.mods_depressed,
919 keyboard->modifiers.mods_latched,
920 keyboard->modifiers.mods_locked,
921 keyboard->modifiers.group);
922}
923
924static struct weston_xkb_info *
925weston_xkb_info_create(struct xkb_keymap *keymap);
926static void
927weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
928
929static void
930update_keymap(struct weston_seat *seat)
931{
932 struct wl_resource *resource;
933 struct weston_xkb_info *xkb_info;
934 struct xkb_state *state;
935 xkb_mod_mask_t latched_mods;
936 xkb_mod_mask_t locked_mods;
937
938 xkb_info = weston_xkb_info_create(seat->pending_keymap);
939
940 xkb_keymap_unref(seat->pending_keymap);
941 seat->pending_keymap = NULL;
942
943 if (!xkb_info) {
944 weston_log("failed to create XKB info\n");
945 return;
946 }
947
948 state = xkb_state_new(xkb_info->keymap);
949 if (!state) {
950 weston_log("failed to initialise XKB state\n");
951 weston_xkb_info_destroy(xkb_info);
952 return;
953 }
954
955 latched_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LATCHED);
956 locked_mods = xkb_state_serialize_mods(seat->xkb_state.state, XKB_STATE_MODS_LOCKED);
957 xkb_state_update_mask(state,
958 0, /* depressed */
959 latched_mods,
960 locked_mods,
961 0, 0, 0);
962
963 weston_xkb_info_destroy(seat->xkb_info);
964 seat->xkb_info = xkb_info;
965
966 xkb_state_unref(seat->xkb_state.state);
967 seat->xkb_state.state = state;
968
969 wl_resource_for_each(resource, &seat->keyboard->resource_list)
970 send_keymap(resource, xkb_info);
971 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
972 send_keymap(resource, xkb_info);
973
974 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
975
976 if (!latched_mods && !locked_mods)
977 return;
978
979 wl_resource_for_each(resource, &seat->keyboard->resource_list)
980 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
981 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
982 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
983}
Rob Bradford382ff462013-06-24 16:52:45 +0100984#else
985WL_EXPORT void
986notify_modifiers(struct weston_seat *seat, uint32_t serial)
987{
988}
989
990static void
991update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
992 enum wl_keyboard_key_state state)
993{
994}
Rui Matos65196bc2013-10-10 19:44:19 +0200995
996static void
997update_keymap(struct weston_seat *seat)
998{
999}
Rob Bradford382ff462013-06-24 16:52:45 +01001000#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001001
1002WL_EXPORT void
1003notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1004 enum wl_keyboard_key_state state,
1005 enum weston_key_state_update update_state)
1006{
1007 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001008 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001010 struct weston_keyboard_grab *grab = keyboard->grab;
1011 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1012 uint32_t *k, *end;
1013
1014 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1015 if (compositor->ping_handler && focus)
1016 compositor->ping_handler(focus, serial);
1017
1018 weston_compositor_idle_inhibit(compositor);
1019 keyboard->grab_key = key;
1020 keyboard->grab_time = time;
1021 } else {
1022 weston_compositor_idle_release(compositor);
1023 }
1024
1025 end = keyboard->keys.data + keyboard->keys.size;
1026 for (k = keyboard->keys.data; k < end; k++) {
1027 if (*k == key) {
1028 /* Ignore server-generated repeats. */
1029 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1030 return;
1031 *k = *--end;
1032 }
1033 }
1034 keyboard->keys.size = (void *) end - keyboard->keys.data;
1035 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1036 k = wl_array_add(&keyboard->keys, sizeof *k);
1037 *k = key;
1038 }
1039
1040 if (grab == &keyboard->default_grab ||
1041 grab == &keyboard->input_method_grab) {
1042 weston_compositor_run_key_binding(compositor, seat, time, key,
1043 state);
1044 grab = keyboard->grab;
1045 }
1046
1047 grab->interface->key(grab, time, key, state);
1048
Rui Matos65196bc2013-10-10 19:44:19 +02001049 if (seat->pending_keymap &&
1050 keyboard->keys.size == 0)
1051 update_keymap(seat);
1052
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001053 if (update_state == STATE_UPDATE_AUTOMATIC) {
1054 update_modifier_state(seat,
1055 wl_display_get_serial(compositor->wl_display),
1056 key,
1057 state);
1058 }
1059}
1060
1061WL_EXPORT void
1062notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1063 wl_fixed_t x, wl_fixed_t y)
1064{
1065 struct weston_compositor *compositor = seat->compositor;
1066
1067 if (output) {
1068 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001069 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001070 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001071 * NULL) here, but somehow that breaks re-entry... */
1072 }
1073}
1074
1075static void
1076destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1077{
1078 struct weston_seat *ws;
1079
1080 ws = container_of(listener, struct weston_seat,
1081 saved_kbd_focus_listener);
1082
1083 ws->saved_kbd_focus = NULL;
1084}
1085
1086WL_EXPORT void
1087notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1088 enum weston_key_state_update update_state)
1089{
1090 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001091 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001092 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001093 uint32_t *k, serial;
1094
1095 serial = wl_display_next_serial(compositor->wl_display);
1096 wl_array_copy(&keyboard->keys, keys);
1097 wl_array_for_each(k, &keyboard->keys) {
1098 weston_compositor_idle_inhibit(compositor);
1099 if (update_state == STATE_UPDATE_AUTOMATIC)
1100 update_modifier_state(seat, serial, *k,
1101 WL_KEYBOARD_KEY_STATE_PRESSED);
1102 }
1103
1104 /* Run key bindings after we've updated the state. */
1105 wl_array_for_each(k, &keyboard->keys) {
1106 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1107 WL_KEYBOARD_KEY_STATE_PRESSED);
1108 }
1109
1110 surface = seat->saved_kbd_focus;
1111
1112 if (surface) {
1113 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1114 weston_keyboard_set_focus(keyboard, surface);
1115 seat->saved_kbd_focus = NULL;
1116 }
1117}
1118
1119WL_EXPORT void
1120notify_keyboard_focus_out(struct weston_seat *seat)
1121{
1122 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001123 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001124 uint32_t *k, serial;
1125
1126 serial = wl_display_next_serial(compositor->wl_display);
1127 wl_array_for_each(k, &keyboard->keys) {
1128 weston_compositor_idle_release(compositor);
1129 update_modifier_state(seat, serial, *k,
1130 WL_KEYBOARD_KEY_STATE_RELEASED);
1131 }
1132
1133 seat->modifier_state = 0;
1134
1135 if (keyboard->focus) {
1136 seat->saved_kbd_focus = keyboard->focus;
1137 seat->saved_kbd_focus_listener.notify =
1138 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001139 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001140 &seat->saved_kbd_focus_listener);
1141 }
1142
1143 weston_keyboard_set_focus(keyboard, NULL);
1144 /* FIXME: We really need keyboard grab cancel here to
1145 * let the grab shut down properly. As it is we leak
1146 * the grab data. */
1147 weston_keyboard_end_grab(keyboard);
1148}
1149
Michael Fua2bb7912013-07-23 15:51:06 +08001150WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001152{
Neil Roberts96d790e2013-09-19 17:32:00 +01001153 struct wl_list *focus_resource_list;
1154
1155 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001156
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 if (seat->touch->focus->surface == view->surface) {
1158 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001159 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001161
Neil Roberts96d790e2013-09-19 17:32:00 +01001162 if (!wl_list_empty(focus_resource_list)) {
1163 move_resources(&seat->touch->resource_list,
1164 focus_resource_list);
1165 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001166
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001168 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001170 move_resources_for_client(focus_resource_list,
1171 &seat->touch->resource_list,
1172 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001173 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001175}
1176
1177/**
1178 * notify_touch - emulates button touches and notifies surfaces accordingly.
1179 *
1180 * It assumes always the correct cycle sequence until it gets here: touch_down
1181 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1182 * for sending along such order.
1183 *
1184 */
1185WL_EXPORT void
1186notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1187 wl_fixed_t x, wl_fixed_t y, int touch_type)
1188{
1189 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001190 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001191 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001193 wl_fixed_t sx, sy;
1194
1195 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001196 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1197 touch->grab_x = x;
1198 touch->grab_y = y;
1199 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001200
1201 switch (touch_type) {
1202 case WL_TOUCH_DOWN:
1203 weston_compositor_idle_inhibit(ec);
1204
1205 seat->num_tp++;
1206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 /* the first finger down picks the view, and all further go
1208 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 * until all touch points are up again. */
1210 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1212 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001213 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 ev = touch->focus;
1215 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216 } else {
1217 /* Unexpected condition: We have non-initial touch but
1218 * there is no focused surface.
1219 */
1220 weston_log("touch event received with %d points down"
1221 "but no surface focused\n", seat->num_tp);
1222 return;
1223 }
1224
1225 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001226 if (seat->num_tp == 1) {
1227 touch->grab_serial =
1228 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001229 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001230 touch->grab_time = time;
1231 touch->grab_x = x;
1232 touch->grab_y = y;
1233 }
1234
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001235 break;
1236 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 ev = touch->focus;
1238 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001239 break;
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001242 grab->interface->motion(grab, time, touch_id, sx, sy);
1243 break;
1244 case WL_TOUCH_UP:
1245 weston_compositor_idle_release(ec);
1246 seat->num_tp--;
1247
1248 grab->interface->up(grab, time, touch_id);
1249 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001250 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001251 break;
1252 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001253
1254 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001255}
1256
1257static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001258pointer_cursor_surface_configure(struct weston_surface *es,
1259 int32_t dx, int32_t dy, int32_t width, int32_t height)
1260{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001261 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001262 int x, y;
1263
1264 if (width == 0)
1265 return;
1266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001268
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001269 pointer->hotspot_x -= dx;
1270 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001271
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001272 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1273 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001276
1277 empty_region(&es->pending.input);
1278
1279 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 wl_list_insert(&es->compositor->cursor_layer.view_list,
1281 &pointer->sprite->layer_link);
1282 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001283 }
1284}
1285
1286static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001287pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1288 uint32_t serial, struct wl_resource *surface_resource,
1289 int32_t x, int32_t y)
1290{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001291 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001292 struct weston_surface *surface = NULL;
1293
1294 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001295 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001296
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001297 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001298 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001300 black_surface used in shell.c for fullscreen don't have
1301 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001303 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001305 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001306 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001307 return;
1308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001310 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001311 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001312 WL_DISPLAY_ERROR_INVALID_OBJECT,
1313 "surface->configure already "
1314 "set");
1315 return;
1316 }
1317 }
1318
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001319 if (pointer->sprite)
1320 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001321
1322 if (!surface)
1323 return;
1324
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001325 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001326 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001327
1328 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001329 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001331 pointer->hotspot_x = x;
1332 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001333
1334 if (surface->buffer_ref.buffer)
1335 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1336 weston_surface_buffer_height(surface));
1337}
1338
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001339static void
1340pointer_release(struct wl_client *client, struct wl_resource *resource)
1341{
1342 wl_resource_destroy(resource);
1343}
1344
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001345static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001346 pointer_set_cursor,
1347 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001348};
1349
1350static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001351seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1352 uint32_t id)
1353{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001354 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001355 struct wl_resource *cr;
1356
Kristian Høgsberge3148752013-05-06 23:19:49 -04001357 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001358 return;
1359
Jason Ekstranda85118c2013-06-27 20:17:02 -05001360 cr = wl_resource_create(client, &wl_pointer_interface,
1361 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001362 if (cr == NULL) {
1363 wl_client_post_no_memory(client);
1364 return;
1365 }
1366
Neil Roberts96d790e2013-09-19 17:32:00 +01001367 /* May be moved to focused list later by either
1368 * weston_pointer_set_focus or directly if this client is already
1369 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001370 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001371 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1372 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001373
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1375 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001376 wl_fixed_t sx, sy;
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 weston_view_from_global_fixed(seat->pointer->focus,
1379 seat->pointer->x,
1380 seat->pointer->y,
1381 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001382
1383 wl_list_remove(wl_resource_get_link(cr));
1384 wl_list_insert(&seat->pointer->focus_resource_list,
1385 wl_resource_get_link(cr));
1386 wl_pointer_send_enter(cr,
1387 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001389 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001390 }
1391}
1392
1393static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001394keyboard_release(struct wl_client *client, struct wl_resource *resource)
1395{
1396 wl_resource_destroy(resource);
1397}
1398
1399static const struct wl_keyboard_interface keyboard_interface = {
1400 keyboard_release
1401};
1402
Neil Roberts96d790e2013-09-19 17:32:00 +01001403static int
1404should_send_modifiers_to_client(struct weston_seat *seat,
1405 struct wl_client *client)
1406{
1407 if (seat->keyboard &&
1408 seat->keyboard->focus &&
1409 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1410 return 1;
1411
1412 if (seat->pointer &&
1413 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001415 return 1;
1416
1417 return 0;
1418}
1419
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001420static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001421seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1422 uint32_t id)
1423{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001424 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001425 struct wl_resource *cr;
1426
Kristian Høgsberge3148752013-05-06 23:19:49 -04001427 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001428 return;
1429
Jason Ekstranda85118c2013-06-27 20:17:02 -05001430 cr = wl_resource_create(client, &wl_keyboard_interface,
1431 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001432 if (cr == NULL) {
1433 wl_client_post_no_memory(client);
1434 return;
1435 }
1436
Neil Roberts96d790e2013-09-19 17:32:00 +01001437 /* May be moved to focused list later by either
1438 * weston_keyboard_set_focus or directly if this client is already
1439 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001440 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001441 wl_resource_set_implementation(cr, &keyboard_interface,
1442 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001443
Matt Roper01a92732013-06-24 16:52:44 +01001444 if (seat->compositor->use_xkbcommon) {
1445 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001446 seat->xkb_info->keymap_fd,
1447 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001448 } else {
1449 int null_fd = open("/dev/null", O_RDONLY);
1450 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1451 null_fd,
1452 0);
1453 close(null_fd);
1454 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001455
Neil Roberts96d790e2013-09-19 17:32:00 +01001456 if (should_send_modifiers_to_client(seat, client)) {
1457 send_modifiers_to_resource(seat->keyboard,
1458 cr,
1459 seat->keyboard->focus_serial);
1460 }
1461
Kristian Høgsberge3148752013-05-06 23:19:49 -04001462 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001463 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001464 struct weston_surface *surface =
1465 (struct weston_surface *) seat->keyboard->focus;
1466
1467 wl_list_remove(wl_resource_get_link(cr));
1468 wl_list_insert(&seat->keyboard->focus_resource_list,
1469 wl_resource_get_link(cr));
1470 wl_keyboard_send_enter(cr,
1471 seat->keyboard->focus_serial,
1472 surface->resource,
1473 &seat->keyboard->keys);
1474
1475 /* If this is the first keyboard resource for this
1476 * client... */
1477 if (seat->keyboard->focus_resource_list.prev ==
1478 wl_resource_get_link(cr))
1479 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001480 }
1481}
1482
1483static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001484touch_release(struct wl_client *client, struct wl_resource *resource)
1485{
1486 wl_resource_destroy(resource);
1487}
1488
1489static const struct wl_touch_interface touch_interface = {
1490 touch_release
1491};
1492
1493static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001494seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1495 uint32_t id)
1496{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001497 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001498 struct wl_resource *cr;
1499
Kristian Høgsberge3148752013-05-06 23:19:49 -04001500 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001501 return;
1502
Jason Ekstranda85118c2013-06-27 20:17:02 -05001503 cr = wl_resource_create(client, &wl_touch_interface,
1504 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001505 if (cr == NULL) {
1506 wl_client_post_no_memory(client);
1507 return;
1508 }
1509
Neil Roberts96d790e2013-09-19 17:32:00 +01001510 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001512 wl_list_insert(&seat->touch->resource_list,
1513 wl_resource_get_link(cr));
1514 } else {
1515 wl_list_insert(&seat->touch->focus_resource_list,
1516 wl_resource_get_link(cr));
1517 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001518 wl_resource_set_implementation(cr, &touch_interface,
1519 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001520}
1521
1522static const struct wl_seat_interface seat_interface = {
1523 seat_get_pointer,
1524 seat_get_keyboard,
1525 seat_get_touch,
1526};
1527
1528static void
1529bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1530{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001531 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001532 struct wl_resource *resource;
1533 enum wl_seat_capability caps = 0;
1534
Jason Ekstranda85118c2013-06-27 20:17:02 -05001535 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001536 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001537 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001538 wl_resource_set_implementation(resource, &seat_interface, data,
1539 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001540
1541 if (seat->pointer)
1542 caps |= WL_SEAT_CAPABILITY_POINTER;
1543 if (seat->keyboard)
1544 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1545 if (seat->touch)
1546 caps |= WL_SEAT_CAPABILITY_TOUCH;
1547
1548 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001549 if (version >= 2)
1550 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001551}
1552
Rob Bradford382ff462013-06-24 16:52:45 +01001553#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001554int
1555weston_compositor_xkb_init(struct weston_compositor *ec,
1556 struct xkb_rule_names *names)
1557{
Rob Bradford382ff462013-06-24 16:52:45 +01001558 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001559
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001560 if (ec->xkb_context == NULL) {
1561 ec->xkb_context = xkb_context_new(0);
1562 if (ec->xkb_context == NULL) {
1563 weston_log("failed to create XKB context\n");
1564 return -1;
1565 }
1566 }
1567
1568 if (names)
1569 ec->xkb_names = *names;
1570 if (!ec->xkb_names.rules)
1571 ec->xkb_names.rules = strdup("evdev");
1572 if (!ec->xkb_names.model)
1573 ec->xkb_names.model = strdup("pc105");
1574 if (!ec->xkb_names.layout)
1575 ec->xkb_names.layout = strdup("us");
1576
1577 return 0;
1578}
1579
Stefan Schmidtfda26522013-09-17 10:54:09 +01001580static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001581weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001582{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001583 if (--xkb_info->ref_count > 0)
1584 return;
1585
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001586 if (xkb_info->keymap)
1587 xkb_map_unref(xkb_info->keymap);
1588
1589 if (xkb_info->keymap_area)
1590 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1591 if (xkb_info->keymap_fd >= 0)
1592 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001593 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001594}
1595
1596void
1597weston_compositor_xkb_destroy(struct weston_compositor *ec)
1598{
Matt Roper01a92732013-06-24 16:52:44 +01001599 /*
1600 * If we're operating in raw keyboard mode, we never initialized
1601 * libxkbcommon so there's no cleanup to do either.
1602 */
1603 if (!ec->use_xkbcommon)
1604 return;
1605
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001606 free((char *) ec->xkb_names.rules);
1607 free((char *) ec->xkb_names.model);
1608 free((char *) ec->xkb_names.layout);
1609 free((char *) ec->xkb_names.variant);
1610 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001611
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001612 if (ec->xkb_info)
1613 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614 xkb_context_unref(ec->xkb_context);
1615}
1616
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001617static struct weston_xkb_info *
1618weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001619{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001620 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1621 if (xkb_info == NULL)
1622 return NULL;
1623
1624 xkb_info->keymap = xkb_map_ref(keymap);
1625 xkb_info->ref_count = 1;
1626
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627 char *keymap_str;
1628
1629 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1630 XKB_MOD_NAME_SHIFT);
1631 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1632 XKB_MOD_NAME_CAPS);
1633 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1634 XKB_MOD_NAME_CTRL);
1635 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1636 XKB_MOD_NAME_ALT);
1637 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1638 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1639 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1640 XKB_MOD_NAME_LOGO);
1641 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1642
1643 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1644 XKB_LED_NAME_NUM);
1645 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1646 XKB_LED_NAME_CAPS);
1647 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1648 XKB_LED_NAME_SCROLL);
1649
1650 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1651 if (keymap_str == NULL) {
1652 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001653 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001654 }
1655 xkb_info->keymap_size = strlen(keymap_str) + 1;
1656
1657 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1658 if (xkb_info->keymap_fd < 0) {
1659 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1660 (unsigned long) xkb_info->keymap_size);
1661 goto err_keymap_str;
1662 }
1663
1664 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1665 PROT_READ | PROT_WRITE,
1666 MAP_SHARED, xkb_info->keymap_fd, 0);
1667 if (xkb_info->keymap_area == MAP_FAILED) {
1668 weston_log("failed to mmap() %lu bytes\n",
1669 (unsigned long) xkb_info->keymap_size);
1670 goto err_dev_zero;
1671 }
1672 strcpy(xkb_info->keymap_area, keymap_str);
1673 free(keymap_str);
1674
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001675 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001676
1677err_dev_zero:
1678 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001679err_keymap_str:
1680 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001681err_keymap:
1682 xkb_map_unref(xkb_info->keymap);
1683 free(xkb_info);
1684 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001685}
1686
1687static int
1688weston_compositor_build_global_keymap(struct weston_compositor *ec)
1689{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001690 struct xkb_keymap *keymap;
1691
1692 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001693 return 0;
1694
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001695 keymap = xkb_map_new_from_names(ec->xkb_context,
1696 &ec->xkb_names,
1697 0);
1698 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699 weston_log("failed to compile global XKB keymap\n");
1700 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1701 "options %s\n",
1702 ec->xkb_names.rules, ec->xkb_names.model,
1703 ec->xkb_names.layout, ec->xkb_names.variant,
1704 ec->xkb_names.options);
1705 return -1;
1706 }
1707
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001708 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001709 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710 return -1;
1711
1712 return 0;
1713}
Rob Bradford382ff462013-06-24 16:52:45 +01001714#else
1715int
1716weston_compositor_xkb_init(struct weston_compositor *ec,
1717 struct xkb_rule_names *names)
1718{
1719 return 0;
1720}
1721
1722void
1723weston_compositor_xkb_destroy(struct weston_compositor *ec)
1724{
1725}
1726#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001727
Rui Matos65196bc2013-10-10 19:44:19 +02001728WL_EXPORT void
1729weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1730{
1731 if (!seat->keyboard || !keymap)
1732 return;
1733
1734#ifdef ENABLE_XKBCOMMON
1735 if (!seat->compositor->use_xkbcommon)
1736 return;
1737
1738 xkb_keymap_unref(seat->pending_keymap);
1739 seat->pending_keymap = xkb_keymap_ref(keymap);
1740
1741 if (seat->keyboard->keys.size == 0)
1742 update_keymap(seat);
1743#endif
1744}
1745
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001746WL_EXPORT int
1747weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1748{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001749 struct weston_keyboard *keyboard;
1750
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001751 if (seat->keyboard) {
1752 seat->keyboard_device_count += 1;
1753 if (seat->keyboard_device_count == 1)
1754 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001755 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001756 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001757
Rob Bradford382ff462013-06-24 16:52:45 +01001758#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001759 if (seat->compositor->use_xkbcommon) {
1760 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001761 seat->xkb_info = weston_xkb_info_create(keymap);
1762 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001763 return -1;
1764 } else {
1765 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1766 return -1;
1767 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001768 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001769 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001770
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001771 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001772 if (seat->xkb_state.state == NULL) {
1773 weston_log("failed to initialise XKB state\n");
1774 return -1;
1775 }
1776
1777 seat->xkb_state.leds = 0;
1778 }
Rob Bradford382ff462013-06-24 16:52:45 +01001779#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001780
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001781 keyboard = weston_keyboard_create();
1782 if (keyboard == NULL) {
1783 weston_log("failed to allocate weston keyboard struct\n");
1784 return -1;
1785 }
1786
1787 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001788 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001789 keyboard->seat = seat;
1790
1791 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793 return 0;
1794}
1795
1796WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001797weston_seat_release_keyboard(struct weston_seat *seat)
1798{
1799 seat->keyboard_device_count--;
1800 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001801 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001802 seat_send_updated_caps(seat);
1803 }
1804}
1805
1806WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001807weston_seat_init_pointer(struct weston_seat *seat)
1808{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001809 struct weston_pointer *pointer;
1810
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001811 if (seat->pointer) {
1812 seat->pointer_device_count += 1;
1813 if (seat->pointer_device_count == 1)
1814 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001815 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001816 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001817
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001818 pointer = weston_pointer_create();
1819 if (pointer == NULL)
1820 return;
1821
1822 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001823 seat->pointer_device_count = 1;
1824 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001825
1826 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827}
1828
1829WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001830weston_seat_release_pointer(struct weston_seat *seat)
1831{
1832 struct weston_pointer *pointer = seat->pointer;
1833
1834 seat->pointer_device_count--;
1835 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001836 weston_pointer_set_focus(pointer, NULL,
1837 wl_fixed_from_int(0),
1838 wl_fixed_from_int(0));
1839
Jonas Ådahla4932742013-10-17 23:04:07 +02001840 if (pointer->sprite)
1841 pointer_unmap_sprite(pointer);
1842
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001843 seat_send_updated_caps(seat);
1844 }
1845}
1846
1847WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001848weston_seat_init_touch(struct weston_seat *seat)
1849{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001850 struct weston_touch *touch;
1851
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001852 if (seat->touch) {
1853 seat->touch_device_count += 1;
1854 if (seat->touch_device_count == 1)
1855 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001856 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001857 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001858
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001859 touch = weston_touch_create();
1860 if (touch == NULL)
1861 return;
1862
1863 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001864 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001865 touch->seat = seat;
1866
1867 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001868}
1869
1870WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001871weston_seat_release_touch(struct weston_seat *seat)
1872{
1873 seat->touch_device_count--;
1874 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001875 weston_touch_set_focus(seat, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001876 seat_send_updated_caps(seat);
1877 }
1878}
1879
1880WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001881weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1882 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001883{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001884 memset(seat, 0, sizeof *seat);
1885
Kristian Høgsberge3148752013-05-06 23:19:49 -04001886 seat->selection_data_source = NULL;
1887 wl_list_init(&seat->base_resource_list);
1888 wl_signal_init(&seat->selection_signal);
1889 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001890 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001891
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001892 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001893 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001894
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001895 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001896 seat->modifier_state = 0;
1897 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001898 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001899
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001900 wl_list_insert(ec->seat_list.prev, &seat->link);
1901
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001902 clipboard_create(seat);
1903
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001904 wl_signal_emit(&ec->seat_created_signal, seat);
1905}
1906
1907WL_EXPORT void
1908weston_seat_release(struct weston_seat *seat)
1909{
1910 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001911
Rob Bradford382ff462013-06-24 16:52:45 +01001912#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001913 if (seat->compositor->use_xkbcommon) {
1914 if (seat->xkb_state.state != NULL)
1915 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001916 if (seat->xkb_info)
1917 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001918 if (seat->pending_keymap)
1919 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001920 }
Rob Bradford382ff462013-06-24 16:52:45 +01001921#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001922
Kristian Høgsberge3148752013-05-06 23:19:49 -04001923 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001924 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001925 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001926 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001927 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001928 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001929
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001930 free (seat->seat_name);
1931
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001932 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001933
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001934 wl_signal_emit(&seat->destroy_signal, seat);
1935}