Jason Ekstrand | 47928d8 | 2014-04-02 19:54:01 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2014 Jason Ekstrand |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "config.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <signal.h> |
| 33 | #include <linux/input.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include <wayland-client.h> |
| 37 | |
| 38 | #include "compositor.h" |
| 39 | #include "../shared/os-compatibility.h" |
| 40 | #include "fullscreen-shell-client-protocol.h" |
| 41 | |
| 42 | struct shared_output { |
| 43 | struct weston_output *output; |
| 44 | struct wl_listener output_destroyed; |
| 45 | struct wl_list seat_list; |
| 46 | |
| 47 | struct { |
| 48 | struct wl_display *display; |
| 49 | struct wl_registry *registry; |
| 50 | struct wl_compositor *compositor; |
| 51 | struct wl_shm *shm; |
| 52 | uint32_t shm_formats; |
| 53 | struct _wl_fullscreen_shell *fshell; |
| 54 | struct wl_output *output; |
| 55 | struct wl_surface *surface; |
| 56 | struct wl_callback *frame_cb; |
| 57 | struct _wl_fullscreen_shell_mode_feedback *mode_feedback; |
| 58 | } parent; |
| 59 | |
| 60 | struct wl_event_source *event_source; |
| 61 | struct wl_listener frame_listener; |
| 62 | |
| 63 | struct { |
| 64 | int32_t width, height; |
| 65 | |
| 66 | struct wl_list buffers; |
| 67 | struct wl_list free_buffers; |
| 68 | } shm; |
| 69 | |
| 70 | int cache_dirty; |
| 71 | pixman_image_t *cache_image; |
| 72 | uint32_t *tmp_data; |
| 73 | size_t tmp_data_size; |
| 74 | }; |
| 75 | |
| 76 | struct ss_seat { |
| 77 | struct weston_seat base; |
| 78 | struct shared_output *output; |
| 79 | struct wl_list link; |
| 80 | |
| 81 | struct { |
| 82 | struct wl_seat *seat; |
| 83 | struct wl_pointer *pointer; |
| 84 | struct wl_keyboard *keyboard; |
| 85 | } parent; |
| 86 | |
| 87 | enum weston_key_state_update keyboard_state_update; |
| 88 | uint32_t key_serial; |
| 89 | }; |
| 90 | |
| 91 | struct ss_shm_buffer { |
| 92 | struct shared_output *output; |
| 93 | struct wl_list link; |
| 94 | struct wl_list free_link; |
| 95 | |
| 96 | struct wl_buffer *buffer; |
| 97 | void *data; |
| 98 | size_t size; |
| 99 | pixman_region32_t damage; |
| 100 | |
| 101 | pixman_image_t *pm_image; |
| 102 | }; |
| 103 | |
| 104 | static void |
| 105 | ss_seat_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 106 | uint32_t serial, struct wl_surface *surface, |
| 107 | wl_fixed_t x, wl_fixed_t y) |
| 108 | { |
| 109 | struct ss_seat *seat = data; |
| 110 | |
| 111 | /* No transformation of input position is required here because we are |
| 112 | * always receiving the input in the same coordinates as the output. */ |
| 113 | |
| 114 | notify_pointer_focus(&seat->base, NULL, 0, 0); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 119 | uint32_t serial, struct wl_surface *surface) |
| 120 | { |
| 121 | struct ss_seat *seat = data; |
| 122 | |
| 123 | notify_pointer_focus(&seat->base, NULL, 0, 0); |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | ss_seat_handle_motion(void *data, struct wl_pointer *pointer, |
| 128 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 129 | { |
| 130 | struct ss_seat *seat = data; |
| 131 | |
| 132 | /* No transformation of input position is required here because we are |
| 133 | * always receiving the input in the same coordinates as the output. */ |
| 134 | |
| 135 | notify_motion_absolute(&seat->base, time, x, y); |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | ss_seat_handle_button(void *data, struct wl_pointer *pointer, |
| 140 | uint32_t serial, uint32_t time, uint32_t button, |
| 141 | uint32_t state) |
| 142 | { |
| 143 | struct ss_seat *seat = data; |
| 144 | |
| 145 | notify_button(&seat->base, time, button, state); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | ss_seat_handle_axis(void *data, struct wl_pointer *pointer, |
| 150 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 151 | { |
| 152 | struct ss_seat *seat = data; |
| 153 | |
| 154 | notify_axis(&seat->base, time, axis, value); |
| 155 | } |
| 156 | |
| 157 | static const struct wl_pointer_listener ss_seat_pointer_listener = { |
| 158 | ss_seat_handle_pointer_enter, |
| 159 | ss_seat_handle_pointer_leave, |
| 160 | ss_seat_handle_motion, |
| 161 | ss_seat_handle_button, |
| 162 | ss_seat_handle_axis, |
| 163 | }; |
| 164 | |
| 165 | static void |
| 166 | ss_seat_handle_keymap(void *data, struct wl_keyboard *keyboard, |
| 167 | uint32_t format, int fd, uint32_t size) |
| 168 | { |
| 169 | struct ss_seat *seat = data; |
| 170 | struct xkb_keymap *keymap; |
| 171 | char *map_str; |
| 172 | |
| 173 | if (!data) |
| 174 | goto error; |
| 175 | |
| 176 | if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 177 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 178 | if (map_str == MAP_FAILED) { |
| 179 | weston_log("mmap failed: %m\n"); |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | keymap = xkb_map_new_from_string(seat->base.compositor->xkb_context, |
| 184 | map_str, |
| 185 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 186 | 0); |
| 187 | munmap(map_str, size); |
| 188 | |
| 189 | if (!keymap) { |
| 190 | weston_log("failed to compile keymap\n"); |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | seat->keyboard_state_update = STATE_UPDATE_NONE; |
| 195 | } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) { |
| 196 | weston_log("No keymap provided; falling back to default\n"); |
| 197 | keymap = NULL; |
| 198 | seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC; |
| 199 | } else { |
| 200 | weston_log("Invalid keymap\n"); |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | close(fd); |
| 205 | |
| 206 | if (seat->base.keyboard) |
| 207 | weston_seat_update_keymap(&seat->base, keymap); |
| 208 | else |
| 209 | weston_seat_init_keyboard(&seat->base, keymap); |
| 210 | |
| 211 | if (keymap) |
| 212 | xkb_map_unref(keymap); |
| 213 | |
| 214 | return; |
| 215 | |
| 216 | error: |
| 217 | wl_keyboard_release(seat->parent.keyboard); |
| 218 | close(fd); |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard, |
| 223 | uint32_t serial, struct wl_surface *surface, |
| 224 | struct wl_array *keys) |
| 225 | { |
| 226 | struct ss_seat *seat = data; |
| 227 | |
| 228 | /* XXX: If we get a modifier event immediately before the focus, |
| 229 | * we should try to keep the same serial. */ |
| 230 | notify_keyboard_focus_in(&seat->base, keys, |
| 231 | STATE_UPDATE_AUTOMATIC); |
| 232 | } |
| 233 | |
| 234 | static void |
| 235 | ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard, |
| 236 | uint32_t serial, struct wl_surface *surface) |
| 237 | { |
| 238 | struct ss_seat *seat = data; |
| 239 | |
| 240 | notify_keyboard_focus_out(&seat->base); |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | ss_seat_handle_key(void *data, struct wl_keyboard *keyboard, |
| 245 | uint32_t serial, uint32_t time, |
| 246 | uint32_t key, uint32_t state) |
| 247 | { |
| 248 | struct ss_seat *seat = data; |
| 249 | |
| 250 | seat->key_serial = serial; |
| 251 | notify_key(&seat->base, time, key, |
| 252 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
| 253 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 254 | seat->keyboard_state_update); |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | ss_seat_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 259 | uint32_t serial_in, uint32_t mods_depressed, |
| 260 | uint32_t mods_latched, uint32_t mods_locked, |
| 261 | uint32_t group) |
| 262 | { |
| 263 | struct ss_seat *seat = data; |
| 264 | struct weston_compositor *c = seat->output->output->compositor; |
| 265 | uint32_t serial_out; |
| 266 | |
| 267 | /* If we get a key event followed by a modifier event with the |
| 268 | * same serial number, then we try to preserve those semantics by |
| 269 | * reusing the same serial number on the way out too. */ |
| 270 | if (serial_in == seat->key_serial) |
| 271 | serial_out = wl_display_get_serial(c->wl_display); |
| 272 | else |
| 273 | serial_out = wl_display_next_serial(c->wl_display); |
| 274 | |
| 275 | xkb_state_update_mask(seat->base.keyboard->xkb_state.state, |
| 276 | mods_depressed, mods_latched, |
| 277 | mods_locked, 0, 0, group); |
| 278 | notify_modifiers(&seat->base, serial_out); |
| 279 | } |
| 280 | |
| 281 | static const struct wl_keyboard_listener ss_seat_keyboard_listener = { |
| 282 | ss_seat_handle_keymap, |
| 283 | ss_seat_handle_keyboard_enter, |
| 284 | ss_seat_handle_keyboard_leave, |
| 285 | ss_seat_handle_key, |
| 286 | ss_seat_handle_modifiers, |
| 287 | }; |
| 288 | |
| 289 | static void |
| 290 | ss_seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 291 | enum wl_seat_capability caps) |
| 292 | { |
| 293 | struct ss_seat *ss_seat = data; |
| 294 | |
| 295 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) { |
| 296 | ss_seat->parent.pointer = wl_seat_get_pointer(seat); |
| 297 | wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat); |
| 298 | wl_pointer_add_listener(ss_seat->parent.pointer, |
| 299 | &ss_seat_pointer_listener, ss_seat); |
| 300 | weston_seat_init_pointer(&ss_seat->base); |
| 301 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) { |
| 302 | wl_pointer_destroy(ss_seat->parent.pointer); |
| 303 | ss_seat->parent.pointer = NULL; |
| 304 | } |
| 305 | |
| 306 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) { |
| 307 | ss_seat->parent.keyboard = wl_seat_get_keyboard(seat); |
| 308 | wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat); |
| 309 | wl_keyboard_add_listener(ss_seat->parent.keyboard, |
| 310 | &ss_seat_keyboard_listener, ss_seat); |
| 311 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) { |
| 312 | wl_keyboard_destroy(ss_seat->parent.keyboard); |
| 313 | ss_seat->parent.keyboard = NULL; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static const struct wl_seat_listener ss_seat_listener = { |
| 318 | ss_seat_handle_capabilities, |
| 319 | }; |
| 320 | |
| 321 | static struct ss_seat * |
| 322 | ss_seat_create(struct shared_output *so, uint32_t id) |
| 323 | { |
| 324 | struct ss_seat *seat; |
| 325 | |
| 326 | seat = zalloc(sizeof *seat); |
| 327 | if (seat == NULL) |
| 328 | return NULL; |
| 329 | |
| 330 | weston_seat_init(&seat->base, so->output->compositor, "default"); |
| 331 | seat->output = so; |
| 332 | seat->parent.seat = wl_registry_bind(so->parent.registry, id, |
| 333 | &wl_seat_interface, 1); |
| 334 | wl_list_insert(so->seat_list.prev, &seat->link); |
| 335 | |
| 336 | wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat); |
| 337 | wl_seat_set_user_data(seat->parent.seat, seat); |
| 338 | |
| 339 | return seat; |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | ss_seat_destroy(struct ss_seat *seat) |
| 344 | { |
| 345 | if (seat->parent.pointer) |
| 346 | wl_pointer_release(seat->parent.pointer); |
| 347 | if (seat->parent.keyboard) |
| 348 | wl_keyboard_release(seat->parent.keyboard); |
| 349 | wl_seat_destroy(seat->parent.seat); |
| 350 | |
| 351 | wl_list_remove(&seat->link); |
| 352 | |
| 353 | weston_seat_release(&seat->base); |
| 354 | |
| 355 | free(seat); |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | ss_shm_buffer_destroy(struct ss_shm_buffer *buffer) |
| 360 | { |
| 361 | pixman_image_unref(buffer->pm_image); |
| 362 | |
| 363 | wl_buffer_destroy(buffer->buffer); |
| 364 | munmap(buffer->data, buffer->size); |
| 365 | |
| 366 | pixman_region32_fini(&buffer->damage); |
| 367 | |
| 368 | wl_list_remove(&buffer->link); |
| 369 | wl_list_remove(&buffer->free_link); |
| 370 | free(buffer); |
| 371 | } |
| 372 | |
| 373 | static void |
| 374 | buffer_release(void *data, struct wl_buffer *buffer) |
| 375 | { |
| 376 | struct ss_shm_buffer *sb = data; |
| 377 | |
| 378 | if (sb->output) { |
| 379 | wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link); |
| 380 | } else { |
| 381 | ss_shm_buffer_destroy(sb); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static const struct wl_buffer_listener buffer_listener = { |
| 386 | buffer_release |
| 387 | }; |
| 388 | |
| 389 | static struct ss_shm_buffer * |
| 390 | shared_output_get_shm_buffer(struct shared_output *so) |
| 391 | { |
| 392 | struct ss_shm_buffer *sb, *bnext; |
| 393 | struct wl_shm_pool *pool; |
| 394 | int width, height, stride; |
| 395 | int fd; |
| 396 | unsigned char *data; |
| 397 | |
| 398 | width = so->output->width; |
| 399 | height = so->output->height; |
| 400 | stride = width * 4; |
| 401 | |
| 402 | /* If the size of the output changed, we free the old buffers and |
| 403 | * make new ones. */ |
| 404 | if (so->shm.width != width || |
| 405 | so->shm.height != height) { |
| 406 | |
| 407 | /* Destroy free buffers */ |
| 408 | wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link) |
| 409 | ss_shm_buffer_destroy(sb); |
| 410 | |
| 411 | /* Orphan in-use buffers so they get destroyed */ |
| 412 | wl_list_for_each(sb, &so->shm.buffers, link) |
| 413 | sb->output = NULL; |
| 414 | |
| 415 | so->shm.width = width; |
| 416 | so->shm.height = height; |
| 417 | } |
| 418 | |
| 419 | if (!wl_list_empty(&so->shm.free_buffers)) { |
| 420 | sb = container_of(so->shm.free_buffers.next, |
| 421 | struct ss_shm_buffer, free_link); |
| 422 | wl_list_remove(&sb->free_link); |
| 423 | wl_list_init(&sb->free_link); |
| 424 | |
| 425 | return sb; |
| 426 | } |
| 427 | |
| 428 | fd = os_create_anonymous_file(height * stride); |
| 429 | if (fd < 0) { |
| 430 | weston_log("os_create_anonymous_file: %m"); |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 435 | if (data == MAP_FAILED) { |
| 436 | weston_log("mmap: %m"); |
| 437 | close(fd); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | sb = zalloc(sizeof *sb); |
| 442 | |
| 443 | sb->output = so; |
| 444 | wl_list_init(&sb->free_link); |
| 445 | wl_list_insert(&so->shm.buffers, &sb->link); |
| 446 | |
| 447 | pixman_region32_init_rect(&sb->damage, 0, 0, width, height); |
| 448 | |
| 449 | sb->data = data; |
| 450 | sb->size = height * stride; |
| 451 | |
| 452 | pool = wl_shm_create_pool(so->parent.shm, fd, sb->size); |
| 453 | |
| 454 | sb->buffer = wl_shm_pool_create_buffer(pool, 0, |
| 455 | width, height, stride, |
| 456 | WL_SHM_FORMAT_ARGB8888); |
| 457 | wl_buffer_add_listener(sb->buffer, &buffer_listener, sb); |
| 458 | wl_shm_pool_destroy(pool); |
| 459 | close(fd); |
| 460 | |
| 461 | memset(data, 0, sb->size); |
| 462 | |
| 463 | sb->pm_image = |
| 464 | pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height, |
| 465 | (uint32_t *)data, stride); |
| 466 | |
| 467 | return sb; |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | output_compute_transform(struct weston_output *output, |
| 472 | pixman_transform_t *transform) |
| 473 | { |
| 474 | pixman_fixed_t fw, fh; |
| 475 | |
| 476 | pixman_transform_init_identity(transform); |
| 477 | |
| 478 | fw = pixman_int_to_fixed(output->width); |
| 479 | fh = pixman_int_to_fixed(output->height); |
| 480 | |
| 481 | switch (output->transform) { |
| 482 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 483 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 484 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 485 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 486 | pixman_transform_scale(transform, NULL, |
| 487 | pixman_int_to_fixed (-1), |
| 488 | pixman_int_to_fixed (1)); |
| 489 | pixman_transform_translate(transform, NULL, fw, 0); |
| 490 | } |
| 491 | |
| 492 | switch (output->transform) { |
| 493 | default: |
| 494 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 495 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 496 | break; |
| 497 | case WL_OUTPUT_TRANSFORM_90: |
| 498 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 499 | pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1); |
| 500 | pixman_transform_translate(transform, NULL, fh, 0); |
| 501 | break; |
| 502 | case WL_OUTPUT_TRANSFORM_180: |
| 503 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 504 | pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0); |
| 505 | pixman_transform_translate(transform, NULL, fw, fh); |
| 506 | break; |
| 507 | case WL_OUTPUT_TRANSFORM_270: |
| 508 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 509 | pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1); |
| 510 | pixman_transform_translate(transform, NULL, 0, fw); |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | pixman_transform_scale(transform, NULL, |
| 515 | pixman_fixed_1 * output->current_scale, |
| 516 | pixman_fixed_1 * output->current_scale); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | shared_output_destroy(struct shared_output *so); |
| 521 | |
| 522 | static int |
| 523 | shared_output_ensure_tmp_data(struct shared_output *so, |
| 524 | pixman_region32_t *region) |
| 525 | { |
| 526 | pixman_box32_t *ext; |
| 527 | size_t size; |
| 528 | |
| 529 | if (!pixman_region32_not_empty(region)) |
| 530 | return 0; |
| 531 | |
| 532 | ext = pixman_region32_extents(region); |
| 533 | |
| 534 | /* Damage is in output coordinates. |
| 535 | * |
| 536 | * We are multiplying by 4 because the temporary data needs to be able |
| 537 | * to store an 32 bit-per-pixel buffer. |
| 538 | */ |
| 539 | size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1) |
| 540 | * so->output->current_scale * so->output->current_scale; |
| 541 | |
| 542 | if (so->tmp_data != NULL && size <= so->tmp_data_size) |
| 543 | return 0; |
| 544 | |
| 545 | free(so->tmp_data); |
| 546 | so->tmp_data = malloc(size); |
| 547 | if (so->tmp_data == NULL) { |
| 548 | so->tmp_data_size = 0; |
| 549 | errno = ENOMEM; |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | so->tmp_data_size = size; |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static void |
| 559 | shared_output_update(struct shared_output *so); |
| 560 | |
| 561 | static void |
| 562 | shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time) |
| 563 | { |
| 564 | struct shared_output *so = data; |
| 565 | |
| 566 | if (cb != so->parent.frame_cb) |
| 567 | return; |
| 568 | |
| 569 | wl_callback_destroy(cb); |
| 570 | so->parent.frame_cb = NULL; |
| 571 | |
| 572 | shared_output_update(so); |
| 573 | } |
| 574 | |
| 575 | static const struct wl_callback_listener shared_output_frame_listener = { |
| 576 | shared_output_frame_callback |
| 577 | }; |
| 578 | |
| 579 | static void |
| 580 | shared_output_update(struct shared_output *so) |
| 581 | { |
| 582 | struct ss_shm_buffer *sb; |
| 583 | pixman_box32_t *r; |
| 584 | int i, nrects; |
| 585 | pixman_transform_t transform; |
| 586 | |
| 587 | /* Only update if we need to */ |
| 588 | if (!so->cache_dirty || so->parent.frame_cb) |
| 589 | return; |
| 590 | |
| 591 | sb = shared_output_get_shm_buffer(so); |
| 592 | if (sb == NULL) { |
| 593 | shared_output_destroy(so); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | output_compute_transform(so->output, &transform); |
| 598 | pixman_image_set_transform(so->cache_image, &transform); |
| 599 | |
| 600 | pixman_image_set_clip_region32(sb->pm_image, &sb->damage); |
| 601 | |
| 602 | if (so->output->current_scale == 1) { |
| 603 | pixman_image_set_filter(so->cache_image, |
| 604 | PIXMAN_FILTER_NEAREST, NULL, 0); |
| 605 | } else { |
| 606 | pixman_image_set_filter(so->cache_image, |
| 607 | PIXMAN_FILTER_BILINEAR, NULL, 0); |
| 608 | } |
| 609 | |
| 610 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 611 | so->cache_image, /* src */ |
| 612 | NULL, /* mask */ |
| 613 | sb->pm_image, /* dest */ |
| 614 | 0, 0, /* src_x, src_y */ |
| 615 | 0, 0, /* mask_x, mask_y */ |
| 616 | 0, 0, /* dest_x, dest_y */ |
| 617 | so->output->width, /* width */ |
| 618 | so->output->height /* height */); |
| 619 | |
| 620 | pixman_image_set_transform(sb->pm_image, NULL); |
| 621 | pixman_image_set_clip_region32(sb->pm_image, NULL); |
| 622 | |
| 623 | r = pixman_region32_rectangles(&sb->damage, &nrects); |
| 624 | for (i = 0; i < nrects; ++i) |
| 625 | wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1, |
| 626 | r[i].x2 - r[i].x1, r[i].y2 - r[i].y1); |
| 627 | |
| 628 | wl_surface_attach(so->parent.surface, sb->buffer, 0, 0); |
| 629 | |
| 630 | so->parent.frame_cb = wl_surface_frame(so->parent.surface); |
| 631 | wl_callback_add_listener(so->parent.frame_cb, |
| 632 | &shared_output_frame_listener, so); |
| 633 | |
| 634 | wl_surface_commit(so->parent.surface); |
| 635 | wl_callback_destroy(wl_display_sync(so->parent.display)); |
| 636 | wl_display_flush(so->parent.display); |
| 637 | |
| 638 | /* Clear the buffer damage */ |
| 639 | pixman_region32_fini(&sb->damage); |
| 640 | pixman_region32_init(&sb->damage); |
| 641 | } |
| 642 | |
| 643 | static void |
| 644 | shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 645 | { |
| 646 | struct shared_output *so = data; |
| 647 | |
| 648 | so->parent.shm_formats |= (1 << format); |
| 649 | } |
| 650 | |
| 651 | struct wl_shm_listener shm_listener = { |
| 652 | shm_handle_format |
| 653 | }; |
| 654 | |
| 655 | static void |
| 656 | registry_handle_global(void *data, struct wl_registry *registry, |
| 657 | uint32_t id, const char *interface, uint32_t version) |
| 658 | { |
| 659 | struct shared_output *so = data; |
| 660 | |
| 661 | if (strcmp(interface, "wl_compositor") == 0) { |
| 662 | so->parent.compositor = |
| 663 | wl_registry_bind(registry, |
| 664 | id, &wl_compositor_interface, 1); |
| 665 | } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) { |
| 666 | so->parent.output = |
| 667 | wl_registry_bind(registry, |
| 668 | id, &wl_output_interface, 1); |
| 669 | } else if (strcmp(interface, "wl_seat") == 0) { |
| 670 | ss_seat_create(so, id); |
| 671 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 672 | so->parent.shm = |
| 673 | wl_registry_bind(registry, |
| 674 | id, &wl_shm_interface, 1); |
| 675 | wl_shm_add_listener(so->parent.shm, &shm_listener, so); |
| 676 | } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) { |
| 677 | so->parent.fshell = |
| 678 | wl_registry_bind(registry, |
| 679 | id, &_wl_fullscreen_shell_interface, 1); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | static void |
| 684 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 685 | uint32_t name) |
| 686 | { |
| 687 | } |
| 688 | |
| 689 | static const struct wl_registry_listener registry_listener = { |
| 690 | registry_handle_global, |
| 691 | registry_handle_global_remove |
| 692 | }; |
| 693 | |
| 694 | static int |
| 695 | shared_output_handle_event(int fd, uint32_t mask, void *data) |
| 696 | { |
| 697 | struct shared_output *so = data; |
| 698 | int count = 0; |
| 699 | |
| 700 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 701 | shared_output_destroy(so); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | if (mask & WL_EVENT_READABLE) |
| 706 | count = wl_display_dispatch(so->parent.display); |
| 707 | if (mask & WL_EVENT_WRITABLE) |
| 708 | wl_display_flush(so->parent.display); |
| 709 | |
| 710 | if (mask == 0) { |
| 711 | count = wl_display_dispatch_pending(so->parent.display); |
| 712 | wl_display_flush(so->parent.display); |
| 713 | } |
| 714 | |
| 715 | return count; |
| 716 | } |
| 717 | |
| 718 | static void |
| 719 | output_destroyed(struct wl_listener *l, void *data) |
| 720 | { |
| 721 | struct shared_output *so; |
| 722 | |
| 723 | so = container_of(l, struct shared_output, output_destroyed); |
| 724 | |
| 725 | shared_output_destroy(so); |
| 726 | } |
| 727 | |
| 728 | static void |
| 729 | mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb) |
| 730 | { |
| 731 | struct shared_output *so = data; |
| 732 | |
| 733 | _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback); |
| 734 | } |
| 735 | |
| 736 | static void |
| 737 | mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb) |
| 738 | { |
| 739 | struct shared_output *so = data; |
| 740 | |
| 741 | _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback); |
| 742 | |
| 743 | weston_log("Screen share failed: present_surface_for_mode failed\n"); |
| 744 | shared_output_destroy(so); |
| 745 | } |
| 746 | |
| 747 | struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = { |
| 748 | mode_feedback_ok, |
| 749 | mode_feedback_failed, |
| 750 | mode_feedback_ok, |
| 751 | }; |
| 752 | |
| 753 | static void |
| 754 | shared_output_repainted(struct wl_listener *listener, void *data) |
| 755 | { |
| 756 | struct shared_output *so = |
| 757 | container_of(listener, struct shared_output, frame_listener); |
| 758 | pixman_region32_t damage; |
| 759 | struct ss_shm_buffer *sb; |
| 760 | int32_t x, y, width, height, stride; |
| 761 | int i, nrects, do_yflip; |
| 762 | pixman_box32_t *r; |
| 763 | uint32_t *cache_data; |
| 764 | |
| 765 | /* Damage in output coordinates */ |
| 766 | pixman_region32_init(&damage); |
| 767 | pixman_region32_intersect(&damage, &so->output->region, |
| 768 | &so->output->previous_damage); |
| 769 | pixman_region32_translate(&damage, -so->output->x, -so->output->y); |
| 770 | |
| 771 | /* Apply damage to all buffers */ |
| 772 | wl_list_for_each(sb, &so->shm.buffers, link) |
| 773 | pixman_region32_union(&sb->damage, &sb->damage, &damage); |
| 774 | |
| 775 | /* Transform to buffer coordinates */ |
| 776 | weston_transformed_region(so->output->width, so->output->height, |
| 777 | so->output->transform, |
| 778 | so->output->current_scale, |
| 779 | &damage, &damage); |
| 780 | |
| 781 | width = so->output->current_mode->width; |
| 782 | height = so->output->current_mode->height; |
| 783 | stride = width; |
| 784 | |
| 785 | if (!so->cache_image || |
| 786 | pixman_image_get_width(so->cache_image) != width || |
| 787 | pixman_image_get_height(so->cache_image) != height) { |
| 788 | if (so->cache_image) |
| 789 | pixman_image_unref(so->cache_image); |
| 790 | |
| 791 | so->cache_image = |
| 792 | pixman_image_create_bits(PIXMAN_a8r8g8b8, |
| 793 | width, height, NULL, |
| 794 | stride); |
| 795 | if (!so->cache_image) { |
| 796 | shared_output_destroy(so); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | pixman_region32_fini(&damage); |
| 801 | pixman_region32_init_rect(&damage, 0, 0, width, height); |
| 802 | } |
| 803 | |
| 804 | if (shared_output_ensure_tmp_data(so, &damage) < 0) { |
| 805 | shared_output_destroy(so); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP); |
| 810 | |
| 811 | cache_data = pixman_image_get_data(so->cache_image); |
| 812 | r = pixman_region32_rectangles(&damage, &nrects); |
| 813 | for (i = 0; i < nrects; ++i) { |
| 814 | x = r[i].x1; |
| 815 | y = r[i].y1; |
| 816 | width = r[i].x2 - r[i].x1; |
| 817 | height = r[i].y2 - r[i].y1; |
| 818 | |
| 819 | if (do_yflip) { |
| 820 | so->output->compositor->renderer->read_pixels( |
| 821 | so->output, PIXMAN_a8r8g8b8, so->tmp_data, |
| 822 | x, so->output->current_mode->height - r[i].y2, |
| 823 | width, height); |
| 824 | |
| 825 | pixman_blt(so->tmp_data, cache_data, -width, stride, |
| 826 | 32, 32, 0, 1 - height, x, y, width, height); |
| 827 | } else { |
| 828 | so->output->compositor->renderer->read_pixels( |
| 829 | so->output, PIXMAN_a8r8g8b8, so->tmp_data, |
| 830 | x, y, width, height); |
| 831 | |
| 832 | pixman_blt(so->tmp_data, cache_data, width, stride, |
| 833 | 32, 32, 0, 0, x, y, width, height); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | pixman_region32_fini(&damage); |
| 838 | |
| 839 | so->cache_dirty = 1; |
| 840 | |
| 841 | shared_output_update(so); |
| 842 | } |
| 843 | |
| 844 | static struct shared_output * |
| 845 | shared_output_create(struct weston_output *output, int parent_fd) |
| 846 | { |
| 847 | struct shared_output *so; |
| 848 | struct wl_event_loop *loop; |
| 849 | struct ss_seat *seat; |
| 850 | int epoll_fd; |
| 851 | |
| 852 | so = zalloc(sizeof *so); |
| 853 | if (so == NULL) |
| 854 | goto err_close; |
| 855 | |
| 856 | wl_list_init(&so->seat_list); |
| 857 | |
| 858 | so->parent.display = wl_display_connect_to_fd(parent_fd); |
| 859 | if (!so->parent.display) |
| 860 | goto err_alloc; |
| 861 | |
| 862 | so->parent.registry = wl_display_get_registry(so->parent.display); |
| 863 | if (!so->parent.registry) |
| 864 | goto err_display; |
| 865 | wl_registry_add_listener(so->parent.registry, |
| 866 | ®istry_listener, so); |
| 867 | wl_display_roundtrip(so->parent.display); |
| 868 | if (so->parent.shm == NULL) { |
| 869 | weston_log("Screen share failed: No wl_shm found\n"); |
| 870 | goto err_display; |
| 871 | } |
| 872 | if (so->parent.fshell == NULL) { |
| 873 | weston_log("Screen share failed: " |
| 874 | "Parent does not support wl_fullscreen_shell\n"); |
| 875 | goto err_display; |
| 876 | } |
| 877 | if (so->parent.compositor == NULL) { |
| 878 | weston_log("Screen share failed: No wl_compositor found\n"); |
| 879 | goto err_display; |
| 880 | } |
| 881 | |
| 882 | /* Get SHM formats */ |
| 883 | wl_display_roundtrip(so->parent.display); |
| 884 | if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) { |
| 885 | weston_log("Screen share failed: " |
| 886 | "WL_SHM_FORMAT_XRGB8888 not available\n"); |
| 887 | goto err_display; |
| 888 | } |
| 889 | |
| 890 | so->parent.surface = |
| 891 | wl_compositor_create_surface(so->parent.compositor); |
| 892 | if (!so->parent.surface) { |
| 893 | weston_log("Screen share failed: %m"); |
| 894 | goto err_display; |
| 895 | } |
| 896 | |
| 897 | so->parent.mode_feedback = |
| 898 | _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell, |
| 899 | so->parent.surface, |
| 900 | so->parent.output, |
| 901 | output->current_mode->refresh); |
| 902 | if (!so->parent.mode_feedback) { |
| 903 | weston_log("Screen share failed: %m"); |
| 904 | goto err_display; |
| 905 | } |
| 906 | _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback, |
| 907 | &mode_feedback_listener, |
| 908 | so); |
| 909 | |
| 910 | loop = wl_display_get_event_loop(output->compositor->wl_display); |
| 911 | |
| 912 | epoll_fd = wl_display_get_fd(so->parent.display); |
| 913 | so->event_source = |
| 914 | wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE, |
| 915 | shared_output_handle_event, so); |
| 916 | if (!so->event_source) { |
| 917 | weston_log("Screen share failed: %m"); |
| 918 | goto err_display; |
| 919 | } |
| 920 | |
| 921 | /* Ok, everything's created. We should be good to go */ |
| 922 | wl_list_init(&so->shm.buffers); |
| 923 | wl_list_init(&so->shm.free_buffers); |
| 924 | |
| 925 | so->output = output; |
| 926 | so->output_destroyed.notify = output_destroyed; |
| 927 | wl_signal_add(&so->output->destroy_signal, &so->output_destroyed); |
| 928 | |
| 929 | so->frame_listener.notify = shared_output_repainted; |
| 930 | wl_signal_add(&output->frame_signal, &so->frame_listener); |
| 931 | output->disable_planes++; |
| 932 | weston_output_damage(output); |
| 933 | |
| 934 | return so; |
| 935 | |
| 936 | err_display: |
| 937 | wl_list_for_each(seat, &so->seat_list, link) |
| 938 | ss_seat_destroy(seat); |
| 939 | wl_display_disconnect(so->parent.display); |
| 940 | err_alloc: |
| 941 | free(so); |
| 942 | err_close: |
| 943 | close(parent_fd); |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | static void |
| 948 | shared_output_destroy(struct shared_output *so) |
| 949 | { |
| 950 | struct ss_shm_buffer *buffer, *bnext; |
| 951 | |
| 952 | wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link) |
| 953 | ss_shm_buffer_destroy(buffer); |
| 954 | wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, link) |
| 955 | ss_shm_buffer_destroy(buffer); |
| 956 | |
| 957 | wl_display_disconnect(so->parent.display); |
| 958 | wl_event_source_remove(so->event_source); |
| 959 | |
| 960 | wl_list_remove(&so->output_destroyed.link); |
| 961 | wl_list_remove(&so->frame_listener.link); |
| 962 | |
| 963 | pixman_image_unref(so->cache_image); |
| 964 | free(so->tmp_data); |
| 965 | |
| 966 | free(so); |
| 967 | } |
| 968 | |
| 969 | static struct shared_output * |
| 970 | weston_output_share(struct weston_output *output, |
| 971 | const char *path, char *const argv[]) |
| 972 | { |
| 973 | int sv[2]; |
| 974 | char str[32]; |
| 975 | pid_t pid; |
| 976 | sigset_t allsigs; |
| 977 | |
| 978 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
| 979 | weston_log("weston_output_share: socketpair failed: %m\n"); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | pid = fork(); |
| 984 | |
| 985 | if (pid == -1) { |
| 986 | close(sv[0]); |
| 987 | close(sv[1]); |
| 988 | weston_log("weston_output_share: fork failed: %m\n"); |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | if (pid == 0) { |
| 993 | /* We don't want anything circular */ |
| 994 | unsetenv("WAYLAND_DISPLAY"); |
| 995 | unsetenv("WAYLAND_SOCKET"); |
| 996 | |
| 997 | /* do not give our signal mask to the new process */ |
| 998 | sigfillset(&allsigs); |
| 999 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 1000 | |
| 1001 | /* Launch clients as the user. Do not launch clients with |
| 1002 | * wrong euid. */ |
| 1003 | if (seteuid(getuid()) == -1) { |
| 1004 | weston_log("weston_output_share: setuid failed: %m\n"); |
| 1005 | abort(); |
| 1006 | } |
| 1007 | |
| 1008 | sv[1] = dup(sv[1]); |
| 1009 | if (sv[1] == -1) { |
| 1010 | weston_log("weston_output_share: dup failed: %m\n"); |
| 1011 | abort(); |
| 1012 | } |
| 1013 | |
| 1014 | snprintf(str, sizeof str, "%d", sv[1]); |
| 1015 | setenv("WAYLAND_SERVER_SOCKET", str, 1); |
| 1016 | |
| 1017 | execv(path, argv); |
| 1018 | weston_log("weston_output_share: exec failed: %m\n"); |
| 1019 | abort(); |
| 1020 | } else { |
| 1021 | close(sv[1]); |
| 1022 | return shared_output_create(output, sv[0]); |
| 1023 | } |
| 1024 | |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | static struct weston_output * |
| 1029 | weston_output_find(struct weston_compositor *c, int32_t x, int32_t y) |
| 1030 | { |
| 1031 | struct weston_output *output; |
| 1032 | |
| 1033 | wl_list_for_each(output, &c->output_list, link) { |
| 1034 | if (x >= output->x && y >= output->y && |
| 1035 | x < output->x + output->width && |
| 1036 | y < output->y + output->height) |
| 1037 | return output; |
| 1038 | } |
| 1039 | |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | static void |
| 1044 | share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key, |
| 1045 | void *data) |
| 1046 | { |
| 1047 | struct weston_output *output; |
| 1048 | const char *path = BINDIR "/weston"; |
| 1049 | |
| 1050 | if (!seat->pointer) { |
| 1051 | weston_log("Cannot pick output: Seat does not have pointer\n"); |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | output = weston_output_find(seat->compositor, |
| 1056 | wl_fixed_to_int(seat->pointer->x), |
| 1057 | wl_fixed_to_int(seat->pointer->y)); |
| 1058 | if (!output) { |
| 1059 | weston_log("Cannot pick output: Pointer not on any output\n"); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | char *const argv[] = { |
| 1064 | "weston", |
| 1065 | "--backend=rdp-backend.so", |
| 1066 | "--shell=fullscreen-shell.so", |
| 1067 | "--no-clients-resize", |
| 1068 | NULL |
| 1069 | }; |
| 1070 | |
| 1071 | weston_output_share(output, path, argv); |
| 1072 | } |
| 1073 | |
| 1074 | WL_EXPORT int |
| 1075 | module_init(struct weston_compositor *compositor, |
| 1076 | int *argc, char *argv[]) |
| 1077 | { |
| 1078 | weston_compositor_add_key_binding(compositor, KEY_S, |
| 1079 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1080 | share_output_binding, compositor); |
| 1081 | return 0; |
| 1082 | } |