compositor-wayland: Move config parsing into backend_init
[platform/upstream/weston.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
34 #include <wayland-client.h>
35 #include <wayland-egl.h>
36 #include <wayland-cursor.h>
37
38 #include "compositor.h"
39 #include "gl-renderer.h"
40 #include "pixman-renderer.h"
41 #include "../shared/image-loader.h"
42 #include "../shared/os-compatibility.h"
43 #include "../shared/cairo-util.h"
44
45 #define WINDOW_TITLE "Weston Compositor"
46
47 struct wayland_compositor {
48         struct weston_compositor base;
49
50         struct {
51                 struct wl_display *wl_display;
52                 struct wl_registry *registry;
53                 struct wl_compositor *compositor;
54                 struct wl_shell *shell;
55                 struct wl_shm *shm;
56
57                 struct wl_event_source *wl_source;
58                 uint32_t event_mask;
59         } parent;
60
61         int use_pixman;
62
63         struct theme *theme;
64         cairo_device_t *frame_device;
65         struct wl_cursor_theme *cursor_theme;
66         struct wl_cursor *cursor;
67
68         struct wl_list input_list;
69 };
70
71 struct wayland_output {
72         struct weston_output base;
73
74         struct {
75                 int draw_initial_frame;
76                 struct wl_surface *surface;
77                 struct wl_shell_surface *shell_surface;
78         } parent;
79
80         int keyboard_count;
81
82         struct frame *frame;
83
84         struct {
85                 struct wl_egl_window *egl_window;
86                 struct {
87                         cairo_surface_t *top;
88                         cairo_surface_t *left;
89                         cairo_surface_t *right;
90                         cairo_surface_t *bottom;
91                 } border;
92         } gl;
93
94         struct {
95                 struct wl_list buffers;
96                 struct wl_list free_buffers;
97         } shm;
98
99         struct weston_mode mode;
100         uint32_t scale;
101 };
102
103 struct wayland_shm_buffer {
104         struct wayland_output *output;
105         struct wl_list link;
106         struct wl_list free_link;
107
108         struct wl_buffer *buffer;
109         void *data;
110         size_t size;
111         pixman_region32_t damage;
112         int frame_damaged;
113
114         pixman_image_t *pm_image;
115         cairo_surface_t *c_surface;
116 };
117
118 struct wayland_input {
119         struct weston_seat base;
120         struct wayland_compositor *compositor;
121         struct wl_list link;
122
123         struct {
124                 struct wl_seat *seat;
125                 struct wl_pointer *pointer;
126                 struct wl_keyboard *keyboard;
127                 struct wl_touch *touch;
128
129                 struct {
130                         struct wl_surface *surface;
131                         int32_t hx, hy;
132                 } cursor;
133         } parent;
134
135         uint32_t key_serial;
136         uint32_t enter_serial;
137         int focus;
138         struct wayland_output *output;
139         struct wayland_output *keyboard_focus;
140 };
141
142 struct gl_renderer_interface *gl_renderer;
143
144 static void
145 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
146 {
147         cairo_surface_destroy(buffer->c_surface);
148         pixman_image_unref(buffer->pm_image);
149
150         wl_buffer_destroy(buffer->buffer);
151         munmap(buffer->data, buffer->size);
152
153         pixman_region32_fini(&buffer->damage);
154
155         wl_list_remove(&buffer->link);
156         wl_list_remove(&buffer->free_link);
157         free(buffer);
158 }
159
160 static void
161 buffer_release(void *data, struct wl_buffer *buffer)
162 {
163         struct wayland_shm_buffer *sb = data;
164
165         if (sb->output) {
166                 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
167         } else {
168                 wayland_shm_buffer_destroy(sb);
169         }
170 }
171
172 static const struct wl_buffer_listener buffer_listener = {
173         buffer_release
174 };
175
176 static struct wayland_shm_buffer *
177 wayland_output_get_shm_buffer(struct wayland_output *output)
178 {
179         struct wayland_compositor *c =
180                 (struct wayland_compositor *) output->base.compositor;
181         struct wl_shm *shm = c->parent.shm;
182         struct wayland_shm_buffer *sb;
183
184         struct wl_shm_pool *pool;
185         int width, height, stride;
186         int32_t fx, fy;
187         int fd;
188         unsigned char *data;
189
190         if (!wl_list_empty(&output->shm.free_buffers)) {
191                 sb = container_of(output->shm.free_buffers.next,
192                                   struct wayland_shm_buffer, free_link);
193                 wl_list_remove(&sb->free_link);
194                 wl_list_init(&sb->free_link);
195
196                 return sb;
197         }
198
199         if (output->frame) {
200                 width = frame_width(output->frame);
201                 height = frame_height(output->frame);
202         } else {
203                 width = output->base.current_mode->width;
204                 height = output->base.current_mode->height;
205         }
206
207         stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
208
209         fd = os_create_anonymous_file(height * stride);
210         if (fd < 0) {
211                 perror("os_create_anonymous_file");
212                 return NULL;
213         }
214
215         data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
216         if (data == MAP_FAILED) {
217                 perror("mmap");
218                 close(fd);
219                 return NULL;
220         }
221
222         sb = zalloc(sizeof *sb);
223
224         sb->output = output;
225         wl_list_init(&sb->free_link);
226         wl_list_insert(&output->shm.buffers, &sb->link);
227
228         pixman_region32_init_rect(&sb->damage, 0, 0,
229                                   output->base.width, output->base.height);
230         sb->frame_damaged = 1;
231
232         sb->data = data;
233         sb->size = height * stride;
234
235         pool = wl_shm_create_pool(shm, fd, sb->size);
236
237         sb->buffer = wl_shm_pool_create_buffer(pool, 0,
238                                                width, height,
239                                                stride,
240                                                WL_SHM_FORMAT_ARGB8888);
241         wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
242         wl_shm_pool_destroy(pool);
243         close(fd);
244
245         memset(data, 0, sb->size);
246
247         sb->c_surface =
248                 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
249                                                     width, height, stride);
250
251         fx = 0;
252         fy = 0;
253         if (output->frame)
254                 frame_interior(output->frame, &fx, &fy, 0, 0);
255         sb->pm_image =
256                 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
257                                          (uint32_t *)(data + fy * stride) + fx,
258                                          stride);
259
260         return sb;
261 }
262
263 static void
264 frame_done(void *data, struct wl_callback *callback, uint32_t time)
265 {
266         struct weston_output *output = data;
267
268         wl_callback_destroy(callback);
269         weston_output_finish_frame(output, time);
270 }
271
272 static const struct wl_callback_listener frame_listener = {
273         frame_done
274 };
275
276 static void
277 draw_initial_frame(struct wayland_output *output)
278 {
279         struct wayland_shm_buffer *sb;
280
281         sb = wayland_output_get_shm_buffer(output);
282
283         /* If we are rendering with GL, then orphan it so that it gets
284          * destroyed immediately */
285         if (output->gl.egl_window)
286                 sb->output = NULL;
287
288         wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
289
290         /* We only need to damage some part, as its only transparant
291          * pixels anyway. */
292         wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
293 }
294
295 static void
296 wayland_output_update_gl_border(struct wayland_output *output)
297 {
298         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
299         cairo_t *cr;
300
301         if (!output->frame)
302                 return;
303         if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
304                 return;
305
306         fwidth = frame_width(output->frame);
307         fheight = frame_height(output->frame);
308         frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
309
310         if (!output->gl.border.top)
311                 output->gl.border.top =
312                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
313                                                    fwidth, iy);
314         cr = cairo_create(output->gl.border.top);
315         frame_repaint(output->frame, cr);
316         cairo_destroy(cr);
317         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
318                                        fwidth, iy,
319                                        cairo_image_surface_get_stride(output->gl.border.top) / 4,
320                                        cairo_image_surface_get_data(output->gl.border.top));
321
322
323         if (!output->gl.border.left)
324                 output->gl.border.left =
325                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
326                                                    ix, 1);
327         cr = cairo_create(output->gl.border.left);
328         cairo_translate(cr, 0, -iy);
329         frame_repaint(output->frame, cr);
330         cairo_destroy(cr);
331         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
332                                        ix, 1,
333                                        cairo_image_surface_get_stride(output->gl.border.left) / 4,
334                                        cairo_image_surface_get_data(output->gl.border.left));
335
336
337         if (!output->gl.border.right)
338                 output->gl.border.right =
339                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
340                                                    fwidth - (ix + iwidth), 1);
341         cr = cairo_create(output->gl.border.right);
342         cairo_translate(cr, -(iwidth + ix), -iy);
343         frame_repaint(output->frame, cr);
344         cairo_destroy(cr);
345         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
346                                        fwidth - (ix + iwidth), 1,
347                                        cairo_image_surface_get_stride(output->gl.border.right) / 4,
348                                        cairo_image_surface_get_data(output->gl.border.right));
349
350
351         if (!output->gl.border.bottom)
352                 output->gl.border.bottom =
353                         cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
354                                                    fwidth, fheight - (iy + iheight));
355         cr = cairo_create(output->gl.border.bottom);
356         cairo_translate(cr, 0, -(iy + iheight));
357         frame_repaint(output->frame, cr);
358         cairo_destroy(cr);
359         gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
360                                        fwidth, fheight - (iy + iheight),
361                                        cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
362                                        cairo_image_surface_get_data(output->gl.border.bottom));
363 }
364
365 static void
366 wayland_output_start_repaint_loop(struct weston_output *output_base)
367 {
368         struct wayland_output *output = (struct wayland_output *) output_base;
369         struct wayland_compositor *wc =
370                 (struct wayland_compositor *)output->base.compositor;
371         struct wl_callback *callback;
372
373         /* If this is the initial frame, we need to attach a buffer so that
374          * the compositor can map the surface and include it in its render
375          * loop. If the surface doesn't end up in the render loop, the frame
376          * callback won't be invoked. The buffer is transparent and of the
377          * same size as the future real output buffer. */
378         if (output->parent.draw_initial_frame) {
379                 output->parent.draw_initial_frame = 0;
380
381                 draw_initial_frame(output);
382         }
383
384         callback = wl_surface_frame(output->parent.surface);
385         wl_callback_add_listener(callback, &frame_listener, output);
386         wl_surface_commit(output->parent.surface);
387         wl_display_flush(wc->parent.wl_display);
388 }
389
390 static int
391 wayland_output_repaint_gl(struct weston_output *output_base,
392                           pixman_region32_t *damage)
393 {
394         struct wayland_output *output = (struct wayland_output *) output_base;
395         struct weston_compositor *ec = output->base.compositor;
396         struct wl_callback *callback;
397
398         callback = wl_surface_frame(output->parent.surface);
399         wl_callback_add_listener(callback, &frame_listener, output);
400
401         wayland_output_update_gl_border(output);
402
403         ec->renderer->repaint_output(&output->base, damage);
404
405         pixman_region32_subtract(&ec->primary_plane.damage,
406                                  &ec->primary_plane.damage, damage);
407         return 0;
408 }
409
410 static void
411 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
412 {
413         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
414         cairo_t *cr;
415
416         if (!buffer->output->frame || !buffer->frame_damaged)
417                 return;
418
419         cr = cairo_create(buffer->c_surface);
420
421         frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
422         fwidth = frame_width(buffer->output->frame);
423         fheight = frame_height(buffer->output->frame);
424
425         /* Set the clip so we don't unnecisaraly damage the surface */
426         cairo_move_to(cr, ix, iy);
427         cairo_rel_line_to(cr, iwidth, 0);
428         cairo_rel_line_to(cr, 0, iheight);
429         cairo_rel_line_to(cr, -iwidth, 0);
430         cairo_line_to(cr, ix, iy);
431         cairo_line_to(cr, 0, iy);
432         cairo_line_to(cr, 0, fheight);
433         cairo_line_to(cr, fwidth, fheight);
434         cairo_line_to(cr, fwidth, 0);
435         cairo_line_to(cr, 0, 0);
436         cairo_line_to(cr, 0, iy);
437         cairo_close_path(cr);
438         cairo_clip(cr);
439
440         /* Draw using a pattern so that the final result gets clipped */
441         cairo_push_group(cr);
442         frame_repaint(buffer->output->frame, cr);
443         cairo_pop_group_to_source(cr);
444         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
445         cairo_paint(cr);
446
447         cairo_destroy(cr);
448 }
449
450 static void
451 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
452 {
453         pixman_region32_t damage;
454         pixman_box32_t *rects;
455         int32_t ix, iy, iwidth, iheight, fwidth, fheight;
456         int i, n;
457
458         pixman_region32_init(&damage);
459         weston_transformed_region(sb->output->base.width,
460                                   sb->output->base.height,
461                                   sb->output->base.transform,
462                                   sb->output->base.current_scale,
463                                   &sb->damage, &damage);
464
465         if (sb->output->frame) {
466                 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
467                 fwidth = frame_width(sb->output->frame);
468                 fheight = frame_height(sb->output->frame);
469
470                 pixman_region32_translate(&damage, ix, iy);
471
472                 if (sb->frame_damaged) {
473                         pixman_region32_union_rect(&damage, &damage,
474                                                    0, 0, fwidth, iy);
475                         pixman_region32_union_rect(&damage, &damage,
476                                                    0, iy, ix, iheight);
477                         pixman_region32_union_rect(&damage, &damage,
478                                                    ix + iwidth, iy,
479                                                    fwidth - (ix + iwidth), iheight);
480                         pixman_region32_union_rect(&damage, &damage,
481                                                    0, iy + iheight,
482                                                    fwidth, fheight - (iy + iheight));
483                 }
484         }
485
486         rects = pixman_region32_rectangles(&damage, &n);
487         wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
488         for (i = 0; i < n; ++i)
489                 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
490                                   rects[i].y1, rects[i].x2 - rects[i].x1,
491                                   rects[i].y2 - rects[i].y1);
492
493         if (sb->output->frame)
494                 pixman_region32_fini(&damage);
495 }
496
497 static int
498 wayland_output_repaint_pixman(struct weston_output *output_base,
499                               pixman_region32_t *damage)
500 {
501         struct wayland_output *output = (struct wayland_output *) output_base;
502         struct wayland_compositor *c =
503                 (struct wayland_compositor *)output->base.compositor;
504         struct wl_callback *callback;
505         struct wayland_shm_buffer *sb;
506
507         if (output->frame) {
508                 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
509                         wl_list_for_each(sb, &output->shm.buffers, link)
510                                 sb->frame_damaged = 1;
511         }
512
513         wl_list_for_each(sb, &output->shm.buffers, link)
514                 pixman_region32_union(&sb->damage, &sb->damage, damage);
515
516         sb = wayland_output_get_shm_buffer(output);
517
518         wayland_output_update_shm_border(sb);
519         pixman_renderer_output_set_buffer(output_base, sb->pm_image);
520         c->base.renderer->repaint_output(output_base, &sb->damage);
521
522         wayland_shm_buffer_attach(sb);
523
524         callback = wl_surface_frame(output->parent.surface);
525         wl_callback_add_listener(callback, &frame_listener, output);
526         wl_surface_commit(output->parent.surface);
527         wl_display_flush(c->parent.wl_display);
528
529         pixman_region32_fini(&sb->damage);
530         pixman_region32_init(&sb->damage);
531         sb->frame_damaged = 0;
532
533         pixman_region32_subtract(&c->base.primary_plane.damage,
534                                  &c->base.primary_plane.damage, damage);
535         return 0;
536 }
537
538 static void
539 wayland_output_destroy(struct weston_output *output_base)
540 {
541         struct wayland_output *output = (struct wayland_output *) output_base;
542         struct wayland_compositor *c =
543                 (struct wayland_compositor *) output->base.compositor;
544
545         if (c->use_pixman) {
546                 pixman_renderer_output_destroy(output_base);
547         } else {
548                 gl_renderer->output_destroy(output_base);
549         }
550
551         wl_egl_window_destroy(output->gl.egl_window);
552         wl_surface_destroy(output->parent.surface);
553         wl_shell_surface_destroy(output->parent.shell_surface);
554
555         if (output->frame) {
556                 frame_destroy(output->frame);
557                 cairo_surface_destroy(output->gl.border.top);
558                 cairo_surface_destroy(output->gl.border.left);
559                 cairo_surface_destroy(output->gl.border.right);
560                 cairo_surface_destroy(output->gl.border.bottom);
561         }
562
563         weston_output_destroy(&output->base);
564         free(output);
565
566         return;
567 }
568
569 static const struct wl_shell_surface_listener shell_surface_listener;
570
571 static int
572 wayland_output_init_gl_renderer(struct wayland_output *output)
573 {
574         int32_t fwidth = 0, fheight = 0;
575
576         if (output->frame) {
577                 fwidth = frame_width(output->frame);
578                 fheight = frame_height(output->frame);
579         } else {
580                 fwidth = output->base.current_mode->width;
581                 fheight = output->base.current_mode->height;
582         }
583
584         output->gl.egl_window =
585                 wl_egl_window_create(output->parent.surface,
586                                      fwidth, fheight);
587         if (!output->gl.egl_window) {
588                 weston_log("failure to create wl_egl_window\n");
589                 return -1;
590         }
591
592         if (gl_renderer->output_create(&output->base,
593                         output->gl.egl_window) < 0)
594                 goto cleanup_window;
595
596         return 0;
597
598 cleanup_window:
599         wl_egl_window_destroy(output->gl.egl_window);
600         return -1;
601 }
602
603 static int
604 wayland_output_init_pixman_renderer(struct wayland_output *output)
605 {
606         return pixman_renderer_output_create(&output->base);
607 }
608
609 static struct wayland_output *
610 wayland_output_create(struct wayland_compositor *c, int x, int y,
611                       int width, int height, const char *name,
612                       uint32_t transform, int32_t scale)
613 {
614         struct wayland_output *output;
615         int32_t fx, fy, fwidth, fheight;
616         int output_width, output_height;
617         struct wl_region *region;
618
619         output = zalloc(sizeof *output);
620         if (output == NULL)
621                 return NULL;
622
623         output_width = width * scale;
624         output_height = height * scale;
625
626         output->mode.flags =
627                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
628         output->mode.width = output_width;
629         output->mode.height = output_height;
630         output->mode.refresh = 60000;
631         output->scale = scale;
632         wl_list_init(&output->base.mode_list);
633         wl_list_insert(&output->base.mode_list, &output->mode.link);
634
635         weston_log("Creating %dx%d wayland output at (%d, %d)\n",
636                    width, height, x, y);
637
638         output->base.current_mode = &output->mode;
639         weston_output_init(&output->base, &c->base, x, y, width, height,
640                            transform, scale);
641
642         output->base.make = "waywayland";
643         output->base.model = "none";
644
645         wl_list_init(&output->shm.buffers);
646         wl_list_init(&output->shm.free_buffers);
647
648         if (!c->theme)
649                 c->theme = theme_create();
650         output->frame = frame_create(c->theme, width, height,
651                                      FRAME_BUTTON_CLOSE, name);
652         frame_resize_inside(output->frame, output_width, output_height);
653
654         output->parent.surface =
655                 wl_compositor_create_surface(c->parent.compositor);
656         wl_surface_set_user_data(output->parent.surface, output);
657
658         if (c->use_pixman) {
659                 if (wayland_output_init_pixman_renderer(output) < 0)
660                         goto cleanup_output;
661                 output->base.repaint = wayland_output_repaint_pixman;
662         } else {
663                 if (wayland_output_init_gl_renderer(output) < 0)
664                         goto cleanup_output;
665                 output->base.repaint = wayland_output_repaint_gl;
666         }
667
668         frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight);
669         region = wl_compositor_create_region(c->parent.compositor);
670         wl_region_add(region, fx, fy, fwidth, fheight);
671         wl_surface_set_input_region(output->parent.surface, region);
672         wl_region_destroy(region);
673
674         frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight);
675         region = wl_compositor_create_region(c->parent.compositor);
676         wl_region_add(region, fx, fy, fwidth, fheight);
677         wl_surface_set_opaque_region(output->parent.surface, region);
678         wl_region_destroy(region);
679
680         output->parent.draw_initial_frame = 1;
681         output->parent.shell_surface =
682                 wl_shell_get_shell_surface(c->parent.shell,
683                                            output->parent.surface);
684         wl_shell_surface_add_listener(output->parent.shell_surface,
685                                       &shell_surface_listener, output);
686         wl_shell_surface_set_toplevel(output->parent.shell_surface);
687
688         output->base.start_repaint_loop = wayland_output_start_repaint_loop;
689         output->base.destroy = wayland_output_destroy;
690         output->base.assign_planes = NULL;
691         output->base.set_backlight = NULL;
692         output->base.set_dpms = NULL;
693         output->base.switch_mode = NULL;
694
695         wl_list_insert(c->base.output_list.prev, &output->base.link);
696
697         return output;
698
699 cleanup_output:
700         /* FIXME: cleanup weston_output */
701         free(output);
702
703         return NULL;
704 }
705
706 static struct wayland_output *
707 wayland_output_create_for_config(struct wayland_compositor *c,
708                                  struct weston_config_section *config_section,
709                                  int option_width, int option_height,
710                                  int32_t x, int32_t y)
711 {
712         struct wayland_output *output;
713         char *mode, *t, *name, *str;
714         int width, height, scale;
715         uint32_t transform;
716         unsigned int i, slen;
717
718         static const struct { const char *name; uint32_t token; } transform_names[] = {
719                 { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
720                 { "90",         WL_OUTPUT_TRANSFORM_90 },
721                 { "180",        WL_OUTPUT_TRANSFORM_180 },
722                 { "270",        WL_OUTPUT_TRANSFORM_270 },
723                 { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
724                 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
725                 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
726                 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
727         };
728
729         weston_config_section_get_string(config_section, "name", &name, NULL);
730         if (name) {
731                 slen = strlen(name);
732                 slen += strlen(WINDOW_TITLE " - ");
733                 str = malloc(slen + 1);
734                 if (str)
735                         snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
736                 free(name);
737                 name = str;
738         }
739         if (!name)
740                 name = WINDOW_TITLE;
741
742         weston_config_section_get_string(config_section,
743                                          "mode", &mode, "1024x600");
744         if (sscanf(mode, "%dx%d", &width, &height) != 2) {
745                 weston_log("Invalid mode \"%s\" for output %s\n",
746                            mode, name);
747                 width = 1024;
748                 height = 640;
749         }
750         free(mode);
751
752         if (option_width)
753                 width = option_width;
754         if (option_height)
755                 height = option_height;
756
757         weston_config_section_get_int(config_section, "scale", &scale, 1);
758
759         weston_config_section_get_string(config_section,
760                                          "transform", &t, "normal");
761         transform = WL_OUTPUT_TRANSFORM_NORMAL;
762         for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
763                 if (strcmp(transform_names[i].name, t) == 0) {
764                         transform = transform_names[i].token;
765                         break;
766                 }
767         }
768         if (i >= ARRAY_LENGTH(transform_names))
769                 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
770         free(t);
771
772         output = wayland_output_create(c, x, y, width, height, name, transform, scale);
773         free(name);
774
775         return output;
776 }
777
778 static void
779 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
780                    uint32_t serial)
781 {
782         wl_shell_surface_pong(shell_surface, serial);
783 }
784
785 static void
786 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
787                         uint32_t edges, int32_t width, int32_t height)
788 {
789         /* FIXME: implement resizing */
790 }
791
792 static void
793 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
794 {
795 }
796
797 static const struct wl_shell_surface_listener shell_surface_listener = {
798         shell_surface_ping,
799         shell_surface_configure,
800         shell_surface_popup_done
801 };
802
803 /* Events received from the wayland-server this compositor is client of: */
804
805 /* parent input interface */
806 static void
807 input_set_cursor(struct wayland_input *input)
808 {
809
810         struct wl_buffer *buffer;
811         struct wl_cursor_image *image;
812
813         if (!input->compositor->cursor)
814                 return; /* Couldn't load the cursor. Can't set it */
815
816         image = input->compositor->cursor->images[0];
817         buffer = wl_cursor_image_get_buffer(image);
818
819
820         wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
821                               input->parent.cursor.surface,
822                               image->hotspot_x, image->hotspot_y);
823
824         wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
825         wl_surface_damage(input->parent.cursor.surface, 0, 0,
826                           image->width, image->height);
827         wl_surface_commit(input->parent.cursor.surface);
828 }
829
830 static void
831 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
832                            uint32_t serial, struct wl_surface *surface,
833                            wl_fixed_t x, wl_fixed_t y)
834 {
835         struct wayland_input *input = data;
836         int32_t fx, fy;
837         enum theme_location location;
838
839         /* XXX: If we get a modifier event immediately before the focus,
840          *      we should try to keep the same serial. */
841         input->enter_serial = serial;
842         input->output = wl_surface_get_user_data(surface);
843
844         if (input->output->frame) {
845                 location = frame_pointer_enter(input->output->frame, input,
846                                                wl_fixed_to_int(x),
847                                                wl_fixed_to_int(y));
848                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
849                 x -= wl_fixed_from_int(fx);
850                 y -= wl_fixed_from_int(fy);
851
852                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
853                         weston_output_schedule_repaint(&input->output->base);
854         } else {
855                 location = THEME_LOCATION_CLIENT_AREA;
856         }
857
858         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
859
860         if (location == THEME_LOCATION_CLIENT_AREA) {
861                 input->focus = 1;
862                 notify_pointer_focus(&input->base, &input->output->base, x, y);
863                 wl_pointer_set_cursor(input->parent.pointer,
864                                       input->enter_serial, NULL, 0, 0);
865         } else {
866                 input->focus = 0;
867                 notify_pointer_focus(&input->base, NULL, 0, 0);
868                 input_set_cursor(input);
869         }
870 }
871
872 static void
873 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
874                            uint32_t serial, struct wl_surface *surface)
875 {
876         struct wayland_input *input = data;
877
878         if (input->output->frame) {
879                 frame_pointer_leave(input->output->frame, input);
880
881                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
882                         weston_output_schedule_repaint(&input->output->base);
883         }
884
885         notify_pointer_focus(&input->base, NULL, 0, 0);
886         input->output = NULL;
887         input->focus = 0;
888 }
889
890 static void
891 input_handle_motion(void *data, struct wl_pointer *pointer,
892                     uint32_t time, wl_fixed_t x, wl_fixed_t y)
893 {
894         struct wayland_input *input = data;
895         int32_t fx, fy;
896         enum theme_location location;
897
898         if (input->output->frame) {
899                 location = frame_pointer_motion(input->output->frame, input,
900                                                 wl_fixed_to_int(x),
901                                                 wl_fixed_to_int(y));
902                 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
903                 x -= wl_fixed_from_int(fx);
904                 y -= wl_fixed_from_int(fy);
905
906                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
907                         weston_output_schedule_repaint(&input->output->base);
908         } else {
909                 location = THEME_LOCATION_CLIENT_AREA;
910         }
911
912         weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
913
914         if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
915                 input_set_cursor(input);
916                 notify_pointer_focus(&input->base, NULL, 0, 0);
917                 input->focus = 0;
918         } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
919                 wl_pointer_set_cursor(input->parent.pointer,
920                                       input->enter_serial, NULL, 0, 0);
921                 notify_pointer_focus(&input->base, &input->output->base, x, y);
922                 input->focus = 1;
923         }
924
925         if (location == THEME_LOCATION_CLIENT_AREA)
926                 notify_motion_absolute(&input->base, time, x, y);
927 }
928
929 static void
930 input_handle_button(void *data, struct wl_pointer *pointer,
931                     uint32_t serial, uint32_t time, uint32_t button,
932                     uint32_t state_w)
933 {
934         struct wayland_input *input = data;
935         enum wl_pointer_button_state state = state_w;
936         enum frame_button_state fstate;
937         enum theme_location location;
938
939         if (input->output->frame) {
940                 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
941                         FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
942
943                 location = frame_pointer_button(input->output->frame, input,
944                                                 button, fstate);
945
946                 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
947
948                         wl_shell_surface_move(input->output->parent.shell_surface,
949                                               input->parent.seat, serial);
950                         frame_status_clear(input->output->frame,
951                                            FRAME_STATUS_MOVE);
952                         return;
953                 }
954
955                 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
956                         wl_display_terminate(input->compositor->base.wl_display);
957
958                 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
959                         weston_output_schedule_repaint(&input->output->base);
960         } else {
961                 location = THEME_LOCATION_CLIENT_AREA;
962         }
963
964         if (location == THEME_LOCATION_CLIENT_AREA)
965                 notify_button(&input->base, time, button, state);
966 }
967
968 static void
969 input_handle_axis(void *data, struct wl_pointer *pointer,
970                   uint32_t time, uint32_t axis, wl_fixed_t value)
971 {
972         struct wayland_input *input = data;
973
974         notify_axis(&input->base, time, axis, value);
975 }
976
977 static const struct wl_pointer_listener pointer_listener = {
978         input_handle_pointer_enter,
979         input_handle_pointer_leave,
980         input_handle_motion,
981         input_handle_button,
982         input_handle_axis,
983 };
984
985 static void
986 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
987                     int fd, uint32_t size)
988 {
989         struct wayland_input *input = data;
990         struct xkb_keymap *keymap;
991         char *map_str;
992
993         if (!data) {
994                 close(fd);
995                 return;
996         }
997
998         if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
999                 close(fd);
1000                 return;
1001         }
1002
1003         map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1004         if (map_str == MAP_FAILED) {
1005                 close(fd);
1006                 return;
1007         }
1008
1009         keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1010                                          map_str,
1011                                          XKB_KEYMAP_FORMAT_TEXT_V1,
1012                                          0);
1013         munmap(map_str, size);
1014         close(fd);
1015
1016         if (!keymap) {
1017                 weston_log("failed to compile keymap\n");
1018                 return;
1019         }
1020
1021         if (input->base.keyboard)
1022                 weston_seat_update_keymap(&input->base, keymap);
1023         else
1024                 weston_seat_init_keyboard(&input->base, keymap);
1025
1026         xkb_map_unref(keymap);
1027 }
1028
1029 static void
1030 input_handle_keyboard_enter(void *data,
1031                             struct wl_keyboard *keyboard,
1032                             uint32_t serial,
1033                             struct wl_surface *surface,
1034                             struct wl_array *keys)
1035 {
1036         struct wayland_input *input = data;
1037         struct wayland_output *focus;
1038
1039         focus = input->keyboard_focus;
1040         if (focus) {
1041                 /* This shouldn't happen */
1042                 focus->keyboard_count--;
1043                 if (!focus->keyboard_count && focus->frame)
1044                         frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1045                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1046                         weston_output_schedule_repaint(&focus->base);
1047         }
1048
1049         input->keyboard_focus = wl_surface_get_user_data(surface);
1050         input->keyboard_focus->keyboard_count++;
1051
1052         focus = input->keyboard_focus;
1053         if (focus->frame) {
1054                 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1055                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1056                         weston_output_schedule_repaint(&focus->base);
1057         }
1058
1059
1060         /* XXX: If we get a modifier event immediately before the focus,
1061          *      we should try to keep the same serial. */
1062         notify_keyboard_focus_in(&input->base, keys,
1063                                  STATE_UPDATE_AUTOMATIC);
1064 }
1065
1066 static void
1067 input_handle_keyboard_leave(void *data,
1068                             struct wl_keyboard *keyboard,
1069                             uint32_t serial,
1070                             struct wl_surface *surface)
1071 {
1072         struct wayland_input *input = data;
1073         struct wayland_output *focus;
1074
1075         notify_keyboard_focus_out(&input->base);
1076
1077         focus = input->keyboard_focus;
1078         if (!focus)
1079                 return; /* This shouldn't happen */
1080
1081         focus->keyboard_count--;
1082         if (!focus->keyboard_count && focus->frame) {
1083                 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1084                 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1085                         weston_output_schedule_repaint(&focus->base);
1086         }
1087
1088         input->keyboard_focus = NULL;
1089 }
1090
1091 static void
1092 input_handle_key(void *data, struct wl_keyboard *keyboard,
1093                  uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1094 {
1095         struct wayland_input *input = data;
1096
1097         input->key_serial = serial;
1098         notify_key(&input->base, time, key,
1099                    state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1100                            WL_KEYBOARD_KEY_STATE_RELEASED,
1101                    STATE_UPDATE_NONE);
1102 }
1103
1104 static void
1105 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1106                        uint32_t serial_in, uint32_t mods_depressed,
1107                        uint32_t mods_latched, uint32_t mods_locked,
1108                        uint32_t group)
1109 {
1110         struct wayland_input *input = data;
1111         struct wayland_compositor *c = input->compositor;
1112         uint32_t serial_out;
1113
1114         /* If we get a key event followed by a modifier event with the
1115          * same serial number, then we try to preserve those semantics by
1116          * reusing the same serial number on the way out too. */
1117         if (serial_in == input->key_serial)
1118                 serial_out = wl_display_get_serial(c->base.wl_display);
1119         else
1120                 serial_out = wl_display_next_serial(c->base.wl_display);
1121
1122         xkb_state_update_mask(input->base.xkb_state.state,
1123                               mods_depressed, mods_latched,
1124                               mods_locked, 0, 0, group);
1125         notify_modifiers(&input->base, serial_out);
1126 }
1127
1128 static const struct wl_keyboard_listener keyboard_listener = {
1129         input_handle_keymap,
1130         input_handle_keyboard_enter,
1131         input_handle_keyboard_leave,
1132         input_handle_key,
1133         input_handle_modifiers,
1134 };
1135
1136 static void
1137 input_handle_capabilities(void *data, struct wl_seat *seat,
1138                           enum wl_seat_capability caps)
1139 {
1140         struct wayland_input *input = data;
1141
1142         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1143                 input->parent.pointer = wl_seat_get_pointer(seat);
1144                 wl_pointer_set_user_data(input->parent.pointer, input);
1145                 wl_pointer_add_listener(input->parent.pointer,
1146                                         &pointer_listener, input);
1147                 weston_seat_init_pointer(&input->base);
1148         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1149                 wl_pointer_destroy(input->parent.pointer);
1150                 input->parent.pointer = NULL;
1151         }
1152
1153         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1154                 input->parent.keyboard = wl_seat_get_keyboard(seat);
1155                 wl_keyboard_set_user_data(input->parent.keyboard, input);
1156                 wl_keyboard_add_listener(input->parent.keyboard,
1157                                          &keyboard_listener, input);
1158         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1159                 wl_keyboard_destroy(input->parent.keyboard);
1160                 input->parent.keyboard = NULL;
1161         }
1162 }
1163
1164 static const struct wl_seat_listener seat_listener = {
1165         input_handle_capabilities,
1166 };
1167
1168 static void
1169 display_add_seat(struct wayland_compositor *c, uint32_t id)
1170 {
1171         struct wayland_input *input;
1172
1173         input = zalloc(sizeof *input);
1174         if (input == NULL)
1175                 return;
1176
1177         weston_seat_init(&input->base, &c->base, "default");
1178         input->compositor = c;
1179         input->parent.seat = wl_registry_bind(c->parent.registry, id,
1180                                               &wl_seat_interface, 1);
1181         wl_list_insert(c->input_list.prev, &input->link);
1182
1183         wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1184         wl_seat_set_user_data(input->parent.seat, input);
1185
1186         input->parent.cursor.surface =
1187                 wl_compositor_create_surface(c->parent.compositor);
1188 }
1189
1190 static void
1191 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1192                        const char *interface, uint32_t version)
1193 {
1194         struct wayland_compositor *c = data;
1195
1196         if (strcmp(interface, "wl_compositor") == 0) {
1197                 c->parent.compositor =
1198                         wl_registry_bind(registry, name,
1199                                          &wl_compositor_interface, 1);
1200         } else if (strcmp(interface, "wl_shell") == 0) {
1201                 c->parent.shell =
1202                         wl_registry_bind(registry, name,
1203                                          &wl_shell_interface, 1);
1204         } else if (strcmp(interface, "wl_seat") == 0) {
1205                 display_add_seat(c, name);
1206         } else if (strcmp(interface, "wl_shm") == 0) {
1207                 c->parent.shm =
1208                         wl_registry_bind(registry, name, &wl_shm_interface, 1);
1209         }
1210 }
1211
1212 static const struct wl_registry_listener registry_listener = {
1213         registry_handle_global
1214 };
1215
1216 static int
1217 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1218 {
1219         struct wayland_compositor *c = data;
1220         int count = 0;
1221
1222         if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1223                 wl_display_terminate(c->base.wl_display);
1224                 return 0;
1225         }
1226
1227         if (mask & WL_EVENT_READABLE)
1228                 count = wl_display_dispatch(c->parent.wl_display);
1229         if (mask & WL_EVENT_WRITABLE)
1230                 wl_display_flush(c->parent.wl_display);
1231
1232         if (mask == 0) {
1233                 count = wl_display_dispatch_pending(c->parent.wl_display);
1234                 wl_display_flush(c->parent.wl_display);
1235         }
1236
1237         return count;
1238 }
1239
1240 static void
1241 wayland_restore(struct weston_compositor *ec)
1242 {
1243 }
1244
1245 static void
1246 wayland_destroy(struct weston_compositor *ec)
1247 {
1248         struct wayland_compositor *c = (struct wayland_compositor *) ec;
1249
1250         ec->renderer->destroy(ec);
1251
1252         weston_compositor_shutdown(ec);
1253
1254         if (c->parent.shm)
1255                 wl_shm_destroy(c->parent.shm);
1256
1257         free(ec);
1258 }
1259
1260 static const char *left_ptrs[] = {
1261         "left_ptr",
1262         "default",
1263         "top_left_arrow",
1264         "left-arrow"
1265 };
1266
1267 static void
1268 create_cursor(struct wayland_compositor *c, struct weston_config *config)
1269 {
1270         struct weston_config_section *s;
1271         int size;
1272         char *theme = NULL;
1273         unsigned int i;
1274
1275         s = weston_config_get_section(config, "shell", NULL, NULL);
1276         weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1277         weston_config_section_get_int(s, "cursor-size", &size, 32);
1278
1279         c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1280
1281         c->cursor = NULL;
1282         for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1283                 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1284                                                        left_ptrs[i]);
1285         if (!c->cursor) {
1286                 fprintf(stderr, "could not load left cursor\n");
1287                 return;
1288         }
1289 }
1290
1291 static struct wayland_compositor *
1292 wayland_compositor_create(struct wl_display *display, int use_pixman,
1293                           const char *display_name, int *argc, char *argv[],
1294                           struct weston_config *config)
1295 {
1296         struct wayland_compositor *c;
1297         struct wl_event_loop *loop;
1298         int fd;
1299
1300         c = zalloc(sizeof *c);
1301         if (c == NULL)
1302                 return NULL;
1303
1304         if (weston_compositor_init(&c->base, display, argc, argv,
1305                                    config) < 0)
1306                 goto err_free;
1307
1308         c->parent.wl_display = wl_display_connect(display_name);
1309
1310         if (c->parent.wl_display == NULL) {
1311                 weston_log("failed to create display: %m\n");
1312                 goto err_compositor;
1313         }
1314
1315         wl_list_init(&c->input_list);
1316         c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1317         wl_registry_add_listener(c->parent.registry, &registry_listener, c);
1318         wl_display_roundtrip(c->parent.wl_display);
1319
1320         create_cursor(c, config);
1321
1322         c->base.wl_display = display;
1323
1324         c->use_pixman = use_pixman;
1325
1326         if (!c->use_pixman) {
1327                 gl_renderer = weston_load_module("gl-renderer.so",
1328                                                  "gl_renderer_interface");
1329                 if (!gl_renderer)
1330                         c->use_pixman = 1;
1331         }
1332
1333         if (!c->use_pixman) {
1334                 if (gl_renderer->create(&c->base, c->parent.wl_display,
1335                                 gl_renderer->alpha_attribs,
1336                                 NULL) < 0) {
1337                         weston_log("Failed to initialize the GL renderer; "
1338                                    "falling back to pixman.\n");
1339                         c->use_pixman = 1;
1340                 }
1341         }
1342
1343         if (c->use_pixman) {
1344                 if (pixman_renderer_init(&c->base) < 0) {
1345                         weston_log("Failed to initialize pixman renderer\n");
1346                         goto err_display;
1347                 }
1348         }
1349
1350         c->base.destroy = wayland_destroy;
1351         c->base.restore = wayland_restore;
1352
1353         loop = wl_display_get_event_loop(c->base.wl_display);
1354
1355         fd = wl_display_get_fd(c->parent.wl_display);
1356         c->parent.wl_source =
1357                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1358                                      wayland_compositor_handle_event, c);
1359         if (c->parent.wl_source == NULL)
1360                 goto err_renderer;
1361
1362         wl_event_source_check(c->parent.wl_source);
1363
1364         return c;
1365 err_renderer:
1366         c->base.renderer->destroy(&c->base);
1367 err_display:
1368         wl_display_disconnect(c->parent.wl_display);
1369 err_compositor:
1370         weston_compositor_shutdown(&c->base);
1371 err_free:
1372         free(c);
1373         return NULL;
1374 }
1375
1376 static void
1377 wayland_compositor_destroy(struct wayland_compositor *c)
1378 {
1379         struct weston_output *output;
1380
1381         wl_list_for_each(output, &c->base.output_list, link)
1382                 wayland_output_destroy(output);
1383
1384         c->base.renderer->destroy(&c->base);
1385         wl_display_disconnect(c->parent.wl_display);
1386
1387         if (c->theme)
1388                 theme_destroy(c->theme);
1389         if (c->frame_device)
1390                 cairo_device_destroy(c->frame_device);
1391         wl_cursor_theme_destroy(c->cursor_theme);
1392
1393         weston_compositor_shutdown(&c->base);
1394         free(c);
1395 }
1396
1397 WL_EXPORT struct weston_compositor *
1398 backend_init(struct wl_display *display, int *argc, char *argv[],
1399              struct weston_config *config)
1400 {
1401         struct wayland_compositor *c;
1402         struct wayland_output *output;
1403         struct weston_config_section *section;
1404         int x, count, width, height, use_pixman;
1405         const char *section_name, *display_name;
1406         char *name;
1407
1408         const struct weston_option wayland_options[] = {
1409                 { WESTON_OPTION_INTEGER, "width", 0, &width },
1410                 { WESTON_OPTION_INTEGER, "height", 0, &height },
1411                 { WESTON_OPTION_STRING, "display", 0, &display_name },
1412                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1413                 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1414         };
1415
1416         width = 0;
1417         height = 0;
1418         display_name = NULL;
1419         use_pixman = 0;
1420         count = 1;
1421
1422         parse_options(wayland_options,
1423                       ARRAY_LENGTH(wayland_options), argc, argv);
1424
1425         c = wayland_compositor_create(display, use_pixman, display_name,
1426                                       argc, argv, config);
1427         if (!c)
1428                 return NULL;
1429
1430         section = NULL;
1431         x = 0;
1432         while (weston_config_next_section(config, &section, &section_name)) {
1433                 if (!section_name || strcmp(section_name, "output") != 0)
1434                         continue;
1435                 weston_config_section_get_string(section, "name", &name, NULL);
1436                 if (name == NULL)
1437                         continue;
1438
1439                 if (name[0] != 'W' || name[1] != 'L') {
1440                         free(name);
1441                         continue;
1442                 }
1443                 free(name);
1444
1445                 output = wayland_output_create_for_config(c, section, width,
1446                                                           height, x, 0);
1447                 if (!output)
1448                         goto err_outputs;
1449
1450                 x += output->base.width;
1451                 --count;
1452         }
1453
1454         if (!width)
1455                 width = 1024;
1456         if (!height)
1457                 height = 640;
1458         while (count > 0) {
1459                 output = wayland_output_create(c, x, 0, width, height, NULL, 0, 1);
1460                 if (!output)
1461                         goto err_outputs;
1462
1463                 x += width;
1464                 --count;
1465         }
1466
1467         return &c->base;
1468
1469 err_outputs:
1470         wayland_compositor_destroy(c);
1471         return NULL;
1472 }