Add new shared/image-loader.h to separate include dependencies
[profile/ivi/weston.git] / src / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35
36 #include <wayland-client.h>
37 #include <wayland-egl.h>
38
39 #include <GLES2/gl2.h>
40 #include <GLES2/gl2ext.h>
41 #include <EGL/egl.h>
42 #include <EGL/eglext.h>
43
44 #include "compositor.h"
45 #include "../shared/image-loader.h"
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_output *output;
56
57                 struct {
58                         int32_t x, y, width, height;
59                 } screen_allocation;
60
61                 struct wl_event_source *wl_source;
62                 uint32_t event_mask;
63         } parent;
64
65         struct {
66                 int32_t top, bottom, left, right;
67                 GLuint texture;
68                 int32_t width, height;
69         } border;
70
71         struct wl_list input_list;
72 };
73
74 struct wayland_output {
75         struct weston_output    base;
76         struct wl_listener      frame_listener;
77         struct {
78                 struct wl_surface       *surface;
79                 struct wl_shell_surface *shell_surface;
80                 struct wl_egl_window    *egl_window;
81         } parent;
82         struct weston_mode      mode;
83 };
84
85 struct wayland_input {
86         struct weston_seat base;
87         struct wayland_compositor *compositor;
88         struct wl_seat *seat;
89         struct wl_pointer *pointer;
90         struct wl_keyboard *keyboard;
91         struct wl_touch *touch;
92         struct wl_list link;
93         uint32_t key_serial;
94         uint32_t enter_serial;
95         int focus;
96         struct wayland_output *output;
97 };
98
99
100 static int
101 texture_border(struct wayland_output *output)
102 {
103         struct wayland_compositor *c =
104                 (struct wayland_compositor *) output->base.compositor;
105         GLfloat *d;
106         unsigned int *p;
107         int i, j, k, n;
108         GLfloat x[4], y[4], u[4], v[4];
109
110         x[0] = -c->border.left;
111         x[1] = 0;
112         x[2] = output->base.current->width;
113         x[3] = output->base.current->width + c->border.right;
114
115         y[0] = -c->border.top;
116         y[1] = 0;
117         y[2] = output->base.current->height;
118         y[3] = output->base.current->height + c->border.bottom;
119
120         u[0] = 0.0;
121         u[1] = (GLfloat) c->border.left / c->border.width;
122         u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
123         u[3] = 1.0;
124
125         v[0] = 0.0;
126         v[1] = (GLfloat) c->border.top / c->border.height;
127         v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
128         v[3] = 1.0;
129
130         n = 8;
131         d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
132         p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
133
134         k = 0;
135         for (i = 0; i < 3; i++)
136                 for (j = 0; j < 3; j++) {
137
138                         if (i == 1 && j == 1)
139                                 continue;
140
141                         d[ 0] = x[i];
142                         d[ 1] = y[j];
143                         d[ 2] = u[i];
144                         d[ 3] = v[j];
145
146                         d[ 4] = x[i];
147                         d[ 5] = y[j + 1];
148                         d[ 6] = u[i];
149                         d[ 7] = v[j + 1];
150
151                         d[ 8] = x[i + 1];
152                         d[ 9] = y[j];
153                         d[10] = u[i + 1];
154                         d[11] = v[j];
155
156                         d[12] = x[i + 1];
157                         d[13] = y[j + 1];
158                         d[14] = u[i + 1];
159                         d[15] = v[j + 1];
160
161                         p[0] = k + 0;
162                         p[1] = k + 1;
163                         p[2] = k + 2;
164                         p[3] = k + 2;
165                         p[4] = k + 1;
166                         p[5] = k + 3;
167
168                         d += 16;
169                         p += 6;
170                         k += 4;
171                 }
172
173         return k / 4;
174 }
175
176 static void
177 draw_border(struct wayland_output *output)
178 {
179         struct wayland_compositor *c =
180                 (struct wayland_compositor *) output->base.compositor;
181         struct weston_shader *shader = &c->base.texture_shader_rgba;
182         GLfloat *v;
183         int n;
184
185         glDisable(GL_BLEND);
186         glUseProgram(shader->program);
187         c->base.current_shader = shader;
188
189         glUniformMatrix4fv(shader->proj_uniform,
190                            1, GL_FALSE, output->base.matrix.d);
191
192         glUniform1i(shader->tex_uniforms[0], 0);
193         glUniform1f(shader->alpha_uniform, 1);
194
195         n = texture_border(output);
196
197         glBindTexture(GL_TEXTURE_2D, c->border.texture);
198
199         v = c->base.vertices.data;
200         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
201         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
202         glEnableVertexAttribArray(0);
203         glEnableVertexAttribArray(1);
204
205         glDrawElements(GL_TRIANGLES, n * 6,
206                        GL_UNSIGNED_INT, c->base.indices.data);
207
208         glDisableVertexAttribArray(1);
209         glDisableVertexAttribArray(0);
210
211         c->base.vertices.size = 0;
212         c->base.indices.size = 0;
213 }
214
215 static void
216 create_border(struct wayland_compositor *c)
217 {
218         pixman_image_t *image;
219
220         image = load_image(DATADIR "/weston/border.png");
221         if (!image) {
222                 weston_log("could'nt load border image\n");
223                 return;
224         }
225
226         c->border.width = pixman_image_get_width(image);
227         c->border.height = pixman_image_get_height(image);
228
229         glGenTextures(1, &c->border.texture);
230         glBindTexture(GL_TEXTURE_2D, c->border.texture);
231         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
232         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
233         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
234         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
235
236         glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
237                      c->border.width,
238                      c->border.height,
239                      0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
240                      pixman_image_get_data(image));
241
242         pixman_image_unref(image);
243 }
244
245 static int
246 wayland_compositor_init_egl(struct wayland_compositor *c)
247 {
248         EGLint major, minor;
249         EGLint n;
250         EGLint config_attribs[] = {
251                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
252                 EGL_RED_SIZE, 1,
253                 EGL_GREEN_SIZE, 1,
254                 EGL_BLUE_SIZE, 1,
255                 EGL_ALPHA_SIZE, 1,
256                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
257                 EGL_NONE
258         };
259
260         c->base.egl_display = eglGetDisplay(c->parent.wl_display);
261         if (c->base.egl_display == NULL) {
262                 weston_log("failed to create display\n");
263                 return -1;
264         }
265
266         if (!eglInitialize(c->base.egl_display, &major, &minor)) {
267                 weston_log("failed to initialize display\n");
268                 return -1;
269         }
270
271         if (!eglChooseConfig(c->base.egl_display, config_attribs,
272                              &c->base.egl_config, 1, &n) || n == 0) {
273                 weston_log("failed to choose config: %d\n", n);
274                 return -1;
275         }
276
277         return 0;
278 }
279
280 static void
281 frame_done(void *data, struct wl_callback *callback, uint32_t time)
282 {
283         struct weston_output *output = data;
284
285         wl_callback_destroy(callback);
286         weston_output_finish_frame(output, time);
287 }
288
289 static const struct wl_callback_listener frame_listener = {
290         frame_done
291 };
292
293 static void
294 wayland_output_frame_notify(struct wl_listener *listener, void *data)
295 {
296         struct wayland_output *output =
297                 container_of(listener,
298                              struct wayland_output, frame_listener);
299
300         draw_border(output);
301 }
302
303 static void
304 wayland_output_repaint(struct weston_output *output_base,
305                        pixman_region32_t *damage)
306 {
307         struct wayland_output *output = (struct wayland_output *) output_base;
308         struct weston_compositor *ec = output->base.compositor;
309         struct wl_callback *callback;
310
311         callback = wl_surface_frame(output->parent.surface);
312         wl_callback_add_listener(callback, &frame_listener, output);
313
314         ec->renderer->repaint_output(&output->base, damage);
315
316         pixman_region32_subtract(&ec->primary_plane.damage,
317                                  &ec->primary_plane.damage, damage);
318
319 }
320
321 static void
322 wayland_output_destroy(struct weston_output *output_base)
323 {
324         struct wayland_output *output = (struct wayland_output *) output_base;
325         struct weston_compositor *ec = output->base.compositor;
326
327         eglDestroySurface(ec->egl_display, output->base.egl_surface);
328         wl_egl_window_destroy(output->parent.egl_window);
329         free(output);
330
331         return;
332 }
333
334 static const struct wl_shell_surface_listener shell_surface_listener;
335
336 static int
337 wayland_compositor_create_output(struct wayland_compositor *c,
338                                  int width, int height)
339 {
340         struct wayland_output *output;
341
342         output = malloc(sizeof *output);
343         if (output == NULL)
344                 return -1;
345         memset(output, 0, sizeof *output);
346
347         output->mode.flags =
348                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
349         output->mode.width = width;
350         output->mode.height = height;
351         output->mode.refresh = 60;
352         wl_list_init(&output->base.mode_list);
353         wl_list_insert(&output->base.mode_list, &output->mode.link);
354
355         output->base.current = &output->mode;
356         weston_output_init(&output->base, &c->base, 0, 0, width, height,
357                                                 WL_OUTPUT_TRANSFORM_NORMAL);
358
359         output->base.border.top = c->border.top;
360         output->base.border.bottom = c->border.bottom;
361         output->base.border.left = c->border.left;
362         output->base.border.right = c->border.right;
363         output->base.make = "waywayland";
364         output->base.model = "none";
365
366         weston_output_move(&output->base, 0, 0);
367
368         output->parent.surface =
369                 wl_compositor_create_surface(c->parent.compositor);
370         wl_surface_set_user_data(output->parent.surface, output);
371
372         output->parent.egl_window =
373                 wl_egl_window_create(output->parent.surface,
374                                      width + c->border.left + c->border.right,
375                                      height + c->border.top + c->border.bottom);
376         if (!output->parent.egl_window) {
377                 weston_log("failure to create wl_egl_window\n");
378                 goto cleanup_output;
379         }
380
381         output->base.egl_surface =
382                 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
383                                        output->parent.egl_window, NULL);
384         if (!output->base.egl_surface) {
385                 weston_log("failed to create window surface\n");
386                 goto cleanup_window;
387         }
388
389         output->parent.shell_surface =
390                 wl_shell_get_shell_surface(c->parent.shell,
391                                            output->parent.surface);
392         wl_shell_surface_add_listener(output->parent.shell_surface,
393                                       &shell_surface_listener, output);
394         wl_shell_surface_set_toplevel(output->parent.shell_surface);
395
396         output->base.origin = output->base.current;
397         output->base.repaint = wayland_output_repaint;
398         output->base.destroy = wayland_output_destroy;
399         output->base.assign_planes = NULL;
400         output->base.set_backlight = NULL;
401         output->base.set_dpms = NULL;
402         output->base.switch_mode = NULL;
403
404         wl_list_insert(c->base.output_list.prev, &output->base.link);
405
406         output->frame_listener.notify = wayland_output_frame_notify;
407         wl_signal_add(&output->base.frame_signal, &output->frame_listener);
408
409         return 0;
410
411 cleanup_window:
412         wl_egl_window_destroy(output->parent.egl_window);
413 cleanup_output:
414         /* FIXME: cleanup weston_output */
415         free(output);
416
417         return -1;
418 }
419
420 static void
421 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
422                    uint32_t serial)
423 {
424         wl_shell_surface_pong(shell_surface, serial);
425 }
426
427 static void
428 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
429                         uint32_t edges, int32_t width, int32_t height)
430 {
431         /* FIXME: implement resizing */
432 }
433
434 static void
435 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
436 {
437 }
438
439 static const struct wl_shell_surface_listener shell_surface_listener = {
440         shell_surface_ping,
441         shell_surface_configure,
442         shell_surface_popup_done
443 };
444
445 /* Events received from the wayland-server this compositor is client of: */
446
447 /* parent output interface */
448 static void
449 display_handle_geometry(void *data,
450                         struct wl_output *wl_output,
451                         int x,
452                         int y,
453                         int physical_width,
454                         int physical_height,
455                         int subpixel,
456                         const char *make,
457                         const char *model,
458                         int transform)
459 {
460         struct wayland_compositor *c = data;
461
462         c->parent.screen_allocation.x = x;
463         c->parent.screen_allocation.y = y;
464 }
465
466 static void
467 display_handle_mode(void *data,
468                     struct wl_output *wl_output,
469                     uint32_t flags,
470                     int width,
471                     int height,
472                     int refresh)
473 {
474         struct wayland_compositor *c = data;
475
476         c->parent.screen_allocation.width = width;
477         c->parent.screen_allocation.height = height;
478 }
479
480 static const struct wl_output_listener output_listener = {
481         display_handle_geometry,
482         display_handle_mode
483 };
484
485 static void
486 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
487 {
488         struct wayland_compositor *c = input->compositor;
489         int width, height, inside;
490
491         width = input->output->mode.width;
492         height = input->output->mode.height;
493
494         inside = c->border.left <= wl_fixed_to_int(x) &&
495                 wl_fixed_to_int(x) < width + c->border.left &&
496                 c->border.top <= wl_fixed_to_int(y) &&
497                 wl_fixed_to_int(y) < height + c->border.top;
498
499         if (!input->focus && inside) {
500                 notify_pointer_focus(&input->base, &input->output->base,
501                                      x - wl_fixed_from_int(c->border.left),
502                                      y = wl_fixed_from_int(c->border.top));
503                 wl_pointer_set_cursor(input->pointer,
504                                       input->enter_serial, NULL, 0, 0);
505         } else if (input->focus && !inside) {
506                 notify_pointer_focus(&input->base, NULL, 0, 0);
507                 /* FIXME: Should set default cursor here. */
508         }
509
510         input->focus = inside;
511 }
512
513 /* parent input interface */
514 static void
515 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
516                            uint32_t serial, struct wl_surface *surface,
517                            wl_fixed_t x, wl_fixed_t y)
518 {
519         struct wayland_input *input = data;
520
521         /* XXX: If we get a modifier event immediately before the focus,
522          *      we should try to keep the same serial. */
523         input->enter_serial = serial;
524         input->output = wl_surface_get_user_data(surface);
525         check_focus(input, x, y);
526 }
527
528 static void
529 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
530                            uint32_t serial, struct wl_surface *surface)
531 {
532         struct wayland_input *input = data;
533
534         notify_pointer_focus(&input->base, NULL, 0, 0);
535         input->output = NULL;
536         input->focus = 0;
537 }
538
539 static void
540 input_handle_motion(void *data, struct wl_pointer *pointer,
541                     uint32_t time, wl_fixed_t x, wl_fixed_t y)
542 {
543         struct wayland_input *input = data;
544         struct wayland_compositor *c = input->compositor;
545
546         check_focus(input, x, y);
547         if (input->focus)
548                 notify_motion(&input->base, time,
549                               x - wl_fixed_from_int(c->border.left),
550                               y - wl_fixed_from_int(c->border.top));
551 }
552
553 static void
554 input_handle_button(void *data, struct wl_pointer *pointer,
555                     uint32_t serial, uint32_t time, uint32_t button,
556                     uint32_t state_w)
557 {
558         struct wayland_input *input = data;
559         enum wl_pointer_button_state state = state_w;
560
561         notify_button(&input->base, time, button, state);
562 }
563
564 static void
565 input_handle_axis(void *data, struct wl_pointer *pointer,
566                   uint32_t time, uint32_t axis, wl_fixed_t value)
567 {
568         struct wayland_input *input = data;
569
570         notify_axis(&input->base, time, axis, value);
571 }
572
573 static const struct wl_pointer_listener pointer_listener = {
574         input_handle_pointer_enter,
575         input_handle_pointer_leave,
576         input_handle_motion,
577         input_handle_button,
578         input_handle_axis,
579 };
580
581 static void
582 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
583                     int fd, uint32_t size)
584 {
585         struct wayland_input *input = data;
586         struct xkb_keymap *keymap;
587         char *map_str;
588
589         if (!data) {
590                 close(fd);
591                 return;
592         }
593
594         if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
595                 close(fd);
596                 return;
597         }
598
599         map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
600         if (map_str == MAP_FAILED) {
601                 close(fd);
602                 return;
603         }
604
605         keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
606                                          map_str,
607                                          XKB_KEYMAP_FORMAT_TEXT_V1,
608                                          0);
609         munmap(map_str, size);
610         close(fd);
611
612         if (!keymap) {
613                 weston_log("failed to compile keymap\n");
614                 return;
615         }
616
617         weston_seat_init_keyboard(&input->base, keymap);
618         xkb_map_unref(keymap);
619 }
620
621 static void
622 input_handle_keyboard_enter(void *data,
623                             struct wl_keyboard *keyboard,
624                             uint32_t serial,
625                             struct wl_surface *surface,
626                             struct wl_array *keys)
627 {
628         struct wayland_input *input = data;
629
630         /* XXX: If we get a modifier event immediately before the focus,
631          *      we should try to keep the same serial. */
632         notify_keyboard_focus_in(&input->base, keys,
633                                  STATE_UPDATE_AUTOMATIC);
634 }
635
636 static void
637 input_handle_keyboard_leave(void *data,
638                             struct wl_keyboard *keyboard,
639                             uint32_t serial,
640                             struct wl_surface *surface)
641 {
642         struct wayland_input *input = data;
643
644         notify_keyboard_focus_out(&input->base);
645 }
646
647 static void
648 input_handle_key(void *data, struct wl_keyboard *keyboard,
649                  uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
650 {
651         struct wayland_input *input = data;
652
653         input->key_serial = serial;
654         notify_key(&input->base, time, key,
655                    state ? WL_KEYBOARD_KEY_STATE_PRESSED :
656                            WL_KEYBOARD_KEY_STATE_RELEASED,
657                    STATE_UPDATE_NONE);
658 }
659
660 static void
661 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
662                        uint32_t serial_in, uint32_t mods_depressed,
663                        uint32_t mods_latched, uint32_t mods_locked,
664                        uint32_t group)
665 {
666         struct wayland_input *input = data;
667         struct wayland_compositor *c = input->compositor;
668         uint32_t serial_out;
669
670         /* If we get a key event followed by a modifier event with the
671          * same serial number, then we try to preserve those semantics by
672          * reusing the same serial number on the way out too. */
673         if (serial_in == input->key_serial)
674                 serial_out = wl_display_get_serial(c->base.wl_display);
675         else
676                 serial_out = wl_display_next_serial(c->base.wl_display);
677
678         xkb_state_update_mask(input->base.xkb_state.state,
679                               mods_depressed, mods_latched,
680                               mods_locked, 0, 0, group);
681         notify_modifiers(&input->base, serial_out);
682 }
683
684 static const struct wl_keyboard_listener keyboard_listener = {
685         input_handle_keymap,
686         input_handle_keyboard_enter,
687         input_handle_keyboard_leave,
688         input_handle_key,
689         input_handle_modifiers,
690 };
691
692 static void
693 input_handle_capabilities(void *data, struct wl_seat *seat,
694                           enum wl_seat_capability caps)
695 {
696         struct wayland_input *input = data;
697
698         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
699                 input->pointer = wl_seat_get_pointer(seat);
700                 wl_pointer_set_user_data(input->pointer, input);
701                 wl_pointer_add_listener(input->pointer, &pointer_listener,
702                                         input);
703                 weston_seat_init_pointer(&input->base);
704         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
705                 wl_pointer_destroy(input->pointer);
706                 input->pointer = NULL;
707         }
708
709         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
710                 input->keyboard = wl_seat_get_keyboard(seat);
711                 wl_keyboard_set_user_data(input->keyboard, input);
712                 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
713                                          input);
714         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
715                 wl_keyboard_destroy(input->keyboard);
716                 input->keyboard = NULL;
717         }
718 }
719
720 static const struct wl_seat_listener seat_listener = {
721         input_handle_capabilities,
722 };
723
724 static void
725 display_add_seat(struct wayland_compositor *c, uint32_t id)
726 {
727         struct wayland_input *input;
728
729         input = malloc(sizeof *input);
730         if (input == NULL)
731                 return;
732
733         memset(input, 0, sizeof *input);
734
735         weston_seat_init(&input->base, &c->base);
736         input->compositor = c;
737         input->seat = wl_registry_bind(c->parent.registry, id,
738                                        &wl_seat_interface, 1);
739         wl_list_insert(c->input_list.prev, &input->link);
740
741         wl_seat_add_listener(input->seat, &seat_listener, input);
742         wl_seat_set_user_data(input->seat, input);
743 }
744
745 static void
746 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
747                        const char *interface, uint32_t version)
748 {
749         struct wayland_compositor *c = data;
750
751         if (strcmp(interface, "wl_compositor") == 0) {
752                 c->parent.compositor =
753                         wl_registry_bind(registry, name,
754                                          &wl_compositor_interface, 1);
755         } else if (strcmp(interface, "wl_output") == 0) {
756                 c->parent.output =
757                         wl_registry_bind(registry, name,
758                                          &wl_output_interface, 1);
759                 wl_output_add_listener(c->parent.output, &output_listener, c);
760         } else if (strcmp(interface, "wl_shell") == 0) {
761                 c->parent.shell =
762                         wl_registry_bind(registry, name,
763                                          &wl_shell_interface, 1);
764         } else if (strcmp(interface, "wl_seat") == 0) {
765                 display_add_seat(c, name);
766         }
767 }
768
769 static const struct wl_registry_listener registry_listener = {
770         registry_handle_global
771 };
772
773 static int
774 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
775 {
776         struct wayland_compositor *c = data;
777         int count = 0;
778
779         if (mask & WL_EVENT_READABLE)
780                 count = wl_display_dispatch(c->parent.wl_display);
781         if (mask & WL_EVENT_WRITABLE)
782                 wl_display_flush(c->parent.wl_display);
783
784         if (mask == 0) {
785                 count = wl_display_dispatch_pending(c->parent.wl_display);
786                 wl_display_flush(c->parent.wl_display);
787         }
788
789         return count;
790 }
791
792 static void
793 wayland_restore(struct weston_compositor *ec)
794 {
795 }
796
797 static void
798 wayland_destroy(struct weston_compositor *ec)
799 {
800         gles2_renderer_destroy(ec);
801
802         weston_compositor_shutdown(ec);
803
804         free(ec);
805 }
806
807 static struct weston_compositor *
808 wayland_compositor_create(struct wl_display *display,
809                           int width, int height, const char *display_name,
810                           int argc, char *argv[], const char *config_file)
811 {
812         struct wayland_compositor *c;
813         struct wl_event_loop *loop;
814         int fd;
815
816         c = malloc(sizeof *c);
817         if (c == NULL)
818                 return NULL;
819
820         memset(c, 0, sizeof *c);
821
822         if (weston_compositor_init(&c->base, display, argc, argv,
823                                    config_file) < 0)
824                 goto err_free;
825
826         c->parent.wl_display = wl_display_connect(display_name);
827
828         if (c->parent.wl_display == NULL) {
829                 weston_log("failed to create display: %m\n");
830                 goto err_compositor;
831         }
832
833         wl_list_init(&c->input_list);
834         c->parent.registry = wl_display_get_registry(c->parent.wl_display);
835         wl_registry_add_listener(c->parent.registry, &registry_listener, c);
836         wl_display_dispatch(c->parent.wl_display);
837
838         c->base.wl_display = display;
839         if (wayland_compositor_init_egl(c) < 0)
840                 goto err_display;
841
842         c->base.destroy = wayland_destroy;
843         c->base.restore = wayland_restore;
844
845         c->border.top = 30;
846         c->border.bottom = 24;
847         c->border.left = 25;
848         c->border.right = 26;
849
850         /* requires border fields */
851         if (wayland_compositor_create_output(c, width, height) < 0)
852                 goto err_display;
853
854         /* requires wayland_compositor_create_output */
855         if (gles2_renderer_init(&c->base) < 0)
856                 goto err_display;
857
858         /* requires gles2_renderer_init */
859         create_border(c);
860
861         loop = wl_display_get_event_loop(c->base.wl_display);
862
863         fd = wl_display_get_fd(c->parent.wl_display);
864         c->parent.wl_source =
865                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
866                                      wayland_compositor_handle_event, c);
867         if (c->parent.wl_source == NULL)
868                 goto err_display;
869
870         wl_event_source_check(c->parent.wl_source);
871
872         return &c->base;
873
874 err_display:
875         wl_display_disconnect(c->parent.wl_display);
876 err_compositor:
877         weston_compositor_shutdown(&c->base);
878 err_free:
879         free(c);
880         return NULL;
881 }
882
883 WL_EXPORT struct weston_compositor *
884 backend_init(struct wl_display *display, int argc, char *argv[],
885              const char *config_file)
886 {
887         int width = 1024, height = 640;
888         char *display_name = NULL;
889
890         const struct weston_option wayland_options[] = {
891                 { WESTON_OPTION_INTEGER, "width", 0, &width },
892                 { WESTON_OPTION_INTEGER, "height", 0, &height },
893                 { WESTON_OPTION_STRING, "display", 0, &display_name },
894         };
895
896         parse_options(wayland_options,
897                       ARRAY_LENGTH(wayland_options), argc, argv);
898
899         return wayland_compositor_create(display, width, height, display_name,
900                                          argc, argv, config_file);
901 }