1 /*
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
33 #include <libweston/libweston.h>
34 #include <libweston/zalloc.h>
35
36 #include <libweston-desktop/libweston-desktop.h>
37 #include "internal.h"
38 #include "shared/timespec-util.h"
39
40 struct 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
53 static void weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat);
54
55 static void
weston_desktop_seat_popup_grab_keyboard_key(struct weston_keyboard_grab * grab,const struct timespec * time,uint32_t key,enum wl_keyboard_key_state state)56 weston_desktop_seat_popup_grab_keyboard_key(struct weston_keyboard_grab *grab,
57 const struct timespec *time,
58 uint32_t key,
59 enum wl_keyboard_key_state state)
60 {
61 weston_keyboard_send_key(grab->keyboard, time, key, state);
62 }
63
64 static void
weston_desktop_seat_popup_grab_keyboard_modifiers(struct weston_keyboard_grab * grab,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)65 weston_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
76 static void
weston_desktop_seat_popup_grab_keyboard_cancel(struct weston_keyboard_grab * grab)77 weston_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
85 static 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
91 static void
weston_desktop_seat_popup_grab_pointer_focus(struct weston_pointer_grab * grab)92 weston_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
111 static void
weston_desktop_seat_popup_grab_pointer_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)112 weston_desktop_seat_popup_grab_pointer_motion(struct weston_pointer_grab *grab,
113 const struct timespec *time,
114 struct weston_pointer_motion_event *event)
115 {
116 weston_pointer_send_motion(grab->pointer, time, event);
117 }
118
119 static void
weston_desktop_seat_popup_grab_pointer_button(struct weston_pointer_grab * grab,const struct timespec * time,uint32_t button,enum wl_pointer_button_state state)120 weston_desktop_seat_popup_grab_pointer_button(struct weston_pointer_grab *grab,
121 const struct timespec *time,
122 uint32_t button,
123 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 &&
136 (initial_up ||
137 (timespec_sub_to_msec(time, &grab->pointer->grab_time) > 500)))
138 weston_desktop_seat_popup_grab_end(seat);
139 }
140
141 static void
weston_desktop_seat_popup_grab_pointer_axis(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_axis_event * event)142 weston_desktop_seat_popup_grab_pointer_axis(struct weston_pointer_grab *grab,
143 const struct timespec *time,
144 struct weston_pointer_axis_event *event)
145 {
146 weston_pointer_send_axis(grab->pointer, time, event);
147 }
148
149 static void
weston_desktop_seat_popup_grab_pointer_axis_source(struct weston_pointer_grab * grab,uint32_t source)150 weston_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
156 static void
weston_desktop_seat_popup_grab_pointer_frame(struct weston_pointer_grab * grab)157 weston_desktop_seat_popup_grab_pointer_frame(struct weston_pointer_grab *grab)
158 {
159 weston_pointer_send_frame(grab->pointer);
160 }
161
162 static void
weston_desktop_seat_popup_grab_pointer_cancel(struct weston_pointer_grab * grab)163 weston_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
171 static 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
181 static void
weston_desktop_seat_popup_grab_touch_down(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t sx,wl_fixed_t sy)182 weston_desktop_seat_popup_grab_touch_down(struct weston_touch_grab *grab,
183 const struct timespec *time,
184 int touch_id,
185 wl_fixed_t sx, wl_fixed_t sy)
186 {
187 weston_touch_send_down(grab->touch, time, touch_id, sx, sy);
188 }
189
190 static void
weston_desktop_seat_popup_grab_touch_up(struct weston_touch_grab * grab,const struct timespec * time,int touch_id)191 weston_desktop_seat_popup_grab_touch_up(struct weston_touch_grab *grab,
192 const struct timespec *time,
193 int touch_id)
194 {
195 weston_touch_send_up(grab->touch, time, touch_id);
196 }
197
198 static void
weston_desktop_seat_popup_grab_touch_motion(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t sx,wl_fixed_t sy)199 weston_desktop_seat_popup_grab_touch_motion(struct weston_touch_grab *grab,
200 const struct timespec *time,
201 int touch_id,
202 wl_fixed_t sx, wl_fixed_t sy)
203 {
204 weston_touch_send_motion(grab->touch, time, touch_id, sx, sy);
205 }
206
207 static void
weston_desktop_seat_popup_grab_touch_frame(struct weston_touch_grab * grab)208 weston_desktop_seat_popup_grab_touch_frame(struct weston_touch_grab *grab)
209 {
210 weston_touch_send_frame(grab->touch);
211 }
212
213 static void
weston_desktop_seat_popup_grab_touch_cancel(struct weston_touch_grab * grab)214 weston_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
222 static 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
230 static void
weston_desktop_seat_destroy(struct wl_listener * listener,void * data)231 weston_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
239 struct weston_desktop_seat *
weston_desktop_seat_from_seat(struct weston_seat * wseat)240 weston_desktop_seat_from_seat(struct weston_seat *wseat)
241 {
242 struct wl_listener *listener;
243 struct weston_desktop_seat *seat;
244
245 if (wseat == NULL)
246 return NULL;
247
248 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
273 struct weston_desktop_surface *
weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat * seat)274 weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat *seat)
275 {
276 if (seat == NULL || wl_list_empty(&seat->popup_grab.surfaces))
277 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
284 bool
weston_desktop_seat_popup_grab_start(struct weston_desktop_seat * seat,struct wl_client * client,uint32_t serial)285 weston_desktop_seat_popup_grab_start(struct weston_desktop_seat *seat,
286 struct wl_client *client, uint32_t serial)
287 {
288 assert(seat == NULL || seat->popup_grab.client == NULL ||
289 seat->popup_grab.client == client);
290
291 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);
296
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
322 static void
weston_desktop_seat_popup_grab_end(struct weston_desktop_seat * seat)323 weston_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
354 void
weston_desktop_seat_popup_grab_add_surface(struct weston_desktop_seat * seat,struct wl_list * link)355 weston_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
363 void
weston_desktop_seat_popup_grab_remove_surface(struct weston_desktop_seat * seat,struct wl_list * link)364 weston_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
375 WL_EXPORT void
weston_seat_break_desktop_grabs(struct weston_seat * wseat)376 weston_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 }
382