blob: 8ef5b2a1ee1f4a21bf8a765f9c449fb1848ede03 [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
23#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
26
27#include "compositor.h"
28
29static void
30lose_pointer_focus(struct wl_listener *listener, void *data)
31{
32 struct wl_pointer *pointer =
33 container_of(listener, struct wl_pointer, focus_listener);
34
35 pointer->focus_resource = NULL;
36}
37
38static void
39lose_keyboard_focus(struct wl_listener *listener, void *data)
40{
41 struct wl_keyboard *keyboard =
42 container_of(listener, struct wl_keyboard, focus_listener);
43
44 keyboard->focus_resource = NULL;
45}
46
47static void
48lose_touch_focus(struct wl_listener *listener, void *data)
49{
50 struct wl_touch *touch =
51 container_of(listener, struct wl_touch, focus_listener);
52
53 touch->focus_resource = NULL;
54}
55
56static void
57default_grab_focus(struct wl_pointer_grab *grab,
58 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
59{
60 struct wl_pointer *pointer = grab->pointer;
61
62 if (pointer->button_count > 0)
63 return;
64
65 wl_pointer_set_focus(pointer, surface, x, y);
66}
67
68static void
69default_grab_motion(struct wl_pointer_grab *grab,
70 uint32_t time, wl_fixed_t x, wl_fixed_t y)
71{
72 struct wl_resource *resource;
73
74 resource = grab->pointer->focus_resource;
75 if (resource)
76 wl_pointer_send_motion(resource, time, x, y);
77}
78
79static void
80default_grab_button(struct wl_pointer_grab *grab,
81 uint32_t time, uint32_t button, uint32_t state_w)
82{
83 struct wl_pointer *pointer = grab->pointer;
84 struct wl_resource *resource;
85 uint32_t serial;
86 enum wl_pointer_button_state state = state_w;
87 struct wl_display *display;
88
89 resource = pointer->focus_resource;
90 if (resource) {
91 display = wl_client_get_display(resource->client);
92 serial = wl_display_next_serial(display);
93 wl_pointer_send_button(resource, serial, time, button, state_w);
94 }
95
96 if (pointer->button_count == 0 &&
97 state == WL_POINTER_BUTTON_STATE_RELEASED)
98 wl_pointer_set_focus(pointer, pointer->current,
99 pointer->current_x, pointer->current_y);
100}
101
102static const struct wl_pointer_grab_interface
103 default_pointer_grab_interface = {
104 default_grab_focus,
105 default_grab_motion,
106 default_grab_button
107};
108
109static void default_grab_touch_down(struct wl_touch_grab *grab,
110 uint32_t time,
111 int touch_id,
112 wl_fixed_t sx,
113 wl_fixed_t sy)
114{
115 struct wl_touch *touch = grab->touch;
116 struct wl_display *display;
117 uint32_t serial;
118
119 if (touch->focus_resource && touch->focus) {
120 display = wl_client_get_display(touch->focus_resource->client);
121 serial = wl_display_next_serial(display);
122 wl_touch_send_down(touch->focus_resource, serial, time,
123 &touch->focus->resource, touch_id, sx, sy);
124 }
125}
126
127static void default_grab_touch_up(struct wl_touch_grab *grab,
128 uint32_t time,
129 int touch_id)
130{
131 struct wl_touch *touch = grab->touch;
132 struct wl_display *display;
133 uint32_t serial;
134
135 if (touch->focus_resource) {
136 display = wl_client_get_display(touch->focus_resource->client);
137 serial = wl_display_next_serial(display);
138 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
139 }
140}
141
142static void default_grab_touch_motion(struct wl_touch_grab *grab,
143 uint32_t time,
144 int touch_id,
145 wl_fixed_t sx,
146 wl_fixed_t sy)
147{
148 struct wl_touch *touch = grab->touch;
149
150 if (touch->focus_resource) {
151 wl_touch_send_motion(touch->focus_resource, time,
152 touch_id, sx, sy);
153 }
154}
155
156static const struct wl_touch_grab_interface default_touch_grab_interface = {
157 default_grab_touch_down,
158 default_grab_touch_up,
159 default_grab_touch_motion
160};
161
162static void
163default_grab_key(struct wl_keyboard_grab *grab,
164 uint32_t time, uint32_t key, uint32_t state)
165{
166 struct wl_keyboard *keyboard = grab->keyboard;
167 struct wl_resource *resource;
168 struct wl_display *display;
169 uint32_t serial;
170
171 resource = keyboard->focus_resource;
172 if (resource) {
173 display = wl_client_get_display(resource->client);
174 serial = wl_display_next_serial(display);
175 wl_keyboard_send_key(resource, serial, time, key, state);
176 }
177}
178
179static struct wl_resource *
180find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
181{
182 struct wl_resource *r;
183
184 if (!surface)
185 return NULL;
186
187 wl_list_for_each(r, list, link) {
188 if (r->client == surface->resource.client)
189 return r;
190 }
191
192 return NULL;
193}
194
195static void
196default_grab_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
197 uint32_t mods_depressed, uint32_t mods_latched,
198 uint32_t mods_locked, uint32_t group)
199{
200 struct wl_keyboard *keyboard = grab->keyboard;
201 struct wl_pointer *pointer = keyboard->seat->pointer;
202 struct wl_resource *resource, *pr;
203
204 resource = keyboard->focus_resource;
205 if (!resource)
206 return;
207
208 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
209 mods_latched, mods_locked, group);
210
211 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
212 pr = find_resource_for_surface(&keyboard->resource_list,
213 pointer->focus);
214 if (pr) {
215 wl_keyboard_send_modifiers(pr,
216 serial,
217 keyboard->modifiers.mods_depressed,
218 keyboard->modifiers.mods_latched,
219 keyboard->modifiers.mods_locked,
220 keyboard->modifiers.group);
221 }
222 }
223}
224
225static const struct wl_keyboard_grab_interface
226 default_keyboard_grab_interface = {
227 default_grab_key,
228 default_grab_modifiers,
229};
230
231WL_EXPORT void
232wl_pointer_init(struct wl_pointer *pointer)
233{
234 memset(pointer, 0, sizeof *pointer);
235 wl_list_init(&pointer->resource_list);
236 pointer->focus_listener.notify = lose_pointer_focus;
237 pointer->default_grab.interface = &default_pointer_grab_interface;
238 pointer->default_grab.pointer = pointer;
239 pointer->grab = &pointer->default_grab;
240 wl_signal_init(&pointer->focus_signal);
241
242 /* FIXME: Pick better co-ords. */
243 pointer->x = wl_fixed_from_int(100);
244 pointer->y = wl_fixed_from_int(100);
245}
246
247WL_EXPORT void
248wl_pointer_release(struct wl_pointer *pointer)
249{
250 /* XXX: What about pointer->resource_list? */
251 if (pointer->focus_resource)
252 wl_list_remove(&pointer->focus_listener.link);
253}
254
255WL_EXPORT void
256wl_keyboard_init(struct wl_keyboard *keyboard)
257{
258 memset(keyboard, 0, sizeof *keyboard);
259 wl_list_init(&keyboard->resource_list);
260 wl_array_init(&keyboard->keys);
261 keyboard->focus_listener.notify = lose_keyboard_focus;
262 keyboard->default_grab.interface = &default_keyboard_grab_interface;
263 keyboard->default_grab.keyboard = keyboard;
264 keyboard->grab = &keyboard->default_grab;
265 wl_signal_init(&keyboard->focus_signal);
266}
267
268WL_EXPORT void
269wl_keyboard_release(struct wl_keyboard *keyboard)
270{
271 /* XXX: What about keyboard->resource_list? */
272 if (keyboard->focus_resource)
273 wl_list_remove(&keyboard->focus_listener.link);
274 wl_array_release(&keyboard->keys);
275}
276
277WL_EXPORT void
278wl_touch_init(struct wl_touch *touch)
279{
280 memset(touch, 0, sizeof *touch);
281 wl_list_init(&touch->resource_list);
282 touch->focus_listener.notify = lose_touch_focus;
283 touch->default_grab.interface = &default_touch_grab_interface;
284 touch->default_grab.touch = touch;
285 touch->grab = &touch->default_grab;
286 wl_signal_init(&touch->focus_signal);
287}
288
289WL_EXPORT void
290wl_touch_release(struct wl_touch *touch)
291{
292 /* XXX: What about touch->resource_list? */
293 if (touch->focus_resource)
294 wl_list_remove(&touch->focus_listener.link);
295}
296
297WL_EXPORT void
298wl_seat_init(struct wl_seat *seat)
299{
300 memset(seat, 0, sizeof *seat);
301
302 wl_signal_init(&seat->destroy_signal);
303
304 seat->selection_data_source = NULL;
305 wl_list_init(&seat->base_resource_list);
306 wl_signal_init(&seat->selection_signal);
307 wl_list_init(&seat->drag_resource_list);
308 wl_signal_init(&seat->drag_icon_signal);
309}
310
311WL_EXPORT void
312wl_seat_release(struct wl_seat *seat)
313{
314 wl_signal_emit(&seat->destroy_signal, seat);
315
316 if (seat->pointer)
317 wl_pointer_release(seat->pointer);
318 if (seat->keyboard)
319 wl_keyboard_release(seat->keyboard);
320 if (seat->touch)
321 wl_touch_release(seat->touch);
322}
323
324static void
325seat_send_updated_caps(struct wl_seat *seat)
326{
327 struct wl_resource *r;
328 enum wl_seat_capability caps = 0;
329
330 if (seat->pointer)
331 caps |= WL_SEAT_CAPABILITY_POINTER;
332 if (seat->keyboard)
333 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
334 if (seat->touch)
335 caps |= WL_SEAT_CAPABILITY_TOUCH;
336
337 wl_list_for_each(r, &seat->base_resource_list, link)
338 wl_seat_send_capabilities(r, caps);
339}
340
341WL_EXPORT void
342wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer)
343{
344 if (pointer && (seat->pointer || pointer->seat))
345 return; /* XXX: error? */
346 if (!pointer && !seat->pointer)
347 return;
348
349 seat->pointer = pointer;
350 if (pointer)
351 pointer->seat = seat;
352
353 seat_send_updated_caps(seat);
354}
355
356WL_EXPORT void
357wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard)
358{
359 if (keyboard && (seat->keyboard || keyboard->seat))
360 return; /* XXX: error? */
361 if (!keyboard && !seat->keyboard)
362 return;
363
364 seat->keyboard = keyboard;
365 if (keyboard)
366 keyboard->seat = seat;
367
368 seat_send_updated_caps(seat);
369}
370
371WL_EXPORT void
372wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch)
373{
374 if (touch && (seat->touch || touch->seat))
375 return; /* XXX: error? */
376 if (!touch && !seat->touch)
377 return;
378
379 seat->touch = touch;
380 if (touch)
381 touch->seat = seat;
382
383 seat_send_updated_caps(seat);
384}
385
386WL_EXPORT void
387wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
388 wl_fixed_t sx, wl_fixed_t sy)
389{
390 struct wl_keyboard *kbd = pointer->seat->keyboard;
391 struct wl_resource *resource, *kr;
392 struct wl_display *display;
393 uint32_t serial;
394
395 resource = pointer->focus_resource;
396 if (resource && pointer->focus != surface) {
397 display = wl_client_get_display(resource->client);
398 serial = wl_display_next_serial(display);
399 wl_pointer_send_leave(resource, serial,
400 &pointer->focus->resource);
401 wl_list_remove(&pointer->focus_listener.link);
402 }
403
404 resource = find_resource_for_surface(&pointer->resource_list,
405 surface);
406 if (resource &&
407 (pointer->focus != surface ||
408 pointer->focus_resource != resource)) {
409 display = wl_client_get_display(resource->client);
410 serial = wl_display_next_serial(display);
411 if (kbd) {
412 kr = find_resource_for_surface(&kbd->resource_list,
413 surface);
414 if (kr) {
415 wl_keyboard_send_modifiers(kr,
416 serial,
417 kbd->modifiers.mods_depressed,
418 kbd->modifiers.mods_latched,
419 kbd->modifiers.mods_locked,
420 kbd->modifiers.group);
421 }
422 }
423 wl_pointer_send_enter(resource, serial, &surface->resource,
424 sx, sy);
425 wl_signal_add(&resource->destroy_signal,
426 &pointer->focus_listener);
427 pointer->focus_serial = serial;
428 }
429
430 pointer->focus_resource = resource;
431 pointer->focus = surface;
432 pointer->default_grab.focus = surface;
433 wl_signal_emit(&pointer->focus_signal, pointer);
434}
435
436WL_EXPORT void
437wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface)
438{
439 struct wl_resource *resource;
440 struct wl_display *display;
441 uint32_t serial;
442
443 if (keyboard->focus_resource && keyboard->focus != surface) {
444 resource = keyboard->focus_resource;
445 display = wl_client_get_display(resource->client);
446 serial = wl_display_next_serial(display);
447 wl_keyboard_send_leave(resource, serial,
448 &keyboard->focus->resource);
449 wl_list_remove(&keyboard->focus_listener.link);
450 }
451
452 resource = find_resource_for_surface(&keyboard->resource_list,
453 surface);
454 if (resource &&
455 (keyboard->focus != surface ||
456 keyboard->focus_resource != resource)) {
457 display = wl_client_get_display(resource->client);
458 serial = wl_display_next_serial(display);
459 wl_keyboard_send_modifiers(resource, serial,
460 keyboard->modifiers.mods_depressed,
461 keyboard->modifiers.mods_latched,
462 keyboard->modifiers.mods_locked,
463 keyboard->modifiers.group);
464 wl_keyboard_send_enter(resource, serial, &surface->resource,
465 &keyboard->keys);
466 wl_signal_add(&resource->destroy_signal,
467 &keyboard->focus_listener);
468 keyboard->focus_serial = serial;
469 }
470
471 keyboard->focus_resource = resource;
472 keyboard->focus = surface;
473 wl_signal_emit(&keyboard->focus_signal, keyboard);
474}
475
476WL_EXPORT void
477wl_keyboard_start_grab(struct wl_keyboard *keyboard,
478 struct wl_keyboard_grab *grab)
479{
480 keyboard->grab = grab;
481 grab->keyboard = keyboard;
482
483 /* XXX focus? */
484}
485
486WL_EXPORT void
487wl_keyboard_end_grab(struct wl_keyboard *keyboard)
488{
489 keyboard->grab = &keyboard->default_grab;
490}
491
492WL_EXPORT void
493wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab)
494{
495 const struct wl_pointer_grab_interface *interface;
496
497 pointer->grab = grab;
498 interface = pointer->grab->interface;
499 grab->pointer = pointer;
500
501 if (pointer->current)
502 interface->focus(pointer->grab, pointer->current,
503 pointer->current_x, pointer->current_y);
504}
505
506WL_EXPORT void
507wl_pointer_end_grab(struct wl_pointer *pointer)
508{
509 const struct wl_pointer_grab_interface *interface;
510
511 pointer->grab = &pointer->default_grab;
512 interface = pointer->grab->interface;
513 interface->focus(pointer->grab, pointer->current,
514 pointer->current_x, pointer->current_y);
515}
516
517static void
518current_surface_destroy(struct wl_listener *listener, void *data)
519{
520 struct wl_pointer *pointer =
521 container_of(listener, struct wl_pointer, current_listener);
522
523 pointer->current = NULL;
524}
525
526WL_EXPORT void
527wl_pointer_set_current(struct wl_pointer *pointer, struct wl_surface *surface)
528{
529 if (pointer->current)
530 wl_list_remove(&pointer->current_listener.link);
531
532 pointer->current = surface;
533
534 if (!surface)
535 return;
536
537 wl_signal_add(&surface->resource.destroy_signal,
538 &pointer->current_listener);
539 pointer->current_listener.notify = current_surface_destroy;
540}
541
542WL_EXPORT void
543wl_touch_start_grab(struct wl_touch *touch, struct wl_touch_grab *grab)
544{
545 touch->grab = grab;
546 grab->touch = touch;
547}
548
549WL_EXPORT void
550wl_touch_end_grab(struct wl_touch *touch)
551{
552 touch->grab = &touch->default_grab;
553}