desktop-shell: Fix black edges on scaled desktop pattern
[profile/ivi/weston-ivi-shell.git] / clients / simple-egl.c
1 /*
2  * Copyright © 2011 Benjamin Franzke
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <math.h>
30 #include <assert.h>
31 #include <signal.h>
32
33 #include <linux/input.h>
34
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37 #include <wayland-cursor.h>
38
39 #include <GLES2/gl2.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42
43 #include "xdg-shell-client-protocol.h"
44
45 #ifndef EGL_EXT_swap_buffers_with_damage
46 #define EGL_EXT_swap_buffers_with_damage 1
47 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
48 #endif
49
50 #ifndef EGL_EXT_buffer_age
51 #define EGL_EXT_buffer_age 1
52 #define EGL_BUFFER_AGE_EXT                      0x313D
53 #endif
54
55 struct window;
56 struct seat;
57
58 struct display {
59         struct wl_display *display;
60         struct wl_registry *registry;
61         struct wl_compositor *compositor;
62         struct xdg_shell *shell;
63         struct wl_seat *seat;
64         struct wl_pointer *pointer;
65         struct wl_touch *touch;
66         struct wl_keyboard *keyboard;
67         struct wl_shm *shm;
68         struct wl_cursor_theme *cursor_theme;
69         struct wl_cursor *default_cursor;
70         struct wl_surface *cursor_surface;
71         struct {
72                 EGLDisplay dpy;
73                 EGLContext ctx;
74                 EGLConfig conf;
75         } egl;
76         struct window *window;
77
78         PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
79 };
80
81 struct geometry {
82         int width, height;
83 };
84
85 struct window {
86         struct display *display;
87         struct geometry geometry, window_size;
88         struct {
89                 GLuint rotation_uniform;
90                 GLuint pos;
91                 GLuint col;
92         } gl;
93
94         uint32_t benchmark_time, frames;
95         struct wl_egl_window *native;
96         struct wl_surface *surface;
97         struct xdg_surface *xdg_surface;
98         EGLSurface egl_surface;
99         struct wl_callback *callback;
100         int fullscreen, opaque, buffer_size, frame_sync;
101 };
102
103 static const char *vert_shader_text =
104         "uniform mat4 rotation;\n"
105         "attribute vec4 pos;\n"
106         "attribute vec4 color;\n"
107         "varying vec4 v_color;\n"
108         "void main() {\n"
109         "  gl_Position = rotation * pos;\n"
110         "  v_color = color;\n"
111         "}\n";
112
113 static const char *frag_shader_text =
114         "precision mediump float;\n"
115         "varying vec4 v_color;\n"
116         "void main() {\n"
117         "  gl_FragColor = v_color;\n"
118         "}\n";
119
120 static int running = 1;
121
122 static void
123 init_egl(struct display *display, struct window *window)
124 {
125         static const EGLint context_attribs[] = {
126                 EGL_CONTEXT_CLIENT_VERSION, 2,
127                 EGL_NONE
128         };
129         const char *extensions;
130
131         EGLint config_attribs[] = {
132                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
133                 EGL_RED_SIZE, 1,
134                 EGL_GREEN_SIZE, 1,
135                 EGL_BLUE_SIZE, 1,
136                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
137                 EGL_NONE
138         };
139
140         EGLint major, minor, n, count, i, size;
141         EGLConfig *configs;
142         EGLBoolean ret;
143
144         if (window->opaque || window->buffer_size == 16)
145                 config_attribs[9] = 0;
146
147         display->egl.dpy = eglGetDisplay(display->display);
148         assert(display->egl.dpy);
149
150         ret = eglInitialize(display->egl.dpy, &major, &minor);
151         assert(ret == EGL_TRUE);
152         ret = eglBindAPI(EGL_OPENGL_ES_API);
153         assert(ret == EGL_TRUE);
154
155         if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
156                 assert(0);
157
158         configs = calloc(count, sizeof *configs);
159         assert(configs);
160
161         ret = eglChooseConfig(display->egl.dpy, config_attribs,
162                               configs, count, &n);
163         assert(ret && n >= 1);
164
165         for (i = 0; i < n; i++) {
166                 eglGetConfigAttrib(display->egl.dpy,
167                                    configs[i], EGL_BUFFER_SIZE, &size);
168                 if (window->buffer_size == size) {
169                         display->egl.conf = configs[i];
170                         break;
171                 }
172         }
173         free(configs);
174         if (display->egl.conf == NULL) {
175                 fprintf(stderr, "did not find config with buffer size %d\n",
176                         window->buffer_size);
177                 exit(EXIT_FAILURE);
178         }
179
180         display->egl.ctx = eglCreateContext(display->egl.dpy,
181                                             display->egl.conf,
182                                             EGL_NO_CONTEXT, context_attribs);
183         assert(display->egl.ctx);
184
185         display->swap_buffers_with_damage = NULL;
186         extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
187         if (extensions &&
188             strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
189             strstr(extensions, "EGL_EXT_buffer_age"))
190                 display->swap_buffers_with_damage =
191                         (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
192                         eglGetProcAddress("eglSwapBuffersWithDamageEXT");
193
194         if (display->swap_buffers_with_damage)
195                 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
196
197 }
198
199 static void
200 fini_egl(struct display *display)
201 {
202         eglTerminate(display->egl.dpy);
203         eglReleaseThread();
204 }
205
206 static GLuint
207 create_shader(struct window *window, const char *source, GLenum shader_type)
208 {
209         GLuint shader;
210         GLint status;
211
212         shader = glCreateShader(shader_type);
213         assert(shader != 0);
214
215         glShaderSource(shader, 1, (const char **) &source, NULL);
216         glCompileShader(shader);
217
218         glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
219         if (!status) {
220                 char log[1000];
221                 GLsizei len;
222                 glGetShaderInfoLog(shader, 1000, &len, log);
223                 fprintf(stderr, "Error: compiling %s: %*s\n",
224                         shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
225                         len, log);
226                 exit(1);
227         }
228
229         return shader;
230 }
231
232 static void
233 init_gl(struct window *window)
234 {
235         GLuint frag, vert;
236         GLuint program;
237         GLint status;
238
239         frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
240         vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
241
242         program = glCreateProgram();
243         glAttachShader(program, frag);
244         glAttachShader(program, vert);
245         glLinkProgram(program);
246
247         glGetProgramiv(program, GL_LINK_STATUS, &status);
248         if (!status) {
249                 char log[1000];
250                 GLsizei len;
251                 glGetProgramInfoLog(program, 1000, &len, log);
252                 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
253                 exit(1);
254         }
255
256         glUseProgram(program);
257         
258         window->gl.pos = 0;
259         window->gl.col = 1;
260
261         glBindAttribLocation(program, window->gl.pos, "pos");
262         glBindAttribLocation(program, window->gl.col, "color");
263         glLinkProgram(program);
264
265         window->gl.rotation_uniform =
266                 glGetUniformLocation(program, "rotation");
267 }
268
269 static void
270 handle_surface_configure(void *data, struct xdg_surface *surface,
271                          int32_t width, int32_t height)
272 {
273         struct window *window = data;
274
275         if (window->native)
276                 wl_egl_window_resize(window->native, width, height, 0, 0);
277
278         window->geometry.width = width;
279         window->geometry.height = height;
280
281         if (!window->fullscreen)
282                 window->window_size = window->geometry;
283 }
284
285 static void
286 handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
287                             uint32_t state,
288                             uint32_t value,
289                             uint32_t serial)
290 {
291         struct window *window = data;
292
293         switch (state) {
294         case XDG_SURFACE_STATE_FULLSCREEN:
295                 window->fullscreen = value;
296
297                 if (!value)
298                         handle_surface_configure(window, window->xdg_surface,
299                                                  window->window_size.width,
300                                                  window->window_size.height);
301                 break;
302         }
303
304         xdg_surface_ack_change_state(xdg_surface, state, value, serial);
305 }
306
307 static void
308 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
309 {
310 }
311
312 static void
313 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
314 {
315 }
316
317 static void
318 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
319 {
320         running = 0;
321 }
322
323 static const struct xdg_surface_listener xdg_surface_listener = {
324         handle_surface_configure,
325         handle_surface_change_state,
326         handle_surface_activated,
327         handle_surface_deactivated,
328         handle_surface_delete,
329 };
330
331 static void
332 create_surface(struct window *window)
333 {
334         struct display *display = window->display;
335         EGLBoolean ret;
336         
337         window->surface = wl_compositor_create_surface(display->compositor);
338         window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
339                                                         window->surface);
340
341         xdg_surface_add_listener(window->xdg_surface,
342                                  &xdg_surface_listener, window);
343
344         window->native =
345                 wl_egl_window_create(window->surface,
346                                      window->window_size.width,
347                                      window->window_size.height);
348         window->egl_surface =
349                 eglCreateWindowSurface(display->egl.dpy,
350                                        display->egl.conf,
351                                        window->native, NULL);
352
353         xdg_surface_set_title(window->xdg_surface, "simple-egl");
354
355         ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
356                              window->egl_surface, window->display->egl.ctx);
357         assert(ret == EGL_TRUE);
358
359         if (!window->frame_sync)
360                 eglSwapInterval(display->egl.dpy, 0);
361
362         xdg_surface_request_change_state(window->xdg_surface,
363                                          XDG_SURFACE_STATE_FULLSCREEN,
364                                          window->fullscreen, 0);
365 }
366
367 static void
368 destroy_surface(struct window *window)
369 {
370         /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
371          * on eglReleaseThread(). */
372         eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
373                        EGL_NO_CONTEXT);
374
375         eglDestroySurface(window->display->egl.dpy, window->egl_surface);
376         wl_egl_window_destroy(window->native);
377
378         xdg_surface_destroy(window->xdg_surface);
379         wl_surface_destroy(window->surface);
380
381         if (window->callback)
382                 wl_callback_destroy(window->callback);
383 }
384
385 static const struct wl_callback_listener frame_listener;
386
387 static void
388 redraw(void *data, struct wl_callback *callback, uint32_t time)
389 {
390         struct window *window = data;
391         struct display *display = window->display;
392         static const GLfloat verts[3][2] = {
393                 { -0.5, -0.5 },
394                 {  0.5, -0.5 },
395                 {  0,    0.5 }
396         };
397         static const GLfloat colors[3][3] = {
398                 { 1, 0, 0 },
399                 { 0, 1, 0 },
400                 { 0, 0, 1 }
401         };
402         GLfloat angle;
403         GLfloat rotation[4][4] = {
404                 { 1, 0, 0, 0 },
405                 { 0, 1, 0, 0 },
406                 { 0, 0, 1, 0 },
407                 { 0, 0, 0, 1 }
408         };
409         static const uint32_t speed_div = 5, benchmark_interval = 5;
410         struct wl_region *region;
411         EGLint rect[4];
412         EGLint buffer_age = 0;
413         struct timeval tv;
414
415         assert(window->callback == callback);
416         window->callback = NULL;
417
418         if (callback)
419                 wl_callback_destroy(callback);
420
421         gettimeofday(&tv, NULL);
422         time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
423         if (window->frames == 0)
424                 window->benchmark_time = time;
425         if (time - window->benchmark_time > (benchmark_interval * 1000)) {
426                 printf("%d frames in %d seconds: %f fps\n",
427                        window->frames,
428                        benchmark_interval,
429                        (float) window->frames / benchmark_interval);
430                 window->benchmark_time = time;
431                 window->frames = 0;
432         }
433
434         angle = (time / speed_div) % 360 * M_PI / 180.0;
435         rotation[0][0] =  cos(angle);
436         rotation[0][2] =  sin(angle);
437         rotation[2][0] = -sin(angle);
438         rotation[2][2] =  cos(angle);
439
440         if (display->swap_buffers_with_damage)
441                 eglQuerySurface(display->egl.dpy, window->egl_surface,
442                                 EGL_BUFFER_AGE_EXT, &buffer_age);
443
444         glViewport(0, 0, window->geometry.width, window->geometry.height);
445
446         glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
447                            (GLfloat *) rotation);
448
449         glClearColor(0.0, 0.0, 0.0, 0.5);
450         glClear(GL_COLOR_BUFFER_BIT);
451
452         glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
453         glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
454         glEnableVertexAttribArray(window->gl.pos);
455         glEnableVertexAttribArray(window->gl.col);
456
457         glDrawArrays(GL_TRIANGLES, 0, 3);
458
459         glDisableVertexAttribArray(window->gl.pos);
460         glDisableVertexAttribArray(window->gl.col);
461
462         if (window->opaque || window->fullscreen) {
463                 region = wl_compositor_create_region(window->display->compositor);
464                 wl_region_add(region, 0, 0,
465                               window->geometry.width,
466                               window->geometry.height);
467                 wl_surface_set_opaque_region(window->surface, region);
468                 wl_region_destroy(region);
469         } else {
470                 wl_surface_set_opaque_region(window->surface, NULL);
471         }
472
473         if (display->swap_buffers_with_damage && buffer_age > 0) {
474                 rect[0] = window->geometry.width / 4 - 1;
475                 rect[1] = window->geometry.height / 4 - 1;
476                 rect[2] = window->geometry.width / 2 + 2;
477                 rect[3] = window->geometry.height / 2 + 2;
478                 display->swap_buffers_with_damage(display->egl.dpy,
479                                                   window->egl_surface,
480                                                   rect, 1);
481         } else {
482                 eglSwapBuffers(display->egl.dpy, window->egl_surface);
483         }
484         window->frames++;
485 }
486
487 static const struct wl_callback_listener frame_listener = {
488         redraw
489 };
490
491 static void
492 pointer_handle_enter(void *data, struct wl_pointer *pointer,
493                      uint32_t serial, struct wl_surface *surface,
494                      wl_fixed_t sx, wl_fixed_t sy)
495 {
496         struct display *display = data;
497         struct wl_buffer *buffer;
498         struct wl_cursor *cursor = display->default_cursor;
499         struct wl_cursor_image *image;
500
501         if (display->window->fullscreen)
502                 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
503         else if (cursor) {
504                 image = display->default_cursor->images[0];
505                 buffer = wl_cursor_image_get_buffer(image);
506                 if (!buffer)
507                         return;
508                 wl_pointer_set_cursor(pointer, serial,
509                                       display->cursor_surface,
510                                       image->hotspot_x,
511                                       image->hotspot_y);
512                 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
513                 wl_surface_damage(display->cursor_surface, 0, 0,
514                                   image->width, image->height);
515                 wl_surface_commit(display->cursor_surface);
516         }
517 }
518
519 static void
520 pointer_handle_leave(void *data, struct wl_pointer *pointer,
521                      uint32_t serial, struct wl_surface *surface)
522 {
523 }
524
525 static void
526 pointer_handle_motion(void *data, struct wl_pointer *pointer,
527                       uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
528 {
529 }
530
531 static void
532 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
533                       uint32_t serial, uint32_t time, uint32_t button,
534                       uint32_t state)
535 {
536         struct display *display = data;
537
538         if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
539                 xdg_surface_move(display->window->xdg_surface,
540                                  display->seat, serial);
541 }
542
543 static void
544 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
545                     uint32_t time, uint32_t axis, wl_fixed_t value)
546 {
547 }
548
549 static const struct wl_pointer_listener pointer_listener = {
550         pointer_handle_enter,
551         pointer_handle_leave,
552         pointer_handle_motion,
553         pointer_handle_button,
554         pointer_handle_axis,
555 };
556
557 static void
558 touch_handle_down(void *data, struct wl_touch *wl_touch,
559                   uint32_t serial, uint32_t time, struct wl_surface *surface,
560                   int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
561 {
562         struct display *d = (struct display *)data;
563
564         xdg_surface_move(d->window->xdg_surface, d->seat, serial);
565 }
566
567 static void
568 touch_handle_up(void *data, struct wl_touch *wl_touch,
569                 uint32_t serial, uint32_t time, int32_t id)
570 {
571 }
572
573 static void
574 touch_handle_motion(void *data, struct wl_touch *wl_touch,
575                     uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
576 {
577 }
578
579 static void
580 touch_handle_frame(void *data, struct wl_touch *wl_touch)
581 {
582 }
583
584 static void
585 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
586 {
587 }
588
589 static const struct wl_touch_listener touch_listener = {
590         touch_handle_down,
591         touch_handle_up,
592         touch_handle_motion,
593         touch_handle_frame,
594         touch_handle_cancel,
595 };
596
597 static void
598 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
599                        uint32_t format, int fd, uint32_t size)
600 {
601 }
602
603 static void
604 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
605                       uint32_t serial, struct wl_surface *surface,
606                       struct wl_array *keys)
607 {
608 }
609
610 static void
611 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
612                       uint32_t serial, struct wl_surface *surface)
613 {
614 }
615
616 static void
617 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
618                     uint32_t serial, uint32_t time, uint32_t key,
619                     uint32_t state)
620 {
621         struct display *d = data;
622
623         if (key == KEY_F11 && state)
624                 xdg_surface_request_change_state(d->window->xdg_surface,
625                                                  XDG_SURFACE_STATE_FULLSCREEN,
626                                                  !d->window->fullscreen, 0);
627         else if (key == KEY_ESC && state)
628                 running = 0;
629 }
630
631 static void
632 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
633                           uint32_t serial, uint32_t mods_depressed,
634                           uint32_t mods_latched, uint32_t mods_locked,
635                           uint32_t group)
636 {
637 }
638
639 static const struct wl_keyboard_listener keyboard_listener = {
640         keyboard_handle_keymap,
641         keyboard_handle_enter,
642         keyboard_handle_leave,
643         keyboard_handle_key,
644         keyboard_handle_modifiers,
645 };
646
647 static void
648 seat_handle_capabilities(void *data, struct wl_seat *seat,
649                          enum wl_seat_capability caps)
650 {
651         struct display *d = data;
652
653         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
654                 d->pointer = wl_seat_get_pointer(seat);
655                 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
656         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
657                 wl_pointer_destroy(d->pointer);
658                 d->pointer = NULL;
659         }
660
661         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
662                 d->keyboard = wl_seat_get_keyboard(seat);
663                 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
664         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
665                 wl_keyboard_destroy(d->keyboard);
666                 d->keyboard = NULL;
667         }
668
669         if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
670                 d->touch = wl_seat_get_touch(seat);
671                 wl_touch_set_user_data(d->touch, d);
672                 wl_touch_add_listener(d->touch, &touch_listener, d);
673         } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
674                 wl_touch_destroy(d->touch);
675                 d->touch = NULL;
676         }
677 }
678
679 static const struct wl_seat_listener seat_listener = {
680         seat_handle_capabilities,
681 };
682
683 static void
684 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
685 {
686         xdg_shell_pong(shell, serial);
687 }
688
689 static const struct xdg_shell_listener xdg_shell_listener = {
690         xdg_shell_ping,
691 };
692
693 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
694 #ifdef static_assert
695 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
696               "Interface version doesn't match implementation version");
697 #endif
698
699 static void
700 registry_handle_global(void *data, struct wl_registry *registry,
701                        uint32_t name, const char *interface, uint32_t version)
702 {
703         struct display *d = data;
704
705         if (strcmp(interface, "wl_compositor") == 0) {
706                 d->compositor =
707                         wl_registry_bind(registry, name,
708                                          &wl_compositor_interface, 1);
709         } else if (strcmp(interface, "xdg_shell") == 0) {
710                 d->shell = wl_registry_bind(registry, name,
711                                             &xdg_shell_interface, 1);
712                 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
713                 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
714         } else if (strcmp(interface, "wl_seat") == 0) {
715                 d->seat = wl_registry_bind(registry, name,
716                                            &wl_seat_interface, 1);
717                 wl_seat_add_listener(d->seat, &seat_listener, d);
718         } else if (strcmp(interface, "wl_shm") == 0) {
719                 d->shm = wl_registry_bind(registry, name,
720                                           &wl_shm_interface, 1);
721                 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
722                 if (!d->cursor_theme) {
723                         fprintf(stderr, "unable to load default theme\n");
724                         return;
725                 }
726                 d->default_cursor =
727                         wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
728                 if (!d->default_cursor) {
729                         fprintf(stderr, "unable to load default left pointer\n");
730                         // TODO: abort ?
731                 }
732         }
733 }
734
735 static void
736 registry_handle_global_remove(void *data, struct wl_registry *registry,
737                               uint32_t name)
738 {
739 }
740
741 static const struct wl_registry_listener registry_listener = {
742         registry_handle_global,
743         registry_handle_global_remove
744 };
745
746 static void
747 signal_int(int signum)
748 {
749         running = 0;
750 }
751
752 static void
753 usage(int error_code)
754 {
755         fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
756                 "  -f\tRun in fullscreen mode\n"
757                 "  -o\tCreate an opaque surface\n"
758                 "  -s\tUse a 16 bpp EGL config\n"
759                 "  -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
760                 "  -h\tThis help text\n\n");
761
762         exit(error_code);
763 }
764
765 int
766 main(int argc, char **argv)
767 {
768         struct sigaction sigint;
769         struct display display = { 0 };
770         struct window  window  = { 0 };
771         int i, ret = 0;
772
773         window.display = &display;
774         display.window = &window;
775         window.window_size.width  = 250;
776         window.window_size.height = 250;
777         window.buffer_size = 32;
778         window.frame_sync = 1;
779
780         for (i = 1; i < argc; i++) {
781                 if (strcmp("-f", argv[i]) == 0)
782                         window.fullscreen = 1;
783                 else if (strcmp("-o", argv[i]) == 0)
784                         window.opaque = 1;
785                 else if (strcmp("-s", argv[i]) == 0)
786                         window.buffer_size = 16;
787                 else if (strcmp("-b", argv[i]) == 0)
788                         window.frame_sync = 0;
789                 else if (strcmp("-h", argv[i]) == 0)
790                         usage(EXIT_SUCCESS);
791                 else
792                         usage(EXIT_FAILURE);
793         }
794
795         display.display = wl_display_connect(NULL);
796         assert(display.display);
797
798         display.registry = wl_display_get_registry(display.display);
799         wl_registry_add_listener(display.registry,
800                                  &registry_listener, &display);
801
802         wl_display_dispatch(display.display);
803
804         init_egl(&display, &window);
805         create_surface(&window);
806         init_gl(&window);
807
808         display.cursor_surface =
809                 wl_compositor_create_surface(display.compositor);
810
811         sigint.sa_handler = signal_int;
812         sigemptyset(&sigint.sa_mask);
813         sigint.sa_flags = SA_RESETHAND;
814         sigaction(SIGINT, &sigint, NULL);
815
816         /* The mainloop here is a little subtle.  Redrawing will cause
817          * EGL to read events so we can just call
818          * wl_display_dispatch_pending() to handle any events that got
819          * queued up as a side effect. */
820         while (running && ret != -1) {
821                 wl_display_dispatch_pending(display.display);
822                 redraw(&window, NULL, 0);
823         }
824
825         fprintf(stderr, "simple-egl exiting\n");
826
827         destroy_surface(&window);
828         fini_egl(&display);
829
830         wl_surface_destroy(display.cursor_surface);
831         if (display.cursor_theme)
832                 wl_cursor_theme_destroy(display.cursor_theme);
833
834         if (display.shell)
835                 xdg_shell_destroy(display.shell);
836
837         if (display.compositor)
838                 wl_compositor_destroy(display.compositor);
839
840         wl_registry_destroy(display.registry);
841         wl_display_flush(display.display);
842         wl_display_disconnect(display.display);
843
844         return 0;
845 }