blob: d8233c169fc68db66deda97f65b6134d53461323 [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"
Jonny Lamb51a7ae52015-03-20 15:26:51 +010051
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040052#ifndef EGL_EXT_swap_buffers_with_damage
53#define EGL_EXT_swap_buffers_with_damage 1
54typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
55#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010056
Kristian Høgsberg77787432013-08-22 12:16:51 -070057#ifndef EGL_EXT_buffer_age
58#define EGL_EXT_buffer_age 1
59#define EGL_BUFFER_AGE_EXT 0x313D
60#endif
61
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030062struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010063struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030064
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010065struct display {
66 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040067 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050068 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080069 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040070 struct wl_seat *seat;
71 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070072 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030073 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040074 struct wl_shm *shm;
75 struct wl_cursor_theme *cursor_theme;
76 struct wl_cursor *default_cursor;
77 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010078 struct {
79 EGLDisplay dpy;
80 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050081 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010082 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030083 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090084 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040085
86 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010087};
88
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030089struct geometry {
90 int width, height;
91};
92
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010093struct window {
94 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060095 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010096 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010097 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010098 GLuint pos;
99 GLuint col;
100 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500101
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800102 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500103 struct wl_egl_window *native;
104 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800105 struct xdg_surface *xdg_surface;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900106 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500107 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200108 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500109 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100110};
111
112static const char *vert_shader_text =
113 "uniform mat4 rotation;\n"
114 "attribute vec4 pos;\n"
115 "attribute vec4 color;\n"
116 "varying vec4 v_color;\n"
117 "void main() {\n"
118 " gl_Position = rotation * pos;\n"
119 " v_color = color;\n"
120 "}\n";
121
122static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500123 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100124 "varying vec4 v_color;\n"
125 "void main() {\n"
126 " gl_FragColor = v_color;\n"
127 "}\n";
128
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400129static int running = 1;
130
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100131static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700132init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100133{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500134 static const EGLint context_attribs[] = {
135 EGL_CONTEXT_CLIENT_VERSION, 2,
136 EGL_NONE
137 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400138 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500139
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300140 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500141 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500142 EGL_RED_SIZE, 1,
143 EGL_GREEN_SIZE, 1,
144 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200145 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500146 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
147 EGL_NONE
148 };
149
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700150 EGLint major, minor, n, count, i, size;
151 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100152 EGLBoolean ret;
153
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700154 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400155 config_attribs[9] = 0;
156
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100157 display->egl.dpy =
158 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
159 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100160 assert(display->egl.dpy);
161
162 ret = eglInitialize(display->egl.dpy, &major, &minor);
163 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500164 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100165 assert(ret == EGL_TRUE);
166
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700167 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
168 assert(0);
169
170 configs = calloc(count, sizeof *configs);
171 assert(configs);
172
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300173 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700174 configs, count, &n);
175 assert(ret && n >= 1);
176
177 for (i = 0; i < n; i++) {
178 eglGetConfigAttrib(display->egl.dpy,
179 configs[i], EGL_BUFFER_SIZE, &size);
180 if (window->buffer_size == size) {
181 display->egl.conf = configs[i];
182 break;
183 }
184 }
185 free(configs);
186 if (display->egl.conf == NULL) {
187 fprintf(stderr, "did not find config with buffer size %d\n",
188 window->buffer_size);
189 exit(EXIT_FAILURE);
190 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500191
192 display->egl.ctx = eglCreateContext(display->egl.dpy,
193 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500194 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100195 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500196
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400197 display->swap_buffers_with_damage = NULL;
198 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
199 if (extensions &&
200 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
201 strstr(extensions, "EGL_EXT_buffer_age"))
202 display->swap_buffers_with_damage =
203 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
204 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
205
206 if (display->swap_buffers_with_damage)
207 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
208
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100209}
210
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200211static void
212fini_egl(struct display *display)
213{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200214 eglTerminate(display->egl.dpy);
215 eglReleaseThread();
216}
217
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100218static GLuint
219create_shader(struct window *window, const char *source, GLenum shader_type)
220{
221 GLuint shader;
222 GLint status;
223
224 shader = glCreateShader(shader_type);
225 assert(shader != 0);
226
227 glShaderSource(shader, 1, (const char **) &source, NULL);
228 glCompileShader(shader);
229
230 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
231 if (!status) {
232 char log[1000];
233 GLsizei len;
234 glGetShaderInfoLog(shader, 1000, &len, log);
235 fprintf(stderr, "Error: compiling %s: %*s\n",
236 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
237 len, log);
238 exit(1);
239 }
240
241 return shader;
242}
243
244static void
245init_gl(struct window *window)
246{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100247 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600248 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100249 GLint status;
250
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100251 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
252 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
253
Scott Moreau3ea23d02012-06-13 17:42:21 -0600254 program = glCreateProgram();
255 glAttachShader(program, frag);
256 glAttachShader(program, vert);
257 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100258
Scott Moreau3ea23d02012-06-13 17:42:21 -0600259 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100260 if (!status) {
261 char log[1000];
262 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600263 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100264 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
265 exit(1);
266 }
267
Scott Moreau3ea23d02012-06-13 17:42:21 -0600268 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900269
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100270 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600271 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100272
Scott Moreau3ea23d02012-06-13 17:42:21 -0600273 glBindAttribLocation(program, window->gl.pos, "pos");
274 glBindAttribLocation(program, window->gl.col, "color");
275 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100276
277 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600278 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100279}
280
281static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800282handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700283 int32_t width, int32_t height,
284 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300285{
286 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700287 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300288
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700289 window->fullscreen = 0;
290 wl_array_for_each(p, states) {
291 uint32_t state = *p;
292 switch (state) {
293 case XDG_SURFACE_STATE_FULLSCREEN:
294 window->fullscreen = 1;
295 break;
296 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500297 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800298
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700299 if (width > 0 && height > 0) {
300 if (!window->fullscreen) {
301 window->window_size.width = width;
302 window->window_size.height = height;
303 }
304 window->geometry.width = width;
305 window->geometry.height = height;
306 } else if (!window->fullscreen) {
307 window->geometry = window->window_size;
308 }
309
310 if (window->native)
311 wl_egl_window_resize(window->native,
312 window->geometry.width,
313 window->geometry.height, 0, 0);
314
315 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800316}
317
318static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500319handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
320{
321 running = 0;
322}
323
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800324static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800325 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500326 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300327};
328
329static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900330handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
331 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100332{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900333 struct window *window = data;
334
335 wl_egl_window_resize(window->native, width, height, 0, 0);
336
337 window->geometry.width = width;
338 window->geometry.height = height;
339
340 if (!window->fullscreen)
341 window->window_size = window->geometry;
342}
343
344static const struct ivi_surface_listener ivi_surface_listener = {
345 handle_ivi_surface_configure,
346};
347
348static void
349create_xdg_surface(struct window *window, struct display *display)
350{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800351 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
352 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300353
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800354 xdg_surface_add_listener(window->xdg_surface,
355 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300356
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900357 xdg_surface_set_title(window->xdg_surface, "simple-egl");
358}
359
360static void
361create_ivi_surface(struct window *window, struct display *display)
362{
363 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
364 window->ivi_surface =
365 ivi_application_surface_create(display->ivi_application,
366 id_ivisurf, window->surface);
367
368 if (window->ivi_surface == NULL) {
369 fprintf(stderr, "Failed to create ivi_client_surface\n");
370 abort();
371 }
372
373 ivi_surface_add_listener(window->ivi_surface,
374 &ivi_surface_listener, window);
375}
376
377static void
378create_surface(struct window *window)
379{
380 struct display *display = window->display;
381 EGLBoolean ret;
382
383 window->surface = wl_compositor_create_surface(display->compositor);
384
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500385 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700387 window->geometry.width,
388 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500389 window->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100390 weston_platform_create_egl_surface(display->egl.dpy,
391 display->egl.conf,
392 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100393
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100394
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900395 if (display->shell) {
396 create_xdg_surface(window, display);
397 } else if (display->ivi_application ) {
398 create_ivi_surface(window, display);
399 } else {
400 assert(0);
401 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600402
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500403 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
404 window->egl_surface, window->display->egl.ctx);
405 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300406
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800407 if (!window->frame_sync)
408 eglSwapInterval(display->egl.dpy, 0);
409
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900410 if (!display->shell)
411 return;
412
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700413 if (window->fullscreen)
414 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100415}
416
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200417static void
418destroy_surface(struct window *window)
419{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000420 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
421 * on eglReleaseThread(). */
422 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
423 EGL_NO_CONTEXT);
424
425 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200426 wl_egl_window_destroy(window->native);
427
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900428 if (window->xdg_surface)
429 xdg_surface_destroy(window->xdg_surface);
430 if (window->display->ivi_application)
431 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200432 wl_surface_destroy(window->surface);
433
434 if (window->callback)
435 wl_callback_destroy(window->callback);
436}
437
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100438static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400439redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100440{
441 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400442 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100443 static const GLfloat verts[3][2] = {
444 { -0.5, -0.5 },
445 { 0.5, -0.5 },
446 { 0, 0.5 }
447 };
448 static const GLfloat colors[3][3] = {
449 { 1, 0, 0 },
450 { 0, 1, 0 },
451 { 0, 0, 1 }
452 };
453 GLfloat angle;
454 GLfloat rotation[4][4] = {
455 { 1, 0, 0, 0 },
456 { 0, 1, 0, 0 },
457 { 0, 0, 1, 0 },
458 { 0, 0, 0, 1 }
459 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100460 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400461 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400462 EGLint rect[4];
463 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800464 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100465
Scott Moreau7e300db2012-08-31 03:18:15 -0600466 assert(window->callback == callback);
467 window->callback = NULL;
468
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300469 if (callback)
470 wl_callback_destroy(callback);
471
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800472 gettimeofday(&tv, NULL);
473 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
474 if (window->frames == 0)
475 window->benchmark_time = time;
476 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
477 printf("%d frames in %d seconds: %f fps\n",
478 window->frames,
479 benchmark_interval,
480 (float) window->frames / benchmark_interval);
481 window->benchmark_time = time;
482 window->frames = 0;
483 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100484
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800485 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100486 rotation[0][0] = cos(angle);
487 rotation[0][2] = sin(angle);
488 rotation[2][0] = -sin(angle);
489 rotation[2][2] = cos(angle);
490
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400491 if (display->swap_buffers_with_damage)
492 eglQuerySurface(display->egl.dpy, window->egl_surface,
493 EGL_BUFFER_AGE_EXT, &buffer_age);
494
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300495 glViewport(0, 0, window->geometry.width, window->geometry.height);
496
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100497 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
498 (GLfloat *) rotation);
499
500 glClearColor(0.0, 0.0, 0.0, 0.5);
501 glClear(GL_COLOR_BUFFER_BIT);
502
503 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
504 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
505 glEnableVertexAttribArray(window->gl.pos);
506 glEnableVertexAttribArray(window->gl.col);
507
508 glDrawArrays(GL_TRIANGLES, 0, 3);
509
510 glDisableVertexAttribArray(window->gl.pos);
511 glDisableVertexAttribArray(window->gl.col);
512
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300513 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400514 region = wl_compositor_create_region(window->display->compositor);
515 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300516 window->geometry.width,
517 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400518 wl_surface_set_opaque_region(window->surface, region);
519 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700520 } else {
521 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400522 }
523
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400524 if (display->swap_buffers_with_damage && buffer_age > 0) {
525 rect[0] = window->geometry.width / 4 - 1;
526 rect[1] = window->geometry.height / 4 - 1;
527 rect[2] = window->geometry.width / 2 + 2;
528 rect[3] = window->geometry.height / 2 + 2;
529 display->swap_buffers_with_damage(display->egl.dpy,
530 window->egl_surface,
531 rect, 1);
532 } else {
533 eglSwapBuffers(display->egl.dpy, window->egl_surface);
534 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800535 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100536}
537
538static void
Daniel Stone37816df2012-05-16 18:45:18 +0100539pointer_handle_enter(void *data, struct wl_pointer *pointer,
540 uint32_t serial, struct wl_surface *surface,
541 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300542{
543 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400544 struct wl_buffer *buffer;
545 struct wl_cursor *cursor = display->default_cursor;
546 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300547
548 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300549 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400550 else if (cursor) {
551 image = display->default_cursor->images[0];
552 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100553 if (!buffer)
554 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400555 wl_pointer_set_cursor(pointer, serial,
556 display->cursor_surface,
557 image->hotspot_x,
558 image->hotspot_y);
559 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
560 wl_surface_damage(display->cursor_surface, 0, 0,
561 image->width, image->height);
562 wl_surface_commit(display->cursor_surface);
563 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300564}
565
566static void
Daniel Stone37816df2012-05-16 18:45:18 +0100567pointer_handle_leave(void *data, struct wl_pointer *pointer,
568 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300569{
570}
571
572static void
Daniel Stone37816df2012-05-16 18:45:18 +0100573pointer_handle_motion(void *data, struct wl_pointer *pointer,
574 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300575{
576}
577
578static void
Daniel Stone37816df2012-05-16 18:45:18 +0100579pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
580 uint32_t serial, uint32_t time, uint32_t button,
581 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300582{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300583 struct display *display = data;
584
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900585 if (!display->window->xdg_surface)
586 return;
587
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300588 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800589 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900590 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300591}
592
593static void
Daniel Stone37816df2012-05-16 18:45:18 +0100594pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100595 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300596{
597}
598
Daniel Stone37816df2012-05-16 18:45:18 +0100599static const struct wl_pointer_listener pointer_listener = {
600 pointer_handle_enter,
601 pointer_handle_leave,
602 pointer_handle_motion,
603 pointer_handle_button,
604 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300605};
606
607static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700608touch_handle_down(void *data, struct wl_touch *wl_touch,
609 uint32_t serial, uint32_t time, struct wl_surface *surface,
610 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
611{
612 struct display *d = (struct display *)data;
613
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900614 if (!d->shell)
615 return;
616
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800617 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700618}
619
620static void
621touch_handle_up(void *data, struct wl_touch *wl_touch,
622 uint32_t serial, uint32_t time, int32_t id)
623{
624}
625
626static void
627touch_handle_motion(void *data, struct wl_touch *wl_touch,
628 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
629{
630}
631
632static void
633touch_handle_frame(void *data, struct wl_touch *wl_touch)
634{
635}
636
637static void
638touch_handle_cancel(void *data, struct wl_touch *wl_touch)
639{
640}
641
642static const struct wl_touch_listener touch_listener = {
643 touch_handle_down,
644 touch_handle_up,
645 touch_handle_motion,
646 touch_handle_frame,
647 touch_handle_cancel,
648};
649
650static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300651keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
652 uint32_t format, int fd, uint32_t size)
653{
654}
655
656static void
657keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
658 uint32_t serial, struct wl_surface *surface,
659 struct wl_array *keys)
660{
661}
662
663static void
664keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
665 uint32_t serial, struct wl_surface *surface)
666{
667}
668
669static void
670keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
671 uint32_t serial, uint32_t time, uint32_t key,
672 uint32_t state)
673{
674 struct display *d = data;
675
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900676 if (!d->shell)
677 return;
678
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700679 if (key == KEY_F11 && state) {
680 if (d->window->fullscreen)
681 xdg_surface_unset_fullscreen(d->window->xdg_surface);
682 else
683 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
684 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400685 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300686}
687
688static void
689keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
690 uint32_t serial, uint32_t mods_depressed,
691 uint32_t mods_latched, uint32_t mods_locked,
692 uint32_t group)
693{
694}
695
696static const struct wl_keyboard_listener keyboard_listener = {
697 keyboard_handle_keymap,
698 keyboard_handle_enter,
699 keyboard_handle_leave,
700 keyboard_handle_key,
701 keyboard_handle_modifiers,
702};
703
704static void
Daniel Stone37816df2012-05-16 18:45:18 +0100705seat_handle_capabilities(void *data, struct wl_seat *seat,
706 enum wl_seat_capability caps)
707{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400708 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100709
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400710 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
711 d->pointer = wl_seat_get_pointer(seat);
712 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
713 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
714 wl_pointer_destroy(d->pointer);
715 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100716 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300717
718 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
719 d->keyboard = wl_seat_get_keyboard(seat);
720 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
721 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
722 wl_keyboard_destroy(d->keyboard);
723 d->keyboard = NULL;
724 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700725
726 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
727 d->touch = wl_seat_get_touch(seat);
728 wl_touch_set_user_data(d->touch, d);
729 wl_touch_add_listener(d->touch, &touch_listener, d);
730 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
731 wl_touch_destroy(d->touch);
732 d->touch = NULL;
733 }
Daniel Stone37816df2012-05-16 18:45:18 +0100734}
735
736static const struct wl_seat_listener seat_listener = {
737 seat_handle_capabilities,
738};
739
740static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800741xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
742{
743 xdg_shell_pong(shell, serial);
744}
745
746static const struct xdg_shell_listener xdg_shell_listener = {
747 xdg_shell_ping,
748};
749
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800750#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800751#ifdef static_assert
752static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
753 "Interface version doesn't match implementation version");
754#endif
755
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800756static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400757registry_handle_global(void *data, struct wl_registry *registry,
758 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100759{
760 struct display *d = data;
761
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400762 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400763 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400764 wl_registry_bind(registry, name,
765 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800766 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400767 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800768 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800769 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800770 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100771 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400772 d->seat = wl_registry_bind(registry, name,
773 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400774 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400775 } else if (strcmp(interface, "wl_shm") == 0) {
776 d->shm = wl_registry_bind(registry, name,
777 &wl_shm_interface, 1);
778 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100779 if (!d->cursor_theme) {
780 fprintf(stderr, "unable to load default theme\n");
781 return;
782 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400783 d->default_cursor =
784 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100785 if (!d->default_cursor) {
786 fprintf(stderr, "unable to load default left pointer\n");
787 // TODO: abort ?
788 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900789 } else if (strcmp(interface, "ivi_application") == 0) {
790 d->ivi_application =
791 wl_registry_bind(registry, name,
792 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400793 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100794}
795
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200796static void
797registry_handle_global_remove(void *data, struct wl_registry *registry,
798 uint32_t name)
799{
800}
801
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400802static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200803 registry_handle_global,
804 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400805};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100806
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200807static void
808signal_int(int signum)
809{
810 running = 0;
811}
812
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400813static void
814usage(int error_code)
815{
816 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
817 " -f\tRun in fullscreen mode\n"
818 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700819 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800820 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400821 " -h\tThis help text\n\n");
822
823 exit(error_code);
824}
825
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100826int
827main(int argc, char **argv)
828{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200829 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100830 struct display display = { 0 };
831 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400832 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100833
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100834 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300835 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700836 window.geometry.width = 250;
837 window.geometry.height = 250;
838 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700839 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800840 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100841
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400842 for (i = 1; i < argc; i++) {
843 if (strcmp("-f", argv[i]) == 0)
844 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400845 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300846 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700847 else if (strcmp("-s", argv[i]) == 0)
848 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800849 else if (strcmp("-b", argv[i]) == 0)
850 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400851 else if (strcmp("-h", argv[i]) == 0)
852 usage(EXIT_SUCCESS);
853 else
854 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400855 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300856
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100857 display.display = wl_display_connect(NULL);
858 assert(display.display);
859
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400860 display.registry = wl_display_get_registry(display.display);
861 wl_registry_add_listener(display.registry,
862 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100863
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100864 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200865
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700866 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100867 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500868 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100869
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400870 display.cursor_surface =
871 wl_compositor_create_surface(display.compositor);
872
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200873 sigint.sa_handler = signal_int;
874 sigemptyset(&sigint.sa_mask);
875 sigint.sa_flags = SA_RESETHAND;
876 sigaction(SIGINT, &sigint, NULL);
877
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800878 /* The mainloop here is a little subtle. Redrawing will cause
879 * EGL to read events so we can just call
880 * wl_display_dispatch_pending() to handle any events that got
881 * queued up as a side effect. */
882 while (running && ret != -1) {
883 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800884 redraw(&window, NULL, 0);
885 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500886
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200887 fprintf(stderr, "simple-egl exiting\n");
888
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200889 destroy_surface(&window);
890 fini_egl(&display);
891
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400892 wl_surface_destroy(display.cursor_surface);
893 if (display.cursor_theme)
894 wl_cursor_theme_destroy(display.cursor_theme);
895
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200896 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800897 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200898
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900899 if (display.ivi_application)
900 ivi_application_destroy(display.ivi_application);
901
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200902 if (display.compositor)
903 wl_compositor_destroy(display.compositor);
904
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200905 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200906 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500907 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200908
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100909 return 0;
910}