9e81ef9c054bcb675b8d512027400377d6521291
[platform/upstream/weston.git] / src / screen-share.c
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 #include <ctype.h>
36
37 #include <wayland-client.h>
38
39 #include "compositor.h"
40 #include "../shared/os-compatibility.h"
41 #include "fullscreen-shell-client-protocol.h"
42
43 struct shared_output {
44         struct weston_output *output;
45         struct wl_listener output_destroyed;
46         struct wl_list seat_list;
47
48         struct {
49                 struct wl_display *display;
50                 struct wl_registry *registry;
51                 struct wl_compositor *compositor;
52                 struct wl_shm *shm;
53                 uint32_t shm_formats;
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;
59         } parent;
60
61         struct wl_event_source *event_source;
62         struct wl_listener frame_listener;
63
64         struct {
65                 int32_t width, height;
66
67                 struct wl_list buffers;
68                 struct wl_list free_buffers;
69         } shm;
70
71         int cache_dirty;
72         pixman_image_t *cache_image;
73         uint32_t *tmp_data;
74         size_t tmp_data_size;
75 };
76
77 struct ss_seat {
78         struct weston_seat base;
79         struct shared_output *output;
80         struct wl_list link;
81
82         struct {
83                 struct wl_seat *seat;
84                 struct wl_pointer *pointer;
85                 struct wl_keyboard *keyboard;
86         } parent;
87
88         enum weston_key_state_update keyboard_state_update;
89         uint32_t key_serial;
90 };
91
92 struct ss_shm_buffer {
93         struct shared_output *output;
94         struct wl_list link;
95         struct wl_list free_link;
96
97         struct wl_buffer *buffer;
98         void *data;
99         size_t size;
100         pixman_region32_t damage;
101
102         pixman_image_t *pm_image;
103 };
104
105 struct screen_share {
106         struct weston_compositor *compositor;
107         char *command;
108 };
109
110 static void
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)
114 {
115         struct ss_seat *seat = data;
116
117         /* No transformation of input position is required here because we are
118          * always receiving the input in the same coordinates as the output. */
119
120         notify_pointer_focus(&seat->base, NULL, 0, 0);
121 }
122
123 static void
124 ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer,
125                              uint32_t serial, struct wl_surface *surface)
126 {
127         struct ss_seat *seat = data;
128
129         notify_pointer_focus(&seat->base, NULL, 0, 0);
130 }
131
132 static void
133 ss_seat_handle_motion(void *data, struct wl_pointer *pointer,
134                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
135 {
136         struct ss_seat *seat = data;
137
138         /* No transformation of input position is required here because we are
139          * always receiving the input in the same coordinates as the output. */
140
141         notify_motion_absolute(&seat->base, time, x, y);
142 }
143
144 static void
145 ss_seat_handle_button(void *data, struct wl_pointer *pointer,
146                       uint32_t serial, uint32_t time, uint32_t button,
147                       uint32_t state)
148 {
149         struct ss_seat *seat = data;
150
151         notify_button(&seat->base, time, button, state);
152 }
153
154 static void
155 ss_seat_handle_axis(void *data, struct wl_pointer *pointer,
156                     uint32_t time, uint32_t axis, wl_fixed_t value)
157 {
158         struct ss_seat *seat = data;
159
160         notify_axis(&seat->base, time, axis, value);
161 }
162
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,
168         ss_seat_handle_axis,
169 };
170
171 static void
172 ss_seat_handle_keymap(void *data, struct wl_keyboard *keyboard,
173                       uint32_t format, int fd, uint32_t size)
174 {
175         struct ss_seat *seat = data;
176         struct xkb_keymap *keymap;
177         char *map_str;
178
179         if (!data)
180                 goto error;
181
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");
186                         goto error;
187                 }
188
189                 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
190                                                     map_str,
191                                                     XKB_KEYMAP_FORMAT_TEXT_V1,
192                                                     0);
193                 munmap(map_str, size);
194
195                 if (!keymap) {
196                         weston_log("failed to compile keymap\n");
197                         goto error;
198                 }
199
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");
203                 keymap = NULL;
204                 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
205         } else {
206                 weston_log("Invalid keymap\n");
207                 goto error;
208         }
209
210         close(fd);
211
212         if (seat->base.keyboard)
213                 weston_seat_update_keymap(&seat->base, keymap);
214         else
215                 weston_seat_init_keyboard(&seat->base, keymap);
216
217         if (keymap)
218                 xkb_keymap_unref(keymap);
219
220         return;
221
222 error:
223         wl_keyboard_release(seat->parent.keyboard);
224         close(fd);
225 }
226
227 static void
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)
231 {
232         struct ss_seat *seat = data;
233
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);
238 }
239
240 static void
241 ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
242                               uint32_t serial, struct wl_surface *surface)
243 {
244         struct ss_seat *seat = data;
245
246         notify_keyboard_focus_out(&seat->base);
247 }
248
249 static void
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)
253 {
254         struct ss_seat *seat = data;
255
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);
261 }
262
263 static void
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,
267                          uint32_t group)
268 {
269         struct ss_seat *seat = data;
270         struct weston_compositor *c = seat->output->output->compositor;
271         uint32_t serial_out;
272
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);
278         else
279                 serial_out = wl_display_next_serial(c->wl_display);
280
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);
285 }
286
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,
291         ss_seat_handle_key,
292         ss_seat_handle_modifiers,
293 };
294
295 static void
296 ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
297                             enum wl_seat_capability caps)
298 {
299         struct ss_seat *ss_seat = data;
300
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;
310         }
311
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;
320         }
321 }
322
323 static const struct wl_seat_listener ss_seat_listener = {
324         ss_seat_handle_capabilities,
325 };
326
327 static struct ss_seat *
328 ss_seat_create(struct shared_output *so, uint32_t id)
329 {
330         struct ss_seat *seat;
331
332         seat = zalloc(sizeof *seat);
333         if (seat == NULL)
334                 return NULL;
335
336         weston_seat_init(&seat->base, so->output->compositor, "default");
337         seat->output = so;
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);
341
342         wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
343         wl_seat_set_user_data(seat->parent.seat, seat);
344
345         return seat;
346 }
347
348 static void
349 ss_seat_destroy(struct ss_seat *seat)
350 {
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);
356
357         wl_list_remove(&seat->link);
358
359         weston_seat_release(&seat->base);
360
361         free(seat);
362 }
363
364 static void
365 ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
366 {
367         pixman_image_unref(buffer->pm_image);
368
369         wl_buffer_destroy(buffer->buffer);
370         munmap(buffer->data, buffer->size);
371
372         pixman_region32_fini(&buffer->damage);
373
374         wl_list_remove(&buffer->link);
375         wl_list_remove(&buffer->free_link);
376         free(buffer);
377 }
378
379 static void
380 buffer_release(void *data, struct wl_buffer *buffer)
381 {
382         struct ss_shm_buffer *sb = data;
383
384         if (sb->output) {
385                 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
386         } else {
387                 ss_shm_buffer_destroy(sb);
388         }
389 }
390
391 static const struct wl_buffer_listener buffer_listener = {
392         buffer_release
393 };
394
395 static struct ss_shm_buffer *
396 shared_output_get_shm_buffer(struct shared_output *so)
397 {
398         struct ss_shm_buffer *sb, *bnext;
399         struct wl_shm_pool *pool;
400         int width, height, stride;
401         int fd;
402         unsigned char *data;
403
404         width = so->output->width;
405         height = so->output->height;
406         stride = width * 4;
407
408         /* If the size of the output changed, we free the old buffers and
409          * make new ones. */
410         if (so->shm.width != width ||
411             so->shm.height != height) {
412
413                 /* Destroy free buffers */
414                 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link)
415                         ss_shm_buffer_destroy(sb);
416
417                 /* Orphan in-use buffers so they get destroyed */
418                 wl_list_for_each(sb, &so->shm.buffers, link)
419                         sb->output = NULL;
420
421                 so->shm.width = width;
422                 so->shm.height = height;
423         }
424
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);
430
431                 return sb;
432         }
433
434         fd = os_create_anonymous_file(height * stride);
435         if (fd < 0) {
436                 weston_log("os_create_anonymous_file: %m");
437                 return NULL;
438         }
439
440         data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
441         if (data == MAP_FAILED) {
442                 weston_log("mmap: %m");
443                 goto out_close;
444         }
445
446         sb = zalloc(sizeof *sb);
447         if (!sb)
448                 goto out_unmap;
449
450         sb->output = so;
451         wl_list_init(&sb->free_link);
452         wl_list_insert(&so->shm.buffers, &sb->link);
453
454         pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
455
456         sb->data = data;
457         sb->size = height * stride;
458
459         pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
460
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);
466         close(fd);
467         fd = -1;
468
469         memset(data, 0, sb->size);
470
471         sb->pm_image =
472                 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
473                                          (uint32_t *)data, stride);
474         if (!sb->pm_image)
475                 goto out_pixman_error;
476
477         return sb;
478
479 out_pixman_error:
480         pixman_region32_fini(&sb->damage);
481 out_unmap:
482         munmap(data, height * stride);
483 out_close:
484         if (fd != -1)
485                 close(fd);
486         return NULL;
487 }
488
489 static void
490 output_compute_transform(struct weston_output *output,
491                          pixman_transform_t *transform)
492 {
493         pixman_fixed_t fw, fh;
494
495         pixman_transform_init_identity(transform);
496
497         fw = pixman_int_to_fixed(output->width);
498         fh = pixman_int_to_fixed(output->height);
499
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);
509         }
510
511         switch (output->transform) {
512         default:
513         case WL_OUTPUT_TRANSFORM_NORMAL:
514         case WL_OUTPUT_TRANSFORM_FLIPPED:
515                 break;
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);
520                 break;
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);
525                 break;
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);
530                 break;
531         }
532
533         pixman_transform_scale(transform, NULL,
534                                pixman_fixed_1 * output->current_scale,
535                                pixman_fixed_1 * output->current_scale);
536 }
537
538 static void
539 shared_output_destroy(struct shared_output *so);
540
541 static int
542 shared_output_ensure_tmp_data(struct shared_output *so,
543                               pixman_region32_t *region)
544 {
545         pixman_box32_t *ext;
546         size_t size;
547
548         if (!pixman_region32_not_empty(region))
549                 return 0;
550
551         ext = pixman_region32_extents(region);
552
553         /* Damage is in output coordinates.
554          *
555          * We are multiplying by 4 because the temporary data needs to be able
556          * to store an 32 bit-per-pixel buffer.
557          */
558         size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
559                  * so->output->current_scale * so->output->current_scale;
560
561         if (so->tmp_data != NULL && size <= so->tmp_data_size)
562                 return 0;
563
564         free(so->tmp_data);
565         so->tmp_data = malloc(size);
566         if (so->tmp_data == NULL) {
567                 so->tmp_data_size = 0;
568                 errno = ENOMEM;
569                 return -1;
570         }
571
572         so->tmp_data_size = size;
573
574         return 0;
575 }
576
577 static void
578 shared_output_update(struct shared_output *so);
579
580 static void
581 shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
582 {
583         struct shared_output *so = data;
584
585         if (cb != so->parent.frame_cb)
586                 return;
587
588         wl_callback_destroy(cb);
589         so->parent.frame_cb = NULL;
590
591         shared_output_update(so);
592 }
593
594 static const struct wl_callback_listener shared_output_frame_listener = {
595         shared_output_frame_callback
596 };
597
598 static void
599 shared_output_update(struct shared_output *so)
600 {
601         struct ss_shm_buffer *sb;
602         pixman_box32_t *r;
603         int i, nrects;
604         pixman_transform_t transform;
605
606         /* Only update if we need to */
607         if (!so->cache_dirty || so->parent.frame_cb)
608                 return;
609
610         sb = shared_output_get_shm_buffer(so);
611         if (sb == NULL) {
612                 shared_output_destroy(so);
613                 return;
614         }
615
616         output_compute_transform(so->output, &transform);
617         pixman_image_set_transform(so->cache_image, &transform);
618
619         pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
620
621         if (so->output->current_scale == 1) {
622                 pixman_image_set_filter(so->cache_image,
623                                         PIXMAN_FILTER_NEAREST, NULL, 0);
624         } else {
625                 pixman_image_set_filter(so->cache_image,
626                                         PIXMAN_FILTER_BILINEAR, NULL, 0);
627         }
628
629         pixman_image_composite32(PIXMAN_OP_SRC,
630                                  so->cache_image, /* src */
631                                  NULL, /* mask */
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 */);
638
639         pixman_image_set_transform(sb->pm_image, NULL);
640         pixman_image_set_clip_region32(sb->pm_image, NULL);
641
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);
646
647         wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
648
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);
652
653         wl_surface_commit(so->parent.surface);
654         wl_callback_destroy(wl_display_sync(so->parent.display));
655         wl_display_flush(so->parent.display);
656
657         /* Clear the buffer damage */
658         pixman_region32_fini(&sb->damage);
659         pixman_region32_init(&sb->damage);
660 }
661
662 static void
663 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
664 {
665         struct shared_output *so = data;
666
667         so->parent.shm_formats |= (1 << format);
668 }
669
670 struct wl_shm_listener shm_listener = {
671         shm_handle_format
672 };
673
674 static void
675 registry_handle_global(void *data, struct wl_registry *registry,
676                        uint32_t id, const char *interface, uint32_t version)
677 {
678         struct shared_output *so = data;
679
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) {
685                 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) {
691                 so->parent.shm =
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) {
696                 so->parent.fshell =
697                         wl_registry_bind(registry,
698                                          id, &_wl_fullscreen_shell_interface, 1);
699         }
700 }
701
702 static void
703 registry_handle_global_remove(void *data, struct wl_registry *registry,
704                               uint32_t name)
705 {
706 }
707
708 static const struct wl_registry_listener registry_listener = {
709         registry_handle_global,
710         registry_handle_global_remove
711 };
712
713 static int
714 shared_output_handle_event(int fd, uint32_t mask, void *data)
715 {
716         struct shared_output *so = data;
717         int count = 0;
718
719         if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
720                 shared_output_destroy(so);
721                 return 0;
722         }
723
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);
728
729         if (mask == 0) {
730                 count = wl_display_dispatch_pending(so->parent.display);
731                 wl_display_flush(so->parent.display);
732         }
733
734         return count;
735 }
736
737 static void
738 output_destroyed(struct wl_listener *l, void *data)
739 {
740         struct shared_output *so;
741
742         so = container_of(l, struct shared_output, output_destroyed);
743
744         shared_output_destroy(so);
745 }
746
747 static void
748 mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
749 {
750         struct shared_output *so = data;
751
752         _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
753 }
754
755 static void
756 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
757 {
758         struct shared_output *so = data;
759
760         _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
761
762         weston_log("Screen share failed: present_surface_for_mode failed\n");
763         shared_output_destroy(so);
764 }
765
766 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
767         mode_feedback_ok,
768         mode_feedback_failed,
769         mode_feedback_ok,
770 };
771
772 static void
773 shared_output_repainted(struct wl_listener *listener, void *data)
774 {
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;
781         pixman_box32_t *r;
782         uint32_t *cache_data;
783
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);
789
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);
793
794         /* Transform to buffer coordinates */
795         weston_transformed_region(so->output->width, so->output->height,
796                                   so->output->transform,
797                                   so->output->current_scale,
798                                   &damage, &damage);
799
800         width = so->output->current_mode->width;
801         height = so->output->current_mode->height;
802         stride = width;
803
804         if (!so->cache_image ||
805             pixman_image_get_width(so->cache_image) != width ||
806             pixman_image_get_height(so->cache_image) != height) {
807                 if (so->cache_image)
808                         pixman_image_unref(so->cache_image);
809
810                 so->cache_image =
811                         pixman_image_create_bits(PIXMAN_a8r8g8b8,
812                                                  width, height, NULL,
813                                                  stride);
814                 if (!so->cache_image) {
815                         shared_output_destroy(so);
816                         return;
817                 }
818
819                 pixman_region32_fini(&damage);
820                 pixman_region32_init_rect(&damage, 0, 0, width, height);
821         }
822
823         if (shared_output_ensure_tmp_data(so, &damage) < 0) {
824                 shared_output_destroy(so);
825                 return;
826         }
827
828         do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
829
830         cache_data = pixman_image_get_data(so->cache_image);
831         r = pixman_region32_rectangles(&damage, &nrects);
832         for (i = 0; i < nrects; ++i) {
833                 x = r[i].x1;
834                 y = r[i].y1;
835                 width = r[i].x2 - r[i].x1;
836                 height = r[i].y2 - r[i].y1;
837
838                 if (do_yflip) {
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,
842                                 width, height);
843
844                         pixman_blt(so->tmp_data, cache_data, -width, stride,
845                                    32, 32, 0, 1 - height, x, y, width, height);
846                 } else {
847                         so->output->compositor->renderer->read_pixels(
848                                 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
849                                 x, y, width, height);
850
851                         pixman_blt(so->tmp_data, cache_data, width, stride,
852                                    32, 32, 0, 0, x, y, width, height);
853                 }
854         }
855
856         pixman_region32_fini(&damage);
857
858         so->cache_dirty = 1;
859
860         shared_output_update(so);
861 }
862
863 static struct shared_output *
864 shared_output_create(struct weston_output *output, int parent_fd)
865 {
866         struct shared_output *so;
867         struct wl_event_loop *loop;
868         struct ss_seat *seat;
869         int epoll_fd;
870
871         so = zalloc(sizeof *so);
872         if (so == NULL)
873                 goto err_close;
874
875         wl_list_init(&so->seat_list);
876
877         so->parent.display = wl_display_connect_to_fd(parent_fd);
878         if (!so->parent.display)
879                 goto err_alloc;
880
881         so->parent.registry = wl_display_get_registry(so->parent.display);
882         if (!so->parent.registry)
883                 goto err_display;
884         wl_registry_add_listener(so->parent.registry,
885                                  &registry_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");
889                 goto err_display;
890         }
891         if (so->parent.fshell == NULL) {
892                 weston_log("Screen share failed: "
893                            "Parent does not support wl_fullscreen_shell\n");
894                 goto err_display;
895         }
896         if (so->parent.compositor == NULL) {
897                 weston_log("Screen share failed: No wl_compositor found\n");
898                 goto err_display;
899         }
900
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");
906                 goto err_display;
907         }
908
909         so->parent.surface =
910                 wl_compositor_create_surface(so->parent.compositor);
911         if (!so->parent.surface) {
912                 weston_log("Screen share failed: %m");
913                 goto err_display;
914         }
915
916         so->parent.mode_feedback =
917                 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
918                                                               so->parent.surface,
919                                                               so->parent.output,
920                                                               output->current_mode->refresh);
921         if (!so->parent.mode_feedback) {
922                 weston_log("Screen share failed: %m");
923                 goto err_display;
924         }
925         _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
926                                                         &mode_feedback_listener,
927                                                         so);
928
929         loop = wl_display_get_event_loop(output->compositor->wl_display);
930
931         epoll_fd = wl_display_get_fd(so->parent.display);
932         so->event_source =
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");
937                 goto err_display;
938         }
939
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);
943
944         so->output = output;
945         so->output_destroyed.notify = output_destroyed;
946         wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
947
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);
952
953         return so;
954
955 err_display:
956         wl_list_for_each(seat, &so->seat_list, link)
957                 ss_seat_destroy(seat);
958         wl_display_disconnect(so->parent.display);
959 err_alloc:
960         free(so);
961 err_close:
962         close(parent_fd);
963         return NULL;
964 }
965
966 static void
967 shared_output_destroy(struct shared_output *so)
968 {
969         struct ss_shm_buffer *buffer, *bnext;
970
971         so->output->disable_planes--;
972
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);
977
978         wl_display_disconnect(so->parent.display);
979         wl_event_source_remove(so->event_source);
980
981         wl_list_remove(&so->output_destroyed.link);
982         wl_list_remove(&so->frame_listener.link);
983
984         pixman_image_unref(so->cache_image);
985         free(so->tmp_data);
986
987         free(so);
988 }
989
990 static struct shared_output *
991 weston_output_share(struct weston_output *output, const char* command)
992 {
993         int sv[2];
994         char str[32];
995         pid_t pid;
996         sigset_t allsigs;
997         char *const argv[] = {
998           "/bin/sh",
999           "-c",
1000           (char*)command,
1001           NULL
1002         };
1003
1004         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1005                 weston_log("weston_output_share: socketpair failed: %m\n");
1006                 return NULL;
1007         }
1008
1009         pid = fork();
1010
1011         if (pid == -1) {
1012                 close(sv[0]);
1013                 close(sv[1]);
1014                 weston_log("weston_output_share: fork failed: %m\n");
1015                 return NULL;
1016         }
1017
1018         if (pid == 0) {
1019                 /* do not give our signal mask to the new process */
1020                 sigfillset(&allsigs);
1021                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1022
1023                 /* Launch clients as the user. Do not launch clients with
1024                  * wrong euid. */
1025                 if (seteuid(getuid()) == -1) {
1026                         weston_log("weston_output_share: setuid failed: %m\n");
1027                         abort();
1028                 }
1029
1030                 sv[1] = dup(sv[1]);
1031                 if (sv[1] == -1) {
1032                         weston_log("weston_output_share: dup failed: %m\n");
1033                         abort();
1034                 }
1035
1036                 snprintf(str, sizeof str, "%d", sv[1]);
1037                 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1038
1039                 execv(argv[0], argv);
1040                 weston_log("weston_output_share: exec failed: %m\n");
1041                 abort();
1042         } else {
1043                 close(sv[1]);
1044                 return shared_output_create(output, sv[0]);
1045         }
1046
1047         return NULL;
1048 }
1049
1050 static struct weston_output *
1051 weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1052 {
1053         struct weston_output *output;
1054
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)
1059                         return output;
1060         }
1061
1062         return NULL;
1063 }
1064
1065 static void
1066 share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1067                      void *data)
1068 {
1069         struct weston_output *output;
1070         struct screen_share *ss = data;
1071
1072         if (!seat->pointer) {
1073                 weston_log("Cannot pick output: Seat does not have pointer\n");
1074                 return;
1075         }
1076
1077         output = weston_output_find(seat->compositor,
1078                                     wl_fixed_to_int(seat->pointer->x),
1079                                     wl_fixed_to_int(seat->pointer->y));
1080         if (!output) {
1081                 weston_log("Cannot pick output: Pointer not on any output\n");
1082                 return;
1083         }
1084
1085         weston_output_share(output, ss->command);
1086 }
1087
1088 WL_EXPORT int
1089 module_init(struct weston_compositor *compositor,
1090             int *argc, char *argv[])
1091 {
1092         struct screen_share *ss;
1093         struct weston_config_section *section;
1094
1095         ss = zalloc(sizeof *ss);
1096         if (ss == NULL)
1097                 return -1;
1098         ss->compositor = compositor;
1099
1100         section = weston_config_get_section(compositor->config, "screen-share",
1101                                             NULL, NULL);
1102
1103         weston_config_section_get_string(section, "command", &ss->command, "");
1104
1105         weston_compositor_add_key_binding(compositor, KEY_S,
1106                                           MODIFIER_CTRL | MODIFIER_ALT,
1107                                           share_output_binding, ss);
1108         return 0;
1109 }