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