blob: 92c4e02897fecb1ee5d52b80d1e1dd6f6a10d990 [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 Cruz4678bab2015-06-15 15:37:07 -070043#include "shared/os-compatibility.h"
Jason Ekstrand47928d82014-04-02 19:54:01 -050044#include "fullscreen-shell-client-protocol.h"
45
46struct shared_output {
47 struct weston_output *output;
48 struct wl_listener output_destroyed;
49 struct wl_list seat_list;
50
51 struct {
52 struct wl_display *display;
53 struct wl_registry *registry;
54 struct wl_compositor *compositor;
55 struct wl_shm *shm;
56 uint32_t shm_formats;
57 struct _wl_fullscreen_shell *fshell;
58 struct wl_output *output;
59 struct wl_surface *surface;
60 struct wl_callback *frame_cb;
61 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
62 } parent;
63
64 struct wl_event_source *event_source;
65 struct wl_listener frame_listener;
66
67 struct {
68 int32_t width, height;
69
70 struct wl_list buffers;
71 struct wl_list free_buffers;
72 } shm;
73
74 int cache_dirty;
75 pixman_image_t *cache_image;
76 uint32_t *tmp_data;
77 size_t tmp_data_size;
78};
79
80struct ss_seat {
81 struct weston_seat base;
82 struct shared_output *output;
83 struct wl_list link;
84
85 struct {
86 struct wl_seat *seat;
87 struct wl_pointer *pointer;
88 struct wl_keyboard *keyboard;
89 } parent;
90
91 enum weston_key_state_update keyboard_state_update;
92 uint32_t key_serial;
93};
94
95struct ss_shm_buffer {
96 struct shared_output *output;
97 struct wl_list link;
98 struct wl_list free_link;
99
100 struct wl_buffer *buffer;
101 void *data;
102 size_t size;
103 pixman_region32_t damage;
104
105 pixman_image_t *pm_image;
106};
107
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100108struct screen_share {
109 struct weston_compositor *compositor;
110 char *command;
111};
112
Jason Ekstrand47928d82014-04-02 19:54:01 -0500113static void
114ss_seat_handle_pointer_enter(void *data, struct wl_pointer *pointer,
115 uint32_t serial, struct wl_surface *surface,
116 wl_fixed_t x, wl_fixed_t y)
117{
118 struct ss_seat *seat = data;
119
120 /* No transformation of input position is required here because we are
121 * always receiving the input in the same coordinates as the output. */
122
123 notify_pointer_focus(&seat->base, NULL, 0, 0);
124}
125
126static void
127ss_seat_handle_pointer_leave(void *data, struct wl_pointer *pointer,
128 uint32_t serial, struct wl_surface *surface)
129{
130 struct ss_seat *seat = data;
131
132 notify_pointer_focus(&seat->base, NULL, 0, 0);
133}
134
135static void
136ss_seat_handle_motion(void *data, struct wl_pointer *pointer,
137 uint32_t time, wl_fixed_t x, wl_fixed_t y)
138{
139 struct ss_seat *seat = data;
140
141 /* No transformation of input position is required here because we are
142 * always receiving the input in the same coordinates as the output. */
143
144 notify_motion_absolute(&seat->base, time, x, y);
145}
146
147static void
148ss_seat_handle_button(void *data, struct wl_pointer *pointer,
149 uint32_t serial, uint32_t time, uint32_t button,
150 uint32_t state)
151{
152 struct ss_seat *seat = data;
153
154 notify_button(&seat->base, time, button, state);
155}
156
157static void
158ss_seat_handle_axis(void *data, struct wl_pointer *pointer,
159 uint32_t time, uint32_t axis, wl_fixed_t value)
160{
161 struct ss_seat *seat = data;
162
163 notify_axis(&seat->base, time, axis, value);
164}
165
166static const struct wl_pointer_listener ss_seat_pointer_listener = {
167 ss_seat_handle_pointer_enter,
168 ss_seat_handle_pointer_leave,
169 ss_seat_handle_motion,
170 ss_seat_handle_button,
171 ss_seat_handle_axis,
172};
173
174static void
175ss_seat_handle_keymap(void *data, struct wl_keyboard *keyboard,
176 uint32_t format, int fd, uint32_t size)
177{
178 struct ss_seat *seat = data;
179 struct xkb_keymap *keymap;
180 char *map_str;
181
182 if (!data)
183 goto error;
184
185 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
186 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
187 if (map_str == MAP_FAILED) {
188 weston_log("mmap failed: %m\n");
189 goto error;
190 }
191
Ran Benita2e1968f2014-08-19 23:59:51 +0300192 keymap = xkb_keymap_new_from_string(seat->base.compositor->xkb_context,
193 map_str,
194 XKB_KEYMAP_FORMAT_TEXT_V1,
195 0);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500196 munmap(map_str, size);
197
198 if (!keymap) {
199 weston_log("failed to compile keymap\n");
200 goto error;
201 }
202
203 seat->keyboard_state_update = STATE_UPDATE_NONE;
204 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
205 weston_log("No keymap provided; falling back to default\n");
206 keymap = NULL;
207 seat->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
208 } else {
209 weston_log("Invalid keymap\n");
210 goto error;
211 }
212
213 close(fd);
214
215 if (seat->base.keyboard)
216 weston_seat_update_keymap(&seat->base, keymap);
217 else
218 weston_seat_init_keyboard(&seat->base, keymap);
219
Ran Benitac9c74152014-08-19 23:59:52 +0300220 xkb_keymap_unref(keymap);
Jason Ekstrand47928d82014-04-02 19:54:01 -0500221
222 return;
223
224error:
225 wl_keyboard_release(seat->parent.keyboard);
226 close(fd);
227}
228
229static void
230ss_seat_handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
231 uint32_t serial, struct wl_surface *surface,
232 struct wl_array *keys)
233{
234 struct ss_seat *seat = data;
235
236 /* XXX: If we get a modifier event immediately before the focus,
237 * we should try to keep the same serial. */
238 notify_keyboard_focus_in(&seat->base, keys,
239 STATE_UPDATE_AUTOMATIC);
240}
241
242static void
243ss_seat_handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
244 uint32_t serial, struct wl_surface *surface)
245{
246 struct ss_seat *seat = data;
247
248 notify_keyboard_focus_out(&seat->base);
249}
250
251static void
252ss_seat_handle_key(void *data, struct wl_keyboard *keyboard,
253 uint32_t serial, uint32_t time,
254 uint32_t key, uint32_t state)
255{
256 struct ss_seat *seat = data;
257
258 seat->key_serial = serial;
259 notify_key(&seat->base, time, key,
260 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
261 WL_KEYBOARD_KEY_STATE_RELEASED,
262 seat->keyboard_state_update);
263}
264
265static void
266ss_seat_handle_modifiers(void *data, struct wl_keyboard *keyboard,
267 uint32_t serial_in, uint32_t mods_depressed,
268 uint32_t mods_latched, uint32_t mods_locked,
269 uint32_t group)
270{
271 struct ss_seat *seat = data;
272 struct weston_compositor *c = seat->output->output->compositor;
273 uint32_t serial_out;
274
275 /* If we get a key event followed by a modifier event with the
276 * same serial number, then we try to preserve those semantics by
277 * reusing the same serial number on the way out too. */
278 if (serial_in == seat->key_serial)
279 serial_out = wl_display_get_serial(c->wl_display);
280 else
281 serial_out = wl_display_next_serial(c->wl_display);
282
283 xkb_state_update_mask(seat->base.keyboard->xkb_state.state,
284 mods_depressed, mods_latched,
285 mods_locked, 0, 0, group);
286 notify_modifiers(&seat->base, serial_out);
287}
288
289static const struct wl_keyboard_listener ss_seat_keyboard_listener = {
290 ss_seat_handle_keymap,
291 ss_seat_handle_keyboard_enter,
292 ss_seat_handle_keyboard_leave,
293 ss_seat_handle_key,
294 ss_seat_handle_modifiers,
295};
296
297static void
298ss_seat_handle_capabilities(void *data, struct wl_seat *seat,
299 enum wl_seat_capability caps)
300{
301 struct ss_seat *ss_seat = data;
302
303 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !ss_seat->parent.pointer) {
304 ss_seat->parent.pointer = wl_seat_get_pointer(seat);
305 wl_pointer_set_user_data(ss_seat->parent.pointer, ss_seat);
306 wl_pointer_add_listener(ss_seat->parent.pointer,
307 &ss_seat_pointer_listener, ss_seat);
308 weston_seat_init_pointer(&ss_seat->base);
309 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && ss_seat->parent.pointer) {
310 wl_pointer_destroy(ss_seat->parent.pointer);
311 ss_seat->parent.pointer = NULL;
312 }
313
314 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !ss_seat->parent.keyboard) {
315 ss_seat->parent.keyboard = wl_seat_get_keyboard(seat);
316 wl_keyboard_set_user_data(ss_seat->parent.keyboard, ss_seat);
317 wl_keyboard_add_listener(ss_seat->parent.keyboard,
318 &ss_seat_keyboard_listener, ss_seat);
319 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && ss_seat->parent.keyboard) {
320 wl_keyboard_destroy(ss_seat->parent.keyboard);
321 ss_seat->parent.keyboard = NULL;
322 }
323}
324
325static const struct wl_seat_listener ss_seat_listener = {
326 ss_seat_handle_capabilities,
327};
328
329static struct ss_seat *
330ss_seat_create(struct shared_output *so, uint32_t id)
331{
332 struct ss_seat *seat;
333
334 seat = zalloc(sizeof *seat);
335 if (seat == NULL)
336 return NULL;
337
338 weston_seat_init(&seat->base, so->output->compositor, "default");
339 seat->output = so;
340 seat->parent.seat = wl_registry_bind(so->parent.registry, id,
341 &wl_seat_interface, 1);
342 wl_list_insert(so->seat_list.prev, &seat->link);
343
344 wl_seat_add_listener(seat->parent.seat, &ss_seat_listener, seat);
345 wl_seat_set_user_data(seat->parent.seat, seat);
346
347 return seat;
348}
349
350static void
351ss_seat_destroy(struct ss_seat *seat)
352{
353 if (seat->parent.pointer)
354 wl_pointer_release(seat->parent.pointer);
355 if (seat->parent.keyboard)
356 wl_keyboard_release(seat->parent.keyboard);
357 wl_seat_destroy(seat->parent.seat);
358
359 wl_list_remove(&seat->link);
360
361 weston_seat_release(&seat->base);
362
363 free(seat);
364}
365
366static void
367ss_shm_buffer_destroy(struct ss_shm_buffer *buffer)
368{
369 pixman_image_unref(buffer->pm_image);
370
371 wl_buffer_destroy(buffer->buffer);
372 munmap(buffer->data, buffer->size);
373
374 pixman_region32_fini(&buffer->damage);
375
376 wl_list_remove(&buffer->link);
377 wl_list_remove(&buffer->free_link);
378 free(buffer);
379}
380
381static void
382buffer_release(void *data, struct wl_buffer *buffer)
383{
384 struct ss_shm_buffer *sb = data;
385
386 if (sb->output) {
387 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
388 } else {
389 ss_shm_buffer_destroy(sb);
390 }
391}
392
393static const struct wl_buffer_listener buffer_listener = {
394 buffer_release
395};
396
397static struct ss_shm_buffer *
398shared_output_get_shm_buffer(struct shared_output *so)
399{
400 struct ss_shm_buffer *sb, *bnext;
401 struct wl_shm_pool *pool;
402 int width, height, stride;
403 int fd;
404 unsigned char *data;
405
406 width = so->output->width;
407 height = so->output->height;
408 stride = width * 4;
409
410 /* If the size of the output changed, we free the old buffers and
411 * make new ones. */
412 if (so->shm.width != width ||
413 so->shm.height != height) {
414
415 /* Destroy free buffers */
416 wl_list_for_each_safe(sb, bnext, &so->shm.free_buffers, link)
417 ss_shm_buffer_destroy(sb);
418
419 /* Orphan in-use buffers so they get destroyed */
420 wl_list_for_each(sb, &so->shm.buffers, link)
421 sb->output = NULL;
422
423 so->shm.width = width;
424 so->shm.height = height;
425 }
426
427 if (!wl_list_empty(&so->shm.free_buffers)) {
428 sb = container_of(so->shm.free_buffers.next,
429 struct ss_shm_buffer, free_link);
430 wl_list_remove(&sb->free_link);
431 wl_list_init(&sb->free_link);
432
433 return sb;
434 }
435
436 fd = os_create_anonymous_file(height * stride);
437 if (fd < 0) {
438 weston_log("os_create_anonymous_file: %m");
439 return NULL;
440 }
441
442 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
443 if (data == MAP_FAILED) {
444 weston_log("mmap: %m");
Hardeninge57d1f22014-04-11 09:06:58 +0200445 goto out_close;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500446 }
447
448 sb = zalloc(sizeof *sb);
Hardeninge57d1f22014-04-11 09:06:58 +0200449 if (!sb)
450 goto out_unmap;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500451
452 sb->output = so;
453 wl_list_init(&sb->free_link);
454 wl_list_insert(&so->shm.buffers, &sb->link);
455
456 pixman_region32_init_rect(&sb->damage, 0, 0, width, height);
457
458 sb->data = data;
459 sb->size = height * stride;
460
461 pool = wl_shm_create_pool(so->parent.shm, fd, sb->size);
462
463 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
464 width, height, stride,
465 WL_SHM_FORMAT_ARGB8888);
466 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
467 wl_shm_pool_destroy(pool);
468 close(fd);
Hardeninge57d1f22014-04-11 09:06:58 +0200469 fd = -1;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500470
471 memset(data, 0, sb->size);
472
473 sb->pm_image =
474 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
475 (uint32_t *)data, stride);
Hardeninge57d1f22014-04-11 09:06:58 +0200476 if (!sb->pm_image)
477 goto out_pixman_error;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500478
479 return sb;
Hardeninge57d1f22014-04-11 09:06:58 +0200480
481out_pixman_error:
482 pixman_region32_fini(&sb->damage);
483out_unmap:
484 munmap(data, height * stride);
485out_close:
486 if (fd != -1)
487 close(fd);
488 return NULL;
Jason Ekstrand47928d82014-04-02 19:54:01 -0500489}
490
491static void
492output_compute_transform(struct weston_output *output,
493 pixman_transform_t *transform)
494{
495 pixman_fixed_t fw, fh;
496
497 pixman_transform_init_identity(transform);
498
499 fw = pixman_int_to_fixed(output->width);
500 fh = pixman_int_to_fixed(output->height);
501
502 switch (output->transform) {
503 case WL_OUTPUT_TRANSFORM_FLIPPED:
504 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
505 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
506 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
507 pixman_transform_scale(transform, NULL,
508 pixman_int_to_fixed (-1),
509 pixman_int_to_fixed (1));
510 pixman_transform_translate(transform, NULL, fw, 0);
511 }
512
513 switch (output->transform) {
514 default:
515 case WL_OUTPUT_TRANSFORM_NORMAL:
516 case WL_OUTPUT_TRANSFORM_FLIPPED:
517 break;
518 case WL_OUTPUT_TRANSFORM_90:
519 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
520 pixman_transform_rotate(transform, NULL, 0, pixman_fixed_1);
521 pixman_transform_translate(transform, NULL, fh, 0);
522 break;
523 case WL_OUTPUT_TRANSFORM_180:
524 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
525 pixman_transform_rotate(transform, NULL, -pixman_fixed_1, 0);
526 pixman_transform_translate(transform, NULL, fw, fh);
527 break;
528 case WL_OUTPUT_TRANSFORM_270:
529 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
530 pixman_transform_rotate(transform, NULL, 0, -pixman_fixed_1);
531 pixman_transform_translate(transform, NULL, 0, fw);
532 break;
533 }
534
535 pixman_transform_scale(transform, NULL,
536 pixman_fixed_1 * output->current_scale,
537 pixman_fixed_1 * output->current_scale);
538}
539
540static void
541shared_output_destroy(struct shared_output *so);
542
543static int
544shared_output_ensure_tmp_data(struct shared_output *so,
545 pixman_region32_t *region)
546{
547 pixman_box32_t *ext;
548 size_t size;
549
550 if (!pixman_region32_not_empty(region))
551 return 0;
552
553 ext = pixman_region32_extents(region);
554
555 /* Damage is in output coordinates.
556 *
557 * We are multiplying by 4 because the temporary data needs to be able
558 * to store an 32 bit-per-pixel buffer.
559 */
560 size = 4 * (ext->x2 - ext->x1) * (ext->y2 - ext->y1)
561 * so->output->current_scale * so->output->current_scale;
562
563 if (so->tmp_data != NULL && size <= so->tmp_data_size)
564 return 0;
565
566 free(so->tmp_data);
567 so->tmp_data = malloc(size);
568 if (so->tmp_data == NULL) {
569 so->tmp_data_size = 0;
570 errno = ENOMEM;
571 return -1;
572 }
573
574 so->tmp_data_size = size;
575
576 return 0;
577}
578
579static void
580shared_output_update(struct shared_output *so);
581
582static void
583shared_output_frame_callback(void *data, struct wl_callback *cb, uint32_t time)
584{
585 struct shared_output *so = data;
586
587 if (cb != so->parent.frame_cb)
588 return;
589
590 wl_callback_destroy(cb);
591 so->parent.frame_cb = NULL;
592
593 shared_output_update(so);
594}
595
596static const struct wl_callback_listener shared_output_frame_listener = {
597 shared_output_frame_callback
598};
599
600static void
601shared_output_update(struct shared_output *so)
602{
603 struct ss_shm_buffer *sb;
604 pixman_box32_t *r;
605 int i, nrects;
606 pixman_transform_t transform;
607
608 /* Only update if we need to */
609 if (!so->cache_dirty || so->parent.frame_cb)
610 return;
611
612 sb = shared_output_get_shm_buffer(so);
613 if (sb == NULL) {
614 shared_output_destroy(so);
615 return;
616 }
617
618 output_compute_transform(so->output, &transform);
619 pixman_image_set_transform(so->cache_image, &transform);
620
621 pixman_image_set_clip_region32(sb->pm_image, &sb->damage);
622
623 if (so->output->current_scale == 1) {
624 pixman_image_set_filter(so->cache_image,
625 PIXMAN_FILTER_NEAREST, NULL, 0);
626 } else {
627 pixman_image_set_filter(so->cache_image,
628 PIXMAN_FILTER_BILINEAR, NULL, 0);
629 }
630
631 pixman_image_composite32(PIXMAN_OP_SRC,
632 so->cache_image, /* src */
633 NULL, /* mask */
634 sb->pm_image, /* dest */
635 0, 0, /* src_x, src_y */
636 0, 0, /* mask_x, mask_y */
637 0, 0, /* dest_x, dest_y */
638 so->output->width, /* width */
639 so->output->height /* height */);
640
641 pixman_image_set_transform(sb->pm_image, NULL);
642 pixman_image_set_clip_region32(sb->pm_image, NULL);
643
644 r = pixman_region32_rectangles(&sb->damage, &nrects);
645 for (i = 0; i < nrects; ++i)
646 wl_surface_damage(so->parent.surface, r[i].x1, r[i].y1,
647 r[i].x2 - r[i].x1, r[i].y2 - r[i].y1);
648
649 wl_surface_attach(so->parent.surface, sb->buffer, 0, 0);
650
651 so->parent.frame_cb = wl_surface_frame(so->parent.surface);
652 wl_callback_add_listener(so->parent.frame_cb,
653 &shared_output_frame_listener, so);
654
655 wl_surface_commit(so->parent.surface);
656 wl_callback_destroy(wl_display_sync(so->parent.display));
657 wl_display_flush(so->parent.display);
658
659 /* Clear the buffer damage */
660 pixman_region32_fini(&sb->damage);
661 pixman_region32_init(&sb->damage);
662}
663
664static void
665shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
666{
667 struct shared_output *so = data;
668
669 so->parent.shm_formats |= (1 << format);
670}
671
672struct wl_shm_listener shm_listener = {
673 shm_handle_format
674};
675
676static void
677registry_handle_global(void *data, struct wl_registry *registry,
678 uint32_t id, const char *interface, uint32_t version)
679{
680 struct shared_output *so = data;
681
682 if (strcmp(interface, "wl_compositor") == 0) {
683 so->parent.compositor =
684 wl_registry_bind(registry,
685 id, &wl_compositor_interface, 1);
686 } else if (strcmp(interface, "wl_output") == 0 && !so->parent.output) {
687 so->parent.output =
688 wl_registry_bind(registry,
689 id, &wl_output_interface, 1);
690 } else if (strcmp(interface, "wl_seat") == 0) {
691 ss_seat_create(so, id);
692 } else if (strcmp(interface, "wl_shm") == 0) {
693 so->parent.shm =
694 wl_registry_bind(registry,
695 id, &wl_shm_interface, 1);
696 wl_shm_add_listener(so->parent.shm, &shm_listener, so);
697 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
698 so->parent.fshell =
699 wl_registry_bind(registry,
700 id, &_wl_fullscreen_shell_interface, 1);
701 }
702}
703
704static void
705registry_handle_global_remove(void *data, struct wl_registry *registry,
706 uint32_t name)
707{
708}
709
710static const struct wl_registry_listener registry_listener = {
711 registry_handle_global,
712 registry_handle_global_remove
713};
714
715static int
716shared_output_handle_event(int fd, uint32_t mask, void *data)
717{
718 struct shared_output *so = data;
719 int count = 0;
720
721 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
722 shared_output_destroy(so);
723 return 0;
724 }
725
726 if (mask & WL_EVENT_READABLE)
727 count = wl_display_dispatch(so->parent.display);
728 if (mask & WL_EVENT_WRITABLE)
729 wl_display_flush(so->parent.display);
730
731 if (mask == 0) {
732 count = wl_display_dispatch_pending(so->parent.display);
733 wl_display_flush(so->parent.display);
734 }
735
736 return count;
737}
738
739static void
740output_destroyed(struct wl_listener *l, void *data)
741{
742 struct shared_output *so;
743
744 so = container_of(l, struct shared_output, output_destroyed);
745
746 shared_output_destroy(so);
747}
748
749static void
750mode_feedback_ok(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
751{
752 struct shared_output *so = data;
753
754 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
755}
756
757static void
758mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
759{
760 struct shared_output *so = data;
761
762 _wl_fullscreen_shell_mode_feedback_destroy(so->parent.mode_feedback);
763
764 weston_log("Screen share failed: present_surface_for_mode failed\n");
765 shared_output_destroy(so);
766}
767
768struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
769 mode_feedback_ok,
770 mode_feedback_failed,
771 mode_feedback_ok,
772};
773
774static void
775shared_output_repainted(struct wl_listener *listener, void *data)
776{
777 struct shared_output *so =
778 container_of(listener, struct shared_output, frame_listener);
779 pixman_region32_t damage;
780 struct ss_shm_buffer *sb;
781 int32_t x, y, width, height, stride;
782 int i, nrects, do_yflip;
783 pixman_box32_t *r;
784 uint32_t *cache_data;
785
786 /* Damage in output coordinates */
787 pixman_region32_init(&damage);
788 pixman_region32_intersect(&damage, &so->output->region,
789 &so->output->previous_damage);
790 pixman_region32_translate(&damage, -so->output->x, -so->output->y);
791
792 /* Apply damage to all buffers */
793 wl_list_for_each(sb, &so->shm.buffers, link)
794 pixman_region32_union(&sb->damage, &sb->damage, &damage);
795
796 /* Transform to buffer coordinates */
797 weston_transformed_region(so->output->width, so->output->height,
798 so->output->transform,
799 so->output->current_scale,
800 &damage, &damage);
801
802 width = so->output->current_mode->width;
803 height = so->output->current_mode->height;
804 stride = width;
805
806 if (!so->cache_image ||
807 pixman_image_get_width(so->cache_image) != width ||
808 pixman_image_get_height(so->cache_image) != height) {
809 if (so->cache_image)
810 pixman_image_unref(so->cache_image);
811
812 so->cache_image =
813 pixman_image_create_bits(PIXMAN_a8r8g8b8,
814 width, height, NULL,
815 stride);
816 if (!so->cache_image) {
817 shared_output_destroy(so);
818 return;
819 }
820
821 pixman_region32_fini(&damage);
822 pixman_region32_init_rect(&damage, 0, 0, width, height);
823 }
824
825 if (shared_output_ensure_tmp_data(so, &damage) < 0) {
826 shared_output_destroy(so);
827 return;
828 }
829
830 do_yflip = !!(so->output->compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
831
832 cache_data = pixman_image_get_data(so->cache_image);
833 r = pixman_region32_rectangles(&damage, &nrects);
834 for (i = 0; i < nrects; ++i) {
835 x = r[i].x1;
836 y = r[i].y1;
837 width = r[i].x2 - r[i].x1;
838 height = r[i].y2 - r[i].y1;
839
840 if (do_yflip) {
841 so->output->compositor->renderer->read_pixels(
842 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
843 x, so->output->current_mode->height - r[i].y2,
844 width, height);
845
846 pixman_blt(so->tmp_data, cache_data, -width, stride,
847 32, 32, 0, 1 - height, x, y, width, height);
848 } else {
849 so->output->compositor->renderer->read_pixels(
850 so->output, PIXMAN_a8r8g8b8, so->tmp_data,
851 x, y, width, height);
852
853 pixman_blt(so->tmp_data, cache_data, width, stride,
854 32, 32, 0, 0, x, y, width, height);
855 }
856 }
857
858 pixman_region32_fini(&damage);
859
860 so->cache_dirty = 1;
861
862 shared_output_update(so);
863}
864
865static struct shared_output *
866shared_output_create(struct weston_output *output, int parent_fd)
867{
868 struct shared_output *so;
869 struct wl_event_loop *loop;
870 struct ss_seat *seat;
871 int epoll_fd;
872
873 so = zalloc(sizeof *so);
874 if (so == NULL)
875 goto err_close;
876
877 wl_list_init(&so->seat_list);
878
879 so->parent.display = wl_display_connect_to_fd(parent_fd);
880 if (!so->parent.display)
881 goto err_alloc;
882
883 so->parent.registry = wl_display_get_registry(so->parent.display);
884 if (!so->parent.registry)
885 goto err_display;
886 wl_registry_add_listener(so->parent.registry,
887 &registry_listener, so);
888 wl_display_roundtrip(so->parent.display);
889 if (so->parent.shm == NULL) {
890 weston_log("Screen share failed: No wl_shm found\n");
891 goto err_display;
892 }
893 if (so->parent.fshell == NULL) {
894 weston_log("Screen share failed: "
895 "Parent does not support wl_fullscreen_shell\n");
896 goto err_display;
897 }
898 if (so->parent.compositor == NULL) {
899 weston_log("Screen share failed: No wl_compositor found\n");
900 goto err_display;
901 }
902
903 /* Get SHM formats */
904 wl_display_roundtrip(so->parent.display);
905 if (!(so->parent.shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
906 weston_log("Screen share failed: "
907 "WL_SHM_FORMAT_XRGB8888 not available\n");
908 goto err_display;
909 }
910
911 so->parent.surface =
912 wl_compositor_create_surface(so->parent.compositor);
913 if (!so->parent.surface) {
914 weston_log("Screen share failed: %m");
915 goto err_display;
916 }
917
918 so->parent.mode_feedback =
919 _wl_fullscreen_shell_present_surface_for_mode(so->parent.fshell,
920 so->parent.surface,
921 so->parent.output,
922 output->current_mode->refresh);
923 if (!so->parent.mode_feedback) {
924 weston_log("Screen share failed: %m");
925 goto err_display;
926 }
927 _wl_fullscreen_shell_mode_feedback_add_listener(so->parent.mode_feedback,
928 &mode_feedback_listener,
929 so);
930
931 loop = wl_display_get_event_loop(output->compositor->wl_display);
932
933 epoll_fd = wl_display_get_fd(so->parent.display);
934 so->event_source =
935 wl_event_loop_add_fd(loop, epoll_fd, WL_EVENT_READABLE,
936 shared_output_handle_event, so);
937 if (!so->event_source) {
938 weston_log("Screen share failed: %m");
939 goto err_display;
940 }
941
942 /* Ok, everything's created. We should be good to go */
943 wl_list_init(&so->shm.buffers);
944 wl_list_init(&so->shm.free_buffers);
945
946 so->output = output;
947 so->output_destroyed.notify = output_destroyed;
948 wl_signal_add(&so->output->destroy_signal, &so->output_destroyed);
949
950 so->frame_listener.notify = shared_output_repainted;
951 wl_signal_add(&output->frame_signal, &so->frame_listener);
952 output->disable_planes++;
953 weston_output_damage(output);
954
955 return so;
956
957err_display:
958 wl_list_for_each(seat, &so->seat_list, link)
959 ss_seat_destroy(seat);
960 wl_display_disconnect(so->parent.display);
961err_alloc:
962 free(so);
963err_close:
964 close(parent_fd);
965 return NULL;
966}
967
968static void
969shared_output_destroy(struct shared_output *so)
970{
971 struct ss_shm_buffer *buffer, *bnext;
972
Jason Ekstrand27efc062014-04-03 11:51:42 -0500973 so->output->disable_planes--;
974
Jason Ekstrand47928d82014-04-02 19:54:01 -0500975 wl_list_for_each_safe(buffer, bnext, &so->shm.buffers, link)
976 ss_shm_buffer_destroy(buffer);
977 wl_list_for_each_safe(buffer, bnext, &so->shm.free_buffers, link)
978 ss_shm_buffer_destroy(buffer);
979
980 wl_display_disconnect(so->parent.display);
981 wl_event_source_remove(so->event_source);
982
983 wl_list_remove(&so->output_destroyed.link);
984 wl_list_remove(&so->frame_listener.link);
985
986 pixman_image_unref(so->cache_image);
987 free(so->tmp_data);
988
989 free(so);
990}
991
992static struct shared_output *
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100993weston_output_share(struct weston_output *output, const char* command)
Jason Ekstrand47928d82014-04-02 19:54:01 -0500994{
995 int sv[2];
996 char str[32];
997 pid_t pid;
998 sigset_t allsigs;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +0100999 char *const argv[] = {
1000 "/bin/sh",
1001 "-c",
1002 (char*)command,
1003 NULL
1004 };
Jason Ekstrand47928d82014-04-02 19:54:01 -05001005
1006 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1007 weston_log("weston_output_share: socketpair failed: %m\n");
1008 return NULL;
1009 }
1010
1011 pid = fork();
1012
1013 if (pid == -1) {
1014 close(sv[0]);
1015 close(sv[1]);
1016 weston_log("weston_output_share: fork failed: %m\n");
1017 return NULL;
1018 }
1019
1020 if (pid == 0) {
Jason Ekstrand47928d82014-04-02 19:54:01 -05001021 /* do not give our signal mask to the new process */
1022 sigfillset(&allsigs);
1023 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
1024
1025 /* Launch clients as the user. Do not launch clients with
1026 * wrong euid. */
1027 if (seteuid(getuid()) == -1) {
1028 weston_log("weston_output_share: setuid failed: %m\n");
1029 abort();
1030 }
1031
1032 sv[1] = dup(sv[1]);
1033 if (sv[1] == -1) {
1034 weston_log("weston_output_share: dup failed: %m\n");
1035 abort();
1036 }
1037
1038 snprintf(str, sizeof str, "%d", sv[1]);
1039 setenv("WAYLAND_SERVER_SOCKET", str, 1);
1040
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001041 execv(argv[0], argv);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001042 weston_log("weston_output_share: exec failed: %m\n");
1043 abort();
1044 } else {
1045 close(sv[1]);
1046 return shared_output_create(output, sv[0]);
1047 }
1048
1049 return NULL;
1050}
1051
1052static struct weston_output *
1053weston_output_find(struct weston_compositor *c, int32_t x, int32_t y)
1054{
1055 struct weston_output *output;
1056
1057 wl_list_for_each(output, &c->output_list, link) {
1058 if (x >= output->x && y >= output->y &&
1059 x < output->x + output->width &&
1060 y < output->y + output->height)
1061 return output;
1062 }
1063
1064 return NULL;
1065}
1066
1067static void
1068share_output_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1069 void *data)
1070{
1071 struct weston_output *output;
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001072 struct screen_share *ss = data;
Jason Ekstrand47928d82014-04-02 19:54:01 -05001073
1074 if (!seat->pointer) {
1075 weston_log("Cannot pick output: Seat does not have pointer\n");
1076 return;
1077 }
1078
1079 output = weston_output_find(seat->compositor,
1080 wl_fixed_to_int(seat->pointer->x),
1081 wl_fixed_to_int(seat->pointer->y));
1082 if (!output) {
1083 weston_log("Cannot pick output: Pointer not on any output\n");
1084 return;
1085 }
1086
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001087 weston_output_share(output, ss->command);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001088}
1089
1090WL_EXPORT int
1091module_init(struct weston_compositor *compositor,
1092 int *argc, char *argv[])
1093{
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001094 struct screen_share *ss;
1095 struct weston_config_section *section;
1096
1097 ss = zalloc(sizeof *ss);
1098 if (ss == NULL)
1099 return -1;
1100 ss->compositor = compositor;
1101
1102 section = weston_config_get_section(compositor->config, "screen-share",
1103 NULL, NULL);
1104
1105 weston_config_section_get_string(section, "command", &ss->command, "");
1106
Jason Ekstrand47928d82014-04-02 19:54:01 -05001107 weston_compositor_add_key_binding(compositor, KEY_S,
1108 MODIFIER_CTRL | MODIFIER_ALT,
Andrew Wedgburydfd9d0d2014-05-02 10:01:18 +01001109 share_output_binding, ss);
Jason Ekstrand47928d82014-04-02 19:54:01 -05001110 return 0;
1111}