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