2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2014 Jason Ekstrand
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.
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.
30 #include <sys/socket.h>
33 #include <linux/input.h>
37 #include <wayland-client.h>
39 #include "compositor.h"
40 #include "../shared/os-compatibility.h"
41 #include "fullscreen-shell-client-protocol.h"
43 struct shared_output {
44 struct weston_output *output;
45 struct wl_listener output_destroyed;
46 struct wl_list seat_list;
49 struct wl_display *display;
50 struct wl_registry *registry;
51 struct wl_compositor *compositor;
54 struct _wl_fullscreen_shell *fshell;
55 struct wl_output *output;
56 struct wl_surface *surface;
57 struct wl_callback *frame_cb;
58 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
61 struct wl_event_source *event_source;
62 struct wl_listener frame_listener;
65 int32_t width, height;
67 struct wl_list buffers;
68 struct wl_list free_buffers;
72 pixman_image_t *cache_image;
78 struct weston_seat base;
79 struct shared_output *output;
84 struct wl_pointer *pointer;
85 struct wl_keyboard *keyboard;
88 enum weston_key_state_update keyboard_state_update;
92 struct ss_shm_buffer {
93 struct shared_output *output;
95 struct wl_list free_link;
97 struct wl_buffer *buffer;
100 pixman_region32_t damage;
102 pixman_image_t *pm_image;
105 struct screen_share {
106 struct weston_compositor *compositor;
111 ss_seat_handle_pointer_enter(void *data, struct wl_pointer *pointer,
112 uint32_t serial, struct wl_surface *surface,
113 wl_fixed_t x, wl_fixed_t y)
115 struct ss_seat *seat = data;
117 /* No transformation of input position is required here because we are
118 * always receiving the input in the same coordinates as the output. */
120 notify_pointer_focus(&seat->base, NULL, 0, 0);
124 ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer,
125 uint32_t serial, struct wl_surface *surface)
127 struct ss_seat *seat = data;
129 notify_pointer_focus(&seat->base, NULL, 0, 0);
133 ss_seat_handle_motion(void *data, struct wl_pointer *pointer,
134 uint32_t time, wl_fixed_t x, wl_fixed_t y)
136 struct ss_seat *seat = data;
138 /* No transformation of input position is required here because we are
139 * always receiving the input in the same coordinates as the output. */
141 notify_motion_absolute(&seat->base, time, x, y);
145 ss_seat_handle_button(void *data, struct wl_pointer *pointer,
146 uint32_t serial, uint32_t time, uint32_t button,
149 struct ss_seat *seat = data;
151 notify_button(&seat->base, time, button, state);
155 ss_seat_handle_axis(void *data, struct wl_pointer *pointer,
156 uint32_t time, uint32_t axis, wl_fixed_t value)
158 struct ss_seat *seat = data;
160 notify_axis(&seat->base, time, axis, value);
163 static const struct wl_pointer_listener ss_seat_pointer_listener = {
164 ss_seat_handle_pointer_enter,
165 ss_seat_handle_pointer_leave,
166 ss_seat_handle_motion,
167 ss_seat_handle_button,
172 ss_seat_handle_keymap(void *data, struct wl_keyboard *keyboard,
173 uint32_t format, int fd, uint32_t size)
175 struct ss_seat *seat = data;
176 struct xkb_keymap *keymap;
182 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
183 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
184 if (map_str == MAP_FAILED) {
185 weston_log("mmap failed: %m\n");
189 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
191 XKB_KEYMAP_FORMAT_TEXT_V1,
193 munmap(map_str, size);
196 weston_log("failed to compile keymap\n");
200 seat->keyboard_state_update = STATE_UPDATE_NONE;
201 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
202 weston_log("No keymap provided; falling back to default\n");
204 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
206 weston_log("Invalid keymap\n");
212 if (seat->base.keyboard)
213 weston_seat_update_keymap(&seat->base, keymap);
215 weston_seat_init_keyboard(&seat->base, keymap);
218 xkb_keymap_unref(keymap);
223 wl_keyboard_release(seat->parent.keyboard);
228 ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
229 uint32_t serial, struct wl_surface *surface,
230 struct wl_array *keys)
232 struct ss_seat *seat = data;
234 /* XXX: If we get a modifier event immediately before the focus,
235 * we should try to keep the same serial. */
236 notify_keyboard_focus_in(&seat->base, keys,
237 STATE_UPDATE_AUTOMATIC);
241 ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
242 uint32_t serial, struct wl_surface *surface)
244 struct ss_seat *seat = data;
246 notify_keyboard_focus_out(&seat->base);
250 ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
251 uint32_t serial, uint32_t time,
252 uint32_t key, uint32_t state)
254 struct ss_seat *seat = data;
256 seat->key_serial = serial;
257 notify_key(&seat->base, time, key,
258 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
259 WL_KEYBOARD_KEY_STATE_RELEASED,
260 seat->keyboard_state_update);
264 ss_seat_handle_modifiers(void *data, struct wl_keyboard *keyboard,
265 uint32_t serial_in, uint32_t mods_depressed,
266 uint32_t mods_latched, uint32_t mods_locked,
269 struct ss_seat *seat = data;
270 struct weston_compositor *c = seat->output->output->compositor;
273 /* If we get a key event followed by a modifier event with the
274 * same serial number, then we try to preserve those semantics by
275 * reusing the same serial number on the way out too. */
276 if (serial_in == seat->key_serial)
277 serial_out = wl_display_get_serial(c->wl_display);
279 serial_out = wl_display_next_serial(c->wl_display);
281 xkb_state_update_mask(seat->base.keyboard->xkb_state.state,
282 mods_depressed, mods_latched,
283 mods_locked, 0, 0, group);
284 notify_modifiers(&seat->base, serial_out);
287 static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
288 ss_seat_handle_keymap,
289 ss_seat_handle_keyboard_enter,
290 ss_seat_handle_keyboard_leave,
292 ss_seat_handle_modifiers,
296 ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
297 enum wl_seat_capability caps)
299 struct ss_seat *ss_seat = data;
301 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
302 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
303 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
304 wl_pointer_add_listener(ss_seat->parent.pointer,
305 &ss_seat_pointer_listener, ss_seat);
306 weston_seat_init_pointer(&ss_seat->base);
307 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
308 wl_pointer_destroy(ss_seat->parent.pointer);
309 ss_seat->parent.pointer = NULL;
312 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
313 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
314 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
315 wl_keyboard_add_listener(ss_seat->parent.keyboard,
316 &ss_seat_keyboard_listener, ss_seat);
317 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
318 wl_keyboard_destroy(ss_seat->parent.keyboard);
319 ss_seat->parent.keyboard = NULL;
323 static const struct wl_seat_listener ss_seat_listener = {
324 ss_seat_handle_capabilities,
327 static struct ss_seat *
328 ss_seat_create(struct shared_output *so, uint32_t id)
330 struct ss_seat *seat;
332 seat = zalloc(sizeof *seat);
336 weston_seat_init(&seat->base, so->output->compositor, "default");
338 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
339 &wl_seat_interface, 1);
340 wl_list_insert(so->seat_list.prev, &seat->link);
342 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
343 wl_seat_set_user_data(seat->parent.seat, seat);
349 ss_seat_destroy(struct ss_seat *seat)
351 if (seat->parent.pointer)
352 wl_pointer_release(seat->parent.pointer);
353 if (seat->parent.keyboard)
354 wl_keyboard_release(seat->parent.keyboard);
355 wl_seat_destroy(seat->parent.seat);
357 wl_list_remove(&seat->link);
359 weston_seat_release(&seat->base);
365 ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
367 pixman_image_unref(buffer->pm_image);
369 wl_buffer_destroy(buffer->buffer);
370 munmap(buffer->data, buffer->size);
372 pixman_region32_fini(&buffer->damage);
374 wl_list_remove(&buffer->link);
375 wl_list_remove(&buffer->free_link);
380 buffer_release(void *data, struct wl_buffer *buffer)
382 struct ss_shm_buffer *sb = data;
385 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
387 ss_shm_buffer_destroy(sb);
391 static const struct wl_buffer_listener buffer_listener = {
395 static struct ss_shm_buffer *
396 shared_output_get_shm_buffer(struct shared_output *so)
398 struct ss_shm_buffer *sb, *bnext;
399 struct wl_shm_pool *pool;
400 int width, height, stride;
404 width = so->output->width;
405 height = so->output->height;
408 /* If the size of the output changed, we free the old buffers and
410 if (so->shm.width != width ||
411 so->shm.height != height) {
413 /* Destroy free buffers */
414 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link)
415 ss_shm_buffer_destroy(sb);
417 /* Orphan in-use buffers so they get destroyed */
418 wl_list_for_each(sb, &so->shm.buffers, link)
421 so->shm.width = width;
422 so->shm.height = height;
425 if (!wl_list_empty(&so->shm.free_buffers)) {
426 sb = container_of(so->shm.free_buffers.next,
427 struct ss_shm_buffer, free_link);
428 wl_list_remove(&sb->free_link);
429 wl_list_init(&sb->free_link);
434 fd = os_create_anonymous_file(height * stride);
436 weston_log("os_create_anonymous_file: %m");
440 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
441 if (data == MAP_FAILED) {
442 weston_log("mmap: %m");
446 sb = zalloc(sizeof *sb);
451 wl_list_init(&sb->free_link);
452 wl_list_insert(&so->shm.buffers, &sb->link);
454 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
457 sb->size = height * stride;
459 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
461 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
462 width, height, stride,
463 WL_SHM_FORMAT_ARGB8888);
464 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
465 wl_shm_pool_destroy(pool);
469 memset(data, 0, sb->size);
472 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
473 (uint32_t *)data, stride);
475 goto out_pixman_error;
480 pixman_region32_fini(&sb->damage);
482 munmap(data, height * stride);
490 output_compute_transform(struct weston_output *output,
491 pixman_transform_t *transform)
493 pixman_fixed_t fw, fh;
495 pixman_transform_init_identity(transform);
497 fw = pixman_int_to_fixed(output->width);
498 fh = pixman_int_to_fixed(output->height);
500 switch (output->transform) {
501 case WL_OUTPUT_TRANSFORM_FLIPPED:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
504 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
505 pixman_transform_scale(transform, NULL,
506 pixman_int_to_fixed (-1),
507 pixman_int_to_fixed (1));
508 pixman_transform_translate(transform, NULL, fw, 0);
511 switch (output->transform) {
513 case WL_OUTPUT_TRANSFORM_NORMAL:
514 case WL_OUTPUT_TRANSFORM_FLIPPED:
516 case WL_OUTPUT_TRANSFORM_90:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
518 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
519 pixman_transform_translate(transform, NULL, fh, 0);
521 case WL_OUTPUT_TRANSFORM_180:
522 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
523 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
524 pixman_transform_translate(transform, NULL, fw, fh);
526 case WL_OUTPUT_TRANSFORM_270:
527 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
528 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
529 pixman_transform_translate(transform, NULL, 0, fw);
533 pixman_transform_scale(transform, NULL,
534 pixman_fixed_1 * output->current_scale,
535 pixman_fixed_1 * output->current_scale);
539 shared_output_destroy(struct shared_output *so);
542 shared_output_ensure_tmp_data(struct shared_output *so,
543 pixman_region32_t *region)
548 if (!pixman_region32_not_empty(region))
551 ext = pixman_region32_extents(region);
553 /* Damage is in output coordinates.
555 * We are multiplying by 4 because the temporary data needs to be able
556 * to store an 32 bit-per-pixel buffer.
558 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
559 * so->output->current_scale * so->output->current_scale;
561 if (so->tmp_data != NULL && size <= so->tmp_data_size)
565 so->tmp_data = malloc(size);
566 if (so->tmp_data == NULL) {
567 so->tmp_data_size = 0;
572 so->tmp_data_size = size;
578 shared_output_update(struct shared_output *so);
581 shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
583 struct shared_output *so = data;
585 if (cb != so->parent.frame_cb)
588 wl_callback_destroy(cb);
589 so->parent.frame_cb = NULL;
591 shared_output_update(so);
594 static const struct wl_callback_listener shared_output_frame_listener = {
595 shared_output_frame_callback
599 shared_output_update(struct shared_output *so)
601 struct ss_shm_buffer *sb;
604 pixman_transform_t transform;
606 /* Only update if we need to */
607 if (!so->cache_dirty || so->parent.frame_cb)
610 sb = shared_output_get_shm_buffer(so);
612 shared_output_destroy(so);
616 output_compute_transform(so->output, &transform);
617 pixman_image_set_transform(so->cache_image, &transform);
619 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
621 if (so->output->current_scale == 1) {
622 pixman_image_set_filter(so->cache_image,
623 PIXMAN_FILTER_NEAREST, NULL, 0);
625 pixman_image_set_filter(so->cache_image,
626 PIXMAN_FILTER_BILINEAR, NULL, 0);
629 pixman_image_composite32(PIXMAN_OP_SRC,
630 so->cache_image, /* src */
632 sb->pm_image, /* dest */
633 0, 0, /* src_x, src_y */
634 0, 0, /* mask_x, mask_y */
635 0, 0, /* dest_x, dest_y */
636 so->output->width, /* width */
637 so->output->height /* height */);
639 pixman_image_set_transform(sb->pm_image, NULL);
640 pixman_image_set_clip_region32(sb->pm_image, NULL);
642 r = pixman_region32_rectangles(&sb->damage, &nrects);
643 for (i = 0; i < nrects; ++i)
644 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
645 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
647 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
649 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
650 wl_callback_add_listener(so->parent.frame_cb,
651 &shared_output_frame_listener, so);
653 wl_surface_commit(so->parent.surface);
654 wl_callback_destroy(wl_display_sync(so->parent.display));
655 wl_display_flush(so->parent.display);
657 /* Clear the buffer damage */
658 pixman_region32_fini(&sb->damage);
659 pixman_region32_init(&sb->damage);
663 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
665 struct shared_output *so = data;
667 so->parent.shm_formats |= (1 << format);
670 struct wl_shm_listener shm_listener = {
675 registry_handle_global(void *data, struct wl_registry *registry,
676 uint32_t id, const char *interface, uint32_t version)
678 struct shared_output *so = data;
680 if (strcmp(interface, "wl_compositor") == 0) {
681 so->parent.compositor =
682 wl_registry_bind(registry,
683 id, &wl_compositor_interface, 1);
684 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
686 wl_registry_bind(registry,
687 id, &wl_output_interface, 1);
688 } else if (strcmp(interface, "wl_seat") == 0) {
689 ss_seat_create(so, id);
690 } else if (strcmp(interface, "wl_shm") == 0) {
692 wl_registry_bind(registry,
693 id, &wl_shm_interface, 1);
694 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
695 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
697 wl_registry_bind(registry,
698 id, &_wl_fullscreen_shell_interface, 1);
703 registry_handle_global_remove(void *data, struct wl_registry *registry,
708 static const struct wl_registry_listener registry_listener = {
709 registry_handle_global,
710 registry_handle_global_remove
714 shared_output_handle_event(int fd, uint32_t mask, void *data)
716 struct shared_output *so = data;
719 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
720 shared_output_destroy(so);
724 if (mask & WL_EVENT_READABLE)
725 count = wl_display_dispatch(so->parent.display);
726 if (mask & WL_EVENT_WRITABLE)
727 wl_display_flush(so->parent.display);
730 count = wl_display_dispatch_pending(so->parent.display);
731 wl_display_flush(so->parent.display);
738 output_destroyed(struct wl_listener *l, void *data)
740 struct shared_output *so;
742 so = container_of(l, struct shared_output, output_destroyed);
744 shared_output_destroy(so);
748 mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
750 struct shared_output *so = data;
752 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
756 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
758 struct shared_output *so = data;
760 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
762 weston_log("Screen share failed: present_surface_for_mode failed\n");
763 shared_output_destroy(so);
766 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
768 mode_feedback_failed,
773 shared_output_repainted(struct wl_listener *listener, void *data)
775 struct shared_output *so =
776 container_of(listener, struct shared_output, frame_listener);
777 pixman_region32_t damage;
778 struct ss_shm_buffer *sb;
779 int32_t x, y, width, height, stride;
780 int i, nrects, do_yflip;
782 uint32_t *cache_data;
784 /* Damage in output coordinates */
785 pixman_region32_init(&damage);
786 pixman_region32_intersect(&damage, &so->output->region,
787 &so->output->previous_damage);
788 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
790 /* Apply damage to all buffers */
791 wl_list_for_each(sb, &so->shm.buffers, link)
792 pixman_region32_union(&sb->damage, &sb->damage, &damage);
794 /* Transform to buffer coordinates */
795 weston_transformed_region(so->output->width, so->output->height,
796 so->output->transform,
797 so->output->current_scale,
800 width = so->output->current_mode->width;
801 height = so->output->current_mode->height;
804 if (!so->cache_image ||
805 pixman_image_get_width(so->cache_image) != width ||
806 pixman_image_get_height(so->cache_image) != height) {
808 pixman_image_unref(so->cache_image);
811 pixman_image_create_bits(PIXMAN_a8r8g8b8,
814 if (!so->cache_image) {
815 shared_output_destroy(so);
819 pixman_region32_fini(&damage);
820 pixman_region32_init_rect(&damage, 0, 0, width, height);
823 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
824 shared_output_destroy(so);
828 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
830 cache_data = pixman_image_get_data(so->cache_image);
831 r = pixman_region32_rectangles(&damage, &nrects);
832 for (i = 0; i < nrects; ++i) {
835 width = r[i].x2 - r[i].x1;
836 height = r[i].y2 - r[i].y1;
839 so->output->compositor->renderer->read_pixels(
840 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
841 x, so->output->current_mode->height - r[i].y2,
844 pixman_blt(so->tmp_data, cache_data, -width, stride,
845 32, 32, 0, 1 - height, x, y, width, height);
847 so->output->compositor->renderer->read_pixels(
848 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
849 x, y, width, height);
851 pixman_blt(so->tmp_data, cache_data, width, stride,
852 32, 32, 0, 0, x, y, width, height);
856 pixman_region32_fini(&damage);
860 shared_output_update(so);
863 static struct shared_output *
864 shared_output_create(struct weston_output *output, int parent_fd)
866 struct shared_output *so;
867 struct wl_event_loop *loop;
868 struct ss_seat *seat;
871 so = zalloc(sizeof *so);
875 wl_list_init(&so->seat_list);
877 so->parent.display = wl_display_connect_to_fd(parent_fd);
878 if (!so->parent.display)
881 so->parent.registry = wl_display_get_registry(so->parent.display);
882 if (!so->parent.registry)
884 wl_registry_add_listener(so->parent.registry,
885 ®istry_listener, so);
886 wl_display_roundtrip(so->parent.display);
887 if (so->parent.shm == NULL) {
888 weston_log("Screen share failed: No wl_shm found\n");
891 if (so->parent.fshell == NULL) {
892 weston_log("Screen share failed: "
893 "Parent does not support wl_fullscreen_shell\n");
896 if (so->parent.compositor == NULL) {
897 weston_log("Screen share failed: No wl_compositor found\n");
901 /* Get SHM formats */
902 wl_display_roundtrip(so->parent.display);
903 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
904 weston_log("Screen share failed: "
905 "WL_SHM_FORMAT_XRGB8888 not available\n");
910 wl_compositor_create_surface(so->parent.compositor);
911 if (!so->parent.surface) {
912 weston_log("Screen share failed: %m");
916 so->parent.mode_feedback =
917 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
920 output->current_mode->refresh);
921 if (!so->parent.mode_feedback) {
922 weston_log("Screen share failed: %m");
925 _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
926 &mode_feedback_listener,
929 loop = wl_display_get_event_loop(output->compositor->wl_display);
931 epoll_fd = wl_display_get_fd(so->parent.display);
933 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
934 shared_output_handle_event, so);
935 if (!so->event_source) {
936 weston_log("Screen share failed: %m");
940 /* Ok, everything's created. We should be good to go */
941 wl_list_init(&so->shm.buffers);
942 wl_list_init(&so->shm.free_buffers);
945 so->output_destroyed.notify = output_destroyed;
946 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
948 so->frame_listener.notify = shared_output_repainted;
949 wl_signal_add(&output->frame_signal, &so->frame_listener);
950 output->disable_planes++;
951 weston_output_damage(output);
956 wl_list_for_each(seat, &so->seat_list, link)
957 ss_seat_destroy(seat);
958 wl_display_disconnect(so->parent.display);
967 shared_output_destroy(struct shared_output *so)
969 struct ss_shm_buffer *buffer, *bnext;
971 so->output->disable_planes--;
973 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
974 ss_shm_buffer_destroy(buffer);
975 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, link)
976 ss_shm_buffer_destroy(buffer);
978 wl_display_disconnect(so->parent.display);
979 wl_event_source_remove(so->event_source);
981 wl_list_remove(&so->output_destroyed.link);
982 wl_list_remove(&so->frame_listener.link);
984 pixman_image_unref(so->cache_image);
990 static struct shared_output *
991 weston_output_share(struct weston_output *output, const char* command)
997 char *const argv[] = {
1004 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1005 weston_log("weston_output_share: socketpair failed: %m\n");
1014 weston_log("weston_output_share: fork failed: %m\n");
1019 /* do not give our signal mask to the new process */
1020 sigfillset(&allsigs);
1021 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1023 /* Launch clients as the user. Do not launch clients with
1025 if (seteuid(getuid()) == -1) {
1026 weston_log("weston_output_share: setuid failed: %m\n");
1032 weston_log("weston_output_share: dup failed: %m\n");
1036 snprintf(str, sizeof str, "%d", sv[1]);
1037 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1039 execv(argv[0], argv);
1040 weston_log("weston_output_share: exec failed: %m\n");
1044 return shared_output_create(output, sv[0]);
1050 static struct weston_output *
1051 weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1053 struct weston_output *output;
1055 wl_list_for_each(output, &c->output_list, link) {
1056 if (x >= output->x && y >= output->y &&
1057 x < output->x + output->width &&
1058 y < output->y + output->height)
1066 share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1069 struct weston_output *output;
1070 struct screen_share *ss = data;
1072 if (!seat->pointer) {
1073 weston_log("Cannot pick output: Seat does not have pointer\n");
1077 output = weston_output_find(seat->compositor,
1078 wl_fixed_to_int(seat->pointer->x),
1079 wl_fixed_to_int(seat->pointer->y));
1081 weston_log("Cannot pick output: Pointer not on any output\n");
1085 weston_output_share(output, ss->command);
1089 module_init(struct weston_compositor *compositor,
1090 int *argc, char *argv[])
1092 struct screen_share *ss;
1093 struct weston_config_section *section;
1095 ss = zalloc(sizeof *ss);
1098 ss->compositor = compositor;
1100 section = weston_config_get_section(compositor->config, "screen-share",
1103 weston_config_section_get_string(section, "command", &ss->command, "");
1105 weston_compositor_add_key_binding(compositor, KEY_S,
1106 MODIFIER_CTRL | MODIFIER_ALT,
1107 share_output_binding, ss);