compositor: Move repaint loop into a backend function
[profile/ivi/weston.git] / src / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37
38 #include <GLES2/gl2.h>
39 #include <GLES2/gl2ext.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42
43 #include "compositor.h"
44
45 struct wayland_compositor {
46         struct weston_compositor         base;
47
48         struct {
49                 struct wl_display *display;
50                 struct wl_compositor *compositor;
51                 struct wl_shell *shell;
52                 struct wl_output *output;
53
54                 struct {
55                         int32_t x, y, width, height;
56                 } screen_allocation;
57
58                 struct wl_event_source *wl_source;
59                 uint32_t event_mask;
60         } parent;
61
62         struct wl_list input_list;
63 };
64
65 struct wayland_output {
66         struct weston_output    base;
67
68         struct {
69                 struct wl_surface       *surface;
70                 struct wl_shell_surface *shell_surface;
71                 struct wl_egl_window    *egl_window;
72         } parent;
73         EGLSurface egl_surface;
74         struct weston_mode      mode;
75 };
76
77 struct wayland_input {
78         struct wayland_compositor *compositor;
79         struct wl_input_device *input_device;
80         struct wl_list link;
81 };
82
83 static int
84 wayland_input_create(struct wayland_compositor *c)
85 {
86         struct weston_input_device *input;
87
88         input = malloc(sizeof *input);
89         if (input == NULL)
90                 return -1;
91
92         memset(input, 0, sizeof *input);
93         weston_input_device_init(input, &c->base);
94
95         c->base.input_device = &input->input_device;
96
97         return 0;
98 }
99
100 static int
101 wayland_compositor_init_egl(struct wayland_compositor *c)
102 {
103         EGLint major, minor;
104         EGLint n;
105         const char *extensions;
106         EGLint config_attribs[] = {
107                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108                 EGL_RED_SIZE, 1,
109                 EGL_GREEN_SIZE, 1,
110                 EGL_BLUE_SIZE, 1,
111                 EGL_ALPHA_SIZE, 0,
112                 EGL_DEPTH_SIZE, 1,
113                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
114                 EGL_NONE
115         };
116         static const EGLint context_attribs[] = {
117                 EGL_CONTEXT_CLIENT_VERSION, 2,
118                 EGL_NONE
119         };
120
121         c->base.display = eglGetDisplay(c->parent.display);
122         if (c->base.display == NULL) {
123                 fprintf(stderr, "failed to create display\n");
124                 return -1;
125         }
126
127         if (!eglInitialize(c->base.display, &major, &minor)) {
128                 fprintf(stderr, "failed to initialize display\n");
129                 return -1;
130         }
131
132         extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
133         if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
134                 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
135                 return -1;
136         }
137
138         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
139                 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
140                 return -1;
141         }
142         if (!eglChooseConfig(c->base.display, config_attribs,
143                              &c->base.config, 1, &n) || n == 0) {
144                 fprintf(stderr, "failed to choose config: %d\n", n);
145                 return -1;
146         }
147
148         c->base.context = eglCreateContext(c->base.display, c->base.config,
149                                            EGL_NO_CONTEXT, context_attribs);
150         if (c->base.context == NULL) {
151                 fprintf(stderr, "failed to create context\n");
152                 return -1;
153         }
154
155         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
156                             EGL_NO_SURFACE, c->base.context)) {
157                 fprintf(stderr, "failed to make context current\n");
158                 return -1;
159         }
160
161         return 0;
162 }
163
164 static int
165 wayland_output_prepare_render(struct weston_output *output_base)
166 {
167         struct wayland_output *output = (struct wayland_output *) output_base;
168         struct weston_compositor *ec = output->base.compositor;
169
170         if (!eglMakeCurrent(ec->display, output->egl_surface,
171                             output->egl_surface, ec->context)) {
172                 fprintf(stderr, "failed to make current\n");
173                 return -1;
174         }
175
176         return 0;
177 }
178
179 static void
180 wayland_output_repaint(struct weston_output *output)
181 {
182         struct weston_compositor *compositor = output->compositor;
183         struct weston_surface *surface;
184
185         wl_list_for_each_reverse(surface, &compositor->surface_list, link)
186                 weston_surface_draw(surface, output);
187 }
188
189 static void
190 frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
191 {
192         struct weston_output *output = data;
193
194         weston_output_finish_frame(output, time);
195 }
196
197 static const struct wl_callback_listener frame_listener = {
198         frame_done
199 };
200
201 static int
202 wayland_output_present(struct weston_output *output_base)
203 {
204         struct wayland_output *output = (struct wayland_output *) output_base;
205         struct wayland_compositor *c =
206                 (struct wayland_compositor *) output->base.compositor;
207         struct wl_callback *callback;
208
209         if (wayland_output_prepare_render(&output->base))
210                 return -1;
211
212         eglSwapBuffers(c->base.display, output->egl_surface);
213         callback = wl_surface_frame(output->parent.surface);
214         wl_callback_add_listener(callback, &frame_listener, output);
215
216         return 0;
217 }
218
219 static int
220 wayland_output_prepare_scanout_surface(struct weston_output *output_base,
221                                        struct weston_surface *es)
222 {
223         return -1;
224 }
225
226 static int
227 wayland_output_set_cursor(struct weston_output *output_base,
228                           struct weston_input_device *input)
229 {
230         return -1;
231 }
232
233 static void
234 wayland_output_destroy(struct weston_output *output_base)
235 {
236         struct wayland_output *output = (struct wayland_output *) output_base;
237         struct weston_compositor *ec = output->base.compositor;
238
239         eglDestroySurface(ec->display, output->egl_surface);
240         wl_egl_window_destroy(output->parent.egl_window);
241         free(output);
242
243         return;
244 }
245
246 static int
247 wayland_compositor_create_output(struct wayland_compositor *c,
248                                  int width, int height)
249 {
250         struct wayland_output *output;
251
252         output = malloc(sizeof *output);
253         if (output == NULL)
254                 return -1;
255         memset(output, 0, sizeof *output);
256
257         output->mode.flags =
258                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
259         output->mode.width = width;
260         output->mode.height = height;
261         output->mode.refresh = 60;
262         wl_list_init(&output->base.mode_list);
263         wl_list_insert(&output->base.mode_list, &output->mode.link);
264
265         output->base.current = &output->mode;
266         weston_output_init(&output->base, &c->base, 0, 0, width, height,
267                          WL_OUTPUT_FLIPPED);
268
269         output->parent.surface =
270                 wl_compositor_create_surface(c->parent.compositor);
271         wl_surface_set_user_data(output->parent.surface, output);
272
273         output->parent.egl_window =
274                 wl_egl_window_create(output->parent.surface, width, height);
275         if (!output->parent.egl_window) {
276                 fprintf(stderr, "failure to create wl_egl_window\n");
277                 goto cleanup_output;
278         }
279
280         output->egl_surface =
281                 eglCreateWindowSurface(c->base.display, c->base.config,
282                                        output->parent.egl_window, NULL);
283         if (!output->egl_surface) {
284                 fprintf(stderr, "failed to create window surface\n");
285                 goto cleanup_window;
286         }
287
288         if (!eglMakeCurrent(c->base.display, output->egl_surface,
289                             output->egl_surface, c->base.context)) {
290                 fprintf(stderr, "failed to make surface current\n");
291                 goto cleanup_surface;
292                 return -1;
293         }
294
295         output->parent.shell_surface =
296                 wl_shell_get_shell_surface(c->parent.shell,
297                                            output->parent.surface);
298         /* FIXME: add shell_surface listener for resizing */
299         wl_shell_surface_set_toplevel(output->parent.shell_surface);
300
301         glClearColor(0, 0, 0, 0.5);
302
303         output->base.prepare_render = wayland_output_prepare_render;
304         output->base.repaint = wayland_output_repaint;
305         output->base.present = wayland_output_present;
306         output->base.prepare_scanout_surface =
307                 wayland_output_prepare_scanout_surface;
308         output->base.set_hardware_cursor = wayland_output_set_cursor;
309         output->base.destroy = wayland_output_destroy;
310
311         wl_list_insert(c->base.output_list.prev, &output->base.link);
312
313         return 0;
314
315 cleanup_surface:
316         eglDestroySurface(c->base.display, output->egl_surface);
317 cleanup_window:
318         wl_egl_window_destroy(output->parent.egl_window);
319 cleanup_output:
320         /* FIXME: cleanup weston_output */
321         free(output);
322
323         return -1;
324 }
325
326 /* Events received from the wayland-server this compositor is client of: */
327
328 /* parent output interface */
329 static void
330 display_handle_geometry(void *data,
331                         struct wl_output *wl_output,
332                         int x,
333                         int y,
334                         int physical_width,
335                         int physical_height,
336                         int subpixel,
337                         const char *make,
338                         const char *model)
339 {
340         struct wayland_compositor *c = data;
341
342         c->parent.screen_allocation.x = x;
343         c->parent.screen_allocation.y = y;
344 }
345
346 static void
347 display_handle_mode(void *data,
348                     struct wl_output *wl_output,
349                     uint32_t flags,
350                     int width,
351                     int height,
352                     int refresh)
353 {
354         struct wayland_compositor *c = data;
355
356         c->parent.screen_allocation.width = width;
357         c->parent.screen_allocation.height = height;
358 }
359
360 static const struct wl_output_listener output_listener = {
361         display_handle_geometry,
362         display_handle_mode
363 };
364
365 /* parent input interface */
366 static void
367 input_handle_motion(void *data, struct wl_input_device *input_device,
368                      uint32_t time,
369                      int32_t x, int32_t y, int32_t sx, int32_t sy)
370 {
371         struct wayland_input *input = data;
372         struct wayland_compositor *c = input->compositor;
373
374         notify_motion(c->base.input_device, time, sx, sy);
375 }
376
377 static void
378 input_handle_button(void *data,
379                      struct wl_input_device *input_device,
380                      uint32_t time, uint32_t button, uint32_t state)
381 {
382         struct wayland_input *input = data;
383         struct wayland_compositor *c = input->compositor;
384
385         notify_button(c->base.input_device, time, button, state);
386 }
387
388 static void
389 input_handle_key(void *data, struct wl_input_device *input_device,
390                   uint32_t time, uint32_t key, uint32_t state)
391 {
392         struct wayland_input *input = data;
393         struct wayland_compositor *c = input->compositor;
394
395         notify_key(c->base.input_device, time, key, state);
396 }
397
398 static void
399 input_handle_pointer_focus(void *data,
400                             struct wl_input_device *input_device,
401                             uint32_t time, struct wl_surface *surface,
402                             int32_t x, int32_t y, int32_t sx, int32_t sy)
403 {
404         struct wayland_input *input = data;
405         struct wayland_output *output;
406         struct wayland_compositor *c = input->compositor;
407
408         if (surface) {
409                 output = wl_surface_get_user_data(surface);
410                 notify_pointer_focus(c->base.input_device,
411                                      time, &output->base, sx, sy);
412         } else {
413                 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
414         }
415 }
416
417 static void
418 input_handle_keyboard_focus(void *data,
419                              struct wl_input_device *input_device,
420                              uint32_t time,
421                              struct wl_surface *surface,
422                              struct wl_array *keys)
423 {
424         struct wayland_input *input = data;
425         struct wayland_compositor *c = input->compositor;
426         struct wayland_output *output;
427
428         if (surface) {
429                 output = wl_surface_get_user_data(surface);
430                 notify_keyboard_focus(c->base.input_device,
431                                       time, &output->base, keys);
432         } else {
433                 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
434         }
435 }
436
437 static const struct wl_input_device_listener input_device_listener = {
438         input_handle_motion,
439         input_handle_button,
440         input_handle_key,
441         input_handle_pointer_focus,
442         input_handle_keyboard_focus,
443 };
444
445 static void
446 display_add_input(struct wayland_compositor *c, uint32_t id)
447 {
448         struct wayland_input *input;
449
450         input = malloc(sizeof *input);
451         if (input == NULL)
452                 return;
453
454         memset(input, 0, sizeof *input);
455
456         input->compositor = c;
457         input->input_device = wl_display_bind(c->parent.display,
458                                               id, &wl_input_device_interface);
459         wl_list_insert(c->input_list.prev, &input->link);
460
461         wl_input_device_add_listener(input->input_device,
462                                      &input_device_listener, input);
463         wl_input_device_set_user_data(input->input_device, input);
464 }
465
466 static void
467 display_handle_global(struct wl_display *display, uint32_t id,
468                       const char *interface, uint32_t version, void *data)
469 {
470         struct wayland_compositor *c = data;
471
472         if (strcmp(interface, "wl_compositor") == 0) {
473                 c->parent.compositor =
474                         wl_display_bind(display, id, &wl_compositor_interface);
475         } else if (strcmp(interface, "wl_output") == 0) {
476                 c->parent.output =
477                         wl_display_bind(display, id, &wl_output_interface);
478                 wl_output_add_listener(c->parent.output, &output_listener, c);
479         } else if (strcmp(interface, "wl_input_device") == 0) {
480                 display_add_input(c, id);
481         } else if (strcmp(interface, "wl_shell") == 0) {
482                 c->parent.shell =
483                         wl_display_bind(display, id, &wl_shell_interface);
484         }
485 }
486
487 static int
488 update_event_mask(uint32_t mask, void *data)
489 {
490         struct wayland_compositor *c = data;
491
492         c->parent.event_mask = mask;
493         if (c->parent.wl_source)
494                 wl_event_source_fd_update(c->parent.wl_source, mask);
495
496         return 0;
497 }
498
499 static int
500 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
501 {
502         struct wayland_compositor *c = data;
503
504         if (mask & WL_EVENT_READABLE)
505                 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
506         if (mask & WL_EVENT_WRITABLE)
507                 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
508
509         return 1;
510 }
511
512 static void
513 wayland_destroy(struct weston_compositor *ec)
514 {
515         weston_compositor_shutdown(ec);
516
517         free(ec);
518 }
519
520 static struct weston_compositor *
521 wayland_compositor_create(struct wl_display *display, int width, int height)
522 {
523         struct wayland_compositor *c;
524         struct wl_event_loop *loop;
525         int fd;
526
527         c = malloc(sizeof *c);
528         if (c == NULL)
529                 return NULL;
530
531         memset(c, 0, sizeof *c);
532
533         c->parent.display = wl_display_connect(NULL);
534
535         if (c->parent.display == NULL) {
536                 fprintf(stderr, "failed to create display: %m\n");
537                 return NULL;
538         }
539
540         wl_list_init(&c->input_list);
541         wl_display_add_global_listener(c->parent.display,
542                                 display_handle_global, c);
543
544         wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
545
546         c->base.wl_display = display;
547         if (wayland_compositor_init_egl(c) < 0)
548                 return NULL;
549
550         c->base.destroy = wayland_destroy;
551
552         /* Can't init base class until we have a current egl context */
553         if (weston_compositor_init(&c->base, display) < 0)
554                 return NULL;
555
556         if (wayland_compositor_create_output(c, width, height) < 0)
557                 return NULL;
558
559         if (wayland_input_create(c) < 0)
560                 return NULL;
561
562         loop = wl_display_get_event_loop(c->base.wl_display);
563
564         fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
565         c->parent.wl_source =
566                 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
567                                      wayland_compositor_handle_event, c);
568         if (c->parent.wl_source == NULL)
569                 return NULL;
570
571         return &c->base;
572 }
573
574 struct weston_compositor *
575 backend_init(struct wl_display *display, char *options);
576
577 WL_EXPORT struct weston_compositor *
578 backend_init(struct wl_display *display, char *options)
579 {
580         int width = 1024, height = 640, i;
581         char *p, *value;
582
583         static char * const tokens[] = { "width", "height", NULL };
584         
585         p = options;
586         while (i = getsubopt(&p, tokens, &value), i != -1) {
587                 switch (i) {
588                 case 0:
589                         width = strtol(value, NULL, 0);
590                         break;
591                 case 1:
592                         height = strtol(value, NULL, 0);
593                         break;
594                 }
595         }
596
597         return wayland_compositor_create(display, width, height);
598 }