blob: b92546dff30bf13c4dfeb59b1bee4fb6e3a57f5e [file] [log] [blame]
Quentin Glidic248dd102016-08-12 10:41:34 +02001/*
2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
5 * Copyright © 2016 Quentin "Sardem FF7" Glidic
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27#include "config.h"
28
29#include <assert.h>
30
31#include <wayland-server.h>
32
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020033#include <libweston/libweston.h>
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030034#include <libweston/zalloc.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020035
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030036#include <libweston-desktop/libweston-desktop.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020037#include "internal.h"
Alexandros Frantzis215bedc2017-11-16 18:20:55 +020038#include "shared/timespec-util.h"
Quentin Glidic248dd102016-08-12 10:41:34 +020039
40struct weston_desktop_seat {
41 struct wl_listener seat_destroy_listener;
42 struct weston_seat *seat;
43 struct {
44 struct weston_keyboard_grab keyboard;
45 struct weston_pointer_grab pointer;
46 struct weston_touch_grab touch;
47 bool initial_up;
48 struct wl_client *client;
49 struct wl_list surfaces;
50 } popup_grab;
51};
52
53static void weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat);
54
55static void
56weston_desktop_seat_popup_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020057 const struct timespec *time,
58 uint32_t key,
Quentin Glidic248dd102016-08-12 10:41:34 +020059 enum wl_keyboard_key_state state)
60{
61 weston_keyboard_send_key(grab->keyboard, time, key, state);
62}
63
64static void
65weston_desktop_seat_popup_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
66 uint32_t serial,
67 uint32_t mods_depressed,
68 uint32_t mods_latched,
69 uint32_t mods_locked,
70 uint32_t group)
71{
72 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
73 mods_latched, mods_locked, group);
74}
75
76static void
77weston_desktop_seat_popup_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
78{
79 struct weston_desktop_seat *seat =
80 wl_container_of(grab, seat, popup_grab.keyboard);
81
82 weston_desktop_seat_popup_grab_end(seat);
83}
84
85static const struct weston_keyboard_grab_interface weston_desktop_seat_keyboard_popup_grab_interface = {
86 .key = weston_desktop_seat_popup_grab_keyboard_key,
87 .modifiers = weston_desktop_seat_popup_grab_keyboard_modifiers,
88 .cancel = weston_desktop_seat_popup_grab_keyboard_cancel,
89};
90
91static void
92weston_desktop_seat_popup_grab_pointer_focus(struct weston_pointer_grab *grab)
93{
94 struct weston_desktop_seat *seat =
95 wl_container_of(grab, seat, popup_grab.pointer);
96 struct weston_pointer *pointer = grab->pointer;
97 struct weston_view *view;
98 wl_fixed_t sx, sy;
99
100 view = weston_compositor_pick_view(pointer->seat->compositor,
101 pointer->x, pointer->y, &sx, &sy);
102
103 if (view != NULL &&
104 view->surface->resource != NULL &&
105 wl_resource_get_client(view->surface->resource) == seat->popup_grab.client)
106 weston_pointer_set_focus(pointer, view, sx, sy);
107 else
108 weston_pointer_clear_focus(pointer);
109}
110
111static void
112weston_desktop_seat_popup_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200113 const struct timespec *time,
Quentin Glidic248dd102016-08-12 10:41:34 +0200114 struct weston_pointer_motion_event *event)
115{
116 weston_pointer_send_motion(grab->pointer, time, event);
117}
118
119static void
120weston_desktop_seat_popup_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200121 const struct timespec *time,
122 uint32_t button,
Quentin Glidic248dd102016-08-12 10:41:34 +0200123 enum wl_pointer_button_state state)
124{
125 struct weston_desktop_seat *seat =
126 wl_container_of(grab, seat, popup_grab.pointer);
127 struct weston_pointer *pointer = grab->pointer;
128 bool initial_up = seat->popup_grab.initial_up;
129
130 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
131 seat->popup_grab.initial_up = true;
132
133 if (weston_pointer_has_focus_resource(pointer))
134 weston_pointer_send_button(pointer, time, button, state);
135 else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200136 (initial_up ||
137 (timespec_sub_to_msec(time, &grab->pointer->grab_time) > 500)))
Quentin Glidic248dd102016-08-12 10:41:34 +0200138 weston_desktop_seat_popup_grab_end(seat);
139}
140
141static void
142weston_desktop_seat_popup_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200143 const struct timespec *time,
Quentin Glidic248dd102016-08-12 10:41:34 +0200144 struct weston_pointer_axis_event *event)
145{
146 weston_pointer_send_axis(grab->pointer, time, event);
147}
148
149static void
150weston_desktop_seat_popup_grab_pointer_axis_source(struct weston_pointer_grab *grab,
151 uint32_t source)
152{
153 weston_pointer_send_axis_source(grab->pointer, source);
154}
155
156static void
157weston_desktop_seat_popup_grab_pointer_frame(struct weston_pointer_grab *grab)
158{
159 weston_pointer_send_frame(grab->pointer);
160}
161
162static void
163weston_desktop_seat_popup_grab_pointer_cancel(struct weston_pointer_grab *grab)
164{
165 struct weston_desktop_seat *seat =
166 wl_container_of(grab, seat, popup_grab.pointer);
167
168 weston_desktop_seat_popup_grab_end(seat);
169}
170
171static const struct weston_pointer_grab_interface weston_desktop_seat_pointer_popup_grab_interface = {
172 .focus = weston_desktop_seat_popup_grab_pointer_focus,
173 .motion = weston_desktop_seat_popup_grab_pointer_motion,
174 .button = weston_desktop_seat_popup_grab_pointer_button,
175 .axis = weston_desktop_seat_popup_grab_pointer_axis,
176 .axis_source = weston_desktop_seat_popup_grab_pointer_axis_source,
177 .frame = weston_desktop_seat_popup_grab_pointer_frame,
178 .cancel = weston_desktop_seat_popup_grab_pointer_cancel,
179};
180
181static void
182weston_desktop_seat_popup_grab_touch_down(struct weston_touch_grab *grab,
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200183 const struct timespec *time,
184 int touch_id,
Quentin Glidic248dd102016-08-12 10:41:34 +0200185 wl_fixed_t sx, wl_fixed_t sy)
186{
187 weston_touch_send_down(grab->touch, time, touch_id, sx, sy);
188}
189
190static void
191weston_desktop_seat_popup_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200192 const struct timespec *time,
193 int touch_id)
Quentin Glidic248dd102016-08-12 10:41:34 +0200194{
195 weston_touch_send_up(grab->touch, time, touch_id);
196}
197
198static void
199weston_desktop_seat_popup_grab_touch_motion(struct weston_touch_grab *grab,
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200200 const struct timespec *time,
201 int touch_id,
Quentin Glidic248dd102016-08-12 10:41:34 +0200202 wl_fixed_t sx, wl_fixed_t sy)
203{
204 weston_touch_send_motion(grab->touch, time, touch_id, sx, sy);
205}
206
207static void
208weston_desktop_seat_popup_grab_touch_frame(struct weston_touch_grab *grab)
209{
210 weston_touch_send_frame(grab->touch);
211}
212
213static void
214weston_desktop_seat_popup_grab_touch_cancel(struct weston_touch_grab *grab)
215{
216 struct weston_desktop_seat *seat =
217 wl_container_of(grab, seat, popup_grab.touch);
218
219 weston_desktop_seat_popup_grab_end(seat);
220}
221
222static const struct weston_touch_grab_interface weston_desktop_seat_touch_popup_grab_interface = {
223 .down = weston_desktop_seat_popup_grab_touch_down,
224 .up = weston_desktop_seat_popup_grab_touch_up,
225 .motion = weston_desktop_seat_popup_grab_touch_motion,
226 .frame = weston_desktop_seat_popup_grab_touch_frame,
227 .cancel = weston_desktop_seat_popup_grab_touch_cancel,
228};
229
230static void
231weston_desktop_seat_destroy(struct wl_listener *listener, void *data)
232{
233 struct weston_desktop_seat *seat =
234 wl_container_of(listener, seat, seat_destroy_listener);
235
236 free(seat);
237}
238
239struct weston_desktop_seat *
240weston_desktop_seat_from_seat(struct weston_seat *wseat)
241{
242 struct wl_listener *listener;
243 struct weston_desktop_seat *seat;
244
Alexandros Frantzis8480d132018-02-15 13:07:09 +0200245 if (wseat == NULL)
246 return NULL;
247
Quentin Glidic248dd102016-08-12 10:41:34 +0200248 listener = wl_signal_get(&wseat->destroy_signal,
249 weston_desktop_seat_destroy);
250 if (listener != NULL)
251 return wl_container_of(listener, seat, seat_destroy_listener);
252
253 seat = zalloc(sizeof(struct weston_desktop_seat));
254 if (seat == NULL)
255 return NULL;
256
257 seat->seat = wseat;
258
259 seat->seat_destroy_listener.notify = weston_desktop_seat_destroy;
260 wl_signal_add(&wseat->destroy_signal, &seat->seat_destroy_listener);
261
262 seat->popup_grab.keyboard.interface =
263 &weston_desktop_seat_keyboard_popup_grab_interface;
264 seat->popup_grab.pointer.interface =
265 &weston_desktop_seat_pointer_popup_grab_interface;
266 seat->popup_grab.touch.interface =
267 &weston_desktop_seat_touch_popup_grab_interface;
268 wl_list_init(&seat->popup_grab.surfaces);
269
270 return seat;
271}
272
273struct weston_desktop_surface *
274weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat *seat)
275{
Alexandros Frantzis8480d132018-02-15 13:07:09 +0200276 if (seat == NULL || wl_list_empty(&seat->popup_grab.surfaces))
Quentin Glidic248dd102016-08-12 10:41:34 +0200277 return NULL;
278
279 struct wl_list *grab_link = seat->popup_grab.surfaces.next;
280
281 return weston_desktop_surface_from_grab_link(grab_link);
282}
283
284bool
285weston_desktop_seat_popup_grab_start(struct weston_desktop_seat *seat,
286 struct wl_client *client, uint32_t serial)
287{
Alexandros Frantzis8480d132018-02-15 13:07:09 +0200288 assert(seat == NULL || seat->popup_grab.client == NULL ||
289 seat->popup_grab.client == client);
Quentin Glidic248dd102016-08-12 10:41:34 +0200290
Alexandros Frantzis8480d132018-02-15 13:07:09 +0200291 struct weston_seat *wseat = seat != NULL ? seat->seat : NULL;
292 /* weston_seat_get_* functions can properly handle a NULL wseat */
293 struct weston_keyboard *keyboard = weston_seat_get_keyboard(wseat);
294 struct weston_pointer *pointer = weston_seat_get_pointer(wseat);
295 struct weston_touch *touch = weston_seat_get_touch(wseat);
Quentin Glidic248dd102016-08-12 10:41:34 +0200296
297 if ((keyboard == NULL || keyboard->grab_serial != serial) &&
298 (pointer == NULL || pointer->grab_serial != serial) &&
299 (touch == NULL || touch->grab_serial != serial)) {
300 return false;
301 }
302
303 if (keyboard != NULL &&
304 keyboard->grab->interface != &weston_desktop_seat_keyboard_popup_grab_interface)
305 weston_keyboard_start_grab(keyboard, &seat->popup_grab.keyboard);
306
307 if (pointer != NULL &&
308 pointer->grab->interface != &weston_desktop_seat_pointer_popup_grab_interface)
309 weston_pointer_start_grab(pointer, &seat->popup_grab.pointer);
310
311 if (touch != NULL &&
312 touch->grab->interface != &weston_desktop_seat_touch_popup_grab_interface)
313 weston_touch_start_grab(touch, &seat->popup_grab.touch);
314
315 seat->popup_grab.initial_up =
316 (pointer == NULL || pointer->button_count == 0);
317 seat->popup_grab.client = client;
318
319 return true;
320}
321
322static void
323weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat)
324{
325 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat->seat);
326 struct weston_pointer *pointer = weston_seat_get_pointer(seat->seat);
327 struct weston_touch *touch = weston_seat_get_touch(seat->seat);
328
329 while (!wl_list_empty(&seat->popup_grab.surfaces)) {
330 struct wl_list *link = seat->popup_grab.surfaces.prev;
331 struct weston_desktop_surface *surface =
332 weston_desktop_surface_from_grab_link(link);
333
334 wl_list_remove(link);
335 wl_list_init(link);
336 weston_desktop_surface_popup_dismiss(surface);
337 }
338
339 if (keyboard != NULL &&
340 keyboard->grab->interface == &weston_desktop_seat_keyboard_popup_grab_interface)
341 weston_keyboard_end_grab(keyboard);
342
343 if (pointer != NULL &&
344 pointer->grab->interface == &weston_desktop_seat_pointer_popup_grab_interface)
345 weston_pointer_end_grab(pointer);
346
347 if (touch != NULL &&
348 touch->grab->interface == &weston_desktop_seat_touch_popup_grab_interface)
349 weston_touch_end_grab(touch);
350
351 seat->popup_grab.client = NULL;
352}
353
354void
355weston_desktop_seat_popup_grab_add_surface(struct weston_desktop_seat *seat,
356 struct wl_list *link)
357{
358 assert(seat->popup_grab.client != NULL);
359
360 wl_list_insert(&seat->popup_grab.surfaces, link);
361}
362
363void
364weston_desktop_seat_popup_grab_remove_surface(struct weston_desktop_seat *seat,
365 struct wl_list *link)
366{
367 assert(seat->popup_grab.client != NULL);
368
369 wl_list_remove(link);
370 wl_list_init(link);
371 if (wl_list_empty(&seat->popup_grab.surfaces))
372 weston_desktop_seat_popup_grab_end(seat);
373}
374
375WL_EXPORT void
376weston_seat_break_desktop_grabs(struct weston_seat *wseat)
377{
378 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
379
380 weston_desktop_seat_popup_grab_end(seat);
381}