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