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