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