blob: ffb7b35ea44864d73b64e39cab8330a5ab0f811a [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{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001065 if (output) {
1066 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001067 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001069 * NULL) here, but somehow that breaks re-entry... */
1070 }
1071}
1072
1073static void
1074destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1075{
1076 struct weston_seat *ws;
1077
1078 ws = container_of(listener, struct weston_seat,
1079 saved_kbd_focus_listener);
1080
1081 ws->saved_kbd_focus = NULL;
1082}
1083
1084WL_EXPORT void
1085notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1086 enum weston_key_state_update update_state)
1087{
1088 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001089 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001090 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001091 uint32_t *k, serial;
1092
1093 serial = wl_display_next_serial(compositor->wl_display);
1094 wl_array_copy(&keyboard->keys, keys);
1095 wl_array_for_each(k, &keyboard->keys) {
1096 weston_compositor_idle_inhibit(compositor);
1097 if (update_state == STATE_UPDATE_AUTOMATIC)
1098 update_modifier_state(seat, serial, *k,
1099 WL_KEYBOARD_KEY_STATE_PRESSED);
1100 }
1101
1102 /* Run key bindings after we've updated the state. */
1103 wl_array_for_each(k, &keyboard->keys) {
1104 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1105 WL_KEYBOARD_KEY_STATE_PRESSED);
1106 }
1107
1108 surface = seat->saved_kbd_focus;
1109
1110 if (surface) {
1111 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1112 weston_keyboard_set_focus(keyboard, surface);
1113 seat->saved_kbd_focus = NULL;
1114 }
1115}
1116
1117WL_EXPORT void
1118notify_keyboard_focus_out(struct weston_seat *seat)
1119{
1120 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001121 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001122 uint32_t *k, serial;
1123
1124 serial = wl_display_next_serial(compositor->wl_display);
1125 wl_array_for_each(k, &keyboard->keys) {
1126 weston_compositor_idle_release(compositor);
1127 update_modifier_state(seat, serial, *k,
1128 WL_KEYBOARD_KEY_STATE_RELEASED);
1129 }
1130
1131 seat->modifier_state = 0;
1132
1133 if (keyboard->focus) {
1134 seat->saved_kbd_focus = keyboard->focus;
1135 seat->saved_kbd_focus_listener.notify =
1136 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001137 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001138 &seat->saved_kbd_focus_listener);
1139 }
1140
1141 weston_keyboard_set_focus(keyboard, NULL);
1142 /* FIXME: We really need keyboard grab cancel here to
1143 * let the grab shut down properly. As it is we leak
1144 * the grab data. */
1145 weston_keyboard_end_grab(keyboard);
1146}
1147
Michael Fua2bb7912013-07-23 15:51:06 +08001148WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001150{
Neil Roberts96d790e2013-09-19 17:32:00 +01001151 struct wl_list *focus_resource_list;
1152
1153 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001154
Kristian Høgsbergc3244d72013-10-24 14:21:53 -07001155 if (view && seat->touch->focus &&
1156 seat->touch->focus->surface == view->surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001158 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001160
Neil Roberts96d790e2013-09-19 17:32:00 +01001161 if (!wl_list_empty(focus_resource_list)) {
1162 move_resources(&seat->touch->resource_list,
1163 focus_resource_list);
1164 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001167 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001169 move_resources_for_client(focus_resource_list,
1170 &seat->touch->resource_list,
1171 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001172 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001174}
1175
1176/**
1177 * notify_touch - emulates button touches and notifies surfaces accordingly.
1178 *
1179 * It assumes always the correct cycle sequence until it gets here: touch_down
1180 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1181 * for sending along such order.
1182 *
1183 */
1184WL_EXPORT void
1185notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1186 wl_fixed_t x, wl_fixed_t y, int touch_type)
1187{
1188 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001189 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001190 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001192 wl_fixed_t sx, sy;
1193
1194 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001195 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1196 touch->grab_x = x;
1197 touch->grab_y = y;
1198 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001199
1200 switch (touch_type) {
1201 case WL_TOUCH_DOWN:
1202 weston_compositor_idle_inhibit(ec);
1203
1204 seat->num_tp++;
1205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 /* the first finger down picks the view, and all further go
1207 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001208 * until all touch points are up again. */
1209 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1211 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001212 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 ev = touch->focus;
1214 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001215 } else {
1216 /* Unexpected condition: We have non-initial touch but
1217 * there is no focused surface.
1218 */
1219 weston_log("touch event received with %d points down"
1220 "but no surface focused\n", seat->num_tp);
1221 return;
1222 }
1223
1224 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001225 if (seat->num_tp == 1) {
1226 touch->grab_serial =
1227 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001228 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001229 touch->grab_time = time;
1230 touch->grab_x = x;
1231 touch->grab_y = y;
1232 }
1233
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001234 break;
1235 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 ev = touch->focus;
1237 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001238 break;
1239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001241 grab->interface->motion(grab, time, touch_id, sx, sy);
1242 break;
1243 case WL_TOUCH_UP:
1244 weston_compositor_idle_release(ec);
1245 seat->num_tp--;
1246
1247 grab->interface->up(grab, time, touch_id);
1248 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001249 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001250 break;
1251 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001252
1253 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001254}
1255
1256static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001257pointer_cursor_surface_configure(struct weston_surface *es,
1258 int32_t dx, int32_t dy, int32_t width, int32_t height)
1259{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001260 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001261 int x, y;
1262
1263 if (width == 0)
1264 return;
1265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001267
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001268 pointer->hotspot_x -= dx;
1269 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001270
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001271 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1272 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001275
1276 empty_region(&es->pending.input);
1277
1278 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 wl_list_insert(&es->compositor->cursor_layer.view_list,
1280 &pointer->sprite->layer_link);
1281 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001282 }
1283}
1284
1285static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001286pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1287 uint32_t serial, struct wl_resource *surface_resource,
1288 int32_t x, int32_t y)
1289{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001290 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001291 struct weston_surface *surface = NULL;
1292
1293 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001294 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001295
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001296 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001297 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001299 black_surface used in shell.c for fullscreen don't have
1300 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001302 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001304 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001305 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001306 return;
1307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001309 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001310 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001311 WL_DISPLAY_ERROR_INVALID_OBJECT,
1312 "surface->configure already "
1313 "set");
1314 return;
1315 }
1316 }
1317
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001318 if (pointer->sprite)
1319 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001320
1321 if (!surface)
1322 return;
1323
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001324 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001325 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001326
1327 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001328 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001330 pointer->hotspot_x = x;
1331 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001332
1333 if (surface->buffer_ref.buffer)
1334 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1335 weston_surface_buffer_height(surface));
1336}
1337
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001338static void
1339pointer_release(struct wl_client *client, struct wl_resource *resource)
1340{
1341 wl_resource_destroy(resource);
1342}
1343
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001344static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001345 pointer_set_cursor,
1346 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001347};
1348
1349static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001350seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1351 uint32_t id)
1352{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001353 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001354 struct wl_resource *cr;
1355
Kristian Høgsberge3148752013-05-06 23:19:49 -04001356 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001357 return;
1358
Jason Ekstranda85118c2013-06-27 20:17:02 -05001359 cr = wl_resource_create(client, &wl_pointer_interface,
1360 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001361 if (cr == NULL) {
1362 wl_client_post_no_memory(client);
1363 return;
1364 }
1365
Neil Roberts96d790e2013-09-19 17:32:00 +01001366 /* May be moved to focused list later by either
1367 * weston_pointer_set_focus or directly if this client is already
1368 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001369 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001370 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1371 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001372
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1374 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001375 wl_fixed_t sx, sy;
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_from_global_fixed(seat->pointer->focus,
1378 seat->pointer->x,
1379 seat->pointer->y,
1380 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001381
1382 wl_list_remove(wl_resource_get_link(cr));
1383 wl_list_insert(&seat->pointer->focus_resource_list,
1384 wl_resource_get_link(cr));
1385 wl_pointer_send_enter(cr,
1386 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001388 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001389 }
1390}
1391
1392static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001393keyboard_release(struct wl_client *client, struct wl_resource *resource)
1394{
1395 wl_resource_destroy(resource);
1396}
1397
1398static const struct wl_keyboard_interface keyboard_interface = {
1399 keyboard_release
1400};
1401
Neil Roberts96d790e2013-09-19 17:32:00 +01001402static int
1403should_send_modifiers_to_client(struct weston_seat *seat,
1404 struct wl_client *client)
1405{
1406 if (seat->keyboard &&
1407 seat->keyboard->focus &&
1408 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1409 return 1;
1410
1411 if (seat->pointer &&
1412 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001414 return 1;
1415
1416 return 0;
1417}
1418
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001419static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001420seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1421 uint32_t id)
1422{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001423 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001424 struct wl_resource *cr;
1425
Kristian Høgsberge3148752013-05-06 23:19:49 -04001426 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001427 return;
1428
Jason Ekstranda85118c2013-06-27 20:17:02 -05001429 cr = wl_resource_create(client, &wl_keyboard_interface,
1430 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001431 if (cr == NULL) {
1432 wl_client_post_no_memory(client);
1433 return;
1434 }
1435
Neil Roberts96d790e2013-09-19 17:32:00 +01001436 /* May be moved to focused list later by either
1437 * weston_keyboard_set_focus or directly if this client is already
1438 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001439 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001440 wl_resource_set_implementation(cr, &keyboard_interface,
1441 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001442
Matt Roper01a92732013-06-24 16:52:44 +01001443 if (seat->compositor->use_xkbcommon) {
1444 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001445 seat->xkb_info->keymap_fd,
1446 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001447 } else {
1448 int null_fd = open("/dev/null", O_RDONLY);
1449 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1450 null_fd,
1451 0);
1452 close(null_fd);
1453 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001454
Neil Roberts96d790e2013-09-19 17:32:00 +01001455 if (should_send_modifiers_to_client(seat, client)) {
1456 send_modifiers_to_resource(seat->keyboard,
1457 cr,
1458 seat->keyboard->focus_serial);
1459 }
1460
Kristian Høgsberge3148752013-05-06 23:19:49 -04001461 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001462 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001463 struct weston_surface *surface =
1464 (struct weston_surface *) seat->keyboard->focus;
1465
1466 wl_list_remove(wl_resource_get_link(cr));
1467 wl_list_insert(&seat->keyboard->focus_resource_list,
1468 wl_resource_get_link(cr));
1469 wl_keyboard_send_enter(cr,
1470 seat->keyboard->focus_serial,
1471 surface->resource,
1472 &seat->keyboard->keys);
1473
1474 /* If this is the first keyboard resource for this
1475 * client... */
1476 if (seat->keyboard->focus_resource_list.prev ==
1477 wl_resource_get_link(cr))
1478 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001479 }
1480}
1481
1482static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001483touch_release(struct wl_client *client, struct wl_resource *resource)
1484{
1485 wl_resource_destroy(resource);
1486}
1487
1488static const struct wl_touch_interface touch_interface = {
1489 touch_release
1490};
1491
1492static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001493seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1494 uint32_t id)
1495{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001496 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001497 struct wl_resource *cr;
1498
Kristian Høgsberge3148752013-05-06 23:19:49 -04001499 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500 return;
1501
Jason Ekstranda85118c2013-06-27 20:17:02 -05001502 cr = wl_resource_create(client, &wl_touch_interface,
1503 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001504 if (cr == NULL) {
1505 wl_client_post_no_memory(client);
1506 return;
1507 }
1508
Neil Roberts96d790e2013-09-19 17:32:00 +01001509 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001511 wl_list_insert(&seat->touch->resource_list,
1512 wl_resource_get_link(cr));
1513 } else {
1514 wl_list_insert(&seat->touch->focus_resource_list,
1515 wl_resource_get_link(cr));
1516 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001517 wl_resource_set_implementation(cr, &touch_interface,
1518 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519}
1520
1521static const struct wl_seat_interface seat_interface = {
1522 seat_get_pointer,
1523 seat_get_keyboard,
1524 seat_get_touch,
1525};
1526
1527static void
1528bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1529{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001530 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001531 struct wl_resource *resource;
1532 enum wl_seat_capability caps = 0;
1533
Jason Ekstranda85118c2013-06-27 20:17:02 -05001534 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001535 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001536 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001537 wl_resource_set_implementation(resource, &seat_interface, data,
1538 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001539
1540 if (seat->pointer)
1541 caps |= WL_SEAT_CAPABILITY_POINTER;
1542 if (seat->keyboard)
1543 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1544 if (seat->touch)
1545 caps |= WL_SEAT_CAPABILITY_TOUCH;
1546
1547 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001548 if (version >= 2)
1549 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001550}
1551
Rob Bradford382ff462013-06-24 16:52:45 +01001552#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001553int
1554weston_compositor_xkb_init(struct weston_compositor *ec,
1555 struct xkb_rule_names *names)
1556{
Rob Bradford382ff462013-06-24 16:52:45 +01001557 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001558
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001559 if (ec->xkb_context == NULL) {
1560 ec->xkb_context = xkb_context_new(0);
1561 if (ec->xkb_context == NULL) {
1562 weston_log("failed to create XKB context\n");
1563 return -1;
1564 }
1565 }
1566
1567 if (names)
1568 ec->xkb_names = *names;
1569 if (!ec->xkb_names.rules)
1570 ec->xkb_names.rules = strdup("evdev");
1571 if (!ec->xkb_names.model)
1572 ec->xkb_names.model = strdup("pc105");
1573 if (!ec->xkb_names.layout)
1574 ec->xkb_names.layout = strdup("us");
1575
1576 return 0;
1577}
1578
Stefan Schmidtfda26522013-09-17 10:54:09 +01001579static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001580weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001582 if (--xkb_info->ref_count > 0)
1583 return;
1584
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001585 if (xkb_info->keymap)
1586 xkb_map_unref(xkb_info->keymap);
1587
1588 if (xkb_info->keymap_area)
1589 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1590 if (xkb_info->keymap_fd >= 0)
1591 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001592 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001593}
1594
1595void
1596weston_compositor_xkb_destroy(struct weston_compositor *ec)
1597{
Matt Roper01a92732013-06-24 16:52:44 +01001598 /*
1599 * If we're operating in raw keyboard mode, we never initialized
1600 * libxkbcommon so there's no cleanup to do either.
1601 */
1602 if (!ec->use_xkbcommon)
1603 return;
1604
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605 free((char *) ec->xkb_names.rules);
1606 free((char *) ec->xkb_names.model);
1607 free((char *) ec->xkb_names.layout);
1608 free((char *) ec->xkb_names.variant);
1609 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001610
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001611 if (ec->xkb_info)
1612 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001613 xkb_context_unref(ec->xkb_context);
1614}
1615
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001616static struct weston_xkb_info *
1617weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001618{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001619 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1620 if (xkb_info == NULL)
1621 return NULL;
1622
1623 xkb_info->keymap = xkb_map_ref(keymap);
1624 xkb_info->ref_count = 1;
1625
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001626 char *keymap_str;
1627
1628 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1629 XKB_MOD_NAME_SHIFT);
1630 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1631 XKB_MOD_NAME_CAPS);
1632 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1633 XKB_MOD_NAME_CTRL);
1634 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1635 XKB_MOD_NAME_ALT);
1636 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1637 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1638 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1639 XKB_MOD_NAME_LOGO);
1640 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1641
1642 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1643 XKB_LED_NAME_NUM);
1644 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1645 XKB_LED_NAME_CAPS);
1646 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1647 XKB_LED_NAME_SCROLL);
1648
1649 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1650 if (keymap_str == NULL) {
1651 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001652 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001653 }
1654 xkb_info->keymap_size = strlen(keymap_str) + 1;
1655
1656 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1657 if (xkb_info->keymap_fd < 0) {
1658 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1659 (unsigned long) xkb_info->keymap_size);
1660 goto err_keymap_str;
1661 }
1662
1663 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1664 PROT_READ | PROT_WRITE,
1665 MAP_SHARED, xkb_info->keymap_fd, 0);
1666 if (xkb_info->keymap_area == MAP_FAILED) {
1667 weston_log("failed to mmap() %lu bytes\n",
1668 (unsigned long) xkb_info->keymap_size);
1669 goto err_dev_zero;
1670 }
1671 strcpy(xkb_info->keymap_area, keymap_str);
1672 free(keymap_str);
1673
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001674 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675
1676err_dev_zero:
1677 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001678err_keymap_str:
1679 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001680err_keymap:
1681 xkb_map_unref(xkb_info->keymap);
1682 free(xkb_info);
1683 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001684}
1685
1686static int
1687weston_compositor_build_global_keymap(struct weston_compositor *ec)
1688{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001689 struct xkb_keymap *keymap;
1690
1691 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001692 return 0;
1693
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001694 keymap = xkb_map_new_from_names(ec->xkb_context,
1695 &ec->xkb_names,
1696 0);
1697 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001698 weston_log("failed to compile global XKB keymap\n");
1699 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1700 "options %s\n",
1701 ec->xkb_names.rules, ec->xkb_names.model,
1702 ec->xkb_names.layout, ec->xkb_names.variant,
1703 ec->xkb_names.options);
1704 return -1;
1705 }
1706
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001707 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001708 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709 return -1;
1710
1711 return 0;
1712}
Rob Bradford382ff462013-06-24 16:52:45 +01001713#else
1714int
1715weston_compositor_xkb_init(struct weston_compositor *ec,
1716 struct xkb_rule_names *names)
1717{
1718 return 0;
1719}
1720
1721void
1722weston_compositor_xkb_destroy(struct weston_compositor *ec)
1723{
1724}
1725#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726
Rui Matos65196bc2013-10-10 19:44:19 +02001727WL_EXPORT void
1728weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
1729{
1730 if (!seat->keyboard || !keymap)
1731 return;
1732
1733#ifdef ENABLE_XKBCOMMON
1734 if (!seat->compositor->use_xkbcommon)
1735 return;
1736
1737 xkb_keymap_unref(seat->pending_keymap);
1738 seat->pending_keymap = xkb_keymap_ref(keymap);
1739
1740 if (seat->keyboard->keys.size == 0)
1741 update_keymap(seat);
1742#endif
1743}
1744
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001745WL_EXPORT int
1746weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1747{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001748 struct weston_keyboard *keyboard;
1749
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001750 if (seat->keyboard) {
1751 seat->keyboard_device_count += 1;
1752 if (seat->keyboard_device_count == 1)
1753 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001754 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001755 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001756
Rob Bradford382ff462013-06-24 16:52:45 +01001757#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001758 if (seat->compositor->use_xkbcommon) {
1759 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001760 seat->xkb_info = weston_xkb_info_create(keymap);
1761 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001762 return -1;
1763 } else {
1764 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1765 return -1;
1766 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001767 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001768 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001769
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001770 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001771 if (seat->xkb_state.state == NULL) {
1772 weston_log("failed to initialise XKB state\n");
1773 return -1;
1774 }
1775
1776 seat->xkb_state.leds = 0;
1777 }
Rob Bradford382ff462013-06-24 16:52:45 +01001778#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001779
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001780 keyboard = weston_keyboard_create();
1781 if (keyboard == NULL) {
1782 weston_log("failed to allocate weston keyboard struct\n");
1783 return -1;
1784 }
1785
1786 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001787 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001788 keyboard->seat = seat;
1789
1790 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001791
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792 return 0;
1793}
1794
1795WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001796weston_seat_release_keyboard(struct weston_seat *seat)
1797{
1798 seat->keyboard_device_count--;
1799 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001800 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001801 seat_send_updated_caps(seat);
1802 }
1803}
1804
1805WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001806weston_seat_init_pointer(struct weston_seat *seat)
1807{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001808 struct weston_pointer *pointer;
1809
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001810 if (seat->pointer) {
1811 seat->pointer_device_count += 1;
1812 if (seat->pointer_device_count == 1)
1813 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001814 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001815 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001817 pointer = weston_pointer_create();
1818 if (pointer == NULL)
1819 return;
1820
1821 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001822 seat->pointer_device_count = 1;
1823 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001824
1825 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001826}
1827
1828WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001829weston_seat_release_pointer(struct weston_seat *seat)
1830{
1831 struct weston_pointer *pointer = seat->pointer;
1832
1833 seat->pointer_device_count--;
1834 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001835 weston_pointer_set_focus(pointer, NULL,
1836 wl_fixed_from_int(0),
1837 wl_fixed_from_int(0));
1838
Jonas Ådahla4932742013-10-17 23:04:07 +02001839 if (pointer->sprite)
1840 pointer_unmap_sprite(pointer);
1841
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001842 seat_send_updated_caps(seat);
1843 }
1844}
1845
1846WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001847weston_seat_init_touch(struct weston_seat *seat)
1848{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001849 struct weston_touch *touch;
1850
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001851 if (seat->touch) {
1852 seat->touch_device_count += 1;
1853 if (seat->touch_device_count == 1)
1854 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001855 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001856 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001857
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001858 touch = weston_touch_create();
1859 if (touch == NULL)
1860 return;
1861
1862 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001863 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001864 touch->seat = seat;
1865
1866 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001867}
1868
1869WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001870weston_seat_release_touch(struct weston_seat *seat)
1871{
1872 seat->touch_device_count--;
1873 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001874 weston_touch_set_focus(seat, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001875 seat_send_updated_caps(seat);
1876 }
1877}
1878
1879WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001880weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1881 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001883 memset(seat, 0, sizeof *seat);
1884
Kristian Høgsberge3148752013-05-06 23:19:49 -04001885 seat->selection_data_source = NULL;
1886 wl_list_init(&seat->base_resource_list);
1887 wl_signal_init(&seat->selection_signal);
1888 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001889 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001890
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001891 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001892 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001894 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001895 seat->modifier_state = 0;
1896 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001897 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001898
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001899 wl_list_insert(ec->seat_list.prev, &seat->link);
1900
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001901 clipboard_create(seat);
1902
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001903 wl_signal_emit(&ec->seat_created_signal, seat);
1904}
1905
1906WL_EXPORT void
1907weston_seat_release(struct weston_seat *seat)
1908{
1909 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001910
Rob Bradford382ff462013-06-24 16:52:45 +01001911#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001912 if (seat->compositor->use_xkbcommon) {
1913 if (seat->xkb_state.state != NULL)
1914 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001915 if (seat->xkb_info)
1916 weston_xkb_info_destroy(seat->xkb_info);
Rui Matos65196bc2013-10-10 19:44:19 +02001917 if (seat->pending_keymap)
1918 xkb_keymap_unref (seat->pending_keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001919 }
Rob Bradford382ff462013-06-24 16:52:45 +01001920#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001921
Kristian Høgsberge3148752013-05-06 23:19:49 -04001922 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001923 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001924 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001925 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001926 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001927 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001928
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001929 free (seat->seat_name);
1930
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001931 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001932
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933 wl_signal_emit(&seat->destroy_signal, seat);
1934}