Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 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> |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #include <assert.h> |
| 28 | #include <unistd.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 29 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 30 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 31 | #include "compositor.h" |
| 32 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 33 | static void |
| 34 | empty_region(pixman_region32_t *region) |
| 35 | { |
| 36 | pixman_region32_fini(region); |
| 37 | pixman_region32_init(region); |
| 38 | } |
| 39 | |
| 40 | static void unbind_resource(struct wl_resource *resource) |
| 41 | { |
| 42 | wl_list_remove(&resource->link); |
| 43 | free(resource); |
| 44 | } |
| 45 | |
| 46 | void |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame^] | 47 | weston_seat_repick(struct weston_seat *seat) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 48 | { |
| 49 | const struct wl_pointer_grab_interface *interface; |
| 50 | struct weston_surface *surface, *focus; |
| 51 | struct wl_pointer *pointer = seat->seat.pointer; |
| 52 | |
| 53 | if (!pointer) |
| 54 | return; |
| 55 | |
| 56 | surface = weston_compositor_pick_surface(seat->compositor, |
| 57 | pointer->x, |
| 58 | pointer->y, |
| 59 | &pointer->current_x, |
| 60 | &pointer->current_y); |
| 61 | |
| 62 | if (&surface->surface != pointer->current) { |
| 63 | interface = pointer->grab->interface; |
| 64 | wl_pointer_set_current(pointer, &surface->surface); |
| 65 | interface->focus(pointer->grab, &surface->surface, |
| 66 | pointer->current_x, |
| 67 | pointer->current_y); |
| 68 | } |
| 69 | |
| 70 | focus = (struct weston_surface *) pointer->grab->focus; |
| 71 | if (focus) |
| 72 | weston_surface_from_global_fixed(focus, |
| 73 | pointer->x, |
| 74 | pointer->y, |
| 75 | &pointer->grab->x, |
| 76 | &pointer->grab->y); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | weston_compositor_idle_inhibit(struct weston_compositor *compositor) |
| 81 | { |
| 82 | weston_compositor_wake(compositor); |
| 83 | compositor->idle_inhibit++; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | weston_compositor_idle_release(struct weston_compositor *compositor) |
| 88 | { |
| 89 | compositor->idle_inhibit--; |
| 90 | weston_compositor_wake(compositor); |
| 91 | } |
| 92 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 93 | static void |
| 94 | lose_pointer_focus(struct wl_listener *listener, void *data) |
| 95 | { |
| 96 | struct wl_pointer *pointer = |
| 97 | container_of(listener, struct wl_pointer, focus_listener); |
| 98 | |
| 99 | pointer->focus_resource = NULL; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | lose_keyboard_focus(struct wl_listener *listener, void *data) |
| 104 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 105 | struct weston_keyboard *keyboard = |
| 106 | container_of(listener, struct weston_keyboard, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 107 | |
| 108 | keyboard->focus_resource = NULL; |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | lose_touch_focus(struct wl_listener *listener, void *data) |
| 113 | { |
| 114 | struct wl_touch *touch = |
| 115 | container_of(listener, struct wl_touch, focus_listener); |
| 116 | |
| 117 | touch->focus_resource = NULL; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | default_grab_focus(struct wl_pointer_grab *grab, |
| 122 | struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) |
| 123 | { |
| 124 | struct wl_pointer *pointer = grab->pointer; |
| 125 | |
| 126 | if (pointer->button_count > 0) |
| 127 | return; |
| 128 | |
| 129 | wl_pointer_set_focus(pointer, surface, x, y); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | default_grab_motion(struct wl_pointer_grab *grab, |
| 134 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 135 | { |
| 136 | struct wl_resource *resource; |
| 137 | |
| 138 | resource = grab->pointer->focus_resource; |
| 139 | if (resource) |
| 140 | wl_pointer_send_motion(resource, time, x, y); |
| 141 | } |
| 142 | |
| 143 | static void |
| 144 | default_grab_button(struct wl_pointer_grab *grab, |
| 145 | uint32_t time, uint32_t button, uint32_t state_w) |
| 146 | { |
| 147 | struct wl_pointer *pointer = grab->pointer; |
| 148 | struct wl_resource *resource; |
| 149 | uint32_t serial; |
| 150 | enum wl_pointer_button_state state = state_w; |
| 151 | struct wl_display *display; |
| 152 | |
| 153 | resource = pointer->focus_resource; |
| 154 | if (resource) { |
| 155 | display = wl_client_get_display(resource->client); |
| 156 | serial = wl_display_next_serial(display); |
| 157 | wl_pointer_send_button(resource, serial, time, button, state_w); |
| 158 | } |
| 159 | |
| 160 | if (pointer->button_count == 0 && |
| 161 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 162 | wl_pointer_set_focus(pointer, pointer->current, |
| 163 | pointer->current_x, pointer->current_y); |
| 164 | } |
| 165 | |
| 166 | static const struct wl_pointer_grab_interface |
| 167 | default_pointer_grab_interface = { |
| 168 | default_grab_focus, |
| 169 | default_grab_motion, |
| 170 | default_grab_button |
| 171 | }; |
| 172 | |
| 173 | static void default_grab_touch_down(struct wl_touch_grab *grab, |
| 174 | uint32_t time, |
| 175 | int touch_id, |
| 176 | wl_fixed_t sx, |
| 177 | wl_fixed_t sy) |
| 178 | { |
| 179 | struct wl_touch *touch = grab->touch; |
| 180 | struct wl_display *display; |
| 181 | uint32_t serial; |
| 182 | |
| 183 | if (touch->focus_resource && touch->focus) { |
| 184 | display = wl_client_get_display(touch->focus_resource->client); |
| 185 | serial = wl_display_next_serial(display); |
| 186 | wl_touch_send_down(touch->focus_resource, serial, time, |
| 187 | &touch->focus->resource, touch_id, sx, sy); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void default_grab_touch_up(struct wl_touch_grab *grab, |
| 192 | uint32_t time, |
| 193 | int touch_id) |
| 194 | { |
| 195 | struct wl_touch *touch = grab->touch; |
| 196 | struct wl_display *display; |
| 197 | uint32_t serial; |
| 198 | |
| 199 | if (touch->focus_resource) { |
| 200 | display = wl_client_get_display(touch->focus_resource->client); |
| 201 | serial = wl_display_next_serial(display); |
| 202 | wl_touch_send_up(touch->focus_resource, serial, time, touch_id); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void default_grab_touch_motion(struct wl_touch_grab *grab, |
| 207 | uint32_t time, |
| 208 | int touch_id, |
| 209 | wl_fixed_t sx, |
| 210 | wl_fixed_t sy) |
| 211 | { |
| 212 | struct wl_touch *touch = grab->touch; |
| 213 | |
| 214 | if (touch->focus_resource) { |
| 215 | wl_touch_send_motion(touch->focus_resource, time, |
| 216 | touch_id, sx, sy); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static const struct wl_touch_grab_interface default_touch_grab_interface = { |
| 221 | default_grab_touch_down, |
| 222 | default_grab_touch_up, |
| 223 | default_grab_touch_motion |
| 224 | }; |
| 225 | |
| 226 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 227 | default_grab_key(struct weston_keyboard_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 228 | uint32_t time, uint32_t key, uint32_t state) |
| 229 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 230 | struct weston_keyboard *keyboard = grab->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 231 | struct wl_resource *resource; |
| 232 | struct wl_display *display; |
| 233 | uint32_t serial; |
| 234 | |
| 235 | resource = keyboard->focus_resource; |
| 236 | if (resource) { |
| 237 | display = wl_client_get_display(resource->client); |
| 238 | serial = wl_display_next_serial(display); |
| 239 | wl_keyboard_send_key(resource, serial, time, key, state); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static struct wl_resource * |
| 244 | find_resource_for_surface(struct wl_list *list, struct wl_surface *surface) |
| 245 | { |
| 246 | struct wl_resource *r; |
| 247 | |
| 248 | if (!surface) |
| 249 | return NULL; |
| 250 | |
| 251 | wl_list_for_each(r, list, link) { |
| 252 | if (r->client == surface->resource.client) |
| 253 | return r; |
| 254 | } |
| 255 | |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 260 | default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 261 | uint32_t mods_depressed, uint32_t mods_latched, |
| 262 | uint32_t mods_locked, uint32_t group) |
| 263 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 264 | struct weston_keyboard *keyboard = grab->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 265 | struct wl_pointer *pointer = keyboard->seat->pointer; |
| 266 | struct wl_resource *resource, *pr; |
| 267 | |
| 268 | resource = keyboard->focus_resource; |
| 269 | if (!resource) |
| 270 | return; |
| 271 | |
| 272 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 273 | mods_latched, mods_locked, group); |
| 274 | |
| 275 | if (pointer && pointer->focus && pointer->focus != keyboard->focus) { |
| 276 | pr = find_resource_for_surface(&keyboard->resource_list, |
| 277 | pointer->focus); |
| 278 | if (pr) { |
| 279 | wl_keyboard_send_modifiers(pr, |
| 280 | serial, |
| 281 | keyboard->modifiers.mods_depressed, |
| 282 | keyboard->modifiers.mods_latched, |
| 283 | keyboard->modifiers.mods_locked, |
| 284 | keyboard->modifiers.group); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 289 | static const struct weston_keyboard_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 290 | default_keyboard_grab_interface = { |
| 291 | default_grab_key, |
| 292 | default_grab_modifiers, |
| 293 | }; |
| 294 | |
| 295 | WL_EXPORT void |
| 296 | wl_pointer_init(struct wl_pointer *pointer) |
| 297 | { |
| 298 | memset(pointer, 0, sizeof *pointer); |
| 299 | wl_list_init(&pointer->resource_list); |
| 300 | pointer->focus_listener.notify = lose_pointer_focus; |
| 301 | pointer->default_grab.interface = &default_pointer_grab_interface; |
| 302 | pointer->default_grab.pointer = pointer; |
| 303 | pointer->grab = &pointer->default_grab; |
| 304 | wl_signal_init(&pointer->focus_signal); |
| 305 | |
| 306 | /* FIXME: Pick better co-ords. */ |
| 307 | pointer->x = wl_fixed_from_int(100); |
| 308 | pointer->y = wl_fixed_from_int(100); |
| 309 | } |
| 310 | |
| 311 | WL_EXPORT void |
| 312 | wl_pointer_release(struct wl_pointer *pointer) |
| 313 | { |
| 314 | /* XXX: What about pointer->resource_list? */ |
| 315 | if (pointer->focus_resource) |
| 316 | wl_list_remove(&pointer->focus_listener.link); |
| 317 | } |
| 318 | |
| 319 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 320 | weston_keyboard_init(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 321 | { |
| 322 | memset(keyboard, 0, sizeof *keyboard); |
| 323 | wl_list_init(&keyboard->resource_list); |
| 324 | wl_array_init(&keyboard->keys); |
| 325 | keyboard->focus_listener.notify = lose_keyboard_focus; |
| 326 | keyboard->default_grab.interface = &default_keyboard_grab_interface; |
| 327 | keyboard->default_grab.keyboard = keyboard; |
| 328 | keyboard->grab = &keyboard->default_grab; |
| 329 | wl_signal_init(&keyboard->focus_signal); |
| 330 | } |
| 331 | |
| 332 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 333 | weston_keyboard_release(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 334 | { |
| 335 | /* XXX: What about keyboard->resource_list? */ |
| 336 | if (keyboard->focus_resource) |
| 337 | wl_list_remove(&keyboard->focus_listener.link); |
| 338 | wl_array_release(&keyboard->keys); |
| 339 | } |
| 340 | |
| 341 | WL_EXPORT void |
| 342 | wl_touch_init(struct wl_touch *touch) |
| 343 | { |
| 344 | memset(touch, 0, sizeof *touch); |
| 345 | wl_list_init(&touch->resource_list); |
| 346 | touch->focus_listener.notify = lose_touch_focus; |
| 347 | touch->default_grab.interface = &default_touch_grab_interface; |
| 348 | touch->default_grab.touch = touch; |
| 349 | touch->grab = &touch->default_grab; |
| 350 | wl_signal_init(&touch->focus_signal); |
| 351 | } |
| 352 | |
| 353 | WL_EXPORT void |
| 354 | wl_touch_release(struct wl_touch *touch) |
| 355 | { |
| 356 | /* XXX: What about touch->resource_list? */ |
| 357 | if (touch->focus_resource) |
| 358 | wl_list_remove(&touch->focus_listener.link); |
| 359 | } |
| 360 | |
| 361 | WL_EXPORT void |
| 362 | wl_seat_init(struct wl_seat *seat) |
| 363 | { |
| 364 | memset(seat, 0, sizeof *seat); |
| 365 | |
| 366 | wl_signal_init(&seat->destroy_signal); |
| 367 | |
| 368 | seat->selection_data_source = NULL; |
| 369 | wl_list_init(&seat->base_resource_list); |
| 370 | wl_signal_init(&seat->selection_signal); |
| 371 | wl_list_init(&seat->drag_resource_list); |
| 372 | wl_signal_init(&seat->drag_icon_signal); |
| 373 | } |
| 374 | |
| 375 | WL_EXPORT void |
| 376 | wl_seat_release(struct wl_seat *seat) |
| 377 | { |
| 378 | wl_signal_emit(&seat->destroy_signal, seat); |
| 379 | |
| 380 | if (seat->pointer) |
| 381 | wl_pointer_release(seat->pointer); |
| 382 | if (seat->keyboard) |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 383 | weston_keyboard_release(seat->keyboard); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 384 | if (seat->touch) |
| 385 | wl_touch_release(seat->touch); |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | seat_send_updated_caps(struct wl_seat *seat) |
| 390 | { |
| 391 | struct wl_resource *r; |
| 392 | enum wl_seat_capability caps = 0; |
| 393 | |
| 394 | if (seat->pointer) |
| 395 | caps |= WL_SEAT_CAPABILITY_POINTER; |
| 396 | if (seat->keyboard) |
| 397 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
| 398 | if (seat->touch) |
| 399 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 400 | |
| 401 | wl_list_for_each(r, &seat->base_resource_list, link) |
| 402 | wl_seat_send_capabilities(r, caps); |
| 403 | } |
| 404 | |
| 405 | WL_EXPORT void |
| 406 | wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer) |
| 407 | { |
| 408 | if (pointer && (seat->pointer || pointer->seat)) |
| 409 | return; /* XXX: error? */ |
| 410 | if (!pointer && !seat->pointer) |
| 411 | return; |
| 412 | |
| 413 | seat->pointer = pointer; |
| 414 | if (pointer) |
| 415 | pointer->seat = seat; |
| 416 | |
| 417 | seat_send_updated_caps(seat); |
| 418 | } |
| 419 | |
| 420 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 421 | wl_seat_set_keyboard(struct wl_seat *seat, struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 422 | { |
| 423 | if (keyboard && (seat->keyboard || keyboard->seat)) |
| 424 | return; /* XXX: error? */ |
| 425 | if (!keyboard && !seat->keyboard) |
| 426 | return; |
| 427 | |
| 428 | seat->keyboard = keyboard; |
| 429 | if (keyboard) |
| 430 | keyboard->seat = seat; |
| 431 | |
| 432 | seat_send_updated_caps(seat); |
| 433 | } |
| 434 | |
| 435 | WL_EXPORT void |
| 436 | wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch) |
| 437 | { |
| 438 | if (touch && (seat->touch || touch->seat)) |
| 439 | return; /* XXX: error? */ |
| 440 | if (!touch && !seat->touch) |
| 441 | return; |
| 442 | |
| 443 | seat->touch = touch; |
| 444 | if (touch) |
| 445 | touch->seat = seat; |
| 446 | |
| 447 | seat_send_updated_caps(seat); |
| 448 | } |
| 449 | |
| 450 | WL_EXPORT void |
| 451 | wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface, |
| 452 | wl_fixed_t sx, wl_fixed_t sy) |
| 453 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 454 | struct weston_keyboard *kbd = pointer->seat->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 455 | struct wl_resource *resource, *kr; |
| 456 | struct wl_display *display; |
| 457 | uint32_t serial; |
| 458 | |
| 459 | resource = pointer->focus_resource; |
| 460 | if (resource && pointer->focus != surface) { |
| 461 | display = wl_client_get_display(resource->client); |
| 462 | serial = wl_display_next_serial(display); |
| 463 | wl_pointer_send_leave(resource, serial, |
| 464 | &pointer->focus->resource); |
| 465 | wl_list_remove(&pointer->focus_listener.link); |
| 466 | } |
| 467 | |
| 468 | resource = find_resource_for_surface(&pointer->resource_list, |
| 469 | surface); |
| 470 | if (resource && |
| 471 | (pointer->focus != surface || |
| 472 | pointer->focus_resource != resource)) { |
| 473 | display = wl_client_get_display(resource->client); |
| 474 | serial = wl_display_next_serial(display); |
| 475 | if (kbd) { |
| 476 | kr = find_resource_for_surface(&kbd->resource_list, |
| 477 | surface); |
| 478 | if (kr) { |
| 479 | wl_keyboard_send_modifiers(kr, |
| 480 | serial, |
| 481 | kbd->modifiers.mods_depressed, |
| 482 | kbd->modifiers.mods_latched, |
| 483 | kbd->modifiers.mods_locked, |
| 484 | kbd->modifiers.group); |
| 485 | } |
| 486 | } |
| 487 | wl_pointer_send_enter(resource, serial, &surface->resource, |
| 488 | sx, sy); |
| 489 | wl_signal_add(&resource->destroy_signal, |
| 490 | &pointer->focus_listener); |
| 491 | pointer->focus_serial = serial; |
| 492 | } |
| 493 | |
| 494 | pointer->focus_resource = resource; |
| 495 | pointer->focus = surface; |
| 496 | pointer->default_grab.focus = surface; |
| 497 | wl_signal_emit(&pointer->focus_signal, pointer); |
| 498 | } |
| 499 | |
| 500 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 501 | weston_keyboard_set_focus(struct weston_keyboard *keyboard, |
| 502 | struct wl_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 503 | { |
| 504 | struct wl_resource *resource; |
| 505 | struct wl_display *display; |
| 506 | uint32_t serial; |
| 507 | |
| 508 | if (keyboard->focus_resource && keyboard->focus != surface) { |
| 509 | resource = keyboard->focus_resource; |
| 510 | display = wl_client_get_display(resource->client); |
| 511 | serial = wl_display_next_serial(display); |
| 512 | wl_keyboard_send_leave(resource, serial, |
| 513 | &keyboard->focus->resource); |
| 514 | wl_list_remove(&keyboard->focus_listener.link); |
| 515 | } |
| 516 | |
| 517 | resource = find_resource_for_surface(&keyboard->resource_list, |
| 518 | surface); |
| 519 | if (resource && |
| 520 | (keyboard->focus != surface || |
| 521 | keyboard->focus_resource != resource)) { |
| 522 | display = wl_client_get_display(resource->client); |
| 523 | serial = wl_display_next_serial(display); |
| 524 | wl_keyboard_send_modifiers(resource, serial, |
| 525 | keyboard->modifiers.mods_depressed, |
| 526 | keyboard->modifiers.mods_latched, |
| 527 | keyboard->modifiers.mods_locked, |
| 528 | keyboard->modifiers.group); |
| 529 | wl_keyboard_send_enter(resource, serial, &surface->resource, |
| 530 | &keyboard->keys); |
| 531 | wl_signal_add(&resource->destroy_signal, |
| 532 | &keyboard->focus_listener); |
| 533 | keyboard->focus_serial = serial; |
| 534 | } |
| 535 | |
| 536 | keyboard->focus_resource = resource; |
| 537 | keyboard->focus = surface; |
| 538 | wl_signal_emit(&keyboard->focus_signal, keyboard); |
| 539 | } |
| 540 | |
| 541 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 542 | weston_keyboard_start_grab(struct weston_keyboard *keyboard, |
| 543 | struct weston_keyboard_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 544 | { |
| 545 | keyboard->grab = grab; |
| 546 | grab->keyboard = keyboard; |
| 547 | |
| 548 | /* XXX focus? */ |
| 549 | } |
| 550 | |
| 551 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 552 | weston_keyboard_end_grab(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 553 | { |
| 554 | keyboard->grab = &keyboard->default_grab; |
| 555 | } |
| 556 | |
| 557 | WL_EXPORT void |
| 558 | wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab) |
| 559 | { |
| 560 | const struct wl_pointer_grab_interface *interface; |
| 561 | |
| 562 | pointer->grab = grab; |
| 563 | interface = pointer->grab->interface; |
| 564 | grab->pointer = pointer; |
| 565 | |
| 566 | if (pointer->current) |
| 567 | interface->focus(pointer->grab, pointer->current, |
| 568 | pointer->current_x, pointer->current_y); |
| 569 | } |
| 570 | |
| 571 | WL_EXPORT void |
| 572 | wl_pointer_end_grab(struct wl_pointer *pointer) |
| 573 | { |
| 574 | const struct wl_pointer_grab_interface *interface; |
| 575 | |
| 576 | pointer->grab = &pointer->default_grab; |
| 577 | interface = pointer->grab->interface; |
| 578 | interface->focus(pointer->grab, pointer->current, |
| 579 | pointer->current_x, pointer->current_y); |
| 580 | } |
| 581 | |
| 582 | static void |
| 583 | current_surface_destroy(struct wl_listener *listener, void *data) |
| 584 | { |
| 585 | struct wl_pointer *pointer = |
| 586 | container_of(listener, struct wl_pointer, current_listener); |
| 587 | |
| 588 | pointer->current = NULL; |
| 589 | } |
| 590 | |
| 591 | WL_EXPORT void |
| 592 | wl_pointer_set_current(struct wl_pointer *pointer, struct wl_surface *surface) |
| 593 | { |
| 594 | if (pointer->current) |
| 595 | wl_list_remove(&pointer->current_listener.link); |
| 596 | |
| 597 | pointer->current = surface; |
| 598 | |
| 599 | if (!surface) |
| 600 | return; |
| 601 | |
| 602 | wl_signal_add(&surface->resource.destroy_signal, |
| 603 | &pointer->current_listener); |
| 604 | pointer->current_listener.notify = current_surface_destroy; |
| 605 | } |
| 606 | |
| 607 | WL_EXPORT void |
| 608 | wl_touch_start_grab(struct wl_touch *touch, struct wl_touch_grab *grab) |
| 609 | { |
| 610 | touch->grab = grab; |
| 611 | grab->touch = touch; |
| 612 | } |
| 613 | |
| 614 | WL_EXPORT void |
| 615 | wl_touch_end_grab(struct wl_touch *touch) |
| 616 | { |
| 617 | touch->grab = &touch->default_grab; |
| 618 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 619 | |
| 620 | static void |
| 621 | weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy); |
| 622 | |
| 623 | static void |
| 624 | clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy) |
| 625 | { |
| 626 | struct weston_compositor *ec = seat->compositor; |
| 627 | struct weston_output *output, *prev = NULL; |
| 628 | int x, y, old_x, old_y, valid = 0; |
| 629 | |
| 630 | x = wl_fixed_to_int(*fx); |
| 631 | y = wl_fixed_to_int(*fy); |
| 632 | old_x = wl_fixed_to_int(seat->seat.pointer->x); |
| 633 | old_y = wl_fixed_to_int(seat->seat.pointer->y); |
| 634 | |
| 635 | wl_list_for_each(output, &ec->output_list, link) { |
| 636 | if (pixman_region32_contains_point(&output->region, |
| 637 | x, y, NULL)) |
| 638 | valid = 1; |
| 639 | if (pixman_region32_contains_point(&output->region, |
| 640 | old_x, old_y, NULL)) |
| 641 | prev = output; |
| 642 | } |
| 643 | |
| 644 | if (!valid) { |
| 645 | if (x < prev->x) |
| 646 | *fx = wl_fixed_from_int(prev->x); |
| 647 | else if (x >= prev->x + prev->width) |
| 648 | *fx = wl_fixed_from_int(prev->x + |
| 649 | prev->width - 1); |
| 650 | if (y < prev->y) |
| 651 | *fy = wl_fixed_from_int(prev->y); |
| 652 | else if (y >= prev->y + prev->current->height) |
| 653 | *fy = wl_fixed_from_int(prev->y + |
| 654 | prev->height - 1); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* Takes absolute values */ |
| 659 | static void |
| 660 | move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y) |
| 661 | { |
| 662 | struct weston_compositor *ec = seat->compositor; |
| 663 | struct wl_pointer *pointer = seat->seat.pointer; |
| 664 | struct weston_output *output; |
| 665 | int32_t ix, iy; |
| 666 | |
| 667 | clip_pointer_motion(seat, &x, &y); |
| 668 | |
| 669 | weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y); |
| 670 | |
| 671 | pointer->x = x; |
| 672 | pointer->y = y; |
| 673 | |
| 674 | ix = wl_fixed_to_int(x); |
| 675 | iy = wl_fixed_to_int(y); |
| 676 | |
| 677 | wl_list_for_each(output, &ec->output_list, link) |
| 678 | if (output->zoom.active && |
| 679 | pixman_region32_contains_point(&output->region, |
| 680 | ix, iy, NULL)) |
| 681 | weston_output_update_zoom(output, ZOOM_FOCUS_POINTER); |
| 682 | |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame^] | 683 | weston_seat_repick(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 684 | |
| 685 | if (seat->sprite) { |
| 686 | weston_surface_set_position(seat->sprite, |
| 687 | ix - seat->hotspot_x, |
| 688 | iy - seat->hotspot_y); |
| 689 | weston_surface_schedule_repaint(seat->sprite); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | WL_EXPORT void |
| 694 | notify_motion(struct weston_seat *seat, |
| 695 | uint32_t time, wl_fixed_t dx, wl_fixed_t dy) |
| 696 | { |
| 697 | const struct wl_pointer_grab_interface *interface; |
| 698 | struct weston_compositor *ec = seat->compositor; |
| 699 | struct wl_pointer *pointer = seat->seat.pointer; |
| 700 | |
| 701 | weston_compositor_wake(ec); |
| 702 | |
| 703 | move_pointer(seat, pointer->x + dx, pointer->y + dy); |
| 704 | |
| 705 | interface = pointer->grab->interface; |
| 706 | interface->motion(pointer->grab, time, |
| 707 | pointer->grab->x, pointer->grab->y); |
| 708 | } |
| 709 | |
| 710 | WL_EXPORT void |
| 711 | notify_motion_absolute(struct weston_seat *seat, |
| 712 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 713 | { |
| 714 | const struct wl_pointer_grab_interface *interface; |
| 715 | struct weston_compositor *ec = seat->compositor; |
| 716 | struct wl_pointer *pointer = seat->seat.pointer; |
| 717 | |
| 718 | weston_compositor_wake(ec); |
| 719 | |
| 720 | move_pointer(seat, x, y); |
| 721 | |
| 722 | interface = pointer->grab->interface; |
| 723 | interface->motion(pointer->grab, time, |
| 724 | pointer->grab->x, pointer->grab->y); |
| 725 | } |
| 726 | |
| 727 | WL_EXPORT void |
| 728 | weston_surface_activate(struct weston_surface *surface, |
| 729 | struct weston_seat *seat) |
| 730 | { |
| 731 | struct weston_compositor *compositor = seat->compositor; |
| 732 | |
| 733 | if (seat->seat.keyboard) { |
| 734 | weston_keyboard_set_focus(seat->seat.keyboard, &surface->surface); |
| 735 | wl_data_device_set_keyboard_focus(&seat->seat); |
| 736 | } |
| 737 | |
| 738 | wl_signal_emit(&compositor->activate_signal, surface); |
| 739 | } |
| 740 | |
| 741 | WL_EXPORT void |
| 742 | notify_button(struct weston_seat *seat, uint32_t time, int32_t button, |
| 743 | enum wl_pointer_button_state state) |
| 744 | { |
| 745 | struct weston_compositor *compositor = seat->compositor; |
| 746 | struct wl_pointer *pointer = seat->seat.pointer; |
| 747 | struct weston_surface *focus = |
| 748 | (struct weston_surface *) pointer->focus; |
| 749 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 750 | |
| 751 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 752 | if (compositor->ping_handler && focus) |
| 753 | compositor->ping_handler(focus, serial); |
| 754 | weston_compositor_idle_inhibit(compositor); |
| 755 | if (pointer->button_count == 0) { |
| 756 | pointer->grab_button = button; |
| 757 | pointer->grab_time = time; |
| 758 | pointer->grab_x = pointer->x; |
| 759 | pointer->grab_y = pointer->y; |
| 760 | } |
| 761 | pointer->button_count++; |
| 762 | } else { |
| 763 | weston_compositor_idle_release(compositor); |
| 764 | pointer->button_count--; |
| 765 | } |
| 766 | |
| 767 | weston_compositor_run_button_binding(compositor, seat, time, button, |
| 768 | state); |
| 769 | |
| 770 | pointer->grab->interface->button(pointer->grab, time, button, state); |
| 771 | |
| 772 | if (pointer->button_count == 1) |
| 773 | pointer->grab_serial = |
| 774 | wl_display_get_serial(compositor->wl_display); |
| 775 | } |
| 776 | |
| 777 | WL_EXPORT void |
| 778 | notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis, |
| 779 | wl_fixed_t value) |
| 780 | { |
| 781 | struct weston_compositor *compositor = seat->compositor; |
| 782 | struct wl_pointer *pointer = seat->seat.pointer; |
| 783 | struct weston_surface *focus = |
| 784 | (struct weston_surface *) pointer->focus; |
| 785 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 786 | |
| 787 | if (compositor->ping_handler && focus) |
| 788 | compositor->ping_handler(focus, serial); |
| 789 | |
| 790 | weston_compositor_wake(compositor); |
| 791 | |
| 792 | if (!value) |
| 793 | return; |
| 794 | |
| 795 | if (weston_compositor_run_axis_binding(compositor, seat, |
| 796 | time, axis, value)) |
| 797 | return; |
| 798 | |
| 799 | if (pointer->focus_resource) |
| 800 | wl_pointer_send_axis(pointer->focus_resource, time, axis, |
| 801 | value); |
| 802 | } |
| 803 | |
| 804 | WL_EXPORT void |
| 805 | notify_modifiers(struct weston_seat *seat, uint32_t serial) |
| 806 | { |
| 807 | struct weston_keyboard *keyboard = &seat->keyboard; |
| 808 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 809 | uint32_t mods_depressed, mods_latched, mods_locked, group; |
| 810 | uint32_t mods_lookup; |
| 811 | enum weston_led leds = 0; |
| 812 | int changed = 0; |
| 813 | |
| 814 | /* Serialize and update our internal state, checking to see if it's |
| 815 | * different to the previous state. */ |
| 816 | mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state, |
| 817 | XKB_STATE_DEPRESSED); |
| 818 | mods_latched = xkb_state_serialize_mods(seat->xkb_state.state, |
| 819 | XKB_STATE_LATCHED); |
| 820 | mods_locked = xkb_state_serialize_mods(seat->xkb_state.state, |
| 821 | XKB_STATE_LOCKED); |
| 822 | group = xkb_state_serialize_group(seat->xkb_state.state, |
| 823 | XKB_STATE_EFFECTIVE); |
| 824 | |
| 825 | if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed || |
| 826 | mods_latched != seat->seat.keyboard->modifiers.mods_latched || |
| 827 | mods_locked != seat->seat.keyboard->modifiers.mods_locked || |
| 828 | group != seat->seat.keyboard->modifiers.group) |
| 829 | changed = 1; |
| 830 | |
| 831 | seat->seat.keyboard->modifiers.mods_depressed = mods_depressed; |
| 832 | seat->seat.keyboard->modifiers.mods_latched = mods_latched; |
| 833 | seat->seat.keyboard->modifiers.mods_locked = mods_locked; |
| 834 | seat->seat.keyboard->modifiers.group = group; |
| 835 | |
| 836 | /* And update the modifier_state for bindings. */ |
| 837 | mods_lookup = mods_depressed | mods_latched; |
| 838 | seat->modifier_state = 0; |
| 839 | if (mods_lookup & (1 << seat->xkb_info.ctrl_mod)) |
| 840 | seat->modifier_state |= MODIFIER_CTRL; |
| 841 | if (mods_lookup & (1 << seat->xkb_info.alt_mod)) |
| 842 | seat->modifier_state |= MODIFIER_ALT; |
| 843 | if (mods_lookup & (1 << seat->xkb_info.super_mod)) |
| 844 | seat->modifier_state |= MODIFIER_SUPER; |
| 845 | if (mods_lookup & (1 << seat->xkb_info.shift_mod)) |
| 846 | seat->modifier_state |= MODIFIER_SHIFT; |
| 847 | |
| 848 | /* Finally, notify the compositor that LEDs have changed. */ |
| 849 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 850 | seat->xkb_info.num_led)) |
| 851 | leds |= LED_NUM_LOCK; |
| 852 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 853 | seat->xkb_info.caps_led)) |
| 854 | leds |= LED_CAPS_LOCK; |
| 855 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 856 | seat->xkb_info.scroll_led)) |
| 857 | leds |= LED_SCROLL_LOCK; |
| 858 | if (leds != seat->xkb_state.leds && seat->led_update) |
| 859 | seat->led_update(seat, leds); |
| 860 | seat->xkb_state.leds = leds; |
| 861 | |
| 862 | if (changed) { |
| 863 | grab->interface->modifiers(grab, |
| 864 | serial, |
| 865 | keyboard->modifiers.mods_depressed, |
| 866 | keyboard->modifiers.mods_latched, |
| 867 | keyboard->modifiers.mods_locked, |
| 868 | keyboard->modifiers.group); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | static void |
| 873 | update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key, |
| 874 | enum wl_keyboard_key_state state) |
| 875 | { |
| 876 | enum xkb_key_direction direction; |
| 877 | |
| 878 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 879 | direction = XKB_KEY_DOWN; |
| 880 | else |
| 881 | direction = XKB_KEY_UP; |
| 882 | |
| 883 | /* Offset the keycode by 8, as the evdev XKB rules reflect X's |
| 884 | * broken keycode system, which starts at 8. */ |
| 885 | xkb_state_update_key(seat->xkb_state.state, key + 8, direction); |
| 886 | |
| 887 | notify_modifiers(seat, serial); |
| 888 | } |
| 889 | |
| 890 | WL_EXPORT void |
| 891 | notify_key(struct weston_seat *seat, uint32_t time, uint32_t key, |
| 892 | enum wl_keyboard_key_state state, |
| 893 | enum weston_key_state_update update_state) |
| 894 | { |
| 895 | struct weston_compositor *compositor = seat->compositor; |
| 896 | struct weston_keyboard *keyboard = &seat->keyboard; |
| 897 | struct weston_surface *focus = |
| 898 | (struct weston_surface *) keyboard->focus; |
| 899 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 900 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 901 | uint32_t *k, *end; |
| 902 | |
| 903 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 904 | if (compositor->ping_handler && focus) |
| 905 | compositor->ping_handler(focus, serial); |
| 906 | |
| 907 | weston_compositor_idle_inhibit(compositor); |
| 908 | keyboard->grab_key = key; |
| 909 | keyboard->grab_time = time; |
| 910 | } else { |
| 911 | weston_compositor_idle_release(compositor); |
| 912 | } |
| 913 | |
| 914 | end = keyboard->keys.data + keyboard->keys.size; |
| 915 | for (k = keyboard->keys.data; k < end; k++) { |
| 916 | if (*k == key) { |
| 917 | /* Ignore server-generated repeats. */ |
| 918 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 919 | return; |
| 920 | *k = *--end; |
| 921 | } |
| 922 | } |
| 923 | keyboard->keys.size = (void *) end - keyboard->keys.data; |
| 924 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 925 | k = wl_array_add(&keyboard->keys, sizeof *k); |
| 926 | *k = key; |
| 927 | } |
| 928 | |
| 929 | if (grab == &keyboard->default_grab || |
| 930 | grab == &keyboard->input_method_grab) { |
| 931 | weston_compositor_run_key_binding(compositor, seat, time, key, |
| 932 | state); |
| 933 | grab = keyboard->grab; |
| 934 | } |
| 935 | |
| 936 | grab->interface->key(grab, time, key, state); |
| 937 | |
| 938 | if (update_state == STATE_UPDATE_AUTOMATIC) { |
| 939 | update_modifier_state(seat, |
| 940 | wl_display_get_serial(compositor->wl_display), |
| 941 | key, |
| 942 | state); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | WL_EXPORT void |
| 947 | notify_pointer_focus(struct weston_seat *seat, struct weston_output *output, |
| 948 | wl_fixed_t x, wl_fixed_t y) |
| 949 | { |
| 950 | struct weston_compositor *compositor = seat->compositor; |
| 951 | |
| 952 | if (output) { |
| 953 | move_pointer(seat, x, y); |
| 954 | compositor->focus = 1; |
| 955 | } else { |
| 956 | compositor->focus = 0; |
| 957 | /* FIXME: We should call wl_pointer_set_focus(seat, |
| 958 | * NULL) here, but somehow that breaks re-entry... */ |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void |
| 963 | destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data) |
| 964 | { |
| 965 | struct weston_seat *ws; |
| 966 | |
| 967 | ws = container_of(listener, struct weston_seat, |
| 968 | saved_kbd_focus_listener); |
| 969 | |
| 970 | ws->saved_kbd_focus = NULL; |
| 971 | } |
| 972 | |
| 973 | WL_EXPORT void |
| 974 | notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys, |
| 975 | enum weston_key_state_update update_state) |
| 976 | { |
| 977 | struct weston_compositor *compositor = seat->compositor; |
| 978 | struct weston_keyboard *keyboard = seat->seat.keyboard; |
| 979 | struct wl_surface *surface; |
| 980 | uint32_t *k, serial; |
| 981 | |
| 982 | serial = wl_display_next_serial(compositor->wl_display); |
| 983 | wl_array_copy(&keyboard->keys, keys); |
| 984 | wl_array_for_each(k, &keyboard->keys) { |
| 985 | weston_compositor_idle_inhibit(compositor); |
| 986 | if (update_state == STATE_UPDATE_AUTOMATIC) |
| 987 | update_modifier_state(seat, serial, *k, |
| 988 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 989 | } |
| 990 | |
| 991 | /* Run key bindings after we've updated the state. */ |
| 992 | wl_array_for_each(k, &keyboard->keys) { |
| 993 | weston_compositor_run_key_binding(compositor, seat, 0, *k, |
| 994 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 995 | } |
| 996 | |
| 997 | surface = seat->saved_kbd_focus; |
| 998 | |
| 999 | if (surface) { |
| 1000 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 1001 | weston_keyboard_set_focus(keyboard, surface); |
| 1002 | seat->saved_kbd_focus = NULL; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | WL_EXPORT void |
| 1007 | notify_keyboard_focus_out(struct weston_seat *seat) |
| 1008 | { |
| 1009 | struct weston_compositor *compositor = seat->compositor; |
| 1010 | struct weston_keyboard *keyboard = seat->seat.keyboard; |
| 1011 | uint32_t *k, serial; |
| 1012 | |
| 1013 | serial = wl_display_next_serial(compositor->wl_display); |
| 1014 | wl_array_for_each(k, &keyboard->keys) { |
| 1015 | weston_compositor_idle_release(compositor); |
| 1016 | update_modifier_state(seat, serial, *k, |
| 1017 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 1018 | } |
| 1019 | |
| 1020 | seat->modifier_state = 0; |
| 1021 | |
| 1022 | if (keyboard->focus) { |
| 1023 | seat->saved_kbd_focus = keyboard->focus; |
| 1024 | seat->saved_kbd_focus_listener.notify = |
| 1025 | destroy_device_saved_kbd_focus; |
| 1026 | wl_signal_add(&keyboard->focus->resource.destroy_signal, |
| 1027 | &seat->saved_kbd_focus_listener); |
| 1028 | } |
| 1029 | |
| 1030 | weston_keyboard_set_focus(keyboard, NULL); |
| 1031 | /* FIXME: We really need keyboard grab cancel here to |
| 1032 | * let the grab shut down properly. As it is we leak |
| 1033 | * the grab data. */ |
| 1034 | weston_keyboard_end_grab(keyboard); |
| 1035 | } |
| 1036 | |
| 1037 | static void |
| 1038 | touch_set_focus(struct weston_seat *ws, struct wl_surface *surface) |
| 1039 | { |
| 1040 | struct wl_seat *seat = &ws->seat; |
| 1041 | struct wl_resource *resource; |
| 1042 | |
| 1043 | if (seat->touch->focus == surface) |
| 1044 | return; |
| 1045 | |
| 1046 | if (seat->touch->focus_resource) |
| 1047 | wl_list_remove(&seat->touch->focus_listener.link); |
| 1048 | seat->touch->focus = NULL; |
| 1049 | seat->touch->focus_resource = NULL; |
| 1050 | |
| 1051 | if (surface) { |
| 1052 | resource = |
Kristian Høgsberg | 80fb82d | 2013-05-06 21:49:55 -0400 | [diff] [blame] | 1053 | find_resource_for_surface(&seat->touch->resource_list, |
| 1054 | surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1055 | if (!resource) { |
| 1056 | weston_log("couldn't find resource\n"); |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | seat->touch->focus = surface; |
| 1061 | seat->touch->focus_resource = resource; |
| 1062 | wl_signal_add(&resource->destroy_signal, |
| 1063 | &seat->touch->focus_listener); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * notify_touch - emulates button touches and notifies surfaces accordingly. |
| 1069 | * |
| 1070 | * It assumes always the correct cycle sequence until it gets here: touch_down |
| 1071 | * → touch_update → ... → touch_update → touch_end. The driver is responsible |
| 1072 | * for sending along such order. |
| 1073 | * |
| 1074 | */ |
| 1075 | WL_EXPORT void |
| 1076 | notify_touch(struct weston_seat *seat, uint32_t time, int touch_id, |
| 1077 | wl_fixed_t x, wl_fixed_t y, int touch_type) |
| 1078 | { |
| 1079 | struct weston_compositor *ec = seat->compositor; |
| 1080 | struct wl_touch *touch = seat->seat.touch; |
| 1081 | struct wl_touch_grab *grab = touch->grab; |
| 1082 | struct weston_surface *es; |
| 1083 | wl_fixed_t sx, sy; |
| 1084 | |
| 1085 | /* Update grab's global coordinates. */ |
| 1086 | touch->grab_x = x; |
| 1087 | touch->grab_y = y; |
| 1088 | |
| 1089 | switch (touch_type) { |
| 1090 | case WL_TOUCH_DOWN: |
| 1091 | weston_compositor_idle_inhibit(ec); |
| 1092 | |
| 1093 | seat->num_tp++; |
| 1094 | |
| 1095 | /* the first finger down picks the surface, and all further go |
| 1096 | * to that surface for the remainder of the touch session i.e. |
| 1097 | * until all touch points are up again. */ |
| 1098 | if (seat->num_tp == 1) { |
| 1099 | es = weston_compositor_pick_surface(ec, x, y, &sx, &sy); |
| 1100 | touch_set_focus(seat, &es->surface); |
| 1101 | } else if (touch->focus) { |
| 1102 | es = (struct weston_surface *) touch->focus; |
| 1103 | weston_surface_from_global_fixed(es, x, y, &sx, &sy); |
| 1104 | } else { |
| 1105 | /* Unexpected condition: We have non-initial touch but |
| 1106 | * there is no focused surface. |
| 1107 | */ |
| 1108 | weston_log("touch event received with %d points down" |
| 1109 | "but no surface focused\n", seat->num_tp); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | grab->interface->down(grab, time, touch_id, sx, sy); |
| 1114 | break; |
| 1115 | case WL_TOUCH_MOTION: |
| 1116 | es = (struct weston_surface *) touch->focus; |
| 1117 | if (!es) |
| 1118 | break; |
| 1119 | |
| 1120 | weston_surface_from_global_fixed(es, x, y, &sx, &sy); |
| 1121 | grab->interface->motion(grab, time, touch_id, sx, sy); |
| 1122 | break; |
| 1123 | case WL_TOUCH_UP: |
| 1124 | weston_compositor_idle_release(ec); |
| 1125 | seat->num_tp--; |
| 1126 | |
| 1127 | grab->interface->up(grab, time, touch_id); |
| 1128 | if (seat->num_tp == 0) |
| 1129 | touch_set_focus(seat, NULL); |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | static void |
| 1135 | pointer_handle_sprite_destroy(struct wl_listener *listener, void *data) |
| 1136 | { |
| 1137 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 1138 | sprite_destroy_listener); |
| 1139 | |
| 1140 | seat->sprite = NULL; |
| 1141 | } |
| 1142 | |
| 1143 | static void |
| 1144 | pointer_cursor_surface_configure(struct weston_surface *es, |
| 1145 | int32_t dx, int32_t dy, int32_t width, int32_t height) |
| 1146 | { |
| 1147 | struct weston_seat *seat = es->configure_private; |
| 1148 | int x, y; |
| 1149 | |
| 1150 | if (width == 0) |
| 1151 | return; |
| 1152 | |
| 1153 | assert(es == seat->sprite); |
| 1154 | |
| 1155 | seat->hotspot_x -= dx; |
| 1156 | seat->hotspot_y -= dy; |
| 1157 | |
| 1158 | x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x; |
| 1159 | y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y; |
| 1160 | |
| 1161 | weston_surface_configure(seat->sprite, x, y, |
| 1162 | width, height); |
| 1163 | |
| 1164 | empty_region(&es->pending.input); |
| 1165 | |
| 1166 | if (!weston_surface_is_mapped(es)) { |
| 1167 | wl_list_insert(&es->compositor->cursor_layer.surface_list, |
| 1168 | &es->layer_link); |
| 1169 | weston_surface_update_transform(es); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | static void |
| 1174 | pointer_unmap_sprite(struct weston_seat *seat) |
| 1175 | { |
| 1176 | if (weston_surface_is_mapped(seat->sprite)) |
| 1177 | weston_surface_unmap(seat->sprite); |
| 1178 | |
| 1179 | wl_list_remove(&seat->sprite_destroy_listener.link); |
| 1180 | seat->sprite->configure = NULL; |
| 1181 | seat->sprite->configure_private = NULL; |
| 1182 | seat->sprite = NULL; |
| 1183 | } |
| 1184 | |
| 1185 | static void |
| 1186 | pointer_set_cursor(struct wl_client *client, struct wl_resource *resource, |
| 1187 | uint32_t serial, struct wl_resource *surface_resource, |
| 1188 | int32_t x, int32_t y) |
| 1189 | { |
| 1190 | struct weston_seat *seat = resource->data; |
| 1191 | struct weston_surface *surface = NULL; |
| 1192 | |
| 1193 | if (surface_resource) |
| 1194 | surface = surface_resource->data; |
| 1195 | |
| 1196 | if (seat->seat.pointer->focus == NULL) |
| 1197 | return; |
| 1198 | if (seat->seat.pointer->focus->resource.client != client) |
| 1199 | return; |
| 1200 | if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2) |
| 1201 | return; |
| 1202 | |
| 1203 | if (surface && surface != seat->sprite) { |
| 1204 | if (surface->configure) { |
| 1205 | wl_resource_post_error(&surface->surface.resource, |
| 1206 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 1207 | "surface->configure already " |
| 1208 | "set"); |
| 1209 | return; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (seat->sprite) |
| 1214 | pointer_unmap_sprite(seat); |
| 1215 | |
| 1216 | if (!surface) |
| 1217 | return; |
| 1218 | |
| 1219 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 1220 | &seat->sprite_destroy_listener); |
| 1221 | |
| 1222 | surface->configure = pointer_cursor_surface_configure; |
| 1223 | surface->configure_private = seat; |
| 1224 | seat->sprite = surface; |
| 1225 | seat->hotspot_x = x; |
| 1226 | seat->hotspot_y = y; |
| 1227 | |
| 1228 | if (surface->buffer_ref.buffer) |
| 1229 | pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface), |
| 1230 | weston_surface_buffer_height(surface)); |
| 1231 | } |
| 1232 | |
| 1233 | static const struct wl_pointer_interface pointer_interface = { |
| 1234 | pointer_set_cursor |
| 1235 | }; |
| 1236 | |
| 1237 | static void |
| 1238 | handle_drag_surface_destroy(struct wl_listener *listener, void *data) |
| 1239 | { |
| 1240 | struct weston_seat *seat; |
| 1241 | |
| 1242 | seat = container_of(listener, struct weston_seat, |
| 1243 | drag_surface_destroy_listener); |
| 1244 | |
| 1245 | seat->drag_surface = NULL; |
| 1246 | } |
| 1247 | |
| 1248 | static void |
| 1249 | seat_get_pointer(struct wl_client *client, struct wl_resource *resource, |
| 1250 | uint32_t id) |
| 1251 | { |
| 1252 | struct weston_seat *seat = resource->data; |
| 1253 | struct wl_resource *cr; |
| 1254 | |
| 1255 | if (!seat->seat.pointer) |
| 1256 | return; |
| 1257 | |
| 1258 | cr = wl_client_add_object(client, &wl_pointer_interface, |
| 1259 | &pointer_interface, id, seat); |
| 1260 | wl_list_insert(&seat->seat.pointer->resource_list, &cr->link); |
| 1261 | cr->destroy = unbind_resource; |
| 1262 | |
| 1263 | if (seat->seat.pointer->focus && |
| 1264 | seat->seat.pointer->focus->resource.client == client) { |
| 1265 | struct weston_surface *surface; |
| 1266 | wl_fixed_t sx, sy; |
| 1267 | |
| 1268 | surface = (struct weston_surface *) seat->seat.pointer->focus; |
| 1269 | weston_surface_from_global_fixed(surface, |
| 1270 | seat->seat.pointer->x, |
| 1271 | seat->seat.pointer->y, |
| 1272 | &sx, |
| 1273 | &sy); |
| 1274 | wl_pointer_set_focus(seat->seat.pointer, |
| 1275 | seat->seat.pointer->focus, |
| 1276 | sx, |
| 1277 | sy); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | static void |
| 1282 | seat_get_keyboard(struct wl_client *client, struct wl_resource *resource, |
| 1283 | uint32_t id) |
| 1284 | { |
| 1285 | struct weston_seat *seat = resource->data; |
| 1286 | struct wl_resource *cr; |
| 1287 | |
| 1288 | if (!seat->seat.keyboard) |
| 1289 | return; |
| 1290 | |
| 1291 | cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id, |
| 1292 | seat); |
| 1293 | wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link); |
| 1294 | cr->destroy = unbind_resource; |
| 1295 | |
| 1296 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 1297 | seat->xkb_info.keymap_fd, |
| 1298 | seat->xkb_info.keymap_size); |
| 1299 | |
| 1300 | if (seat->seat.keyboard->focus && |
| 1301 | seat->seat.keyboard->focus->resource.client == client) { |
| 1302 | weston_keyboard_set_focus(seat->seat.keyboard, |
| 1303 | seat->seat.keyboard->focus); |
| 1304 | wl_data_device_set_keyboard_focus(&seat->seat); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | static void |
| 1309 | seat_get_touch(struct wl_client *client, struct wl_resource *resource, |
| 1310 | uint32_t id) |
| 1311 | { |
| 1312 | struct weston_seat *seat = resource->data; |
| 1313 | struct wl_resource *cr; |
| 1314 | |
| 1315 | if (!seat->seat.touch) |
| 1316 | return; |
| 1317 | |
| 1318 | cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat); |
| 1319 | wl_list_insert(&seat->seat.touch->resource_list, &cr->link); |
| 1320 | cr->destroy = unbind_resource; |
| 1321 | } |
| 1322 | |
| 1323 | static const struct wl_seat_interface seat_interface = { |
| 1324 | seat_get_pointer, |
| 1325 | seat_get_keyboard, |
| 1326 | seat_get_touch, |
| 1327 | }; |
| 1328 | |
| 1329 | static void |
| 1330 | bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 1331 | { |
| 1332 | struct wl_seat *seat = data; |
| 1333 | struct wl_resource *resource; |
| 1334 | enum wl_seat_capability caps = 0; |
| 1335 | |
| 1336 | resource = wl_client_add_object(client, &wl_seat_interface, |
| 1337 | &seat_interface, id, data); |
| 1338 | wl_list_insert(&seat->base_resource_list, &resource->link); |
| 1339 | resource->destroy = unbind_resource; |
| 1340 | |
| 1341 | if (seat->pointer) |
| 1342 | caps |= WL_SEAT_CAPABILITY_POINTER; |
| 1343 | if (seat->keyboard) |
| 1344 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
| 1345 | if (seat->touch) |
| 1346 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 1347 | |
| 1348 | wl_seat_send_capabilities(resource, caps); |
| 1349 | } |
| 1350 | |
| 1351 | static void |
| 1352 | device_handle_new_drag_icon(struct wl_listener *listener, void *data) |
| 1353 | { |
| 1354 | struct weston_seat *seat; |
| 1355 | |
| 1356 | seat = container_of(listener, struct weston_seat, |
| 1357 | new_drag_icon_listener); |
| 1358 | |
| 1359 | weston_seat_update_drag_surface(seat, 0, 0); |
| 1360 | } |
| 1361 | |
| 1362 | int |
| 1363 | weston_compositor_xkb_init(struct weston_compositor *ec, |
| 1364 | struct xkb_rule_names *names) |
| 1365 | { |
| 1366 | if (ec->xkb_context == NULL) { |
| 1367 | ec->xkb_context = xkb_context_new(0); |
| 1368 | if (ec->xkb_context == NULL) { |
| 1369 | weston_log("failed to create XKB context\n"); |
| 1370 | return -1; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | if (names) |
| 1375 | ec->xkb_names = *names; |
| 1376 | if (!ec->xkb_names.rules) |
| 1377 | ec->xkb_names.rules = strdup("evdev"); |
| 1378 | if (!ec->xkb_names.model) |
| 1379 | ec->xkb_names.model = strdup("pc105"); |
| 1380 | if (!ec->xkb_names.layout) |
| 1381 | ec->xkb_names.layout = strdup("us"); |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | static void xkb_info_destroy(struct weston_xkb_info *xkb_info) |
| 1387 | { |
| 1388 | if (xkb_info->keymap) |
| 1389 | xkb_map_unref(xkb_info->keymap); |
| 1390 | |
| 1391 | if (xkb_info->keymap_area) |
| 1392 | munmap(xkb_info->keymap_area, xkb_info->keymap_size); |
| 1393 | if (xkb_info->keymap_fd >= 0) |
| 1394 | close(xkb_info->keymap_fd); |
| 1395 | } |
| 1396 | |
| 1397 | void |
| 1398 | weston_compositor_xkb_destroy(struct weston_compositor *ec) |
| 1399 | { |
| 1400 | free((char *) ec->xkb_names.rules); |
| 1401 | free((char *) ec->xkb_names.model); |
| 1402 | free((char *) ec->xkb_names.layout); |
| 1403 | free((char *) ec->xkb_names.variant); |
| 1404 | free((char *) ec->xkb_names.options); |
| 1405 | |
| 1406 | xkb_info_destroy(&ec->xkb_info); |
| 1407 | xkb_context_unref(ec->xkb_context); |
| 1408 | } |
| 1409 | |
| 1410 | static int |
| 1411 | weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info) |
| 1412 | { |
| 1413 | char *keymap_str; |
| 1414 | |
| 1415 | xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1416 | XKB_MOD_NAME_SHIFT); |
| 1417 | xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1418 | XKB_MOD_NAME_CAPS); |
| 1419 | xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1420 | XKB_MOD_NAME_CTRL); |
| 1421 | xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1422 | XKB_MOD_NAME_ALT); |
| 1423 | xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2"); |
| 1424 | xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3"); |
| 1425 | xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1426 | XKB_MOD_NAME_LOGO); |
| 1427 | xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5"); |
| 1428 | |
| 1429 | xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1430 | XKB_LED_NAME_NUM); |
| 1431 | xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1432 | XKB_LED_NAME_CAPS); |
| 1433 | xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1434 | XKB_LED_NAME_SCROLL); |
| 1435 | |
| 1436 | keymap_str = xkb_map_get_as_string(xkb_info->keymap); |
| 1437 | if (keymap_str == NULL) { |
| 1438 | weston_log("failed to get string version of keymap\n"); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | xkb_info->keymap_size = strlen(keymap_str) + 1; |
| 1442 | |
| 1443 | xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size); |
| 1444 | if (xkb_info->keymap_fd < 0) { |
| 1445 | weston_log("creating a keymap file for %lu bytes failed: %m\n", |
| 1446 | (unsigned long) xkb_info->keymap_size); |
| 1447 | goto err_keymap_str; |
| 1448 | } |
| 1449 | |
| 1450 | xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size, |
| 1451 | PROT_READ | PROT_WRITE, |
| 1452 | MAP_SHARED, xkb_info->keymap_fd, 0); |
| 1453 | if (xkb_info->keymap_area == MAP_FAILED) { |
| 1454 | weston_log("failed to mmap() %lu bytes\n", |
| 1455 | (unsigned long) xkb_info->keymap_size); |
| 1456 | goto err_dev_zero; |
| 1457 | } |
| 1458 | strcpy(xkb_info->keymap_area, keymap_str); |
| 1459 | free(keymap_str); |
| 1460 | |
| 1461 | return 0; |
| 1462 | |
| 1463 | err_dev_zero: |
| 1464 | close(xkb_info->keymap_fd); |
| 1465 | xkb_info->keymap_fd = -1; |
| 1466 | err_keymap_str: |
| 1467 | free(keymap_str); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
| 1471 | static int |
| 1472 | weston_compositor_build_global_keymap(struct weston_compositor *ec) |
| 1473 | { |
| 1474 | if (ec->xkb_info.keymap != NULL) |
| 1475 | return 0; |
| 1476 | |
| 1477 | ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context, |
| 1478 | &ec->xkb_names, |
| 1479 | 0); |
| 1480 | if (ec->xkb_info.keymap == NULL) { |
| 1481 | weston_log("failed to compile global XKB keymap\n"); |
| 1482 | weston_log(" tried rules %s, model %s, layout %s, variant %s, " |
| 1483 | "options %s\n", |
| 1484 | ec->xkb_names.rules, ec->xkb_names.model, |
| 1485 | ec->xkb_names.layout, ec->xkb_names.variant, |
| 1486 | ec->xkb_names.options); |
| 1487 | return -1; |
| 1488 | } |
| 1489 | |
| 1490 | if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0) |
| 1491 | return -1; |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | WL_EXPORT int |
| 1497 | weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 1498 | { |
| 1499 | if (seat->has_keyboard) |
| 1500 | return 0; |
| 1501 | |
| 1502 | if (keymap != NULL) { |
| 1503 | seat->xkb_info.keymap = xkb_map_ref(keymap); |
| 1504 | if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0) |
| 1505 | return -1; |
| 1506 | } else { |
| 1507 | if (weston_compositor_build_global_keymap(seat->compositor) < 0) |
| 1508 | return -1; |
| 1509 | seat->xkb_info = seat->compositor->xkb_info; |
| 1510 | seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap); |
| 1511 | } |
| 1512 | |
| 1513 | seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap); |
| 1514 | if (seat->xkb_state.state == NULL) { |
| 1515 | weston_log("failed to initialise XKB state\n"); |
| 1516 | return -1; |
| 1517 | } |
| 1518 | |
| 1519 | seat->xkb_state.leds = 0; |
| 1520 | |
| 1521 | weston_keyboard_init(&seat->keyboard); |
| 1522 | wl_seat_set_keyboard(&seat->seat, &seat->keyboard); |
| 1523 | |
| 1524 | seat->has_keyboard = 1; |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | WL_EXPORT void |
| 1530 | weston_seat_init_pointer(struct weston_seat *seat) |
| 1531 | { |
| 1532 | if (seat->has_pointer) |
| 1533 | return; |
| 1534 | |
| 1535 | wl_pointer_init(&seat->pointer); |
| 1536 | wl_seat_set_pointer(&seat->seat, &seat->pointer); |
| 1537 | |
| 1538 | seat->has_pointer = 1; |
| 1539 | } |
| 1540 | |
| 1541 | WL_EXPORT void |
| 1542 | weston_seat_init_touch(struct weston_seat *seat) |
| 1543 | { |
| 1544 | if (seat->has_touch) |
| 1545 | return; |
| 1546 | |
| 1547 | wl_touch_init(&seat->touch); |
| 1548 | wl_seat_set_touch(&seat->seat, &seat->touch); |
| 1549 | |
| 1550 | seat->has_touch = 1; |
| 1551 | } |
| 1552 | |
| 1553 | WL_EXPORT void |
| 1554 | weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec) |
| 1555 | { |
| 1556 | wl_seat_init(&seat->seat); |
| 1557 | seat->has_pointer = 0; |
| 1558 | seat->has_keyboard = 0; |
| 1559 | seat->has_touch = 0; |
| 1560 | |
| 1561 | wl_display_add_global(ec->wl_display, &wl_seat_interface, seat, |
| 1562 | bind_seat); |
| 1563 | |
| 1564 | seat->sprite = NULL; |
| 1565 | seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy; |
| 1566 | |
| 1567 | seat->compositor = ec; |
| 1568 | seat->hotspot_x = 16; |
| 1569 | seat->hotspot_y = 16; |
| 1570 | seat->modifier_state = 0; |
| 1571 | seat->num_tp = 0; |
| 1572 | |
| 1573 | seat->drag_surface_destroy_listener.notify = |
| 1574 | handle_drag_surface_destroy; |
| 1575 | |
| 1576 | wl_list_insert(ec->seat_list.prev, &seat->link); |
| 1577 | |
| 1578 | seat->new_drag_icon_listener.notify = device_handle_new_drag_icon; |
| 1579 | wl_signal_add(&seat->seat.drag_icon_signal, |
| 1580 | &seat->new_drag_icon_listener); |
| 1581 | |
| 1582 | clipboard_create(seat); |
| 1583 | |
| 1584 | wl_signal_init(&seat->destroy_signal); |
| 1585 | wl_signal_emit(&ec->seat_created_signal, seat); |
| 1586 | } |
| 1587 | |
| 1588 | WL_EXPORT void |
| 1589 | weston_seat_release(struct weston_seat *seat) |
| 1590 | { |
| 1591 | wl_list_remove(&seat->link); |
| 1592 | /* The global object is destroyed at wl_display_destroy() time. */ |
| 1593 | |
| 1594 | if (seat->sprite) |
| 1595 | pointer_unmap_sprite(seat); |
| 1596 | |
| 1597 | if (seat->xkb_state.state != NULL) |
| 1598 | xkb_state_unref(seat->xkb_state.state); |
| 1599 | xkb_info_destroy(&seat->xkb_info); |
| 1600 | |
| 1601 | wl_seat_release(&seat->seat); |
| 1602 | wl_signal_emit(&seat->destroy_signal, seat); |
| 1603 | } |
| 1604 | |
| 1605 | static void |
| 1606 | drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height) |
| 1607 | { |
| 1608 | empty_region(&es->pending.input); |
| 1609 | |
| 1610 | weston_surface_configure(es, |
| 1611 | es->geometry.x + sx, es->geometry.y + sy, |
| 1612 | width, height); |
| 1613 | } |
| 1614 | |
| 1615 | static int |
| 1616 | device_setup_new_drag_surface(struct weston_seat *ws, |
| 1617 | struct weston_surface *surface) |
| 1618 | { |
| 1619 | struct wl_seat *seat = &ws->seat; |
| 1620 | |
| 1621 | if (surface->configure) { |
| 1622 | wl_resource_post_error(&surface->surface.resource, |
| 1623 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 1624 | "surface->configure already set"); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | ws->drag_surface = surface; |
| 1629 | |
| 1630 | weston_surface_set_position(ws->drag_surface, |
| 1631 | wl_fixed_to_double(seat->pointer->x), |
| 1632 | wl_fixed_to_double(seat->pointer->y)); |
| 1633 | |
| 1634 | surface->configure = drag_surface_configure; |
| 1635 | |
| 1636 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 1637 | &ws->drag_surface_destroy_listener); |
| 1638 | |
| 1639 | return 1; |
| 1640 | } |
| 1641 | |
| 1642 | static void |
| 1643 | device_release_drag_surface(struct weston_seat *seat) |
| 1644 | { |
| 1645 | if (weston_surface_is_mapped(seat->drag_surface)) |
| 1646 | weston_surface_unmap(seat->drag_surface); |
| 1647 | |
| 1648 | seat->drag_surface->configure = NULL; |
| 1649 | empty_region(&seat->drag_surface->pending.input); |
| 1650 | wl_list_remove(&seat->drag_surface_destroy_listener.link); |
| 1651 | seat->drag_surface = NULL; |
| 1652 | } |
| 1653 | |
| 1654 | static void |
| 1655 | device_map_drag_surface(struct weston_seat *seat) |
| 1656 | { |
| 1657 | struct wl_list *list; |
| 1658 | |
| 1659 | if (weston_surface_is_mapped(seat->drag_surface) || |
| 1660 | !seat->drag_surface->buffer_ref.buffer) |
| 1661 | return; |
| 1662 | |
| 1663 | if (seat->sprite && weston_surface_is_mapped(seat->sprite)) |
| 1664 | list = &seat->sprite->layer_link; |
| 1665 | else |
| 1666 | list = &seat->compositor->cursor_layer.surface_list; |
| 1667 | |
| 1668 | wl_list_insert(list, &seat->drag_surface->layer_link); |
| 1669 | weston_surface_update_transform(seat->drag_surface); |
| 1670 | empty_region(&seat->drag_surface->input); |
| 1671 | } |
| 1672 | |
| 1673 | static void |
| 1674 | weston_seat_update_drag_surface(struct weston_seat *seat, |
| 1675 | int dx, int dy) |
| 1676 | { |
| 1677 | int surface_changed = 0; |
| 1678 | |
| 1679 | if (!seat->drag_surface && !seat->seat.drag_surface) |
| 1680 | return; |
| 1681 | |
| 1682 | if (seat->drag_surface && seat->seat.drag_surface && |
| 1683 | (&seat->drag_surface->surface.resource != |
| 1684 | &seat->seat.drag_surface->resource)) |
| 1685 | /* between calls to this funcion we got a new drag_surface */ |
| 1686 | surface_changed = 1; |
| 1687 | |
| 1688 | if (!seat->seat.drag_surface || surface_changed) { |
| 1689 | device_release_drag_surface(seat); |
| 1690 | if (!surface_changed) |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | if (!seat->drag_surface || surface_changed) { |
| 1695 | struct weston_surface *surface = (struct weston_surface *) |
| 1696 | seat->seat.drag_surface; |
| 1697 | if (!device_setup_new_drag_surface(seat, surface)) |
| 1698 | return; |
| 1699 | } |
| 1700 | |
| 1701 | /* the client may not have attached a buffer to the drag surface |
| 1702 | * when we setup it up, so check if map is needed on every update */ |
| 1703 | device_map_drag_surface(seat); |
| 1704 | |
| 1705 | if (!dx && !dy) |
| 1706 | return; |
| 1707 | |
| 1708 | weston_surface_set_position(seat->drag_surface, |
| 1709 | seat->drag_surface->geometry.x + wl_fixed_to_double(dx), |
| 1710 | seat->drag_surface->geometry.y + wl_fixed_to_double(dy)); |
| 1711 | } |
| 1712 | |
| 1713 | WL_EXPORT void |
| 1714 | weston_compositor_update_drag_surfaces(struct weston_compositor *compositor) |
| 1715 | { |
| 1716 | struct weston_seat *seat; |
| 1717 | |
| 1718 | wl_list_for_each(seat, &compositor->seat_list, link) |
| 1719 | weston_seat_update_drag_surface(seat, 0, 0); |
| 1720 | } |