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