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);
217 xkb_keymap_unref(keymap);
222 wl_keyboard_release(seat->parent.keyboard);
227 ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
228 uint32_t serial, struct wl_surface *surface,
229 struct wl_array *keys)
231 struct ss_seat *seat = data;
233 /* XXX: If we get a modifier event immediately before the focus,
234 * we should try to keep the same serial. */
235 notify_keyboard_focus_in(&seat->base, keys,
236 STATE_UPDATE_AUTOMATIC);
240 ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
241 uint32_t serial, struct wl_surface *surface)
243 struct ss_seat *seat = data;
245 notify_keyboard_focus_out(&seat->base);
249 ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
250 uint32_t serial, uint32_t time,
251 uint32_t key, uint32_t state)
253 struct ss_seat *seat = data;
255 seat->key_serial = serial;
256 notify_key(&seat->base, time, key,
257 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
258 WL_KEYBOARD_KEY_STATE_RELEASED,
259 seat->keyboard_state_update);
263 ss_seat_handle_modifiers(void *data, struct wl_keyboard *keyboard,
264 uint32_t serial_in, uint32_t mods_depressed,
265 uint32_t mods_latched, uint32_t mods_locked,
268 struct ss_seat *seat = data;
269 struct weston_compositor *c = seat->output->output->compositor;
272 /* If we get a key event followed by a modifier event with the
273 * same serial number, then we try to preserve those semantics by
274 * reusing the same serial number on the way out too. */
275 if (serial_in == seat->key_serial)
276 serial_out = wl_display_get_serial(c->wl_display);
278 serial_out = wl_display_next_serial(c->wl_display);
280 xkb_state_update_mask(seat->base.keyboard->xkb_state.state,
281 mods_depressed, mods_latched,
282 mods_locked, 0, 0, group);
283 notify_modifiers(&seat->base, serial_out);
286 static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
287 ss_seat_handle_keymap,
288 ss_seat_handle_keyboard_enter,
289 ss_seat_handle_keyboard_leave,
291 ss_seat_handle_modifiers,
295 ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
296 enum wl_seat_capability caps)
298 struct ss_seat *ss_seat = data;
300 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
301 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
302 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
303 wl_pointer_add_listener(ss_seat->parent.pointer,
304 &ss_seat_pointer_listener, ss_seat);
305 weston_seat_init_pointer(&ss_seat->base);
306 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
307 wl_pointer_destroy(ss_seat->parent.pointer);
308 ss_seat->parent.pointer = NULL;
311 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
312 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
313 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
314 wl_keyboard_add_listener(ss_seat->parent.keyboard,
315 &ss_seat_keyboard_listener, ss_seat);
316 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
317 wl_keyboard_destroy(ss_seat->parent.keyboard);
318 ss_seat->parent.keyboard = NULL;
322 static const struct wl_seat_listener ss_seat_listener = {
323 ss_seat_handle_capabilities,
326 static struct ss_seat *
327 ss_seat_create(struct shared_output *so, uint32_t id)
329 struct ss_seat *seat;
331 seat = zalloc(sizeof *seat);
335 weston_seat_init(&seat->base, so->output->compositor, "default");
337 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
338 &wl_seat_interface, 1);
339 wl_list_insert(so->seat_list.prev, &seat->link);
341 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
342 wl_seat_set_user_data(seat->parent.seat, seat);
348 ss_seat_destroy(struct ss_seat *seat)
350 if (seat->parent.pointer)
351 wl_pointer_release(seat->parent.pointer);
352 if (seat->parent.keyboard)
353 wl_keyboard_release(seat->parent.keyboard);
354 wl_seat_destroy(seat->parent.seat);
356 wl_list_remove(&seat->link);
358 weston_seat_release(&seat->base);
364 ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
366 pixman_image_unref(buffer->pm_image);
368 wl_buffer_destroy(buffer->buffer);
369 munmap(buffer->data, buffer->size);
371 pixman_region32_fini(&buffer->damage);
373 wl_list_remove(&buffer->link);
374 wl_list_remove(&buffer->free_link);
379 buffer_release(void *data, struct wl_buffer *buffer)
381 struct ss_shm_buffer *sb = data;
384 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
386 ss_shm_buffer_destroy(sb);
390 static const struct wl_buffer_listener buffer_listener = {
394 static struct ss_shm_buffer *
395 shared_output_get_shm_buffer(struct shared_output *so)
397 struct ss_shm_buffer *sb, *bnext;
398 struct wl_shm_pool *pool;
399 int width, height, stride;
403 width = so->output->width;
404 height = so->output->height;
407 /* If the size of the output changed, we free the old buffers and
409 if (so->shm.width != width ||
410 so->shm.height != height) {
412 /* Destroy free buffers */
413 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link)
414 ss_shm_buffer_destroy(sb);
416 /* Orphan in-use buffers so they get destroyed */
417 wl_list_for_each(sb, &so->shm.buffers, link)
420 so->shm.width = width;
421 so->shm.height = height;
424 if (!wl_list_empty(&so->shm.free_buffers)) {
425 sb = container_of(so->shm.free_buffers.next,
426 struct ss_shm_buffer, free_link);
427 wl_list_remove(&sb->free_link);
428 wl_list_init(&sb->free_link);
433 fd = os_create_anonymous_file(height * stride);
435 weston_log("os_create_anonymous_file: %m");
439 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
440 if (data == MAP_FAILED) {
441 weston_log("mmap: %m");
445 sb = zalloc(sizeof *sb);
450 wl_list_init(&sb->free_link);
451 wl_list_insert(&so->shm.buffers, &sb->link);
453 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
456 sb->size = height * stride;
458 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
460 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
461 width, height, stride,
462 WL_SHM_FORMAT_ARGB8888);
463 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
464 wl_shm_pool_destroy(pool);
468 memset(data, 0, sb->size);
471 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
472 (uint32_t *)data, stride);
474 goto out_pixman_error;
479 pixman_region32_fini(&sb->damage);
481 munmap(data, height * stride);
489 output_compute_transform(struct weston_output *output,
490 pixman_transform_t *transform)
492 pixman_fixed_t fw, fh;
494 pixman_transform_init_identity(transform);
496 fw = pixman_int_to_fixed(output->width);
497 fh = pixman_int_to_fixed(output->height);
499 switch (output->transform) {
500 case WL_OUTPUT_TRANSFORM_FLIPPED:
501 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 pixman_transform_scale(transform, NULL,
505 pixman_int_to_fixed (-1),
506 pixman_int_to_fixed (1));
507 pixman_transform_translate(transform, NULL, fw, 0);
510 switch (output->transform) {
512 case WL_OUTPUT_TRANSFORM_NORMAL:
513 case WL_OUTPUT_TRANSFORM_FLIPPED:
515 case WL_OUTPUT_TRANSFORM_90:
516 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
517 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
518 pixman_transform_translate(transform, NULL, fh, 0);
520 case WL_OUTPUT_TRANSFORM_180:
521 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
522 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
523 pixman_transform_translate(transform, NULL, fw, fh);
525 case WL_OUTPUT_TRANSFORM_270:
526 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
527 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
528 pixman_transform_translate(transform, NULL, 0, fw);
532 pixman_transform_scale(transform, NULL,
533 pixman_fixed_1 * output->current_scale,
534 pixman_fixed_1 * output->current_scale);
538 shared_output_destroy(struct shared_output *so);
541 shared_output_ensure_tmp_data(struct shared_output *so,
542 pixman_region32_t *region)
547 if (!pixman_region32_not_empty(region))
550 ext = pixman_region32_extents(region);
552 /* Damage is in output coordinates.
554 * We are multiplying by 4 because the temporary data needs to be able
555 * to store an 32 bit-per-pixel buffer.
557 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
558 * so->output->current_scale * so->output->current_scale;
560 if (so->tmp_data != NULL && size <= so->tmp_data_size)
564 so->tmp_data = malloc(size);
565 if (so->tmp_data == NULL) {
566 so->tmp_data_size = 0;
571 so->tmp_data_size = size;
577 shared_output_update(struct shared_output *so);
580 shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
582 struct shared_output *so = data;
584 if (cb != so->parent.frame_cb)
587 wl_callback_destroy(cb);
588 so->parent.frame_cb = NULL;
590 shared_output_update(so);
593 static const struct wl_callback_listener shared_output_frame_listener = {
594 shared_output_frame_callback
598 shared_output_update(struct shared_output *so)
600 struct ss_shm_buffer *sb;
603 pixman_transform_t transform;
605 /* Only update if we need to */
606 if (!so->cache_dirty || so->parent.frame_cb)
609 sb = shared_output_get_shm_buffer(so);
611 shared_output_destroy(so);
615 output_compute_transform(so->output, &transform);
616 pixman_image_set_transform(so->cache_image, &transform);
618 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
620 if (so->output->current_scale == 1) {
621 pixman_image_set_filter(so->cache_image,
622 PIXMAN_FILTER_NEAREST, NULL, 0);
624 pixman_image_set_filter(so->cache_image,
625 PIXMAN_FILTER_BILINEAR, NULL, 0);
628 pixman_image_composite32(PIXMAN_OP_SRC,
629 so->cache_image, /* src */
631 sb->pm_image, /* dest */
632 0, 0, /* src_x, src_y */
633 0, 0, /* mask_x, mask_y */
634 0, 0, /* dest_x, dest_y */
635 so->output->width, /* width */
636 so->output->height /* height */);
638 pixman_image_set_transform(sb->pm_image, NULL);
639 pixman_image_set_clip_region32(sb->pm_image, NULL);
641 r = pixman_region32_rectangles(&sb->damage, &nrects);
642 for (i = 0; i < nrects; ++i)
643 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
644 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
646 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
648 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
649 wl_callback_add_listener(so->parent.frame_cb,
650 &shared_output_frame_listener, so);
652 wl_surface_commit(so->parent.surface);
653 wl_callback_destroy(wl_display_sync(so->parent.display));
654 wl_display_flush(so->parent.display);
656 /* Clear the buffer damage */
657 pixman_region32_fini(&sb->damage);
658 pixman_region32_init(&sb->damage);
662 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
664 struct shared_output *so = data;
666 so->parent.shm_formats |= (1 << format);
669 struct wl_shm_listener shm_listener = {
674 registry_handle_global(void *data, struct wl_registry *registry,
675 uint32_t id, const char *interface, uint32_t version)
677 struct shared_output *so = data;
679 if (strcmp(interface, "wl_compositor") == 0) {
680 so->parent.compositor =
681 wl_registry_bind(registry,
682 id, &wl_compositor_interface, 1);
683 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
685 wl_registry_bind(registry,
686 id, &wl_output_interface, 1);
687 } else if (strcmp(interface, "wl_seat") == 0) {
688 ss_seat_create(so, id);
689 } else if (strcmp(interface, "wl_shm") == 0) {
691 wl_registry_bind(registry,
692 id, &wl_shm_interface, 1);
693 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
694 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
696 wl_registry_bind(registry,
697 id, &_wl_fullscreen_shell_interface, 1);
702 registry_handle_global_remove(void *data, struct wl_registry *registry,
707 static const struct wl_registry_listener registry_listener = {
708 registry_handle_global,
709 registry_handle_global_remove
713 shared_output_handle_event(int fd, uint32_t mask, void *data)
715 struct shared_output *so = data;
718 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
719 shared_output_destroy(so);
723 if (mask & WL_EVENT_READABLE)
724 count = wl_display_dispatch(so->parent.display);
725 if (mask & WL_EVENT_WRITABLE)
726 wl_display_flush(so->parent.display);
729 count = wl_display_dispatch_pending(so->parent.display);
730 wl_display_flush(so->parent.display);
737 output_destroyed(struct wl_listener *l, void *data)
739 struct shared_output *so;
741 so = container_of(l, struct shared_output, output_destroyed);
743 shared_output_destroy(so);
747 mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
749 struct shared_output *so = data;
751 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
755 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
757 struct shared_output *so = data;
759 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
761 weston_log("Screen share failed: present_surface_for_mode failed\n");
762 shared_output_destroy(so);
765 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
767 mode_feedback_failed,
772 shared_output_repainted(struct wl_listener *listener, void *data)
774 struct shared_output *so =
775 container_of(listener, struct shared_output, frame_listener);
776 pixman_region32_t damage;
777 struct ss_shm_buffer *sb;
778 int32_t x, y, width, height, stride;
779 int i, nrects, do_yflip;
781 uint32_t *cache_data;
783 /* Damage in output coordinates */
784 pixman_region32_init(&damage);
785 pixman_region32_intersect(&damage, &so->output->region,
786 &so->output->previous_damage);
787 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
789 /* Apply damage to all buffers */
790 wl_list_for_each(sb, &so->shm.buffers, link)
791 pixman_region32_union(&sb->damage, &sb->damage, &damage);
793 /* Transform to buffer coordinates */
794 weston_transformed_region(so->output->width, so->output->height,
795 so->output->transform,
796 so->output->current_scale,
799 width = so->output->current_mode->width;
800 height = so->output->current_mode->height;
803 if (!so->cache_image ||
804 pixman_image_get_width(so->cache_image) != width ||
805 pixman_image_get_height(so->cache_image) != height) {
807 pixman_image_unref(so->cache_image);
810 pixman_image_create_bits(PIXMAN_a8r8g8b8,
813 if (!so->cache_image) {
814 shared_output_destroy(so);
818 pixman_region32_fini(&damage);
819 pixman_region32_init_rect(&damage, 0, 0, width, height);
822 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
823 shared_output_destroy(so);
827 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
829 cache_data = pixman_image_get_data(so->cache_image);
830 r = pixman_region32_rectangles(&damage, &nrects);
831 for (i = 0; i < nrects; ++i) {
834 width = r[i].x2 - r[i].x1;
835 height = r[i].y2 - r[i].y1;
838 so->output->compositor->renderer->read_pixels(
839 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
840 x, so->output->current_mode->height - r[i].y2,
843 pixman_blt(so->tmp_data, cache_data, -width, stride,
844 32, 32, 0, 1 - height, x, y, width, height);
846 so->output->compositor->renderer->read_pixels(
847 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
848 x, y, width, height);
850 pixman_blt(so->tmp_data, cache_data, width, stride,
851 32, 32, 0, 0, x, y, width, height);
855 pixman_region32_fini(&damage);
859 shared_output_update(so);
862 static struct shared_output *
863 shared_output_create(struct weston_output *output, int parent_fd)
865 struct shared_output *so;
866 struct wl_event_loop *loop;
867 struct ss_seat *seat;
870 so = zalloc(sizeof *so);
874 wl_list_init(&so->seat_list);
876 so->parent.display = wl_display_connect_to_fd(parent_fd);
877 if (!so->parent.display)
880 so->parent.registry = wl_display_get_registry(so->parent.display);
881 if (!so->parent.registry)
883 wl_registry_add_listener(so->parent.registry,
884 ®istry_listener, so);
885 wl_display_roundtrip(so->parent.display);
886 if (so->parent.shm == NULL) {
887 weston_log("Screen share failed: No wl_shm found\n");
890 if (so->parent.fshell == NULL) {
891 weston_log("Screen share failed: "
892 "Parent does not support wl_fullscreen_shell\n");
895 if (so->parent.compositor == NULL) {
896 weston_log("Screen share failed: No wl_compositor found\n");
900 /* Get SHM formats */
901 wl_display_roundtrip(so->parent.display);
902 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
903 weston_log("Screen share failed: "
904 "WL_SHM_FORMAT_XRGB8888 not available\n");
909 wl_compositor_create_surface(so->parent.compositor);
910 if (!so->parent.surface) {
911 weston_log("Screen share failed: %m");
915 so->parent.mode_feedback =
916 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
919 output->current_mode->refresh);
920 if (!so->parent.mode_feedback) {
921 weston_log("Screen share failed: %m");
924 _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
925 &mode_feedback_listener,
928 loop = wl_display_get_event_loop(output->compositor->wl_display);
930 epoll_fd = wl_display_get_fd(so->parent.display);
932 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
933 shared_output_handle_event, so);
934 if (!so->event_source) {
935 weston_log("Screen share failed: %m");
939 /* Ok, everything's created. We should be good to go */
940 wl_list_init(&so->shm.buffers);
941 wl_list_init(&so->shm.free_buffers);
944 so->output_destroyed.notify = output_destroyed;
945 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
947 so->frame_listener.notify = shared_output_repainted;
948 wl_signal_add(&output->frame_signal, &so->frame_listener);
949 output->disable_planes++;
950 weston_output_damage(output);
955 wl_list_for_each(seat, &so->seat_list, link)
956 ss_seat_destroy(seat);
957 wl_display_disconnect(so->parent.display);
966 shared_output_destroy(struct shared_output *so)
968 struct ss_shm_buffer *buffer, *bnext;
970 so->output->disable_planes--;
972 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
973 ss_shm_buffer_destroy(buffer);
974 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, link)
975 ss_shm_buffer_destroy(buffer);
977 wl_display_disconnect(so->parent.display);
978 wl_event_source_remove(so->event_source);
980 wl_list_remove(&so->output_destroyed.link);
981 wl_list_remove(&so->frame_listener.link);
983 pixman_image_unref(so->cache_image);
989 static struct shared_output *
990 weston_output_share(struct weston_output *output, const char* command)
996 char *const argv[] = {
1003 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1004 weston_log("weston_output_share: socketpair failed: %m\n");
1013 weston_log("weston_output_share: fork failed: %m\n");
1018 /* do not give our signal mask to the new process */
1019 sigfillset(&allsigs);
1020 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1022 /* Launch clients as the user. Do not launch clients with
1024 if (seteuid(getuid()) == -1) {
1025 weston_log("weston_output_share: setuid failed: %m\n");
1031 weston_log("weston_output_share: dup failed: %m\n");
1035 snprintf(str, sizeof str, "%d", sv[1]);
1036 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1038 execv(argv[0], argv);
1039 weston_log("weston_output_share: exec failed: %m\n");
1043 return shared_output_create(output, sv[0]);
1049 static struct weston_output *
1050 weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1052 struct weston_output *output;
1054 wl_list_for_each(output, &c->output_list, link) {
1055 if (x >= output->x && y >= output->y &&
1056 x < output->x + output->width &&
1057 y < output->y + output->height)
1065 share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1068 struct weston_output *output;
1069 struct screen_share *ss = data;
1071 if (!seat->pointer) {
1072 weston_log("Cannot pick output: Seat does not have pointer\n");
1076 output = weston_output_find(seat->compositor,
1077 wl_fixed_to_int(seat->pointer->x),
1078 wl_fixed_to_int(seat->pointer->y));
1080 weston_log("Cannot pick output: Pointer not on any output\n");
1084 weston_output_share(output, ss->command);
1088 module_init(struct weston_compositor *compositor,
1089 int *argc, char *argv[])
1091 struct screen_share *ss;
1092 struct weston_config_section *section;
1094 ss = zalloc(sizeof *ss);
1097 ss->compositor = compositor;
1099 section = weston_config_get_section(compositor->config, "screen-share",
1102 weston_config_section_get_string(section, "command", &ss->command, "");
1104 weston_compositor_add_key_binding(compositor, KEY_S,
1105 MODIFIER_CTRL | MODIFIER_ALT,
1106 share_output_binding, ss);