blob: c9e7436f247eefdc8179649abb4d8fb0aa0ceb94 [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"
Daniel Stonec7d88fa2016-06-18 16:02:38 +100043#include "weston.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070044#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070045#include "shared/os-compatibility.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080046#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jason Ekstrand47928d82014-04-02 19:54:01 -050047
48struct shared_output {
49 struct weston_output *output;
50 struct wl_listener output_destroyed;
51 struct wl_list seat_list;
52
53 struct {
54 struct wl_display *display;
55 struct wl_registry *registry;
56 struct wl_compositor *compositor;
57 struct wl_shm *shm;
58 uint32_t shm_formats;
Jonas Ådahl496adb32015-11-17 16:00:27 +080059 struct zwp_fullscreen_shell_v1 *fshell;
Jason Ekstrand47928d82014-04-02 19:54:01 -050060 struct wl_output *output;
61 struct wl_surface *surface;
62 struct wl_callback *frame_cb;
Jonas Ådahl496adb32015-11-17 16:00:27 +080063 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand47928d82014-04-02 19:54:01 -050064 } parent;
65
66 struct wl_event_source *event_source;
67 struct wl_listener frame_listener;
68
69 struct {
70 int32_t width, height;
71
72 struct wl_list buffers;
73 struct wl_list free_buffers;
74 } shm;
75
76 int cache_dirty;
77 pixman_image_t *cache_image;
78 uint32_t *tmp_data;
79 size_t tmp_data_size;
80};
81
82struct ss_seat {
83 struct weston_seat base;
84 struct shared_output *output;
85 struct wl_list link;
86
87 struct {
88 struct wl_seat *seat;
89 struct wl_pointer *pointer;
90 struct wl_keyboard *keyboard;
91 } parent;
92
93 enum weston_key_state_update keyboard_state_update;
94 uint32_t key_serial;
95};
96
97struct ss_shm_buffer {
98 struct shared_output *output;
99 struct wl_list link;
100 struct wl_list free_link;
101
102 struct wl_buffer *buffer;
103 void *data;
104 size_t size;
105 pixman_region32_t damage;
106
107 pixman_image_t *pm_image;
108};
109
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100110struct screen_share {
111 struct weston_compositor *compositor;
112 char *command;
113};
114
Jason Ekstrand47928d82014-04-02 19:54:01 -0500115static void
116ss_seat_handle_pointer_enter(void *data, struct wl_pointer *pointer,
117 uint32_t serial, struct wl_surface *surface,
118 wl_fixed_t x, wl_fixed_t y)
119{
120 struct ss_seat *seat = data;
121
122 /* No transformation of input position is required here because we are
123 * always receiving the input in the same coordinates as the output. */
124
125 notify_pointer_focus(&seat->base, NULL, 0, 0);
126}
127
128static void
129ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer,
130 uint32_t serial, struct wl_surface *surface)
131{
132 struct ss_seat *seat = data;
133
134 notify_pointer_focus(&seat->base, NULL, 0, 0);
135}
136
137static void
138ss_seat_handle_motion(void *data, struct wl_pointer *pointer,
139 uint32_t time, wl_fixed_t x, wl_fixed_t y)
140{
141 struct ss_seat *seat = data;
142
143 /* No transformation of input position is required here because we are
144 * always receiving the input in the same coordinates as the output. */
145
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200146 notify_motion_absolute(&seat->base, time,
147 wl_fixed_to_double(x), wl_fixed_to_double(y));
Peter Hutterer87743e92016-01-18 16:38:22 +1000148 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500149}
150
151static void
152ss_seat_handle_button(void *data, struct wl_pointer *pointer,
153 uint32_t serial, uint32_t time, uint32_t button,
154 uint32_t state)
155{
156 struct ss_seat *seat = data;
157
158 notify_button(&seat->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +1000159 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500160}
161
162static void
163ss_seat_handle_axis(void *data, struct wl_pointer *pointer,
164 uint32_t time, uint32_t axis, wl_fixed_t value)
165{
166 struct ss_seat *seat = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000167 struct weston_pointer_axis_event weston_event;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500168
Peter Hutterer89b6a492016-01-18 15:58:17 +1000169 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200170 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer87743e92016-01-18 16:38:22 +1000171 weston_event.has_discrete = false;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000172
173 notify_axis(&seat->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000174 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500175}
176
177static const struct wl_pointer_listener ss_seat_pointer_listener = {
178 ss_seat_handle_pointer_enter,
179 ss_seat_handle_pointer_leave,
180 ss_seat_handle_motion,
181 ss_seat_handle_button,
182 ss_seat_handle_axis,
183};
184
185static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300186ss_seat_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500187 uint32_t format, int fd, uint32_t size)
188{
189 struct ss_seat *seat = data;
190 struct xkb_keymap *keymap;
191 char *map_str;
192
193 if (!data)
194 goto error;
195
196 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
197 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
198 if (map_str == MAP_FAILED) {
199 weston_log("mmap failed: %m\n");
200 goto error;
201 }
202
Ran Benita2e1968f2014-08-19 23:59:51 +0300203 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
204 map_str,
205 XKB_KEYMAP_FORMAT_TEXT_V1,
206 0);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500207 munmap(map_str, size);
208
209 if (!keymap) {
210 weston_log("failed to compile keymap\n");
211 goto error;
212 }
213
214 seat->keyboard_state_update = STATE_UPDATE_NONE;
215 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
216 weston_log("No keymap provided; falling back to default\n");
217 keymap = NULL;
218 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
219 } else {
220 weston_log("Invalid keymap\n");
221 goto error;
222 }
223
224 close(fd);
225
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300226 if (seat->base.keyboard_device_count)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500227 weston_seat_update_keymap(&seat->base, keymap);
228 else
229 weston_seat_init_keyboard(&seat->base, keymap);
230
Ran Benitac9c74152014-08-19 23:59:52 +0300231 xkb_keymap_unref(keymap);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500232
233 return;
234
235error:
236 wl_keyboard_release(seat->parent.keyboard);
237 close(fd);
238}
239
240static void
241ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
242 uint32_t serial, struct wl_surface *surface,
243 struct wl_array *keys)
244{
245 struct ss_seat *seat = data;
246
247 /* XXX: If we get a modifier event immediately before the focus,
248 * we should try to keep the same serial. */
249 notify_keyboard_focus_in(&seat->base, keys,
250 STATE_UPDATE_AUTOMATIC);
251}
252
253static void
254ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
255 uint32_t serial, struct wl_surface *surface)
256{
257 struct ss_seat *seat = data;
258
259 notify_keyboard_focus_out(&seat->base);
260}
261
262static void
263ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
264 uint32_t serial, uint32_t time,
265 uint32_t key, uint32_t state)
266{
267 struct ss_seat *seat = data;
268
269 seat->key_serial = serial;
270 notify_key(&seat->base, time, key,
271 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
272 WL_KEYBOARD_KEY_STATE_RELEASED,
273 seat->keyboard_state_update);
274}
275
276static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300277ss_seat_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500278 uint32_t serial_in, uint32_t mods_depressed,
279 uint32_t mods_latched, uint32_t mods_locked,
280 uint32_t group)
281{
282 struct ss_seat *seat = data;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300283 struct weston_compositor *c = seat->base.compositor;
284 struct weston_keyboard *keyboard;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500285 uint32_t serial_out;
286
287 /* If we get a key event followed by a modifier event with the
288 * same serial number, then we try to preserve those semantics by
289 * reusing the same serial number on the way out too. */
290 if (serial_in == seat->key_serial)
291 serial_out = wl_display_get_serial(c->wl_display);
292 else
293 serial_out = wl_display_next_serial(c->wl_display);
294
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300295 keyboard = weston_seat_get_keyboard(&seat->base);
296 xkb_state_update_mask(keyboard->xkb_state.state,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500297 mods_depressed, mods_latched,
298 mods_locked, 0, 0, group);
299 notify_modifiers(&seat->base, serial_out);
300}
301
302static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
303 ss_seat_handle_keymap,
304 ss_seat_handle_keyboard_enter,
305 ss_seat_handle_keyboard_leave,
306 ss_seat_handle_key,
307 ss_seat_handle_modifiers,
308};
309
310static void
311ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
312 enum wl_seat_capability caps)
313{
314 struct ss_seat *ss_seat = data;
315
316 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
317 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
318 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
319 wl_pointer_add_listener(ss_seat->parent.pointer,
320 &ss_seat_pointer_listener, ss_seat);
321 weston_seat_init_pointer(&ss_seat->base);
322 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
323 wl_pointer_destroy(ss_seat->parent.pointer);
324 ss_seat->parent.pointer = NULL;
325 }
326
327 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
328 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
329 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
330 wl_keyboard_add_listener(ss_seat->parent.keyboard,
331 &ss_seat_keyboard_listener, ss_seat);
332 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
333 wl_keyboard_destroy(ss_seat->parent.keyboard);
334 ss_seat->parent.keyboard = NULL;
335 }
336}
337
338static const struct wl_seat_listener ss_seat_listener = {
339 ss_seat_handle_capabilities,
340};
341
342static struct ss_seat *
343ss_seat_create(struct shared_output *so, uint32_t id)
344{
345 struct ss_seat *seat;
346
347 seat = zalloc(sizeof *seat);
348 if (seat == NULL)
349 return NULL;
350
351 weston_seat_init(&seat->base, so->output->compositor, "default");
352 seat->output = so;
353 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
354 &wl_seat_interface, 1);
355 wl_list_insert(so->seat_list.prev, &seat->link);
356
357 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
358 wl_seat_set_user_data(seat->parent.seat, seat);
359
360 return seat;
361}
362
363static void
364ss_seat_destroy(struct ss_seat *seat)
365{
366 if (seat->parent.pointer)
367 wl_pointer_release(seat->parent.pointer);
368 if (seat->parent.keyboard)
369 wl_keyboard_release(seat->parent.keyboard);
370 wl_seat_destroy(seat->parent.seat);
371
372 wl_list_remove(&seat->link);
373
374 weston_seat_release(&seat->base);
375
376 free(seat);
377}
378
379static void
380ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
381{
382 pixman_image_unref(buffer->pm_image);
383
384 wl_buffer_destroy(buffer->buffer);
385 munmap(buffer->data, buffer->size);
386
387 pixman_region32_fini(&buffer->damage);
388
389 wl_list_remove(&buffer->link);
390 wl_list_remove(&buffer->free_link);
391 free(buffer);
392}
393
394static void
395buffer_release(void *data, struct wl_buffer *buffer)
396{
397 struct ss_shm_buffer *sb = data;
398
399 if (sb->output) {
400 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
401 } else {
402 ss_shm_buffer_destroy(sb);
403 }
404}
405
406static const struct wl_buffer_listener buffer_listener = {
407 buffer_release
408};
409
410static struct ss_shm_buffer *
411shared_output_get_shm_buffer(struct shared_output *so)
412{
413 struct ss_shm_buffer *sb, *bnext;
414 struct wl_shm_pool *pool;
415 int width, height, stride;
416 int fd;
417 unsigned char *data;
418
419 width = so->output->width;
420 height = so->output->height;
421 stride = width * 4;
422
423 /* If the size of the output changed, we free the old buffers and
424 * make new ones. */
425 if (so->shm.width != width ||
426 so->shm.height != height) {
427
428 /* Destroy free buffers */
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700429 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500430 ss_shm_buffer_destroy(sb);
431
432 /* Orphan in-use buffers so they get destroyed */
433 wl_list_for_each(sb, &so->shm.buffers, link)
434 sb->output = NULL;
435
436 so->shm.width = width;
437 so->shm.height = height;
438 }
439
440 if (!wl_list_empty(&so->shm.free_buffers)) {
441 sb = container_of(so->shm.free_buffers.next,
442 struct ss_shm_buffer, free_link);
443 wl_list_remove(&sb->free_link);
444 wl_list_init(&sb->free_link);
445
446 return sb;
447 }
448
449 fd = os_create_anonymous_file(height * stride);
450 if (fd < 0) {
Chris Michael5169a572015-10-01 10:51:30 -0400451 weston_log("os_create_anonymous_file: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500452 return NULL;
453 }
454
455 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
456 if (data == MAP_FAILED) {
Chris Michael5169a572015-10-01 10:51:30 -0400457 weston_log("mmap: %m\n");
Hardeninge57d1f22014-04-11 09:06:58 +0200458 goto out_close;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500459 }
460
461 sb = zalloc(sizeof *sb);
Hardeninge57d1f22014-04-11 09:06:58 +0200462 if (!sb)
463 goto out_unmap;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500464
465 sb->output = so;
466 wl_list_init(&sb->free_link);
467 wl_list_insert(&so->shm.buffers, &sb->link);
468
469 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
470
471 sb->data = data;
472 sb->size = height * stride;
473
474 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
475
476 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
477 width, height, stride,
478 WL_SHM_FORMAT_ARGB8888);
479 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
480 wl_shm_pool_destroy(pool);
481 close(fd);
Hardeninge57d1f22014-04-11 09:06:58 +0200482 fd = -1;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500483
484 memset(data, 0, sb->size);
485
486 sb->pm_image =
487 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
488 (uint32_t *)data, stride);
Hardeninge57d1f22014-04-11 09:06:58 +0200489 if (!sb->pm_image)
490 goto out_pixman_error;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500491
492 return sb;
Hardeninge57d1f22014-04-11 09:06:58 +0200493
494out_pixman_error:
495 pixman_region32_fini(&sb->damage);
496out_unmap:
497 munmap(data, height * stride);
498out_close:
499 if (fd != -1)
500 close(fd);
501 return NULL;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500502}
503
504static void
505output_compute_transform(struct weston_output *output,
506 pixman_transform_t *transform)
507{
508 pixman_fixed_t fw, fh;
509
510 pixman_transform_init_identity(transform);
511
512 fw = pixman_int_to_fixed(output->width);
513 fh = pixman_int_to_fixed(output->height);
514
515 switch (output->transform) {
516 case WL_OUTPUT_TRANSFORM_FLIPPED:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
518 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
519 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
520 pixman_transform_scale(transform, NULL,
521 pixman_int_to_fixed (-1),
522 pixman_int_to_fixed (1));
523 pixman_transform_translate(transform, NULL, fw, 0);
524 }
525
526 switch (output->transform) {
527 default:
528 case WL_OUTPUT_TRANSFORM_NORMAL:
529 case WL_OUTPUT_TRANSFORM_FLIPPED:
530 break;
531 case WL_OUTPUT_TRANSFORM_90:
532 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
533 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
534 pixman_transform_translate(transform, NULL, fh, 0);
535 break;
536 case WL_OUTPUT_TRANSFORM_180:
537 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
538 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
539 pixman_transform_translate(transform, NULL, fw, fh);
540 break;
541 case WL_OUTPUT_TRANSFORM_270:
542 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
543 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
544 pixman_transform_translate(transform, NULL, 0, fw);
545 break;
546 }
547
548 pixman_transform_scale(transform, NULL,
549 pixman_fixed_1 * output->current_scale,
550 pixman_fixed_1 * output->current_scale);
551}
552
553static void
554shared_output_destroy(struct shared_output *so);
555
556static int
557shared_output_ensure_tmp_data(struct shared_output *so,
558 pixman_region32_t *region)
559{
560 pixman_box32_t *ext;
561 size_t size;
562
563 if (!pixman_region32_not_empty(region))
564 return 0;
565
566 ext = pixman_region32_extents(region);
567
568 /* Damage is in output coordinates.
569 *
570 * We are multiplying by 4 because the temporary data needs to be able
571 * to store an 32 bit-per-pixel buffer.
572 */
573 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
574 * so->output->current_scale * so->output->current_scale;
575
576 if (so->tmp_data != NULL && size <= so->tmp_data_size)
577 return 0;
578
579 free(so->tmp_data);
580 so->tmp_data = malloc(size);
581 if (so->tmp_data == NULL) {
582 so->tmp_data_size = 0;
583 errno = ENOMEM;
584 return -1;
585 }
586
587 so->tmp_data_size = size;
588
589 return 0;
590}
591
592static void
593shared_output_update(struct shared_output *so);
594
595static void
596shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
597{
598 struct shared_output *so = data;
599
600 if (cb != so->parent.frame_cb)
601 return;
602
603 wl_callback_destroy(cb);
604 so->parent.frame_cb = NULL;
605
606 shared_output_update(so);
607}
608
609static const struct wl_callback_listener shared_output_frame_listener = {
610 shared_output_frame_callback
611};
612
613static void
614shared_output_update(struct shared_output *so)
615{
616 struct ss_shm_buffer *sb;
617 pixman_box32_t *r;
618 int i, nrects;
619 pixman_transform_t transform;
620
621 /* Only update if we need to */
622 if (!so->cache_dirty || so->parent.frame_cb)
623 return;
624
625 sb = shared_output_get_shm_buffer(so);
626 if (sb == NULL) {
627 shared_output_destroy(so);
628 return;
629 }
630
631 output_compute_transform(so->output, &transform);
632 pixman_image_set_transform(so->cache_image, &transform);
633
634 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
635
636 if (so->output->current_scale == 1) {
637 pixman_image_set_filter(so->cache_image,
638 PIXMAN_FILTER_NEAREST, NULL, 0);
639 } else {
640 pixman_image_set_filter(so->cache_image,
641 PIXMAN_FILTER_BILINEAR, NULL, 0);
642 }
643
644 pixman_image_composite32(PIXMAN_OP_SRC,
645 so->cache_image, /* src */
646 NULL, /* mask */
647 sb->pm_image, /* dest */
648 0, 0, /* src_x, src_y */
649 0, 0, /* mask_x, mask_y */
650 0, 0, /* dest_x, dest_y */
651 so->output->width, /* width */
652 so->output->height /* height */);
653
654 pixman_image_set_transform(sb->pm_image, NULL);
655 pixman_image_set_clip_region32(sb->pm_image, NULL);
656
657 r = pixman_region32_rectangles(&sb->damage, &nrects);
658 for (i = 0; i < nrects; ++i)
659 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
660 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
661
662 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
663
664 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
665 wl_callback_add_listener(so->parent.frame_cb,
666 &shared_output_frame_listener, so);
667
668 wl_surface_commit(so->parent.surface);
669 wl_callback_destroy(wl_display_sync(so->parent.display));
670 wl_display_flush(so->parent.display);
671
672 /* Clear the buffer damage */
673 pixman_region32_fini(&sb->damage);
674 pixman_region32_init(&sb->damage);
675}
676
677static void
678shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
679{
680 struct shared_output *so = data;
681
682 so->parent.shm_formats |= (1 << format);
683}
684
685struct wl_shm_listener shm_listener = {
686 shm_handle_format
687};
688
689static void
690registry_handle_global(void *data, struct wl_registry *registry,
691 uint32_t id, const char *interface, uint32_t version)
692{
693 struct shared_output *so = data;
694
695 if (strcmp(interface, "wl_compositor") == 0) {
696 so->parent.compositor =
697 wl_registry_bind(registry,
698 id, &wl_compositor_interface, 1);
699 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
700 so->parent.output =
701 wl_registry_bind(registry,
702 id, &wl_output_interface, 1);
703 } else if (strcmp(interface, "wl_seat") == 0) {
704 ss_seat_create(so, id);
705 } else if (strcmp(interface, "wl_shm") == 0) {
706 so->parent.shm =
707 wl_registry_bind(registry,
708 id, &wl_shm_interface, 1);
709 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800710 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500711 so->parent.fshell =
712 wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800713 id,
714 &zwp_fullscreen_shell_v1_interface,
715 1);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500716 }
717}
718
719static void
720registry_handle_global_remove(void *data, struct wl_registry *registry,
721 uint32_t name)
722{
723}
724
725static const struct wl_registry_listener registry_listener = {
726 registry_handle_global,
727 registry_handle_global_remove
728};
729
730static int
731shared_output_handle_event(int fd, uint32_t mask, void *data)
732{
733 struct shared_output *so = data;
734 int count = 0;
735
736 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
737 shared_output_destroy(so);
738 return 0;
739 }
740
741 if (mask & WL_EVENT_READABLE)
742 count = wl_display_dispatch(so->parent.display);
743 if (mask & WL_EVENT_WRITABLE)
744 wl_display_flush(so->parent.display);
745
746 if (mask == 0) {
747 count = wl_display_dispatch_pending(so->parent.display);
748 wl_display_flush(so->parent.display);
749 }
750
751 return count;
752}
753
754static void
755output_destroyed(struct wl_listener *l, void *data)
756{
757 struct shared_output *so;
758
759 so = container_of(l, struct shared_output, output_destroyed);
760
761 shared_output_destroy(so);
762}
763
764static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800765mode_feedback_ok(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500766{
767 struct shared_output *so = data;
768
Jonas Ådahl496adb32015-11-17 16:00:27 +0800769 zwp_fullscreen_shell_mode_feedback_v1_destroy(so->parent.mode_feedback);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500770}
771
772static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800773mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500774{
775 struct shared_output *so = data;
776
Jonas Ådahl496adb32015-11-17 16:00:27 +0800777 zwp_fullscreen_shell_mode_feedback_v1_destroy(so->parent.mode_feedback);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500778
779 weston_log("Screen share failed: present_surface_for_mode failed\n");
780 shared_output_destroy(so);
781}
782
Jonas Ådahl496adb32015-11-17 16:00:27 +0800783struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500784 mode_feedback_ok,
785 mode_feedback_failed,
786 mode_feedback_ok,
787};
788
789static void
790shared_output_repainted(struct wl_listener *listener, void *data)
791{
792 struct shared_output *so =
793 container_of(listener, struct shared_output, frame_listener);
794 pixman_region32_t damage;
795 struct ss_shm_buffer *sb;
796 int32_t x, y, width, height, stride;
797 int i, nrects, do_yflip;
798 pixman_box32_t *r;
799 uint32_t *cache_data;
800
801 /* Damage in output coordinates */
802 pixman_region32_init(&damage);
803 pixman_region32_intersect(&damage, &so->output->region,
804 &so->output->previous_damage);
805 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
806
807 /* Apply damage to all buffers */
808 wl_list_for_each(sb, &so->shm.buffers, link)
809 pixman_region32_union(&sb->damage, &sb->damage, &damage);
810
811 /* Transform to buffer coordinates */
812 weston_transformed_region(so->output->width, so->output->height,
813 so->output->transform,
814 so->output->current_scale,
815 &damage, &damage);
816
817 width = so->output->current_mode->width;
818 height = so->output->current_mode->height;
819 stride = width;
820
821 if (!so->cache_image ||
822 pixman_image_get_width(so->cache_image) != width ||
823 pixman_image_get_height(so->cache_image) != height) {
824 if (so->cache_image)
825 pixman_image_unref(so->cache_image);
826
827 so->cache_image =
828 pixman_image_create_bits(PIXMAN_a8r8g8b8,
829 width, height, NULL,
830 stride);
831 if (!so->cache_image) {
832 shared_output_destroy(so);
833 return;
834 }
835
836 pixman_region32_fini(&damage);
837 pixman_region32_init_rect(&damage, 0, 0, width, height);
838 }
839
840 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
841 shared_output_destroy(so);
842 return;
843 }
844
845 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
846
847 cache_data = pixman_image_get_data(so->cache_image);
848 r = pixman_region32_rectangles(&damage, &nrects);
849 for (i = 0; i < nrects; ++i) {
850 x = r[i].x1;
851 y = r[i].y1;
852 width = r[i].x2 - r[i].x1;
853 height = r[i].y2 - r[i].y1;
854
855 if (do_yflip) {
856 so->output->compositor->renderer->read_pixels(
857 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
858 x, so->output->current_mode->height - r[i].y2,
859 width, height);
860
861 pixman_blt(so->tmp_data, cache_data, -width, stride,
862 32, 32, 0, 1 - height, x, y, width, height);
863 } else {
864 so->output->compositor->renderer->read_pixels(
865 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
866 x, y, width, height);
867
868 pixman_blt(so->tmp_data, cache_data, width, stride,
869 32, 32, 0, 0, x, y, width, height);
870 }
871 }
872
873 pixman_region32_fini(&damage);
874
875 so->cache_dirty = 1;
876
877 shared_output_update(so);
878}
879
880static struct shared_output *
881shared_output_create(struct weston_output *output, int parent_fd)
882{
883 struct shared_output *so;
884 struct wl_event_loop *loop;
885 struct ss_seat *seat;
886 int epoll_fd;
887
888 so = zalloc(sizeof *so);
889 if (so == NULL)
890 goto err_close;
891
892 wl_list_init(&so->seat_list);
893
894 so->parent.display = wl_display_connect_to_fd(parent_fd);
895 if (!so->parent.display)
896 goto err_alloc;
897
898 so->parent.registry = wl_display_get_registry(so->parent.display);
899 if (!so->parent.registry)
900 goto err_display;
901 wl_registry_add_listener(so->parent.registry,
902 &registry_listener, so);
903 wl_display_roundtrip(so->parent.display);
904 if (so->parent.shm == NULL) {
905 weston_log("Screen share failed: No wl_shm found\n");
906 goto err_display;
907 }
908 if (so->parent.fshell == NULL) {
909 weston_log("Screen share failed: "
910 "Parent does not support wl_fullscreen_shell\n");
911 goto err_display;
912 }
913 if (so->parent.compositor == NULL) {
914 weston_log("Screen share failed: No wl_compositor found\n");
915 goto err_display;
916 }
917
918 /* Get SHM formats */
919 wl_display_roundtrip(so->parent.display);
920 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
921 weston_log("Screen share failed: "
922 "WL_SHM_FORMAT_XRGB8888 not available\n");
923 goto err_display;
924 }
925
926 so->parent.surface =
927 wl_compositor_create_surface(so->parent.compositor);
928 if (!so->parent.surface) {
Chris Michael5169a572015-10-01 10:51:30 -0400929 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500930 goto err_display;
931 }
932
933 so->parent.mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800934 zwp_fullscreen_shell_v1_present_surface_for_mode(so->parent.fshell,
935 so->parent.surface,
936 so->parent.output,
937 output->current_mode->refresh);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500938 if (!so->parent.mode_feedback) {
Chris Michael5169a572015-10-01 10:51:30 -0400939 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500940 goto err_display;
941 }
Jonas Ådahl496adb32015-11-17 16:00:27 +0800942 zwp_fullscreen_shell_mode_feedback_v1_add_listener(so->parent.mode_feedback,
943 &mode_feedback_listener,
944 so);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500945
946 loop = wl_display_get_event_loop(output->compositor->wl_display);
947
948 epoll_fd = wl_display_get_fd(so->parent.display);
949 so->event_source =
950 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
951 shared_output_handle_event, so);
952 if (!so->event_source) {
Chris Michael5169a572015-10-01 10:51:30 -0400953 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500954 goto err_display;
955 }
956
957 /* Ok, everything's created. We should be good to go */
958 wl_list_init(&so->shm.buffers);
959 wl_list_init(&so->shm.free_buffers);
960
961 so->output = output;
962 so->output_destroyed.notify = output_destroyed;
963 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
964
965 so->frame_listener.notify = shared_output_repainted;
966 wl_signal_add(&output->frame_signal, &so->frame_listener);
967 output->disable_planes++;
968 weston_output_damage(output);
969
970 return so;
971
972err_display:
973 wl_list_for_each(seat, &so->seat_list, link)
974 ss_seat_destroy(seat);
975 wl_display_disconnect(so->parent.display);
976err_alloc:
977 free(so);
978err_close:
979 close(parent_fd);
980 return NULL;
981}
982
983static void
984shared_output_destroy(struct shared_output *so)
985{
986 struct ss_shm_buffer *buffer, *bnext;
987
Jason Ekstrand27efc062014-04-03 11:51:42 -0500988 so->output->disable_planes--;
989
Jason Ekstrand47928d82014-04-02 19:54:01 -0500990 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
991 ss_shm_buffer_destroy(buffer);
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700992 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500993 ss_shm_buffer_destroy(buffer);
994
995 wl_display_disconnect(so->parent.display);
996 wl_event_source_remove(so->event_source);
997
998 wl_list_remove(&so->output_destroyed.link);
999 wl_list_remove(&so->frame_listener.link);
1000
1001 pixman_image_unref(so->cache_image);
1002 free(so->tmp_data);
1003
1004 free(so);
1005}
1006
1007static struct shared_output *
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001008weston_output_share(struct weston_output *output, const char* command)
Jason Ekstrand47928d82014-04-02 19:54:01 -05001009{
1010 int sv[2];
1011 char str[32];
1012 pid_t pid;
1013 sigset_t allsigs;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001014 char *const argv[] = {
1015 "/bin/sh",
1016 "-c",
1017 (char*)command,
1018 NULL
1019 };
Jason Ekstrand47928d82014-04-02 19:54:01 -05001020
1021 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1022 weston_log("weston_output_share: socketpair failed: %m\n");
1023 return NULL;
1024 }
1025
1026 pid = fork();
1027
1028 if (pid == -1) {
1029 close(sv[0]);
1030 close(sv[1]);
1031 weston_log("weston_output_share: fork failed: %m\n");
1032 return NULL;
1033 }
1034
1035 if (pid == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001036 /* do not give our signal mask to the new process */
1037 sigfillset(&allsigs);
1038 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1039
1040 /* Launch clients as the user. Do not launch clients with
1041 * wrong euid. */
1042 if (seteuid(getuid()) == -1) {
1043 weston_log("weston_output_share: setuid failed: %m\n");
1044 abort();
1045 }
1046
1047 sv[1] = dup(sv[1]);
1048 if (sv[1] == -1) {
1049 weston_log("weston_output_share: dup failed: %m\n");
1050 abort();
1051 }
1052
1053 snprintf(str, sizeof str, "%d", sv[1]);
1054 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1055
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001056 execv(argv[0], argv);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001057 weston_log("weston_output_share: exec failed: %m\n");
1058 abort();
1059 } else {
1060 close(sv[1]);
1061 return shared_output_create(output, sv[0]);
1062 }
1063
1064 return NULL;
1065}
1066
1067static struct weston_output *
1068weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1069{
1070 struct weston_output *output;
1071
1072 wl_list_for_each(output, &c->output_list, link) {
1073 if (x >= output->x && y >= output->y &&
1074 x < output->x + output->width &&
1075 y < output->y + output->height)
1076 return output;
1077 }
1078
1079 return NULL;
1080}
1081
1082static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001083share_output_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Jason Ekstrand47928d82014-04-02 19:54:01 -05001084 void *data)
1085{
1086 struct weston_output *output;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001087 struct weston_pointer *pointer;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001088 struct screen_share *ss = data;
Jason Ekstrand47928d82014-04-02 19:54:01 -05001089
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001090 pointer = weston_seat_get_pointer(keyboard->seat);
1091 if (!pointer) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001092 weston_log("Cannot pick output: Seat does not have pointer\n");
1093 return;
1094 }
1095
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001096 output = weston_output_find(pointer->seat->compositor,
1097 wl_fixed_to_int(pointer->x),
1098 wl_fixed_to_int(pointer->y));
Jason Ekstrand47928d82014-04-02 19:54:01 -05001099 if (!output) {
1100 weston_log("Cannot pick output: Pointer not on any output\n");
1101 return;
1102 }
1103
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001104 weston_output_share(output, ss->command);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001105}
1106
1107WL_EXPORT int
1108module_init(struct weston_compositor *compositor,
1109 int *argc, char *argv[])
1110{
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001111 struct screen_share *ss;
Daniel Stonec7d88fa2016-06-18 16:02:38 +10001112 struct weston_config *config = wet_get_config(compositor);
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001113 struct weston_config_section *section;
1114
1115 ss = zalloc(sizeof *ss);
1116 if (ss == NULL)
1117 return -1;
1118 ss->compositor = compositor;
1119
Daniel Stonec7d88fa2016-06-18 16:02:38 +10001120 section = weston_config_get_section(config, "screen-share", NULL, NULL);
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001121
1122 weston_config_section_get_string(section, "command", &ss->command, "");
1123
Jason Ekstrand47928d82014-04-02 19:54:01 -05001124 weston_compositor_add_key_binding(compositor, KEY_S,
1125 MODIFIER_CTRL | MODIFIER_ALT,
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001126 share_output_binding, ss);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001127 return 0;
1128}