compositor-drm: Work around page flip not setting tiling mode on BYT
[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         xkb_keymap_unref(keymap);
218
219         return;
220
221 error:
222         wl_keyboard_release(seat->parent.keyboard);
223         close(fd);
224 }
225
226 static void
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)
230 {
231         struct ss_seat *seat = data;
232
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);
237 }
238
239 static void
240 ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
241                               uint32_t serial, struct wl_surface *surface)
242 {
243         struct ss_seat *seat = data;
244
245         notify_keyboard_focus_out(&seat->base);
246 }
247
248 static void
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)
252 {
253         struct ss_seat *seat = data;
254
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);
260 }
261
262 static void
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,
266                          uint32_t group)
267 {
268         struct ss_seat *seat = data;
269         struct weston_compositor *c = seat->output->output->compositor;
270         uint32_t serial_out;
271
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);
277         else
278                 serial_out = wl_display_next_serial(c->wl_display);
279
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);
284 }
285
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,
290         ss_seat_handle_key,
291         ss_seat_handle_modifiers,
292 };
293
294 static void
295 ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
296                             enum wl_seat_capability caps)
297 {
298         struct ss_seat *ss_seat = data;
299
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;
309         }
310
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;
319         }
320 }
321
322 static const struct wl_seat_listener ss_seat_listener = {
323         ss_seat_handle_capabilities,
324 };
325
326 static struct ss_seat *
327 ss_seat_create(struct shared_output *so, uint32_t id)
328 {
329         struct ss_seat *seat;
330
331         seat = zalloc(sizeof *seat);
332         if (seat == NULL)
333                 return NULL;
334
335         weston_seat_init(&seat->base, so->output->compositor, "default");
336         seat->output = so;
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);
340
341         wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
342         wl_seat_set_user_data(seat->parent.seat, seat);
343
344         return seat;
345 }
346
347 static void
348 ss_seat_destroy(struct ss_seat *seat)
349 {
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);
355
356         wl_list_remove(&seat->link);
357
358         weston_seat_release(&seat->base);
359
360         free(seat);
361 }
362
363 static void
364 ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
365 {
366         pixman_image_unref(buffer->pm_image);
367
368         wl_buffer_destroy(buffer->buffer);
369         munmap(buffer->data, buffer->size);
370
371         pixman_region32_fini(&buffer->damage);
372
373         wl_list_remove(&buffer->link);
374         wl_list_remove(&buffer->free_link);
375         free(buffer);
376 }
377
378 static void
379 buffer_release(void *data, struct wl_buffer *buffer)
380 {
381         struct ss_shm_buffer *sb = data;
382
383         if (sb->output) {
384                 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
385         } else {
386                 ss_shm_buffer_destroy(sb);
387         }
388 }
389
390 static const struct wl_buffer_listener buffer_listener = {
391         buffer_release
392 };
393
394 static struct ss_shm_buffer *
395 shared_output_get_shm_buffer(struct shared_output *so)
396 {
397         struct ss_shm_buffer *sb, *bnext;
398         struct wl_shm_pool *pool;
399         int width, height, stride;
400         int fd;
401         unsigned char *data;
402
403         width = so->output->width;
404         height = so->output->height;
405         stride = width * 4;
406
407         /* If the size of the output changed, we free the old buffers and
408          * make new ones. */
409         if (so->shm.width != width ||
410             so->shm.height != height) {
411
412                 /* Destroy free buffers */
413                 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link)
414                         ss_shm_buffer_destroy(sb);
415
416                 /* Orphan in-use buffers so they get destroyed */
417                 wl_list_for_each(sb, &so->shm.buffers, link)
418                         sb->output = NULL;
419
420                 so->shm.width = width;
421                 so->shm.height = height;
422         }
423
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);
429
430                 return sb;
431         }
432
433         fd = os_create_anonymous_file(height * stride);
434         if (fd < 0) {
435                 weston_log("os_create_anonymous_file: %m");
436                 return NULL;
437         }
438
439         data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
440         if (data == MAP_FAILED) {
441                 weston_log("mmap: %m");
442                 goto out_close;
443         }
444
445         sb = zalloc(sizeof *sb);
446         if (!sb)
447                 goto out_unmap;
448
449         sb->output = so;
450         wl_list_init(&sb->free_link);
451         wl_list_insert(&so->shm.buffers, &sb->link);
452
453         pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
454
455         sb->data = data;
456         sb->size = height * stride;
457
458         pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
459
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);
465         close(fd);
466         fd = -1;
467
468         memset(data, 0, sb->size);
469
470         sb->pm_image =
471                 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
472                                          (uint32_t *)data, stride);
473         if (!sb->pm_image)
474                 goto out_pixman_error;
475
476         return sb;
477
478 out_pixman_error:
479         pixman_region32_fini(&sb->damage);
480 out_unmap:
481         munmap(data, height * stride);
482 out_close:
483         if (fd != -1)
484                 close(fd);
485         return NULL;
486 }
487
488 static void
489 output_compute_transform(struct weston_output *output,
490                          pixman_transform_t *transform)
491 {
492         pixman_fixed_t fw, fh;
493
494         pixman_transform_init_identity(transform);
495
496         fw = pixman_int_to_fixed(output->width);
497         fh = pixman_int_to_fixed(output->height);
498
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);
508         }
509
510         switch (output->transform) {
511         default:
512         case WL_OUTPUT_TRANSFORM_NORMAL:
513         case WL_OUTPUT_TRANSFORM_FLIPPED:
514                 break;
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);
519                 break;
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);
524                 break;
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);
529                 break;
530         }
531
532         pixman_transform_scale(transform, NULL,
533                                pixman_fixed_1 * output->current_scale,
534                                pixman_fixed_1 * output->current_scale);
535 }
536
537 static void
538 shared_output_destroy(struct shared_output *so);
539
540 static int
541 shared_output_ensure_tmp_data(struct shared_output *so,
542                               pixman_region32_t *region)
543 {
544         pixman_box32_t *ext;
545         size_t size;
546
547         if (!pixman_region32_not_empty(region))
548                 return 0;
549
550         ext = pixman_region32_extents(region);
551
552         /* Damage is in output coordinates.
553          *
554          * We are multiplying by 4 because the temporary data needs to be able
555          * to store an 32 bit-per-pixel buffer.
556          */
557         size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
558                  * so->output->current_scale * so->output->current_scale;
559
560         if (so->tmp_data != NULL && size <= so->tmp_data_size)
561                 return 0;
562
563         free(so->tmp_data);
564         so->tmp_data = malloc(size);
565         if (so->tmp_data == NULL) {
566                 so->tmp_data_size = 0;
567                 errno = ENOMEM;
568                 return -1;
569         }
570
571         so->tmp_data_size = size;
572
573         return 0;
574 }
575
576 static void
577 shared_output_update(struct shared_output *so);
578
579 static void
580 shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
581 {
582         struct shared_output *so = data;
583
584         if (cb != so->parent.frame_cb)
585                 return;
586
587         wl_callback_destroy(cb);
588         so->parent.frame_cb = NULL;
589
590         shared_output_update(so);
591 }
592
593 static const struct wl_callback_listener shared_output_frame_listener = {
594         shared_output_frame_callback
595 };
596
597 static void
598 shared_output_update(struct shared_output *so)
599 {
600         struct ss_shm_buffer *sb;
601         pixman_box32_t *r;
602         int i, nrects;
603         pixman_transform_t transform;
604
605         /* Only update if we need to */
606         if (!so->cache_dirty || so->parent.frame_cb)
607                 return;
608
609         sb = shared_output_get_shm_buffer(so);
610         if (sb == NULL) {
611                 shared_output_destroy(so);
612                 return;
613         }
614
615         output_compute_transform(so->output, &transform);
616         pixman_image_set_transform(so->cache_image, &transform);
617
618         pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
619
620         if (so->output->current_scale == 1) {
621                 pixman_image_set_filter(so->cache_image,
622                                         PIXMAN_FILTER_NEAREST, NULL, 0);
623         } else {
624                 pixman_image_set_filter(so->cache_image,
625                                         PIXMAN_FILTER_BILINEAR, NULL, 0);
626         }
627
628         pixman_image_composite32(PIXMAN_OP_SRC,
629                                  so->cache_image, /* src */
630                                  NULL, /* mask */
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 */);
637
638         pixman_image_set_transform(sb->pm_image, NULL);
639         pixman_image_set_clip_region32(sb->pm_image, NULL);
640
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);
645
646         wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
647
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);
651
652         wl_surface_commit(so->parent.surface);
653         wl_callback_destroy(wl_display_sync(so->parent.display));
654         wl_display_flush(so->parent.display);
655
656         /* Clear the buffer damage */
657         pixman_region32_fini(&sb->damage);
658         pixman_region32_init(&sb->damage);
659 }
660
661 static void
662 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
663 {
664         struct shared_output *so = data;
665
666         so->parent.shm_formats |= (1 << format);
667 }
668
669 struct wl_shm_listener shm_listener = {
670         shm_handle_format
671 };
672
673 static void
674 registry_handle_global(void *data, struct wl_registry *registry,
675                        uint32_t id, const char *interface, uint32_t version)
676 {
677         struct shared_output *so = data;
678
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) {
684                 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) {
690                 so->parent.shm =
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) {
695                 so->parent.fshell =
696                         wl_registry_bind(registry,
697                                          id, &_wl_fullscreen_shell_interface, 1);
698         }
699 }
700
701 static void
702 registry_handle_global_remove(void *data, struct wl_registry *registry,
703                               uint32_t name)
704 {
705 }
706
707 static const struct wl_registry_listener registry_listener = {
708         registry_handle_global,
709         registry_handle_global_remove
710 };
711
712 static int
713 shared_output_handle_event(int fd, uint32_t mask, void *data)
714 {
715         struct shared_output *so = data;
716         int count = 0;
717
718         if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
719                 shared_output_destroy(so);
720                 return 0;
721         }
722
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);
727
728         if (mask == 0) {
729                 count = wl_display_dispatch_pending(so->parent.display);
730                 wl_display_flush(so->parent.display);
731         }
732
733         return count;
734 }
735
736 static void
737 output_destroyed(struct wl_listener *l, void *data)
738 {
739         struct shared_output *so;
740
741         so = container_of(l, struct shared_output, output_destroyed);
742
743         shared_output_destroy(so);
744 }
745
746 static void
747 mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
748 {
749         struct shared_output *so = data;
750
751         _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
752 }
753
754 static void
755 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
756 {
757         struct shared_output *so = data;
758
759         _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
760
761         weston_log("Screen share failed: present_surface_for_mode failed\n");
762         shared_output_destroy(so);
763 }
764
765 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
766         mode_feedback_ok,
767         mode_feedback_failed,
768         mode_feedback_ok,
769 };
770
771 static void
772 shared_output_repainted(struct wl_listener *listener, void *data)
773 {
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;
780         pixman_box32_t *r;
781         uint32_t *cache_data;
782
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);
788
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);
792
793         /* Transform to buffer coordinates */
794         weston_transformed_region(so->output->width, so->output->height,
795                                   so->output->transform,
796                                   so->output->current_scale,
797                                   &damage, &damage);
798
799         width = so->output->current_mode->width;
800         height = so->output->current_mode->height;
801         stride = width;
802
803         if (!so->cache_image ||
804             pixman_image_get_width(so->cache_image) != width ||
805             pixman_image_get_height(so->cache_image) != height) {
806                 if (so->cache_image)
807                         pixman_image_unref(so->cache_image);
808
809                 so->cache_image =
810                         pixman_image_create_bits(PIXMAN_a8r8g8b8,
811                                                  width, height, NULL,
812                                                  stride);
813                 if (!so->cache_image) {
814                         shared_output_destroy(so);
815                         return;
816                 }
817
818                 pixman_region32_fini(&damage);
819                 pixman_region32_init_rect(&damage, 0, 0, width, height);
820         }
821
822         if (shared_output_ensure_tmp_data(so, &damage) < 0) {
823                 shared_output_destroy(so);
824                 return;
825         }
826
827         do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
828
829         cache_data = pixman_image_get_data(so->cache_image);
830         r = pixman_region32_rectangles(&damage, &nrects);
831         for (i = 0; i < nrects; ++i) {
832                 x = r[i].x1;
833                 y = r[i].y1;
834                 width = r[i].x2 - r[i].x1;
835                 height = r[i].y2 - r[i].y1;
836
837                 if (do_yflip) {
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,
841                                 width, height);
842
843                         pixman_blt(so->tmp_data, cache_data, -width, stride,
844                                    32, 32, 0, 1 - height, x, y, width, height);
845                 } else {
846                         so->output->compositor->renderer->read_pixels(
847                                 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
848                                 x, y, width, height);
849
850                         pixman_blt(so->tmp_data, cache_data, width, stride,
851                                    32, 32, 0, 0, x, y, width, height);
852                 }
853         }
854
855         pixman_region32_fini(&damage);
856
857         so->cache_dirty = 1;
858
859         shared_output_update(so);
860 }
861
862 static struct shared_output *
863 shared_output_create(struct weston_output *output, int parent_fd)
864 {
865         struct shared_output *so;
866         struct wl_event_loop *loop;
867         struct ss_seat *seat;
868         int epoll_fd;
869
870         so = zalloc(sizeof *so);
871         if (so == NULL)
872                 goto err_close;
873
874         wl_list_init(&so->seat_list);
875
876         so->parent.display = wl_display_connect_to_fd(parent_fd);
877         if (!so->parent.display)
878                 goto err_alloc;
879
880         so->parent.registry = wl_display_get_registry(so->parent.display);
881         if (!so->parent.registry)
882                 goto err_display;
883         wl_registry_add_listener(so->parent.registry,
884                                  &registry_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");
888                 goto err_display;
889         }
890         if (so->parent.fshell == NULL) {
891                 weston_log("Screen share failed: "
892                            "Parent does not support wl_fullscreen_shell\n");
893                 goto err_display;
894         }
895         if (so->parent.compositor == NULL) {
896                 weston_log("Screen share failed: No wl_compositor found\n");
897                 goto err_display;
898         }
899
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");
905                 goto err_display;
906         }
907
908         so->parent.surface =
909                 wl_compositor_create_surface(so->parent.compositor);
910         if (!so->parent.surface) {
911                 weston_log("Screen share failed: %m");
912                 goto err_display;
913         }
914
915         so->parent.mode_feedback =
916                 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
917                                                               so->parent.surface,
918                                                               so->parent.output,
919                                                               output->current_mode->refresh);
920         if (!so->parent.mode_feedback) {
921                 weston_log("Screen share failed: %m");
922                 goto err_display;
923         }
924         _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
925                                                         &mode_feedback_listener,
926                                                         so);
927
928         loop = wl_display_get_event_loop(output->compositor->wl_display);
929
930         epoll_fd = wl_display_get_fd(so->parent.display);
931         so->event_source =
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");
936                 goto err_display;
937         }
938
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);
942
943         so->output = output;
944         so->output_destroyed.notify = output_destroyed;
945         wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
946
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);
951
952         return so;
953
954 err_display:
955         wl_list_for_each(seat, &so->seat_list, link)
956                 ss_seat_destroy(seat);
957         wl_display_disconnect(so->parent.display);
958 err_alloc:
959         free(so);
960 err_close:
961         close(parent_fd);
962         return NULL;
963 }
964
965 static void
966 shared_output_destroy(struct shared_output *so)
967 {
968         struct ss_shm_buffer *buffer, *bnext;
969
970         so->output->disable_planes--;
971
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);
976
977         wl_display_disconnect(so->parent.display);
978         wl_event_source_remove(so->event_source);
979
980         wl_list_remove(&so->output_destroyed.link);
981         wl_list_remove(&so->frame_listener.link);
982
983         pixman_image_unref(so->cache_image);
984         free(so->tmp_data);
985
986         free(so);
987 }
988
989 static struct shared_output *
990 weston_output_share(struct weston_output *output, const char* command)
991 {
992         int sv[2];
993         char str[32];
994         pid_t pid;
995         sigset_t allsigs;
996         char *const argv[] = {
997           "/bin/sh",
998           "-c",
999           (char*)command,
1000           NULL
1001         };
1002
1003         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1004                 weston_log("weston_output_share: socketpair failed: %m\n");
1005                 return NULL;
1006         }
1007
1008         pid = fork();
1009
1010         if (pid == -1) {
1011                 close(sv[0]);
1012                 close(sv[1]);
1013                 weston_log("weston_output_share: fork failed: %m\n");
1014                 return NULL;
1015         }
1016
1017         if (pid == 0) {
1018                 /* do not give our signal mask to the new process */
1019                 sigfillset(&allsigs);
1020                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1021
1022                 /* Launch clients as the user. Do not launch clients with
1023                  * wrong euid. */
1024                 if (seteuid(getuid()) == -1) {
1025                         weston_log("weston_output_share: setuid failed: %m\n");
1026                         abort();
1027                 }
1028
1029                 sv[1] = dup(sv[1]);
1030                 if (sv[1] == -1) {
1031                         weston_log("weston_output_share: dup failed: %m\n");
1032                         abort();
1033                 }
1034
1035                 snprintf(str, sizeof str, "%d", sv[1]);
1036                 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1037
1038                 execv(argv[0], argv);
1039                 weston_log("weston_output_share: exec failed: %m\n");
1040                 abort();
1041         } else {
1042                 close(sv[1]);
1043                 return shared_output_create(output, sv[0]);
1044         }
1045
1046         return NULL;
1047 }
1048
1049 static struct weston_output *
1050 weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1051 {
1052         struct weston_output *output;
1053
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)
1058                         return output;
1059         }
1060
1061         return NULL;
1062 }
1063
1064 static void
1065 share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1066                      void *data)
1067 {
1068         struct weston_output *output;
1069         struct screen_share *ss = data;
1070
1071         if (!seat->pointer) {
1072                 weston_log("Cannot pick output: Seat does not have pointer\n");
1073                 return;
1074         }
1075
1076         output = weston_output_find(seat->compositor,
1077                                     wl_fixed_to_int(seat->pointer->x),
1078                                     wl_fixed_to_int(seat->pointer->y));
1079         if (!output) {
1080                 weston_log("Cannot pick output: Pointer not on any output\n");
1081                 return;
1082         }
1083
1084         weston_output_share(output, ss->command);
1085 }
1086
1087 WL_EXPORT int
1088 module_init(struct weston_compositor *compositor,
1089             int *argc, char *argv[])
1090 {
1091         struct screen_share *ss;
1092         struct weston_config_section *section;
1093
1094         ss = zalloc(sizeof *ss);
1095         if (ss == NULL)
1096                 return -1;
1097         ss->compositor = compositor;
1098
1099         section = weston_config_get_section(compositor->config, "screen-share",
1100                                             NULL, NULL);
1101
1102         weston_config_section_get_string(section, "command", &ss->command, "");
1103
1104         weston_compositor_add_key_binding(compositor, KEY_S,
1105                                           MODIFIER_CTRL | MODIFIER_ALT,
1106                                           share_output_binding, ss);
1107         return 0;
1108 }