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