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