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