blob: ddf2f9bd06a5a14c1d0e5be7f8c599643ad0af42 [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
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030026#include <stdint.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010030#include <stdbool.h>
31#include <math.h>
32#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020033#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010034
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030035#include <linux/input.h>
36
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010037#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050038#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040039#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010041#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010042#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040043#include <EGL/eglext.h>
44
Jonas Ådahl2a229332015-11-17 16:00:32 +080045#include "xdg-shell-unstable-v5-client-protocol.h"
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090046#include <sys/types.h>
47#include <unistd.h>
48#include "protocol/ivi-application-client-protocol.h"
49#define IVI_SURFACE_ID 9000
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080050
Jon Cruz4678bab2015-06-15 15:37:07 -070051#include "shared/platform.h"
Emil Velikov0725cf12016-07-04 15:27:15 +010052#include "weston-egl-ext.h"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010053
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030054struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010055struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030056
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010057struct display {
58 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040059 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050060 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080061 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040062 struct wl_seat *seat;
63 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070064 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030065 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040066 struct wl_shm *shm;
67 struct wl_cursor_theme *cursor_theme;
68 struct wl_cursor *default_cursor;
69 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010070 struct {
71 EGLDisplay dpy;
72 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050073 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010074 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030075 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090076 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040077
78 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010079};
80
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030081struct geometry {
82 int width, height;
83};
84
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010085struct window {
86 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060087 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010088 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010089 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010090 GLuint pos;
91 GLuint col;
92 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050093
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080094 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050095 struct wl_egl_window *native;
96 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080097 struct xdg_surface *xdg_surface;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090098 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050099 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200100 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500101 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100102};
103
104static const char *vert_shader_text =
105 "uniform mat4 rotation;\n"
106 "attribute vec4 pos;\n"
107 "attribute vec4 color;\n"
108 "varying vec4 v_color;\n"
109 "void main() {\n"
110 " gl_Position = rotation * pos;\n"
111 " v_color = color;\n"
112 "}\n";
113
114static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500115 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100116 "varying vec4 v_color;\n"
117 "void main() {\n"
118 " gl_FragColor = v_color;\n"
119 "}\n";
120
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400121static int running = 1;
122
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100123static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700124init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100125{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500126 static const EGLint context_attribs[] = {
127 EGL_CONTEXT_CLIENT_VERSION, 2,
128 EGL_NONE
129 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400130 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500131
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300132 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500133 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500134 EGL_RED_SIZE, 1,
135 EGL_GREEN_SIZE, 1,
136 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200137 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500138 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
139 EGL_NONE
140 };
141
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700142 EGLint major, minor, n, count, i, size;
143 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100144 EGLBoolean ret;
145
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700146 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400147 config_attribs[9] = 0;
148
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100149 display->egl.dpy =
150 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
151 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100152 assert(display->egl.dpy);
153
154 ret = eglInitialize(display->egl.dpy, &major, &minor);
155 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500156 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100157 assert(ret == EGL_TRUE);
158
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700159 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
160 assert(0);
161
162 configs = calloc(count, sizeof *configs);
163 assert(configs);
164
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300165 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700166 configs, count, &n);
167 assert(ret && n >= 1);
168
169 for (i = 0; i < n; i++) {
170 eglGetConfigAttrib(display->egl.dpy,
171 configs[i], EGL_BUFFER_SIZE, &size);
172 if (window->buffer_size == size) {
173 display->egl.conf = configs[i];
174 break;
175 }
176 }
177 free(configs);
178 if (display->egl.conf == NULL) {
179 fprintf(stderr, "did not find config with buffer size %d\n",
180 window->buffer_size);
181 exit(EXIT_FAILURE);
182 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500183
184 display->egl.ctx = eglCreateContext(display->egl.dpy,
185 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500186 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100187 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500188
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400189 display->swap_buffers_with_damage = NULL;
190 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
191 if (extensions &&
Emil Velikov025ad932016-07-04 15:34:21 +0100192 weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage") &&
193 weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400194 display->swap_buffers_with_damage =
195 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
196 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
197
198 if (display->swap_buffers_with_damage)
199 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
200
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100201}
202
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200203static void
204fini_egl(struct display *display)
205{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200206 eglTerminate(display->egl.dpy);
207 eglReleaseThread();
208}
209
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100210static GLuint
211create_shader(struct window *window, const char *source, GLenum shader_type)
212{
213 GLuint shader;
214 GLint status;
215
216 shader = glCreateShader(shader_type);
217 assert(shader != 0);
218
219 glShaderSource(shader, 1, (const char **) &source, NULL);
220 glCompileShader(shader);
221
222 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
223 if (!status) {
224 char log[1000];
225 GLsizei len;
226 glGetShaderInfoLog(shader, 1000, &len, log);
227 fprintf(stderr, "Error: compiling %s: %*s\n",
228 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
229 len, log);
230 exit(1);
231 }
232
233 return shader;
234}
235
236static void
237init_gl(struct window *window)
238{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100239 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600240 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100241 GLint status;
242
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100243 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
244 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
245
Scott Moreau3ea23d02012-06-13 17:42:21 -0600246 program = glCreateProgram();
247 glAttachShader(program, frag);
248 glAttachShader(program, vert);
249 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100250
Scott Moreau3ea23d02012-06-13 17:42:21 -0600251 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100252 if (!status) {
253 char log[1000];
254 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600255 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100256 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
257 exit(1);
258 }
259
Scott Moreau3ea23d02012-06-13 17:42:21 -0600260 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900261
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100262 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600263 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100264
Scott Moreau3ea23d02012-06-13 17:42:21 -0600265 glBindAttribLocation(program, window->gl.pos, "pos");
266 glBindAttribLocation(program, window->gl.col, "color");
267 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100268
269 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600270 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100271}
272
273static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800274handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700275 int32_t width, int32_t height,
276 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300277{
278 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700279 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300280
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700281 window->fullscreen = 0;
282 wl_array_for_each(p, states) {
283 uint32_t state = *p;
284 switch (state) {
285 case XDG_SURFACE_STATE_FULLSCREEN:
286 window->fullscreen = 1;
287 break;
288 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500289 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800290
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700291 if (width > 0 && height > 0) {
292 if (!window->fullscreen) {
293 window->window_size.width = width;
294 window->window_size.height = height;
295 }
296 window->geometry.width = width;
297 window->geometry.height = height;
298 } else if (!window->fullscreen) {
299 window->geometry = window->window_size;
300 }
301
302 if (window->native)
303 wl_egl_window_resize(window->native,
304 window->geometry.width,
305 window->geometry.height, 0, 0);
306
307 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800308}
309
310static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500311handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
312{
313 running = 0;
314}
315
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800316static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800317 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500318 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300319};
320
321static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900322handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
323 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100324{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900325 struct window *window = data;
326
327 wl_egl_window_resize(window->native, width, height, 0, 0);
328
329 window->geometry.width = width;
330 window->geometry.height = height;
331
332 if (!window->fullscreen)
333 window->window_size = window->geometry;
334}
335
336static const struct ivi_surface_listener ivi_surface_listener = {
337 handle_ivi_surface_configure,
338};
339
340static void
341create_xdg_surface(struct window *window, struct display *display)
342{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800343 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
344 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300345
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800346 xdg_surface_add_listener(window->xdg_surface,
347 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300348
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900349 xdg_surface_set_title(window->xdg_surface, "simple-egl");
350}
351
352static void
353create_ivi_surface(struct window *window, struct display *display)
354{
355 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
356 window->ivi_surface =
357 ivi_application_surface_create(display->ivi_application,
358 id_ivisurf, window->surface);
359
360 if (window->ivi_surface == NULL) {
361 fprintf(stderr, "Failed to create ivi_client_surface\n");
362 abort();
363 }
364
365 ivi_surface_add_listener(window->ivi_surface,
366 &ivi_surface_listener, window);
367}
368
369static void
370create_surface(struct window *window)
371{
372 struct display *display = window->display;
373 EGLBoolean ret;
374
375 window->surface = wl_compositor_create_surface(display->compositor);
376
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500377 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400378 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700379 window->geometry.width,
380 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500381 window->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100382 weston_platform_create_egl_surface(display->egl.dpy,
383 display->egl.conf,
384 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100385
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100386
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900387 if (display->shell) {
388 create_xdg_surface(window, display);
389 } else if (display->ivi_application ) {
390 create_ivi_surface(window, display);
391 } else {
392 assert(0);
393 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600394
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500395 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
396 window->egl_surface, window->display->egl.ctx);
397 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300398
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800399 if (!window->frame_sync)
400 eglSwapInterval(display->egl.dpy, 0);
401
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900402 if (!display->shell)
403 return;
404
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700405 if (window->fullscreen)
406 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100407}
408
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200409static void
410destroy_surface(struct window *window)
411{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000412 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
413 * on eglReleaseThread(). */
414 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
415 EGL_NO_CONTEXT);
416
417 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200418 wl_egl_window_destroy(window->native);
419
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900420 if (window->xdg_surface)
421 xdg_surface_destroy(window->xdg_surface);
422 if (window->display->ivi_application)
423 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200424 wl_surface_destroy(window->surface);
425
426 if (window->callback)
427 wl_callback_destroy(window->callback);
428}
429
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100430static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400431redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100432{
433 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400434 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100435 static const GLfloat verts[3][2] = {
436 { -0.5, -0.5 },
437 { 0.5, -0.5 },
438 { 0, 0.5 }
439 };
440 static const GLfloat colors[3][3] = {
441 { 1, 0, 0 },
442 { 0, 1, 0 },
443 { 0, 0, 1 }
444 };
445 GLfloat angle;
446 GLfloat rotation[4][4] = {
447 { 1, 0, 0, 0 },
448 { 0, 1, 0, 0 },
449 { 0, 0, 1, 0 },
450 { 0, 0, 0, 1 }
451 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100452 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400453 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400454 EGLint rect[4];
455 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800456 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100457
Scott Moreau7e300db2012-08-31 03:18:15 -0600458 assert(window->callback == callback);
459 window->callback = NULL;
460
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300461 if (callback)
462 wl_callback_destroy(callback);
463
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800464 gettimeofday(&tv, NULL);
465 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
466 if (window->frames == 0)
467 window->benchmark_time = time;
468 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
469 printf("%d frames in %d seconds: %f fps\n",
470 window->frames,
471 benchmark_interval,
472 (float) window->frames / benchmark_interval);
473 window->benchmark_time = time;
474 window->frames = 0;
475 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100476
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800477 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100478 rotation[0][0] = cos(angle);
479 rotation[0][2] = sin(angle);
480 rotation[2][0] = -sin(angle);
481 rotation[2][2] = cos(angle);
482
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400483 if (display->swap_buffers_with_damage)
484 eglQuerySurface(display->egl.dpy, window->egl_surface,
485 EGL_BUFFER_AGE_EXT, &buffer_age);
486
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300487 glViewport(0, 0, window->geometry.width, window->geometry.height);
488
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100489 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
490 (GLfloat *) rotation);
491
492 glClearColor(0.0, 0.0, 0.0, 0.5);
493 glClear(GL_COLOR_BUFFER_BIT);
494
495 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
496 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
497 glEnableVertexAttribArray(window->gl.pos);
498 glEnableVertexAttribArray(window->gl.col);
499
500 glDrawArrays(GL_TRIANGLES, 0, 3);
501
502 glDisableVertexAttribArray(window->gl.pos);
503 glDisableVertexAttribArray(window->gl.col);
504
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300505 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400506 region = wl_compositor_create_region(window->display->compositor);
507 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300508 window->geometry.width,
509 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400510 wl_surface_set_opaque_region(window->surface, region);
511 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700512 } else {
513 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400514 }
515
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400516 if (display->swap_buffers_with_damage && buffer_age > 0) {
517 rect[0] = window->geometry.width / 4 - 1;
518 rect[1] = window->geometry.height / 4 - 1;
519 rect[2] = window->geometry.width / 2 + 2;
520 rect[3] = window->geometry.height / 2 + 2;
521 display->swap_buffers_with_damage(display->egl.dpy,
522 window->egl_surface,
523 rect, 1);
524 } else {
525 eglSwapBuffers(display->egl.dpy, window->egl_surface);
526 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800527 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100528}
529
530static void
Daniel Stone37816df2012-05-16 18:45:18 +0100531pointer_handle_enter(void *data, struct wl_pointer *pointer,
532 uint32_t serial, struct wl_surface *surface,
533 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300534{
535 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400536 struct wl_buffer *buffer;
537 struct wl_cursor *cursor = display->default_cursor;
538 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300539
540 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300541 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400542 else if (cursor) {
543 image = display->default_cursor->images[0];
544 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100545 if (!buffer)
546 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400547 wl_pointer_set_cursor(pointer, serial,
548 display->cursor_surface,
549 image->hotspot_x,
550 image->hotspot_y);
551 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
552 wl_surface_damage(display->cursor_surface, 0, 0,
553 image->width, image->height);
554 wl_surface_commit(display->cursor_surface);
555 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300556}
557
558static void
Daniel Stone37816df2012-05-16 18:45:18 +0100559pointer_handle_leave(void *data, struct wl_pointer *pointer,
560 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300561{
562}
563
564static void
Daniel Stone37816df2012-05-16 18:45:18 +0100565pointer_handle_motion(void *data, struct wl_pointer *pointer,
566 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300567{
568}
569
570static void
Daniel Stone37816df2012-05-16 18:45:18 +0100571pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
572 uint32_t serial, uint32_t time, uint32_t button,
573 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300574{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300575 struct display *display = data;
576
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900577 if (!display->window->xdg_surface)
578 return;
579
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300580 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800581 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900582 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300583}
584
585static void
Daniel Stone37816df2012-05-16 18:45:18 +0100586pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100587 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300588{
589}
590
Daniel Stone37816df2012-05-16 18:45:18 +0100591static const struct wl_pointer_listener pointer_listener = {
592 pointer_handle_enter,
593 pointer_handle_leave,
594 pointer_handle_motion,
595 pointer_handle_button,
596 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300597};
598
599static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700600touch_handle_down(void *data, struct wl_touch *wl_touch,
601 uint32_t serial, uint32_t time, struct wl_surface *surface,
602 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
603{
604 struct display *d = (struct display *)data;
605
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900606 if (!d->shell)
607 return;
608
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800609 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700610}
611
612static void
613touch_handle_up(void *data, struct wl_touch *wl_touch,
614 uint32_t serial, uint32_t time, int32_t id)
615{
616}
617
618static void
619touch_handle_motion(void *data, struct wl_touch *wl_touch,
620 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
621{
622}
623
624static void
625touch_handle_frame(void *data, struct wl_touch *wl_touch)
626{
627}
628
629static void
630touch_handle_cancel(void *data, struct wl_touch *wl_touch)
631{
632}
633
634static const struct wl_touch_listener touch_listener = {
635 touch_handle_down,
636 touch_handle_up,
637 touch_handle_motion,
638 touch_handle_frame,
639 touch_handle_cancel,
640};
641
642static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300643keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
644 uint32_t format, int fd, uint32_t size)
645{
646}
647
648static void
649keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
650 uint32_t serial, struct wl_surface *surface,
651 struct wl_array *keys)
652{
653}
654
655static void
656keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
657 uint32_t serial, struct wl_surface *surface)
658{
659}
660
661static void
662keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
663 uint32_t serial, uint32_t time, uint32_t key,
664 uint32_t state)
665{
666 struct display *d = data;
667
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900668 if (!d->shell)
669 return;
670
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700671 if (key == KEY_F11 && state) {
672 if (d->window->fullscreen)
673 xdg_surface_unset_fullscreen(d->window->xdg_surface);
674 else
675 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
676 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400677 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300678}
679
680static void
681keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
682 uint32_t serial, uint32_t mods_depressed,
683 uint32_t mods_latched, uint32_t mods_locked,
684 uint32_t group)
685{
686}
687
688static const struct wl_keyboard_listener keyboard_listener = {
689 keyboard_handle_keymap,
690 keyboard_handle_enter,
691 keyboard_handle_leave,
692 keyboard_handle_key,
693 keyboard_handle_modifiers,
694};
695
696static void
Daniel Stone37816df2012-05-16 18:45:18 +0100697seat_handle_capabilities(void *data, struct wl_seat *seat,
698 enum wl_seat_capability caps)
699{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400700 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100701
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400702 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
703 d->pointer = wl_seat_get_pointer(seat);
704 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
705 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
706 wl_pointer_destroy(d->pointer);
707 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100708 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300709
710 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
711 d->keyboard = wl_seat_get_keyboard(seat);
712 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
713 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
714 wl_keyboard_destroy(d->keyboard);
715 d->keyboard = NULL;
716 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700717
718 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
719 d->touch = wl_seat_get_touch(seat);
720 wl_touch_set_user_data(d->touch, d);
721 wl_touch_add_listener(d->touch, &touch_listener, d);
722 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
723 wl_touch_destroy(d->touch);
724 d->touch = NULL;
725 }
Daniel Stone37816df2012-05-16 18:45:18 +0100726}
727
728static const struct wl_seat_listener seat_listener = {
729 seat_handle_capabilities,
730};
731
732static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800733xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
734{
735 xdg_shell_pong(shell, serial);
736}
737
738static const struct xdg_shell_listener xdg_shell_listener = {
739 xdg_shell_ping,
740};
741
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800742#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800743#ifdef static_assert
744static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
745 "Interface version doesn't match implementation version");
746#endif
747
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800748static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400749registry_handle_global(void *data, struct wl_registry *registry,
750 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100751{
752 struct display *d = data;
753
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400754 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400755 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400756 wl_registry_bind(registry, name,
757 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800758 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400759 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800760 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800761 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800762 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100763 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400764 d->seat = wl_registry_bind(registry, name,
765 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400766 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400767 } else if (strcmp(interface, "wl_shm") == 0) {
768 d->shm = wl_registry_bind(registry, name,
769 &wl_shm_interface, 1);
770 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100771 if (!d->cursor_theme) {
772 fprintf(stderr, "unable to load default theme\n");
773 return;
774 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400775 d->default_cursor =
776 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100777 if (!d->default_cursor) {
778 fprintf(stderr, "unable to load default left pointer\n");
779 // TODO: abort ?
780 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900781 } else if (strcmp(interface, "ivi_application") == 0) {
782 d->ivi_application =
783 wl_registry_bind(registry, name,
784 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400785 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100786}
787
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200788static void
789registry_handle_global_remove(void *data, struct wl_registry *registry,
790 uint32_t name)
791{
792}
793
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400794static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200795 registry_handle_global,
796 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400797};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100798
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200799static void
800signal_int(int signum)
801{
802 running = 0;
803}
804
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400805static void
806usage(int error_code)
807{
808 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
809 " -f\tRun in fullscreen mode\n"
810 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700811 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800812 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400813 " -h\tThis help text\n\n");
814
815 exit(error_code);
816}
817
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100818int
819main(int argc, char **argv)
820{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200821 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100822 struct display display = { 0 };
823 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400824 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100825
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100826 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300827 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700828 window.geometry.width = 250;
829 window.geometry.height = 250;
830 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700831 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800832 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100833
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400834 for (i = 1; i < argc; i++) {
835 if (strcmp("-f", argv[i]) == 0)
836 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400837 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300838 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700839 else if (strcmp("-s", argv[i]) == 0)
840 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800841 else if (strcmp("-b", argv[i]) == 0)
842 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400843 else if (strcmp("-h", argv[i]) == 0)
844 usage(EXIT_SUCCESS);
845 else
846 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400847 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300848
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100849 display.display = wl_display_connect(NULL);
850 assert(display.display);
851
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400852 display.registry = wl_display_get_registry(display.display);
853 wl_registry_add_listener(display.registry,
854 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100855
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100856 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200857
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700858 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100859 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500860 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100861
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400862 display.cursor_surface =
863 wl_compositor_create_surface(display.compositor);
864
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200865 sigint.sa_handler = signal_int;
866 sigemptyset(&sigint.sa_mask);
867 sigint.sa_flags = SA_RESETHAND;
868 sigaction(SIGINT, &sigint, NULL);
869
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800870 /* The mainloop here is a little subtle. Redrawing will cause
871 * EGL to read events so we can just call
872 * wl_display_dispatch_pending() to handle any events that got
873 * queued up as a side effect. */
874 while (running && ret != -1) {
875 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800876 redraw(&window, NULL, 0);
877 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500878
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200879 fprintf(stderr, "simple-egl exiting\n");
880
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200881 destroy_surface(&window);
882 fini_egl(&display);
883
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400884 wl_surface_destroy(display.cursor_surface);
885 if (display.cursor_theme)
886 wl_cursor_theme_destroy(display.cursor_theme);
887
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200888 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800889 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200890
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900891 if (display.ivi_application)
892 ivi_application_destroy(display.ivi_application);
893
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200894 if (display.compositor)
895 wl_compositor_destroy(display.compositor);
896
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200897 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200898 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500899 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200900
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100901 return 0;
902}