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