56ed474a56e83db7b6b764b61f71e8c98979c9b1
[platform/upstream/weston.git] / libweston / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  * Copyright © 2013 Jason Ekstrand
4  *
5  * 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:
12  *
13  * 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.
25  */
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/mman.h>
38 #include <linux/input.h>
39
40 #include <wayland-client.h>
41 #include <wayland-cursor.h>
42
43 #ifdef ENABLE_EGL
44 #include <wayland-egl.h>
45 #endif
46
47 #include "compositor.h"
48 #include "compositor-wayland.h"
49 #include "gl-renderer.h"
50 #include "weston-egl-ext.h"
51 #include "pixman-renderer.h"
52 #include "shared/helpers.h"
53 #include "shared/image-loader.h"
54 #include "shared/os-compatibility.h"
55 #include "shared/cairo-util.h"
56 #include "shared/timespec-util.h"
57 #include "fullscreen-shell-unstable-v1-client-protocol.h"
58 #include "xdg-shell-unstable-v6-client-protocol.h"
59 #include "presentation-time-server-protocol.h"
60 #include "linux-dmabuf.h"
61 #include "windowed-output-api.h"
62
63 #define WINDOW_TITLE "Weston Compositor"
64
65 struct wayland_backend {
66         struct weston_backend base;
67         struct weston_compositor *compositor;
68
69         struct {
70                 struct wl_display *wl_display;
71                 struct wl_registry *registry;
72                 struct wl_compositor *compositor;
73                 struct wl_shell *shell;
74                 struct zxdg_shell_v6 *xdg_shell;
75                 struct zwp_fullscreen_shell_v1 *fshell;
76                 struct wl_shm *shm;
77
78                 struct wl_list output_list;
79
80                 struct wl_event_source *wl_source;
81                 uint32_t event_mask;
82         } parent;
83
84         bool use_pixman;
85         bool sprawl_across_outputs;
86         bool fullscreen;
87
88         struct theme *theme;
89         cairo_device_t *frame_device;
90         struct wl_cursor_theme *cursor_theme;
91         struct wl_cursor *cursor;
92
93         struct wl_list input_list;
94 };
95
96 struct wayland_output {
97         struct weston_output base;
98
99         struct {
100                 bool draw_initial_frame;
101                 struct wl_surface *surface;
102
103                 struct wl_output *output;
104                 uint32_t global_id;
105
106                 struct wl_shell_surface *shell_surface;
107                 struct zxdg_surface_v6 *xdg_surface;
108                 struct zxdg_toplevel_v6 *xdg_toplevel;
109                 int configure_width, configure_height;
110                 bool wait_for_configure;
111         } parent;
112
113         int keyboard_count;
114
115         char *title;
116         struct frame *frame;
117
118         struct {
119                 struct wl_egl_window *egl_window;
120                 struct {
121                         cairo_surface_t *top;
122                         cairo_surface_t *left;
123                         cairo_surface_t *right;
124                         cairo_surface_t *bottom;
125                 } border;
126         } gl;
127
128         struct {
129                 struct wl_list buffers;
130                 struct wl_list free_buffers;
131         } shm;
132
133         struct weston_mode mode;
134
135         struct wl_callback *frame_cb;
136 };
137
138 struct wayland_parent_output {
139         struct wayland_backend *backend;        /**< convenience */
140         struct wayland_head *head;
141         struct wl_list link;
142
143         struct wl_output *global;
144         uint32_t id;
145
146         struct {
147                 char *make;
148                 char *model;
149                 int32_t width, height;
150                 uint32_t subpixel;
151         } physical;
152
153         int32_t x, y;
154         uint32_t transform;
155         uint32_t scale;
156
157         struct wl_callback *sync_cb;    /**< wl_output < 2 done replacement */
158
159         struct wl_list mode_list;
160         struct weston_mode *preferred_mode;
161         struct weston_mode *current_mode;
162 };
163
164 struct wayland_head {
165         struct weston_head base;
166         struct wayland_parent_output *parent_output;
167 };
168
169 struct wayland_shm_buffer {
170         struct wayland_output *output;
171         struct wl_list link;
172         struct wl_list free_link;
173
174         struct wl_buffer *buffer;
175         void *data;
176         size_t size;
177         pixman_region32_t damage;               /**< in global coords */
178         int frame_damaged;
179
180         pixman_image_t *pm_image;
181         cairo_surface_t *c_surface;
182 };
183
184 struct wayland_input {
185         struct weston_seat base;
186         struct wayland_backend *backend;
187         struct wl_list link;
188
189         struct {
190                 struct wl_seat *seat;
191                 struct wl_pointer *pointer;
192                 struct wl_keyboard *keyboard;
193                 struct wl_touch *touch;
194
195                 struct {
196                         struct wl_surface *surface;
197                         int32_t hx, hy;
198                 } cursor;
199         } parent;
200
201         enum weston_key_state_update keyboard_state_update;
202         uint32_t key_serial;
203         uint32_t enter_serial;
204         uint32_t touch_points;
205         bool touch_active;
206         bool has_focus;
207         int seat_version;
208
209         struct wayland_output *output;
210         struct wayland_output *touch_focus;
211         struct wayland_output *keyboard_focus;
212
213         struct weston_pointer_axis_event vert, horiz;
214 };
215
216 struct gl_renderer_interface *gl_renderer;
217
218 static inline struct wayland_head *
219 to_wayland_head(struct weston_head *base)
220 {
221         return container_of(base, struct wayland_head, base);
222 }
223
224 static inline struct wayland_output *
225 to_wayland_output(struct weston_output *base)
226 {
227         return container_of(base, struct wayland_output, base);
228 }
229
230 static inline struct wayland_backend *
231 to_wayland_backend(struct weston_compositor *base)
232 {
233         return container_of(base->backend, struct wayland_backend, base);
234 }
235
236 static void
237 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
238 {
239         cairo_surface_destroy(buffer->c_surface);
240         pixman_image_unref(buffer->pm_image);
241
242         wl_buffer_destroy(buffer->buffer);
243         munmap(buffer->data, buffer->size);
244
245         pixman_region32_fini(&buffer->damage);
246
247         wl_list_remove(&buffer->link);
248         wl_list_remove(&buffer->free_link);
249         free(buffer);
250 }
251
252 static void
253 buffer_release(void *data, struct wl_buffer *buffer)
254 {
255         struct wayland_shm_buffer *sb = data;
256
257         if (sb->output) {
258                 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
259         } else {
260                 wayland_shm_buffer_destroy(sb);
261         }
262 }
263
264 static const struct wl_buffer_listener buffer_listener = {
265         buffer_release
266 };
267
268 static struct wayland_shm_buffer *
269 wayland_output_get_shm_buffer(struct wayland_output *output)
270 {
271         struct wayland_backend *b =
272                 to_wayland_backend(output->base.compositor);
273         struct wl_shm *shm = b->parent.shm;
274         struct wayland_shm_buffer *sb;
275
276         struct wl_shm_pool *pool;
277         int width, height, stride;
278         int32_t fx, fy;
279         int fd;
280         unsigned char *data;
281
282         if (!wl_list_empty(&output->shm.free_buffers)) {
283                 sb = container_of(output->shm.free_buffers.next,
284                                   struct wayland_shm_buffer, free_link);
285                 wl_list_remove(&sb->free_link);
286                 wl_list_init(&sb->free_link);
287
288                 return sb;
289         }
290
291         if (output->frame) {
292                 width = frame_width(output->frame);
293                 height = frame_height(output->frame);
294         } else {
295                 width = output->base.current_mode->width;
296                 height = output->base.current_mode->height;
297         }
298
299         stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
300
301         fd = os_create_anonymous_file(height * stride);
302         if (fd < 0) {
303                 weston_log("could not create an anonymous file buffer: %m\n");
304                 return NULL;
305         }
306
307         data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
308         if (data == MAP_FAILED) {
309                 weston_log("could not mmap %d memory for data: %m\n", height * stride);
310                 close(fd);
311                 return NULL;
312         }
313
314         sb = zalloc(sizeof *sb);
315         if (sb == NULL) {
316                 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
317                 close(fd);
318                 munmap(data, height * stride);
319                 return NULL;
320         }
321
322         sb->output = output;
323         wl_list_init(&sb->free_link);
324         wl_list_insert(&output->shm.buffers, &sb->link);
325
326         pixman_region32_init(&sb->damage);
327         pixman_region32_copy(&sb->damage, &output->base.region);
328         sb->frame_damaged = 1;
329
330         sb->data = data;
331         sb->size = height * stride;
332
333         pool = wl_shm_create_pool(shm, fd, sb->size);
334
335         sb->buffer = wl_shm_pool_create_buffer(pool, 0,
336                                                width, height,
337                                                stride,
338                                                WL_SHM_FORMAT_ARGB8888);
339         wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
340         wl_shm_pool_destroy(pool);
341         close(fd);
342
343         memset(data, 0, sb->size);
344
345         sb->c_surface =
346                 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
347                                                     width, height, stride);
348
349         fx = 0;
350         fy = 0;
351         if (output->frame)
352                 frame_interior(output->frame, &fx, &fy, 0, 0);
353         sb->pm_image =
354                 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
355                                          (uint32_t *)(data + fy * stride) + fx,
356                                          stride);
357
358         return sb;
359 }
360
361 static void
362 frame_done(void *data, struct wl_callback *callback, uint32_t time)
363 {
364         struct wayland_output *output = data;
365         struct timespec ts;
366
367         assert(callback == output->frame_cb);
368         wl_callback_destroy(callback);
369         output->frame_cb = NULL;
370
371         /* XXX: use the presentation extension for proper timings */
372
373         /*
374          * This is the fallback case, where Presentation extension is not
375          * available from the parent compositor. We do not know the base for
376          * 'time', so we cannot feed it to finish_frame(). Do the only thing
377          * we can, and pretend finish_frame time is when we process this
378          * event.
379          */
380         weston_compositor_read_presentation_clock(output->base.compositor, &ts);
381         weston_output_finish_frame(&output->base, &ts, 0);
382 }
383
384 static const struct wl_callback_listener frame_listener = {
385         frame_done
386 };
387
388 static void
389 draw_initial_frame(struct wayland_output *output)
390 {
391         struct wayland_shm_buffer *sb;
392
393         sb = wayland_output_get_shm_buffer(output);
394
395         /* If we are rendering with GL, then orphan it so that it gets
396          * destroyed immediately */
397         if (output->gl.egl_window)
398                 sb->output = NULL;
399
400         wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
401         wl_surface_damage(output->parent.surface, 0, 0,
402                           output->base.current_mode->width,
403                           output->base.current_mode->height);
404 }
405
406 #ifdef ENABLE_EGL
407 static void
408 wayland_output_update_gl_border(struct wayland_output *output)
409 {
410         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
411         cairo_t *cr;
412
413         if (!output->frame)
414                 return;
415         if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
416                 return;
417
418         fwidth = frame_width(output->frame);
419         fheight = frame_height(output->frame);
420         frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
421
422         if (!output->gl.border.top)
423                 output->gl.border.top =
424                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
425                                                    fwidth, iy);
426         cr = cairo_create(output->gl.border.top);
427         frame_repaint(output->frame, cr);
428         cairo_destroy(cr);
429         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
430                                        fwidth, iy,
431                                        cairo_image_surface_get_stride(output->gl.border.top) / 4,
432                                        cairo_image_surface_get_data(output->gl.border.top));
433
434
435         if (!output->gl.border.left)
436                 output->gl.border.left =
437                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
438                                                    ix, 1);
439         cr = cairo_create(output->gl.border.left);
440         cairo_translate(cr, 0, -iy);
441         frame_repaint(output->frame, cr);
442         cairo_destroy(cr);
443         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
444                                        ix, 1,
445                                        cairo_image_surface_get_stride(output->gl.border.left) / 4,
446                                        cairo_image_surface_get_data(output->gl.border.left));
447
448
449         if (!output->gl.border.right)
450                 output->gl.border.right =
451                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
452                                                    fwidth - (ix + iwidth), 1);
453         cr = cairo_create(output->gl.border.right);
454         cairo_translate(cr, -(iwidth + ix), -iy);
455         frame_repaint(output->frame, cr);
456         cairo_destroy(cr);
457         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
458                                        fwidth - (ix + iwidth), 1,
459                                        cairo_image_surface_get_stride(output->gl.border.right) / 4,
460                                        cairo_image_surface_get_data(output->gl.border.right));
461
462
463         if (!output->gl.border.bottom)
464                 output->gl.border.bottom =
465                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
466                                                    fwidth, fheight - (iy + iheight));
467         cr = cairo_create(output->gl.border.bottom);
468         cairo_translate(cr, 0, -(iy + iheight));
469         frame_repaint(output->frame, cr);
470         cairo_destroy(cr);
471         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
472                                        fwidth, fheight - (iy + iheight),
473                                        cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
474                                        cairo_image_surface_get_data(output->gl.border.bottom));
475 }
476 #endif
477
478 static void
479 wayland_output_start_repaint_loop(struct weston_output *output_base)
480 {
481         struct wayland_output *output = to_wayland_output(output_base);
482         struct wayland_backend *wb =
483                 to_wayland_backend(output->base.compositor);
484
485         /* If this is the initial frame, we need to attach a buffer so that
486          * the compositor can map the surface and include it in its render
487          * loop. If the surface doesn't end up in the render loop, the frame
488          * callback won't be invoked. The buffer is transparent and of the
489          * same size as the future real output buffer. */
490         if (output->parent.draw_initial_frame) {
491                 output->parent.draw_initial_frame = false;
492
493                 draw_initial_frame(output);
494         }
495
496         output->frame_cb = wl_surface_frame(output->parent.surface);
497         wl_callback_add_listener(output->frame_cb, &frame_listener, output);
498         wl_surface_commit(output->parent.surface);
499         wl_display_flush(wb->parent.wl_display);
500 }
501
502 #ifdef ENABLE_EGL
503 static int
504 wayland_output_repaint_gl(struct weston_output *output_base,
505                           pixman_region32_t *damage,
506                           void *repaint_data)
507 {
508         struct wayland_output *output = to_wayland_output(output_base);
509         struct weston_compositor *ec = output->base.compositor;
510
511         output->frame_cb = wl_surface_frame(output->parent.surface);
512         wl_callback_add_listener(output->frame_cb, &frame_listener, output);
513
514         wayland_output_update_gl_border(output);
515
516         ec->renderer->repaint_output(&output->base, damage);
517
518         pixman_region32_subtract(&ec->primary_plane.damage,
519                                  &ec->primary_plane.damage, damage);
520         return 0;
521 }
522 #endif
523
524 static void
525 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
526 {
527         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
528         cairo_t *cr;
529
530         if (!buffer->output->frame || !buffer->frame_damaged)
531                 return;
532
533         cr = cairo_create(buffer->c_surface);
534
535         frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
536         fwidth = frame_width(buffer->output->frame);
537         fheight = frame_height(buffer->output->frame);
538
539         /* Set the clip so we don't unnecisaraly damage the surface */
540         cairo_move_to(cr, ix, iy);
541         cairo_rel_line_to(cr, iwidth, 0);
542         cairo_rel_line_to(cr, 0, iheight);
543         cairo_rel_line_to(cr, -iwidth, 0);
544         cairo_line_to(cr, ix, iy);
545         cairo_line_to(cr, 0, iy);
546         cairo_line_to(cr, 0, fheight);
547         cairo_line_to(cr, fwidth, fheight);
548         cairo_line_to(cr, fwidth, 0);
549         cairo_line_to(cr, 0, 0);
550         cairo_line_to(cr, 0, iy);
551         cairo_close_path(cr);
552         cairo_clip(cr);
553
554         /* Draw using a pattern so that the final result gets clipped */
555         cairo_push_group(cr);
556         frame_repaint(buffer->output->frame, cr);
557         cairo_pop_group_to_source(cr);
558         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
559         cairo_paint(cr);
560
561         cairo_destroy(cr);
562 }
563
564 static void
565 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
566 {
567         pixman_region32_t damage;
568         pixman_box32_t *rects;
569         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
570         int i, n;
571
572         pixman_region32_init(&damage);
573         pixman_region32_copy(&damage, &sb->damage);
574         pixman_region32_translate(&damage, -sb->output->base.x,
575                                   -sb->output->base.y);
576
577         weston_transformed_region(sb->output->base.width,
578                                   sb->output->base.height,
579                                   sb->output->base.transform,
580                                   sb->output->base.current_scale,
581                                   &damage, &damage);
582
583         if (sb->output->frame) {
584                 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
585                 fwidth = frame_width(sb->output->frame);
586                 fheight = frame_height(sb->output->frame);
587
588                 pixman_region32_translate(&damage, ix, iy);
589
590                 if (sb->frame_damaged) {
591                         pixman_region32_union_rect(&damage, &damage,
592                                                    0, 0, fwidth, iy);
593                         pixman_region32_union_rect(&damage, &damage,
594                                                    0, iy, ix, iheight);
595                         pixman_region32_union_rect(&damage, &damage,
596                                                    ix + iwidth, iy,
597                                                    fwidth - (ix + iwidth), iheight);
598                         pixman_region32_union_rect(&damage, &damage,
599                                                    0, iy + iheight,
600                                                    fwidth, fheight - (iy + iheight));
601                 }
602         }
603
604         rects = pixman_region32_rectangles(&damage, &n);
605         wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
606         for (i = 0; i < n; ++i)
607                 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
608                                   rects[i].y1, rects[i].x2 - rects[i].x1,
609                                   rects[i].y2 - rects[i].y1);
610
611         if (sb->output->frame)
612                 pixman_region32_fini(&damage);
613 }
614
615 static int
616 wayland_output_repaint_pixman(struct weston_output *output_base,
617                               pixman_region32_t *damage,
618                               void *repaint_data)
619 {
620         struct wayland_output *output = to_wayland_output(output_base);
621         struct wayland_backend *b =
622                 to_wayland_backend(output->base.compositor);
623         struct wayland_shm_buffer *sb;
624
625         if (output->frame) {
626                 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
627                         wl_list_for_each(sb, &output->shm.buffers, link)
628                                 sb->frame_damaged = 1;
629         }
630
631         wl_list_for_each(sb, &output->shm.buffers, link)
632                 pixman_region32_union(&sb->damage, &sb->damage, damage);
633
634         sb = wayland_output_get_shm_buffer(output);
635
636         wayland_output_update_shm_border(sb);
637         pixman_renderer_output_set_buffer(output_base, sb->pm_image);
638         b->compositor->renderer->repaint_output(output_base, &sb->damage);
639
640         wayland_shm_buffer_attach(sb);
641
642         output->frame_cb = wl_surface_frame(output->parent.surface);
643         wl_callback_add_listener(output->frame_cb, &frame_listener, output);
644         wl_surface_commit(output->parent.surface);
645         wl_display_flush(b->parent.wl_display);
646
647         pixman_region32_fini(&sb->damage);
648         pixman_region32_init(&sb->damage);
649         sb->frame_damaged = 0;
650
651         pixman_region32_subtract(&b->compositor->primary_plane.damage,
652                                  &b->compositor->primary_plane.damage, damage);
653         return 0;
654 }
655
656 static void
657 wayland_backend_destroy_output_surface(struct wayland_output *output)
658 {
659         assert(output->parent.surface);
660
661         if (output->parent.xdg_toplevel) {
662                 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
663                 output->parent.xdg_toplevel = NULL;
664         }
665
666         if (output->parent.xdg_surface) {
667                 zxdg_surface_v6_destroy(output->parent.xdg_surface);
668                 output->parent.xdg_surface = NULL;
669         }
670
671         if (output->parent.shell_surface) {
672                 wl_shell_surface_destroy(output->parent.shell_surface);
673                 output->parent.shell_surface = NULL;
674         }
675
676         wl_surface_destroy(output->parent.surface);
677         output->parent.surface = NULL;
678 }
679
680 static void
681 wayland_output_destroy_shm_buffers(struct wayland_output *output)
682 {
683         struct wayland_shm_buffer *buffer, *next;
684
685         /* Throw away any remaining SHM buffers */
686         wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
687                 wayland_shm_buffer_destroy(buffer);
688         /* These will get thrown away when they get released */
689         wl_list_for_each(buffer, &output->shm.buffers, link)
690                 buffer->output = NULL;
691 }
692
693 static int
694 wayland_output_disable(struct weston_output *base)
695 {
696         struct wayland_output *output = to_wayland_output(base);
697         struct wayland_backend *b = to_wayland_backend(base->compositor);
698
699         if (!output->base.enabled)
700                 return 0;
701
702         if (b->use_pixman) {
703                 pixman_renderer_output_destroy(&output->base);
704 #ifdef ENABLE_EGL
705         } else {
706                 gl_renderer->output_destroy(&output->base);
707                 wl_egl_window_destroy(output->gl.egl_window);
708 #endif
709         }
710
711         wayland_output_destroy_shm_buffers(output);
712
713         wayland_backend_destroy_output_surface(output);
714
715         if (output->frame)
716                 frame_destroy(output->frame);
717
718         cairo_surface_destroy(output->gl.border.top);
719         cairo_surface_destroy(output->gl.border.left);
720         cairo_surface_destroy(output->gl.border.right);
721         cairo_surface_destroy(output->gl.border.bottom);
722
723         return 0;
724 }
725
726 static void
727 wayland_output_destroy(struct weston_output *base)
728 {
729         struct wayland_output *output = to_wayland_output(base);
730
731         wayland_output_disable(&output->base);
732
733         weston_output_release(&output->base);
734
735         if (output->frame_cb)
736                 wl_callback_destroy(output->frame_cb);
737
738         free(output->title);
739         free(output);
740 }
741
742 static const struct wl_shell_surface_listener shell_surface_listener;
743
744 #ifdef ENABLE_EGL
745 static int
746 wayland_output_init_gl_renderer(struct wayland_output *output)
747 {
748         int32_t fwidth = 0, fheight = 0;
749
750         if (output->frame) {
751                 fwidth = frame_width(output->frame);
752                 fheight = frame_height(output->frame);
753         } else {
754                 fwidth = output->base.current_mode->width;
755                 fheight = output->base.current_mode->height;
756         }
757
758         output->gl.egl_window =
759                 wl_egl_window_create(output->parent.surface,
760                                      fwidth, fheight);
761         if (!output->gl.egl_window) {
762                 weston_log("failure to create wl_egl_window\n");
763                 return -1;
764         }
765
766         if (gl_renderer->output_window_create(&output->base,
767                                               output->gl.egl_window,
768                                               output->gl.egl_window,
769                                               gl_renderer->alpha_attribs,
770                                               NULL,
771                                               0) < 0)
772                 goto cleanup_window;
773
774         return 0;
775
776 cleanup_window:
777         wl_egl_window_destroy(output->gl.egl_window);
778         return -1;
779 }
780 #endif
781
782 static int
783 wayland_output_init_pixman_renderer(struct wayland_output *output)
784 {
785         return pixman_renderer_output_create(&output->base,
786                                      PIXMAN_RENDERER_OUTPUT_USE_SHADOW);
787 }
788
789 static void
790 wayland_output_resize_surface(struct wayland_output *output)
791 {
792         struct wayland_backend *b =
793                 to_wayland_backend(output->base.compositor);
794         int32_t ix, iy, iwidth, iheight;
795         int32_t width, height;
796         struct wl_region *region;
797
798         width = output->base.current_mode->width;
799         height = output->base.current_mode->height;
800
801         if (output->frame) {
802                 frame_resize_inside(output->frame, width, height);
803
804                 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
805                 region = wl_compositor_create_region(b->parent.compositor);
806                 wl_region_add(region, ix, iy, iwidth, iheight);
807                 wl_surface_set_input_region(output->parent.surface, region);
808                 wl_region_destroy(region);
809
810                 if (output->parent.xdg_surface) {
811                         zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
812                                                             ix,
813                                                             iy,
814                                                             iwidth,
815                                                             iheight);
816                 }
817
818                 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
819                 region = wl_compositor_create_region(b->parent.compositor);
820                 wl_region_add(region, ix, iy, iwidth, iheight);
821                 wl_surface_set_opaque_region(output->parent.surface, region);
822                 wl_region_destroy(region);
823
824                 width = frame_width(output->frame);
825                 height = frame_height(output->frame);
826         } else {
827                 region = wl_compositor_create_region(b->parent.compositor);
828                 wl_region_add(region, 0, 0, width, height);
829                 wl_surface_set_input_region(output->parent.surface, region);
830                 wl_region_destroy(region);
831
832                 region = wl_compositor_create_region(b->parent.compositor);
833                 wl_region_add(region, 0, 0, width, height);
834                 wl_surface_set_opaque_region(output->parent.surface, region);
835                 wl_region_destroy(region);
836
837                 if (output->parent.xdg_surface) {
838                         zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
839                                                             0,
840                                                             0,
841                                                             width,
842                                                             height);
843                 }
844         }
845
846 #ifdef ENABLE_EGL
847         if (output->gl.egl_window) {
848                 wl_egl_window_resize(output->gl.egl_window,
849                                      width, height, 0, 0);
850
851                 /* These will need to be re-created due to the resize */
852                 gl_renderer->output_set_border(&output->base,
853                                                GL_RENDERER_BORDER_TOP,
854                                                0, 0, 0, NULL);
855                 cairo_surface_destroy(output->gl.border.top);
856                 output->gl.border.top = NULL;
857                 gl_renderer->output_set_border(&output->base,
858                                                GL_RENDERER_BORDER_LEFT,
859                                                0, 0, 0, NULL);
860                 cairo_surface_destroy(output->gl.border.left);
861                 output->gl.border.left = NULL;
862                 gl_renderer->output_set_border(&output->base,
863                                                GL_RENDERER_BORDER_RIGHT,
864                                                0, 0, 0, NULL);
865                 cairo_surface_destroy(output->gl.border.right);
866                 output->gl.border.right = NULL;
867                 gl_renderer->output_set_border(&output->base,
868                                                GL_RENDERER_BORDER_BOTTOM,
869                                                0, 0, 0, NULL);
870                 cairo_surface_destroy(output->gl.border.bottom);
871                 output->gl.border.bottom = NULL;
872         }
873 #endif
874
875         wayland_output_destroy_shm_buffers(output);
876 }
877
878 static int
879 wayland_output_set_windowed(struct wayland_output *output)
880 {
881         struct wayland_backend *b =
882                 to_wayland_backend(output->base.compositor);
883
884         if (output->frame)
885                 return 0;
886
887         if (!b->theme) {
888                 b->theme = theme_create();
889                 if (!b->theme)
890                         return -1;
891         }
892         output->frame = frame_create(b->theme, 100, 100,
893                                      FRAME_BUTTON_CLOSE, output->title, NULL);
894         if (!output->frame)
895                 return -1;
896
897         if (output->keyboard_count)
898                 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
899
900         wayland_output_resize_surface(output);
901
902         if (output->parent.xdg_toplevel) {
903                 zxdg_toplevel_v6_unset_fullscreen(output->parent.xdg_toplevel);
904         } else if (output->parent.shell_surface) {
905                 wl_shell_surface_set_toplevel(output->parent.shell_surface);
906         } else {
907                 abort();
908         }
909
910         return 0;
911 }
912
913 static void
914 wayland_output_set_fullscreen(struct wayland_output *output,
915                               enum wl_shell_surface_fullscreen_method method,
916                               uint32_t framerate, struct wl_output *target)
917 {
918         if (output->frame) {
919                 frame_destroy(output->frame);
920                 output->frame = NULL;
921         }
922
923         wayland_output_resize_surface(output);
924
925         if (output->parent.xdg_toplevel) {
926                 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
927         } else if (output->parent.shell_surface) {
928                 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
929                                                 method, framerate, target);
930         } else {
931                 abort();
932         }
933 }
934
935 static struct weston_mode *
936 wayland_output_choose_mode(struct wayland_output *output,
937                            struct weston_mode *ref_mode)
938 {
939         struct weston_mode *mode;
940
941         /* First look for an exact match */
942         wl_list_for_each(mode, &output->base.mode_list, link)
943                 if (mode->width == ref_mode->width &&
944                     mode->height == ref_mode->height &&
945                     mode->refresh == ref_mode->refresh)
946                         return mode;
947
948         /* If we can't find an exact match, ignore refresh and try again */
949         wl_list_for_each(mode, &output->base.mode_list, link)
950                 if (mode->width == ref_mode->width &&
951                     mode->height == ref_mode->height)
952                         return mode;
953
954         /* Yeah, we failed */
955         return NULL;
956 }
957
958 enum mode_status {
959         MODE_STATUS_UNKNOWN,
960         MODE_STATUS_SUCCESS,
961         MODE_STATUS_FAIL,
962         MODE_STATUS_CANCEL,
963 };
964
965 static void
966 mode_feedback_successful(void *data,
967                          struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
968 {
969         enum mode_status *value = data;
970
971         printf("Mode switch successful\n");
972
973         *value = MODE_STATUS_SUCCESS;
974 }
975
976 static void
977 mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
978 {
979         enum mode_status *value = data;
980
981         printf("Mode switch failed\n");
982
983         *value = MODE_STATUS_FAIL;
984 }
985
986 static void
987 mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
988 {
989         enum mode_status *value = data;
990
991         printf("Mode switch cancelled\n");
992
993         *value = MODE_STATUS_CANCEL;
994 }
995
996 struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
997         mode_feedback_successful,
998         mode_feedback_failed,
999         mode_feedback_cancelled,
1000 };
1001
1002 static enum mode_status
1003 wayland_output_fullscreen_shell_mode_feedback(struct wayland_output *output,
1004                                               struct weston_mode *mode)
1005 {
1006         struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1007         struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
1008         enum mode_status mode_status;
1009         int ret = 0;
1010
1011         mode_feedback =
1012                 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1013                                                                  output->parent.surface,
1014                                                                  output->parent.output,
1015                                                                  mode->refresh);
1016
1017         zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1018                                                            &mode_feedback_listener,
1019                                                            &mode_status);
1020
1021         output->parent.draw_initial_frame = false;
1022         draw_initial_frame(output);
1023         wl_surface_commit(output->parent.surface);
1024
1025         mode_status = MODE_STATUS_UNKNOWN;
1026         while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
1027                 ret = wl_display_dispatch(b->parent.wl_display);
1028
1029         zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
1030
1031         return mode_status;
1032 }
1033
1034 static int
1035 wayland_output_switch_mode(struct weston_output *output_base,
1036                            struct weston_mode *mode)
1037 {
1038         struct wayland_output *output = to_wayland_output(output_base);
1039         struct wayland_backend *b;
1040         struct wl_surface *old_surface;
1041         struct weston_mode *old_mode;
1042         enum mode_status mode_status;
1043
1044         if (output_base == NULL) {
1045                 weston_log("output is NULL.\n");
1046                 return -1;
1047         }
1048
1049         if (mode == NULL) {
1050                 weston_log("mode is NULL.\n");
1051                 return -1;
1052         }
1053
1054         b = to_wayland_backend(output_base->compositor);
1055
1056         if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
1057                 return -1;
1058
1059         mode = wayland_output_choose_mode(output, mode);
1060         if (mode == NULL)
1061                 return -1;
1062
1063         if (output->base.current_mode == mode)
1064                 return 0;
1065
1066         old_mode = output->base.current_mode;
1067         old_surface = output->parent.surface;
1068         output->base.current_mode = mode;
1069         output->parent.surface =
1070                 wl_compositor_create_surface(b->parent.compositor);
1071         wl_surface_set_user_data(output->parent.surface, output);
1072
1073         /* Blow the old buffers because we changed size/surfaces */
1074         wayland_output_resize_surface(output);
1075
1076         mode_status = wayland_output_fullscreen_shell_mode_feedback(output, mode);
1077
1078         /* This should kick-start things again */
1079         wayland_output_start_repaint_loop(&output->base);
1080
1081         if (mode_status == MODE_STATUS_FAIL) {
1082                 output->base.current_mode = old_mode;
1083                 wl_surface_destroy(output->parent.surface);
1084                 output->parent.surface = old_surface;
1085                 wayland_output_resize_surface(output);
1086
1087                 return -1;
1088         }
1089
1090         old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1091         output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1092
1093         if (b->use_pixman) {
1094                 pixman_renderer_output_destroy(output_base);
1095                 if (wayland_output_init_pixman_renderer(output) < 0)
1096                         goto err_output;
1097 #ifdef ENABLE_EGL
1098         } else {
1099                 gl_renderer->output_destroy(output_base);
1100                 wl_egl_window_destroy(output->gl.egl_window);
1101                 if (wayland_output_init_gl_renderer(output) < 0)
1102                         goto err_output;
1103 #endif
1104         }
1105         wl_surface_destroy(old_surface);
1106
1107         weston_output_schedule_repaint(&output->base);
1108
1109         return 0;
1110
1111 err_output:
1112         /* XXX */
1113         return -1;
1114 }
1115
1116 static void
1117 handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1118                          uint32_t serial)
1119 {
1120         zxdg_surface_v6_ack_configure(surface, serial);
1121 }
1122
1123 static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1124         handle_xdg_surface_configure
1125 };
1126
1127 static void
1128 handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1129                           int32_t width, int32_t height,
1130                           struct wl_array *states)
1131 {
1132         struct wayland_output *output = data;
1133
1134         output->parent.configure_width = width;
1135         output->parent.configure_height = height;
1136
1137         output->parent.wait_for_configure = false;
1138         /* FIXME: implement resizing */
1139 }
1140
1141 static void
1142 handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1143 {
1144         struct wayland_output *output = data;
1145         struct weston_compositor *compositor = output->base.compositor;
1146
1147         wayland_output_destroy(&output->base);
1148
1149         if (wl_list_empty(&compositor->output_list))
1150                 weston_compositor_exit(compositor);
1151 }
1152
1153 static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1154         handle_xdg_toplevel_configure,
1155         handle_xdg_toplevel_close,
1156 };
1157
1158 static int
1159 wayland_backend_create_output_surface(struct wayland_output *output)
1160 {
1161         struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1162
1163         assert(!output->parent.surface);
1164
1165         output->parent.surface =
1166                 wl_compositor_create_surface(b->parent.compositor);
1167         if (!output->parent.surface)
1168                 return -1;
1169
1170         wl_surface_set_user_data(output->parent.surface, output);
1171
1172         output->parent.draw_initial_frame = true;
1173
1174         if (b->parent.xdg_shell) {
1175                 output->parent.xdg_surface =
1176                         zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1177                                                       output->parent.surface);
1178                 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1179                                              &xdg_surface_listener, output);
1180
1181                 output->parent.xdg_toplevel =
1182                         zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1183                 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1184                                               &xdg_toplevel_listener, output);
1185
1186                 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, output->title);
1187
1188                 wl_surface_commit(output->parent.surface);
1189
1190                 output->parent.wait_for_configure = true;
1191
1192                 while (output->parent.wait_for_configure)
1193                         wl_display_dispatch(b->parent.wl_display);
1194
1195                 weston_log("wayland-backend: Using xdg_shell_v6\n");
1196         }
1197         else if (b->parent.shell) {
1198                 output->parent.shell_surface =
1199                         wl_shell_get_shell_surface(b->parent.shell,
1200                                                    output->parent.surface);
1201                 if (!output->parent.shell_surface) {
1202                         wl_surface_destroy(output->parent.surface);
1203                         return -1;
1204                 }
1205
1206                 wl_shell_surface_add_listener(output->parent.shell_surface,
1207                                               &shell_surface_listener, output);
1208
1209                 weston_log("wayland-backend: Using wl_shell\n");
1210         }
1211
1212         return 0;
1213 }
1214
1215 static int
1216 wayland_output_enable(struct weston_output *base)
1217 {
1218         struct wayland_output *output = to_wayland_output(base);
1219         struct wayland_backend *b = to_wayland_backend(base->compositor);
1220         enum mode_status mode_status;
1221         int ret = 0;
1222
1223         weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1224                    output->base.current_mode->width,
1225                    output->base.current_mode->height,
1226                    output->base.x, output->base.y);
1227
1228         if (!output->parent.surface)
1229                 ret = wayland_backend_create_output_surface(output);
1230
1231         if (ret < 0)
1232                 return -1;
1233
1234         wl_list_init(&output->shm.buffers);
1235         wl_list_init(&output->shm.free_buffers);
1236
1237         if (b->use_pixman) {
1238                 if (wayland_output_init_pixman_renderer(output) < 0)
1239                         goto err_output;
1240
1241                 output->base.repaint = wayland_output_repaint_pixman;
1242 #ifdef ENABLE_EGL
1243         } else {
1244                 if (wayland_output_init_gl_renderer(output) < 0)
1245                         goto err_output;
1246
1247                 output->base.repaint = wayland_output_repaint_gl;
1248 #endif
1249         }
1250
1251         output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1252         output->base.assign_planes = NULL;
1253         output->base.set_backlight = NULL;
1254         output->base.set_dpms = NULL;
1255         output->base.switch_mode = wayland_output_switch_mode;
1256
1257         if (b->sprawl_across_outputs) {
1258                 if (b->parent.fshell) {
1259                         wayland_output_resize_surface(output);
1260
1261                         mode_status = wayland_output_fullscreen_shell_mode_feedback(output, &output->mode);
1262
1263                         if (mode_status == MODE_STATUS_FAIL) {
1264                                 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1265                                                                         output->parent.surface,
1266                                                                         ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1267                                                                         output->parent.output);
1268
1269                                 output->parent.draw_initial_frame = true;
1270                         }
1271                 } else {
1272                         wayland_output_set_fullscreen(output,
1273                                                       WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1274                                                       output->mode.refresh, output->parent.output);
1275                 }
1276         } else if (b->fullscreen) {
1277                 wayland_output_set_fullscreen(output, 0, 0, NULL);
1278         } else {
1279                 wayland_output_set_windowed(output);
1280         }
1281
1282         return 0;
1283
1284 err_output:
1285         wayland_backend_destroy_output_surface(output);
1286
1287         return -1;
1288 }
1289
1290 static int
1291 wayland_output_setup_for_parent_output(struct wayland_output *output,
1292                                        struct wayland_parent_output *poutput);
1293
1294 static int
1295 wayland_output_setup_fullscreen(struct wayland_output *output,
1296                                 struct wayland_head *head);
1297
1298 static int
1299 wayland_output_attach_head(struct weston_output *output_base,
1300                            struct weston_head *head_base)
1301 {
1302         struct wayland_backend *b = to_wayland_backend(output_base->compositor);
1303         struct wayland_output *output = to_wayland_output(output_base);
1304         struct wayland_head *head = to_wayland_head(head_base);
1305
1306         if (!wl_list_empty(&output->base.head_list))
1307                 return -1;
1308
1309         if (head->parent_output) {
1310                 if (wayland_output_setup_for_parent_output(output,
1311                                                            head->parent_output) < 0)
1312                         return -1;
1313         } else if (b->fullscreen) {
1314                 if (wayland_output_setup_fullscreen(output, head) < 0)
1315                         return -1;
1316         } else {
1317                 /* A floating window, nothing to do. */
1318         }
1319
1320         return 0;
1321 }
1322
1323 static void
1324 wayland_output_detach_head(struct weston_output *output_base,
1325                            struct weston_head *head)
1326 {
1327         struct wayland_output *output = to_wayland_output(output_base);
1328
1329         /* Rely on the disable hook if the output was enabled. We do not
1330          * support cloned heads, so detaching is guaranteed to disable the
1331          * output.
1332          */
1333         if (output->base.enabled)
1334                 return;
1335
1336         /* undo setup fullscreen */
1337         if (output->parent.surface)
1338                 wayland_backend_destroy_output_surface(output);
1339 }
1340
1341 static struct weston_output *
1342 wayland_output_create(struct weston_compositor *compositor, const char *name)
1343 {
1344         struct wayland_output *output;
1345         char *title;
1346
1347         /* name can't be NULL. */
1348         assert(name);
1349
1350         output = zalloc(sizeof *output);
1351         if (output == NULL) {
1352                 perror("zalloc");
1353                 return NULL;
1354         }
1355
1356         if (asprintf(&title, "%s - %s", WINDOW_TITLE, name) < 0) {
1357                 free(output);
1358                 return NULL;
1359         }
1360         output->title = title;
1361
1362         weston_output_init(&output->base, compositor, name);
1363
1364         output->base.destroy = wayland_output_destroy;
1365         output->base.disable = wayland_output_disable;
1366         output->base.enable = wayland_output_enable;
1367         output->base.attach_head = wayland_output_attach_head;
1368         output->base.detach_head = wayland_output_detach_head;
1369
1370         weston_compositor_add_pending_output(&output->base, compositor);
1371
1372         return &output->base;
1373 }
1374
1375 static struct wayland_head *
1376 wayland_head_create(struct weston_compositor *compositor, const char *name)
1377 {
1378         struct wayland_head *head;
1379
1380         assert(name);
1381
1382         head = zalloc(sizeof *head);
1383         if (!head)
1384                 return NULL;
1385
1386         weston_head_init(&head->base, name);
1387         weston_head_set_connection_status(&head->base, true);
1388         weston_compositor_add_head(compositor, &head->base);
1389
1390         return head;
1391 }
1392
1393 static int
1394 wayland_head_create_windowed(struct weston_compositor *compositor,
1395                              const char *name)
1396 {
1397          if (!wayland_head_create(compositor, name))
1398                 return -1;
1399
1400         return 0;
1401 }
1402
1403 static int
1404 wayland_head_create_for_parent_output(struct weston_compositor *compositor,
1405                                       struct wayland_parent_output *poutput)
1406 {
1407         struct wayland_head *head;
1408         char name[100];
1409         int ret;
1410
1411         ret = snprintf(name, sizeof(name), "wlparent-%d", poutput->id);
1412         if (ret < 1 || (unsigned)ret >= sizeof(name))
1413                 return -1;
1414
1415         head = wayland_head_create(compositor, name);
1416         if (!head)
1417                 return -1;
1418
1419         assert(!poutput->head);
1420         head->parent_output = poutput;
1421         poutput->head = head;
1422
1423         weston_head_set_monitor_strings(&head->base,
1424                                         poutput->physical.make,
1425                                         poutput->physical.model, NULL);
1426         weston_head_set_physical_size(&head->base,
1427                                       poutput->physical.width,
1428                                       poutput->physical.height);
1429
1430         return 0;
1431 }
1432
1433 static void
1434 wayland_head_destroy(struct wayland_head *head)
1435 {
1436         if (head->parent_output)
1437                 head->parent_output->head = NULL;
1438
1439         weston_head_release(&head->base);
1440         free(head);
1441 }
1442
1443 static int
1444 wayland_output_set_size(struct weston_output *base, int width, int height)
1445 {
1446         struct wayland_output *output = to_wayland_output(base);
1447         struct weston_head *head;
1448         int output_width, output_height;
1449
1450         /* We can only be called once. */
1451         assert(!output->base.current_mode);
1452
1453         /* Make sure we have scale set. */
1454         assert(output->base.scale);
1455
1456         if (width < 1) {
1457                 weston_log("Invalid width \"%d\" for output %s\n",
1458                            width, output->base.name);
1459                 return -1;
1460         }
1461
1462         if (height < 1) {
1463                 weston_log("Invalid height \"%d\" for output %s\n",
1464                            height, output->base.name);
1465                 return -1;
1466         }
1467
1468         wl_list_for_each(head, &output->base.head_list, output_link) {
1469                 weston_head_set_monitor_strings(head, "wayland", "none", NULL);
1470
1471                 /* XXX: Calculate proper size. */
1472                 weston_head_set_physical_size(head, width, height);
1473         }
1474
1475         output_width = width * output->base.scale;
1476         output_height = height * output->base.scale;
1477
1478         output->mode.flags =
1479                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1480
1481         output->mode.width = output_width;
1482         output->mode.height = output_height;
1483         output->mode.refresh = 60000;
1484         wl_list_insert(&output->base.mode_list, &output->mode.link);
1485
1486         output->base.current_mode = &output->mode;
1487
1488         return 0;
1489 }
1490
1491 static int
1492 wayland_output_setup_for_parent_output(struct wayland_output *output,
1493                                        struct wayland_parent_output *poutput)
1494 {
1495         struct weston_mode *mode;
1496
1497         if (poutput->current_mode) {
1498                 mode = poutput->current_mode;
1499         } else if (poutput->preferred_mode) {
1500                 mode = poutput->preferred_mode;
1501         } else if (!wl_list_empty(&poutput->mode_list)) {
1502                 mode = container_of(poutput->mode_list.next,
1503                                     struct weston_mode, link);
1504         } else {
1505                 weston_log("No valid modes found. Skipping output.\n");
1506                 return -1;
1507         }
1508
1509         output->base.scale = 1;
1510         output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1511
1512         output->parent.output = poutput->global;
1513
1514         wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1515         wl_list_init(&poutput->mode_list);
1516
1517         /* No other mode should have CURRENT already. */
1518         mode->flags |= WL_OUTPUT_MODE_CURRENT;
1519         output->base.current_mode = mode;
1520
1521         /* output->mode is unused in this path. */
1522
1523         return 0;
1524 }
1525
1526 static int
1527 wayland_output_setup_fullscreen(struct wayland_output *output,
1528                                 struct wayland_head *head)
1529 {
1530         struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1531         int width = 0, height = 0;
1532
1533         output->base.scale = 1;
1534         output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1535
1536         if (wayland_backend_create_output_surface(output) < 0)
1537                 return -1;
1538
1539         /* What should size be set if conditional is false? */
1540         if (b->parent.xdg_shell || b->parent.shell) {
1541                 if (output->parent.xdg_toplevel)
1542                         zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1543                                                         output->parent.output);
1544                 else if (output->parent.shell_surface)
1545                         wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1546                                                         0, 0, NULL);
1547
1548                 wl_display_roundtrip(b->parent.wl_display);
1549
1550                 width = output->parent.configure_width;
1551                 height = output->parent.configure_height;
1552         }
1553
1554         if (wayland_output_set_size(&output->base, width, height) < 0)
1555                 goto err_set_size;
1556
1557         /* The head is not attached yet, so set_size did not set these. */
1558         weston_head_set_monitor_strings(&head->base, "wayland", "none", NULL);
1559         /* XXX: Calculate proper size. */
1560         weston_head_set_physical_size(&head->base, width, height);
1561
1562         return 0;
1563
1564 err_set_size:
1565         wayland_backend_destroy_output_surface(output);
1566
1567         return -1;
1568 }
1569
1570 static void
1571 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1572                    uint32_t serial)
1573 {
1574         wl_shell_surface_pong(shell_surface, serial);
1575 }
1576
1577 static void
1578 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1579                         uint32_t edges, int32_t width, int32_t height)
1580 {
1581         struct wayland_output *output = data;
1582
1583         output->parent.configure_width = width;
1584         output->parent.configure_height = height;
1585
1586         /* FIXME: implement resizing */
1587 }
1588
1589 static void
1590 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1591 {
1592 }
1593
1594 static const struct wl_shell_surface_listener shell_surface_listener = {
1595         shell_surface_ping,
1596         shell_surface_configure,
1597         shell_surface_popup_done
1598 };
1599
1600 /* Events received from the wayland-server this compositor is client of: */
1601
1602 /* parent input interface */
1603 static void
1604 input_set_cursor(struct wayland_input *input)
1605 {
1606
1607         struct wl_buffer *buffer;
1608         struct wl_cursor_image *image;
1609
1610         if (!input->backend->cursor)
1611                 return; /* Couldn't load the cursor. Can't set it */
1612
1613         image = input->backend->cursor->images[0];
1614         buffer = wl_cursor_image_get_buffer(image);
1615         if (!buffer)
1616                 return;
1617
1618         wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1619                               input->parent.cursor.surface,
1620                               image->hotspot_x, image->hotspot_y);
1621
1622         wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1623         wl_surface_damage(input->parent.cursor.surface, 0, 0,
1624                           image->width, image->height);
1625         wl_surface_commit(input->parent.cursor.surface);
1626 }
1627
1628 static void
1629 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1630                            uint32_t serial, struct wl_surface *surface,
1631                            wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1632 {
1633         struct wayland_input *input = data;
1634         int32_t fx, fy;
1635         enum theme_location location;
1636         double x, y;
1637
1638         if (!surface) {
1639                 input->output = NULL;
1640                 input->has_focus = false;
1641                 notify_pointer_focus(&input->base, NULL, 0, 0);
1642                 return;
1643         }
1644
1645         x = wl_fixed_to_double(fixed_x);
1646         y = wl_fixed_to_double(fixed_y);
1647
1648         /* XXX: If we get a modifier event immediately before the focus,
1649          *      we should try to keep the same serial. */
1650         input->enter_serial = serial;
1651         input->output = wl_surface_get_user_data(surface);
1652
1653         if (input->output->frame) {
1654                 location = frame_pointer_enter(input->output->frame, input,
1655                                                x, y);
1656                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1657                 x -= fx;
1658                 y -= fy;
1659
1660                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1661                         weston_output_schedule_repaint(&input->output->base);
1662         } else {
1663                 location = THEME_LOCATION_CLIENT_AREA;
1664         }
1665
1666         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1667
1668         if (location == THEME_LOCATION_CLIENT_AREA) {
1669                 input->has_focus = true;
1670                 notify_pointer_focus(&input->base, &input->output->base, x, y);
1671                 wl_pointer_set_cursor(input->parent.pointer,
1672                                       input->enter_serial, NULL, 0, 0);
1673         } else {
1674                 input->has_focus = false;
1675                 notify_pointer_focus(&input->base, NULL, 0, 0);
1676                 input_set_cursor(input);
1677         }
1678 }
1679
1680 static void
1681 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1682                            uint32_t serial, struct wl_surface *surface)
1683 {
1684         struct wayland_input *input = data;
1685
1686         if (!input->output)
1687                 return;
1688
1689         if (input->output->frame) {
1690                 frame_pointer_leave(input->output->frame, input);
1691
1692                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1693                         weston_output_schedule_repaint(&input->output->base);
1694         }
1695
1696         notify_pointer_focus(&input->base, NULL, 0, 0);
1697         input->output = NULL;
1698         input->has_focus = false;
1699 }
1700
1701 static void
1702 input_handle_motion(void *data, struct wl_pointer *pointer,
1703                     uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1704 {
1705         struct wayland_input *input = data;
1706         int32_t fx, fy;
1707         enum theme_location location;
1708         bool want_frame = false;
1709         double x, y;
1710         struct timespec ts;
1711
1712         if (!input->output)
1713                 return;
1714
1715         x = wl_fixed_to_double(fixed_x);
1716         y = wl_fixed_to_double(fixed_y);
1717
1718         if (input->output->frame) {
1719                 location = frame_pointer_motion(input->output->frame, input,
1720                                                 x, y);
1721                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1722                 x -= fx;
1723                 y -= fy;
1724
1725                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1726                         weston_output_schedule_repaint(&input->output->base);
1727         } else {
1728                 location = THEME_LOCATION_CLIENT_AREA;
1729         }
1730
1731         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1732
1733         if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
1734                 input_set_cursor(input);
1735                 notify_pointer_focus(&input->base, NULL, 0, 0);
1736                 input->has_focus = false;
1737                 want_frame = true;
1738         } else if (!input->has_focus &&
1739                    location == THEME_LOCATION_CLIENT_AREA) {
1740                 wl_pointer_set_cursor(input->parent.pointer,
1741                                       input->enter_serial, NULL, 0, 0);
1742                 notify_pointer_focus(&input->base, &input->output->base, x, y);
1743                 input->has_focus = true;
1744                 want_frame = true;
1745         }
1746
1747         if (location == THEME_LOCATION_CLIENT_AREA) {
1748                 timespec_from_msec(&ts, time);
1749                 notify_motion_absolute(&input->base, &ts, x, y);
1750                 want_frame = true;
1751         }
1752
1753         if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1754                 notify_pointer_frame(&input->base);
1755 }
1756
1757 static void
1758 input_handle_button(void *data, struct wl_pointer *pointer,
1759                     uint32_t serial, uint32_t time, uint32_t button,
1760                     enum wl_pointer_button_state state)
1761 {
1762         struct wayland_input *input = data;
1763         enum theme_location location;
1764         struct timespec ts;
1765
1766         if (!input->output)
1767                 return;
1768
1769         if (input->output->frame) {
1770                 location = frame_pointer_button(input->output->frame, input,
1771                                                 button, state);
1772
1773                 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1774                         if (input->output->parent.xdg_toplevel)
1775                                 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
1776                                               input->parent.seat, serial);
1777                         else if (input->output->parent.shell_surface)
1778                                 wl_shell_surface_move(input->output->parent.shell_surface,
1779                                                       input->parent.seat, serial);
1780                         frame_status_clear(input->output->frame,
1781                                            FRAME_STATUS_MOVE);
1782                         return;
1783                 }
1784
1785                 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1786                         wayland_output_destroy(&input->output->base);
1787                         input->output = NULL;
1788                         input->keyboard_focus = NULL;
1789
1790                         if (wl_list_empty(&input->backend->compositor->output_list))
1791                                 weston_compositor_exit(input->backend->compositor);
1792
1793                         return;
1794                 }
1795
1796                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1797                         weston_output_schedule_repaint(&input->output->base);
1798         } else {
1799                 location = THEME_LOCATION_CLIENT_AREA;
1800         }
1801
1802         if (location == THEME_LOCATION_CLIENT_AREA) {
1803                 timespec_from_msec(&ts, time);
1804                 notify_button(&input->base, &ts, button, state);
1805                 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1806                         notify_pointer_frame(&input->base);
1807         }
1808 }
1809
1810 static void
1811 input_handle_axis(void *data, struct wl_pointer *pointer,
1812                   uint32_t time, uint32_t axis, wl_fixed_t value)
1813 {
1814         struct wayland_input *input = data;
1815         struct weston_pointer_axis_event weston_event;
1816         struct timespec ts;
1817
1818         weston_event.axis = axis;
1819         weston_event.value = wl_fixed_to_double(value);
1820         weston_event.has_discrete = false;
1821
1822         if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1823             input->vert.has_discrete) {
1824                 weston_event.has_discrete = true;
1825                 weston_event.discrete = input->vert.discrete;
1826                 input->vert.has_discrete = false;
1827         } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1828                    input->horiz.has_discrete) {
1829                 weston_event.has_discrete = true;
1830                 weston_event.discrete = input->horiz.discrete;
1831                 input->horiz.has_discrete = false;
1832         }
1833
1834         timespec_from_msec(&ts, time);
1835
1836         notify_axis(&input->base, &ts, &weston_event);
1837
1838         if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1839                 notify_pointer_frame(&input->base);
1840 }
1841
1842 static void
1843 input_handle_frame(void *data, struct wl_pointer *pointer)
1844 {
1845         struct wayland_input *input = data;
1846
1847         notify_pointer_frame(&input->base);
1848 }
1849
1850 static void
1851 input_handle_axis_source(void *data, struct wl_pointer *pointer,
1852                          uint32_t source)
1853 {
1854         struct wayland_input *input = data;
1855
1856         notify_axis_source(&input->base, source);
1857 }
1858
1859 static void
1860 input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1861                        uint32_t time, uint32_t axis)
1862 {
1863         struct wayland_input *input = data;
1864         struct weston_pointer_axis_event weston_event;
1865         struct timespec ts;
1866
1867         weston_event.axis = axis;
1868         weston_event.value = 0;
1869
1870         timespec_from_msec(&ts, time);
1871
1872         notify_axis(&input->base, &ts, &weston_event);
1873 }
1874
1875 static void
1876 input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1877                            uint32_t axis, int32_t discrete)
1878 {
1879         struct wayland_input *input = data;
1880
1881         if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1882                 input->vert.has_discrete = true;
1883                 input->vert.discrete = discrete;
1884         } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1885                 input->horiz.has_discrete = true;
1886                 input->horiz.discrete = discrete;
1887         }
1888 }
1889
1890 static const struct wl_pointer_listener pointer_listener = {
1891         input_handle_pointer_enter,
1892         input_handle_pointer_leave,
1893         input_handle_motion,
1894         input_handle_button,
1895         input_handle_axis,
1896         input_handle_frame,
1897         input_handle_axis_source,
1898         input_handle_axis_stop,
1899         input_handle_axis_discrete,
1900 };
1901
1902 static void
1903 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1904                     int fd, uint32_t size)
1905 {
1906         struct wayland_input *input = data;
1907         struct xkb_keymap *keymap;
1908         char *map_str;
1909
1910         if (!data) {
1911                 close(fd);
1912                 return;
1913         }
1914
1915         if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1916                 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1917                 if (map_str == MAP_FAILED) {
1918                         weston_log("mmap failed: %m\n");
1919                         goto error;
1920                 }
1921
1922                 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
1923                                                     map_str,
1924                                                     XKB_KEYMAP_FORMAT_TEXT_V1,
1925                                                     0);
1926                 munmap(map_str, size);
1927
1928                 if (!keymap) {
1929                         weston_log("failed to compile keymap\n");
1930                         goto error;
1931                 }
1932
1933                 input->keyboard_state_update = STATE_UPDATE_NONE;
1934         } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1935                 weston_log("No keymap provided; falling back to defalt\n");
1936                 keymap = NULL;
1937                 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1938         } else {
1939                 weston_log("Invalid keymap\n");
1940                 goto error;
1941         }
1942
1943         close(fd);
1944
1945         if (weston_seat_get_keyboard(&input->base))
1946                 weston_seat_update_keymap(&input->base, keymap);
1947         else
1948                 weston_seat_init_keyboard(&input->base, keymap);
1949
1950         xkb_keymap_unref(keymap);
1951
1952         return;
1953
1954 error:
1955         wl_keyboard_release(input->parent.keyboard);
1956         close(fd);
1957 }
1958
1959 static void
1960 input_handle_keyboard_enter(void *data,
1961                             struct wl_keyboard *keyboard,
1962                             uint32_t serial,
1963                             struct wl_surface *surface,
1964                             struct wl_array *keys)
1965 {
1966         struct wayland_input *input = data;
1967         struct wayland_output *focus;
1968
1969         focus = input->keyboard_focus;
1970         if (focus) {
1971                 /* This shouldn't happen */
1972                 focus->keyboard_count--;
1973                 if (!focus->keyboard_count && focus->frame)
1974                         frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1975                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1976                         weston_output_schedule_repaint(&focus->base);
1977         }
1978
1979         if (!surface) {
1980                 input->keyboard_focus = NULL;
1981                 return;
1982         }
1983
1984         input->keyboard_focus = wl_surface_get_user_data(surface);
1985         input->keyboard_focus->keyboard_count++;
1986
1987         focus = input->keyboard_focus;
1988         if (focus->frame) {
1989                 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1990                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1991                         weston_output_schedule_repaint(&focus->base);
1992         }
1993
1994
1995         /* XXX: If we get a modifier event immediately before the focus,
1996          *      we should try to keep the same serial. */
1997         notify_keyboard_focus_in(&input->base, keys,
1998                                  STATE_UPDATE_AUTOMATIC);
1999 }
2000
2001 static void
2002 input_handle_keyboard_leave(void *data,
2003                             struct wl_keyboard *keyboard,
2004                             uint32_t serial,
2005                             struct wl_surface *surface)
2006 {
2007         struct wayland_input *input = data;
2008         struct wayland_output *focus;
2009
2010         notify_keyboard_focus_out(&input->base);
2011
2012         focus = input->keyboard_focus;
2013         if (!focus)
2014                 return;
2015
2016         focus->keyboard_count--;
2017         if (!focus->keyboard_count && focus->frame) {
2018                 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
2019                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
2020                         weston_output_schedule_repaint(&focus->base);
2021         }
2022
2023         input->keyboard_focus = NULL;
2024 }
2025
2026 static void
2027 input_handle_key(void *data, struct wl_keyboard *keyboard,
2028                  uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
2029 {
2030         struct wayland_input *input = data;
2031         struct timespec ts;
2032
2033         if (!input->keyboard_focus)
2034                 return;
2035
2036         timespec_from_msec(&ts, time);
2037
2038         input->key_serial = serial;
2039         notify_key(&input->base, &ts, key,
2040                    state ? WL_KEYBOARD_KEY_STATE_PRESSED :
2041                            WL_KEYBOARD_KEY_STATE_RELEASED,
2042                    input->keyboard_state_update);
2043 }
2044
2045 static void
2046 input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
2047                        uint32_t serial_in, uint32_t mods_depressed,
2048                        uint32_t mods_latched, uint32_t mods_locked,
2049                        uint32_t group)
2050 {
2051         struct weston_keyboard *keyboard;
2052         struct wayland_input *input = data;
2053         struct wayland_backend *b = input->backend;
2054         uint32_t serial_out;
2055
2056         /* If we get a key event followed by a modifier event with the
2057          * same serial number, then we try to preserve those semantics by
2058          * reusing the same serial number on the way out too. */
2059         if (serial_in == input->key_serial)
2060                 serial_out = wl_display_get_serial(b->compositor->wl_display);
2061         else
2062                 serial_out = wl_display_next_serial(b->compositor->wl_display);
2063
2064         keyboard = weston_seat_get_keyboard(&input->base);
2065         xkb_state_update_mask(keyboard->xkb_state.state,
2066                               mods_depressed, mods_latched,
2067                               mods_locked, 0, 0, group);
2068         notify_modifiers(&input->base, serial_out);
2069 }
2070
2071 static void
2072 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2073                          int32_t rate, int32_t delay)
2074 {
2075         struct wayland_input *input = data;
2076         struct wayland_backend *b = input->backend;
2077
2078         b->compositor->kb_repeat_rate = rate;
2079         b->compositor->kb_repeat_delay = delay;
2080 }
2081
2082 static const struct wl_keyboard_listener keyboard_listener = {
2083         input_handle_keymap,
2084         input_handle_keyboard_enter,
2085         input_handle_keyboard_leave,
2086         input_handle_key,
2087         input_handle_modifiers,
2088         input_handle_repeat_info,
2089 };
2090
2091 static void
2092 input_handle_touch_down(void *data, struct wl_touch *wl_touch,
2093                         uint32_t serial, uint32_t time,
2094                         struct wl_surface *surface, int32_t id,
2095                         wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2096 {
2097         struct wayland_input *input = data;
2098         struct wayland_output *output;
2099         enum theme_location location;
2100         bool first_touch;
2101         int32_t fx, fy;
2102         double x, y;
2103         struct timespec ts;
2104
2105         x = wl_fixed_to_double(fixed_x);
2106         y = wl_fixed_to_double(fixed_y);
2107
2108         timespec_from_msec(&ts, time);
2109
2110         first_touch = (input->touch_points == 0);
2111         input->touch_points++;
2112
2113         input->touch_focus = wl_surface_get_user_data(surface);
2114         output = input->touch_focus;
2115         if (!first_touch && !input->touch_active)
2116                 return;
2117
2118         if (output->frame) {
2119                 location = frame_touch_down(output->frame, input, id, x, y);
2120
2121                 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2122                 x -= fx;
2123                 y -= fy;
2124
2125                 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2126                         weston_output_schedule_repaint(&output->base);
2127
2128                 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
2129                         input->touch_points--;
2130                         if (output->parent.xdg_toplevel)
2131                                 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
2132                                                       input->parent.seat, serial);
2133                         else if (output->parent.shell_surface)
2134                                 wl_shell_surface_move(output->parent.shell_surface,
2135                                                       input->parent.seat, serial);
2136                         frame_status_clear(output->frame,
2137                                            FRAME_STATUS_MOVE);
2138                         return;
2139                 }
2140
2141                 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
2142                         return;
2143         }
2144
2145         weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2146
2147         notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_DOWN);
2148         input->touch_active = true;
2149 }
2150
2151 static void
2152 input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2153                       uint32_t serial, uint32_t time, int32_t id)
2154 {
2155         struct wayland_input *input = data;
2156         struct wayland_output *output = input->touch_focus;
2157         bool active = input->touch_active;
2158         struct timespec ts;
2159
2160         timespec_from_msec(&ts, time);
2161
2162         input->touch_points--;
2163         if (input->touch_points == 0) {
2164                 input->touch_focus = NULL;
2165                 input->touch_active = false;
2166         }
2167
2168         if (!output)
2169                 return;
2170
2171         if (output->frame) {
2172                 frame_touch_up(output->frame, input, id);
2173
2174                 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2175                         wayland_output_destroy(&output->base);
2176                         input->touch_focus = NULL;
2177                         input->keyboard_focus = NULL;
2178                         if (wl_list_empty(&input->backend->compositor->output_list))
2179                                 weston_compositor_exit(input->backend->compositor);
2180
2181                         return;
2182                 }
2183                 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2184                         weston_output_schedule_repaint(&output->base);
2185         }
2186
2187         if (active)
2188                 notify_touch(&input->base, &ts, id, 0, 0, WL_TOUCH_UP);
2189 }
2190
2191 static void
2192 input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
2193                         uint32_t time, int32_t id,
2194                         wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2195 {
2196         struct wayland_input *input = data;
2197         struct wayland_output *output = input->touch_focus;
2198         int32_t fx, fy;
2199         double x, y;
2200         struct timespec ts;
2201
2202         x = wl_fixed_to_double(fixed_x);
2203         y = wl_fixed_to_double(fixed_y);
2204         timespec_from_msec(&ts, time);
2205
2206         if (!output || !input->touch_active)
2207                 return;
2208
2209         if (output->frame) {
2210                 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2211                 x -= fx;
2212                 y -= fy;
2213         }
2214
2215         weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2216
2217         notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_MOTION);
2218 }
2219
2220 static void
2221 input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2222 {
2223         struct wayland_input *input = data;
2224
2225         if (!input->touch_focus || !input->touch_active)
2226                 return;
2227
2228         notify_touch_frame(&input->base);
2229 }
2230
2231 static void
2232 input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2233 {
2234         struct wayland_input *input = data;
2235
2236         if (!input->touch_focus || !input->touch_active)
2237                 return;
2238
2239         notify_touch_cancel(&input->base);
2240 }
2241
2242 static const struct wl_touch_listener touch_listener = {
2243         input_handle_touch_down,
2244         input_handle_touch_up,
2245         input_handle_touch_motion,
2246         input_handle_touch_frame,
2247         input_handle_touch_cancel,
2248 };
2249
2250
2251 static void
2252 input_handle_capabilities(void *data, struct wl_seat *seat,
2253                           enum wl_seat_capability caps)
2254 {
2255         struct wayland_input *input = data;
2256
2257         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2258                 input->parent.pointer = wl_seat_get_pointer(seat);
2259                 wl_pointer_set_user_data(input->parent.pointer, input);
2260                 wl_pointer_add_listener(input->parent.pointer,
2261                                         &pointer_listener, input);
2262                 weston_seat_init_pointer(&input->base);
2263         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
2264                 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2265                         wl_pointer_release(input->parent.pointer);
2266                 else
2267                         wl_pointer_destroy(input->parent.pointer);
2268                 input->parent.pointer = NULL;
2269                 weston_seat_release_pointer(&input->base);
2270         }
2271
2272         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2273                 input->parent.keyboard = wl_seat_get_keyboard(seat);
2274                 wl_keyboard_set_user_data(input->parent.keyboard, input);
2275                 wl_keyboard_add_listener(input->parent.keyboard,
2276                                          &keyboard_listener, input);
2277         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
2278                 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2279                         wl_keyboard_release(input->parent.keyboard);
2280                 else
2281                         wl_keyboard_destroy(input->parent.keyboard);
2282                 input->parent.keyboard = NULL;
2283                 weston_seat_release_keyboard(&input->base);
2284         }
2285
2286         if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2287                 input->parent.touch = wl_seat_get_touch(seat);
2288                 wl_touch_set_user_data(input->parent.touch, input);
2289                 wl_touch_add_listener(input->parent.touch,
2290                                       &touch_listener, input);
2291                 weston_seat_init_touch(&input->base);
2292         } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2293                 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2294                         wl_touch_release(input->parent.touch);
2295                 else
2296                         wl_touch_destroy(input->parent.touch);
2297                 input->parent.touch = NULL;
2298                 weston_seat_release_touch(&input->base);
2299         }
2300 }
2301
2302 static void
2303 input_handle_name(void *data, struct wl_seat *seat,
2304                   const char *name)
2305 {
2306 }
2307
2308 static const struct wl_seat_listener seat_listener = {
2309         input_handle_capabilities,
2310         input_handle_name,
2311 };
2312
2313 static void
2314 display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
2315 {
2316         struct wayland_input *input;
2317         uint32_t version = MIN(available_version, 4);
2318
2319         input = zalloc(sizeof *input);
2320         if (input == NULL)
2321                 return;
2322
2323         weston_seat_init(&input->base, b->compositor, "default");
2324         input->backend = b;
2325         input->parent.seat = wl_registry_bind(b->parent.registry, id,
2326                                               &wl_seat_interface, version);
2327         input->seat_version = version;
2328         wl_list_insert(b->input_list.prev, &input->link);
2329
2330         wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2331         wl_seat_set_user_data(input->parent.seat, input);
2332
2333         input->parent.cursor.surface =
2334                 wl_compositor_create_surface(b->parent.compositor);
2335
2336         input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2337         input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
2338 }
2339
2340 static void
2341 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2342                                int32_t x, int32_t y,
2343                                int32_t physical_width, int32_t physical_height,
2344                                int32_t subpixel, const char *make,
2345                                const char *model, int32_t transform)
2346 {
2347         struct wayland_parent_output *output = data;
2348
2349         output->x = x;
2350         output->y = y;
2351         output->physical.width = physical_width;
2352         output->physical.height = physical_height;
2353         output->physical.subpixel = subpixel;
2354
2355         free(output->physical.make);
2356         output->physical.make = strdup(make);
2357         free(output->physical.model);
2358         output->physical.model = strdup(model);
2359
2360         output->transform = transform;
2361 }
2362
2363 static struct weston_mode *
2364 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2365 {
2366         struct weston_mode *mode;
2367
2368         wl_list_for_each(mode, list, link) {
2369                 if (mode->width == width && mode->height == height &&
2370                     mode->refresh == refresh)
2371                         return mode;
2372         }
2373
2374         mode = zalloc(sizeof *mode);
2375         if (!mode)
2376                 return NULL;
2377
2378         mode->width = width;
2379         mode->height = height;
2380         mode->refresh = refresh;
2381         wl_list_insert(list, &mode->link);
2382
2383         return mode;
2384 }
2385
2386 static struct weston_output *
2387 wayland_parent_output_get_enabled_output(struct wayland_parent_output *poutput)
2388 {
2389         struct wayland_head *head = poutput->head;
2390
2391         if (!head)
2392                 return NULL;
2393
2394         if (!weston_head_is_enabled(&head->base))
2395                 return NULL;
2396
2397         return weston_head_get_output(&head->base);
2398 }
2399
2400 static void
2401 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2402                            uint32_t flags, int32_t width, int32_t height,
2403                            int32_t refresh)
2404 {
2405         struct wayland_parent_output *output = data;
2406         struct weston_output *enabled_output;
2407         struct weston_mode *mode;
2408
2409         enabled_output = wayland_parent_output_get_enabled_output(output);
2410         if (enabled_output) {
2411                 mode = find_mode(&enabled_output->mode_list,
2412                                  width, height, refresh);
2413                 if (!mode)
2414                         return;
2415                 mode->flags = flags;
2416                 /* Do a mode-switch on current mode change? */
2417         } else {
2418                 mode = find_mode(&output->mode_list, width, height, refresh);
2419                 if (!mode)
2420                         return;
2421                 mode->flags = flags;
2422                 if (flags & WL_OUTPUT_MODE_CURRENT)
2423                         output->current_mode = mode;
2424                 if (flags & WL_OUTPUT_MODE_PREFERRED)
2425                         output->preferred_mode = mode;
2426         }
2427 }
2428
2429 static const struct wl_output_listener output_listener = {
2430         wayland_parent_output_geometry,
2431         wayland_parent_output_mode
2432 };
2433
2434 static void
2435 output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2436 {
2437         struct wayland_parent_output *output = data;
2438
2439         assert(output->sync_cb == callback);
2440         wl_callback_destroy(callback);
2441         output->sync_cb = NULL;
2442
2443         assert(output->backend->sprawl_across_outputs);
2444
2445         wayland_head_create_for_parent_output(output->backend->compositor, output);
2446 }
2447
2448 static const struct wl_callback_listener output_sync_listener = {
2449         output_sync_callback
2450 };
2451
2452 static void
2453 wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
2454 {
2455         struct wayland_parent_output *output;
2456
2457         output = zalloc(sizeof *output);
2458         if (!output)
2459                 return;
2460
2461         output->backend = b;
2462         output->id = id;
2463         output->global = wl_registry_bind(b->parent.registry, id,
2464                                           &wl_output_interface, 1);
2465         if (!output->global) {
2466                 free(output);
2467                 return;
2468         }
2469
2470         wl_output_add_listener(output->global, &output_listener, output);
2471
2472         output->scale = 0;
2473         output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2474         output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2475         wl_list_init(&output->mode_list);
2476         wl_list_insert(&b->parent.output_list, &output->link);
2477
2478         if (b->sprawl_across_outputs) {
2479                 output->sync_cb = wl_display_sync(b->parent.wl_display);
2480                 wl_callback_add_listener(output->sync_cb,
2481                                          &output_sync_listener, output);
2482         }
2483 }
2484
2485 static void
2486 wayland_parent_output_destroy(struct wayland_parent_output *output)
2487 {
2488         struct weston_mode *mode, *next;
2489
2490         if (output->sync_cb)
2491                 wl_callback_destroy(output->sync_cb);
2492
2493         if (output->head)
2494                 wayland_head_destroy(output->head);
2495
2496         wl_output_destroy(output->global);
2497         free(output->physical.make);
2498         free(output->physical.model);
2499
2500         wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2501                 wl_list_remove(&mode->link);
2502                 free(mode);
2503         }
2504
2505         wl_list_remove(&output->link);
2506         free(output);
2507 }
2508
2509 static void
2510 xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2511 {
2512         zxdg_shell_v6_pong(shell, serial);
2513 }
2514
2515 static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2516         xdg_shell_ping,
2517 };
2518
2519 static void
2520 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2521                        const char *interface, uint32_t version)
2522 {
2523         struct wayland_backend *b = data;
2524
2525         if (strcmp(interface, "wl_compositor") == 0) {
2526                 b->parent.compositor =
2527                         wl_registry_bind(registry, name,
2528                                          &wl_compositor_interface,
2529                                          MIN(version, 4));
2530         } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2531                 b->parent.xdg_shell =
2532                         wl_registry_bind(registry, name,
2533                                          &zxdg_shell_v6_interface, 1);
2534                 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2535                                            &xdg_shell_listener, b);
2536         } else if (strcmp(interface, "wl_shell") == 0) {
2537                 b->parent.shell =
2538                         wl_registry_bind(registry, name,
2539                                          &wl_shell_interface, 1);
2540         } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
2541                 b->parent.fshell =
2542                         wl_registry_bind(registry, name,
2543                                          &zwp_fullscreen_shell_v1_interface, 1);
2544         } else if (strcmp(interface, "wl_seat") == 0) {
2545                 display_add_seat(b, name, version);
2546         } else if (strcmp(interface, "wl_output") == 0) {
2547                 wayland_backend_register_output(b, name);
2548         } else if (strcmp(interface, "wl_shm") == 0) {
2549                 b->parent.shm =
2550                         wl_registry_bind(registry, name, &wl_shm_interface, 1);
2551         }
2552 }
2553
2554 static void
2555 registry_handle_global_remove(void *data, struct wl_registry *registry,
2556                               uint32_t name)
2557 {
2558         struct wayland_backend *b = data;
2559         struct wayland_parent_output *output, *next;
2560
2561         wl_list_for_each_safe(output, next, &b->parent.output_list, link)
2562                 if (output->id == name)
2563                         wayland_parent_output_destroy(output);
2564 }
2565
2566 static const struct wl_registry_listener registry_listener = {
2567         registry_handle_global,
2568         registry_handle_global_remove
2569 };
2570
2571 static int
2572 wayland_backend_handle_event(int fd, uint32_t mask, void *data)
2573 {
2574         struct wayland_backend *b = data;
2575         int count = 0;
2576
2577         if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
2578                 weston_compositor_exit(b->compositor);
2579                 return 0;
2580         }
2581
2582         if (mask & WL_EVENT_READABLE)
2583                 count = wl_display_dispatch(b->parent.wl_display);
2584         if (mask & WL_EVENT_WRITABLE)
2585                 wl_display_flush(b->parent.wl_display);
2586
2587         if (mask == 0) {
2588                 count = wl_display_dispatch_pending(b->parent.wl_display);
2589                 wl_display_flush(b->parent.wl_display);
2590         }
2591
2592         return count;
2593 }
2594
2595 static void
2596 wayland_destroy(struct weston_compositor *ec)
2597 {
2598         struct wayland_backend *b = to_wayland_backend(ec);
2599         struct weston_head *base, *next;
2600
2601         wl_event_source_remove(b->parent.wl_source);
2602
2603         weston_compositor_shutdown(ec);
2604
2605         wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2606                 wayland_head_destroy(to_wayland_head(base));
2607
2608         if (b->parent.shm)
2609                 wl_shm_destroy(b->parent.shm);
2610
2611         if (b->parent.xdg_shell)
2612                 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2613
2614         if (b->parent.shell)
2615                 wl_shell_destroy(b->parent.shell);
2616
2617         if (b->parent.fshell)
2618                 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2619
2620         if (b->parent.compositor)
2621                 wl_compositor_destroy(b->parent.compositor);
2622
2623         if (b->theme)
2624                 theme_destroy(b->theme);
2625
2626         if (b->frame_device)
2627                 cairo_device_destroy(b->frame_device);
2628
2629         wl_cursor_theme_destroy(b->cursor_theme);
2630
2631         wl_registry_destroy(b->parent.registry);
2632         wl_display_flush(b->parent.wl_display);
2633         wl_display_disconnect(b->parent.wl_display);
2634
2635         free(b);
2636 }
2637
2638 static const char *left_ptrs[] = {
2639         "left_ptr",
2640         "default",
2641         "top_left_arrow",
2642         "left-arrow"
2643 };
2644
2645 static void
2646 create_cursor(struct wayland_backend *b,
2647               struct weston_wayland_backend_config *config)
2648 {
2649         unsigned int i;
2650
2651         b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2652                                                config->cursor_size,
2653                                                b->parent.shm);
2654         if (!b->cursor_theme) {
2655                 fprintf(stderr, "could not load cursor theme\n");
2656                 return;
2657         }
2658
2659         b->cursor = NULL;
2660         for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2661                 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
2662                                                        left_ptrs[i]);
2663         if (!b->cursor) {
2664                 fprintf(stderr, "could not load left cursor\n");
2665                 return;
2666         }
2667 }
2668
2669 static void
2670 fullscreen_binding(struct weston_keyboard *keyboard,
2671                    const struct timespec *time, uint32_t key, void *data)
2672 {
2673         struct wayland_backend *b = data;
2674         struct wayland_input *input = NULL;
2675
2676         wl_list_for_each(input, &b->input_list, link)
2677                 if (&input->base == keyboard->seat)
2678                         break;
2679
2680         if (!input || !input->output)
2681                 return;
2682
2683         if (input->output->frame)
2684                 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2685         else
2686                 wayland_output_set_windowed(input->output);
2687
2688         weston_output_schedule_repaint(&input->output->base);
2689 }
2690
2691 static struct wayland_backend *
2692 wayland_backend_create(struct weston_compositor *compositor,
2693                        struct weston_wayland_backend_config *new_config)
2694 {
2695         struct wayland_backend *b;
2696         struct wl_event_loop *loop;
2697         int fd;
2698
2699         b = zalloc(sizeof *b);
2700         if (b == NULL)
2701                 return NULL;
2702
2703         b->compositor = compositor;
2704         compositor->backend = &b->base;
2705
2706         if (weston_compositor_set_presentation_clock_software(compositor) < 0)
2707                 goto err_compositor;
2708
2709         b->parent.wl_display = wl_display_connect(new_config->display_name);
2710         if (b->parent.wl_display == NULL) {
2711                 weston_log("Error: Failed to connect to parent Wayland compositor: %m\n");
2712                 weston_log_continue(STAMP_SPACE "display option: %s, WAYLAND_DISPLAY=%s\n",
2713                                     new_config->display_name ?: "(none)",
2714                                     getenv("WAYLAND_DISPLAY") ?: "(not set)");
2715                 goto err_compositor;
2716         }
2717
2718         wl_list_init(&b->parent.output_list);
2719         wl_list_init(&b->input_list);
2720         b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2721         wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2722         wl_display_roundtrip(b->parent.wl_display);
2723
2724         create_cursor(b, new_config);
2725
2726 #ifdef ENABLE_EGL
2727         b->use_pixman = new_config->use_pixman;
2728 #else
2729         b->use_pixman = true;
2730 #endif
2731         b->fullscreen = new_config->fullscreen;
2732
2733         if (!b->use_pixman) {
2734                 gl_renderer = weston_load_module("gl-renderer.so",
2735                                                  "gl_renderer_interface");
2736                 if (!gl_renderer)
2737                         b->use_pixman = true;
2738         }
2739
2740         if (!b->use_pixman) {
2741                 if (gl_renderer->display_create(compositor,
2742                                                 EGL_PLATFORM_WAYLAND_KHR,
2743                                                 b->parent.wl_display,
2744                                                 NULL,
2745                                                 gl_renderer->alpha_attribs,
2746                                                 NULL,
2747                                                 0) < 0) {
2748                         weston_log("Failed to initialize the GL renderer; "
2749                                    "falling back to pixman.\n");
2750                         b->use_pixman = true;
2751                 }
2752         }
2753
2754         if (b->use_pixman) {
2755                 if (pixman_renderer_init(compositor) < 0) {
2756                         weston_log("Failed to initialize pixman renderer\n");
2757                         goto err_display;
2758                 }
2759         }
2760
2761         b->base.destroy = wayland_destroy;
2762         b->base.create_output = wayland_output_create;
2763
2764         loop = wl_display_get_event_loop(compositor->wl_display);
2765
2766         fd = wl_display_get_fd(b->parent.wl_display);
2767         b->parent.wl_source =
2768                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2769                                      wayland_backend_handle_event, b);
2770         if (b->parent.wl_source == NULL)
2771                 goto err_display;
2772
2773         wl_event_source_check(b->parent.wl_source);
2774
2775         if (compositor->renderer->import_dmabuf) {
2776                 if (linux_dmabuf_setup(compositor) < 0)
2777                         weston_log("Error: initializing dmabuf "
2778                                    "support failed.\n");
2779         }
2780
2781         return b;
2782 err_display:
2783         wl_display_disconnect(b->parent.wl_display);
2784 err_compositor:
2785         weston_compositor_shutdown(compositor);
2786         free(b);
2787         return NULL;
2788 }
2789
2790 static void
2791 wayland_backend_destroy(struct wayland_backend *b)
2792 {
2793         wl_display_disconnect(b->parent.wl_display);
2794
2795         if (b->theme)
2796                 theme_destroy(b->theme);
2797         if (b->frame_device)
2798                 cairo_device_destroy(b->frame_device);
2799         wl_cursor_theme_destroy(b->cursor_theme);
2800
2801         weston_compositor_shutdown(b->compositor);
2802         free(b);
2803 }
2804
2805 static const struct weston_windowed_output_api windowed_api = {
2806         wayland_output_set_size,
2807         wayland_head_create_windowed,
2808 };
2809
2810 static void
2811 config_init_to_defaults(struct weston_wayland_backend_config *config)
2812 {
2813 }
2814
2815 WL_EXPORT int
2816 weston_backend_init(struct weston_compositor *compositor,
2817                     struct weston_backend_config *config_base)
2818 {
2819         struct wayland_backend *b;
2820         struct wayland_parent_output *poutput;
2821         struct weston_wayland_backend_config new_config;
2822         int ret;
2823
2824         if (config_base == NULL ||
2825             config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2826             config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2827                 weston_log("wayland backend config structure is invalid\n");
2828                 return -1;
2829         }
2830
2831         config_init_to_defaults(&new_config);
2832         memcpy(&new_config, config_base, config_base->struct_size);
2833
2834         b = wayland_backend_create(compositor, &new_config);
2835
2836         if (!b)
2837                 return -1;
2838
2839         if (new_config.sprawl || b->parent.fshell) {
2840                 b->sprawl_across_outputs = true;
2841                 wl_display_roundtrip(b->parent.wl_display);
2842
2843                 wl_list_for_each(poutput, &b->parent.output_list, link)
2844                         wayland_head_create_for_parent_output(compositor, poutput);
2845
2846                 return 0;
2847         }
2848
2849         if (new_config.fullscreen) {
2850                 if (!wayland_head_create(compositor, "wayland-fullscreen")) {
2851                         weston_log("Unable to create a fullscreen head.\n");
2852                         goto err_outputs;
2853                 }
2854
2855                 return 0;
2856         }
2857
2858         ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2859                                          &windowed_api, sizeof(windowed_api));
2860
2861         if (ret < 0) {
2862                 weston_log("Failed to register output API.\n");
2863                 wayland_backend_destroy(b);
2864                 return -1;
2865         }
2866
2867         weston_compositor_add_key_binding(compositor, KEY_F,
2868                                           MODIFIER_CTRL | MODIFIER_ALT,
2869                                           fullscreen_binding, b);
2870         return 0;
2871
2872 err_outputs:
2873         wayland_backend_destroy(b);
2874         return -1;
2875 }