Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 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 "compositor.h" |
| 34 | #include "zalloc.h" |
| 35 | |
| 36 | #include "libweston-desktop.h" |
| 37 | #include "internal.h" |
| 38 | |
| 39 | struct weston_desktop_seat { |
| 40 | struct wl_listener seat_destroy_listener; |
| 41 | struct weston_seat *seat; |
| 42 | struct { |
| 43 | struct weston_keyboard_grab keyboard; |
| 44 | struct weston_pointer_grab pointer; |
| 45 | struct weston_touch_grab touch; |
| 46 | bool initial_up; |
| 47 | struct wl_client *client; |
| 48 | struct wl_list surfaces; |
| 49 | } popup_grab; |
| 50 | }; |
| 51 | |
| 52 | static void weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat); |
| 53 | |
| 54 | static void |
| 55 | weston_desktop_seat_popup_grab_keyboard_key(struct weston_keyboard_grab *grab, |
| 56 | uint32_t time, uint32_t key, |
| 57 | enum wl_keyboard_key_state state) |
| 58 | { |
| 59 | weston_keyboard_send_key(grab->keyboard, time, key, state); |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | weston_desktop_seat_popup_grab_keyboard_modifiers(struct weston_keyboard_grab *grab, |
| 64 | uint32_t serial, |
| 65 | uint32_t mods_depressed, |
| 66 | uint32_t mods_latched, |
| 67 | uint32_t mods_locked, |
| 68 | uint32_t group) |
| 69 | { |
| 70 | weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed, |
| 71 | mods_latched, mods_locked, group); |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | weston_desktop_seat_popup_grab_keyboard_cancel(struct weston_keyboard_grab *grab) |
| 76 | { |
| 77 | struct weston_desktop_seat *seat = |
| 78 | wl_container_of(grab, seat, popup_grab.keyboard); |
| 79 | |
| 80 | weston_desktop_seat_popup_grab_end(seat); |
| 81 | } |
| 82 | |
| 83 | static const struct weston_keyboard_grab_interface weston_desktop_seat_keyboard_popup_grab_interface = { |
| 84 | .key = weston_desktop_seat_popup_grab_keyboard_key, |
| 85 | .modifiers = weston_desktop_seat_popup_grab_keyboard_modifiers, |
| 86 | .cancel = weston_desktop_seat_popup_grab_keyboard_cancel, |
| 87 | }; |
| 88 | |
| 89 | static void |
| 90 | weston_desktop_seat_popup_grab_pointer_focus(struct weston_pointer_grab *grab) |
| 91 | { |
| 92 | struct weston_desktop_seat *seat = |
| 93 | wl_container_of(grab, seat, popup_grab.pointer); |
| 94 | struct weston_pointer *pointer = grab->pointer; |
| 95 | struct weston_view *view; |
| 96 | wl_fixed_t sx, sy; |
| 97 | |
| 98 | view = weston_compositor_pick_view(pointer->seat->compositor, |
| 99 | pointer->x, pointer->y, &sx, &sy); |
| 100 | |
| 101 | if (view != NULL && |
| 102 | view->surface->resource != NULL && |
| 103 | wl_resource_get_client(view->surface->resource) == seat->popup_grab.client) |
| 104 | weston_pointer_set_focus(pointer, view, sx, sy); |
| 105 | else |
| 106 | weston_pointer_clear_focus(pointer); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | weston_desktop_seat_popup_grab_pointer_motion(struct weston_pointer_grab *grab, |
| 111 | uint32_t time, |
| 112 | struct weston_pointer_motion_event *event) |
| 113 | { |
| 114 | weston_pointer_send_motion(grab->pointer, time, event); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | weston_desktop_seat_popup_grab_pointer_button(struct weston_pointer_grab *grab, |
| 119 | uint32_t time, uint32_t button, |
| 120 | enum wl_pointer_button_state state) |
| 121 | { |
| 122 | struct weston_desktop_seat *seat = |
| 123 | wl_container_of(grab, seat, popup_grab.pointer); |
| 124 | struct weston_pointer *pointer = grab->pointer; |
| 125 | bool initial_up = seat->popup_grab.initial_up; |
| 126 | |
| 127 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 128 | seat->popup_grab.initial_up = true; |
| 129 | |
| 130 | if (weston_pointer_has_focus_resource(pointer)) |
| 131 | weston_pointer_send_button(pointer, time, button, state); |
| 132 | else if (state == WL_POINTER_BUTTON_STATE_RELEASED && |
| 133 | (initial_up || (time - grab->pointer->grab_time) > 500)) |
| 134 | weston_desktop_seat_popup_grab_end(seat); |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | weston_desktop_seat_popup_grab_pointer_axis(struct weston_pointer_grab *grab, |
| 139 | uint32_t time, |
| 140 | struct weston_pointer_axis_event *event) |
| 141 | { |
| 142 | weston_pointer_send_axis(grab->pointer, time, event); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | weston_desktop_seat_popup_grab_pointer_axis_source(struct weston_pointer_grab *grab, |
| 147 | uint32_t source) |
| 148 | { |
| 149 | weston_pointer_send_axis_source(grab->pointer, source); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | weston_desktop_seat_popup_grab_pointer_frame(struct weston_pointer_grab *grab) |
| 154 | { |
| 155 | weston_pointer_send_frame(grab->pointer); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | weston_desktop_seat_popup_grab_pointer_cancel(struct weston_pointer_grab *grab) |
| 160 | { |
| 161 | struct weston_desktop_seat *seat = |
| 162 | wl_container_of(grab, seat, popup_grab.pointer); |
| 163 | |
| 164 | weston_desktop_seat_popup_grab_end(seat); |
| 165 | } |
| 166 | |
| 167 | static const struct weston_pointer_grab_interface weston_desktop_seat_pointer_popup_grab_interface = { |
| 168 | .focus = weston_desktop_seat_popup_grab_pointer_focus, |
| 169 | .motion = weston_desktop_seat_popup_grab_pointer_motion, |
| 170 | .button = weston_desktop_seat_popup_grab_pointer_button, |
| 171 | .axis = weston_desktop_seat_popup_grab_pointer_axis, |
| 172 | .axis_source = weston_desktop_seat_popup_grab_pointer_axis_source, |
| 173 | .frame = weston_desktop_seat_popup_grab_pointer_frame, |
| 174 | .cancel = weston_desktop_seat_popup_grab_pointer_cancel, |
| 175 | }; |
| 176 | |
| 177 | static void |
| 178 | weston_desktop_seat_popup_grab_touch_down(struct weston_touch_grab *grab, |
| 179 | uint32_t time, int touch_id, |
| 180 | wl_fixed_t sx, wl_fixed_t sy) |
| 181 | { |
| 182 | weston_touch_send_down(grab->touch, time, touch_id, sx, sy); |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | weston_desktop_seat_popup_grab_touch_up(struct weston_touch_grab *grab, |
| 187 | uint32_t time, int touch_id) |
| 188 | { |
| 189 | weston_touch_send_up(grab->touch, time, touch_id); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | weston_desktop_seat_popup_grab_touch_motion(struct weston_touch_grab *grab, |
| 194 | uint32_t time, int touch_id, |
| 195 | wl_fixed_t sx, wl_fixed_t sy) |
| 196 | { |
| 197 | weston_touch_send_motion(grab->touch, time, touch_id, sx, sy); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | weston_desktop_seat_popup_grab_touch_frame(struct weston_touch_grab *grab) |
| 202 | { |
| 203 | weston_touch_send_frame(grab->touch); |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | weston_desktop_seat_popup_grab_touch_cancel(struct weston_touch_grab *grab) |
| 208 | { |
| 209 | struct weston_desktop_seat *seat = |
| 210 | wl_container_of(grab, seat, popup_grab.touch); |
| 211 | |
| 212 | weston_desktop_seat_popup_grab_end(seat); |
| 213 | } |
| 214 | |
| 215 | static const struct weston_touch_grab_interface weston_desktop_seat_touch_popup_grab_interface = { |
| 216 | .down = weston_desktop_seat_popup_grab_touch_down, |
| 217 | .up = weston_desktop_seat_popup_grab_touch_up, |
| 218 | .motion = weston_desktop_seat_popup_grab_touch_motion, |
| 219 | .frame = weston_desktop_seat_popup_grab_touch_frame, |
| 220 | .cancel = weston_desktop_seat_popup_grab_touch_cancel, |
| 221 | }; |
| 222 | |
| 223 | static void |
| 224 | weston_desktop_seat_destroy(struct wl_listener *listener, void *data) |
| 225 | { |
| 226 | struct weston_desktop_seat *seat = |
| 227 | wl_container_of(listener, seat, seat_destroy_listener); |
| 228 | |
| 229 | free(seat); |
| 230 | } |
| 231 | |
| 232 | struct weston_desktop_seat * |
| 233 | weston_desktop_seat_from_seat(struct weston_seat *wseat) |
| 234 | { |
| 235 | struct wl_listener *listener; |
| 236 | struct weston_desktop_seat *seat; |
| 237 | |
| 238 | listener = wl_signal_get(&wseat->destroy_signal, |
| 239 | weston_desktop_seat_destroy); |
| 240 | if (listener != NULL) |
| 241 | return wl_container_of(listener, seat, seat_destroy_listener); |
| 242 | |
| 243 | seat = zalloc(sizeof(struct weston_desktop_seat)); |
| 244 | if (seat == NULL) |
| 245 | return NULL; |
| 246 | |
| 247 | seat->seat = wseat; |
| 248 | |
| 249 | seat->seat_destroy_listener.notify = weston_desktop_seat_destroy; |
| 250 | wl_signal_add(&wseat->destroy_signal, &seat->seat_destroy_listener); |
| 251 | |
| 252 | seat->popup_grab.keyboard.interface = |
| 253 | &weston_desktop_seat_keyboard_popup_grab_interface; |
| 254 | seat->popup_grab.pointer.interface = |
| 255 | &weston_desktop_seat_pointer_popup_grab_interface; |
| 256 | seat->popup_grab.touch.interface = |
| 257 | &weston_desktop_seat_touch_popup_grab_interface; |
| 258 | wl_list_init(&seat->popup_grab.surfaces); |
| 259 | |
| 260 | return seat; |
| 261 | } |
| 262 | |
| 263 | struct weston_desktop_surface * |
| 264 | weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat *seat) |
| 265 | { |
| 266 | if (wl_list_empty(&seat->popup_grab.surfaces)) |
| 267 | return NULL; |
| 268 | |
| 269 | struct wl_list *grab_link = seat->popup_grab.surfaces.next; |
| 270 | |
| 271 | return weston_desktop_surface_from_grab_link(grab_link); |
| 272 | } |
| 273 | |
| 274 | bool |
| 275 | weston_desktop_seat_popup_grab_start(struct weston_desktop_seat *seat, |
| 276 | struct wl_client *client, uint32_t serial) |
| 277 | { |
| 278 | assert(seat->popup_grab.client == NULL || seat->popup_grab.client == client); |
| 279 | |
| 280 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat->seat); |
| 281 | struct weston_pointer *pointer = weston_seat_get_pointer(seat->seat); |
| 282 | struct weston_touch *touch = weston_seat_get_touch(seat->seat); |
| 283 | |
| 284 | if ((keyboard == NULL || keyboard->grab_serial != serial) && |
| 285 | (pointer == NULL || pointer->grab_serial != serial) && |
| 286 | (touch == NULL || touch->grab_serial != serial)) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if (keyboard != NULL && |
| 291 | keyboard->grab->interface != &weston_desktop_seat_keyboard_popup_grab_interface) |
| 292 | weston_keyboard_start_grab(keyboard, &seat->popup_grab.keyboard); |
| 293 | |
| 294 | if (pointer != NULL && |
| 295 | pointer->grab->interface != &weston_desktop_seat_pointer_popup_grab_interface) |
| 296 | weston_pointer_start_grab(pointer, &seat->popup_grab.pointer); |
| 297 | |
| 298 | if (touch != NULL && |
| 299 | touch->grab->interface != &weston_desktop_seat_touch_popup_grab_interface) |
| 300 | weston_touch_start_grab(touch, &seat->popup_grab.touch); |
| 301 | |
| 302 | seat->popup_grab.initial_up = |
| 303 | (pointer == NULL || pointer->button_count == 0); |
| 304 | seat->popup_grab.client = client; |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat) |
| 311 | { |
| 312 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat->seat); |
| 313 | struct weston_pointer *pointer = weston_seat_get_pointer(seat->seat); |
| 314 | struct weston_touch *touch = weston_seat_get_touch(seat->seat); |
| 315 | |
| 316 | while (!wl_list_empty(&seat->popup_grab.surfaces)) { |
| 317 | struct wl_list *link = seat->popup_grab.surfaces.prev; |
| 318 | struct weston_desktop_surface *surface = |
| 319 | weston_desktop_surface_from_grab_link(link); |
| 320 | |
| 321 | wl_list_remove(link); |
| 322 | wl_list_init(link); |
| 323 | weston_desktop_surface_popup_dismiss(surface); |
| 324 | } |
| 325 | |
| 326 | if (keyboard != NULL && |
| 327 | keyboard->grab->interface == &weston_desktop_seat_keyboard_popup_grab_interface) |
| 328 | weston_keyboard_end_grab(keyboard); |
| 329 | |
| 330 | if (pointer != NULL && |
| 331 | pointer->grab->interface == &weston_desktop_seat_pointer_popup_grab_interface) |
| 332 | weston_pointer_end_grab(pointer); |
| 333 | |
| 334 | if (touch != NULL && |
| 335 | touch->grab->interface == &weston_desktop_seat_touch_popup_grab_interface) |
| 336 | weston_touch_end_grab(touch); |
| 337 | |
| 338 | seat->popup_grab.client = NULL; |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | weston_desktop_seat_popup_grab_add_surface(struct weston_desktop_seat *seat, |
| 343 | struct wl_list *link) |
| 344 | { |
| 345 | assert(seat->popup_grab.client != NULL); |
| 346 | |
| 347 | wl_list_insert(&seat->popup_grab.surfaces, link); |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | weston_desktop_seat_popup_grab_remove_surface(struct weston_desktop_seat *seat, |
| 352 | struct wl_list *link) |
| 353 | { |
| 354 | assert(seat->popup_grab.client != NULL); |
| 355 | |
| 356 | wl_list_remove(link); |
| 357 | wl_list_init(link); |
| 358 | if (wl_list_empty(&seat->popup_grab.surfaces)) |
| 359 | weston_desktop_seat_popup_grab_end(seat); |
| 360 | } |
| 361 | |
| 362 | WL_EXPORT void |
| 363 | weston_seat_break_desktop_grabs(struct weston_seat *wseat) |
| 364 | { |
| 365 | struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat); |
| 366 | |
| 367 | weston_desktop_seat_popup_grab_end(seat); |
| 368 | } |