weston-editor --help works
[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                          struct wl_array *states, uint32_t serial)
273 {
274         struct window *window = data;
275         uint32_t *p;
276
277         window->fullscreen = 0;
278         wl_array_for_each(p, states) {
279                 uint32_t state = *p;
280                 switch (state) {
281                 case XDG_SURFACE_STATE_FULLSCREEN:
282                         window->fullscreen = 1;
283                         break;
284                 }
285         }
286
287         if (width > 0 && height > 0) {
288                 if (!window->fullscreen) {
289                         window->window_size.width = width;
290                         window->window_size.height = height;
291                 }
292                 window->geometry.width = width;
293                 window->geometry.height = height;
294         } else if (!window->fullscreen) {
295                 window->geometry = window->window_size;
296         }
297
298         if (window->native)
299                 wl_egl_window_resize(window->native,
300                                      window->geometry.width,
301                                      window->geometry.height, 0, 0);
302
303         xdg_surface_ack_configure(surface, serial);
304 }
305
306 static void
307 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
308 {
309         running = 0;
310 }
311
312 static const struct xdg_surface_listener xdg_surface_listener = {
313         handle_surface_configure,
314         handle_surface_delete,
315 };
316
317 static void
318 create_surface(struct window *window)
319 {
320         struct display *display = window->display;
321         EGLBoolean ret;
322         
323         window->surface = wl_compositor_create_surface(display->compositor);
324         window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
325                                                         window->surface);
326
327         xdg_surface_add_listener(window->xdg_surface,
328                                  &xdg_surface_listener, window);
329
330         window->native =
331                 wl_egl_window_create(window->surface,
332                                      window->geometry.width,
333                                      window->geometry.height);
334         window->egl_surface =
335                 eglCreateWindowSurface(display->egl.dpy,
336                                        display->egl.conf,
337                                        window->native, NULL);
338
339         xdg_surface_set_title(window->xdg_surface, "simple-egl");
340
341         ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
342                              window->egl_surface, window->display->egl.ctx);
343         assert(ret == EGL_TRUE);
344
345         if (!window->frame_sync)
346                 eglSwapInterval(display->egl.dpy, 0);
347
348         if (window->fullscreen)
349                 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
350 }
351
352 static void
353 destroy_surface(struct window *window)
354 {
355         /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
356          * on eglReleaseThread(). */
357         eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
358                        EGL_NO_CONTEXT);
359
360         eglDestroySurface(window->display->egl.dpy, window->egl_surface);
361         wl_egl_window_destroy(window->native);
362
363         xdg_surface_destroy(window->xdg_surface);
364         wl_surface_destroy(window->surface);
365
366         if (window->callback)
367                 wl_callback_destroy(window->callback);
368 }
369
370 static void
371 redraw(void *data, struct wl_callback *callback, uint32_t time)
372 {
373         struct window *window = data;
374         struct display *display = window->display;
375         static const GLfloat verts[3][2] = {
376                 { -0.5, -0.5 },
377                 {  0.5, -0.5 },
378                 {  0,    0.5 }
379         };
380         static const GLfloat colors[3][3] = {
381                 { 1, 0, 0 },
382                 { 0, 1, 0 },
383                 { 0, 0, 1 }
384         };
385         GLfloat angle;
386         GLfloat rotation[4][4] = {
387                 { 1, 0, 0, 0 },
388                 { 0, 1, 0, 0 },
389                 { 0, 0, 1, 0 },
390                 { 0, 0, 0, 1 }
391         };
392         static const uint32_t speed_div = 5, benchmark_interval = 5;
393         struct wl_region *region;
394         EGLint rect[4];
395         EGLint buffer_age = 0;
396         struct timeval tv;
397
398         assert(window->callback == callback);
399         window->callback = NULL;
400
401         if (callback)
402                 wl_callback_destroy(callback);
403
404         gettimeofday(&tv, NULL);
405         time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
406         if (window->frames == 0)
407                 window->benchmark_time = time;
408         if (time - window->benchmark_time > (benchmark_interval * 1000)) {
409                 printf("%d frames in %d seconds: %f fps\n",
410                        window->frames,
411                        benchmark_interval,
412                        (float) window->frames / benchmark_interval);
413                 window->benchmark_time = time;
414                 window->frames = 0;
415         }
416
417         angle = (time / speed_div) % 360 * M_PI / 180.0;
418         rotation[0][0] =  cos(angle);
419         rotation[0][2] =  sin(angle);
420         rotation[2][0] = -sin(angle);
421         rotation[2][2] =  cos(angle);
422
423         if (display->swap_buffers_with_damage)
424                 eglQuerySurface(display->egl.dpy, window->egl_surface,
425                                 EGL_BUFFER_AGE_EXT, &buffer_age);
426
427         glViewport(0, 0, window->geometry.width, window->geometry.height);
428
429         glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
430                            (GLfloat *) rotation);
431
432         glClearColor(0.0, 0.0, 0.0, 0.5);
433         glClear(GL_COLOR_BUFFER_BIT);
434
435         glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
436         glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
437         glEnableVertexAttribArray(window->gl.pos);
438         glEnableVertexAttribArray(window->gl.col);
439
440         glDrawArrays(GL_TRIANGLES, 0, 3);
441
442         glDisableVertexAttribArray(window->gl.pos);
443         glDisableVertexAttribArray(window->gl.col);
444
445         if (window->opaque || window->fullscreen) {
446                 region = wl_compositor_create_region(window->display->compositor);
447                 wl_region_add(region, 0, 0,
448                               window->geometry.width,
449                               window->geometry.height);
450                 wl_surface_set_opaque_region(window->surface, region);
451                 wl_region_destroy(region);
452         } else {
453                 wl_surface_set_opaque_region(window->surface, NULL);
454         }
455
456         if (display->swap_buffers_with_damage && buffer_age > 0) {
457                 rect[0] = window->geometry.width / 4 - 1;
458                 rect[1] = window->geometry.height / 4 - 1;
459                 rect[2] = window->geometry.width / 2 + 2;
460                 rect[3] = window->geometry.height / 2 + 2;
461                 display->swap_buffers_with_damage(display->egl.dpy,
462                                                   window->egl_surface,
463                                                   rect, 1);
464         } else {
465                 eglSwapBuffers(display->egl.dpy, window->egl_surface);
466         }
467         window->frames++;
468 }
469
470 static void
471 pointer_handle_enter(void *data, struct wl_pointer *pointer,
472                      uint32_t serial, struct wl_surface *surface,
473                      wl_fixed_t sx, wl_fixed_t sy)
474 {
475         struct display *display = data;
476         struct wl_buffer *buffer;
477         struct wl_cursor *cursor = display->default_cursor;
478         struct wl_cursor_image *image;
479
480         if (display->window->fullscreen)
481                 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
482         else if (cursor) {
483                 image = display->default_cursor->images[0];
484                 buffer = wl_cursor_image_get_buffer(image);
485                 if (!buffer)
486                         return;
487                 wl_pointer_set_cursor(pointer, serial,
488                                       display->cursor_surface,
489                                       image->hotspot_x,
490                                       image->hotspot_y);
491                 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
492                 wl_surface_damage(display->cursor_surface, 0, 0,
493                                   image->width, image->height);
494                 wl_surface_commit(display->cursor_surface);
495         }
496 }
497
498 static void
499 pointer_handle_leave(void *data, struct wl_pointer *pointer,
500                      uint32_t serial, struct wl_surface *surface)
501 {
502 }
503
504 static void
505 pointer_handle_motion(void *data, struct wl_pointer *pointer,
506                       uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
507 {
508 }
509
510 static void
511 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
512                       uint32_t serial, uint32_t time, uint32_t button,
513                       uint32_t state)
514 {
515         struct display *display = data;
516
517         if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
518                 xdg_surface_move(display->window->xdg_surface,
519                                  display->seat, serial);
520 }
521
522 static void
523 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
524                     uint32_t time, uint32_t axis, wl_fixed_t value)
525 {
526 }
527
528 static const struct wl_pointer_listener pointer_listener = {
529         pointer_handle_enter,
530         pointer_handle_leave,
531         pointer_handle_motion,
532         pointer_handle_button,
533         pointer_handle_axis,
534 };
535
536 static void
537 touch_handle_down(void *data, struct wl_touch *wl_touch,
538                   uint32_t serial, uint32_t time, struct wl_surface *surface,
539                   int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
540 {
541         struct display *d = (struct display *)data;
542
543         xdg_surface_move(d->window->xdg_surface, d->seat, serial);
544 }
545
546 static void
547 touch_handle_up(void *data, struct wl_touch *wl_touch,
548                 uint32_t serial, uint32_t time, int32_t id)
549 {
550 }
551
552 static void
553 touch_handle_motion(void *data, struct wl_touch *wl_touch,
554                     uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
555 {
556 }
557
558 static void
559 touch_handle_frame(void *data, struct wl_touch *wl_touch)
560 {
561 }
562
563 static void
564 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
565 {
566 }
567
568 static const struct wl_touch_listener touch_listener = {
569         touch_handle_down,
570         touch_handle_up,
571         touch_handle_motion,
572         touch_handle_frame,
573         touch_handle_cancel,
574 };
575
576 static void
577 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
578                        uint32_t format, int fd, uint32_t size)
579 {
580 }
581
582 static void
583 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
584                       uint32_t serial, struct wl_surface *surface,
585                       struct wl_array *keys)
586 {
587 }
588
589 static void
590 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
591                       uint32_t serial, struct wl_surface *surface)
592 {
593 }
594
595 static void
596 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
597                     uint32_t serial, uint32_t time, uint32_t key,
598                     uint32_t state)
599 {
600         struct display *d = data;
601
602         if (key == KEY_F11 && state) {
603                 if (d->window->fullscreen)
604                         xdg_surface_unset_fullscreen(d->window->xdg_surface);
605                 else
606                         xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
607         } else if (key == KEY_ESC && state)
608                 running = 0;
609 }
610
611 static void
612 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
613                           uint32_t serial, uint32_t mods_depressed,
614                           uint32_t mods_latched, uint32_t mods_locked,
615                           uint32_t group)
616 {
617 }
618
619 static const struct wl_keyboard_listener keyboard_listener = {
620         keyboard_handle_keymap,
621         keyboard_handle_enter,
622         keyboard_handle_leave,
623         keyboard_handle_key,
624         keyboard_handle_modifiers,
625 };
626
627 static void
628 seat_handle_capabilities(void *data, struct wl_seat *seat,
629                          enum wl_seat_capability caps)
630 {
631         struct display *d = data;
632
633         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
634                 d->pointer = wl_seat_get_pointer(seat);
635                 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
636         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
637                 wl_pointer_destroy(d->pointer);
638                 d->pointer = NULL;
639         }
640
641         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
642                 d->keyboard = wl_seat_get_keyboard(seat);
643                 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
644         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
645                 wl_keyboard_destroy(d->keyboard);
646                 d->keyboard = NULL;
647         }
648
649         if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
650                 d->touch = wl_seat_get_touch(seat);
651                 wl_touch_set_user_data(d->touch, d);
652                 wl_touch_add_listener(d->touch, &touch_listener, d);
653         } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
654                 wl_touch_destroy(d->touch);
655                 d->touch = NULL;
656         }
657 }
658
659 static const struct wl_seat_listener seat_listener = {
660         seat_handle_capabilities,
661 };
662
663 static void
664 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
665 {
666         xdg_shell_pong(shell, serial);
667 }
668
669 static const struct xdg_shell_listener xdg_shell_listener = {
670         xdg_shell_ping,
671 };
672
673 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
674 #ifdef static_assert
675 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
676               "Interface version doesn't match implementation version");
677 #endif
678
679 static void
680 registry_handle_global(void *data, struct wl_registry *registry,
681                        uint32_t name, const char *interface, uint32_t version)
682 {
683         struct display *d = data;
684
685         if (strcmp(interface, "wl_compositor") == 0) {
686                 d->compositor =
687                         wl_registry_bind(registry, name,
688                                          &wl_compositor_interface, 1);
689         } else if (strcmp(interface, "xdg_shell") == 0) {
690                 d->shell = wl_registry_bind(registry, name,
691                                             &xdg_shell_interface, 1);
692                 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
693                 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
694         } else if (strcmp(interface, "wl_seat") == 0) {
695                 d->seat = wl_registry_bind(registry, name,
696                                            &wl_seat_interface, 1);
697                 wl_seat_add_listener(d->seat, &seat_listener, d);
698         } else if (strcmp(interface, "wl_shm") == 0) {
699                 d->shm = wl_registry_bind(registry, name,
700                                           &wl_shm_interface, 1);
701                 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
702                 if (!d->cursor_theme) {
703                         fprintf(stderr, "unable to load default theme\n");
704                         return;
705                 }
706                 d->default_cursor =
707                         wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
708                 if (!d->default_cursor) {
709                         fprintf(stderr, "unable to load default left pointer\n");
710                         // TODO: abort ?
711                 }
712         }
713 }
714
715 static void
716 registry_handle_global_remove(void *data, struct wl_registry *registry,
717                               uint32_t name)
718 {
719 }
720
721 static const struct wl_registry_listener registry_listener = {
722         registry_handle_global,
723         registry_handle_global_remove
724 };
725
726 static void
727 signal_int(int signum)
728 {
729         running = 0;
730 }
731
732 static void
733 usage(int error_code)
734 {
735         fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
736                 "  -f\tRun in fullscreen mode\n"
737                 "  -o\tCreate an opaque surface\n"
738                 "  -s\tUse a 16 bpp EGL config\n"
739                 "  -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
740                 "  -h\tThis help text\n\n");
741
742         exit(error_code);
743 }
744
745 int
746 main(int argc, char **argv)
747 {
748         struct sigaction sigint;
749         struct display display = { 0 };
750         struct window  window  = { 0 };
751         int i, ret = 0;
752
753         window.display = &display;
754         display.window = &window;
755         window.geometry.width  = 250;
756         window.geometry.height = 250;
757         window.window_size = window.geometry;
758         window.buffer_size = 32;
759         window.frame_sync = 1;
760
761         for (i = 1; i < argc; i++) {
762                 if (strcmp("-f", argv[i]) == 0)
763                         window.fullscreen = 1;
764                 else if (strcmp("-o", argv[i]) == 0)
765                         window.opaque = 1;
766                 else if (strcmp("-s", argv[i]) == 0)
767                         window.buffer_size = 16;
768                 else if (strcmp("-b", argv[i]) == 0)
769                         window.frame_sync = 0;
770                 else if (strcmp("-h", argv[i]) == 0)
771                         usage(EXIT_SUCCESS);
772                 else
773                         usage(EXIT_FAILURE);
774         }
775
776         display.display = wl_display_connect(NULL);
777         assert(display.display);
778
779         display.registry = wl_display_get_registry(display.display);
780         wl_registry_add_listener(display.registry,
781                                  &registry_listener, &display);
782
783         wl_display_dispatch(display.display);
784
785         init_egl(&display, &window);
786         create_surface(&window);
787         init_gl(&window);
788
789         display.cursor_surface =
790                 wl_compositor_create_surface(display.compositor);
791
792         sigint.sa_handler = signal_int;
793         sigemptyset(&sigint.sa_mask);
794         sigint.sa_flags = SA_RESETHAND;
795         sigaction(SIGINT, &sigint, NULL);
796
797         /* The mainloop here is a little subtle.  Redrawing will cause
798          * EGL to read events so we can just call
799          * wl_display_dispatch_pending() to handle any events that got
800          * queued up as a side effect. */
801         while (running && ret != -1) {
802                 wl_display_dispatch_pending(display.display);
803                 redraw(&window, NULL, 0);
804         }
805
806         fprintf(stderr, "simple-egl exiting\n");
807
808         destroy_surface(&window);
809         fini_egl(&display);
810
811         wl_surface_destroy(display.cursor_surface);
812         if (display.cursor_theme)
813                 wl_cursor_theme_destroy(display.cursor_theme);
814
815         if (display.shell)
816                 xdg_shell_destroy(display.shell);
817
818         if (display.compositor)
819                 wl_compositor_destroy(display.compositor);
820
821         wl_registry_destroy(display.registry);
822         wl_display_flush(display.display);
823         wl_display_disconnect(display.display);
824
825         return 0;
826 }