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