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