compositor: Pass weston_output as the frame_signal argument
[platform/upstream/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 "log.h"
46
47 struct wayland_compositor {
48         struct weston_compositor         base;
49
50         struct wl_egl_pixmap            *dummy_pixmap;
51         EGLSurface                       dummy_egl_surface;
52
53         struct {
54                 struct wl_display *wl_display;
55                 struct wl_compositor *compositor;
56                 struct wl_shell *shell;
57                 struct wl_output *output;
58
59                 struct {
60                         int32_t x, y, width, height;
61                 } screen_allocation;
62
63                 struct wl_event_source *wl_source;
64                 uint32_t event_mask;
65         } parent;
66
67         struct {
68                 int32_t top, bottom, left, right;
69                 GLuint texture;
70                 int32_t width, height;
71         } border;
72
73         struct wl_list input_list;
74 };
75
76 struct wayland_output {
77         struct weston_output    base;
78
79         struct {
80                 struct wl_surface       *surface;
81                 struct wl_shell_surface *shell_surface;
82                 struct wl_egl_window    *egl_window;
83         } parent;
84         EGLSurface egl_surface;
85         struct weston_mode      mode;
86 };
87
88 struct wayland_input {
89         struct wayland_compositor *compositor;
90         struct wl_seat *seat;
91         struct wl_pointer *pointer;
92         struct wl_keyboard *keyboard;
93         struct wl_touch *touch;
94         struct wl_list link;
95 };
96
97
98 static int
99 texture_border(struct wayland_output *output)
100 {
101         struct wayland_compositor *c =
102                 (struct wayland_compositor *) output->base.compositor;
103         GLfloat *d;
104         unsigned int *p;
105         int i, j, k, n;
106         GLfloat x[4], y[4], u[4], v[4];
107
108         x[0] = -c->border.left;
109         x[1] = 0;
110         x[2] = output->base.current->width;
111         x[3] = output->base.current->width + c->border.right;
112
113         y[0] = -c->border.top;
114         y[1] = 0;
115         y[2] = output->base.current->height;
116         y[3] = output->base.current->height + c->border.bottom;
117
118         u[0] = 0.0;
119         u[1] = (GLfloat) c->border.left / c->border.width;
120         u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
121         u[3] = 1.0;
122
123         v[0] = 0.0;
124         v[1] = (GLfloat) c->border.top / c->border.height;
125         v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
126         v[3] = 1.0;
127
128         n = 8;
129         d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
130         p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
131
132         k = 0;
133         for (i = 0; i < 3; i++)
134                 for (j = 0; j < 3; j++) {
135
136                         if (i == 1 && j == 1)
137                                 continue;
138
139                         d[ 0] = x[i];
140                         d[ 1] = y[j];
141                         d[ 2] = u[i];
142                         d[ 3] = v[j];
143
144                         d[ 4] = x[i];
145                         d[ 5] = y[j + 1];
146                         d[ 6] = u[i];
147                         d[ 7] = v[j + 1];
148
149                         d[ 8] = x[i + 1];
150                         d[ 9] = y[j];
151                         d[10] = u[i + 1];
152                         d[11] = v[j];
153
154                         d[12] = x[i + 1];
155                         d[13] = y[j + 1];
156                         d[14] = u[i + 1];
157                         d[15] = v[j + 1];
158
159                         p[0] = k + 0;
160                         p[1] = k + 1;
161                         p[2] = k + 2;
162                         p[3] = k + 2;
163                         p[4] = k + 1;
164                         p[5] = k + 3;
165
166                         d += 16;
167                         p += 6;
168                         k += 4;
169                 }
170
171         return k / 4;
172 }
173
174 static void
175 draw_border(struct wayland_output *output)
176 {
177         struct wayland_compositor *c =
178                 (struct wayland_compositor *) output->base.compositor;
179         struct weston_shader *shader = &c->base.texture_shader;
180         GLfloat *v;
181         int n;
182
183         glDisable(GL_BLEND);
184         glUseProgram(shader->program);
185         c->base.current_shader = shader;
186
187         glUniformMatrix4fv(shader->proj_uniform,
188                            1, GL_FALSE, output->base.matrix.d);
189
190         glUniform1i(shader->tex_uniform, 0);
191         glUniform1f(shader->alpha_uniform, 1);
192         glUniform1f(shader->texwidth_uniform, 1);
193
194         n = texture_border(output);
195
196         glBindTexture(GL_TEXTURE_2D, c->border.texture);
197
198         v = c->base.vertices.data;
199         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
200         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
201         glEnableVertexAttribArray(0);
202         glEnableVertexAttribArray(1);
203
204         glDrawElements(GL_TRIANGLES, n * 6,
205                        GL_UNSIGNED_INT, c->base.indices.data);
206
207         glDisableVertexAttribArray(1);
208         glDisableVertexAttribArray(0);
209
210         c->base.vertices.size = 0;
211         c->base.indices.size = 0;
212 }
213
214 static void
215 create_border(struct wayland_compositor *c)
216 {
217         pixman_image_t *image;
218
219         image = load_image(DATADIR "/weston/border.png");
220         if (!image) {
221                 weston_log("could'nt load border image\n");
222                 return;
223         }
224
225         c->border.width = pixman_image_get_width(image);
226         c->border.height = pixman_image_get_height(image);
227
228         glGenTextures(1, &c->border.texture);
229         glBindTexture(GL_TEXTURE_2D, c->border.texture);
230         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
231         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
232         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234
235         glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
236                      c->border.width,
237                      c->border.height,
238                      0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
239                      pixman_image_get_data(image));
240
241         c->border.top = 25;
242         c->border.bottom = 50;
243         c->border.left = 25;
244         c->border.right = 25;
245
246         pixman_image_unref(image);
247 }
248
249 static int
250 wayland_compositor_init_egl(struct wayland_compositor *c)
251 {
252         EGLint major, minor;
253         EGLint n;
254         EGLint config_attribs[] = {
255                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
256                 EGL_RED_SIZE, 1,
257                 EGL_GREEN_SIZE, 1,
258                 EGL_BLUE_SIZE, 1,
259                 EGL_ALPHA_SIZE, 1,
260                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
261                 EGL_NONE
262         };
263         static const EGLint context_attribs[] = {
264                 EGL_CONTEXT_CLIENT_VERSION, 2,
265                 EGL_NONE
266         };
267
268         c->base.egl_display = eglGetDisplay(c->parent.wl_display);
269         if (c->base.egl_display == NULL) {
270                 weston_log("failed to create display\n");
271                 return -1;
272         }
273
274         if (!eglInitialize(c->base.egl_display, &major, &minor)) {
275                 weston_log("failed to initialize display\n");
276                 return -1;
277         }
278
279         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
280                 weston_log("failed to bind EGL_OPENGL_ES_API\n");
281                 return -1;
282         }
283         if (!eglChooseConfig(c->base.egl_display, config_attribs,
284                              &c->base.egl_config, 1, &n) || n == 0) {
285                 weston_log("failed to choose config: %d\n", n);
286                 return -1;
287         }
288
289         c->base.egl_context =
290                 eglCreateContext(c->base.egl_display, c->base.egl_config,
291                                  EGL_NO_CONTEXT, context_attribs);
292         if (c->base.egl_context == NULL) {
293                 weston_log("failed to create context\n");
294                 return -1;
295         }
296
297         c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
298         if (!c->dummy_pixmap) {
299                 weston_log("failure to create dummy_pixmap\n");
300                 return -1;
301         }
302
303         c->dummy_egl_surface =
304                 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
305                                        c->dummy_pixmap, NULL);
306         if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
307                             c->dummy_egl_surface, c->base.egl_context)) {
308                 weston_log("failed to make context current\n");
309                 return -1;
310         }
311
312         return 0;
313 }
314
315 static void
316 frame_done(void *data, struct wl_callback *callback, uint32_t time)
317 {
318         struct weston_output *output = data;
319
320         wl_callback_destroy(callback);
321         weston_output_finish_frame(output, time);
322 }
323
324 static const struct wl_callback_listener frame_listener = {
325         frame_done
326 };
327
328 static void
329 wayland_output_repaint(struct weston_output *output_base,
330                        pixman_region32_t *damage)
331 {
332         struct wayland_output *output = (struct wayland_output *) output_base;
333         struct wayland_compositor *compositor =
334                 (struct wayland_compositor *) output->base.compositor;
335         struct wl_callback *callback;
336         struct weston_surface *surface;
337
338         if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
339                             output->egl_surface,
340                             compositor->base.egl_context)) {
341                 weston_log("failed to make current\n");
342                 return;
343         }
344
345         wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
346                 weston_surface_draw(surface, &output->base, damage);
347
348         draw_border(output);
349
350         wl_signal_emit(&output->base.frame_signal, output);
351
352         eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
353         callback = wl_surface_frame(output->parent.surface);
354         wl_callback_add_listener(callback, &frame_listener, output);
355
356         return;
357 }
358
359 static void
360 wayland_output_destroy(struct weston_output *output_base)
361 {
362         struct wayland_output *output = (struct wayland_output *) output_base;
363         struct weston_compositor *ec = output->base.compositor;
364
365         eglDestroySurface(ec->egl_display, output->egl_surface);
366         wl_egl_window_destroy(output->parent.egl_window);
367         free(output);
368
369         return;
370 }
371
372 static const struct wl_shell_surface_listener shell_surface_listener;
373
374 static int
375 wayland_compositor_create_output(struct wayland_compositor *c,
376                                  int width, int height)
377 {
378         struct wayland_output *output;
379
380         output = malloc(sizeof *output);
381         if (output == NULL)
382                 return -1;
383         memset(output, 0, sizeof *output);
384
385         output->mode.flags =
386                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
387         output->mode.width = width;
388         output->mode.height = height;
389         output->mode.refresh = 60;
390         wl_list_init(&output->base.mode_list);
391         wl_list_insert(&output->base.mode_list, &output->mode.link);
392
393         output->base.current = &output->mode;
394         weston_output_init(&output->base, &c->base, 0, 0, width, height,
395                          WL_OUTPUT_FLIPPED);
396
397         output->base.border.top = c->border.top;
398         output->base.border.bottom = c->border.bottom;
399         output->base.border.left = c->border.left;
400         output->base.border.right = c->border.right;
401
402         weston_output_move(&output->base, 0, 0);
403
404         output->parent.surface =
405                 wl_compositor_create_surface(c->parent.compositor);
406         wl_surface_set_user_data(output->parent.surface, output);
407
408         output->parent.egl_window =
409                 wl_egl_window_create(output->parent.surface,
410                                      width + c->border.left + c->border.right,
411                                      height + c->border.top + c->border.bottom);
412         if (!output->parent.egl_window) {
413                 weston_log("failure to create wl_egl_window\n");
414                 goto cleanup_output;
415         }
416
417         output->egl_surface =
418                 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
419                                        output->parent.egl_window, NULL);
420         if (!output->egl_surface) {
421                 weston_log("failed to create window surface\n");
422                 goto cleanup_window;
423         }
424
425         if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
426                             output->egl_surface, c->base.egl_context)) {
427                 weston_log("failed to make surface current\n");
428                 goto cleanup_surface;
429                 return -1;
430         }
431
432         output->parent.shell_surface =
433                 wl_shell_get_shell_surface(c->parent.shell,
434                                            output->parent.surface);
435         wl_shell_surface_add_listener(output->parent.shell_surface,
436                                       &shell_surface_listener, output);
437         wl_shell_surface_set_toplevel(output->parent.shell_surface);
438
439         output->base.origin = output->base.current;
440         output->base.repaint = wayland_output_repaint;
441         output->base.destroy = wayland_output_destroy;
442         output->base.assign_planes = NULL;
443         output->base.set_backlight = NULL;
444         output->base.set_dpms = NULL;
445         output->base.switch_mode = NULL;
446
447         wl_list_insert(c->base.output_list.prev, &output->base.link);
448
449         return 0;
450
451 cleanup_surface:
452         eglDestroySurface(c->base.egl_display, output->egl_surface);
453 cleanup_window:
454         wl_egl_window_destroy(output->parent.egl_window);
455 cleanup_output:
456         /* FIXME: cleanup weston_output */
457         free(output);
458
459         return -1;
460 }
461
462 static void
463 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
464                    uint32_t serial)
465 {
466         wl_shell_surface_pong(shell_surface, serial);
467 }
468
469 static void
470 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
471                         uint32_t edges, int32_t width, int32_t height)
472 {
473         /* FIXME: implement resizing */
474 }
475
476 static void
477 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
478 {
479 }
480
481 static const struct wl_shell_surface_listener shell_surface_listener = {
482         shell_surface_ping,
483         shell_surface_configure,
484         shell_surface_popup_done
485 };
486
487 /* Events received from the wayland-server this compositor is client of: */
488
489 /* parent output interface */
490 static void
491 display_handle_geometry(void *data,
492                         struct wl_output *wl_output,
493                         int x,
494                         int y,
495                         int physical_width,
496                         int physical_height,
497                         int subpixel,
498                         const char *make,
499                         const char *model)
500 {
501         struct wayland_compositor *c = data;
502
503         c->parent.screen_allocation.x = x;
504         c->parent.screen_allocation.y = y;
505 }
506
507 static void
508 display_handle_mode(void *data,
509                     struct wl_output *wl_output,
510                     uint32_t flags,
511                     int width,
512                     int height,
513                     int refresh)
514 {
515         struct wayland_compositor *c = data;
516
517         c->parent.screen_allocation.width = width;
518         c->parent.screen_allocation.height = height;
519 }
520
521 static const struct wl_output_listener output_listener = {
522         display_handle_geometry,
523         display_handle_mode
524 };
525
526 /* parent input interface */
527 static void
528 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
529                            uint32_t serial, struct wl_surface *surface,
530                            wl_fixed_t x, wl_fixed_t y)
531 {
532         struct wayland_input *input = data;
533         struct wayland_output *output;
534         struct wayland_compositor *c = input->compositor;
535
536         output = wl_surface_get_user_data(surface);
537         notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
538         wl_pointer_set_cursor(input->pointer, serial, NULL, 0, 0);
539 }
540
541 static void
542 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
543                            uint32_t serial, struct wl_surface *surface)
544 {
545         struct wayland_input *input = data;
546         struct wayland_compositor *c = input->compositor;
547
548         notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
549 }
550
551 static void
552 input_handle_motion(void *data, struct wl_pointer *pointer,
553                     uint32_t time, wl_fixed_t x, wl_fixed_t y)
554 {
555         struct wayland_input *input = data;
556         struct wayland_compositor *c = input->compositor;
557
558         notify_motion(&c->base.seat->seat, time,
559                       x - wl_fixed_from_int(c->border.left),
560                       y - wl_fixed_from_int(c->border.top));
561 }
562
563 static void
564 input_handle_button(void *data, struct wl_pointer *pointer,
565                     uint32_t serial, uint32_t time, uint32_t button,
566                     uint32_t state_w)
567 {
568         struct wayland_input *input = data;
569         struct wayland_compositor *c = input->compositor;
570         enum wl_pointer_button_state state = state_w;
571
572         notify_button(&c->base.seat->seat, time, button, state);
573 }
574
575 static void
576 input_handle_axis(void *data, struct wl_pointer *pointer,
577                   uint32_t time, uint32_t axis, wl_fixed_t value)
578 {
579         struct wayland_input *input = data;
580         struct wayland_compositor *c = input->compositor;
581
582         notify_axis(&c->base.seat->seat, time, axis, value);
583 }
584
585 static const struct wl_pointer_listener pointer_listener = {
586         input_handle_pointer_enter,
587         input_handle_pointer_leave,
588         input_handle_motion,
589         input_handle_button,
590         input_handle_axis,
591 };
592
593 static void
594 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
595                     int fd, uint32_t size)
596 {
597         struct wayland_input *input = data;
598         struct xkb_keymap *keymap;
599         char *map_str;
600
601         if (!data) {
602                 close(fd);
603                 return;
604         }
605
606         if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
607                 close(fd);
608                 return;
609         }
610
611         map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
612         if (map_str == MAP_FAILED) {
613                 close(fd);
614                 return;
615         }
616
617         keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
618                                          map_str,
619                                          XKB_KEYMAP_FORMAT_TEXT_V1,
620                                          0);
621         munmap(map_str, size);
622         close(fd);
623
624         if (!keymap) {
625                 weston_log("failed to compile keymap\n");
626                 return;
627         }
628
629         weston_seat_init_keyboard(input->compositor->base.seat, keymap);
630         xkb_map_unref(keymap);
631 }
632
633 static void
634 input_handle_keyboard_enter(void *data,
635                             struct wl_keyboard *keyboard,
636                             uint32_t serial,
637                             struct wl_surface *surface,
638                             struct wl_array *keys)
639 {
640         struct wayland_input *input = data;
641         struct wayland_compositor *c = input->compositor;
642
643         notify_keyboard_focus(&c->base.seat->seat, keys);
644 }
645
646 static void
647 input_handle_keyboard_leave(void *data,
648                             struct wl_keyboard *keyboard,
649                             uint32_t serial,
650                             struct wl_surface *surface)
651 {
652         struct wayland_input *input = data;
653         struct wayland_compositor *c = input->compositor;
654
655         notify_keyboard_focus(&c->base.seat->seat, NULL);
656 }
657
658 static void
659 input_handle_key(void *data, struct wl_keyboard *keyboard,
660                  uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
661 {
662         struct wayland_input *input = data;
663         struct wayland_compositor *c = input->compositor;
664
665         notify_key(&c->base.seat->seat, time, key,
666                    state ? WL_KEYBOARD_KEY_STATE_PRESSED :
667                            WL_KEYBOARD_KEY_STATE_RELEASED);
668 }
669
670 static void
671 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
672                        uint32_t serial, uint32_t mods_depressed,
673                        uint32_t mods_latched, uint32_t mods_locked,
674                        uint32_t group)
675 {
676         /* XXX notify_modifiers(); */
677 }
678
679 static const struct wl_keyboard_listener keyboard_listener = {
680         input_handle_keymap,
681         input_handle_keyboard_enter,
682         input_handle_keyboard_leave,
683         input_handle_key,
684         input_handle_modifiers,
685 };
686
687 static void
688 input_handle_capabilities(void *data, struct wl_seat *seat,
689                           enum wl_seat_capability caps)
690 {
691         struct wayland_input *input = data;
692
693         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
694                 input->pointer = wl_seat_get_pointer(seat);
695                 wl_pointer_set_user_data(input->pointer, input);
696                 wl_pointer_add_listener(input->pointer, &pointer_listener,
697                                         input);
698                 weston_seat_init_pointer(input->compositor->base.seat);
699         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
700                 wl_pointer_destroy(input->pointer);
701                 input->pointer = NULL;
702         }
703
704         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
705                 input->keyboard = wl_seat_get_keyboard(seat);
706                 wl_keyboard_set_user_data(input->keyboard, input);
707                 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
708                                          input);
709         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
710                 wl_keyboard_destroy(input->keyboard);
711                 input->keyboard = NULL;
712         }
713 }
714
715 static const struct wl_seat_listener seat_listener = {
716         input_handle_capabilities,
717 };
718
719 static void
720 display_add_seat(struct wayland_compositor *c, uint32_t id)
721 {
722         struct wayland_input *input;
723
724         input = malloc(sizeof *input);
725         if (input == NULL)
726                 return;
727
728         memset(input, 0, sizeof *input);
729
730         input->compositor = c;
731         input->seat = wl_display_bind(c->parent.wl_display, id,
732                                       &wl_seat_interface);
733         wl_list_insert(c->input_list.prev, &input->link);
734
735         wl_seat_add_listener(input->seat, &seat_listener, input);
736         wl_seat_set_user_data(input->seat, input);
737 }
738
739 /* We can't start adding input devices until weston_compositor_init, but
740  * also can't call weston_compositor_init until we've handled some of the
741  * base display code first.  So, we have a separate global handler for
742  * seats. */
743 static void
744 display_handle_global_input(struct wl_display *display, uint32_t id,
745                             const char *interface, uint32_t version,
746                             void *data)
747 {
748         struct wayland_compositor *c = data;
749
750         if (strcmp(interface, "wl_seat") == 0)
751                 display_add_seat(c, id);
752 }
753
754
755 static void
756 display_handle_global(struct wl_display *display, uint32_t id,
757                       const char *interface, uint32_t version, void *data)
758 {
759         struct wayland_compositor *c = data;
760
761         if (strcmp(interface, "wl_compositor") == 0) {
762                 c->parent.compositor =
763                         wl_display_bind(display, id, &wl_compositor_interface);
764         } else if (strcmp(interface, "wl_output") == 0) {
765                 c->parent.output =
766                         wl_display_bind(display, id, &wl_output_interface);
767                 wl_output_add_listener(c->parent.output, &output_listener, c);
768         } else if (strcmp(interface, "wl_shell") == 0) {
769                 c->parent.shell =
770                         wl_display_bind(display, id, &wl_shell_interface);
771         }
772 }
773
774 static int
775 update_event_mask(uint32_t mask, void *data)
776 {
777         struct wayland_compositor *c = data;
778
779         c->parent.event_mask = mask;
780         if (c->parent.wl_source)
781                 wl_event_source_fd_update(c->parent.wl_source, mask);
782
783         return 0;
784 }
785
786 static int
787 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
788 {
789         struct wayland_compositor *c = data;
790
791         if (mask & WL_EVENT_READABLE)
792                 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
793         if (mask & WL_EVENT_WRITABLE)
794                 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
795
796         return 1;
797 }
798
799 static int
800 wayland_input_create(struct wayland_compositor *c)
801 {
802         struct weston_seat *seat;
803
804         seat = malloc(sizeof *seat);
805         if (seat == NULL)
806                 return -1;
807
808         memset(seat, 0, sizeof *seat);
809         weston_seat_init(seat, &c->base);
810
811         c->base.seat = seat;
812
813         wl_display_add_global_listener(c->parent.wl_display,
814                                        display_handle_global_input,
815                                        c);
816
817         return 0;
818 }
819
820 static void
821 wayland_destroy(struct weston_compositor *ec)
822 {
823         weston_compositor_shutdown(ec);
824
825         free(ec);
826 }
827
828 static struct weston_compositor *
829 wayland_compositor_create(struct wl_display *display,
830                           int width, int height, const char *display_name,
831                           int argc, char *argv[], const char *config_file)
832 {
833         struct wayland_compositor *c;
834         struct wl_event_loop *loop;
835         int fd;
836
837         c = malloc(sizeof *c);
838         if (c == NULL)
839                 return NULL;
840
841         memset(c, 0, sizeof *c);
842
843         c->parent.wl_display = wl_display_connect(display_name);
844
845         if (c->parent.wl_display == NULL) {
846                 weston_log("failed to create display: %m\n");
847                 return NULL;
848         }
849
850         wl_list_init(&c->input_list);
851         wl_display_add_global_listener(c->parent.wl_display,
852                                 display_handle_global, c);
853
854         wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
855
856         c->base.wl_display = display;
857         if (wayland_compositor_init_egl(c) < 0)
858                 return NULL;
859
860         c->base.destroy = wayland_destroy;
861
862         /* Can't init base class until we have a current egl context */
863         if (weston_compositor_init(&c->base, display, argc, argv,
864                                    config_file) < 0)
865                 return NULL;
866
867         create_border(c);
868         if (wayland_compositor_create_output(c, width, height) < 0)
869                 return NULL;
870
871         if (wayland_input_create(c) < 0)
872                 return NULL;
873
874         loop = wl_display_get_event_loop(c->base.wl_display);
875
876         fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
877         c->parent.wl_source =
878                 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
879                                      wayland_compositor_handle_event, c);
880         if (c->parent.wl_source == NULL)
881                 return NULL;
882
883         return &c->base;
884 }
885
886 WL_EXPORT struct weston_compositor *
887 backend_init(struct wl_display *display, int argc, char *argv[],
888              const char *config_file)
889 {
890         int width = 1024, height = 640;
891         char *display_name = NULL;
892
893         const struct weston_option wayland_options[] = {
894                 { WESTON_OPTION_INTEGER, "width", 0, &width },
895                 { WESTON_OPTION_INTEGER, "height", 0, &height },
896                 { WESTON_OPTION_STRING, "display", 0, &display_name },
897         };
898
899         parse_options(wayland_options,
900                       ARRAY_LENGTH(wayland_options), argc, argv);
901
902         return wayland_compositor_create(display, width, height, display_name,
903                                          argc, argv, config_file);
904 }