blob: 6b4f1a10eb1596a11fe2a506842dfd6cc06bc964 [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010022 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010029#include <stdbool.h>
30#include <math.h>
31#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020032#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010033
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030034#include <linux/input.h>
35
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010036#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050037#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040038#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010041#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040042#include <EGL/eglext.h>
43
Jonas Ådahl2a229332015-11-17 16:00:32 +080044#include "xdg-shell-unstable-v5-client-protocol.h"
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090045#include <sys/types.h>
46#include <unistd.h>
47#include "protocol/ivi-application-client-protocol.h"
48#define IVI_SURFACE_ID 9000
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080049
Jon Cruz4678bab2015-06-15 15:37:07 -070050#include "shared/platform.h"
Emil Velikov0725cf12016-07-04 15:27:15 +010051#include "weston-egl-ext.h"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010052
Kristian Høgsberg77787432013-08-22 12:16:51 -070053#ifndef EGL_EXT_buffer_age
54#define EGL_EXT_buffer_age 1
55#define EGL_BUFFER_AGE_EXT 0x313D
56#endif
57
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030058struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010059struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030060
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010061struct display {
62 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040063 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050064 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080065 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040066 struct wl_seat *seat;
67 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070068 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030069 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040070 struct wl_shm *shm;
71 struct wl_cursor_theme *cursor_theme;
72 struct wl_cursor *default_cursor;
73 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010074 struct {
75 EGLDisplay dpy;
76 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050077 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010078 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030079 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090080 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040081
82 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010083};
84
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030085struct geometry {
86 int width, height;
87};
88
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010089struct window {
90 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060091 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010092 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010093 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010094 GLuint pos;
95 GLuint col;
96 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050097
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080098 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050099 struct wl_egl_window *native;
100 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800101 struct xdg_surface *xdg_surface;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900102 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500103 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200104 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500105 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100106};
107
108static const char *vert_shader_text =
109 "uniform mat4 rotation;\n"
110 "attribute vec4 pos;\n"
111 "attribute vec4 color;\n"
112 "varying vec4 v_color;\n"
113 "void main() {\n"
114 " gl_Position = rotation * pos;\n"
115 " v_color = color;\n"
116 "}\n";
117
118static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500119 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100120 "varying vec4 v_color;\n"
121 "void main() {\n"
122 " gl_FragColor = v_color;\n"
123 "}\n";
124
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400125static int running = 1;
126
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100127static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700128init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100129{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500130 static const EGLint context_attribs[] = {
131 EGL_CONTEXT_CLIENT_VERSION, 2,
132 EGL_NONE
133 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400134 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500135
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300136 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500137 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500138 EGL_RED_SIZE, 1,
139 EGL_GREEN_SIZE, 1,
140 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200141 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500142 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
143 EGL_NONE
144 };
145
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700146 EGLint major, minor, n, count, i, size;
147 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100148 EGLBoolean ret;
149
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700150 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400151 config_attribs[9] = 0;
152
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100153 display->egl.dpy =
154 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
155 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100156 assert(display->egl.dpy);
157
158 ret = eglInitialize(display->egl.dpy, &major, &minor);
159 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500160 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100161 assert(ret == EGL_TRUE);
162
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700163 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
164 assert(0);
165
166 configs = calloc(count, sizeof *configs);
167 assert(configs);
168
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300169 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700170 configs, count, &n);
171 assert(ret && n >= 1);
172
173 for (i = 0; i < n; i++) {
174 eglGetConfigAttrib(display->egl.dpy,
175 configs[i], EGL_BUFFER_SIZE, &size);
176 if (window->buffer_size == size) {
177 display->egl.conf = configs[i];
178 break;
179 }
180 }
181 free(configs);
182 if (display->egl.conf == NULL) {
183 fprintf(stderr, "did not find config with buffer size %d\n",
184 window->buffer_size);
185 exit(EXIT_FAILURE);
186 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500187
188 display->egl.ctx = eglCreateContext(display->egl.dpy,
189 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500190 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100191 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500192
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400193 display->swap_buffers_with_damage = NULL;
194 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
195 if (extensions &&
196 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
197 strstr(extensions, "EGL_EXT_buffer_age"))
198 display->swap_buffers_with_damage =
199 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
200 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
201
202 if (display->swap_buffers_with_damage)
203 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
204
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100205}
206
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200207static void
208fini_egl(struct display *display)
209{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200210 eglTerminate(display->egl.dpy);
211 eglReleaseThread();
212}
213
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100214static GLuint
215create_shader(struct window *window, const char *source, GLenum shader_type)
216{
217 GLuint shader;
218 GLint status;
219
220 shader = glCreateShader(shader_type);
221 assert(shader != 0);
222
223 glShaderSource(shader, 1, (const char **) &source, NULL);
224 glCompileShader(shader);
225
226 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
227 if (!status) {
228 char log[1000];
229 GLsizei len;
230 glGetShaderInfoLog(shader, 1000, &len, log);
231 fprintf(stderr, "Error: compiling %s: %*s\n",
232 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
233 len, log);
234 exit(1);
235 }
236
237 return shader;
238}
239
240static void
241init_gl(struct window *window)
242{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100243 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600244 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100245 GLint status;
246
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100247 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
248 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
249
Scott Moreau3ea23d02012-06-13 17:42:21 -0600250 program = glCreateProgram();
251 glAttachShader(program, frag);
252 glAttachShader(program, vert);
253 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100254
Scott Moreau3ea23d02012-06-13 17:42:21 -0600255 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100256 if (!status) {
257 char log[1000];
258 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600259 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100260 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
261 exit(1);
262 }
263
Scott Moreau3ea23d02012-06-13 17:42:21 -0600264 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900265
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100266 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600267 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100268
Scott Moreau3ea23d02012-06-13 17:42:21 -0600269 glBindAttribLocation(program, window->gl.pos, "pos");
270 glBindAttribLocation(program, window->gl.col, "color");
271 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100272
273 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600274 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100275}
276
277static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800278handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700279 int32_t width, int32_t height,
280 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300281{
282 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700283 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300284
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700285 window->fullscreen = 0;
286 wl_array_for_each(p, states) {
287 uint32_t state = *p;
288 switch (state) {
289 case XDG_SURFACE_STATE_FULLSCREEN:
290 window->fullscreen = 1;
291 break;
292 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500293 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800294
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700295 if (width > 0 && height > 0) {
296 if (!window->fullscreen) {
297 window->window_size.width = width;
298 window->window_size.height = height;
299 }
300 window->geometry.width = width;
301 window->geometry.height = height;
302 } else if (!window->fullscreen) {
303 window->geometry = window->window_size;
304 }
305
306 if (window->native)
307 wl_egl_window_resize(window->native,
308 window->geometry.width,
309 window->geometry.height, 0, 0);
310
311 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800312}
313
314static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500315handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
316{
317 running = 0;
318}
319
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800320static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800321 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500322 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300323};
324
325static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900326handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
327 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100328{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900329 struct window *window = data;
330
331 wl_egl_window_resize(window->native, width, height, 0, 0);
332
333 window->geometry.width = width;
334 window->geometry.height = height;
335
336 if (!window->fullscreen)
337 window->window_size = window->geometry;
338}
339
340static const struct ivi_surface_listener ivi_surface_listener = {
341 handle_ivi_surface_configure,
342};
343
344static void
345create_xdg_surface(struct window *window, struct display *display)
346{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800347 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
348 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300349
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800350 xdg_surface_add_listener(window->xdg_surface,
351 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300352
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900353 xdg_surface_set_title(window->xdg_surface, "simple-egl");
354}
355
356static void
357create_ivi_surface(struct window *window, struct display *display)
358{
359 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
360 window->ivi_surface =
361 ivi_application_surface_create(display->ivi_application,
362 id_ivisurf, window->surface);
363
364 if (window->ivi_surface == NULL) {
365 fprintf(stderr, "Failed to create ivi_client_surface\n");
366 abort();
367 }
368
369 ivi_surface_add_listener(window->ivi_surface,
370 &ivi_surface_listener, window);
371}
372
373static void
374create_surface(struct window *window)
375{
376 struct display *display = window->display;
377 EGLBoolean ret;
378
379 window->surface = wl_compositor_create_surface(display->compositor);
380
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500381 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400382 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700383 window->geometry.width,
384 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500385 window->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100386 weston_platform_create_egl_surface(display->egl.dpy,
387 display->egl.conf,
388 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100389
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100390
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900391 if (display->shell) {
392 create_xdg_surface(window, display);
393 } else if (display->ivi_application ) {
394 create_ivi_surface(window, display);
395 } else {
396 assert(0);
397 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600398
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500399 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
400 window->egl_surface, window->display->egl.ctx);
401 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300402
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800403 if (!window->frame_sync)
404 eglSwapInterval(display->egl.dpy, 0);
405
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900406 if (!display->shell)
407 return;
408
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700409 if (window->fullscreen)
410 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100411}
412
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200413static void
414destroy_surface(struct window *window)
415{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000416 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
417 * on eglReleaseThread(). */
418 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
419 EGL_NO_CONTEXT);
420
421 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200422 wl_egl_window_destroy(window->native);
423
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900424 if (window->xdg_surface)
425 xdg_surface_destroy(window->xdg_surface);
426 if (window->display->ivi_application)
427 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200428 wl_surface_destroy(window->surface);
429
430 if (window->callback)
431 wl_callback_destroy(window->callback);
432}
433
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100434static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400435redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100436{
437 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400438 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100439 static const GLfloat verts[3][2] = {
440 { -0.5, -0.5 },
441 { 0.5, -0.5 },
442 { 0, 0.5 }
443 };
444 static const GLfloat colors[3][3] = {
445 { 1, 0, 0 },
446 { 0, 1, 0 },
447 { 0, 0, 1 }
448 };
449 GLfloat angle;
450 GLfloat rotation[4][4] = {
451 { 1, 0, 0, 0 },
452 { 0, 1, 0, 0 },
453 { 0, 0, 1, 0 },
454 { 0, 0, 0, 1 }
455 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100456 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400457 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400458 EGLint rect[4];
459 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800460 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100461
Scott Moreau7e300db2012-08-31 03:18:15 -0600462 assert(window->callback == callback);
463 window->callback = NULL;
464
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300465 if (callback)
466 wl_callback_destroy(callback);
467
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800468 gettimeofday(&tv, NULL);
469 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
470 if (window->frames == 0)
471 window->benchmark_time = time;
472 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
473 printf("%d frames in %d seconds: %f fps\n",
474 window->frames,
475 benchmark_interval,
476 (float) window->frames / benchmark_interval);
477 window->benchmark_time = time;
478 window->frames = 0;
479 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100480
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800481 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100482 rotation[0][0] = cos(angle);
483 rotation[0][2] = sin(angle);
484 rotation[2][0] = -sin(angle);
485 rotation[2][2] = cos(angle);
486
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400487 if (display->swap_buffers_with_damage)
488 eglQuerySurface(display->egl.dpy, window->egl_surface,
489 EGL_BUFFER_AGE_EXT, &buffer_age);
490
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300491 glViewport(0, 0, window->geometry.width, window->geometry.height);
492
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100493 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
494 (GLfloat *) rotation);
495
496 glClearColor(0.0, 0.0, 0.0, 0.5);
497 glClear(GL_COLOR_BUFFER_BIT);
498
499 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
500 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
501 glEnableVertexAttribArray(window->gl.pos);
502 glEnableVertexAttribArray(window->gl.col);
503
504 glDrawArrays(GL_TRIANGLES, 0, 3);
505
506 glDisableVertexAttribArray(window->gl.pos);
507 glDisableVertexAttribArray(window->gl.col);
508
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300509 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400510 region = wl_compositor_create_region(window->display->compositor);
511 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300512 window->geometry.width,
513 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400514 wl_surface_set_opaque_region(window->surface, region);
515 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700516 } else {
517 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400518 }
519
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400520 if (display->swap_buffers_with_damage && buffer_age > 0) {
521 rect[0] = window->geometry.width / 4 - 1;
522 rect[1] = window->geometry.height / 4 - 1;
523 rect[2] = window->geometry.width / 2 + 2;
524 rect[3] = window->geometry.height / 2 + 2;
525 display->swap_buffers_with_damage(display->egl.dpy,
526 window->egl_surface,
527 rect, 1);
528 } else {
529 eglSwapBuffers(display->egl.dpy, window->egl_surface);
530 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800531 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100532}
533
534static void
Daniel Stone37816df2012-05-16 18:45:18 +0100535pointer_handle_enter(void *data, struct wl_pointer *pointer,
536 uint32_t serial, struct wl_surface *surface,
537 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300538{
539 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400540 struct wl_buffer *buffer;
541 struct wl_cursor *cursor = display->default_cursor;
542 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300543
544 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300545 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400546 else if (cursor) {
547 image = display->default_cursor->images[0];
548 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100549 if (!buffer)
550 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400551 wl_pointer_set_cursor(pointer, serial,
552 display->cursor_surface,
553 image->hotspot_x,
554 image->hotspot_y);
555 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
556 wl_surface_damage(display->cursor_surface, 0, 0,
557 image->width, image->height);
558 wl_surface_commit(display->cursor_surface);
559 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300560}
561
562static void
Daniel Stone37816df2012-05-16 18:45:18 +0100563pointer_handle_leave(void *data, struct wl_pointer *pointer,
564 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300565{
566}
567
568static void
Daniel Stone37816df2012-05-16 18:45:18 +0100569pointer_handle_motion(void *data, struct wl_pointer *pointer,
570 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300571{
572}
573
574static void
Daniel Stone37816df2012-05-16 18:45:18 +0100575pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
576 uint32_t serial, uint32_t time, uint32_t button,
577 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300578{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300579 struct display *display = data;
580
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900581 if (!display->window->xdg_surface)
582 return;
583
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300584 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800585 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900586 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300587}
588
589static void
Daniel Stone37816df2012-05-16 18:45:18 +0100590pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100591 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300592{
593}
594
Daniel Stone37816df2012-05-16 18:45:18 +0100595static const struct wl_pointer_listener pointer_listener = {
596 pointer_handle_enter,
597 pointer_handle_leave,
598 pointer_handle_motion,
599 pointer_handle_button,
600 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300601};
602
603static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700604touch_handle_down(void *data, struct wl_touch *wl_touch,
605 uint32_t serial, uint32_t time, struct wl_surface *surface,
606 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
607{
608 struct display *d = (struct display *)data;
609
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900610 if (!d->shell)
611 return;
612
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800613 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700614}
615
616static void
617touch_handle_up(void *data, struct wl_touch *wl_touch,
618 uint32_t serial, uint32_t time, int32_t id)
619{
620}
621
622static void
623touch_handle_motion(void *data, struct wl_touch *wl_touch,
624 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
625{
626}
627
628static void
629touch_handle_frame(void *data, struct wl_touch *wl_touch)
630{
631}
632
633static void
634touch_handle_cancel(void *data, struct wl_touch *wl_touch)
635{
636}
637
638static const struct wl_touch_listener touch_listener = {
639 touch_handle_down,
640 touch_handle_up,
641 touch_handle_motion,
642 touch_handle_frame,
643 touch_handle_cancel,
644};
645
646static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300647keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
648 uint32_t format, int fd, uint32_t size)
649{
650}
651
652static void
653keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
654 uint32_t serial, struct wl_surface *surface,
655 struct wl_array *keys)
656{
657}
658
659static void
660keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
661 uint32_t serial, struct wl_surface *surface)
662{
663}
664
665static void
666keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
667 uint32_t serial, uint32_t time, uint32_t key,
668 uint32_t state)
669{
670 struct display *d = data;
671
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900672 if (!d->shell)
673 return;
674
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700675 if (key == KEY_F11 && state) {
676 if (d->window->fullscreen)
677 xdg_surface_unset_fullscreen(d->window->xdg_surface);
678 else
679 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
680 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400681 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300682}
683
684static void
685keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
686 uint32_t serial, uint32_t mods_depressed,
687 uint32_t mods_latched, uint32_t mods_locked,
688 uint32_t group)
689{
690}
691
692static const struct wl_keyboard_listener keyboard_listener = {
693 keyboard_handle_keymap,
694 keyboard_handle_enter,
695 keyboard_handle_leave,
696 keyboard_handle_key,
697 keyboard_handle_modifiers,
698};
699
700static void
Daniel Stone37816df2012-05-16 18:45:18 +0100701seat_handle_capabilities(void *data, struct wl_seat *seat,
702 enum wl_seat_capability caps)
703{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400704 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100705
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400706 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
707 d->pointer = wl_seat_get_pointer(seat);
708 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
709 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
710 wl_pointer_destroy(d->pointer);
711 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100712 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300713
714 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
715 d->keyboard = wl_seat_get_keyboard(seat);
716 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
717 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
718 wl_keyboard_destroy(d->keyboard);
719 d->keyboard = NULL;
720 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700721
722 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
723 d->touch = wl_seat_get_touch(seat);
724 wl_touch_set_user_data(d->touch, d);
725 wl_touch_add_listener(d->touch, &touch_listener, d);
726 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
727 wl_touch_destroy(d->touch);
728 d->touch = NULL;
729 }
Daniel Stone37816df2012-05-16 18:45:18 +0100730}
731
732static const struct wl_seat_listener seat_listener = {
733 seat_handle_capabilities,
734};
735
736static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800737xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
738{
739 xdg_shell_pong(shell, serial);
740}
741
742static const struct xdg_shell_listener xdg_shell_listener = {
743 xdg_shell_ping,
744};
745
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800746#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800747#ifdef static_assert
748static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
749 "Interface version doesn't match implementation version");
750#endif
751
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800752static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400753registry_handle_global(void *data, struct wl_registry *registry,
754 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100755{
756 struct display *d = data;
757
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400758 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400759 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400760 wl_registry_bind(registry, name,
761 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800762 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400763 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800764 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800765 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800766 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100767 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400768 d->seat = wl_registry_bind(registry, name,
769 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400770 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400771 } else if (strcmp(interface, "wl_shm") == 0) {
772 d->shm = wl_registry_bind(registry, name,
773 &wl_shm_interface, 1);
774 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100775 if (!d->cursor_theme) {
776 fprintf(stderr, "unable to load default theme\n");
777 return;
778 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400779 d->default_cursor =
780 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100781 if (!d->default_cursor) {
782 fprintf(stderr, "unable to load default left pointer\n");
783 // TODO: abort ?
784 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900785 } else if (strcmp(interface, "ivi_application") == 0) {
786 d->ivi_application =
787 wl_registry_bind(registry, name,
788 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400789 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100790}
791
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200792static void
793registry_handle_global_remove(void *data, struct wl_registry *registry,
794 uint32_t name)
795{
796}
797
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400798static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200799 registry_handle_global,
800 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400801};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100802
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200803static void
804signal_int(int signum)
805{
806 running = 0;
807}
808
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400809static void
810usage(int error_code)
811{
812 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
813 " -f\tRun in fullscreen mode\n"
814 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700815 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800816 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400817 " -h\tThis help text\n\n");
818
819 exit(error_code);
820}
821
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100822int
823main(int argc, char **argv)
824{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200825 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100826 struct display display = { 0 };
827 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400828 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100829
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100830 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300831 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700832 window.geometry.width = 250;
833 window.geometry.height = 250;
834 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700835 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800836 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100837
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400838 for (i = 1; i < argc; i++) {
839 if (strcmp("-f", argv[i]) == 0)
840 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400841 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300842 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700843 else if (strcmp("-s", argv[i]) == 0)
844 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800845 else if (strcmp("-b", argv[i]) == 0)
846 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400847 else if (strcmp("-h", argv[i]) == 0)
848 usage(EXIT_SUCCESS);
849 else
850 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400851 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300852
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100853 display.display = wl_display_connect(NULL);
854 assert(display.display);
855
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400856 display.registry = wl_display_get_registry(display.display);
857 wl_registry_add_listener(display.registry,
858 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100859
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100860 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200861
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700862 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100863 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500864 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100865
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400866 display.cursor_surface =
867 wl_compositor_create_surface(display.compositor);
868
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200869 sigint.sa_handler = signal_int;
870 sigemptyset(&sigint.sa_mask);
871 sigint.sa_flags = SA_RESETHAND;
872 sigaction(SIGINT, &sigint, NULL);
873
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800874 /* The mainloop here is a little subtle. Redrawing will cause
875 * EGL to read events so we can just call
876 * wl_display_dispatch_pending() to handle any events that got
877 * queued up as a side effect. */
878 while (running && ret != -1) {
879 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800880 redraw(&window, NULL, 0);
881 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500882
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200883 fprintf(stderr, "simple-egl exiting\n");
884
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200885 destroy_surface(&window);
886 fini_egl(&display);
887
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400888 wl_surface_destroy(display.cursor_surface);
889 if (display.cursor_theme)
890 wl_cursor_theme_destroy(display.cursor_theme);
891
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200892 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800893 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200894
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900895 if (display.ivi_application)
896 ivi_application_destroy(display.ivi_application);
897
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200898 if (display.compositor)
899 wl_compositor_destroy(display.compositor);
900
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200901 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200902 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500903 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200904
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100905 return 0;
906}