downstream: Multiseat support for drm/wayland backends
[profile/ivi/weston-ivi-shell.git] / src / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  * Copyright © 2013 Jason Ekstrand
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <linux/input.h>
34
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37 #include <wayland-cursor.h>
38
39 #include "compositor.h"
40 #include "gl-renderer.h"
41 #include "pixman-renderer.h"
42 #include "../shared/image-loader.h"
43 #include "../shared/os-compatibility.h"
44 #include "../shared/cairo-util.h"
45 #include "fullscreen-shell-client-protocol.h"
46
47 #define WINDOW_TITLE "Weston Compositor"
48
49 struct wayland_compositor {
50         struct weston_compositor base;
51
52         struct {
53                 struct wl_display *wl_display;
54                 struct wl_registry *registry;
55                 struct wl_compositor *compositor;
56                 struct wl_shell *shell;
57                 struct _wl_fullscreen_shell *fshell;
58                 struct wl_shm *shm;
59
60                 struct wl_list output_list;
61
62                 struct wl_event_source *wl_source;
63                 uint32_t event_mask;
64         } parent;
65
66         int use_pixman;
67         int sprawl_across_outputs;
68         int seatfilter;
69         char *seatname;
70
71         struct theme *theme;
72         cairo_device_t *frame_device;
73         struct wl_cursor_theme *cursor_theme;
74         struct wl_cursor *cursor;
75
76         struct wl_list input_list;
77 };
78
79 struct wayland_output {
80         struct weston_output base;
81
82         struct {
83                 int draw_initial_frame;
84                 struct wl_surface *surface;
85
86                 struct wl_output *output;
87                 uint32_t global_id;
88
89                 struct wl_shell_surface *shell_surface;
90                 int configure_width, configure_height;
91         } parent;
92
93         int keyboard_count;
94
95         char *name;
96         struct frame *frame;
97
98         struct {
99                 struct wl_egl_window *egl_window;
100                 struct {
101                         cairo_surface_t *top;
102                         cairo_surface_t *left;
103                         cairo_surface_t *right;
104                         cairo_surface_t *bottom;
105                 } border;
106         } gl;
107
108         struct {
109                 struct wl_list buffers;
110                 struct wl_list free_buffers;
111         } shm;
112
113         struct weston_mode mode;
114         uint32_t scale;
115 };
116
117 struct wayland_parent_output {
118         struct wayland_output *output;
119         struct wayland_compositor *c;
120         struct wl_list link;
121
122         struct wl_output *global;
123         uint32_t id;
124
125         struct {
126                 char *make;
127                 char *model;
128                 int32_t width, height;
129                 uint32_t subpixel;
130         } physical;
131
132         int32_t x, y;
133         uint32_t transform;
134         uint32_t scale;
135
136         struct wl_list mode_list;
137         struct weston_mode *preferred_mode;
138         struct weston_mode *current_mode;
139         char *seatname;
140 };
141
142 struct wayland_shm_buffer {
143         struct wayland_output *output;
144
145         struct wl_list link;
146         struct wl_list free_link;
147
148         struct wl_buffer *buffer;
149         void *data;
150         size_t size;
151         pixman_region32_t damage;
152         int frame_damaged;
153
154         pixman_image_t *pm_image;
155         cairo_surface_t *c_surface;
156 };
157
158 struct wayland_input {
159         struct weston_seat base;
160         struct wayland_compositor *compositor;
161         struct wl_list link;
162
163         struct {
164                 struct wl_seat *seat;
165                 struct wl_pointer *pointer;
166                 struct wl_keyboard *keyboard;
167                 struct wl_touch *touch;
168
169                 struct {
170                         struct wl_surface *surface;
171                         int32_t hx, hy;
172                 } cursor;
173         } parent;
174
175         enum weston_key_state_update keyboard_state_update;
176         uint32_t key_serial;
177         uint32_t enter_serial;
178         int focus;
179         struct wayland_output *output;
180         struct wayland_output *keyboard_focus;
181
182         enum wl_seat_capability caps;
183         int initialized;
184         int caps_update_required;
185 };
186
187 struct gl_renderer_interface *gl_renderer;
188
189 static void
190 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
191 {
192         cairo_surface_destroy(buffer->c_surface);
193         pixman_image_unref(buffer->pm_image);
194
195         wl_buffer_destroy(buffer->buffer);
196         munmap(buffer->data, buffer->size);
197
198         pixman_region32_fini(&buffer->damage);
199
200         wl_list_remove(&buffer->link);
201         wl_list_remove(&buffer->free_link);
202         free(buffer);
203 }
204
205 static void
206 buffer_release(void *data, struct wl_buffer *buffer)
207 {
208         struct wayland_shm_buffer *sb = data;
209
210         if (sb->output) {
211                 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
212         } else {
213                 wayland_shm_buffer_destroy(sb);
214         }
215 }
216
217 static const struct wl_buffer_listener buffer_listener = {
218         buffer_release
219 };
220
221 static struct wayland_shm_buffer *
222 wayland_output_get_shm_buffer(struct wayland_output *output)
223 {
224         struct wayland_compositor *c =
225                 (struct wayland_compositor *) output->base.compositor;
226         struct wl_shm *shm = c->parent.shm;
227         struct wayland_shm_buffer *sb;
228
229         struct wl_shm_pool *pool;
230         int width, height, stride;
231         int32_t fx, fy;
232         int fd;
233         unsigned char *data;
234
235         if (!wl_list_empty(&output->shm.free_buffers)) {
236                 sb = container_of(output->shm.free_buffers.next,
237                                   struct wayland_shm_buffer, free_link);
238                 wl_list_remove(&sb->free_link);
239                 wl_list_init(&sb->free_link);
240
241                 return sb;
242         }
243
244         if (output->frame) {
245                 width = frame_width(output->frame);
246                 height = frame_height(output->frame);
247         } else {
248                 width = output->base.current_mode->width;
249                 height = output->base.current_mode->height;
250         }
251
252         stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
253
254         fd = os_create_anonymous_file(height * stride);
255         if (fd < 0) {
256                 weston_log("could not create an anonymous file buffer: %m\n");
257                 return NULL;
258         }
259
260         data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
261         if (data == MAP_FAILED) {
262                 weston_log("could not mmap %d memory for data: %m\n", height * stride);
263                 close(fd);
264                 return NULL;
265         }
266
267         sb = zalloc(sizeof *sb);
268         if (sb == NULL) {
269                 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
270                 close(fd);
271                 free(data);
272                 return NULL;
273         }
274
275         sb->output = output;
276         wl_list_init(&sb->free_link);
277         wl_list_insert(&output->shm.buffers, &sb->link);
278
279         pixman_region32_init_rect(&sb->damage, 0, 0,
280                                   output->base.width, output->base.height);
281         sb->frame_damaged = 1;
282
283         sb->data = data;
284         sb->size = height * stride;
285
286         pool = wl_shm_create_pool(shm, fd, sb->size);
287
288         sb->buffer = wl_shm_pool_create_buffer(pool, 0,
289                                                width, height,
290                                                stride,
291                                                WL_SHM_FORMAT_ARGB8888);
292         wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
293         wl_shm_pool_destroy(pool);
294         close(fd);
295
296         memset(data, 0, sb->size);
297
298         sb->c_surface =
299                 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
300                                                     width, height, stride);
301
302         fx = 0;
303         fy = 0;
304         if (output->frame)
305                 frame_interior(output->frame, &fx, &fy, 0, 0);
306         sb->pm_image =
307                 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
308                                          (uint32_t *)(data + fy * stride) + fx,
309                                          stride);
310
311         return sb;
312 }
313
314 static void
315 frame_done(void *data, struct wl_callback *callback, uint32_t time)
316 {
317         struct weston_output *output = data;
318
319         wl_callback_destroy(callback);
320         weston_output_finish_frame(output, time);
321 }
322
323 static const struct wl_callback_listener frame_listener = {
324         frame_done
325 };
326
327 static void
328 draw_initial_frame(struct wayland_output *output)
329 {
330         struct wayland_shm_buffer *sb;
331
332         sb = wayland_output_get_shm_buffer(output);
333
334         /* If we are rendering with GL, then orphan it so that it gets
335          * destroyed immediately */
336         if (output->gl.egl_window)
337                 sb->output = NULL;
338
339         wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
340         wl_surface_damage(output->parent.surface, 0, 0,
341                           output->base.current_mode->width,
342                           output->base.current_mode->height);
343 }
344
345 static void
346 wayland_output_update_gl_border(struct wayland_output *output)
347 {
348         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
349         cairo_t *cr;
350
351         if (!output->frame)
352                 return;
353         if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
354                 return;
355
356         fwidth = frame_width(output->frame);
357         fheight = frame_height(output->frame);
358         frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
359
360         if (!output->gl.border.top)
361                 output->gl.border.top =
362                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
363                                                    fwidth, iy);
364         cr = cairo_create(output->gl.border.top);
365         frame_repaint(output->frame, cr);
366         cairo_destroy(cr);
367         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
368                                        fwidth, iy,
369                                        cairo_image_surface_get_stride(output->gl.border.top) / 4,
370                                        cairo_image_surface_get_data(output->gl.border.top));
371
372
373         if (!output->gl.border.left)
374                 output->gl.border.left =
375                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
376                                                    ix, 1);
377         cr = cairo_create(output->gl.border.left);
378         cairo_translate(cr, 0, -iy);
379         frame_repaint(output->frame, cr);
380         cairo_destroy(cr);
381         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
382                                        ix, 1,
383                                        cairo_image_surface_get_stride(output->gl.border.left) / 4,
384                                        cairo_image_surface_get_data(output->gl.border.left));
385
386
387         if (!output->gl.border.right)
388                 output->gl.border.right =
389                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
390                                                    fwidth - (ix + iwidth), 1);
391         cr = cairo_create(output->gl.border.right);
392         cairo_translate(cr, -(iwidth + ix), -iy);
393         frame_repaint(output->frame, cr);
394         cairo_destroy(cr);
395         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
396                                        fwidth - (ix + iwidth), 1,
397                                        cairo_image_surface_get_stride(output->gl.border.right) / 4,
398                                        cairo_image_surface_get_data(output->gl.border.right));
399
400
401         if (!output->gl.border.bottom)
402                 output->gl.border.bottom =
403                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
404                                                    fwidth, fheight - (iy + iheight));
405         cr = cairo_create(output->gl.border.bottom);
406         cairo_translate(cr, 0, -(iy + iheight));
407         frame_repaint(output->frame, cr);
408         cairo_destroy(cr);
409         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
410                                        fwidth, fheight - (iy + iheight),
411                                        cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
412                                        cairo_image_surface_get_data(output->gl.border.bottom));
413 }
414
415 static void
416 wayland_output_start_repaint_loop(struct weston_output *output_base)
417 {
418         struct wayland_output *output = (struct wayland_output *) output_base;
419         struct wayland_compositor *wc =
420                 (struct wayland_compositor *)output->base.compositor;
421         struct wl_callback *callback;
422
423         /* If this is the initial frame, we need to attach a buffer so that
424          * the compositor can map the surface and include it in its render
425          * loop. If the surface doesn't end up in the render loop, the frame
426          * callback won't be invoked. The buffer is transparent and of the
427          * same size as the future real output buffer. */
428         if (output->parent.draw_initial_frame) {
429                 output->parent.draw_initial_frame = 0;
430
431                 draw_initial_frame(output);
432         }
433
434         callback = wl_surface_frame(output->parent.surface);
435         wl_callback_add_listener(callback, &frame_listener, output);
436         wl_surface_commit(output->parent.surface);
437         wl_display_flush(wc->parent.wl_display);
438 }
439
440 static int
441 wayland_output_repaint_gl(struct weston_output *output_base,
442                           pixman_region32_t *damage)
443 {
444         struct wayland_output *output = (struct wayland_output *) output_base;
445         struct weston_compositor *ec = output->base.compositor;
446         struct wl_callback *callback;
447
448         callback = wl_surface_frame(output->parent.surface);
449         wl_callback_add_listener(callback, &frame_listener, output);
450
451         wayland_output_update_gl_border(output);
452
453         ec->renderer->repaint_output(&output->base, damage);
454
455         pixman_region32_subtract(&ec->primary_plane.damage,
456                                  &ec->primary_plane.damage, damage);
457         return 0;
458 }
459
460 static void
461 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
462 {
463         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
464         cairo_t *cr;
465
466         if (!buffer->output->frame || !buffer->frame_damaged)
467                 return;
468
469         cr = cairo_create(buffer->c_surface);
470
471         frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
472         fwidth = frame_width(buffer->output->frame);
473         fheight = frame_height(buffer->output->frame);
474
475         /* Set the clip so we don't unnecisaraly damage the surface */
476         cairo_move_to(cr, ix, iy);
477         cairo_rel_line_to(cr, iwidth, 0);
478         cairo_rel_line_to(cr, 0, iheight);
479         cairo_rel_line_to(cr, -iwidth, 0);
480         cairo_line_to(cr, ix, iy);
481         cairo_line_to(cr, 0, iy);
482         cairo_line_to(cr, 0, fheight);
483         cairo_line_to(cr, fwidth, fheight);
484         cairo_line_to(cr, fwidth, 0);
485         cairo_line_to(cr, 0, 0);
486         cairo_line_to(cr, 0, iy);
487         cairo_close_path(cr);
488         cairo_clip(cr);
489
490         /* Draw using a pattern so that the final result gets clipped */
491         cairo_push_group(cr);
492         frame_repaint(buffer->output->frame, cr);
493         cairo_pop_group_to_source(cr);
494         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
495         cairo_paint(cr);
496
497         cairo_destroy(cr);
498 }
499
500 static void
501 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
502 {
503         pixman_region32_t damage;
504         pixman_box32_t *rects;
505         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
506         int i, n;
507
508         pixman_region32_init(&damage);
509         weston_transformed_region(sb->output->base.width,
510                                   sb->output->base.height,
511                                   sb->output->base.transform,
512                                   sb->output->base.current_scale,
513                                   &sb->damage, &damage);
514
515         if (sb->output->frame) {
516                 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
517                 fwidth = frame_width(sb->output->frame);
518                 fheight = frame_height(sb->output->frame);
519
520                 pixman_region32_translate(&damage, ix, iy);
521
522                 if (sb->frame_damaged) {
523                         pixman_region32_union_rect(&damage, &damage,
524                                                    0, 0, fwidth, iy);
525                         pixman_region32_union_rect(&damage, &damage,
526                                                    0, iy, ix, iheight);
527                         pixman_region32_union_rect(&damage, &damage,
528                                                    ix + iwidth, iy,
529                                                    fwidth - (ix + iwidth), iheight);
530                         pixman_region32_union_rect(&damage, &damage,
531                                                    0, iy + iheight,
532                                                    fwidth, fheight - (iy + iheight));
533                 }
534         }
535
536         rects = pixman_region32_rectangles(&damage, &n);
537         wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
538         for (i = 0; i < n; ++i)
539                 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
540                                   rects[i].y1, rects[i].x2 - rects[i].x1,
541                                   rects[i].y2 - rects[i].y1);
542
543         if (sb->output->frame)
544                 pixman_region32_fini(&damage);
545 }
546
547 static int
548 wayland_output_repaint_pixman(struct weston_output *output_base,
549                               pixman_region32_t *damage)
550 {
551         struct wayland_output *output = (struct wayland_output *) output_base;
552         struct wayland_compositor *c =
553                 (struct wayland_compositor *)output->base.compositor;
554         struct wl_callback *callback;
555         struct wayland_shm_buffer *sb;
556
557         if (output->frame) {
558                 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
559                         wl_list_for_each(sb, &output->shm.buffers, link)
560                                 sb->frame_damaged = 1;
561         }
562
563         wl_list_for_each(sb, &output->shm.buffers, link)
564                 pixman_region32_union(&sb->damage, &sb->damage, damage);
565
566         sb = wayland_output_get_shm_buffer(output);
567
568         wayland_output_update_shm_border(sb);
569         pixman_renderer_output_set_buffer(output_base, sb->pm_image);
570         c->base.renderer->repaint_output(output_base, &sb->damage);
571
572         wayland_shm_buffer_attach(sb);
573
574         callback = wl_surface_frame(output->parent.surface);
575         wl_callback_add_listener(callback, &frame_listener, output);
576         wl_surface_commit(output->parent.surface);
577         wl_display_flush(c->parent.wl_display);
578
579         pixman_region32_fini(&sb->damage);
580         pixman_region32_init(&sb->damage);
581         sb->frame_damaged = 0;
582
583         pixman_region32_subtract(&c->base.primary_plane.damage,
584                                  &c->base.primary_plane.damage, damage);
585         return 0;
586 }
587
588 static void
589 wayland_output_destroy(struct weston_output *output_base)
590 {
591         struct wayland_output *output = (struct wayland_output *) output_base;
592         struct wayland_compositor *c =
593                 (struct wayland_compositor *) output->base.compositor;
594
595         if (c->use_pixman) {
596                 pixman_renderer_output_destroy(output_base);
597         } else {
598                 gl_renderer->output_destroy(output_base);
599         }
600
601         wl_egl_window_destroy(output->gl.egl_window);
602         wl_surface_destroy(output->parent.surface);
603         if (output->parent.shell_surface)
604                 wl_shell_surface_destroy(output->parent.shell_surface);
605
606         if (output->frame)
607                 frame_destroy(output->frame);
608         free(output->name);
609
610         cairo_surface_destroy(output->gl.border.top);
611         cairo_surface_destroy(output->gl.border.left);
612         cairo_surface_destroy(output->gl.border.right);
613         cairo_surface_destroy(output->gl.border.bottom);
614
615         weston_output_destroy(&output->base);
616         free(output);
617
618         return;
619 }
620
621 static const struct wl_shell_surface_listener shell_surface_listener;
622
623 static int
624 wayland_output_init_gl_renderer(struct wayland_output *output)
625 {
626         int32_t fwidth = 0, fheight = 0;
627
628         if (output->frame) {
629                 fwidth = frame_width(output->frame);
630                 fheight = frame_height(output->frame);
631         } else {
632                 fwidth = output->base.current_mode->width;
633                 fheight = output->base.current_mode->height;
634         }
635
636         output->gl.egl_window =
637                 wl_egl_window_create(output->parent.surface,
638                                      fwidth, fheight);
639         if (!output->gl.egl_window) {
640                 weston_log("failure to create wl_egl_window\n");
641                 return -1;
642         }
643
644         if (gl_renderer->output_create(&output->base,
645                                        output->gl.egl_window,
646                                        gl_renderer->alpha_attribs,
647                                        NULL) < 0)
648                 goto cleanup_window;
649
650         return 0;
651
652 cleanup_window:
653         wl_egl_window_destroy(output->gl.egl_window);
654         return -1;
655 }
656
657 static int
658 wayland_output_init_pixman_renderer(struct wayland_output *output)
659 {
660         return pixman_renderer_output_create(&output->base);
661 }
662
663 static void
664 wayland_output_resize_surface(struct wayland_output *output)
665 {
666         struct wayland_compositor *c =
667                 (struct wayland_compositor *)output->base.compositor;
668         struct wayland_shm_buffer *buffer, *next;
669         int32_t ix, iy, iwidth, iheight;
670         int32_t width, height;
671         struct wl_region *region;
672
673         width = output->base.current_mode->width;
674         height = output->base.current_mode->height;
675
676         if (output->frame) {
677                 frame_resize_inside(output->frame, width, height);
678
679                 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
680                 region = wl_compositor_create_region(c->parent.compositor);
681                 wl_region_add(region, ix, iy, iwidth, iheight);
682                 wl_surface_set_input_region(output->parent.surface, region);
683                 wl_region_destroy(region);
684
685                 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
686                 region = wl_compositor_create_region(c->parent.compositor);
687                 wl_region_add(region, ix, iy, iwidth, iheight);
688                 wl_surface_set_opaque_region(output->parent.surface, region);
689                 wl_region_destroy(region);
690
691                 width = frame_width(output->frame);
692                 height = frame_height(output->frame);
693         } else {
694                 region = wl_compositor_create_region(c->parent.compositor);
695                 wl_region_add(region, 0, 0, width, height);
696                 wl_surface_set_input_region(output->parent.surface, region);
697                 wl_region_destroy(region);
698
699                 region = wl_compositor_create_region(c->parent.compositor);
700                 wl_region_add(region, 0, 0, width, height);
701                 wl_surface_set_opaque_region(output->parent.surface, region);
702                 wl_region_destroy(region);
703         }
704
705         if (output->gl.egl_window) {
706                 wl_egl_window_resize(output->gl.egl_window,
707                                      width, height, 0, 0);
708
709                 /* These will need to be re-created due to the resize */
710                 gl_renderer->output_set_border(&output->base,
711                                                GL_RENDERER_BORDER_TOP,
712                                                0, 0, 0, NULL);
713                 cairo_surface_destroy(output->gl.border.top);
714                 output->gl.border.top = NULL;
715                 gl_renderer->output_set_border(&output->base,
716                                                GL_RENDERER_BORDER_LEFT,
717                                                0, 0, 0, NULL);
718                 cairo_surface_destroy(output->gl.border.left);
719                 output->gl.border.left = NULL;
720                 gl_renderer->output_set_border(&output->base,
721                                                GL_RENDERER_BORDER_RIGHT,
722                                                0, 0, 0, NULL);
723                 cairo_surface_destroy(output->gl.border.right);
724                 output->gl.border.right = NULL;
725                 gl_renderer->output_set_border(&output->base,
726                                                GL_RENDERER_BORDER_BOTTOM,
727                                                0, 0, 0, NULL);
728                 cairo_surface_destroy(output->gl.border.bottom);
729                 output->gl.border.bottom = NULL;
730         }
731
732         /* Throw away any remaining SHM buffers */
733         wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
734                 wayland_shm_buffer_destroy(buffer);
735         /* These will get thrown away when they get released */
736         wl_list_for_each(buffer, &output->shm.buffers, link)
737                 buffer->output = NULL;
738 }
739
740 static int
741 wayland_output_set_windowed(struct wayland_output *output)
742 {
743         struct wayland_compositor *c =
744                 (struct wayland_compositor *)output->base.compositor;
745         int tlen;
746         char *title;
747
748         if (output->frame)
749                 return 0;
750
751         if (output->name) {
752                 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
753                 title = malloc(tlen + 1);
754                 if (!title)
755                         return -1;
756
757                 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
758         } else {
759                 title = strdup(WINDOW_TITLE);
760         }
761
762         if (!c->theme) {
763                 c->theme = theme_create();
764                 if (!c->theme) {
765                         free(title);
766                         return -1;
767                 }
768         }
769         output->frame = frame_create(c->theme, 100, 100,
770                                      FRAME_BUTTON_CLOSE, title);
771         free(title);
772         if (!output->frame)
773                 return -1;
774
775         if (output->keyboard_count)
776                 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
777
778         wayland_output_resize_surface(output);
779
780         wl_shell_surface_set_toplevel(output->parent.shell_surface);
781
782         return 0;
783 }
784
785 static void
786 wayland_output_set_fullscreen(struct wayland_output *output,
787                               enum wl_shell_surface_fullscreen_method method,
788                               uint32_t framerate, struct wl_output *target)
789 {
790         struct wayland_compositor *c =
791                 (struct wayland_compositor *)output->base.compositor;
792
793         if (output->frame) {
794                 frame_destroy(output->frame);
795                 output->frame = NULL;
796         }
797
798         wayland_output_resize_surface(output);
799
800         if (output->parent.shell_surface) {
801                 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
802                                                 method, framerate, target);
803         } else if (c->parent.fshell) {
804                 _wl_fullscreen_shell_present_surface(c->parent.fshell,
805                                                      output->parent.surface,
806                                                      method, target);
807         }
808 }
809
810 static struct weston_mode *
811 wayland_output_choose_mode(struct wayland_output *output,
812                            struct weston_mode *ref_mode)
813 {
814         struct weston_mode *mode;
815
816         /* First look for an exact match */
817         wl_list_for_each(mode, &output->base.mode_list, link)
818                 if (mode->width == ref_mode->width &&
819                     mode->height == ref_mode->height &&
820                     mode->refresh == ref_mode->refresh)
821                         return mode;
822
823         /* If we can't find an exact match, ignore refresh and try again */
824         wl_list_for_each(mode, &output->base.mode_list, link)
825                 if (mode->width == ref_mode->width &&
826                     mode->height == ref_mode->height)
827                         return mode;
828
829         /* Yeah, we failed */
830         return NULL;
831 }
832
833 enum mode_status {
834         MODE_STATUS_UNKNOWN,
835         MODE_STATUS_SUCCESS,
836         MODE_STATUS_FAIL,
837         MODE_STATUS_CANCEL,
838 };
839
840 static void
841 mode_feedback_successful(void *data,
842                          struct _wl_fullscreen_shell_mode_feedback *fb)
843 {
844         enum mode_status *value = data;
845
846         printf("Mode switch successful\n");
847
848         *value = MODE_STATUS_SUCCESS;
849 }
850
851 static void
852 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
853 {
854         enum mode_status *value = data;
855
856         printf("Mode switch failed\n");
857
858         *value = MODE_STATUS_FAIL;
859 }
860
861 static void
862 mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
863 {
864         enum mode_status *value = data;
865
866         printf("Mode switch cancelled\n");
867
868         *value = MODE_STATUS_CANCEL;
869 }
870
871 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
872         mode_feedback_successful,
873         mode_feedback_failed,
874         mode_feedback_cancelled,
875 };
876
877 static int
878 wayland_output_switch_mode(struct weston_output *output_base,
879                            struct weston_mode *mode)
880 {
881         struct wayland_output *output = (struct wayland_output *) output_base;
882         struct wayland_compositor *c;
883         struct wl_surface *old_surface;
884         struct weston_mode *old_mode;
885         struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
886         enum mode_status mode_status;
887         int ret = 0;
888
889         if (output_base == NULL) {
890                 weston_log("output is NULL.\n");
891                 return -1;
892         }
893
894         if (mode == NULL) {
895                 weston_log("mode is NULL.\n");
896                 return -1;
897         }
898
899         c = (struct wayland_compositor *)output_base->compositor;
900
901         if (output->parent.shell_surface || !c->parent.fshell)
902                 return -1;
903
904         mode = wayland_output_choose_mode(output, mode);
905         if (mode == NULL)
906                 return -1;
907
908         if (output->base.current_mode == mode)
909                 return 0;
910
911         old_mode = output->base.current_mode;
912         old_surface = output->parent.surface;
913         output->base.current_mode = mode;
914         output->parent.surface =
915                 wl_compositor_create_surface(c->parent.compositor);
916         wl_surface_set_user_data(output->parent.surface, output);
917
918         /* Blow the old buffers because we changed size/surfaces */
919         wayland_output_resize_surface(output);
920
921         mode_feedback =
922                 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
923                                                               output->parent.surface,
924                                                               output->parent.output,
925                                                               mode->refresh);
926         _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
927                                                         &mode_feedback_listener,
928                                                         &mode_status);
929
930         /* This should kick-start things again */
931         output->parent.draw_initial_frame = 1;
932         wayland_output_start_repaint_loop(&output->base);
933
934         mode_status = MODE_STATUS_UNKNOWN;
935         while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
936                 ret = wl_display_dispatch(c->parent.wl_display);
937
938         _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
939
940         if (mode_status == MODE_STATUS_FAIL) {
941                 output->base.current_mode = old_mode;
942                 wl_surface_destroy(output->parent.surface);
943                 output->parent.surface = old_surface;
944                 wayland_output_resize_surface(output);
945
946                 return -1;
947         }
948
949         old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
950         output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
951
952         if (c->use_pixman) {
953                 pixman_renderer_output_destroy(output_base);
954                 if (wayland_output_init_pixman_renderer(output) < 0)
955                         goto err_output;
956         } else {
957                 gl_renderer->output_destroy(output_base);
958                 wl_egl_window_destroy(output->gl.egl_window);
959                 if (wayland_output_init_gl_renderer(output) < 0)
960                         goto err_output;
961         }
962         wl_surface_destroy(old_surface);
963
964         weston_output_schedule_repaint(&output->base);
965
966         return 0;
967
968 err_output:
969         /* XXX */
970         return -1;
971 }
972
973 static struct wayland_output *
974 wayland_output_create(struct wayland_compositor *c, int x, int y,
975                       int width, int height, const char *name, int fullscreen,
976                       uint32_t transform, int32_t scale)
977 {
978         struct wayland_output *output;
979         int output_width, output_height;
980
981         weston_log("Creating %dx%d wayland output at (%d, %d)\n",
982                    width, height, x, y);
983
984         output = zalloc(sizeof *output);
985         if (output == NULL)
986                 return NULL;
987
988         output->name = name ? strdup(name) : NULL;
989         output->base.make = "waywayland";
990         output->base.model = "none";
991
992         output_width = width * scale;
993         output_height = height * scale;
994
995         output->parent.surface =
996                 wl_compositor_create_surface(c->parent.compositor);
997         if (!output->parent.surface)
998                 goto err_name;
999         wl_surface_set_user_data(output->parent.surface, output);
1000
1001         output->parent.draw_initial_frame = 1;
1002
1003         if (c->parent.shell) {
1004                 output->parent.shell_surface =
1005                         wl_shell_get_shell_surface(c->parent.shell,
1006                                                    output->parent.surface);
1007                 if (!output->parent.shell_surface)
1008                         goto err_surface;
1009                 wl_shell_surface_add_listener(output->parent.shell_surface,
1010                                               &shell_surface_listener, output);
1011         }
1012
1013         if (fullscreen && c->parent.shell) {
1014                 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1015                                                 0, 0, NULL);
1016                 wl_display_roundtrip(c->parent.wl_display);
1017                 if (!width)
1018                         output_width = output->parent.configure_width;
1019                 if (!height)
1020                         output_height = output->parent.configure_height;
1021         }
1022
1023         output->mode.flags =
1024                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1025         output->mode.width = output_width;
1026         output->mode.height = output_height;
1027         output->mode.refresh = 60000;
1028         output->scale = scale;
1029         wl_list_init(&output->base.mode_list);
1030         wl_list_insert(&output->base.mode_list, &output->mode.link);
1031         output->base.current_mode = &output->mode;
1032
1033         wl_list_init(&output->shm.buffers);
1034         wl_list_init(&output->shm.free_buffers);
1035
1036         weston_output_init(&output->base, &c->base, x, y, width, height,
1037                            transform, scale);
1038
1039         if (c->use_pixman) {
1040                 if (wayland_output_init_pixman_renderer(output) < 0)
1041                         goto err_output;
1042                 output->base.repaint = wayland_output_repaint_pixman;
1043         } else {
1044                 if (wayland_output_init_gl_renderer(output) < 0)
1045                         goto err_output;
1046                 output->base.repaint = wayland_output_repaint_gl;
1047         }
1048
1049         output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1050         output->base.destroy = wayland_output_destroy;
1051         output->base.assign_planes = NULL;
1052         output->base.set_backlight = NULL;
1053         output->base.set_dpms = NULL;
1054         output->base.switch_mode = wayland_output_switch_mode;
1055
1056         wl_list_insert(c->base.output_list.prev, &output->base.link);
1057
1058         return output;
1059
1060 err_output:
1061         weston_output_destroy(&output->base);
1062         if (output->parent.shell_surface)
1063                 wl_shell_surface_destroy(output->parent.shell_surface);
1064 err_surface:
1065         wl_surface_destroy(output->parent.surface);
1066 err_name:
1067         free(output->name);
1068
1069         /* FIXME: cleanup weston_output */
1070         free(output);
1071
1072         return NULL;
1073 }
1074
1075 static struct wayland_output *
1076 wayland_output_create_for_config(struct wayland_compositor *c,
1077                                  struct weston_config_section *config_section,
1078                                  int option_width, int option_height,
1079                                  int option_scale, int32_t x, int32_t y)
1080 {
1081         struct wayland_output *output;
1082         char *mode, *t, *name, *str;
1083         int width, height, scale;
1084         uint32_t transform;
1085         unsigned int i, slen;
1086
1087         static const struct { const char *name; uint32_t token; } transform_names[] = {
1088                 { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
1089                 { "90",         WL_OUTPUT_TRANSFORM_90 },
1090                 { "180",        WL_OUTPUT_TRANSFORM_180 },
1091                 { "270",        WL_OUTPUT_TRANSFORM_270 },
1092                 { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
1093                 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1094                 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1095                 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1096         };
1097
1098         weston_config_section_get_string(config_section, "name", &name, NULL);
1099         if (name) {
1100                 slen = strlen(name);
1101                 slen += strlen(WINDOW_TITLE " - ");
1102                 str = malloc(slen + 1);
1103                 if (str)
1104                         snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1105                 free(name);
1106                 name = str;
1107         }
1108         if (!name)
1109                 name = strdup(WINDOW_TITLE);
1110
1111         weston_config_section_get_string(config_section,
1112                                          "mode", &mode, "1024x600");
1113         if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1114                 weston_log("Invalid mode \"%s\" for output %s\n",
1115                            mode, name);
1116                 width = 1024;
1117                 height = 640;
1118         }
1119         free(mode);
1120
1121         if (option_width)
1122                 width = option_width;
1123         if (option_height)
1124                 height = option_height;
1125
1126         weston_config_section_get_int(config_section, "scale", &scale, 1);
1127
1128         if (option_scale)
1129                 scale = option_scale;
1130
1131         weston_config_section_get_string(config_section,
1132                                          "transform", &t, "normal");
1133         transform = WL_OUTPUT_TRANSFORM_NORMAL;
1134         for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1135                 if (strcmp(transform_names[i].name, t) == 0) {
1136                         transform = transform_names[i].token;
1137                         break;
1138                 }
1139         }
1140         if (i >= ARRAY_LENGTH(transform_names))
1141                 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1142         free(t);
1143
1144         output = wayland_output_create(c, x, y, width, height, name, 0,
1145                                        transform, scale);
1146         free(name);
1147
1148         return output;
1149 }
1150
1151 static struct wayland_output *
1152 wayland_output_create_for_parent_output(struct wayland_compositor *c,
1153                                         struct wayland_parent_output *poutput)
1154 {
1155         struct wayland_output *output;
1156         struct weston_mode *mode;
1157         int32_t x;
1158
1159         if(poutput->output) {
1160                 weston_log("parent output already exists\n");
1161                 return poutput->output;
1162         }
1163
1164         if (poutput->current_mode) {
1165                 mode = poutput->current_mode;
1166         } else if (poutput->preferred_mode) {
1167                 mode = poutput->preferred_mode;
1168         } else if (!wl_list_empty(&poutput->mode_list)) {
1169                 mode = container_of(poutput->mode_list.next,
1170                                     struct weston_mode, link);
1171         } else {
1172                 weston_log("No valid modes found.  Skipping output\n");
1173                 return NULL;
1174         }
1175
1176         if (!wl_list_empty(&c->base.output_list)) {
1177                 output = container_of(c->base.output_list.prev,
1178                                       struct wayland_output, base.link);
1179                 x = output->base.x + output->base.current_mode->width;
1180         } else {
1181                 x = 0;
1182         }
1183
1184         output = wayland_output_create(c, x, 0, mode->width, mode->height,
1185                                        NULL, 0,
1186                                        WL_OUTPUT_TRANSFORM_NORMAL, 1);
1187         if (!output)
1188                 return NULL;
1189
1190         poutput->output = output;
1191         output->parent.output = poutput->global;
1192
1193         output->base.make = poutput->physical.make;
1194         output->base.model = poutput->physical.model;
1195         wl_list_init(&output->base.mode_list);
1196         wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1197         wl_list_init(&poutput->mode_list);
1198
1199         wayland_output_set_fullscreen(output,
1200                                       WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1201                                       mode->refresh, poutput->global);
1202
1203         if (output->parent.shell_surface) {
1204                 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1205                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1206                                                 mode->refresh, poutput->global);
1207         } else if (c->parent.fshell) {
1208                 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1209                                                      output->parent.surface,
1210                                                      _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1211                                                      poutput->global);
1212                 _wl_fullscreen_shell_mode_feedback_destroy(
1213                         _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1214                                                                       output->parent.surface,
1215                                                                       poutput->global,
1216                                                                       mode->refresh));
1217         }
1218
1219         return output;
1220 }
1221
1222 static void
1223 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1224                    uint32_t serial)
1225 {
1226         wl_shell_surface_pong(shell_surface, serial);
1227 }
1228
1229 static void
1230 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1231                         uint32_t edges, int32_t width, int32_t height)
1232 {
1233         struct wayland_output *output = data;
1234
1235         output->parent.configure_width = width;
1236         output->parent.configure_height = height;
1237
1238         /* FIXME: implement resizing */
1239 }
1240
1241 static void
1242 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1243 {
1244 }
1245
1246 static const struct wl_shell_surface_listener shell_surface_listener = {
1247         shell_surface_ping,
1248         shell_surface_configure,
1249         shell_surface_popup_done
1250 };
1251
1252 /* Events received from the wayland-server this compositor is client of: */
1253
1254 /* parent input interface */
1255 static void
1256 input_set_cursor(struct wayland_input *input)
1257 {
1258
1259         struct wl_buffer *buffer;
1260         struct wl_cursor_image *image;
1261
1262         if (!input->compositor->cursor)
1263                 return; /* Couldn't load the cursor. Can't set it */
1264
1265         image = input->compositor->cursor->images[0];
1266         buffer = wl_cursor_image_get_buffer(image);
1267         if (!buffer)
1268                 return;
1269
1270         wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1271                               input->parent.cursor.surface,
1272                               image->hotspot_x, image->hotspot_y);
1273
1274         wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1275         wl_surface_damage(input->parent.cursor.surface, 0, 0,
1276                           image->width, image->height);
1277         wl_surface_commit(input->parent.cursor.surface);
1278 }
1279
1280 static void
1281 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1282                            uint32_t serial, struct wl_surface *surface,
1283                            wl_fixed_t x, wl_fixed_t y)
1284 {
1285         struct wayland_input *input = data;
1286         int32_t fx, fy;
1287         enum theme_location location;
1288
1289         /* XXX: If we get a modifier event immediately before the focus,
1290          *      we should try to keep the same serial. */
1291         input->enter_serial = serial;
1292         input->output = wl_surface_get_user_data(surface);
1293
1294         if (input->output->frame) {
1295                 location = frame_pointer_enter(input->output->frame, input,
1296                                                wl_fixed_to_int(x),
1297                                                wl_fixed_to_int(y));
1298                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1299                 x -= wl_fixed_from_int(fx);
1300                 y -= wl_fixed_from_int(fy);
1301
1302                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1303                         weston_output_schedule_repaint(&input->output->base);
1304         } else {
1305                 location = THEME_LOCATION_CLIENT_AREA;
1306         }
1307
1308         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1309
1310         if (location == THEME_LOCATION_CLIENT_AREA) {
1311                 input->focus = 1;
1312                 notify_pointer_focus(&input->base, &input->output->base, x, y);
1313                 wl_pointer_set_cursor(input->parent.pointer,
1314                                       input->enter_serial, NULL, 0, 0);
1315         } else {
1316                 input->focus = 0;
1317                 notify_pointer_focus(&input->base, NULL, 0, 0);
1318                 input_set_cursor(input);
1319         }
1320 }
1321
1322 static void
1323 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1324                            uint32_t serial, struct wl_surface *surface)
1325 {
1326         struct wayland_input *input = data;
1327
1328         if (input->output->frame) {
1329                 frame_pointer_leave(input->output->frame, input);
1330
1331                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1332                         weston_output_schedule_repaint(&input->output->base);
1333         }
1334
1335         notify_pointer_focus(&input->base, NULL, 0, 0);
1336         input->output = NULL;
1337         input->focus = 0;
1338 }
1339
1340 static void
1341 input_handle_motion(void *data, struct wl_pointer *pointer,
1342                     uint32_t time, wl_fixed_t x, wl_fixed_t y)
1343 {
1344         struct wayland_input *input = data;
1345         int32_t fx, fy;
1346         enum theme_location location;
1347
1348         if (input->output->frame) {
1349                 location = frame_pointer_motion(input->output->frame, input,
1350                                                 wl_fixed_to_int(x),
1351                                                 wl_fixed_to_int(y));
1352                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1353                 x -= wl_fixed_from_int(fx);
1354                 y -= wl_fixed_from_int(fy);
1355
1356                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1357                         weston_output_schedule_repaint(&input->output->base);
1358         } else {
1359                 location = THEME_LOCATION_CLIENT_AREA;
1360         }
1361
1362         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1363
1364         if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1365                 input_set_cursor(input);
1366                 notify_pointer_focus(&input->base, NULL, 0, 0);
1367                 input->focus = 0;
1368         } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1369                 wl_pointer_set_cursor(input->parent.pointer,
1370                                       input->enter_serial, NULL, 0, 0);
1371                 notify_pointer_focus(&input->base, &input->output->base, x, y);
1372                 input->focus = 1;
1373         }
1374
1375         if (location == THEME_LOCATION_CLIENT_AREA)
1376                 notify_motion_absolute(&input->base, time, x, y);
1377 }
1378
1379 static void
1380 input_handle_button(void *data, struct wl_pointer *pointer,
1381                     uint32_t serial, uint32_t time, uint32_t button,
1382                     uint32_t state_w)
1383 {
1384         struct wayland_input *input = data;
1385         enum wl_pointer_button_state state = state_w;
1386         enum frame_button_state fstate;
1387         enum theme_location location;
1388
1389         if (input->output->frame) {
1390                 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1391                         FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1392
1393                 location = frame_pointer_button(input->output->frame, input,
1394                                                 button, fstate);
1395
1396                 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1397
1398                         wl_shell_surface_move(input->output->parent.shell_surface,
1399                                               input->parent.seat, serial);
1400                         frame_status_clear(input->output->frame,
1401                                            FRAME_STATUS_MOVE);
1402                         return;
1403                 }
1404
1405                 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1406                         wl_display_terminate(input->compositor->base.wl_display);
1407
1408                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1409                         weston_output_schedule_repaint(&input->output->base);
1410         } else {
1411                 location = THEME_LOCATION_CLIENT_AREA;
1412         }
1413
1414         if (location == THEME_LOCATION_CLIENT_AREA)
1415                 notify_button(&input->base, time, button, state);
1416 }
1417
1418 static void
1419 input_handle_axis(void *data, struct wl_pointer *pointer,
1420                   uint32_t time, uint32_t axis, wl_fixed_t value)
1421 {
1422         struct wayland_input *input = data;
1423
1424         notify_axis(&input->base, time, axis, value);
1425 }
1426
1427 static const struct wl_pointer_listener pointer_listener = {
1428         input_handle_pointer_enter,
1429         input_handle_pointer_leave,
1430         input_handle_motion,
1431         input_handle_button,
1432         input_handle_axis,
1433 };
1434
1435 static void
1436 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1437                     int fd, uint32_t size)
1438 {
1439         struct wayland_input *input = data;
1440         struct xkb_keymap *keymap;
1441         char *map_str;
1442
1443         if (!data) {
1444                 close(fd);
1445                 return;
1446         }
1447
1448         if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1449                 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1450                 if (map_str == MAP_FAILED) {
1451                         weston_log("mmap failed: %m\n");
1452                         goto error;
1453                 }
1454
1455                 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1456                                                     map_str,
1457                                                     XKB_KEYMAP_FORMAT_TEXT_V1,
1458                                                     0);
1459                 munmap(map_str, size);
1460
1461                 if (!keymap) {
1462                         weston_log("failed to compile keymap\n");
1463                         goto error;
1464                 }
1465
1466                 input->keyboard_state_update = STATE_UPDATE_NONE;
1467         } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1468                 weston_log("No keymap provided; falling back to defalt\n");
1469                 keymap = NULL;
1470                 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1471         } else {
1472                 weston_log("Invalid keymap\n");
1473                 goto error;
1474         }
1475
1476         close(fd);
1477
1478         if (input->base.keyboard)
1479                 weston_seat_update_keymap(&input->base, keymap);
1480         else
1481                 weston_seat_init_keyboard(&input->base, keymap);
1482
1483         xkb_keymap_unref(keymap);
1484
1485         return;
1486
1487 error:
1488         wl_keyboard_release(input->parent.keyboard);
1489         close(fd);
1490 }
1491
1492 static void
1493 input_handle_keyboard_enter(void *data,
1494                             struct wl_keyboard *keyboard,
1495                             uint32_t serial,
1496                             struct wl_surface *surface,
1497                             struct wl_array *keys)
1498 {
1499         struct wayland_input *input = data;
1500         struct wayland_output *focus;
1501
1502         focus = input->keyboard_focus;
1503         if (focus) {
1504                 /* This shouldn't happen */
1505                 focus->keyboard_count--;
1506                 if (!focus->keyboard_count && focus->frame)
1507                         frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1508                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1509                         weston_output_schedule_repaint(&focus->base);
1510         }
1511
1512         input->keyboard_focus = wl_surface_get_user_data(surface);
1513         input->keyboard_focus->keyboard_count++;
1514
1515         focus = input->keyboard_focus;
1516         if (focus->frame) {
1517                 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1518                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1519                         weston_output_schedule_repaint(&focus->base);
1520         }
1521
1522
1523         /* XXX: If we get a modifier event immediately before the focus,
1524          *      we should try to keep the same serial. */
1525         notify_keyboard_focus_in(&input->base, keys,
1526                                  STATE_UPDATE_AUTOMATIC);
1527 }
1528
1529 static void
1530 input_handle_keyboard_leave(void *data,
1531                             struct wl_keyboard *keyboard,
1532                             uint32_t serial,
1533                             struct wl_surface *surface)
1534 {
1535         struct wayland_input *input = data;
1536         struct wayland_output *focus;
1537
1538         notify_keyboard_focus_out(&input->base);
1539
1540         focus = input->keyboard_focus;
1541         if (!focus)
1542                 return; /* This shouldn't happen */
1543
1544         focus->keyboard_count--;
1545         if (!focus->keyboard_count && focus->frame) {
1546                 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1547                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1548                         weston_output_schedule_repaint(&focus->base);
1549         }
1550
1551         input->keyboard_focus = NULL;
1552 }
1553
1554 static void
1555 input_handle_key(void *data, struct wl_keyboard *keyboard,
1556                  uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1557 {
1558         struct wayland_input *input = data;
1559
1560         input->key_serial = serial;
1561         notify_key(&input->base, time, key,
1562                    state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1563                            WL_KEYBOARD_KEY_STATE_RELEASED,
1564                    input->keyboard_state_update);
1565 }
1566
1567 static void
1568 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1569                        uint32_t serial_in, uint32_t mods_depressed,
1570                        uint32_t mods_latched, uint32_t mods_locked,
1571                        uint32_t group)
1572 {
1573         struct wayland_input *input = data;
1574         struct wayland_compositor *c = input->compositor;
1575         uint32_t serial_out;
1576
1577         /* If we get a key event followed by a modifier event with the
1578          * same serial number, then we try to preserve those semantics by
1579          * reusing the same serial number on the way out too. */
1580         if (serial_in == input->key_serial)
1581                 serial_out = wl_display_get_serial(c->base.wl_display);
1582         else
1583                 serial_out = wl_display_next_serial(c->base.wl_display);
1584
1585         xkb_state_update_mask(input->base.keyboard->xkb_state.state,
1586                               mods_depressed, mods_latched,
1587                               mods_locked, 0, 0, group);
1588         notify_modifiers(&input->base, serial_out);
1589 }
1590
1591 static void
1592 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1593                          int32_t rate, int32_t delay)
1594 {
1595         struct wayland_input *input = data;
1596         struct wayland_compositor *c = input->compositor;
1597
1598         c->base.kb_repeat_rate = rate;
1599         c->base.kb_repeat_delay = delay;
1600 }
1601
1602 static const struct wl_keyboard_listener keyboard_listener = {
1603         input_handle_keymap,
1604         input_handle_keyboard_enter,
1605         input_handle_keyboard_leave,
1606         input_handle_key,
1607         input_handle_modifiers,
1608         input_handle_repeat_info,
1609 };
1610
1611 static void
1612 input_capabilities_updated(struct wayland_input *input)
1613 {
1614         if ((input->caps & WL_SEAT_CAPABILITY_POINTER) &&
1615                         !input->parent.pointer) {
1616                 input->parent.pointer = wl_seat_get_pointer(input->parent.seat);
1617                 wl_pointer_set_user_data(input->parent.pointer, input);
1618                 wl_pointer_add_listener(input->parent.pointer,
1619                                         &pointer_listener, input);
1620                 weston_seat_init_pointer(&input->base);
1621         } else if (!(input->caps & WL_SEAT_CAPABILITY_POINTER) &&
1622                         input->parent.pointer) {
1623                 wl_pointer_destroy(input->parent.pointer);
1624                 input->parent.pointer = NULL;
1625         }
1626
1627         if ((input->caps & WL_SEAT_CAPABILITY_KEYBOARD) &&
1628                         !input->parent.keyboard) {
1629                 input->parent.keyboard = wl_seat_get_keyboard(
1630                                 input->parent.seat);
1631                 wl_keyboard_set_user_data(input->parent.keyboard, input);
1632                 wl_keyboard_add_listener(input->parent.keyboard,
1633                                          &keyboard_listener, input);
1634         } else if (!(input->caps & WL_SEAT_CAPABILITY_KEYBOARD) &&
1635                         input->parent.keyboard) {
1636                 wl_keyboard_destroy(input->parent.keyboard);
1637                 input->parent.keyboard = NULL;
1638         }
1639         input->caps_update_required = 0;
1640 }
1641
1642 static void
1643 input_handle_capabilities(void *data, struct wl_seat *seat,
1644                           enum wl_seat_capability caps)
1645 {
1646         struct wayland_input *input = data;
1647
1648         input->caps = caps;
1649         weston_log ("input_handle_capabilities input with seatname %s"
1650                 " and caps %d\n", input->base.seat_name, input->caps);
1651         if (input->base.seat_name || !input->compositor->seatfilter) {
1652                 input_capabilities_updated (input);
1653         } else {
1654                 input->caps_update_required = 1;
1655         }
1656 }
1657
1658 static void
1659 input_handle_name(void *data, struct wl_seat *seat,
1660                   const char *name)
1661 {
1662         struct wayland_input *input = data;
1663         if (input->compositor->seatfilter &&
1664                 (!name || !input->compositor->seatname ||
1665                 strcmp(input->compositor->seatname, name) != 0)) {
1666                 weston_log("seatname does not match %s:%s; remove "
1667                         "input\n", name, input->compositor->seatname);
1668                 wl_seat_destroy(input->parent.seat);
1669                 wl_list_remove(&input->link);
1670                 free(input);
1671                 return;
1672         }
1673         if (!input->initialized) {
1674                 weston_log ("initialise input with seat name %s\n", name);
1675                 weston_seat_init (&input->base, &input->compositor->base, name);
1676                 input->parent.cursor.surface = wl_compositor_create_surface(
1677                                 input->compositor->parent.compositor);
1678                 input->initialized = 1;
1679         }
1680         if (input->caps_update_required)
1681                 input_capabilities_updated (input);
1682 }
1683
1684 static const struct wl_seat_listener seat_listener = {
1685         input_handle_capabilities,
1686         input_handle_name,
1687 };
1688
1689 static void
1690 display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
1691 {
1692         struct wayland_input *input;
1693
1694         input = zalloc(sizeof *input);
1695         if (input == NULL)
1696                 return;
1697
1698         weston_log ("create input with id %d\n", id);
1699         input->compositor = c;
1700         if (c->seatfilter)
1701                 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1702                                               &wl_seat_interface,
1703                                               version > 2 ? version : 2);
1704         else
1705                 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1706                                       &wl_seat_interface, MIN(version, 4));
1707         input->base.seat_name = NULL;
1708         input->initialized = 0;
1709
1710         wl_list_insert(c->input_list.prev, &input->link);
1711
1712         wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1713         wl_seat_set_user_data(input->parent.seat, input);
1714
1715         if (!input->compositor->seatfilter) {
1716                 weston_log ("initialise input with seat name %s\n",
1717                                 c->seatname);
1718                 weston_seat_init (&input->base, &input->compositor->base,
1719                                 c->seatname ? c->seatname : "");
1720                 input->parent.cursor.surface = wl_compositor_create_surface(
1721                                 c->parent.compositor);
1722                 input->initialized = 1;
1723         }
1724 }
1725
1726 static void
1727 wayland_parent_output_destroy(struct wayland_parent_output *output)
1728 {
1729         struct weston_mode *mode, *next;
1730
1731         if (output->output)
1732                 wayland_output_destroy(&output->output->base);
1733
1734         wl_output_destroy(output->global);
1735         free(output->physical.make);
1736         free(output->physical.model);
1737         free(output->seatname);
1738
1739         wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1740                 wl_list_remove(&mode->link);
1741                 free(mode);
1742         }
1743 }
1744
1745 static void
1746 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1747                                int32_t x, int32_t y,
1748                                int32_t physical_width, int32_t physical_height,
1749                                int32_t subpixel, const char *make,
1750                                const char *model, int32_t transform)
1751 {
1752         struct wayland_parent_output *output = data;
1753
1754         output->x = x;
1755         output->y = y;
1756         output->physical.width = physical_width;
1757         output->physical.height = physical_height;
1758         output->physical.subpixel = subpixel;
1759
1760         free(output->physical.make);
1761         output->physical.make = strdup(make);
1762         free(output->physical.model);
1763         output->physical.model = strdup(model);
1764
1765         output->transform = transform;
1766 }
1767
1768 static struct weston_mode *
1769 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1770 {
1771         struct weston_mode *mode;
1772
1773         wl_list_for_each(mode, list, link) {
1774                 if (mode->width == width && mode->height == height &&
1775                     mode->refresh == refresh)
1776                         return mode;
1777         }
1778
1779         mode = zalloc(sizeof *mode);
1780         if (!mode)
1781                 return NULL;
1782
1783         mode->width = width;
1784         mode->height = height;
1785         mode->refresh = refresh;
1786         wl_list_insert(list, &mode->link);
1787
1788         return mode;
1789 }
1790
1791 static void
1792 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1793                            uint32_t flags, int32_t width, int32_t height,
1794                            int32_t refresh)
1795 {
1796         struct wayland_parent_output *output = data;
1797         struct weston_mode *mode;
1798
1799         if (output->output) {
1800                 mode = find_mode(&output->output->base.mode_list,
1801                                  width, height, refresh);
1802                 if (!mode)
1803                         return;
1804                 mode->flags = flags;
1805                 /* Do a mode-switch on current mode change? */
1806         } else {
1807                 mode = find_mode(&output->mode_list, width, height, refresh);
1808                 if (!mode)
1809                         return;
1810                 mode->flags = flags;
1811                 if (flags & WL_OUTPUT_MODE_CURRENT)
1812                         output->current_mode = mode;
1813                 if (flags & WL_OUTPUT_MODE_PREFERRED)
1814                         output->preferred_mode = mode;
1815         }
1816 }
1817
1818 #if HAVE_MULTISEAT
1819 static void
1820 wayland_parent_output_done(void *data,
1821                    struct wl_output *wl_output)
1822 {
1823         struct wayland_parent_output *output = data;
1824         if (!output->seatname || !output->c || !output->c->seatname ||
1825                 strcmp(output->c->seatname, output->seatname) != 0) {
1826                 weston_log("output seatname (%s) doesnt match with"
1827                                 " compositor seatname. destroying output\n",
1828                                 output->seatname);
1829                 wayland_parent_output_destroy (output);
1830                 wl_list_remove(&output->link);
1831                 return;
1832         }
1833         if (output->c->sprawl_across_outputs) {
1834                 wayland_output_create_for_parent_output(output->c, output);
1835         }
1836 }
1837
1838 static void
1839 wayland_parent_output_scale(void *data,
1840                     struct wl_output *wl_output,
1841                     int32_t scale)
1842 {
1843 }
1844
1845 static void
1846 wayland_parent_output_name(void *data, struct wl_output *wl_output_proxy,
1847         const char *name)
1848 {
1849         weston_log("output name %s\n", name);
1850 }
1851
1852 static void
1853 wayland_parent_output_seatname(void *data, struct wl_output *wl_output_proxy,
1854                const char *name)
1855 {
1856         struct wayland_parent_output *poutput = data;
1857         weston_log("output seatname %s\n", name);
1858         poutput->seatname = strdup(name);
1859 }
1860 #endif
1861
1862 static const struct wl_output_listener output_listener = {
1863         wayland_parent_output_geometry,
1864         wayland_parent_output_mode
1865 #if HAVE_MULTISEAT
1866         ,
1867         wayland_parent_output_done,
1868         wayland_parent_output_scale,
1869         wayland_parent_output_name,
1870         wayland_parent_output_seatname
1871 #endif
1872 };
1873
1874 static void
1875 wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1876 {
1877         struct wayland_parent_output *output;
1878
1879         output = zalloc(sizeof *output);
1880         if (!output)
1881                 return;
1882
1883         output->id = id;
1884
1885         if (c->seatfilter)
1886                 output->global = wl_registry_bind(c->parent.registry, id,
1887                                           &wl_output_interface, 3);
1888         else
1889                 output->global = wl_registry_bind(c->parent.registry, id,
1890                                           &wl_output_interface, 1);
1891         if (!output->global) {
1892                 free(output);
1893                 return;
1894         }
1895
1896         output->c = c;
1897         output->seatname = NULL;
1898         output->output = NULL;
1899         wl_output_add_listener(output->global, &output_listener, output);
1900
1901         output->scale = 0;
1902         output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1903         output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1904         wl_list_init(&output->mode_list);
1905         wl_list_insert(&c->parent.output_list, &output->link);
1906
1907         if (!c->seatfilter && output->c->sprawl_across_outputs) {
1908                 wl_display_roundtrip(output->c->parent.wl_display);
1909                 wayland_output_create_for_parent_output(output->c, output);
1910         }
1911 }
1912
1913 static void
1914 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1915                        const char *interface, uint32_t version)
1916 {
1917         struct wayland_compositor *c = data;
1918
1919         if (strcmp(interface, "wl_compositor") == 0) {
1920                 c->parent.compositor =
1921                         wl_registry_bind(registry, name,
1922                                          &wl_compositor_interface, 1);
1923         } else if (strcmp(interface, "wl_shell") == 0) {
1924                 c->parent.shell =
1925                         wl_registry_bind(registry, name,
1926                                          &wl_shell_interface, 1);
1927         } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1928                 c->parent.fshell =
1929                         wl_registry_bind(registry, name,
1930                                          &_wl_fullscreen_shell_interface, 1);
1931         } else if (strcmp(interface, "wl_seat") == 0) {
1932                 display_add_seat(c, name, version);
1933         } else if (strcmp(interface, "wl_output") == 0) {
1934                 wayland_compositor_register_output(c, name);
1935         } else if (strcmp(interface, "wl_shm") == 0) {
1936                 c->parent.shm =
1937                         wl_registry_bind(registry, name, &wl_shm_interface, 1);
1938         }
1939 }
1940
1941 static void
1942 registry_handle_global_remove(void *data, struct wl_registry *registry,
1943                               uint32_t name)
1944 {
1945         struct wayland_compositor *c = data;
1946         struct wayland_parent_output *output;
1947
1948         wl_list_for_each(output, &c->parent.output_list, link)
1949                 if (output->id == name)
1950                         wayland_parent_output_destroy(output);
1951 }
1952
1953 static const struct wl_registry_listener registry_listener = {
1954         registry_handle_global,
1955         registry_handle_global_remove
1956 };
1957
1958 static int
1959 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1960 {
1961         struct wayland_compositor *c = data;
1962         int count = 0;
1963
1964         if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1965                 wl_display_terminate(c->base.wl_display);
1966                 return 0;
1967         }
1968
1969         if (mask & WL_EVENT_READABLE)
1970                 count = wl_display_dispatch(c->parent.wl_display);
1971         if (mask & WL_EVENT_WRITABLE)
1972                 wl_display_flush(c->parent.wl_display);
1973
1974         if (mask == 0) {
1975                 count = wl_display_dispatch_pending(c->parent.wl_display);
1976                 wl_display_flush(c->parent.wl_display);
1977         }
1978
1979         return count;
1980 }
1981
1982 static void
1983 wayland_restore(struct weston_compositor *ec)
1984 {
1985 }
1986
1987 static void
1988 wayland_destroy(struct weston_compositor *ec)
1989 {
1990         struct wayland_compositor *c = (struct wayland_compositor *) ec;
1991
1992         weston_compositor_shutdown(ec);
1993
1994         if (c->parent.shm)
1995                 wl_shm_destroy(c->parent.shm);
1996
1997         free(ec);
1998 }
1999
2000 static const char *left_ptrs[] = {
2001         "left_ptr",
2002         "default",
2003         "top_left_arrow",
2004         "left-arrow"
2005 };
2006
2007 static void
2008 create_cursor(struct wayland_compositor *c, struct weston_config *config)
2009 {
2010         struct weston_config_section *s;
2011         int size;
2012         char *theme = NULL;
2013         unsigned int i;
2014
2015         s = weston_config_get_section(config, "shell", NULL, NULL);
2016         weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
2017         weston_config_section_get_int(s, "cursor-size", &size, 32);
2018
2019         c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
2020         if (!c->cursor_theme) {
2021                 fprintf(stderr, "could not load cursor theme\n");
2022                 return;
2023         }
2024
2025         free(theme);
2026
2027         c->cursor = NULL;
2028         for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2029                 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
2030                                                        left_ptrs[i]);
2031         if (!c->cursor) {
2032                 fprintf(stderr, "could not load left cursor\n");
2033                 return;
2034         }
2035 }
2036
2037 static void
2038 fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
2039                    void *data)
2040 {
2041         struct wayland_compositor *c = data;
2042         struct wayland_input *input = NULL;
2043
2044         wl_list_for_each(input, &c->input_list, link)
2045                 if (&input->base == seat_base)
2046                         break;
2047
2048         if (!input || !input->output)
2049                 return;
2050
2051         if (input->output->frame)
2052                 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2053         else
2054                 wayland_output_set_windowed(input->output);
2055
2056         weston_output_schedule_repaint(&input->output->base);
2057 }
2058
2059 static struct wayland_compositor *
2060 wayland_compositor_create(struct wl_display *display, int use_pixman,
2061                           const char *display_name, int *argc, char *argv[],
2062                           struct weston_config *config, const char* seatname,
2063                           int seatfilter)
2064 {
2065         struct wayland_compositor *c;
2066         struct wl_event_loop *loop;
2067         int fd;
2068
2069         c = zalloc(sizeof *c);
2070         if (c == NULL)
2071                 return NULL;
2072         c->seatname = seatname ? strdup(seatname) : NULL;
2073         c->seatfilter = seatfilter;
2074
2075         if (weston_compositor_init(&c->base, display, argc, argv,
2076                                    config) < 0)
2077                 goto err_free;
2078
2079         c->parent.wl_display = wl_display_connect(display_name);
2080
2081         if (c->parent.wl_display == NULL) {
2082                 weston_log("failed to create display: %m\n");
2083                 goto err_compositor;
2084         }
2085
2086         wl_list_init(&c->parent.output_list);
2087         wl_list_init(&c->input_list);
2088         c->parent.registry = wl_display_get_registry(c->parent.wl_display);
2089         wl_registry_add_listener(c->parent.registry, &registry_listener, c);
2090         wl_display_roundtrip(c->parent.wl_display);
2091
2092         create_cursor(c, config);
2093
2094         c->base.wl_display = display;
2095
2096         c->use_pixman = use_pixman;
2097
2098         if (!c->use_pixman) {
2099                 gl_renderer = weston_load_module("gl-renderer.so",
2100                                                  "gl_renderer_interface");
2101                 if (!gl_renderer)
2102                         c->use_pixman = 1;
2103         }
2104
2105         if (!c->use_pixman) {
2106                 if (gl_renderer->create(&c->base, c->parent.wl_display,
2107                                 gl_renderer->alpha_attribs,
2108                                 NULL) < 0) {
2109                         weston_log("Failed to initialize the GL renderer; "
2110                                    "falling back to pixman.\n");
2111                         c->use_pixman = 1;
2112                 }
2113         }
2114
2115         if (c->use_pixman) {
2116                 if (pixman_renderer_init(&c->base) < 0) {
2117                         weston_log("Failed to initialize pixman renderer\n");
2118                         goto err_display;
2119                 }
2120         }
2121
2122         c->base.destroy = wayland_destroy;
2123         c->base.restore = wayland_restore;
2124
2125         loop = wl_display_get_event_loop(c->base.wl_display);
2126
2127         fd = wl_display_get_fd(c->parent.wl_display);
2128         c->parent.wl_source =
2129                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2130                                      wayland_compositor_handle_event, c);
2131         if (c->parent.wl_source == NULL)
2132                 goto err_renderer;
2133
2134         wl_event_source_check(c->parent.wl_source);
2135
2136         return c;
2137 err_renderer:
2138         c->base.renderer->destroy(&c->base);
2139 err_display:
2140         wl_display_disconnect(c->parent.wl_display);
2141 err_compositor:
2142         weston_compositor_shutdown(&c->base);
2143 err_free:
2144         free(c->seatname);
2145         free(c);
2146         return NULL;
2147 }
2148
2149 static void
2150 wayland_compositor_destroy(struct wayland_compositor *c)
2151 {
2152         struct weston_output *output, *next;
2153
2154         wl_list_for_each_safe(output, next, &c->base.output_list, link)
2155                 wayland_output_destroy(output);
2156
2157         c->base.renderer->destroy(&c->base);
2158         wl_display_disconnect(c->parent.wl_display);
2159
2160         if (c->theme)
2161                 theme_destroy(c->theme);
2162         if (c->frame_device)
2163                 cairo_device_destroy(c->frame_device);
2164         wl_cursor_theme_destroy(c->cursor_theme);
2165
2166         weston_compositor_shutdown(&c->base);
2167         free(c->seatname);
2168         free(c);
2169 }
2170
2171 WL_EXPORT struct weston_compositor *
2172 backend_init(struct wl_display *display, int *argc, char *argv[],
2173              struct weston_config *config)
2174 {
2175         struct wayland_compositor *c;
2176         struct wayland_output *output;
2177         struct wayland_parent_output *poutput;
2178         struct weston_config_section *section;
2179         int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
2180 #if HAVE_MULTISEAT
2181         int seatfilter = 1;
2182 #else
2183         int seatfilter = 0;
2184 #endif
2185         const char *section_name, *display_name, *seatname;
2186         char *name;
2187
2188         const struct weston_option wayland_options[] = {
2189                 { WESTON_OPTION_INTEGER, "width", 0, &width },
2190                 { WESTON_OPTION_INTEGER, "height", 0, &height },
2191                 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
2192                 { WESTON_OPTION_STRING, "display", 0, &display_name },
2193                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2194                 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
2195                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
2196                 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
2197         };
2198
2199         width = 0;
2200         height = 0;
2201         scale = 0;
2202         display_name = NULL;
2203         use_pixman = 0;
2204         count = 1;
2205         fullscreen = 0;
2206         sprawl = 0;
2207         parse_options(wayland_options,
2208                       ARRAY_LENGTH(wayland_options), argc, argv);
2209
2210         seatname = getenv("XDG_SEAT");
2211         if (!seatname && seatfilter) {
2212                 weston_log("unable to determine seat for the compositor from"
2213                         "XDG_SEAT env. Disabling seat filter\n");
2214                 seatfilter = 0;
2215         }
2216
2217         weston_log("Multiseat enabled %d\n", seatfilter);
2218         c = wayland_compositor_create(display, use_pixman, display_name,
2219                                       argc, argv, config, seatname, seatfilter);
2220         if (!c)
2221                 return NULL;
2222
2223         if (sprawl || c->parent.fshell) {
2224                 c->sprawl_across_outputs = 1;
2225                 wl_display_roundtrip(c->parent.wl_display);
2226                 if (wl_list_empty(&c->parent.output_list))
2227                         return NULL;
2228
2229                 wl_list_for_each(poutput, &c->parent.output_list, link)
2230                         wayland_output_create_for_parent_output(c, poutput);
2231
2232                 return &c->base;
2233         }
2234
2235         if (fullscreen) {
2236                 output = wayland_output_create(c, 0, 0, width, height,
2237                                                NULL, 1, 0, 1);
2238                 if (!output)
2239                         goto err_outputs;
2240
2241                 wayland_output_set_fullscreen(output, 0, 0, NULL);
2242                 return &c->base;
2243         }
2244
2245         section = NULL;
2246         x = 0;
2247         while (weston_config_next_section(config, &section, &section_name)) {
2248                 if (!section_name || strcmp(section_name, "output") != 0)
2249                         continue;
2250                 weston_config_section_get_string(section, "name", &name, NULL);
2251                 if (name == NULL)
2252                         continue;
2253
2254                 if (name[0] != 'W' || name[1] != 'L') {
2255                         free(name);
2256                         continue;
2257                 }
2258                 free(name);
2259
2260                 output = wayland_output_create_for_config(c, section, width,
2261                                                           height, scale, x, 0);
2262                 if (!output)
2263                         goto err_outputs;
2264                 if (wayland_output_set_windowed(output))
2265                         goto err_outputs;
2266
2267                 x += output->base.width;
2268                 --count;
2269         }
2270
2271         if (!width)
2272                 width = 1024;
2273         if (!height)
2274                 height = 640;
2275         if (!scale)
2276                 scale = 1;
2277         while (count > 0) {
2278                 output = wayland_output_create(c, x, 0, width, height,
2279                                                NULL, 0, 0, scale);
2280                 if (!output)
2281                         goto err_outputs;
2282                 if (wayland_output_set_windowed(output))
2283                         goto err_outputs;
2284
2285                 x += width;
2286                 --count;
2287         }
2288
2289         weston_compositor_add_key_binding(&c->base, KEY_F,
2290                                           MODIFIER_CTRL | MODIFIER_ALT,
2291                                           fullscreen_binding, c);
2292
2293         return &c->base;
2294
2295 err_outputs:
2296         wayland_compositor_destroy(c);
2297         return NULL;
2298 }