blob: 33e02e75ac070ba8251be64af9d0e6b9c6a2252f [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010028#include <stdbool.h>
29#include <math.h>
30#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020031#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010032
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030033#include <linux/input.h>
34
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050036#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040037#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040041#include <EGL/eglext.h>
42
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080043#include "xdg-shell-client-protocol.h"
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090044#include <sys/types.h>
45#include <unistd.h>
46#include "protocol/ivi-application-client-protocol.h"
47#define IVI_SURFACE_ID 9000
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080048
Jonny Lamb51a7ae52015-03-20 15:26:51 +010049#include "../shared/platform.h"
50
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040051#ifndef EGL_EXT_swap_buffers_with_damage
52#define EGL_EXT_swap_buffers_with_damage 1
53typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
54#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010055
Kristian Høgsberg77787432013-08-22 12:16:51 -070056#ifndef EGL_EXT_buffer_age
57#define EGL_EXT_buffer_age 1
58#define EGL_BUFFER_AGE_EXT 0x313D
59#endif
60
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030061struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010062struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030063
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010064struct display {
65 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040066 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050067 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080068 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040069 struct wl_seat *seat;
70 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070071 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030072 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040073 struct wl_shm *shm;
74 struct wl_cursor_theme *cursor_theme;
75 struct wl_cursor *default_cursor;
76 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010077 struct {
78 EGLDisplay dpy;
79 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050080 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010081 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030082 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090083 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040084
85 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086};
87
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030088struct geometry {
89 int width, height;
90};
91
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010092struct window {
93 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060094 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010095 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010096 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010097 GLuint pos;
98 GLuint col;
99 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500100
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800101 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500102 struct wl_egl_window *native;
103 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800104 struct xdg_surface *xdg_surface;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900105 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500106 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200107 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500108 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100109};
110
111static const char *vert_shader_text =
112 "uniform mat4 rotation;\n"
113 "attribute vec4 pos;\n"
114 "attribute vec4 color;\n"
115 "varying vec4 v_color;\n"
116 "void main() {\n"
117 " gl_Position = rotation * pos;\n"
118 " v_color = color;\n"
119 "}\n";
120
121static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500122 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100123 "varying vec4 v_color;\n"
124 "void main() {\n"
125 " gl_FragColor = v_color;\n"
126 "}\n";
127
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400128static int running = 1;
129
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100130static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700131init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100132{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500133 static const EGLint context_attribs[] = {
134 EGL_CONTEXT_CLIENT_VERSION, 2,
135 EGL_NONE
136 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400137 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500138
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300139 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500140 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500141 EGL_RED_SIZE, 1,
142 EGL_GREEN_SIZE, 1,
143 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200144 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500145 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
146 EGL_NONE
147 };
148
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700149 EGLint major, minor, n, count, i, size;
150 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100151 EGLBoolean ret;
152
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700153 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400154 config_attribs[9] = 0;
155
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100156 display->egl.dpy =
157 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
158 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100159 assert(display->egl.dpy);
160
161 ret = eglInitialize(display->egl.dpy, &major, &minor);
162 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500163 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100164 assert(ret == EGL_TRUE);
165
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700166 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
167 assert(0);
168
169 configs = calloc(count, sizeof *configs);
170 assert(configs);
171
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300172 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700173 configs, count, &n);
174 assert(ret && n >= 1);
175
176 for (i = 0; i < n; i++) {
177 eglGetConfigAttrib(display->egl.dpy,
178 configs[i], EGL_BUFFER_SIZE, &size);
179 if (window->buffer_size == size) {
180 display->egl.conf = configs[i];
181 break;
182 }
183 }
184 free(configs);
185 if (display->egl.conf == NULL) {
186 fprintf(stderr, "did not find config with buffer size %d\n",
187 window->buffer_size);
188 exit(EXIT_FAILURE);
189 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500190
191 display->egl.ctx = eglCreateContext(display->egl.dpy,
192 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500193 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100194 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500195
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400196 display->swap_buffers_with_damage = NULL;
197 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
198 if (extensions &&
199 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
200 strstr(extensions, "EGL_EXT_buffer_age"))
201 display->swap_buffers_with_damage =
202 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
203 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
204
205 if (display->swap_buffers_with_damage)
206 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
207
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100208}
209
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200210static void
211fini_egl(struct display *display)
212{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200213 eglTerminate(display->egl.dpy);
214 eglReleaseThread();
215}
216
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100217static GLuint
218create_shader(struct window *window, const char *source, GLenum shader_type)
219{
220 GLuint shader;
221 GLint status;
222
223 shader = glCreateShader(shader_type);
224 assert(shader != 0);
225
226 glShaderSource(shader, 1, (const char **) &source, NULL);
227 glCompileShader(shader);
228
229 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
230 if (!status) {
231 char log[1000];
232 GLsizei len;
233 glGetShaderInfoLog(shader, 1000, &len, log);
234 fprintf(stderr, "Error: compiling %s: %*s\n",
235 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
236 len, log);
237 exit(1);
238 }
239
240 return shader;
241}
242
243static void
244init_gl(struct window *window)
245{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100246 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600247 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100248 GLint status;
249
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100250 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
251 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
252
Scott Moreau3ea23d02012-06-13 17:42:21 -0600253 program = glCreateProgram();
254 glAttachShader(program, frag);
255 glAttachShader(program, vert);
256 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100257
Scott Moreau3ea23d02012-06-13 17:42:21 -0600258 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100259 if (!status) {
260 char log[1000];
261 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600262 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100263 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
264 exit(1);
265 }
266
Scott Moreau3ea23d02012-06-13 17:42:21 -0600267 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900268
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100269 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600270 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100271
Scott Moreau3ea23d02012-06-13 17:42:21 -0600272 glBindAttribLocation(program, window->gl.pos, "pos");
273 glBindAttribLocation(program, window->gl.col, "color");
274 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100275
276 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600277 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100278}
279
280static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800281handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700282 int32_t width, int32_t height,
283 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300284{
285 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700286 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300287
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700288 window->fullscreen = 0;
289 wl_array_for_each(p, states) {
290 uint32_t state = *p;
291 switch (state) {
292 case XDG_SURFACE_STATE_FULLSCREEN:
293 window->fullscreen = 1;
294 break;
295 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500296 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800297
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700298 if (width > 0 && height > 0) {
299 if (!window->fullscreen) {
300 window->window_size.width = width;
301 window->window_size.height = height;
302 }
303 window->geometry.width = width;
304 window->geometry.height = height;
305 } else if (!window->fullscreen) {
306 window->geometry = window->window_size;
307 }
308
309 if (window->native)
310 wl_egl_window_resize(window->native,
311 window->geometry.width,
312 window->geometry.height, 0, 0);
313
314 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800315}
316
317static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500318handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
319{
320 running = 0;
321}
322
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800323static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800324 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500325 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300326};
327
328static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900329handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
330 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100331{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900332 struct window *window = data;
333
334 wl_egl_window_resize(window->native, width, height, 0, 0);
335
336 window->geometry.width = width;
337 window->geometry.height = height;
338
339 if (!window->fullscreen)
340 window->window_size = window->geometry;
341}
342
343static const struct ivi_surface_listener ivi_surface_listener = {
344 handle_ivi_surface_configure,
345};
346
347static void
348create_xdg_surface(struct window *window, struct display *display)
349{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800350 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
351 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300352
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800353 xdg_surface_add_listener(window->xdg_surface,
354 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300355
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900356 xdg_surface_set_title(window->xdg_surface, "simple-egl");
357}
358
359static void
360create_ivi_surface(struct window *window, struct display *display)
361{
362 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
363 window->ivi_surface =
364 ivi_application_surface_create(display->ivi_application,
365 id_ivisurf, window->surface);
366
367 if (window->ivi_surface == NULL) {
368 fprintf(stderr, "Failed to create ivi_client_surface\n");
369 abort();
370 }
371
372 ivi_surface_add_listener(window->ivi_surface,
373 &ivi_surface_listener, window);
374}
375
376static void
377create_surface(struct window *window)
378{
379 struct display *display = window->display;
380 EGLBoolean ret;
381
382 window->surface = wl_compositor_create_surface(display->compositor);
383
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500384 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400385 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700386 window->geometry.width,
387 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500388 window->egl_surface =
389 eglCreateWindowSurface(display->egl.dpy,
390 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500391 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100392
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900393 if (display->shell) {
394 create_xdg_surface(window, display);
395 } else if (display->ivi_application ) {
396 create_ivi_surface(window, display);
397 } else {
398 assert(0);
399 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600400
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500401 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
402 window->egl_surface, window->display->egl.ctx);
403 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300404
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800405 if (!window->frame_sync)
406 eglSwapInterval(display->egl.dpy, 0);
407
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900408 if (!display->shell)
409 return;
410
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700411 if (window->fullscreen)
412 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100413}
414
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200415static void
416destroy_surface(struct window *window)
417{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000418 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
419 * on eglReleaseThread(). */
420 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
421 EGL_NO_CONTEXT);
422
423 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200424 wl_egl_window_destroy(window->native);
425
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900426 if (window->xdg_surface)
427 xdg_surface_destroy(window->xdg_surface);
428 if (window->display->ivi_application)
429 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200430 wl_surface_destroy(window->surface);
431
432 if (window->callback)
433 wl_callback_destroy(window->callback);
434}
435
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100436static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400437redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100438{
439 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400440 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100441 static const GLfloat verts[3][2] = {
442 { -0.5, -0.5 },
443 { 0.5, -0.5 },
444 { 0, 0.5 }
445 };
446 static const GLfloat colors[3][3] = {
447 { 1, 0, 0 },
448 { 0, 1, 0 },
449 { 0, 0, 1 }
450 };
451 GLfloat angle;
452 GLfloat rotation[4][4] = {
453 { 1, 0, 0, 0 },
454 { 0, 1, 0, 0 },
455 { 0, 0, 1, 0 },
456 { 0, 0, 0, 1 }
457 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100458 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400459 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400460 EGLint rect[4];
461 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800462 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100463
Scott Moreau7e300db2012-08-31 03:18:15 -0600464 assert(window->callback == callback);
465 window->callback = NULL;
466
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300467 if (callback)
468 wl_callback_destroy(callback);
469
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800470 gettimeofday(&tv, NULL);
471 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
472 if (window->frames == 0)
473 window->benchmark_time = time;
474 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
475 printf("%d frames in %d seconds: %f fps\n",
476 window->frames,
477 benchmark_interval,
478 (float) window->frames / benchmark_interval);
479 window->benchmark_time = time;
480 window->frames = 0;
481 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100482
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800483 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100484 rotation[0][0] = cos(angle);
485 rotation[0][2] = sin(angle);
486 rotation[2][0] = -sin(angle);
487 rotation[2][2] = cos(angle);
488
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400489 if (display->swap_buffers_with_damage)
490 eglQuerySurface(display->egl.dpy, window->egl_surface,
491 EGL_BUFFER_AGE_EXT, &buffer_age);
492
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300493 glViewport(0, 0, window->geometry.width, window->geometry.height);
494
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100495 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
496 (GLfloat *) rotation);
497
498 glClearColor(0.0, 0.0, 0.0, 0.5);
499 glClear(GL_COLOR_BUFFER_BIT);
500
501 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
502 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
503 glEnableVertexAttribArray(window->gl.pos);
504 glEnableVertexAttribArray(window->gl.col);
505
506 glDrawArrays(GL_TRIANGLES, 0, 3);
507
508 glDisableVertexAttribArray(window->gl.pos);
509 glDisableVertexAttribArray(window->gl.col);
510
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300511 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400512 region = wl_compositor_create_region(window->display->compositor);
513 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300514 window->geometry.width,
515 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400516 wl_surface_set_opaque_region(window->surface, region);
517 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700518 } else {
519 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400520 }
521
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400522 if (display->swap_buffers_with_damage && buffer_age > 0) {
523 rect[0] = window->geometry.width / 4 - 1;
524 rect[1] = window->geometry.height / 4 - 1;
525 rect[2] = window->geometry.width / 2 + 2;
526 rect[3] = window->geometry.height / 2 + 2;
527 display->swap_buffers_with_damage(display->egl.dpy,
528 window->egl_surface,
529 rect, 1);
530 } else {
531 eglSwapBuffers(display->egl.dpy, window->egl_surface);
532 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800533 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100534}
535
536static void
Daniel Stone37816df2012-05-16 18:45:18 +0100537pointer_handle_enter(void *data, struct wl_pointer *pointer,
538 uint32_t serial, struct wl_surface *surface,
539 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300540{
541 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400542 struct wl_buffer *buffer;
543 struct wl_cursor *cursor = display->default_cursor;
544 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300545
546 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300547 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400548 else if (cursor) {
549 image = display->default_cursor->images[0];
550 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100551 if (!buffer)
552 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400553 wl_pointer_set_cursor(pointer, serial,
554 display->cursor_surface,
555 image->hotspot_x,
556 image->hotspot_y);
557 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
558 wl_surface_damage(display->cursor_surface, 0, 0,
559 image->width, image->height);
560 wl_surface_commit(display->cursor_surface);
561 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300562}
563
564static void
Daniel Stone37816df2012-05-16 18:45:18 +0100565pointer_handle_leave(void *data, struct wl_pointer *pointer,
566 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300567{
568}
569
570static void
Daniel Stone37816df2012-05-16 18:45:18 +0100571pointer_handle_motion(void *data, struct wl_pointer *pointer,
572 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300573{
574}
575
576static void
Daniel Stone37816df2012-05-16 18:45:18 +0100577pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
578 uint32_t serial, uint32_t time, uint32_t button,
579 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300580{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300581 struct display *display = data;
582
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900583 if (!display->window->xdg_surface)
584 return;
585
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300586 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800587 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900588 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300589}
590
591static void
Daniel Stone37816df2012-05-16 18:45:18 +0100592pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100593 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300594{
595}
596
Daniel Stone37816df2012-05-16 18:45:18 +0100597static const struct wl_pointer_listener pointer_listener = {
598 pointer_handle_enter,
599 pointer_handle_leave,
600 pointer_handle_motion,
601 pointer_handle_button,
602 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300603};
604
605static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700606touch_handle_down(void *data, struct wl_touch *wl_touch,
607 uint32_t serial, uint32_t time, struct wl_surface *surface,
608 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
609{
610 struct display *d = (struct display *)data;
611
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900612 if (!d->shell)
613 return;
614
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800615 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700616}
617
618static void
619touch_handle_up(void *data, struct wl_touch *wl_touch,
620 uint32_t serial, uint32_t time, int32_t id)
621{
622}
623
624static void
625touch_handle_motion(void *data, struct wl_touch *wl_touch,
626 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
627{
628}
629
630static void
631touch_handle_frame(void *data, struct wl_touch *wl_touch)
632{
633}
634
635static void
636touch_handle_cancel(void *data, struct wl_touch *wl_touch)
637{
638}
639
640static const struct wl_touch_listener touch_listener = {
641 touch_handle_down,
642 touch_handle_up,
643 touch_handle_motion,
644 touch_handle_frame,
645 touch_handle_cancel,
646};
647
648static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300649keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
650 uint32_t format, int fd, uint32_t size)
651{
652}
653
654static void
655keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
656 uint32_t serial, struct wl_surface *surface,
657 struct wl_array *keys)
658{
659}
660
661static void
662keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
663 uint32_t serial, struct wl_surface *surface)
664{
665}
666
667static void
668keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
669 uint32_t serial, uint32_t time, uint32_t key,
670 uint32_t state)
671{
672 struct display *d = data;
673
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900674 if (!d->shell)
675 return;
676
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700677 if (key == KEY_F11 && state) {
678 if (d->window->fullscreen)
679 xdg_surface_unset_fullscreen(d->window->xdg_surface);
680 else
681 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
682 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400683 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300684}
685
686static void
687keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
688 uint32_t serial, uint32_t mods_depressed,
689 uint32_t mods_latched, uint32_t mods_locked,
690 uint32_t group)
691{
692}
693
694static const struct wl_keyboard_listener keyboard_listener = {
695 keyboard_handle_keymap,
696 keyboard_handle_enter,
697 keyboard_handle_leave,
698 keyboard_handle_key,
699 keyboard_handle_modifiers,
700};
701
702static void
Daniel Stone37816df2012-05-16 18:45:18 +0100703seat_handle_capabilities(void *data, struct wl_seat *seat,
704 enum wl_seat_capability caps)
705{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400706 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100707
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400708 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
709 d->pointer = wl_seat_get_pointer(seat);
710 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
711 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
712 wl_pointer_destroy(d->pointer);
713 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100714 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300715
716 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
717 d->keyboard = wl_seat_get_keyboard(seat);
718 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
719 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
720 wl_keyboard_destroy(d->keyboard);
721 d->keyboard = NULL;
722 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700723
724 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
725 d->touch = wl_seat_get_touch(seat);
726 wl_touch_set_user_data(d->touch, d);
727 wl_touch_add_listener(d->touch, &touch_listener, d);
728 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
729 wl_touch_destroy(d->touch);
730 d->touch = NULL;
731 }
Daniel Stone37816df2012-05-16 18:45:18 +0100732}
733
734static const struct wl_seat_listener seat_listener = {
735 seat_handle_capabilities,
736};
737
738static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800739xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
740{
741 xdg_shell_pong(shell, serial);
742}
743
744static const struct xdg_shell_listener xdg_shell_listener = {
745 xdg_shell_ping,
746};
747
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800748#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800749#ifdef static_assert
750static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
751 "Interface version doesn't match implementation version");
752#endif
753
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800754static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400755registry_handle_global(void *data, struct wl_registry *registry,
756 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100757{
758 struct display *d = data;
759
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400760 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400761 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400762 wl_registry_bind(registry, name,
763 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800764 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400765 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800766 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800767 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800768 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100769 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400770 d->seat = wl_registry_bind(registry, name,
771 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400772 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400773 } else if (strcmp(interface, "wl_shm") == 0) {
774 d->shm = wl_registry_bind(registry, name,
775 &wl_shm_interface, 1);
776 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100777 if (!d->cursor_theme) {
778 fprintf(stderr, "unable to load default theme\n");
779 return;
780 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400781 d->default_cursor =
782 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100783 if (!d->default_cursor) {
784 fprintf(stderr, "unable to load default left pointer\n");
785 // TODO: abort ?
786 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900787 } else if (strcmp(interface, "ivi_application") == 0) {
788 d->ivi_application =
789 wl_registry_bind(registry, name,
790 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400791 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100792}
793
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200794static void
795registry_handle_global_remove(void *data, struct wl_registry *registry,
796 uint32_t name)
797{
798}
799
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400800static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200801 registry_handle_global,
802 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400803};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100804
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200805static void
806signal_int(int signum)
807{
808 running = 0;
809}
810
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400811static void
812usage(int error_code)
813{
814 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
815 " -f\tRun in fullscreen mode\n"
816 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700817 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800818 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400819 " -h\tThis help text\n\n");
820
821 exit(error_code);
822}
823
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100824int
825main(int argc, char **argv)
826{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200827 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100828 struct display display = { 0 };
829 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400830 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100831
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100832 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300833 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700834 window.geometry.width = 250;
835 window.geometry.height = 250;
836 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700837 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800838 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100839
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400840 for (i = 1; i < argc; i++) {
841 if (strcmp("-f", argv[i]) == 0)
842 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400843 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300844 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700845 else if (strcmp("-s", argv[i]) == 0)
846 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800847 else if (strcmp("-b", argv[i]) == 0)
848 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400849 else if (strcmp("-h", argv[i]) == 0)
850 usage(EXIT_SUCCESS);
851 else
852 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400853 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300854
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100855 display.display = wl_display_connect(NULL);
856 assert(display.display);
857
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400858 display.registry = wl_display_get_registry(display.display);
859 wl_registry_add_listener(display.registry,
860 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100861
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400862 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200863
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700864 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100865 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500866 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100867
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400868 display.cursor_surface =
869 wl_compositor_create_surface(display.compositor);
870
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200871 sigint.sa_handler = signal_int;
872 sigemptyset(&sigint.sa_mask);
873 sigint.sa_flags = SA_RESETHAND;
874 sigaction(SIGINT, &sigint, NULL);
875
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800876 /* The mainloop here is a little subtle. Redrawing will cause
877 * EGL to read events so we can just call
878 * wl_display_dispatch_pending() to handle any events that got
879 * queued up as a side effect. */
880 while (running && ret != -1) {
881 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800882 redraw(&window, NULL, 0);
883 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500884
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200885 fprintf(stderr, "simple-egl exiting\n");
886
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200887 destroy_surface(&window);
888 fini_egl(&display);
889
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400890 wl_surface_destroy(display.cursor_surface);
891 if (display.cursor_theme)
892 wl_cursor_theme_destroy(display.cursor_theme);
893
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200894 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800895 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200896
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900897 if (display.ivi_application)
898 ivi_application_destroy(display.ivi_application);
899
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200900 if (display.compositor)
901 wl_compositor_destroy(display.compositor);
902
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200903 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200904 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500905 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200906
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100907 return 0;
908}