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