blob: d3c205f092d3223dc3d68050aa37e103bba3096b [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
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
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010028#include <stdbool.h>
29#include <math.h>
30#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020031#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010032
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030033#include <linux/input.h>
34
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050036#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040037#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040041#include <EGL/eglext.h>
42
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080043#include "xdg-shell-client-protocol.h"
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090044#include <sys/types.h>
45#include <unistd.h>
46#include "protocol/ivi-application-client-protocol.h"
47#define IVI_SURFACE_ID 9000
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080048
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040049#ifndef EGL_EXT_swap_buffers_with_damage
50#define EGL_EXT_swap_buffers_with_damage 1
51typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
52#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010053
Kristian Høgsberg77787432013-08-22 12:16:51 -070054#ifndef EGL_EXT_buffer_age
55#define EGL_EXT_buffer_age 1
56#define EGL_BUFFER_AGE_EXT 0x313D
57#endif
58
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030059struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010060struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030061
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010062struct display {
63 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040064 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050065 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080066 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040067 struct wl_seat *seat;
68 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070069 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030070 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040071 struct wl_shm *shm;
72 struct wl_cursor_theme *cursor_theme;
73 struct wl_cursor *default_cursor;
74 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010075 struct {
76 EGLDisplay dpy;
77 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050078 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010079 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030080 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090081 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040082
83 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010084};
85
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030086struct geometry {
87 int width, height;
88};
89
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010090struct window {
91 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060092 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010093 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010094 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010095 GLuint pos;
96 GLuint col;
97 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050098
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080099 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500100 struct wl_egl_window *native;
101 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800102 struct xdg_surface *xdg_surface;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900103 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500104 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200105 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500106 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100107};
108
109static const char *vert_shader_text =
110 "uniform mat4 rotation;\n"
111 "attribute vec4 pos;\n"
112 "attribute vec4 color;\n"
113 "varying vec4 v_color;\n"
114 "void main() {\n"
115 " gl_Position = rotation * pos;\n"
116 " v_color = color;\n"
117 "}\n";
118
119static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500120 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100121 "varying vec4 v_color;\n"
122 "void main() {\n"
123 " gl_FragColor = v_color;\n"
124 "}\n";
125
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400126static int running = 1;
127
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100128static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700129init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100130{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500131 static const EGLint context_attribs[] = {
132 EGL_CONTEXT_CLIENT_VERSION, 2,
133 EGL_NONE
134 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400135 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500136
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300137 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500138 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500139 EGL_RED_SIZE, 1,
140 EGL_GREEN_SIZE, 1,
141 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200142 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500143 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
144 EGL_NONE
145 };
146
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700147 EGLint major, minor, n, count, i, size;
148 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100149 EGLBoolean ret;
150
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700151 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400152 config_attribs[9] = 0;
153
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400154 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100155 assert(display->egl.dpy);
156
157 ret = eglInitialize(display->egl.dpy, &major, &minor);
158 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500159 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100160 assert(ret == EGL_TRUE);
161
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700162 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
163 assert(0);
164
165 configs = calloc(count, sizeof *configs);
166 assert(configs);
167
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300168 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700169 configs, count, &n);
170 assert(ret && n >= 1);
171
172 for (i = 0; i < n; i++) {
173 eglGetConfigAttrib(display->egl.dpy,
174 configs[i], EGL_BUFFER_SIZE, &size);
175 if (window->buffer_size == size) {
176 display->egl.conf = configs[i];
177 break;
178 }
179 }
180 free(configs);
181 if (display->egl.conf == NULL) {
182 fprintf(stderr, "did not find config with buffer size %d\n",
183 window->buffer_size);
184 exit(EXIT_FAILURE);
185 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500186
187 display->egl.ctx = eglCreateContext(display->egl.dpy,
188 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500189 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100190 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500191
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400192 display->swap_buffers_with_damage = NULL;
193 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
194 if (extensions &&
195 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
196 strstr(extensions, "EGL_EXT_buffer_age"))
197 display->swap_buffers_with_damage =
198 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
199 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
200
201 if (display->swap_buffers_with_damage)
202 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
203
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100204}
205
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200206static void
207fini_egl(struct display *display)
208{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200209 eglTerminate(display->egl.dpy);
210 eglReleaseThread();
211}
212
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100213static GLuint
214create_shader(struct window *window, const char *source, GLenum shader_type)
215{
216 GLuint shader;
217 GLint status;
218
219 shader = glCreateShader(shader_type);
220 assert(shader != 0);
221
222 glShaderSource(shader, 1, (const char **) &source, NULL);
223 glCompileShader(shader);
224
225 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
226 if (!status) {
227 char log[1000];
228 GLsizei len;
229 glGetShaderInfoLog(shader, 1000, &len, log);
230 fprintf(stderr, "Error: compiling %s: %*s\n",
231 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
232 len, log);
233 exit(1);
234 }
235
236 return shader;
237}
238
239static void
240init_gl(struct window *window)
241{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100242 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600243 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100244 GLint status;
245
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100246 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
247 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
248
Scott Moreau3ea23d02012-06-13 17:42:21 -0600249 program = glCreateProgram();
250 glAttachShader(program, frag);
251 glAttachShader(program, vert);
252 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100253
Scott Moreau3ea23d02012-06-13 17:42:21 -0600254 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100255 if (!status) {
256 char log[1000];
257 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600258 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100259 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
260 exit(1);
261 }
262
Scott Moreau3ea23d02012-06-13 17:42:21 -0600263 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900264
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100265 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600266 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100267
Scott Moreau3ea23d02012-06-13 17:42:21 -0600268 glBindAttribLocation(program, window->gl.pos, "pos");
269 glBindAttribLocation(program, window->gl.col, "color");
270 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100271
272 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600273 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100274}
275
276static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800277handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700278 int32_t width, int32_t height,
279 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300280{
281 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700282 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300283
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700284 window->fullscreen = 0;
285 wl_array_for_each(p, states) {
286 uint32_t state = *p;
287 switch (state) {
288 case XDG_SURFACE_STATE_FULLSCREEN:
289 window->fullscreen = 1;
290 break;
291 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500292 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800293
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700294 if (width > 0 && height > 0) {
295 if (!window->fullscreen) {
296 window->window_size.width = width;
297 window->window_size.height = height;
298 }
299 window->geometry.width = width;
300 window->geometry.height = height;
301 } else if (!window->fullscreen) {
302 window->geometry = window->window_size;
303 }
304
305 if (window->native)
306 wl_egl_window_resize(window->native,
307 window->geometry.width,
308 window->geometry.height, 0, 0);
309
310 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800311}
312
313static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500314handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
315{
316 running = 0;
317}
318
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800319static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800320 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500321 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300322};
323
324static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900325handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
326 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100327{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900328 struct window *window = data;
329
330 wl_egl_window_resize(window->native, width, height, 0, 0);
331
332 window->geometry.width = width;
333 window->geometry.height = height;
334
335 if (!window->fullscreen)
336 window->window_size = window->geometry;
337}
338
339static const struct ivi_surface_listener ivi_surface_listener = {
340 handle_ivi_surface_configure,
341};
342
343static void
344create_xdg_surface(struct window *window, struct display *display)
345{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800346 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
347 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300348
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800349 xdg_surface_add_listener(window->xdg_surface,
350 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300351
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900352 xdg_surface_set_title(window->xdg_surface, "simple-egl");
353}
354
355static void
356create_ivi_surface(struct window *window, struct display *display)
357{
358 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
359 window->ivi_surface =
360 ivi_application_surface_create(display->ivi_application,
361 id_ivisurf, window->surface);
362
363 if (window->ivi_surface == NULL) {
364 fprintf(stderr, "Failed to create ivi_client_surface\n");
365 abort();
366 }
367
368 ivi_surface_add_listener(window->ivi_surface,
369 &ivi_surface_listener, window);
370}
371
372static void
373create_surface(struct window *window)
374{
375 struct display *display = window->display;
376 EGLBoolean ret;
377
378 window->surface = wl_compositor_create_surface(display->compositor);
379
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500380 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400381 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700382 window->geometry.width,
383 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500384 window->egl_surface =
385 eglCreateWindowSurface(display->egl.dpy,
386 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500387 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100388
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900389 if (display->shell) {
390 create_xdg_surface(window, display);
391 } else if (display->ivi_application ) {
392 create_ivi_surface(window, display);
393 } else {
394 assert(0);
395 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600396
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500397 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
398 window->egl_surface, window->display->egl.ctx);
399 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300400
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800401 if (!window->frame_sync)
402 eglSwapInterval(display->egl.dpy, 0);
403
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900404 if (!display->shell)
405 return;
406
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700407 if (window->fullscreen)
408 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100409}
410
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200411static void
412destroy_surface(struct window *window)
413{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000414 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
415 * on eglReleaseThread(). */
416 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
417 EGL_NO_CONTEXT);
418
419 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200420 wl_egl_window_destroy(window->native);
421
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900422 if (window->xdg_surface)
423 xdg_surface_destroy(window->xdg_surface);
424 if (window->display->ivi_application)
425 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200426 wl_surface_destroy(window->surface);
427
428 if (window->callback)
429 wl_callback_destroy(window->callback);
430}
431
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100432static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400433redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100434{
435 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400436 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100437 static const GLfloat verts[3][2] = {
438 { -0.5, -0.5 },
439 { 0.5, -0.5 },
440 { 0, 0.5 }
441 };
442 static const GLfloat colors[3][3] = {
443 { 1, 0, 0 },
444 { 0, 1, 0 },
445 { 0, 0, 1 }
446 };
447 GLfloat angle;
448 GLfloat rotation[4][4] = {
449 { 1, 0, 0, 0 },
450 { 0, 1, 0, 0 },
451 { 0, 0, 1, 0 },
452 { 0, 0, 0, 1 }
453 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100454 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400455 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400456 EGLint rect[4];
457 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800458 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100459
Scott Moreau7e300db2012-08-31 03:18:15 -0600460 assert(window->callback == callback);
461 window->callback = NULL;
462
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300463 if (callback)
464 wl_callback_destroy(callback);
465
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800466 gettimeofday(&tv, NULL);
467 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
468 if (window->frames == 0)
469 window->benchmark_time = time;
470 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
471 printf("%d frames in %d seconds: %f fps\n",
472 window->frames,
473 benchmark_interval,
474 (float) window->frames / benchmark_interval);
475 window->benchmark_time = time;
476 window->frames = 0;
477 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100478
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800479 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100480 rotation[0][0] = cos(angle);
481 rotation[0][2] = sin(angle);
482 rotation[2][0] = -sin(angle);
483 rotation[2][2] = cos(angle);
484
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400485 if (display->swap_buffers_with_damage)
486 eglQuerySurface(display->egl.dpy, window->egl_surface,
487 EGL_BUFFER_AGE_EXT, &buffer_age);
488
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300489 glViewport(0, 0, window->geometry.width, window->geometry.height);
490
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100491 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
492 (GLfloat *) rotation);
493
494 glClearColor(0.0, 0.0, 0.0, 0.5);
495 glClear(GL_COLOR_BUFFER_BIT);
496
497 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
498 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
499 glEnableVertexAttribArray(window->gl.pos);
500 glEnableVertexAttribArray(window->gl.col);
501
502 glDrawArrays(GL_TRIANGLES, 0, 3);
503
504 glDisableVertexAttribArray(window->gl.pos);
505 glDisableVertexAttribArray(window->gl.col);
506
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300507 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400508 region = wl_compositor_create_region(window->display->compositor);
509 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300510 window->geometry.width,
511 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400512 wl_surface_set_opaque_region(window->surface, region);
513 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700514 } else {
515 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400516 }
517
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400518 if (display->swap_buffers_with_damage && buffer_age > 0) {
519 rect[0] = window->geometry.width / 4 - 1;
520 rect[1] = window->geometry.height / 4 - 1;
521 rect[2] = window->geometry.width / 2 + 2;
522 rect[3] = window->geometry.height / 2 + 2;
523 display->swap_buffers_with_damage(display->egl.dpy,
524 window->egl_surface,
525 rect, 1);
526 } else {
527 eglSwapBuffers(display->egl.dpy, window->egl_surface);
528 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800529 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100530}
531
532static void
Daniel Stone37816df2012-05-16 18:45:18 +0100533pointer_handle_enter(void *data, struct wl_pointer *pointer,
534 uint32_t serial, struct wl_surface *surface,
535 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300536{
537 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400538 struct wl_buffer *buffer;
539 struct wl_cursor *cursor = display->default_cursor;
540 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300541
542 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300543 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400544 else if (cursor) {
545 image = display->default_cursor->images[0];
546 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100547 if (!buffer)
548 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400549 wl_pointer_set_cursor(pointer, serial,
550 display->cursor_surface,
551 image->hotspot_x,
552 image->hotspot_y);
553 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
554 wl_surface_damage(display->cursor_surface, 0, 0,
555 image->width, image->height);
556 wl_surface_commit(display->cursor_surface);
557 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300558}
559
560static void
Daniel Stone37816df2012-05-16 18:45:18 +0100561pointer_handle_leave(void *data, struct wl_pointer *pointer,
562 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300563{
564}
565
566static void
Daniel Stone37816df2012-05-16 18:45:18 +0100567pointer_handle_motion(void *data, struct wl_pointer *pointer,
568 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300569{
570}
571
572static void
Daniel Stone37816df2012-05-16 18:45:18 +0100573pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
574 uint32_t serial, uint32_t time, uint32_t button,
575 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300576{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300577 struct display *display = data;
578
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900579 if (!display->window->xdg_surface)
580 return;
581
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300582 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800583 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900584 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300585}
586
587static void
Daniel Stone37816df2012-05-16 18:45:18 +0100588pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100589 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300590{
591}
592
Daniel Stone37816df2012-05-16 18:45:18 +0100593static const struct wl_pointer_listener pointer_listener = {
594 pointer_handle_enter,
595 pointer_handle_leave,
596 pointer_handle_motion,
597 pointer_handle_button,
598 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300599};
600
601static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700602touch_handle_down(void *data, struct wl_touch *wl_touch,
603 uint32_t serial, uint32_t time, struct wl_surface *surface,
604 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
605{
606 struct display *d = (struct display *)data;
607
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900608 if (!d->shell)
609 return;
610
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800611 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700612}
613
614static void
615touch_handle_up(void *data, struct wl_touch *wl_touch,
616 uint32_t serial, uint32_t time, int32_t id)
617{
618}
619
620static void
621touch_handle_motion(void *data, struct wl_touch *wl_touch,
622 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
623{
624}
625
626static void
627touch_handle_frame(void *data, struct wl_touch *wl_touch)
628{
629}
630
631static void
632touch_handle_cancel(void *data, struct wl_touch *wl_touch)
633{
634}
635
636static const struct wl_touch_listener touch_listener = {
637 touch_handle_down,
638 touch_handle_up,
639 touch_handle_motion,
640 touch_handle_frame,
641 touch_handle_cancel,
642};
643
644static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300645keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
646 uint32_t format, int fd, uint32_t size)
647{
648}
649
650static void
651keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
652 uint32_t serial, struct wl_surface *surface,
653 struct wl_array *keys)
654{
655}
656
657static void
658keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
659 uint32_t serial, struct wl_surface *surface)
660{
661}
662
663static void
664keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
665 uint32_t serial, uint32_t time, uint32_t key,
666 uint32_t state)
667{
668 struct display *d = data;
669
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900670 if (!d->shell)
671 return;
672
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700673 if (key == KEY_F11 && state) {
674 if (d->window->fullscreen)
675 xdg_surface_unset_fullscreen(d->window->xdg_surface);
676 else
677 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
678 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400679 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300680}
681
682static void
683keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
684 uint32_t serial, uint32_t mods_depressed,
685 uint32_t mods_latched, uint32_t mods_locked,
686 uint32_t group)
687{
688}
689
690static const struct wl_keyboard_listener keyboard_listener = {
691 keyboard_handle_keymap,
692 keyboard_handle_enter,
693 keyboard_handle_leave,
694 keyboard_handle_key,
695 keyboard_handle_modifiers,
696};
697
698static void
Daniel Stone37816df2012-05-16 18:45:18 +0100699seat_handle_capabilities(void *data, struct wl_seat *seat,
700 enum wl_seat_capability caps)
701{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400702 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100703
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400704 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
705 d->pointer = wl_seat_get_pointer(seat);
706 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
707 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
708 wl_pointer_destroy(d->pointer);
709 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100710 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300711
712 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
713 d->keyboard = wl_seat_get_keyboard(seat);
714 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
715 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
716 wl_keyboard_destroy(d->keyboard);
717 d->keyboard = NULL;
718 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700719
720 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
721 d->touch = wl_seat_get_touch(seat);
722 wl_touch_set_user_data(d->touch, d);
723 wl_touch_add_listener(d->touch, &touch_listener, d);
724 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
725 wl_touch_destroy(d->touch);
726 d->touch = NULL;
727 }
Daniel Stone37816df2012-05-16 18:45:18 +0100728}
729
730static const struct wl_seat_listener seat_listener = {
731 seat_handle_capabilities,
732};
733
734static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800735xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
736{
737 xdg_shell_pong(shell, serial);
738}
739
740static const struct xdg_shell_listener xdg_shell_listener = {
741 xdg_shell_ping,
742};
743
Pekka Paalanen71182ae2014-08-21 17:47:20 +0300744#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800745#ifdef static_assert
746static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
747 "Interface version doesn't match implementation version");
748#endif
749
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800750static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400751registry_handle_global(void *data, struct wl_registry *registry,
752 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100753{
754 struct display *d = data;
755
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400756 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400757 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400758 wl_registry_bind(registry, name,
759 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800760 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400761 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800762 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800763 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800764 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100765 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400766 d->seat = wl_registry_bind(registry, name,
767 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400768 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400769 } else if (strcmp(interface, "wl_shm") == 0) {
770 d->shm = wl_registry_bind(registry, name,
771 &wl_shm_interface, 1);
772 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100773 if (!d->cursor_theme) {
774 fprintf(stderr, "unable to load default theme\n");
775 return;
776 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400777 d->default_cursor =
778 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100779 if (!d->default_cursor) {
780 fprintf(stderr, "unable to load default left pointer\n");
781 // TODO: abort ?
782 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900783 } else if (strcmp(interface, "ivi_application") == 0) {
784 d->ivi_application =
785 wl_registry_bind(registry, name,
786 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400787 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100788}
789
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200790static void
791registry_handle_global_remove(void *data, struct wl_registry *registry,
792 uint32_t name)
793{
794}
795
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400796static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200797 registry_handle_global,
798 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400799};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100800
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200801static void
802signal_int(int signum)
803{
804 running = 0;
805}
806
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400807static void
808usage(int error_code)
809{
810 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
811 " -f\tRun in fullscreen mode\n"
812 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700813 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800814 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400815 " -h\tThis help text\n\n");
816
817 exit(error_code);
818}
819
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100820int
821main(int argc, char **argv)
822{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200823 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100824 struct display display = { 0 };
825 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400826 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100827
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100828 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300829 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700830 window.geometry.width = 250;
831 window.geometry.height = 250;
832 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700833 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800834 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100835
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400836 for (i = 1; i < argc; i++) {
837 if (strcmp("-f", argv[i]) == 0)
838 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400839 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300840 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700841 else if (strcmp("-s", argv[i]) == 0)
842 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800843 else if (strcmp("-b", argv[i]) == 0)
844 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400845 else if (strcmp("-h", argv[i]) == 0)
846 usage(EXIT_SUCCESS);
847 else
848 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400849 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300850
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100851 display.display = wl_display_connect(NULL);
852 assert(display.display);
853
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400854 display.registry = wl_display_get_registry(display.display);
855 wl_registry_add_listener(display.registry,
856 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100857
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400858 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200859
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700860 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100861 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500862 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100863
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400864 display.cursor_surface =
865 wl_compositor_create_surface(display.compositor);
866
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200867 sigint.sa_handler = signal_int;
868 sigemptyset(&sigint.sa_mask);
869 sigint.sa_flags = SA_RESETHAND;
870 sigaction(SIGINT, &sigint, NULL);
871
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800872 /* The mainloop here is a little subtle. Redrawing will cause
873 * EGL to read events so we can just call
874 * wl_display_dispatch_pending() to handle any events that got
875 * queued up as a side effect. */
876 while (running && ret != -1) {
877 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800878 redraw(&window, NULL, 0);
879 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500880
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200881 fprintf(stderr, "simple-egl exiting\n");
882
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200883 destroy_surface(&window);
884 fini_egl(&display);
885
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400886 wl_surface_destroy(display.cursor_surface);
887 if (display.cursor_theme)
888 wl_cursor_theme_destroy(display.cursor_theme);
889
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200890 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800891 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200892
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900893 if (display.ivi_application)
894 ivi_application_destroy(display.ivi_application);
895
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200896 if (display.compositor)
897 wl_compositor_destroy(display.compositor);
898
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200899 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200900 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500901 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200902
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100903 return 0;
904}