blob: a6f82b19ce17b1e0b906e840e0a9e32ba6262a19 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Jason Ekstrand47928d82014-04-02 19:54:01 -050031#include <stdio.h>
32#include <string.h>
33#include <unistd.h>
34#include <sys/socket.h>
35#include <sys/mman.h>
36#include <signal.h>
37#include <linux/input.h>
38#include <errno.h>
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +010039#include <ctype.h>
Jason Ekstrand47928d82014-04-02 19:54:01 -050040
41#include <wayland-client.h>
42
43#include "compositor.h"
Daniel Stonec7d88fa2016-06-18 16:02:38 +100044#include "weston.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070045#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/os-compatibility.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080047#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jason Ekstrand47928d82014-04-02 19:54:01 -050048
49struct shared_output {
50 struct weston_output *output;
51 struct wl_listener output_destroyed;
52 struct wl_list seat_list;
53
54 struct {
55 struct wl_display *display;
56 struct wl_registry *registry;
57 struct wl_compositor *compositor;
58 struct wl_shm *shm;
59 uint32_t shm_formats;
Jonas Ådahl496adb32015-11-17 16:00:27 +080060 struct zwp_fullscreen_shell_v1 *fshell;
Jason Ekstrand47928d82014-04-02 19:54:01 -050061 struct wl_output *output;
62 struct wl_surface *surface;
63 struct wl_callback *frame_cb;
Jonas Ådahl496adb32015-11-17 16:00:27 +080064 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand47928d82014-04-02 19:54:01 -050065 } parent;
66
67 struct wl_event_source *event_source;
68 struct wl_listener frame_listener;
69
70 struct {
71 int32_t width, height;
72
73 struct wl_list buffers;
74 struct wl_list free_buffers;
75 } shm;
76
77 int cache_dirty;
78 pixman_image_t *cache_image;
79 uint32_t *tmp_data;
80 size_t tmp_data_size;
81};
82
83struct ss_seat {
84 struct weston_seat base;
85 struct shared_output *output;
86 struct wl_list link;
87
88 struct {
89 struct wl_seat *seat;
90 struct wl_pointer *pointer;
91 struct wl_keyboard *keyboard;
92 } parent;
93
94 enum weston_key_state_update keyboard_state_update;
95 uint32_t key_serial;
96};
97
98struct ss_shm_buffer {
99 struct shared_output *output;
100 struct wl_list link;
101 struct wl_list free_link;
102
103 struct wl_buffer *buffer;
104 void *data;
105 size_t size;
106 pixman_region32_t damage;
107
108 pixman_image_t *pm_image;
109};
110
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100111struct screen_share {
112 struct weston_compositor *compositor;
113 char *command;
114};
115
Jason Ekstrand47928d82014-04-02 19:54:01 -0500116static void
117ss_seat_handle_pointer_enter(void *data, struct wl_pointer *pointer,
118 uint32_t serial, struct wl_surface *surface,
119 wl_fixed_t x, wl_fixed_t y)
120{
121 struct ss_seat *seat = data;
122
123 /* No transformation of input position is required here because we are
124 * always receiving the input in the same coordinates as the output. */
125
126 notify_pointer_focus(&seat->base, NULL, 0, 0);
127}
128
129static void
130ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer,
131 uint32_t serial, struct wl_surface *surface)
132{
133 struct ss_seat *seat = data;
134
135 notify_pointer_focus(&seat->base, NULL, 0, 0);
136}
137
138static void
139ss_seat_handle_motion(void *data, struct wl_pointer *pointer,
140 uint32_t time, wl_fixed_t x, wl_fixed_t y)
141{
142 struct ss_seat *seat = data;
143
144 /* No transformation of input position is required here because we are
145 * always receiving the input in the same coordinates as the output. */
146
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200147 notify_motion_absolute(&seat->base, time,
148 wl_fixed_to_double(x), wl_fixed_to_double(y));
Peter Hutterer87743e92016-01-18 16:38:22 +1000149 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500150}
151
152static void
153ss_seat_handle_button(void *data, struct wl_pointer *pointer,
154 uint32_t serial, uint32_t time, uint32_t button,
155 uint32_t state)
156{
157 struct ss_seat *seat = data;
158
159 notify_button(&seat->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +1000160 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500161}
162
163static void
164ss_seat_handle_axis(void *data, struct wl_pointer *pointer,
165 uint32_t time, uint32_t axis, wl_fixed_t value)
166{
167 struct ss_seat *seat = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000168 struct weston_pointer_axis_event weston_event;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500169
Peter Hutterer89b6a492016-01-18 15:58:17 +1000170 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200171 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer87743e92016-01-18 16:38:22 +1000172 weston_event.has_discrete = false;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000173
174 notify_axis(&seat->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000175 notify_pointer_frame(&seat->base);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500176}
177
178static const struct wl_pointer_listener ss_seat_pointer_listener = {
179 ss_seat_handle_pointer_enter,
180 ss_seat_handle_pointer_leave,
181 ss_seat_handle_motion,
182 ss_seat_handle_button,
183 ss_seat_handle_axis,
184};
185
186static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300187ss_seat_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500188 uint32_t format, int fd, uint32_t size)
189{
190 struct ss_seat *seat = data;
191 struct xkb_keymap *keymap;
192 char *map_str;
193
194 if (!data)
Daniel Stonef81959e2017-02-16 19:59:50 +0000195 goto error_no_seat;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500196
197 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
198 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
199 if (map_str == MAP_FAILED) {
200 weston_log("mmap failed: %m\n");
201 goto error;
202 }
203
Ran Benita2e1968f2014-08-19 23:59:51 +0300204 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
205 map_str,
206 XKB_KEYMAP_FORMAT_TEXT_V1,
207 0);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500208 munmap(map_str, size);
209
210 if (!keymap) {
211 weston_log("failed to compile keymap\n");
212 goto error;
213 }
214
215 seat->keyboard_state_update = STATE_UPDATE_NONE;
216 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
217 weston_log("No keymap provided; falling back to default\n");
218 keymap = NULL;
219 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
220 } else {
221 weston_log("Invalid keymap\n");
222 goto error;
223 }
224
225 close(fd);
226
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300227 if (seat->base.keyboard_device_count)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500228 weston_seat_update_keymap(&seat->base, keymap);
229 else
230 weston_seat_init_keyboard(&seat->base, keymap);
231
Ran Benitac9c74152014-08-19 23:59:52 +0300232 xkb_keymap_unref(keymap);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500233
234 return;
235
236error:
237 wl_keyboard_release(seat->parent.keyboard);
Daniel Stonef81959e2017-02-16 19:59:50 +0000238error_no_seat:
Jason Ekstrand47928d82014-04-02 19:54:01 -0500239 close(fd);
240}
241
242static void
243ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
244 uint32_t serial, struct wl_surface *surface,
245 struct wl_array *keys)
246{
247 struct ss_seat *seat = data;
248
249 /* XXX: If we get a modifier event immediately before the focus,
250 * we should try to keep the same serial. */
251 notify_keyboard_focus_in(&seat->base, keys,
252 STATE_UPDATE_AUTOMATIC);
253}
254
255static void
256ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
257 uint32_t serial, struct wl_surface *surface)
258{
259 struct ss_seat *seat = data;
260
261 notify_keyboard_focus_out(&seat->base);
262}
263
264static void
265ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
266 uint32_t serial, uint32_t time,
267 uint32_t key, uint32_t state)
268{
269 struct ss_seat *seat = data;
270
271 seat->key_serial = serial;
272 notify_key(&seat->base, time, key,
273 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
274 WL_KEYBOARD_KEY_STATE_RELEASED,
275 seat->keyboard_state_update);
276}
277
278static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300279ss_seat_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500280 uint32_t serial_in, uint32_t mods_depressed,
281 uint32_t mods_latched, uint32_t mods_locked,
282 uint32_t group)
283{
284 struct ss_seat *seat = data;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300285 struct weston_compositor *c = seat->base.compositor;
286 struct weston_keyboard *keyboard;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500287 uint32_t serial_out;
288
289 /* If we get a key event followed by a modifier event with the
290 * same serial number, then we try to preserve those semantics by
291 * reusing the same serial number on the way out too. */
292 if (serial_in == seat->key_serial)
293 serial_out = wl_display_get_serial(c->wl_display);
294 else
295 serial_out = wl_display_next_serial(c->wl_display);
296
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300297 keyboard = weston_seat_get_keyboard(&seat->base);
298 xkb_state_update_mask(keyboard->xkb_state.state,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500299 mods_depressed, mods_latched,
300 mods_locked, 0, 0, group);
301 notify_modifiers(&seat->base, serial_out);
302}
303
304static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
305 ss_seat_handle_keymap,
306 ss_seat_handle_keyboard_enter,
307 ss_seat_handle_keyboard_leave,
308 ss_seat_handle_key,
309 ss_seat_handle_modifiers,
310};
311
312static void
313ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
314 enum wl_seat_capability caps)
315{
316 struct ss_seat *ss_seat = data;
317
318 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
319 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
320 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
321 wl_pointer_add_listener(ss_seat->parent.pointer,
322 &ss_seat_pointer_listener, ss_seat);
323 weston_seat_init_pointer(&ss_seat->base);
324 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
325 wl_pointer_destroy(ss_seat->parent.pointer);
326 ss_seat->parent.pointer = NULL;
327 }
328
329 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
330 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
331 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
332 wl_keyboard_add_listener(ss_seat->parent.keyboard,
333 &ss_seat_keyboard_listener, ss_seat);
334 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
335 wl_keyboard_destroy(ss_seat->parent.keyboard);
336 ss_seat->parent.keyboard = NULL;
337 }
338}
339
340static const struct wl_seat_listener ss_seat_listener = {
341 ss_seat_handle_capabilities,
342};
343
344static struct ss_seat *
345ss_seat_create(struct shared_output *so, uint32_t id)
346{
347 struct ss_seat *seat;
348
349 seat = zalloc(sizeof *seat);
350 if (seat == NULL)
351 return NULL;
352
353 weston_seat_init(&seat->base, so->output->compositor, "default");
354 seat->output = so;
355 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
356 &wl_seat_interface, 1);
357 wl_list_insert(so->seat_list.prev, &seat->link);
358
359 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
360 wl_seat_set_user_data(seat->parent.seat, seat);
361
362 return seat;
363}
364
365static void
366ss_seat_destroy(struct ss_seat *seat)
367{
368 if (seat->parent.pointer)
369 wl_pointer_release(seat->parent.pointer);
370 if (seat->parent.keyboard)
371 wl_keyboard_release(seat->parent.keyboard);
372 wl_seat_destroy(seat->parent.seat);
373
374 wl_list_remove(&seat->link);
375
376 weston_seat_release(&seat->base);
377
378 free(seat);
379}
380
381static void
382ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
383{
384 pixman_image_unref(buffer->pm_image);
385
386 wl_buffer_destroy(buffer->buffer);
387 munmap(buffer->data, buffer->size);
388
389 pixman_region32_fini(&buffer->damage);
390
391 wl_list_remove(&buffer->link);
392 wl_list_remove(&buffer->free_link);
393 free(buffer);
394}
395
396static void
397buffer_release(void *data, struct wl_buffer *buffer)
398{
399 struct ss_shm_buffer *sb = data;
400
401 if (sb->output) {
402 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
403 } else {
404 ss_shm_buffer_destroy(sb);
405 }
406}
407
408static const struct wl_buffer_listener buffer_listener = {
409 buffer_release
410};
411
412static struct ss_shm_buffer *
413shared_output_get_shm_buffer(struct shared_output *so)
414{
415 struct ss_shm_buffer *sb, *bnext;
416 struct wl_shm_pool *pool;
417 int width, height, stride;
418 int fd;
419 unsigned char *data;
420
421 width = so->output->width;
422 height = so->output->height;
423 stride = width * 4;
424
425 /* If the size of the output changed, we free the old buffers and
426 * make new ones. */
427 if (so->shm.width != width ||
428 so->shm.height != height) {
429
430 /* Destroy free buffers */
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700431 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500432 ss_shm_buffer_destroy(sb);
433
434 /* Orphan in-use buffers so they get destroyed */
435 wl_list_for_each(sb, &so->shm.buffers, link)
436 sb->output = NULL;
437
438 so->shm.width = width;
439 so->shm.height = height;
440 }
441
442 if (!wl_list_empty(&so->shm.free_buffers)) {
443 sb = container_of(so->shm.free_buffers.next,
444 struct ss_shm_buffer, free_link);
445 wl_list_remove(&sb->free_link);
446 wl_list_init(&sb->free_link);
447
448 return sb;
449 }
450
451 fd = os_create_anonymous_file(height * stride);
452 if (fd < 0) {
Chris Michael5169a572015-10-01 10:51:30 -0400453 weston_log("os_create_anonymous_file: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500454 return NULL;
455 }
456
457 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
458 if (data == MAP_FAILED) {
Chris Michael5169a572015-10-01 10:51:30 -0400459 weston_log("mmap: %m\n");
Hardeninge57d1f22014-04-11 09:06:58 +0200460 goto out_close;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500461 }
462
463 sb = zalloc(sizeof *sb);
Hardeninge57d1f22014-04-11 09:06:58 +0200464 if (!sb)
465 goto out_unmap;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500466
467 sb->output = so;
468 wl_list_init(&sb->free_link);
469 wl_list_insert(&so->shm.buffers, &sb->link);
470
471 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
472
473 sb->data = data;
474 sb->size = height * stride;
475
476 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
477
478 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
479 width, height, stride,
480 WL_SHM_FORMAT_ARGB8888);
481 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
482 wl_shm_pool_destroy(pool);
483 close(fd);
Hardeninge57d1f22014-04-11 09:06:58 +0200484 fd = -1;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500485
486 memset(data, 0, sb->size);
487
488 sb->pm_image =
489 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
490 (uint32_t *)data, stride);
Hardeninge57d1f22014-04-11 09:06:58 +0200491 if (!sb->pm_image)
492 goto out_pixman_error;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500493
494 return sb;
Hardeninge57d1f22014-04-11 09:06:58 +0200495
496out_pixman_error:
497 pixman_region32_fini(&sb->damage);
498out_unmap:
499 munmap(data, height * stride);
500out_close:
501 if (fd != -1)
502 close(fd);
503 return NULL;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500504}
505
506static void
507output_compute_transform(struct weston_output *output,
508 pixman_transform_t *transform)
509{
510 pixman_fixed_t fw, fh;
511
512 pixman_transform_init_identity(transform);
513
514 fw = pixman_int_to_fixed(output->width);
515 fh = pixman_int_to_fixed(output->height);
516
517 switch (output->transform) {
518 case WL_OUTPUT_TRANSFORM_FLIPPED:
519 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
520 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
521 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
522 pixman_transform_scale(transform, NULL,
523 pixman_int_to_fixed (-1),
524 pixman_int_to_fixed (1));
525 pixman_transform_translate(transform, NULL, fw, 0);
526 }
527
528 switch (output->transform) {
529 default:
530 case WL_OUTPUT_TRANSFORM_NORMAL:
531 case WL_OUTPUT_TRANSFORM_FLIPPED:
532 break;
533 case WL_OUTPUT_TRANSFORM_90:
534 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
535 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
536 pixman_transform_translate(transform, NULL, fh, 0);
537 break;
538 case WL_OUTPUT_TRANSFORM_180:
539 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
540 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
541 pixman_transform_translate(transform, NULL, fw, fh);
542 break;
543 case WL_OUTPUT_TRANSFORM_270:
544 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
545 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
546 pixman_transform_translate(transform, NULL, 0, fw);
547 break;
548 }
549
550 pixman_transform_scale(transform, NULL,
551 pixman_fixed_1 * output->current_scale,
552 pixman_fixed_1 * output->current_scale);
553}
554
555static void
556shared_output_destroy(struct shared_output *so);
557
558static int
559shared_output_ensure_tmp_data(struct shared_output *so,
560 pixman_region32_t *region)
561{
562 pixman_box32_t *ext;
563 size_t size;
564
565 if (!pixman_region32_not_empty(region))
566 return 0;
567
568 ext = pixman_region32_extents(region);
569
570 /* Damage is in output coordinates.
571 *
572 * We are multiplying by 4 because the temporary data needs to be able
573 * to store an 32 bit-per-pixel buffer.
574 */
575 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
576 * so->output->current_scale * so->output->current_scale;
577
578 if (so->tmp_data != NULL && size <= so->tmp_data_size)
579 return 0;
580
581 free(so->tmp_data);
582 so->tmp_data = malloc(size);
583 if (so->tmp_data == NULL) {
584 so->tmp_data_size = 0;
585 errno = ENOMEM;
586 return -1;
587 }
588
589 so->tmp_data_size = size;
590
591 return 0;
592}
593
594static void
595shared_output_update(struct shared_output *so);
596
597static void
598shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
599{
600 struct shared_output *so = data;
601
602 if (cb != so->parent.frame_cb)
603 return;
604
605 wl_callback_destroy(cb);
606 so->parent.frame_cb = NULL;
607
608 shared_output_update(so);
609}
610
611static const struct wl_callback_listener shared_output_frame_listener = {
612 shared_output_frame_callback
613};
614
615static void
616shared_output_update(struct shared_output *so)
617{
618 struct ss_shm_buffer *sb;
619 pixman_box32_t *r;
620 int i, nrects;
621 pixman_transform_t transform;
622
623 /* Only update if we need to */
624 if (!so->cache_dirty || so->parent.frame_cb)
625 return;
626
627 sb = shared_output_get_shm_buffer(so);
628 if (sb == NULL) {
629 shared_output_destroy(so);
630 return;
631 }
632
633 output_compute_transform(so->output, &transform);
634 pixman_image_set_transform(so->cache_image, &transform);
635
636 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
637
638 if (so->output->current_scale == 1) {
639 pixman_image_set_filter(so->cache_image,
640 PIXMAN_FILTER_NEAREST, NULL, 0);
641 } else {
642 pixman_image_set_filter(so->cache_image,
643 PIXMAN_FILTER_BILINEAR, NULL, 0);
644 }
645
646 pixman_image_composite32(PIXMAN_OP_SRC,
647 so->cache_image, /* src */
648 NULL, /* mask */
649 sb->pm_image, /* dest */
650 0, 0, /* src_x, src_y */
651 0, 0, /* mask_x, mask_y */
652 0, 0, /* dest_x, dest_y */
653 so->output->width, /* width */
654 so->output->height /* height */);
655
656 pixman_image_set_transform(sb->pm_image, NULL);
657 pixman_image_set_clip_region32(sb->pm_image, NULL);
658
659 r = pixman_region32_rectangles(&sb->damage, &nrects);
660 for (i = 0; i < nrects; ++i)
661 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
662 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
663
664 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
665
666 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
667 wl_callback_add_listener(so->parent.frame_cb,
668 &shared_output_frame_listener, so);
669
670 wl_surface_commit(so->parent.surface);
671 wl_callback_destroy(wl_display_sync(so->parent.display));
672 wl_display_flush(so->parent.display);
673
674 /* Clear the buffer damage */
675 pixman_region32_fini(&sb->damage);
676 pixman_region32_init(&sb->damage);
677}
678
679static void
680shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
681{
682 struct shared_output *so = data;
683
684 so->parent.shm_formats |= (1 << format);
685}
686
687struct wl_shm_listener shm_listener = {
688 shm_handle_format
689};
690
691static void
692registry_handle_global(void *data, struct wl_registry *registry,
693 uint32_t id, const char *interface, uint32_t version)
694{
695 struct shared_output *so = data;
696
697 if (strcmp(interface, "wl_compositor") == 0) {
698 so->parent.compositor =
699 wl_registry_bind(registry,
700 id, &wl_compositor_interface, 1);
701 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
702 so->parent.output =
703 wl_registry_bind(registry,
704 id, &wl_output_interface, 1);
705 } else if (strcmp(interface, "wl_seat") == 0) {
706 ss_seat_create(so, id);
707 } else if (strcmp(interface, "wl_shm") == 0) {
708 so->parent.shm =
709 wl_registry_bind(registry,
710 id, &wl_shm_interface, 1);
711 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800712 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500713 so->parent.fshell =
714 wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800715 id,
716 &zwp_fullscreen_shell_v1_interface,
717 1);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500718 }
719}
720
721static void
722registry_handle_global_remove(void *data, struct wl_registry *registry,
723 uint32_t name)
724{
725}
726
727static const struct wl_registry_listener registry_listener = {
728 registry_handle_global,
729 registry_handle_global_remove
730};
731
732static int
733shared_output_handle_event(int fd, uint32_t mask, void *data)
734{
735 struct shared_output *so = data;
736 int count = 0;
737
738 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
739 shared_output_destroy(so);
740 return 0;
741 }
742
743 if (mask & WL_EVENT_READABLE)
744 count = wl_display_dispatch(so->parent.display);
745 if (mask & WL_EVENT_WRITABLE)
746 wl_display_flush(so->parent.display);
747
748 if (mask == 0) {
749 count = wl_display_dispatch_pending(so->parent.display);
750 wl_display_flush(so->parent.display);
751 }
752
753 return count;
754}
755
756static void
757output_destroyed(struct wl_listener *l, void *data)
758{
759 struct shared_output *so;
760
761 so = container_of(l, struct shared_output, output_destroyed);
762
763 shared_output_destroy(so);
764}
765
766static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800767mode_feedback_ok(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500768{
769 struct shared_output *so = data;
770
Jonas Ådahl496adb32015-11-17 16:00:27 +0800771 zwp_fullscreen_shell_mode_feedback_v1_destroy(so->parent.mode_feedback);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500772}
773
774static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800775mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500776{
777 struct shared_output *so = data;
778
Jonas Ådahl496adb32015-11-17 16:00:27 +0800779 zwp_fullscreen_shell_mode_feedback_v1_destroy(so->parent.mode_feedback);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500780
781 weston_log("Screen share failed: present_surface_for_mode failed\n");
782 shared_output_destroy(so);
783}
784
Jonas Ådahl496adb32015-11-17 16:00:27 +0800785struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500786 mode_feedback_ok,
787 mode_feedback_failed,
788 mode_feedback_ok,
789};
790
791static void
792shared_output_repainted(struct wl_listener *listener, void *data)
793{
794 struct shared_output *so =
795 container_of(listener, struct shared_output, frame_listener);
796 pixman_region32_t damage;
797 struct ss_shm_buffer *sb;
798 int32_t x, y, width, height, stride;
799 int i, nrects, do_yflip;
800 pixman_box32_t *r;
801 uint32_t *cache_data;
802
803 /* Damage in output coordinates */
804 pixman_region32_init(&damage);
805 pixman_region32_intersect(&damage, &so->output->region,
806 &so->output->previous_damage);
807 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
808
809 /* Apply damage to all buffers */
810 wl_list_for_each(sb, &so->shm.buffers, link)
811 pixman_region32_union(&sb->damage, &sb->damage, &damage);
812
813 /* Transform to buffer coordinates */
814 weston_transformed_region(so->output->width, so->output->height,
815 so->output->transform,
816 so->output->current_scale,
817 &damage, &damage);
818
819 width = so->output->current_mode->width;
820 height = so->output->current_mode->height;
821 stride = width;
822
823 if (!so->cache_image ||
824 pixman_image_get_width(so->cache_image) != width ||
825 pixman_image_get_height(so->cache_image) != height) {
826 if (so->cache_image)
827 pixman_image_unref(so->cache_image);
828
829 so->cache_image =
830 pixman_image_create_bits(PIXMAN_a8r8g8b8,
831 width, height, NULL,
832 stride);
833 if (!so->cache_image) {
834 shared_output_destroy(so);
835 return;
836 }
837
838 pixman_region32_fini(&damage);
839 pixman_region32_init_rect(&damage, 0, 0, width, height);
840 }
841
842 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
843 shared_output_destroy(so);
844 return;
845 }
846
847 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
848
849 cache_data = pixman_image_get_data(so->cache_image);
850 r = pixman_region32_rectangles(&damage, &nrects);
851 for (i = 0; i < nrects; ++i) {
852 x = r[i].x1;
853 y = r[i].y1;
854 width = r[i].x2 - r[i].x1;
855 height = r[i].y2 - r[i].y1;
856
857 if (do_yflip) {
858 so->output->compositor->renderer->read_pixels(
859 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
860 x, so->output->current_mode->height - r[i].y2,
861 width, height);
862
863 pixman_blt(so->tmp_data, cache_data, -width, stride,
864 32, 32, 0, 1 - height, x, y, width, height);
865 } else {
866 so->output->compositor->renderer->read_pixels(
867 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
868 x, y, width, height);
869
870 pixman_blt(so->tmp_data, cache_data, width, stride,
871 32, 32, 0, 0, x, y, width, height);
872 }
873 }
874
875 pixman_region32_fini(&damage);
876
877 so->cache_dirty = 1;
878
879 shared_output_update(so);
880}
881
882static struct shared_output *
883shared_output_create(struct weston_output *output, int parent_fd)
884{
885 struct shared_output *so;
886 struct wl_event_loop *loop;
Daniel Stonef589dac2017-02-16 19:59:51 +0000887 struct ss_seat *seat, *tmp;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500888 int epoll_fd;
889
890 so = zalloc(sizeof *so);
891 if (so == NULL)
892 goto err_close;
893
894 wl_list_init(&so->seat_list);
895
896 so->parent.display = wl_display_connect_to_fd(parent_fd);
897 if (!so->parent.display)
898 goto err_alloc;
899
900 so->parent.registry = wl_display_get_registry(so->parent.display);
901 if (!so->parent.registry)
902 goto err_display;
903 wl_registry_add_listener(so->parent.registry,
904 &registry_listener, so);
905 wl_display_roundtrip(so->parent.display);
906 if (so->parent.shm == NULL) {
907 weston_log("Screen share failed: No wl_shm found\n");
908 goto err_display;
909 }
910 if (so->parent.fshell == NULL) {
911 weston_log("Screen share failed: "
912 "Parent does not support wl_fullscreen_shell\n");
913 goto err_display;
914 }
915 if (so->parent.compositor == NULL) {
916 weston_log("Screen share failed: No wl_compositor found\n");
917 goto err_display;
918 }
919
920 /* Get SHM formats */
921 wl_display_roundtrip(so->parent.display);
922 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
923 weston_log("Screen share failed: "
924 "WL_SHM_FORMAT_XRGB8888 not available\n");
925 goto err_display;
926 }
927
928 so->parent.surface =
929 wl_compositor_create_surface(so->parent.compositor);
930 if (!so->parent.surface) {
Chris Michael5169a572015-10-01 10:51:30 -0400931 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500932 goto err_display;
933 }
934
935 so->parent.mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800936 zwp_fullscreen_shell_v1_present_surface_for_mode(so->parent.fshell,
937 so->parent.surface,
938 so->parent.output,
939 output->current_mode->refresh);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500940 if (!so->parent.mode_feedback) {
Chris Michael5169a572015-10-01 10:51:30 -0400941 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500942 goto err_display;
943 }
Jonas Ådahl496adb32015-11-17 16:00:27 +0800944 zwp_fullscreen_shell_mode_feedback_v1_add_listener(so->parent.mode_feedback,
945 &mode_feedback_listener,
946 so);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500947
948 loop = wl_display_get_event_loop(output->compositor->wl_display);
949
950 epoll_fd = wl_display_get_fd(so->parent.display);
951 so->event_source =
952 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
953 shared_output_handle_event, so);
954 if (!so->event_source) {
Chris Michael5169a572015-10-01 10:51:30 -0400955 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500956 goto err_display;
957 }
958
959 /* Ok, everything's created. We should be good to go */
960 wl_list_init(&so->shm.buffers);
961 wl_list_init(&so->shm.free_buffers);
962
963 so->output = output;
964 so->output_destroyed.notify = output_destroyed;
965 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
966
967 so->frame_listener.notify = shared_output_repainted;
968 wl_signal_add(&output->frame_signal, &so->frame_listener);
969 output->disable_planes++;
970 weston_output_damage(output);
971
972 return so;
973
974err_display:
Daniel Stonef589dac2017-02-16 19:59:51 +0000975 wl_list_for_each_safe(seat, tmp, &so->seat_list, link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500976 ss_seat_destroy(seat);
977 wl_display_disconnect(so->parent.display);
978err_alloc:
979 free(so);
980err_close:
981 close(parent_fd);
982 return NULL;
983}
984
985static void
986shared_output_destroy(struct shared_output *so)
987{
988 struct ss_shm_buffer *buffer, *bnext;
989
Jason Ekstrand27efc062014-04-03 11:51:42 -0500990 so->output->disable_planes--;
991
Jason Ekstrand47928d82014-04-02 19:54:01 -0500992 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
993 ss_shm_buffer_destroy(buffer);
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700994 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500995 ss_shm_buffer_destroy(buffer);
996
997 wl_display_disconnect(so->parent.display);
998 wl_event_source_remove(so->event_source);
999
1000 wl_list_remove(&so->output_destroyed.link);
1001 wl_list_remove(&so->frame_listener.link);
1002
1003 pixman_image_unref(so->cache_image);
1004 free(so->tmp_data);
1005
1006 free(so);
1007}
1008
1009static struct shared_output *
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001010weston_output_share(struct weston_output *output, const char* command)
Jason Ekstrand47928d82014-04-02 19:54:01 -05001011{
1012 int sv[2];
1013 char str[32];
1014 pid_t pid;
1015 sigset_t allsigs;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001016 char *const argv[] = {
1017 "/bin/sh",
1018 "-c",
1019 (char*)command,
1020 NULL
1021 };
Jason Ekstrand47928d82014-04-02 19:54:01 -05001022
1023 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1024 weston_log("weston_output_share: socketpair failed: %m\n");
1025 return NULL;
1026 }
1027
1028 pid = fork();
1029
1030 if (pid == -1) {
1031 close(sv[0]);
1032 close(sv[1]);
1033 weston_log("weston_output_share: fork failed: %m\n");
1034 return NULL;
1035 }
1036
1037 if (pid == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001038 /* do not give our signal mask to the new process */
1039 sigfillset(&allsigs);
1040 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1041
1042 /* Launch clients as the user. Do not launch clients with
1043 * wrong euid. */
1044 if (seteuid(getuid()) == -1) {
1045 weston_log("weston_output_share: setuid failed: %m\n");
1046 abort();
1047 }
1048
1049 sv[1] = dup(sv[1]);
1050 if (sv[1] == -1) {
1051 weston_log("weston_output_share: dup failed: %m\n");
1052 abort();
1053 }
1054
1055 snprintf(str, sizeof str, "%d", sv[1]);
1056 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1057
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001058 execv(argv[0], argv);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001059 weston_log("weston_output_share: exec failed: %m\n");
1060 abort();
1061 } else {
1062 close(sv[1]);
1063 return shared_output_create(output, sv[0]);
1064 }
1065
1066 return NULL;
1067}
1068
1069static struct weston_output *
1070weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1071{
1072 struct weston_output *output;
1073
1074 wl_list_for_each(output, &c->output_list, link) {
1075 if (x >= output->x && y >= output->y &&
1076 x < output->x + output->width &&
1077 y < output->y + output->height)
1078 return output;
1079 }
1080
1081 return NULL;
1082}
1083
1084static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001085share_output_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Jason Ekstrand47928d82014-04-02 19:54:01 -05001086 void *data)
1087{
1088 struct weston_output *output;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001089 struct weston_pointer *pointer;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001090 struct screen_share *ss = data;
Jason Ekstrand47928d82014-04-02 19:54:01 -05001091
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001092 pointer = weston_seat_get_pointer(keyboard->seat);
1093 if (!pointer) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001094 weston_log("Cannot pick output: Seat does not have pointer\n");
1095 return;
1096 }
1097
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001098 output = weston_output_find(pointer->seat->compositor,
1099 wl_fixed_to_int(pointer->x),
1100 wl_fixed_to_int(pointer->y));
Jason Ekstrand47928d82014-04-02 19:54:01 -05001101 if (!output) {
1102 weston_log("Cannot pick output: Pointer not on any output\n");
1103 return;
1104 }
1105
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001106 weston_output_share(output, ss->command);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001107}
1108
1109WL_EXPORT int
Quentin Glidic8af2bec2016-12-02 14:21:46 +01001110wet_module_init(struct weston_compositor *compositor,
1111 int *argc, char *argv[])
Jason Ekstrand47928d82014-04-02 19:54:01 -05001112{
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001113 struct screen_share *ss;
Daniel Stonec7d88fa2016-06-18 16:02:38 +10001114 struct weston_config *config = wet_get_config(compositor);
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001115 struct weston_config_section *section;
1116
1117 ss = zalloc(sizeof *ss);
1118 if (ss == NULL)
1119 return -1;
1120 ss->compositor = compositor;
1121
Daniel Stonec7d88fa2016-06-18 16:02:38 +10001122 section = weston_config_get_section(config, "screen-share", NULL, NULL);
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001123
1124 weston_config_section_get_string(section, "command", &ss->command, "");
1125
Jason Ekstrand47928d82014-04-02 19:54:01 -05001126 weston_compositor_add_key_binding(compositor, KEY_S,
1127 MODIFIER_CTRL | MODIFIER_ALT,
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001128 share_output_binding, ss);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001129 return 0;
1130}