blob: 5ad247a9b2ae95f5576aafd639c5e6d3c319a5de [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}
Rob Bradford382ff462013-06-24 16:52:45 +0100904#else
905WL_EXPORT void
906notify_modifiers(struct weston_seat *seat, uint32_t serial)
907{
908}
909
910static void
911update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
912 enum wl_keyboard_key_state state)
913{
914}
915#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400916
917WL_EXPORT void
918notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
919 enum wl_keyboard_key_state state,
920 enum weston_key_state_update update_state)
921{
922 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400923 struct weston_keyboard *keyboard = seat->keyboard;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400925 struct weston_keyboard_grab *grab = keyboard->grab;
926 uint32_t serial = wl_display_next_serial(compositor->wl_display);
927 uint32_t *k, *end;
928
929 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
930 if (compositor->ping_handler && focus)
931 compositor->ping_handler(focus, serial);
932
933 weston_compositor_idle_inhibit(compositor);
934 keyboard->grab_key = key;
935 keyboard->grab_time = time;
936 } else {
937 weston_compositor_idle_release(compositor);
938 }
939
940 end = keyboard->keys.data + keyboard->keys.size;
941 for (k = keyboard->keys.data; k < end; k++) {
942 if (*k == key) {
943 /* Ignore server-generated repeats. */
944 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
945 return;
946 *k = *--end;
947 }
948 }
949 keyboard->keys.size = (void *) end - keyboard->keys.data;
950 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
951 k = wl_array_add(&keyboard->keys, sizeof *k);
952 *k = key;
953 }
954
955 if (grab == &keyboard->default_grab ||
956 grab == &keyboard->input_method_grab) {
957 weston_compositor_run_key_binding(compositor, seat, time, key,
958 state);
959 grab = keyboard->grab;
960 }
961
962 grab->interface->key(grab, time, key, state);
963
964 if (update_state == STATE_UPDATE_AUTOMATIC) {
965 update_modifier_state(seat,
966 wl_display_get_serial(compositor->wl_display),
967 key,
968 state);
969 }
970}
971
972WL_EXPORT void
973notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
974 wl_fixed_t x, wl_fixed_t y)
975{
976 struct weston_compositor *compositor = seat->compositor;
977
978 if (output) {
979 move_pointer(seat, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400980 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400981 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400982 * NULL) here, but somehow that breaks re-entry... */
983 }
984}
985
986static void
987destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
988{
989 struct weston_seat *ws;
990
991 ws = container_of(listener, struct weston_seat,
992 saved_kbd_focus_listener);
993
994 ws->saved_kbd_focus = NULL;
995}
996
997WL_EXPORT void
998notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
999 enum weston_key_state_update update_state)
1000{
1001 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001002 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001003 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001004 uint32_t *k, serial;
1005
1006 serial = wl_display_next_serial(compositor->wl_display);
1007 wl_array_copy(&keyboard->keys, keys);
1008 wl_array_for_each(k, &keyboard->keys) {
1009 weston_compositor_idle_inhibit(compositor);
1010 if (update_state == STATE_UPDATE_AUTOMATIC)
1011 update_modifier_state(seat, serial, *k,
1012 WL_KEYBOARD_KEY_STATE_PRESSED);
1013 }
1014
1015 /* Run key bindings after we've updated the state. */
1016 wl_array_for_each(k, &keyboard->keys) {
1017 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1018 WL_KEYBOARD_KEY_STATE_PRESSED);
1019 }
1020
1021 surface = seat->saved_kbd_focus;
1022
1023 if (surface) {
1024 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1025 weston_keyboard_set_focus(keyboard, surface);
1026 seat->saved_kbd_focus = NULL;
1027 }
1028}
1029
1030WL_EXPORT void
1031notify_keyboard_focus_out(struct weston_seat *seat)
1032{
1033 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001034 struct weston_keyboard *keyboard = seat->keyboard;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001035 uint32_t *k, serial;
1036
1037 serial = wl_display_next_serial(compositor->wl_display);
1038 wl_array_for_each(k, &keyboard->keys) {
1039 weston_compositor_idle_release(compositor);
1040 update_modifier_state(seat, serial, *k,
1041 WL_KEYBOARD_KEY_STATE_RELEASED);
1042 }
1043
1044 seat->modifier_state = 0;
1045
1046 if (keyboard->focus) {
1047 seat->saved_kbd_focus = keyboard->focus;
1048 seat->saved_kbd_focus_listener.notify =
1049 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001050 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001051 &seat->saved_kbd_focus_listener);
1052 }
1053
1054 weston_keyboard_set_focus(keyboard, NULL);
1055 /* FIXME: We really need keyboard grab cancel here to
1056 * let the grab shut down properly. As it is we leak
1057 * the grab data. */
1058 weston_keyboard_end_grab(keyboard);
1059}
1060
Michael Fua2bb7912013-07-23 15:51:06 +08001061WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001063{
Neil Roberts96d790e2013-09-19 17:32:00 +01001064 struct wl_list *focus_resource_list;
1065
1066 focus_resource_list = &seat->touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001067
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 if (seat->touch->focus->surface == view->surface) {
1069 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001070 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001072
Neil Roberts96d790e2013-09-19 17:32:00 +01001073 if (!wl_list_empty(focus_resource_list)) {
1074 move_resources(&seat->touch->resource_list,
1075 focus_resource_list);
1076 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001077
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 if (view) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001079 struct wl_client *surface_client =
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01001081 move_resources_for_client(focus_resource_list,
1082 &seat->touch->resource_list,
1083 surface_client);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001084 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 seat->touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001086}
1087
1088/**
1089 * notify_touch - emulates button touches and notifies surfaces accordingly.
1090 *
1091 * It assumes always the correct cycle sequence until it gets here: touch_down
1092 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1093 * for sending along such order.
1094 *
1095 */
1096WL_EXPORT void
1097notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1098 wl_fixed_t x, wl_fixed_t y, int touch_type)
1099{
1100 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 struct weston_touch *touch = seat->touch;
Kristian Høgsberge329f362013-05-06 22:19:57 -04001102 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001104 wl_fixed_t sx, sy;
1105
1106 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01001107 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1108 touch->grab_x = x;
1109 touch->grab_y = y;
1110 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001111
1112 switch (touch_type) {
1113 case WL_TOUCH_DOWN:
1114 weston_compositor_idle_inhibit(ec);
1115
1116 seat->num_tp++;
1117
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 /* the first finger down picks the view, and all further go
1119 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001120 * until all touch points are up again. */
1121 if (seat->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1123 weston_touch_set_focus(seat, ev);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001124 } else if (touch->focus) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 ev = touch->focus;
1126 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001127 } else {
1128 /* Unexpected condition: We have non-initial touch but
1129 * there is no focused surface.
1130 */
1131 weston_log("touch event received with %d points down"
1132 "but no surface focused\n", seat->num_tp);
1133 return;
1134 }
1135
1136 grab->interface->down(grab, time, touch_id, sx, sy);
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001137 if (seat->num_tp == 1) {
1138 touch->grab_serial =
1139 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01001140 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07001141 touch->grab_time = time;
1142 touch->grab_x = x;
1143 touch->grab_y = y;
1144 }
1145
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001146 break;
1147 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 ev = touch->focus;
1149 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001150 break;
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001153 grab->interface->motion(grab, time, touch_id, sx, sy);
1154 break;
1155 case WL_TOUCH_UP:
1156 weston_compositor_idle_release(ec);
1157 seat->num_tp--;
1158
1159 grab->interface->up(grab, time, touch_id);
1160 if (seat->num_tp == 0)
Michael Fua2bb7912013-07-23 15:51:06 +08001161 weston_touch_set_focus(seat, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001162 break;
1163 }
Neil Robertsa28c6932013-10-03 16:43:04 +01001164
1165 weston_compositor_run_touch_binding(ec, seat, time, touch_type);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001166}
1167
1168static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001169pointer_cursor_surface_configure(struct weston_surface *es,
1170 int32_t dx, int32_t dy, int32_t width, int32_t height)
1171{
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001172 struct weston_pointer *pointer = es->configure_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001173 int x, y;
1174
1175 if (width == 0)
1176 return;
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001179
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001180 pointer->hotspot_x -= dx;
1181 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001182
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001183 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1184 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_view_configure(pointer->sprite, x, y, width, height);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001187
1188 empty_region(&es->pending.input);
1189
1190 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 wl_list_insert(&es->compositor->cursor_layer.view_list,
1192 &pointer->sprite->layer_link);
1193 weston_view_update_transform(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001194 }
1195}
1196
1197static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001198pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1199 uint32_t serial, struct wl_resource *surface_resource,
1200 int32_t x, int32_t y)
1201{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001202 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001203 struct weston_surface *surface = NULL;
1204
1205 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001206 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001207
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001208 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001209 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001211 black_surface used in shell.c for fullscreen don't have
1212 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02001214 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001216 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001217 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001218 return;
1219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001221 if (surface->configure) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001222 wl_resource_post_error(surface->resource,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001223 WL_DISPLAY_ERROR_INVALID_OBJECT,
1224 "surface->configure already "
1225 "set");
1226 return;
1227 }
1228 }
1229
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001230 if (pointer->sprite)
1231 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001232
1233 if (!surface)
1234 return;
1235
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001236 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001237 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001238
1239 surface->configure = pointer_cursor_surface_configure;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001240 surface->configure_private = pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 pointer->sprite = weston_view_create(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001242 pointer->hotspot_x = x;
1243 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001244
1245 if (surface->buffer_ref.buffer)
1246 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1247 weston_surface_buffer_height(surface));
1248}
1249
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001250static void
1251pointer_release(struct wl_client *client, struct wl_resource *resource)
1252{
1253 wl_resource_destroy(resource);
1254}
1255
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001256static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001257 pointer_set_cursor,
1258 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001259};
1260
1261static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001262seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1263 uint32_t id)
1264{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001265 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001266 struct wl_resource *cr;
1267
Kristian Høgsberge3148752013-05-06 23:19:49 -04001268 if (!seat->pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001269 return;
1270
Jason Ekstranda85118c2013-06-27 20:17:02 -05001271 cr = wl_resource_create(client, &wl_pointer_interface,
1272 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001273 if (cr == NULL) {
1274 wl_client_post_no_memory(client);
1275 return;
1276 }
1277
Neil Roberts96d790e2013-09-19 17:32:00 +01001278 /* May be moved to focused list later by either
1279 * weston_pointer_set_focus or directly if this client is already
1280 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001281 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001282 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1283 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1286 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001287 wl_fixed_t sx, sy;
1288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 weston_view_from_global_fixed(seat->pointer->focus,
1290 seat->pointer->x,
1291 seat->pointer->y,
1292 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01001293
1294 wl_list_remove(wl_resource_get_link(cr));
1295 wl_list_insert(&seat->pointer->focus_resource_list,
1296 wl_resource_get_link(cr));
1297 wl_pointer_send_enter(cr,
1298 seat->pointer->focus_serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 seat->pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001300 sx, sy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001301 }
1302}
1303
1304static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001305keyboard_release(struct wl_client *client, struct wl_resource *resource)
1306{
1307 wl_resource_destroy(resource);
1308}
1309
1310static const struct wl_keyboard_interface keyboard_interface = {
1311 keyboard_release
1312};
1313
Neil Roberts96d790e2013-09-19 17:32:00 +01001314static int
1315should_send_modifiers_to_client(struct weston_seat *seat,
1316 struct wl_client *client)
1317{
1318 if (seat->keyboard &&
1319 seat->keyboard->focus &&
1320 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1321 return 1;
1322
1323 if (seat->pointer &&
1324 seat->pointer->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
Neil Roberts96d790e2013-09-19 17:32:00 +01001326 return 1;
1327
1328 return 0;
1329}
1330
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001331static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001332seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1333 uint32_t id)
1334{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001335 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001336 struct wl_resource *cr;
1337
Kristian Høgsberge3148752013-05-06 23:19:49 -04001338 if (!seat->keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001339 return;
1340
Jason Ekstranda85118c2013-06-27 20:17:02 -05001341 cr = wl_resource_create(client, &wl_keyboard_interface,
1342 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001343 if (cr == NULL) {
1344 wl_client_post_no_memory(client);
1345 return;
1346 }
1347
Neil Roberts96d790e2013-09-19 17:32:00 +01001348 /* May be moved to focused list later by either
1349 * weston_keyboard_set_focus or directly if this client is already
1350 * focused */
Jason Ekstrand44a38632013-06-14 10:08:00 -05001351 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001352 wl_resource_set_implementation(cr, &keyboard_interface,
1353 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001354
Matt Roper01a92732013-06-24 16:52:44 +01001355 if (seat->compositor->use_xkbcommon) {
1356 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001357 seat->xkb_info->keymap_fd,
1358 seat->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01001359 } else {
1360 int null_fd = open("/dev/null", O_RDONLY);
1361 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1362 null_fd,
1363 0);
1364 close(null_fd);
1365 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001366
Neil Roberts96d790e2013-09-19 17:32:00 +01001367 if (should_send_modifiers_to_client(seat, client)) {
1368 send_modifiers_to_resource(seat->keyboard,
1369 cr,
1370 seat->keyboard->focus_serial);
1371 }
1372
Kristian Høgsberge3148752013-05-06 23:19:49 -04001373 if (seat->keyboard->focus &&
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001374 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001375 struct weston_surface *surface =
1376 (struct weston_surface *) seat->keyboard->focus;
1377
1378 wl_list_remove(wl_resource_get_link(cr));
1379 wl_list_insert(&seat->keyboard->focus_resource_list,
1380 wl_resource_get_link(cr));
1381 wl_keyboard_send_enter(cr,
1382 seat->keyboard->focus_serial,
1383 surface->resource,
1384 &seat->keyboard->keys);
1385
1386 /* If this is the first keyboard resource for this
1387 * client... */
1388 if (seat->keyboard->focus_resource_list.prev ==
1389 wl_resource_get_link(cr))
1390 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001391 }
1392}
1393
1394static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001395touch_release(struct wl_client *client, struct wl_resource *resource)
1396{
1397 wl_resource_destroy(resource);
1398}
1399
1400static const struct wl_touch_interface touch_interface = {
1401 touch_release
1402};
1403
1404static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001405seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1406 uint32_t id)
1407{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001408 struct weston_seat *seat = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001409 struct wl_resource *cr;
1410
Kristian Høgsberge3148752013-05-06 23:19:49 -04001411 if (!seat->touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001412 return;
1413
Jason Ekstranda85118c2013-06-27 20:17:02 -05001414 cr = wl_resource_create(client, &wl_touch_interface,
1415 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001416 if (cr == NULL) {
1417 wl_client_post_no_memory(client);
1418 return;
1419 }
1420
Neil Roberts96d790e2013-09-19 17:32:00 +01001421 if (seat->touch->focus &&
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001423 wl_list_insert(&seat->touch->resource_list,
1424 wl_resource_get_link(cr));
1425 } else {
1426 wl_list_insert(&seat->touch->focus_resource_list,
1427 wl_resource_get_link(cr));
1428 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001429 wl_resource_set_implementation(cr, &touch_interface,
1430 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001431}
1432
1433static const struct wl_seat_interface seat_interface = {
1434 seat_get_pointer,
1435 seat_get_keyboard,
1436 seat_get_touch,
1437};
1438
1439static void
1440bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1441{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001442 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001443 struct wl_resource *resource;
1444 enum wl_seat_capability caps = 0;
1445
Jason Ekstranda85118c2013-06-27 20:17:02 -05001446 resource = wl_resource_create(client,
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001447 &wl_seat_interface, MIN(version, 3), id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001448 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05001449 wl_resource_set_implementation(resource, &seat_interface, data,
1450 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001451
1452 if (seat->pointer)
1453 caps |= WL_SEAT_CAPABILITY_POINTER;
1454 if (seat->keyboard)
1455 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1456 if (seat->touch)
1457 caps |= WL_SEAT_CAPABILITY_TOUCH;
1458
1459 wl_seat_send_capabilities(resource, caps);
Rob Bradforde445ae62013-05-31 18:09:51 +01001460 if (version >= 2)
1461 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001462}
1463
Rob Bradford382ff462013-06-24 16:52:45 +01001464#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001465int
1466weston_compositor_xkb_init(struct weston_compositor *ec,
1467 struct xkb_rule_names *names)
1468{
Rob Bradford382ff462013-06-24 16:52:45 +01001469 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01001470
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001471 if (ec->xkb_context == NULL) {
1472 ec->xkb_context = xkb_context_new(0);
1473 if (ec->xkb_context == NULL) {
1474 weston_log("failed to create XKB context\n");
1475 return -1;
1476 }
1477 }
1478
1479 if (names)
1480 ec->xkb_names = *names;
1481 if (!ec->xkb_names.rules)
1482 ec->xkb_names.rules = strdup("evdev");
1483 if (!ec->xkb_names.model)
1484 ec->xkb_names.model = strdup("pc105");
1485 if (!ec->xkb_names.layout)
1486 ec->xkb_names.layout = strdup("us");
1487
1488 return 0;
1489}
1490
Stefan Schmidtfda26522013-09-17 10:54:09 +01001491static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001492weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001493{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001494 if (--xkb_info->ref_count > 0)
1495 return;
1496
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001497 if (xkb_info->keymap)
1498 xkb_map_unref(xkb_info->keymap);
1499
1500 if (xkb_info->keymap_area)
1501 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1502 if (xkb_info->keymap_fd >= 0)
1503 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001504 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001505}
1506
1507void
1508weston_compositor_xkb_destroy(struct weston_compositor *ec)
1509{
Matt Roper01a92732013-06-24 16:52:44 +01001510 /*
1511 * If we're operating in raw keyboard mode, we never initialized
1512 * libxkbcommon so there's no cleanup to do either.
1513 */
1514 if (!ec->use_xkbcommon)
1515 return;
1516
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001517 free((char *) ec->xkb_names.rules);
1518 free((char *) ec->xkb_names.model);
1519 free((char *) ec->xkb_names.layout);
1520 free((char *) ec->xkb_names.variant);
1521 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001522
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001523 if (ec->xkb_info)
1524 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001525 xkb_context_unref(ec->xkb_context);
1526}
1527
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001528static struct weston_xkb_info *
1529weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001530{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001531 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1532 if (xkb_info == NULL)
1533 return NULL;
1534
1535 xkb_info->keymap = xkb_map_ref(keymap);
1536 xkb_info->ref_count = 1;
1537
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001538 char *keymap_str;
1539
1540 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1541 XKB_MOD_NAME_SHIFT);
1542 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1543 XKB_MOD_NAME_CAPS);
1544 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1545 XKB_MOD_NAME_CTRL);
1546 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1547 XKB_MOD_NAME_ALT);
1548 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1549 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1550 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1551 XKB_MOD_NAME_LOGO);
1552 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1553
1554 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1555 XKB_LED_NAME_NUM);
1556 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1557 XKB_LED_NAME_CAPS);
1558 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1559 XKB_LED_NAME_SCROLL);
1560
1561 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1562 if (keymap_str == NULL) {
1563 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001564 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001565 }
1566 xkb_info->keymap_size = strlen(keymap_str) + 1;
1567
1568 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1569 if (xkb_info->keymap_fd < 0) {
1570 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1571 (unsigned long) xkb_info->keymap_size);
1572 goto err_keymap_str;
1573 }
1574
1575 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1576 PROT_READ | PROT_WRITE,
1577 MAP_SHARED, xkb_info->keymap_fd, 0);
1578 if (xkb_info->keymap_area == MAP_FAILED) {
1579 weston_log("failed to mmap() %lu bytes\n",
1580 (unsigned long) xkb_info->keymap_size);
1581 goto err_dev_zero;
1582 }
1583 strcpy(xkb_info->keymap_area, keymap_str);
1584 free(keymap_str);
1585
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001586 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001587
1588err_dev_zero:
1589 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001590err_keymap_str:
1591 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001592err_keymap:
1593 xkb_map_unref(xkb_info->keymap);
1594 free(xkb_info);
1595 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596}
1597
1598static int
1599weston_compositor_build_global_keymap(struct weston_compositor *ec)
1600{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001601 struct xkb_keymap *keymap;
1602
1603 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001604 return 0;
1605
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001606 keymap = xkb_map_new_from_names(ec->xkb_context,
1607 &ec->xkb_names,
1608 0);
1609 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001610 weston_log("failed to compile global XKB keymap\n");
1611 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
1612 "options %s\n",
1613 ec->xkb_names.rules, ec->xkb_names.model,
1614 ec->xkb_names.layout, ec->xkb_names.variant,
1615 ec->xkb_names.options);
1616 return -1;
1617 }
1618
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001619 ec->xkb_info = weston_xkb_info_create(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01001620 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001621 return -1;
1622
1623 return 0;
1624}
Rob Bradford382ff462013-06-24 16:52:45 +01001625#else
1626int
1627weston_compositor_xkb_init(struct weston_compositor *ec,
1628 struct xkb_rule_names *names)
1629{
1630 return 0;
1631}
1632
1633void
1634weston_compositor_xkb_destroy(struct weston_compositor *ec)
1635{
1636}
1637#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001638
1639WL_EXPORT int
1640weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1641{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001642 struct weston_keyboard *keyboard;
1643
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001644 if (seat->keyboard) {
1645 seat->keyboard_device_count += 1;
1646 if (seat->keyboard_device_count == 1)
1647 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001648 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001649 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001650
Rob Bradford382ff462013-06-24 16:52:45 +01001651#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001652 if (seat->compositor->use_xkbcommon) {
1653 if (keymap != NULL) {
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001654 seat->xkb_info = weston_xkb_info_create(keymap);
1655 if (seat->xkb_info == NULL)
Matt Roper01a92732013-06-24 16:52:44 +01001656 return -1;
1657 } else {
1658 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1659 return -1;
1660 seat->xkb_info = seat->compositor->xkb_info;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001661 seat->xkb_info->ref_count++;
Matt Roper01a92732013-06-24 16:52:44 +01001662 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001663
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001664 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
Matt Roper01a92732013-06-24 16:52:44 +01001665 if (seat->xkb_state.state == NULL) {
1666 weston_log("failed to initialise XKB state\n");
1667 return -1;
1668 }
1669
1670 seat->xkb_state.leds = 0;
1671 }
Rob Bradford382ff462013-06-24 16:52:45 +01001672#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001673
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001674 keyboard = weston_keyboard_create();
1675 if (keyboard == NULL) {
1676 weston_log("failed to allocate weston keyboard struct\n");
1677 return -1;
1678 }
1679
1680 seat->keyboard = keyboard;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001681 seat->keyboard_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001682 keyboard->seat = seat;
1683
1684 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001685
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686 return 0;
1687}
1688
1689WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001690weston_seat_release_keyboard(struct weston_seat *seat)
1691{
1692 seat->keyboard_device_count--;
1693 if (seat->keyboard_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001694 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001695 seat_send_updated_caps(seat);
1696 }
1697}
1698
1699WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700weston_seat_init_pointer(struct weston_seat *seat)
1701{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001702 struct weston_pointer *pointer;
1703
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001704 if (seat->pointer) {
1705 seat->pointer_device_count += 1;
1706 if (seat->pointer_device_count == 1)
1707 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001709 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001711 pointer = weston_pointer_create();
1712 if (pointer == NULL)
1713 return;
1714
1715 seat->pointer = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001716 seat->pointer_device_count = 1;
1717 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001718
1719 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720}
1721
1722WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001723weston_seat_release_pointer(struct weston_seat *seat)
1724{
1725 struct weston_pointer *pointer = seat->pointer;
1726
1727 seat->pointer_device_count--;
1728 if (seat->pointer_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001729 weston_pointer_set_focus(pointer, NULL,
1730 wl_fixed_from_int(0),
1731 wl_fixed_from_int(0));
1732
Jonas Ådahla4932742013-10-17 23:04:07 +02001733 if (pointer->sprite)
1734 pointer_unmap_sprite(pointer);
1735
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001736 seat_send_updated_caps(seat);
1737 }
1738}
1739
1740WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001741weston_seat_init_touch(struct weston_seat *seat)
1742{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001743 struct weston_touch *touch;
1744
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001745 if (seat->touch) {
1746 seat->touch_device_count += 1;
1747 if (seat->touch_device_count == 1)
1748 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001749 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001750 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001751
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001752 touch = weston_touch_create();
1753 if (touch == NULL)
1754 return;
1755
1756 seat->touch = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001757 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001758 touch->seat = seat;
1759
1760 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001761}
1762
1763WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001764weston_seat_release_touch(struct weston_seat *seat)
1765{
1766 seat->touch_device_count--;
1767 if (seat->touch_device_count == 0) {
Jonas Ådahl630bae82013-10-17 23:04:06 +02001768 weston_touch_set_focus(seat, NULL);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001769 seat_send_updated_caps(seat);
1770 }
1771}
1772
1773WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001774weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1775 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001776{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001777 memset(seat, 0, sizeof *seat);
1778
Kristian Høgsberge3148752013-05-06 23:19:49 -04001779 seat->selection_data_source = NULL;
1780 wl_list_init(&seat->base_resource_list);
1781 wl_signal_init(&seat->selection_signal);
1782 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001783 wl_signal_init(&seat->destroy_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001784
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01001785 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001786 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001788 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001789 seat->modifier_state = 0;
1790 seat->num_tp = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001791 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793 wl_list_insert(ec->seat_list.prev, &seat->link);
1794
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001795 clipboard_create(seat);
1796
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001797 wl_signal_emit(&ec->seat_created_signal, seat);
1798}
1799
1800WL_EXPORT void
1801weston_seat_release(struct weston_seat *seat)
1802{
1803 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001804
Rob Bradford382ff462013-06-24 16:52:45 +01001805#ifdef ENABLE_XKBCOMMON
Matt Roper01a92732013-06-24 16:52:44 +01001806 if (seat->compositor->use_xkbcommon) {
1807 if (seat->xkb_state.state != NULL)
1808 xkb_state_unref(seat->xkb_state.state);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00001809 if (seat->xkb_info)
1810 weston_xkb_info_destroy(seat->xkb_info);
Matt Roper01a92732013-06-24 16:52:44 +01001811 }
Rob Bradford382ff462013-06-24 16:52:45 +01001812#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001813
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 if (seat->pointer)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001815 weston_pointer_destroy(seat->pointer);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001816 if (seat->keyboard)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001817 weston_keyboard_destroy(seat->keyboard);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001818 if (seat->touch)
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001819 weston_touch_destroy(seat->touch);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04001820
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001821 free (seat->seat_name);
1822
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04001823 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04001824
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825 wl_signal_emit(&seat->destroy_signal, seat);
1826}