blob: e5f91ea9022e6a04bd69edc6a3788553b8455ea2 [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
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300176ss_seat_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500177 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
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300216 if (seat->base.keyboard_device_count)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500217 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
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300267ss_seat_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500268 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;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300273 struct weston_compositor *c = seat->base.compositor;
274 struct weston_keyboard *keyboard;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500275 uint32_t serial_out;
276
277 /* If we get a key event followed by a modifier event with the
278 * same serial number, then we try to preserve those semantics by
279 * reusing the same serial number on the way out too. */
280 if (serial_in == seat->key_serial)
281 serial_out = wl_display_get_serial(c->wl_display);
282 else
283 serial_out = wl_display_next_serial(c->wl_display);
284
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300285 keyboard = weston_seat_get_keyboard(&seat->base);
286 xkb_state_update_mask(keyboard->xkb_state.state,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500287 mods_depressed, mods_latched,
288 mods_locked, 0, 0, group);
289 notify_modifiers(&seat->base, serial_out);
290}
291
292static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
293 ss_seat_handle_keymap,
294 ss_seat_handle_keyboard_enter,
295 ss_seat_handle_keyboard_leave,
296 ss_seat_handle_key,
297 ss_seat_handle_modifiers,
298};
299
300static void
301ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
302 enum wl_seat_capability caps)
303{
304 struct ss_seat *ss_seat = data;
305
306 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
307 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
308 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
309 wl_pointer_add_listener(ss_seat->parent.pointer,
310 &ss_seat_pointer_listener, ss_seat);
311 weston_seat_init_pointer(&ss_seat->base);
312 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
313 wl_pointer_destroy(ss_seat->parent.pointer);
314 ss_seat->parent.pointer = NULL;
315 }
316
317 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
318 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
319 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
320 wl_keyboard_add_listener(ss_seat->parent.keyboard,
321 &ss_seat_keyboard_listener, ss_seat);
322 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
323 wl_keyboard_destroy(ss_seat->parent.keyboard);
324 ss_seat->parent.keyboard = NULL;
325 }
326}
327
328static const struct wl_seat_listener ss_seat_listener = {
329 ss_seat_handle_capabilities,
330};
331
332static struct ss_seat *
333ss_seat_create(struct shared_output *so, uint32_t id)
334{
335 struct ss_seat *seat;
336
337 seat = zalloc(sizeof *seat);
338 if (seat == NULL)
339 return NULL;
340
341 weston_seat_init(&seat->base, so->output->compositor, "default");
342 seat->output = so;
343 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
344 &wl_seat_interface, 1);
345 wl_list_insert(so->seat_list.prev, &seat->link);
346
347 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
348 wl_seat_set_user_data(seat->parent.seat, seat);
349
350 return seat;
351}
352
353static void
354ss_seat_destroy(struct ss_seat *seat)
355{
356 if (seat->parent.pointer)
357 wl_pointer_release(seat->parent.pointer);
358 if (seat->parent.keyboard)
359 wl_keyboard_release(seat->parent.keyboard);
360 wl_seat_destroy(seat->parent.seat);
361
362 wl_list_remove(&seat->link);
363
364 weston_seat_release(&seat->base);
365
366 free(seat);
367}
368
369static void
370ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
371{
372 pixman_image_unref(buffer->pm_image);
373
374 wl_buffer_destroy(buffer->buffer);
375 munmap(buffer->data, buffer->size);
376
377 pixman_region32_fini(&buffer->damage);
378
379 wl_list_remove(&buffer->link);
380 wl_list_remove(&buffer->free_link);
381 free(buffer);
382}
383
384static void
385buffer_release(void *data, struct wl_buffer *buffer)
386{
387 struct ss_shm_buffer *sb = data;
388
389 if (sb->output) {
390 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
391 } else {
392 ss_shm_buffer_destroy(sb);
393 }
394}
395
396static const struct wl_buffer_listener buffer_listener = {
397 buffer_release
398};
399
400static struct ss_shm_buffer *
401shared_output_get_shm_buffer(struct shared_output *so)
402{
403 struct ss_shm_buffer *sb, *bnext;
404 struct wl_shm_pool *pool;
405 int width, height, stride;
406 int fd;
407 unsigned char *data;
408
409 width = so->output->width;
410 height = so->output->height;
411 stride = width * 4;
412
413 /* If the size of the output changed, we free the old buffers and
414 * make new ones. */
415 if (so->shm.width != width ||
416 so->shm.height != height) {
417
418 /* Destroy free buffers */
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700419 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500420 ss_shm_buffer_destroy(sb);
421
422 /* Orphan in-use buffers so they get destroyed */
423 wl_list_for_each(sb, &so->shm.buffers, link)
424 sb->output = NULL;
425
426 so->shm.width = width;
427 so->shm.height = height;
428 }
429
430 if (!wl_list_empty(&so->shm.free_buffers)) {
431 sb = container_of(so->shm.free_buffers.next,
432 struct ss_shm_buffer, free_link);
433 wl_list_remove(&sb->free_link);
434 wl_list_init(&sb->free_link);
435
436 return sb;
437 }
438
439 fd = os_create_anonymous_file(height * stride);
440 if (fd < 0) {
441 weston_log("os_create_anonymous_file: %m");
442 return NULL;
443 }
444
445 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
446 if (data == MAP_FAILED) {
447 weston_log("mmap: %m");
Hardeninge57d1f22014-04-11 09:06:58 +0200448 goto out_close;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500449 }
450
451 sb = zalloc(sizeof *sb);
Hardeninge57d1f22014-04-11 09:06:58 +0200452 if (!sb)
453 goto out_unmap;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500454
455 sb->output = so;
456 wl_list_init(&sb->free_link);
457 wl_list_insert(&so->shm.buffers, &sb->link);
458
459 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
460
461 sb->data = data;
462 sb->size = height * stride;
463
464 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
465
466 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
467 width, height, stride,
468 WL_SHM_FORMAT_ARGB8888);
469 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
470 wl_shm_pool_destroy(pool);
471 close(fd);
Hardeninge57d1f22014-04-11 09:06:58 +0200472 fd = -1;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500473
474 memset(data, 0, sb->size);
475
476 sb->pm_image =
477 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
478 (uint32_t *)data, stride);
Hardeninge57d1f22014-04-11 09:06:58 +0200479 if (!sb->pm_image)
480 goto out_pixman_error;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500481
482 return sb;
Hardeninge57d1f22014-04-11 09:06:58 +0200483
484out_pixman_error:
485 pixman_region32_fini(&sb->damage);
486out_unmap:
487 munmap(data, height * stride);
488out_close:
489 if (fd != -1)
490 close(fd);
491 return NULL;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500492}
493
494static void
495output_compute_transform(struct weston_output *output,
496 pixman_transform_t *transform)
497{
498 pixman_fixed_t fw, fh;
499
500 pixman_transform_init_identity(transform);
501
502 fw = pixman_int_to_fixed(output->width);
503 fh = pixman_int_to_fixed(output->height);
504
505 switch (output->transform) {
506 case WL_OUTPUT_TRANSFORM_FLIPPED:
507 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
508 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
509 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
510 pixman_transform_scale(transform, NULL,
511 pixman_int_to_fixed (-1),
512 pixman_int_to_fixed (1));
513 pixman_transform_translate(transform, NULL, fw, 0);
514 }
515
516 switch (output->transform) {
517 default:
518 case WL_OUTPUT_TRANSFORM_NORMAL:
519 case WL_OUTPUT_TRANSFORM_FLIPPED:
520 break;
521 case WL_OUTPUT_TRANSFORM_90:
522 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
523 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
524 pixman_transform_translate(transform, NULL, fh, 0);
525 break;
526 case WL_OUTPUT_TRANSFORM_180:
527 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
528 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
529 pixman_transform_translate(transform, NULL, fw, fh);
530 break;
531 case WL_OUTPUT_TRANSFORM_270:
532 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
533 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
534 pixman_transform_translate(transform, NULL, 0, fw);
535 break;
536 }
537
538 pixman_transform_scale(transform, NULL,
539 pixman_fixed_1 * output->current_scale,
540 pixman_fixed_1 * output->current_scale);
541}
542
543static void
544shared_output_destroy(struct shared_output *so);
545
546static int
547shared_output_ensure_tmp_data(struct shared_output *so,
548 pixman_region32_t *region)
549{
550 pixman_box32_t *ext;
551 size_t size;
552
553 if (!pixman_region32_not_empty(region))
554 return 0;
555
556 ext = pixman_region32_extents(region);
557
558 /* Damage is in output coordinates.
559 *
560 * We are multiplying by 4 because the temporary data needs to be able
561 * to store an 32 bit-per-pixel buffer.
562 */
563 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
564 * so->output->current_scale * so->output->current_scale;
565
566 if (so->tmp_data != NULL && size <= so->tmp_data_size)
567 return 0;
568
569 free(so->tmp_data);
570 so->tmp_data = malloc(size);
571 if (so->tmp_data == NULL) {
572 so->tmp_data_size = 0;
573 errno = ENOMEM;
574 return -1;
575 }
576
577 so->tmp_data_size = size;
578
579 return 0;
580}
581
582static void
583shared_output_update(struct shared_output *so);
584
585static void
586shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
587{
588 struct shared_output *so = data;
589
590 if (cb != so->parent.frame_cb)
591 return;
592
593 wl_callback_destroy(cb);
594 so->parent.frame_cb = NULL;
595
596 shared_output_update(so);
597}
598
599static const struct wl_callback_listener shared_output_frame_listener = {
600 shared_output_frame_callback
601};
602
603static void
604shared_output_update(struct shared_output *so)
605{
606 struct ss_shm_buffer *sb;
607 pixman_box32_t *r;
608 int i, nrects;
609 pixman_transform_t transform;
610
611 /* Only update if we need to */
612 if (!so->cache_dirty || so->parent.frame_cb)
613 return;
614
615 sb = shared_output_get_shm_buffer(so);
616 if (sb == NULL) {
617 shared_output_destroy(so);
618 return;
619 }
620
621 output_compute_transform(so->output, &transform);
622 pixman_image_set_transform(so->cache_image, &transform);
623
624 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
625
626 if (so->output->current_scale == 1) {
627 pixman_image_set_filter(so->cache_image,
628 PIXMAN_FILTER_NEAREST, NULL, 0);
629 } else {
630 pixman_image_set_filter(so->cache_image,
631 PIXMAN_FILTER_BILINEAR, NULL, 0);
632 }
633
634 pixman_image_composite32(PIXMAN_OP_SRC,
635 so->cache_image, /* src */
636 NULL, /* mask */
637 sb->pm_image, /* dest */
638 0, 0, /* src_x, src_y */
639 0, 0, /* mask_x, mask_y */
640 0, 0, /* dest_x, dest_y */
641 so->output->width, /* width */
642 so->output->height /* height */);
643
644 pixman_image_set_transform(sb->pm_image, NULL);
645 pixman_image_set_clip_region32(sb->pm_image, NULL);
646
647 r = pixman_region32_rectangles(&sb->damage, &nrects);
648 for (i = 0; i < nrects; ++i)
649 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
650 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
651
652 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
653
654 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
655 wl_callback_add_listener(so->parent.frame_cb,
656 &shared_output_frame_listener, so);
657
658 wl_surface_commit(so->parent.surface);
659 wl_callback_destroy(wl_display_sync(so->parent.display));
660 wl_display_flush(so->parent.display);
661
662 /* Clear the buffer damage */
663 pixman_region32_fini(&sb->damage);
664 pixman_region32_init(&sb->damage);
665}
666
667static void
668shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
669{
670 struct shared_output *so = data;
671
672 so->parent.shm_formats |= (1 << format);
673}
674
675struct wl_shm_listener shm_listener = {
676 shm_handle_format
677};
678
679static void
680registry_handle_global(void *data, struct wl_registry *registry,
681 uint32_t id, const char *interface, uint32_t version)
682{
683 struct shared_output *so = data;
684
685 if (strcmp(interface, "wl_compositor") == 0) {
686 so->parent.compositor =
687 wl_registry_bind(registry,
688 id, &wl_compositor_interface, 1);
689 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
690 so->parent.output =
691 wl_registry_bind(registry,
692 id, &wl_output_interface, 1);
693 } else if (strcmp(interface, "wl_seat") == 0) {
694 ss_seat_create(so, id);
695 } else if (strcmp(interface, "wl_shm") == 0) {
696 so->parent.shm =
697 wl_registry_bind(registry,
698 id, &wl_shm_interface, 1);
699 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
700 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
701 so->parent.fshell =
702 wl_registry_bind(registry,
703 id, &_wl_fullscreen_shell_interface, 1);
704 }
705}
706
707static void
708registry_handle_global_remove(void *data, struct wl_registry *registry,
709 uint32_t name)
710{
711}
712
713static const struct wl_registry_listener registry_listener = {
714 registry_handle_global,
715 registry_handle_global_remove
716};
717
718static int
719shared_output_handle_event(int fd, uint32_t mask, void *data)
720{
721 struct shared_output *so = data;
722 int count = 0;
723
724 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
725 shared_output_destroy(so);
726 return 0;
727 }
728
729 if (mask & WL_EVENT_READABLE)
730 count = wl_display_dispatch(so->parent.display);
731 if (mask & WL_EVENT_WRITABLE)
732 wl_display_flush(so->parent.display);
733
734 if (mask == 0) {
735 count = wl_display_dispatch_pending(so->parent.display);
736 wl_display_flush(so->parent.display);
737 }
738
739 return count;
740}
741
742static void
743output_destroyed(struct wl_listener *l, void *data)
744{
745 struct shared_output *so;
746
747 so = container_of(l, struct shared_output, output_destroyed);
748
749 shared_output_destroy(so);
750}
751
752static void
753mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
754{
755 struct shared_output *so = data;
756
757 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
758}
759
760static void
761mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
762{
763 struct shared_output *so = data;
764
765 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
766
767 weston_log("Screen share failed: present_surface_for_mode failed\n");
768 shared_output_destroy(so);
769}
770
771struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
772 mode_feedback_ok,
773 mode_feedback_failed,
774 mode_feedback_ok,
775};
776
777static void
778shared_output_repainted(struct wl_listener *listener, void *data)
779{
780 struct shared_output *so =
781 container_of(listener, struct shared_output, frame_listener);
782 pixman_region32_t damage;
783 struct ss_shm_buffer *sb;
784 int32_t x, y, width, height, stride;
785 int i, nrects, do_yflip;
786 pixman_box32_t *r;
787 uint32_t *cache_data;
788
789 /* Damage in output coordinates */
790 pixman_region32_init(&damage);
791 pixman_region32_intersect(&damage, &so->output->region,
792 &so->output->previous_damage);
793 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
794
795 /* Apply damage to all buffers */
796 wl_list_for_each(sb, &so->shm.buffers, link)
797 pixman_region32_union(&sb->damage, &sb->damage, &damage);
798
799 /* Transform to buffer coordinates */
800 weston_transformed_region(so->output->width, so->output->height,
801 so->output->transform,
802 so->output->current_scale,
803 &damage, &damage);
804
805 width = so->output->current_mode->width;
806 height = so->output->current_mode->height;
807 stride = width;
808
809 if (!so->cache_image ||
810 pixman_image_get_width(so->cache_image) != width ||
811 pixman_image_get_height(so->cache_image) != height) {
812 if (so->cache_image)
813 pixman_image_unref(so->cache_image);
814
815 so->cache_image =
816 pixman_image_create_bits(PIXMAN_a8r8g8b8,
817 width, height, NULL,
818 stride);
819 if (!so->cache_image) {
820 shared_output_destroy(so);
821 return;
822 }
823
824 pixman_region32_fini(&damage);
825 pixman_region32_init_rect(&damage, 0, 0, width, height);
826 }
827
828 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
829 shared_output_destroy(so);
830 return;
831 }
832
833 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
834
835 cache_data = pixman_image_get_data(so->cache_image);
836 r = pixman_region32_rectangles(&damage, &nrects);
837 for (i = 0; i < nrects; ++i) {
838 x = r[i].x1;
839 y = r[i].y1;
840 width = r[i].x2 - r[i].x1;
841 height = r[i].y2 - r[i].y1;
842
843 if (do_yflip) {
844 so->output->compositor->renderer->read_pixels(
845 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
846 x, so->output->current_mode->height - r[i].y2,
847 width, height);
848
849 pixman_blt(so->tmp_data, cache_data, -width, stride,
850 32, 32, 0, 1 - height, x, y, width, height);
851 } else {
852 so->output->compositor->renderer->read_pixels(
853 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
854 x, y, width, height);
855
856 pixman_blt(so->tmp_data, cache_data, width, stride,
857 32, 32, 0, 0, x, y, width, height);
858 }
859 }
860
861 pixman_region32_fini(&damage);
862
863 so->cache_dirty = 1;
864
865 shared_output_update(so);
866}
867
868static struct shared_output *
869shared_output_create(struct weston_output *output, int parent_fd)
870{
871 struct shared_output *so;
872 struct wl_event_loop *loop;
873 struct ss_seat *seat;
874 int epoll_fd;
875
876 so = zalloc(sizeof *so);
877 if (so == NULL)
878 goto err_close;
879
880 wl_list_init(&so->seat_list);
881
882 so->parent.display = wl_display_connect_to_fd(parent_fd);
883 if (!so->parent.display)
884 goto err_alloc;
885
886 so->parent.registry = wl_display_get_registry(so->parent.display);
887 if (!so->parent.registry)
888 goto err_display;
889 wl_registry_add_listener(so->parent.registry,
890 &registry_listener, so);
891 wl_display_roundtrip(so->parent.display);
892 if (so->parent.shm == NULL) {
893 weston_log("Screen share failed: No wl_shm found\n");
894 goto err_display;
895 }
896 if (so->parent.fshell == NULL) {
897 weston_log("Screen share failed: "
898 "Parent does not support wl_fullscreen_shell\n");
899 goto err_display;
900 }
901 if (so->parent.compositor == NULL) {
902 weston_log("Screen share failed: No wl_compositor found\n");
903 goto err_display;
904 }
905
906 /* Get SHM formats */
907 wl_display_roundtrip(so->parent.display);
908 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
909 weston_log("Screen share failed: "
910 "WL_SHM_FORMAT_XRGB8888 not available\n");
911 goto err_display;
912 }
913
914 so->parent.surface =
915 wl_compositor_create_surface(so->parent.compositor);
916 if (!so->parent.surface) {
917 weston_log("Screen share failed: %m");
918 goto err_display;
919 }
920
921 so->parent.mode_feedback =
922 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
923 so->parent.surface,
924 so->parent.output,
925 output->current_mode->refresh);
926 if (!so->parent.mode_feedback) {
927 weston_log("Screen share failed: %m");
928 goto err_display;
929 }
930 _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
931 &mode_feedback_listener,
932 so);
933
934 loop = wl_display_get_event_loop(output->compositor->wl_display);
935
936 epoll_fd = wl_display_get_fd(so->parent.display);
937 so->event_source =
938 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
939 shared_output_handle_event, so);
940 if (!so->event_source) {
941 weston_log("Screen share failed: %m");
942 goto err_display;
943 }
944
945 /* Ok, everything's created. We should be good to go */
946 wl_list_init(&so->shm.buffers);
947 wl_list_init(&so->shm.free_buffers);
948
949 so->output = output;
950 so->output_destroyed.notify = output_destroyed;
951 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
952
953 so->frame_listener.notify = shared_output_repainted;
954 wl_signal_add(&output->frame_signal, &so->frame_listener);
955 output->disable_planes++;
956 weston_output_damage(output);
957
958 return so;
959
960err_display:
961 wl_list_for_each(seat, &so->seat_list, link)
962 ss_seat_destroy(seat);
963 wl_display_disconnect(so->parent.display);
964err_alloc:
965 free(so);
966err_close:
967 close(parent_fd);
968 return NULL;
969}
970
971static void
972shared_output_destroy(struct shared_output *so)
973{
974 struct ss_shm_buffer *buffer, *bnext;
975
Jason Ekstrand27efc062014-04-03 11:51:42 -0500976 so->output->disable_planes--;
977
Jason Ekstrand47928d82014-04-02 19:54:01 -0500978 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
979 ss_shm_buffer_destroy(buffer);
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700980 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500981 ss_shm_buffer_destroy(buffer);
982
983 wl_display_disconnect(so->parent.display);
984 wl_event_source_remove(so->event_source);
985
986 wl_list_remove(&so->output_destroyed.link);
987 wl_list_remove(&so->frame_listener.link);
988
989 pixman_image_unref(so->cache_image);
990 free(so->tmp_data);
991
992 free(so);
993}
994
995static struct shared_output *
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100996weston_output_share(struct weston_output *output, const char* command)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500997{
998 int sv[2];
999 char str[32];
1000 pid_t pid;
1001 sigset_t allsigs;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001002 char *const argv[] = {
1003 "/bin/sh",
1004 "-c",
1005 (char*)command,
1006 NULL
1007 };
Jason Ekstrand47928d82014-04-02 19:54:01 -05001008
1009 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1010 weston_log("weston_output_share: socketpair failed: %m\n");
1011 return NULL;
1012 }
1013
1014 pid = fork();
1015
1016 if (pid == -1) {
1017 close(sv[0]);
1018 close(sv[1]);
1019 weston_log("weston_output_share: fork failed: %m\n");
1020 return NULL;
1021 }
1022
1023 if (pid == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001024 /* do not give our signal mask to the new process */
1025 sigfillset(&allsigs);
1026 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1027
1028 /* Launch clients as the user. Do not launch clients with
1029 * wrong euid. */
1030 if (seteuid(getuid()) == -1) {
1031 weston_log("weston_output_share: setuid failed: %m\n");
1032 abort();
1033 }
1034
1035 sv[1] = dup(sv[1]);
1036 if (sv[1] == -1) {
1037 weston_log("weston_output_share: dup failed: %m\n");
1038 abort();
1039 }
1040
1041 snprintf(str, sizeof str, "%d", sv[1]);
1042 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1043
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001044 execv(argv[0], argv);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001045 weston_log("weston_output_share: exec failed: %m\n");
1046 abort();
1047 } else {
1048 close(sv[1]);
1049 return shared_output_create(output, sv[0]);
1050 }
1051
1052 return NULL;
1053}
1054
1055static struct weston_output *
1056weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1057{
1058 struct weston_output *output;
1059
1060 wl_list_for_each(output, &c->output_list, link) {
1061 if (x >= output->x && y >= output->y &&
1062 x < output->x + output->width &&
1063 y < output->y + output->height)
1064 return output;
1065 }
1066
1067 return NULL;
1068}
1069
1070static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001071share_output_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Jason Ekstrand47928d82014-04-02 19:54:01 -05001072 void *data)
1073{
1074 struct weston_output *output;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001075 struct weston_pointer *pointer;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001076 struct screen_share *ss = data;
Jason Ekstrand47928d82014-04-02 19:54:01 -05001077
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001078 pointer = weston_seat_get_pointer(keyboard->seat);
1079 if (!pointer) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001080 weston_log("Cannot pick output: Seat does not have pointer\n");
1081 return;
1082 }
1083
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001084 output = weston_output_find(pointer->seat->compositor,
1085 wl_fixed_to_int(pointer->x),
1086 wl_fixed_to_int(pointer->y));
Jason Ekstrand47928d82014-04-02 19:54:01 -05001087 if (!output) {
1088 weston_log("Cannot pick output: Pointer not on any output\n");
1089 return;
1090 }
1091
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001092 weston_output_share(output, ss->command);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001093}
1094
1095WL_EXPORT int
1096module_init(struct weston_compositor *compositor,
1097 int *argc, char *argv[])
1098{
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001099 struct screen_share *ss;
1100 struct weston_config_section *section;
1101
1102 ss = zalloc(sizeof *ss);
1103 if (ss == NULL)
1104 return -1;
1105 ss->compositor = compositor;
1106
1107 section = weston_config_get_section(compositor->config, "screen-share",
1108 NULL, NULL);
1109
1110 weston_config_section_get_string(section, "command", &ss->command, "");
1111
Jason Ekstrand47928d82014-04-02 19:54:01 -05001112 weston_compositor_add_key_binding(compositor, KEY_S,
1113 MODIFIER_CTRL | MODIFIER_ALT,
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001114 share_output_binding, ss);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001115 return 0;
1116}