blob: ab649e3b2ce2d44e051b875a863ab7ac05e61d78 [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"
Jonas Ådahl496adb32015-11-17 16:00:27 +080045#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jason Ekstrand47928d82014-04-02 19:54:01 -050046
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;
Jonas Ådahl496adb32015-11-17 16:00:27 +080058 struct zwp_fullscreen_shell_v1 *fshell;
Jason Ekstrand47928d82014-04-02 19:54:01 -050059 struct wl_output *output;
60 struct wl_surface *surface;
61 struct wl_callback *frame_cb;
Jonas Ådahl496adb32015-11-17 16:00:27 +080062 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand47928d82014-04-02 19:54:01 -050063 } 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;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000163 struct weston_pointer_axis_event weston_event;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500164
Peter Hutterer89b6a492016-01-18 15:58:17 +1000165 weston_event.axis = axis;
166 weston_event.value = value;
167
168 notify_axis(&seat->base, time, &weston_event);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500169}
170
171static const struct wl_pointer_listener ss_seat_pointer_listener = {
172 ss_seat_handle_pointer_enter,
173 ss_seat_handle_pointer_leave,
174 ss_seat_handle_motion,
175 ss_seat_handle_button,
176 ss_seat_handle_axis,
177};
178
179static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300180ss_seat_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500181 uint32_t format, int fd, uint32_t size)
182{
183 struct ss_seat *seat = data;
184 struct xkb_keymap *keymap;
185 char *map_str;
186
187 if (!data)
188 goto error;
189
190 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
191 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
192 if (map_str == MAP_FAILED) {
193 weston_log("mmap failed: %m\n");
194 goto error;
195 }
196
Ran Benita2e1968f2014-08-19 23:59:51 +0300197 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
198 map_str,
199 XKB_KEYMAP_FORMAT_TEXT_V1,
200 0);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500201 munmap(map_str, size);
202
203 if (!keymap) {
204 weston_log("failed to compile keymap\n");
205 goto error;
206 }
207
208 seat->keyboard_state_update = STATE_UPDATE_NONE;
209 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
210 weston_log("No keymap provided; falling back to default\n");
211 keymap = NULL;
212 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
213 } else {
214 weston_log("Invalid keymap\n");
215 goto error;
216 }
217
218 close(fd);
219
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300220 if (seat->base.keyboard_device_count)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500221 weston_seat_update_keymap(&seat->base, keymap);
222 else
223 weston_seat_init_keyboard(&seat->base, keymap);
224
Ran Benitac9c74152014-08-19 23:59:52 +0300225 xkb_keymap_unref(keymap);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500226
227 return;
228
229error:
230 wl_keyboard_release(seat->parent.keyboard);
231 close(fd);
232}
233
234static void
235ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
236 uint32_t serial, struct wl_surface *surface,
237 struct wl_array *keys)
238{
239 struct ss_seat *seat = data;
240
241 /* XXX: If we get a modifier event immediately before the focus,
242 * we should try to keep the same serial. */
243 notify_keyboard_focus_in(&seat->base, keys,
244 STATE_UPDATE_AUTOMATIC);
245}
246
247static void
248ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
249 uint32_t serial, struct wl_surface *surface)
250{
251 struct ss_seat *seat = data;
252
253 notify_keyboard_focus_out(&seat->base);
254}
255
256static void
257ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
258 uint32_t serial, uint32_t time,
259 uint32_t key, uint32_t state)
260{
261 struct ss_seat *seat = data;
262
263 seat->key_serial = serial;
264 notify_key(&seat->base, time, key,
265 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
266 WL_KEYBOARD_KEY_STATE_RELEASED,
267 seat->keyboard_state_update);
268}
269
270static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300271ss_seat_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500272 uint32_t serial_in, uint32_t mods_depressed,
273 uint32_t mods_latched, uint32_t mods_locked,
274 uint32_t group)
275{
276 struct ss_seat *seat = data;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300277 struct weston_compositor *c = seat->base.compositor;
278 struct weston_keyboard *keyboard;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500279 uint32_t serial_out;
280
281 /* If we get a key event followed by a modifier event with the
282 * same serial number, then we try to preserve those semantics by
283 * reusing the same serial number on the way out too. */
284 if (serial_in == seat->key_serial)
285 serial_out = wl_display_get_serial(c->wl_display);
286 else
287 serial_out = wl_display_next_serial(c->wl_display);
288
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -0300289 keyboard = weston_seat_get_keyboard(&seat->base);
290 xkb_state_update_mask(keyboard->xkb_state.state,
Jason Ekstrand47928d82014-04-02 19:54:01 -0500291 mods_depressed, mods_latched,
292 mods_locked, 0, 0, group);
293 notify_modifiers(&seat->base, serial_out);
294}
295
296static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
297 ss_seat_handle_keymap,
298 ss_seat_handle_keyboard_enter,
299 ss_seat_handle_keyboard_leave,
300 ss_seat_handle_key,
301 ss_seat_handle_modifiers,
302};
303
304static void
305ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
306 enum wl_seat_capability caps)
307{
308 struct ss_seat *ss_seat = data;
309
310 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
311 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
312 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
313 wl_pointer_add_listener(ss_seat->parent.pointer,
314 &ss_seat_pointer_listener, ss_seat);
315 weston_seat_init_pointer(&ss_seat->base);
316 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
317 wl_pointer_destroy(ss_seat->parent.pointer);
318 ss_seat->parent.pointer = NULL;
319 }
320
321 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
322 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
323 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
324 wl_keyboard_add_listener(ss_seat->parent.keyboard,
325 &ss_seat_keyboard_listener, ss_seat);
326 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
327 wl_keyboard_destroy(ss_seat->parent.keyboard);
328 ss_seat->parent.keyboard = NULL;
329 }
330}
331
332static const struct wl_seat_listener ss_seat_listener = {
333 ss_seat_handle_capabilities,
334};
335
336static struct ss_seat *
337ss_seat_create(struct shared_output *so, uint32_t id)
338{
339 struct ss_seat *seat;
340
341 seat = zalloc(sizeof *seat);
342 if (seat == NULL)
343 return NULL;
344
345 weston_seat_init(&seat->base, so->output->compositor, "default");
346 seat->output = so;
347 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
348 &wl_seat_interface, 1);
349 wl_list_insert(so->seat_list.prev, &seat->link);
350
351 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
352 wl_seat_set_user_data(seat->parent.seat, seat);
353
354 return seat;
355}
356
357static void
358ss_seat_destroy(struct ss_seat *seat)
359{
360 if (seat->parent.pointer)
361 wl_pointer_release(seat->parent.pointer);
362 if (seat->parent.keyboard)
363 wl_keyboard_release(seat->parent.keyboard);
364 wl_seat_destroy(seat->parent.seat);
365
366 wl_list_remove(&seat->link);
367
368 weston_seat_release(&seat->base);
369
370 free(seat);
371}
372
373static void
374ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
375{
376 pixman_image_unref(buffer->pm_image);
377
378 wl_buffer_destroy(buffer->buffer);
379 munmap(buffer->data, buffer->size);
380
381 pixman_region32_fini(&buffer->damage);
382
383 wl_list_remove(&buffer->link);
384 wl_list_remove(&buffer->free_link);
385 free(buffer);
386}
387
388static void
389buffer_release(void *data, struct wl_buffer *buffer)
390{
391 struct ss_shm_buffer *sb = data;
392
393 if (sb->output) {
394 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
395 } else {
396 ss_shm_buffer_destroy(sb);
397 }
398}
399
400static const struct wl_buffer_listener buffer_listener = {
401 buffer_release
402};
403
404static struct ss_shm_buffer *
405shared_output_get_shm_buffer(struct shared_output *so)
406{
407 struct ss_shm_buffer *sb, *bnext;
408 struct wl_shm_pool *pool;
409 int width, height, stride;
410 int fd;
411 unsigned char *data;
412
413 width = so->output->width;
414 height = so->output->height;
415 stride = width * 4;
416
417 /* If the size of the output changed, we free the old buffers and
418 * make new ones. */
419 if (so->shm.width != width ||
420 so->shm.height != height) {
421
422 /* Destroy free buffers */
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700423 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500424 ss_shm_buffer_destroy(sb);
425
426 /* Orphan in-use buffers so they get destroyed */
427 wl_list_for_each(sb, &so->shm.buffers, link)
428 sb->output = NULL;
429
430 so->shm.width = width;
431 so->shm.height = height;
432 }
433
434 if (!wl_list_empty(&so->shm.free_buffers)) {
435 sb = container_of(so->shm.free_buffers.next,
436 struct ss_shm_buffer, free_link);
437 wl_list_remove(&sb->free_link);
438 wl_list_init(&sb->free_link);
439
440 return sb;
441 }
442
443 fd = os_create_anonymous_file(height * stride);
444 if (fd < 0) {
Chris Michael5169a572015-10-01 10:51:30 -0400445 weston_log("os_create_anonymous_file: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500446 return NULL;
447 }
448
449 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
450 if (data == MAP_FAILED) {
Chris Michael5169a572015-10-01 10:51:30 -0400451 weston_log("mmap: %m\n");
Hardeninge57d1f22014-04-11 09:06:58 +0200452 goto out_close;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500453 }
454
455 sb = zalloc(sizeof *sb);
Hardeninge57d1f22014-04-11 09:06:58 +0200456 if (!sb)
457 goto out_unmap;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500458
459 sb->output = so;
460 wl_list_init(&sb->free_link);
461 wl_list_insert(&so->shm.buffers, &sb->link);
462
463 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
464
465 sb->data = data;
466 sb->size = height * stride;
467
468 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
469
470 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
471 width, height, stride,
472 WL_SHM_FORMAT_ARGB8888);
473 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
474 wl_shm_pool_destroy(pool);
475 close(fd);
Hardeninge57d1f22014-04-11 09:06:58 +0200476 fd = -1;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500477
478 memset(data, 0, sb->size);
479
480 sb->pm_image =
481 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
482 (uint32_t *)data, stride);
Hardeninge57d1f22014-04-11 09:06:58 +0200483 if (!sb->pm_image)
484 goto out_pixman_error;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500485
486 return sb;
Hardeninge57d1f22014-04-11 09:06:58 +0200487
488out_pixman_error:
489 pixman_region32_fini(&sb->damage);
490out_unmap:
491 munmap(data, height * stride);
492out_close:
493 if (fd != -1)
494 close(fd);
495 return NULL;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500496}
497
498static void
499output_compute_transform(struct weston_output *output,
500 pixman_transform_t *transform)
501{
502 pixman_fixed_t fw, fh;
503
504 pixman_transform_init_identity(transform);
505
506 fw = pixman_int_to_fixed(output->width);
507 fh = pixman_int_to_fixed(output->height);
508
509 switch (output->transform) {
510 case WL_OUTPUT_TRANSFORM_FLIPPED:
511 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
512 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
513 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
514 pixman_transform_scale(transform, NULL,
515 pixman_int_to_fixed (-1),
516 pixman_int_to_fixed (1));
517 pixman_transform_translate(transform, NULL, fw, 0);
518 }
519
520 switch (output->transform) {
521 default:
522 case WL_OUTPUT_TRANSFORM_NORMAL:
523 case WL_OUTPUT_TRANSFORM_FLIPPED:
524 break;
525 case WL_OUTPUT_TRANSFORM_90:
526 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
527 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
528 pixman_transform_translate(transform, NULL, fh, 0);
529 break;
530 case WL_OUTPUT_TRANSFORM_180:
531 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
532 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
533 pixman_transform_translate(transform, NULL, fw, fh);
534 break;
535 case WL_OUTPUT_TRANSFORM_270:
536 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
537 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
538 pixman_transform_translate(transform, NULL, 0, fw);
539 break;
540 }
541
542 pixman_transform_scale(transform, NULL,
543 pixman_fixed_1 * output->current_scale,
544 pixman_fixed_1 * output->current_scale);
545}
546
547static void
548shared_output_destroy(struct shared_output *so);
549
550static int
551shared_output_ensure_tmp_data(struct shared_output *so,
552 pixman_region32_t *region)
553{
554 pixman_box32_t *ext;
555 size_t size;
556
557 if (!pixman_region32_not_empty(region))
558 return 0;
559
560 ext = pixman_region32_extents(region);
561
562 /* Damage is in output coordinates.
563 *
564 * We are multiplying by 4 because the temporary data needs to be able
565 * to store an 32 bit-per-pixel buffer.
566 */
567 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
568 * so->output->current_scale * so->output->current_scale;
569
570 if (so->tmp_data != NULL && size <= so->tmp_data_size)
571 return 0;
572
573 free(so->tmp_data);
574 so->tmp_data = malloc(size);
575 if (so->tmp_data == NULL) {
576 so->tmp_data_size = 0;
577 errno = ENOMEM;
578 return -1;
579 }
580
581 so->tmp_data_size = size;
582
583 return 0;
584}
585
586static void
587shared_output_update(struct shared_output *so);
588
589static void
590shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
591{
592 struct shared_output *so = data;
593
594 if (cb != so->parent.frame_cb)
595 return;
596
597 wl_callback_destroy(cb);
598 so->parent.frame_cb = NULL;
599
600 shared_output_update(so);
601}
602
603static const struct wl_callback_listener shared_output_frame_listener = {
604 shared_output_frame_callback
605};
606
607static void
608shared_output_update(struct shared_output *so)
609{
610 struct ss_shm_buffer *sb;
611 pixman_box32_t *r;
612 int i, nrects;
613 pixman_transform_t transform;
614
615 /* Only update if we need to */
616 if (!so->cache_dirty || so->parent.frame_cb)
617 return;
618
619 sb = shared_output_get_shm_buffer(so);
620 if (sb == NULL) {
621 shared_output_destroy(so);
622 return;
623 }
624
625 output_compute_transform(so->output, &transform);
626 pixman_image_set_transform(so->cache_image, &transform);
627
628 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
629
630 if (so->output->current_scale == 1) {
631 pixman_image_set_filter(so->cache_image,
632 PIXMAN_FILTER_NEAREST, NULL, 0);
633 } else {
634 pixman_image_set_filter(so->cache_image,
635 PIXMAN_FILTER_BILINEAR, NULL, 0);
636 }
637
638 pixman_image_composite32(PIXMAN_OP_SRC,
639 so->cache_image, /* src */
640 NULL, /* mask */
641 sb->pm_image, /* dest */
642 0, 0, /* src_x, src_y */
643 0, 0, /* mask_x, mask_y */
644 0, 0, /* dest_x, dest_y */
645 so->output->width, /* width */
646 so->output->height /* height */);
647
648 pixman_image_set_transform(sb->pm_image, NULL);
649 pixman_image_set_clip_region32(sb->pm_image, NULL);
650
651 r = pixman_region32_rectangles(&sb->damage, &nrects);
652 for (i = 0; i < nrects; ++i)
653 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
654 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
655
656 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
657
658 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
659 wl_callback_add_listener(so->parent.frame_cb,
660 &shared_output_frame_listener, so);
661
662 wl_surface_commit(so->parent.surface);
663 wl_callback_destroy(wl_display_sync(so->parent.display));
664 wl_display_flush(so->parent.display);
665
666 /* Clear the buffer damage */
667 pixman_region32_fini(&sb->damage);
668 pixman_region32_init(&sb->damage);
669}
670
671static void
672shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
673{
674 struct shared_output *so = data;
675
676 so->parent.shm_formats |= (1 << format);
677}
678
679struct wl_shm_listener shm_listener = {
680 shm_handle_format
681};
682
683static void
684registry_handle_global(void *data, struct wl_registry *registry,
685 uint32_t id, const char *interface, uint32_t version)
686{
687 struct shared_output *so = data;
688
689 if (strcmp(interface, "wl_compositor") == 0) {
690 so->parent.compositor =
691 wl_registry_bind(registry,
692 id, &wl_compositor_interface, 1);
693 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
694 so->parent.output =
695 wl_registry_bind(registry,
696 id, &wl_output_interface, 1);
697 } else if (strcmp(interface, "wl_seat") == 0) {
698 ss_seat_create(so, id);
699 } else if (strcmp(interface, "wl_shm") == 0) {
700 so->parent.shm =
701 wl_registry_bind(registry,
702 id, &wl_shm_interface, 1);
703 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800704 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500705 so->parent.fshell =
706 wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800707 id,
708 &zwp_fullscreen_shell_v1_interface,
709 1);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500710 }
711}
712
713static void
714registry_handle_global_remove(void *data, struct wl_registry *registry,
715 uint32_t name)
716{
717}
718
719static const struct wl_registry_listener registry_listener = {
720 registry_handle_global,
721 registry_handle_global_remove
722};
723
724static int
725shared_output_handle_event(int fd, uint32_t mask, void *data)
726{
727 struct shared_output *so = data;
728 int count = 0;
729
730 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
731 shared_output_destroy(so);
732 return 0;
733 }
734
735 if (mask & WL_EVENT_READABLE)
736 count = wl_display_dispatch(so->parent.display);
737 if (mask & WL_EVENT_WRITABLE)
738 wl_display_flush(so->parent.display);
739
740 if (mask == 0) {
741 count = wl_display_dispatch_pending(so->parent.display);
742 wl_display_flush(so->parent.display);
743 }
744
745 return count;
746}
747
748static void
749output_destroyed(struct wl_listener *l, void *data)
750{
751 struct shared_output *so;
752
753 so = container_of(l, struct shared_output, output_destroyed);
754
755 shared_output_destroy(so);
756}
757
758static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800759mode_feedback_ok(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500760{
761 struct shared_output *so = data;
762
Jonas Ådahl496adb32015-11-17 16:00:27 +0800763 zwp_fullscreen_shell_mode_feedback_v1_destroy(so->parent.mode_feedback);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500764}
765
766static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800767mode_feedback_failed(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 weston_log("Screen share failed: present_surface_for_mode failed\n");
774 shared_output_destroy(so);
775}
776
Jonas Ådahl496adb32015-11-17 16:00:27 +0800777struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand47928d82014-04-02 19:54:01 -0500778 mode_feedback_ok,
779 mode_feedback_failed,
780 mode_feedback_ok,
781};
782
783static void
784shared_output_repainted(struct wl_listener *listener, void *data)
785{
786 struct shared_output *so =
787 container_of(listener, struct shared_output, frame_listener);
788 pixman_region32_t damage;
789 struct ss_shm_buffer *sb;
790 int32_t x, y, width, height, stride;
791 int i, nrects, do_yflip;
792 pixman_box32_t *r;
793 uint32_t *cache_data;
794
795 /* Damage in output coordinates */
796 pixman_region32_init(&damage);
797 pixman_region32_intersect(&damage, &so->output->region,
798 &so->output->previous_damage);
799 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
800
801 /* Apply damage to all buffers */
802 wl_list_for_each(sb, &so->shm.buffers, link)
803 pixman_region32_union(&sb->damage, &sb->damage, &damage);
804
805 /* Transform to buffer coordinates */
806 weston_transformed_region(so->output->width, so->output->height,
807 so->output->transform,
808 so->output->current_scale,
809 &damage, &damage);
810
811 width = so->output->current_mode->width;
812 height = so->output->current_mode->height;
813 stride = width;
814
815 if (!so->cache_image ||
816 pixman_image_get_width(so->cache_image) != width ||
817 pixman_image_get_height(so->cache_image) != height) {
818 if (so->cache_image)
819 pixman_image_unref(so->cache_image);
820
821 so->cache_image =
822 pixman_image_create_bits(PIXMAN_a8r8g8b8,
823 width, height, NULL,
824 stride);
825 if (!so->cache_image) {
826 shared_output_destroy(so);
827 return;
828 }
829
830 pixman_region32_fini(&damage);
831 pixman_region32_init_rect(&damage, 0, 0, width, height);
832 }
833
834 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
835 shared_output_destroy(so);
836 return;
837 }
838
839 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
840
841 cache_data = pixman_image_get_data(so->cache_image);
842 r = pixman_region32_rectangles(&damage, &nrects);
843 for (i = 0; i < nrects; ++i) {
844 x = r[i].x1;
845 y = r[i].y1;
846 width = r[i].x2 - r[i].x1;
847 height = r[i].y2 - r[i].y1;
848
849 if (do_yflip) {
850 so->output->compositor->renderer->read_pixels(
851 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
852 x, so->output->current_mode->height - r[i].y2,
853 width, height);
854
855 pixman_blt(so->tmp_data, cache_data, -width, stride,
856 32, 32, 0, 1 - height, x, y, width, height);
857 } else {
858 so->output->compositor->renderer->read_pixels(
859 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
860 x, y, width, height);
861
862 pixman_blt(so->tmp_data, cache_data, width, stride,
863 32, 32, 0, 0, x, y, width, height);
864 }
865 }
866
867 pixman_region32_fini(&damage);
868
869 so->cache_dirty = 1;
870
871 shared_output_update(so);
872}
873
874static struct shared_output *
875shared_output_create(struct weston_output *output, int parent_fd)
876{
877 struct shared_output *so;
878 struct wl_event_loop *loop;
879 struct ss_seat *seat;
880 int epoll_fd;
881
882 so = zalloc(sizeof *so);
883 if (so == NULL)
884 goto err_close;
885
886 wl_list_init(&so->seat_list);
887
888 so->parent.display = wl_display_connect_to_fd(parent_fd);
889 if (!so->parent.display)
890 goto err_alloc;
891
892 so->parent.registry = wl_display_get_registry(so->parent.display);
893 if (!so->parent.registry)
894 goto err_display;
895 wl_registry_add_listener(so->parent.registry,
896 &registry_listener, so);
897 wl_display_roundtrip(so->parent.display);
898 if (so->parent.shm == NULL) {
899 weston_log("Screen share failed: No wl_shm found\n");
900 goto err_display;
901 }
902 if (so->parent.fshell == NULL) {
903 weston_log("Screen share failed: "
904 "Parent does not support wl_fullscreen_shell\n");
905 goto err_display;
906 }
907 if (so->parent.compositor == NULL) {
908 weston_log("Screen share failed: No wl_compositor found\n");
909 goto err_display;
910 }
911
912 /* Get SHM formats */
913 wl_display_roundtrip(so->parent.display);
914 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
915 weston_log("Screen share failed: "
916 "WL_SHM_FORMAT_XRGB8888 not available\n");
917 goto err_display;
918 }
919
920 so->parent.surface =
921 wl_compositor_create_surface(so->parent.compositor);
922 if (!so->parent.surface) {
Chris Michael5169a572015-10-01 10:51:30 -0400923 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500924 goto err_display;
925 }
926
927 so->parent.mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800928 zwp_fullscreen_shell_v1_present_surface_for_mode(so->parent.fshell,
929 so->parent.surface,
930 so->parent.output,
931 output->current_mode->refresh);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500932 if (!so->parent.mode_feedback) {
Chris Michael5169a572015-10-01 10:51:30 -0400933 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500934 goto err_display;
935 }
Jonas Ådahl496adb32015-11-17 16:00:27 +0800936 zwp_fullscreen_shell_mode_feedback_v1_add_listener(so->parent.mode_feedback,
937 &mode_feedback_listener,
938 so);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500939
940 loop = wl_display_get_event_loop(output->compositor->wl_display);
941
942 epoll_fd = wl_display_get_fd(so->parent.display);
943 so->event_source =
944 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
945 shared_output_handle_event, so);
946 if (!so->event_source) {
Chris Michael5169a572015-10-01 10:51:30 -0400947 weston_log("Screen share failed: %m\n");
Jason Ekstrand47928d82014-04-02 19:54:01 -0500948 goto err_display;
949 }
950
951 /* Ok, everything's created. We should be good to go */
952 wl_list_init(&so->shm.buffers);
953 wl_list_init(&so->shm.free_buffers);
954
955 so->output = output;
956 so->output_destroyed.notify = output_destroyed;
957 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
958
959 so->frame_listener.notify = shared_output_repainted;
960 wl_signal_add(&output->frame_signal, &so->frame_listener);
961 output->disable_planes++;
962 weston_output_damage(output);
963
964 return so;
965
966err_display:
967 wl_list_for_each(seat, &so->seat_list, link)
968 ss_seat_destroy(seat);
969 wl_display_disconnect(so->parent.display);
970err_alloc:
971 free(so);
972err_close:
973 close(parent_fd);
974 return NULL;
975}
976
977static void
978shared_output_destroy(struct shared_output *so)
979{
980 struct ss_shm_buffer *buffer, *bnext;
981
Jason Ekstrand27efc062014-04-03 11:51:42 -0500982 so->output->disable_planes--;
983
Jason Ekstrand47928d82014-04-02 19:54:01 -0500984 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
985 ss_shm_buffer_destroy(buffer);
Bryce Harrington29f09fe2015-07-10 18:52:56 -0700986 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, free_link)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500987 ss_shm_buffer_destroy(buffer);
988
989 wl_display_disconnect(so->parent.display);
990 wl_event_source_remove(so->event_source);
991
992 wl_list_remove(&so->output_destroyed.link);
993 wl_list_remove(&so->frame_listener.link);
994
995 pixman_image_unref(so->cache_image);
996 free(so->tmp_data);
997
998 free(so);
999}
1000
1001static struct shared_output *
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001002weston_output_share(struct weston_output *output, const char* command)
Jason Ekstrand47928d82014-04-02 19:54:01 -05001003{
1004 int sv[2];
1005 char str[32];
1006 pid_t pid;
1007 sigset_t allsigs;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001008 char *const argv[] = {
1009 "/bin/sh",
1010 "-c",
1011 (char*)command,
1012 NULL
1013 };
Jason Ekstrand47928d82014-04-02 19:54:01 -05001014
1015 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1016 weston_log("weston_output_share: socketpair failed: %m\n");
1017 return NULL;
1018 }
1019
1020 pid = fork();
1021
1022 if (pid == -1) {
1023 close(sv[0]);
1024 close(sv[1]);
1025 weston_log("weston_output_share: fork failed: %m\n");
1026 return NULL;
1027 }
1028
1029 if (pid == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001030 /* do not give our signal mask to the new process */
1031 sigfillset(&allsigs);
1032 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1033
1034 /* Launch clients as the user. Do not launch clients with
1035 * wrong euid. */
1036 if (seteuid(getuid()) == -1) {
1037 weston_log("weston_output_share: setuid failed: %m\n");
1038 abort();
1039 }
1040
1041 sv[1] = dup(sv[1]);
1042 if (sv[1] == -1) {
1043 weston_log("weston_output_share: dup failed: %m\n");
1044 abort();
1045 }
1046
1047 snprintf(str, sizeof str, "%d", sv[1]);
1048 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1049
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001050 execv(argv[0], argv);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001051 weston_log("weston_output_share: exec failed: %m\n");
1052 abort();
1053 } else {
1054 close(sv[1]);
1055 return shared_output_create(output, sv[0]);
1056 }
1057
1058 return NULL;
1059}
1060
1061static struct weston_output *
1062weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1063{
1064 struct weston_output *output;
1065
1066 wl_list_for_each(output, &c->output_list, link) {
1067 if (x >= output->x && y >= output->y &&
1068 x < output->x + output->width &&
1069 y < output->y + output->height)
1070 return output;
1071 }
1072
1073 return NULL;
1074}
1075
1076static void
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001077share_output_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Jason Ekstrand47928d82014-04-02 19:54:01 -05001078 void *data)
1079{
1080 struct weston_output *output;
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001081 struct weston_pointer *pointer;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001082 struct screen_share *ss = data;
Jason Ekstrand47928d82014-04-02 19:54:01 -05001083
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001084 pointer = weston_seat_get_pointer(keyboard->seat);
1085 if (!pointer) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001086 weston_log("Cannot pick output: Seat does not have pointer\n");
1087 return;
1088 }
1089
Dawid Gajownik0b2bcbf2015-07-31 23:46:46 -03001090 output = weston_output_find(pointer->seat->compositor,
1091 wl_fixed_to_int(pointer->x),
1092 wl_fixed_to_int(pointer->y));
Jason Ekstrand47928d82014-04-02 19:54:01 -05001093 if (!output) {
1094 weston_log("Cannot pick output: Pointer not on any output\n");
1095 return;
1096 }
1097
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001098 weston_output_share(output, ss->command);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001099}
1100
1101WL_EXPORT int
1102module_init(struct weston_compositor *compositor,
1103 int *argc, char *argv[])
1104{
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001105 struct screen_share *ss;
1106 struct weston_config_section *section;
1107
1108 ss = zalloc(sizeof *ss);
1109 if (ss == NULL)
1110 return -1;
1111 ss->compositor = compositor;
1112
1113 section = weston_config_get_section(compositor->config, "screen-share",
1114 NULL, NULL);
1115
1116 weston_config_section_get_string(section, "command", &ss->command, "");
1117
Jason Ekstrand47928d82014-04-02 19:54:01 -05001118 weston_compositor_add_key_binding(compositor, KEY_S,
1119 MODIFIER_CTRL | MODIFIER_ALT,
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001120 share_output_binding, ss);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001121 return 0;
1122}