Use enum wl_pointer_button_state instead of integer
[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
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, int32_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, state);
611 }
612
613 static void
614 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
615                        uint32_t serial, uint32_t mods_depressed,
616                        uint32_t mods_latched, uint32_t mods_locked,
617                        uint32_t group)
618 {
619         /* XXX notify_modifiers(); */
620 }
621
622 static const struct wl_keyboard_listener keyboard_listener = {
623         input_handle_keyboard_enter,
624         input_handle_keyboard_leave,
625         input_handle_key,
626         input_handle_modifiers,
627 };
628
629 static void
630 input_handle_capabilities(void *data, struct wl_seat *seat,
631                           enum wl_seat_capability caps)
632 {
633         struct wayland_input *input = data;
634
635         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
636                 input->pointer = wl_seat_get_pointer(seat);
637                 wl_pointer_set_user_data(input->pointer, input);
638                 wl_pointer_add_listener(input->pointer, &pointer_listener,
639                                         input);
640         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
641                 wl_pointer_destroy(input->pointer);
642                 input->pointer = NULL;
643         }
644
645         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
646                 input->keyboard = wl_seat_get_keyboard(seat);
647                 wl_keyboard_set_user_data(input->keyboard, input);
648                 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
649                                          input);
650         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
651                 wl_keyboard_destroy(input->keyboard);
652                 input->keyboard = NULL;
653         }
654 }
655
656 static const struct wl_seat_listener seat_listener = {
657         input_handle_capabilities,
658 };
659
660 static void
661 display_add_seat(struct wayland_compositor *c, uint32_t id)
662 {
663         struct wayland_input *input;
664
665         input = malloc(sizeof *input);
666         if (input == NULL)
667                 return;
668
669         memset(input, 0, sizeof *input);
670
671         input->compositor = c;
672         input->seat = wl_display_bind(c->parent.display, id,
673                                       &wl_seat_interface);
674         wl_list_insert(c->input_list.prev, &input->link);
675
676         wl_seat_add_listener(input->seat, &seat_listener, input);
677         wl_seat_set_user_data(input->seat, input);
678 }
679
680 static void
681 display_handle_global(struct wl_display *display, uint32_t id,
682                       const char *interface, uint32_t version, void *data)
683 {
684         struct wayland_compositor *c = data;
685
686         if (strcmp(interface, "wl_compositor") == 0) {
687                 c->parent.compositor =
688                         wl_display_bind(display, id, &wl_compositor_interface);
689         } else if (strcmp(interface, "wl_output") == 0) {
690                 c->parent.output =
691                         wl_display_bind(display, id, &wl_output_interface);
692                 wl_output_add_listener(c->parent.output, &output_listener, c);
693         } else if (strcmp(interface, "wl_seat") == 0) {
694                 display_add_seat(c, id);
695         } else if (strcmp(interface, "wl_shell") == 0) {
696                 c->parent.shell =
697                         wl_display_bind(display, id, &wl_shell_interface);
698         }
699 }
700
701 static int
702 update_event_mask(uint32_t mask, void *data)
703 {
704         struct wayland_compositor *c = data;
705
706         c->parent.event_mask = mask;
707         if (c->parent.wl_source)
708                 wl_event_source_fd_update(c->parent.wl_source, mask);
709
710         return 0;
711 }
712
713 static int
714 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
715 {
716         struct wayland_compositor *c = data;
717
718         if (mask & WL_EVENT_READABLE)
719                 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
720         if (mask & WL_EVENT_WRITABLE)
721                 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
722
723         return 1;
724 }
725
726 static void
727 wayland_destroy(struct weston_compositor *ec)
728 {
729         weston_compositor_shutdown(ec);
730
731         free(ec);
732 }
733
734 static struct weston_compositor *
735 wayland_compositor_create(struct wl_display *display,
736                           int width, int height, const char *display_name)
737 {
738         struct wayland_compositor *c;
739         struct wl_event_loop *loop;
740         int fd;
741
742         c = malloc(sizeof *c);
743         if (c == NULL)
744                 return NULL;
745
746         memset(c, 0, sizeof *c);
747
748         c->parent.display = wl_display_connect(display_name);
749
750         if (c->parent.display == NULL) {
751                 fprintf(stderr, "failed to create display: %m\n");
752                 return NULL;
753         }
754
755         wl_list_init(&c->input_list);
756         wl_display_add_global_listener(c->parent.display,
757                                 display_handle_global, c);
758
759         wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
760
761         c->base.wl_display = display;
762         if (wayland_compositor_init_egl(c) < 0)
763                 return NULL;
764
765         c->base.destroy = wayland_destroy;
766
767         /* Can't init base class until we have a current egl context */
768         if (weston_compositor_init(&c->base, display) < 0)
769                 return NULL;
770
771         create_border(c);
772         if (wayland_compositor_create_output(c, width, height) < 0)
773                 return NULL;
774
775         if (wayland_input_create(c) < 0)
776                 return NULL;
777
778         loop = wl_display_get_event_loop(c->base.wl_display);
779
780         fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
781         c->parent.wl_source =
782                 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
783                                      wayland_compositor_handle_event, c);
784         if (c->parent.wl_source == NULL)
785                 return NULL;
786
787         return &c->base;
788 }
789
790 WL_EXPORT struct weston_compositor *
791 backend_init(struct wl_display *display, int argc, char *argv[])
792 {
793         int width = 1024, height = 640;
794         char *display_name = NULL;
795
796         const struct weston_option wayland_options[] = {
797                 { WESTON_OPTION_INTEGER, "width", 0, &width },
798                 { WESTON_OPTION_INTEGER, "height", 0, &height },
799                 { WESTON_OPTION_STRING, "display", 0, &display_name },
800         };
801
802         parse_options(wayland_options,
803                       ARRAY_LENGTH(wayland_options), argc, argv);
804
805         return wayland_compositor_create(display, width, height, display_name);
806 }