blob: c6a95ebba30c2d7eb74d3ac0aadc117f8ecc6567 [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 =
Jonny Lambabff8832015-03-24 13:12:09 +0100389 weston_platform_create_egl_surface(display->egl.dpy,
390 display->egl.conf,
391 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100392
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100393
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900394 if (display->shell) {
395 create_xdg_surface(window, display);
396 } else if (display->ivi_application ) {
397 create_ivi_surface(window, display);
398 } else {
399 assert(0);
400 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600401
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500402 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
403 window->egl_surface, window->display->egl.ctx);
404 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300405
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800406 if (!window->frame_sync)
407 eglSwapInterval(display->egl.dpy, 0);
408
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900409 if (!display->shell)
410 return;
411
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700412 if (window->fullscreen)
413 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100414}
415
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200416static void
417destroy_surface(struct window *window)
418{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000419 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
420 * on eglReleaseThread(). */
421 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
422 EGL_NO_CONTEXT);
423
424 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200425 wl_egl_window_destroy(window->native);
426
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900427 if (window->xdg_surface)
428 xdg_surface_destroy(window->xdg_surface);
429 if (window->display->ivi_application)
430 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200431 wl_surface_destroy(window->surface);
432
433 if (window->callback)
434 wl_callback_destroy(window->callback);
435}
436
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100437static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400438redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100439{
440 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400441 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100442 static const GLfloat verts[3][2] = {
443 { -0.5, -0.5 },
444 { 0.5, -0.5 },
445 { 0, 0.5 }
446 };
447 static const GLfloat colors[3][3] = {
448 { 1, 0, 0 },
449 { 0, 1, 0 },
450 { 0, 0, 1 }
451 };
452 GLfloat angle;
453 GLfloat rotation[4][4] = {
454 { 1, 0, 0, 0 },
455 { 0, 1, 0, 0 },
456 { 0, 0, 1, 0 },
457 { 0, 0, 0, 1 }
458 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100459 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400460 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400461 EGLint rect[4];
462 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800463 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100464
Scott Moreau7e300db2012-08-31 03:18:15 -0600465 assert(window->callback == callback);
466 window->callback = NULL;
467
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300468 if (callback)
469 wl_callback_destroy(callback);
470
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800471 gettimeofday(&tv, NULL);
472 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
473 if (window->frames == 0)
474 window->benchmark_time = time;
475 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
476 printf("%d frames in %d seconds: %f fps\n",
477 window->frames,
478 benchmark_interval,
479 (float) window->frames / benchmark_interval);
480 window->benchmark_time = time;
481 window->frames = 0;
482 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100483
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800484 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100485 rotation[0][0] = cos(angle);
486 rotation[0][2] = sin(angle);
487 rotation[2][0] = -sin(angle);
488 rotation[2][2] = cos(angle);
489
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400490 if (display->swap_buffers_with_damage)
491 eglQuerySurface(display->egl.dpy, window->egl_surface,
492 EGL_BUFFER_AGE_EXT, &buffer_age);
493
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300494 glViewport(0, 0, window->geometry.width, window->geometry.height);
495
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100496 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
497 (GLfloat *) rotation);
498
499 glClearColor(0.0, 0.0, 0.0, 0.5);
500 glClear(GL_COLOR_BUFFER_BIT);
501
502 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
503 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
504 glEnableVertexAttribArray(window->gl.pos);
505 glEnableVertexAttribArray(window->gl.col);
506
507 glDrawArrays(GL_TRIANGLES, 0, 3);
508
509 glDisableVertexAttribArray(window->gl.pos);
510 glDisableVertexAttribArray(window->gl.col);
511
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300512 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400513 region = wl_compositor_create_region(window->display->compositor);
514 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300515 window->geometry.width,
516 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400517 wl_surface_set_opaque_region(window->surface, region);
518 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700519 } else {
520 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400521 }
522
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400523 if (display->swap_buffers_with_damage && buffer_age > 0) {
524 rect[0] = window->geometry.width / 4 - 1;
525 rect[1] = window->geometry.height / 4 - 1;
526 rect[2] = window->geometry.width / 2 + 2;
527 rect[3] = window->geometry.height / 2 + 2;
528 display->swap_buffers_with_damage(display->egl.dpy,
529 window->egl_surface,
530 rect, 1);
531 } else {
532 eglSwapBuffers(display->egl.dpy, window->egl_surface);
533 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800534 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100535}
536
537static void
Daniel Stone37816df2012-05-16 18:45:18 +0100538pointer_handle_enter(void *data, struct wl_pointer *pointer,
539 uint32_t serial, struct wl_surface *surface,
540 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300541{
542 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400543 struct wl_buffer *buffer;
544 struct wl_cursor *cursor = display->default_cursor;
545 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300546
547 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300548 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400549 else if (cursor) {
550 image = display->default_cursor->images[0];
551 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100552 if (!buffer)
553 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400554 wl_pointer_set_cursor(pointer, serial,
555 display->cursor_surface,
556 image->hotspot_x,
557 image->hotspot_y);
558 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
559 wl_surface_damage(display->cursor_surface, 0, 0,
560 image->width, image->height);
561 wl_surface_commit(display->cursor_surface);
562 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300563}
564
565static void
Daniel Stone37816df2012-05-16 18:45:18 +0100566pointer_handle_leave(void *data, struct wl_pointer *pointer,
567 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300568{
569}
570
571static void
Daniel Stone37816df2012-05-16 18:45:18 +0100572pointer_handle_motion(void *data, struct wl_pointer *pointer,
573 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300574{
575}
576
577static void
Daniel Stone37816df2012-05-16 18:45:18 +0100578pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
579 uint32_t serial, uint32_t time, uint32_t button,
580 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300581{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300582 struct display *display = data;
583
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900584 if (!display->window->xdg_surface)
585 return;
586
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300587 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800588 xdg_surface_move(display->window->xdg_surface,
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900589 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300590}
591
592static void
Daniel Stone37816df2012-05-16 18:45:18 +0100593pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100594 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300595{
596}
597
Daniel Stone37816df2012-05-16 18:45:18 +0100598static const struct wl_pointer_listener pointer_listener = {
599 pointer_handle_enter,
600 pointer_handle_leave,
601 pointer_handle_motion,
602 pointer_handle_button,
603 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300604};
605
606static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700607touch_handle_down(void *data, struct wl_touch *wl_touch,
608 uint32_t serial, uint32_t time, struct wl_surface *surface,
609 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
610{
611 struct display *d = (struct display *)data;
612
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900613 if (!d->shell)
614 return;
615
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800616 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700617}
618
619static void
620touch_handle_up(void *data, struct wl_touch *wl_touch,
621 uint32_t serial, uint32_t time, int32_t id)
622{
623}
624
625static void
626touch_handle_motion(void *data, struct wl_touch *wl_touch,
627 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
628{
629}
630
631static void
632touch_handle_frame(void *data, struct wl_touch *wl_touch)
633{
634}
635
636static void
637touch_handle_cancel(void *data, struct wl_touch *wl_touch)
638{
639}
640
641static const struct wl_touch_listener touch_listener = {
642 touch_handle_down,
643 touch_handle_up,
644 touch_handle_motion,
645 touch_handle_frame,
646 touch_handle_cancel,
647};
648
649static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300650keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
651 uint32_t format, int fd, uint32_t size)
652{
653}
654
655static void
656keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
657 uint32_t serial, struct wl_surface *surface,
658 struct wl_array *keys)
659{
660}
661
662static void
663keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
664 uint32_t serial, struct wl_surface *surface)
665{
666}
667
668static void
669keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
670 uint32_t serial, uint32_t time, uint32_t key,
671 uint32_t state)
672{
673 struct display *d = data;
674
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900675 if (!d->shell)
676 return;
677
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700678 if (key == KEY_F11 && state) {
679 if (d->window->fullscreen)
680 xdg_surface_unset_fullscreen(d->window->xdg_surface);
681 else
682 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
683 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400684 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300685}
686
687static void
688keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
689 uint32_t serial, uint32_t mods_depressed,
690 uint32_t mods_latched, uint32_t mods_locked,
691 uint32_t group)
692{
693}
694
695static const struct wl_keyboard_listener keyboard_listener = {
696 keyboard_handle_keymap,
697 keyboard_handle_enter,
698 keyboard_handle_leave,
699 keyboard_handle_key,
700 keyboard_handle_modifiers,
701};
702
703static void
Daniel Stone37816df2012-05-16 18:45:18 +0100704seat_handle_capabilities(void *data, struct wl_seat *seat,
705 enum wl_seat_capability caps)
706{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400707 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100708
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400709 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
710 d->pointer = wl_seat_get_pointer(seat);
711 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
712 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
713 wl_pointer_destroy(d->pointer);
714 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100715 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300716
717 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
718 d->keyboard = wl_seat_get_keyboard(seat);
719 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
720 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
721 wl_keyboard_destroy(d->keyboard);
722 d->keyboard = NULL;
723 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700724
725 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
726 d->touch = wl_seat_get_touch(seat);
727 wl_touch_set_user_data(d->touch, d);
728 wl_touch_add_listener(d->touch, &touch_listener, d);
729 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
730 wl_touch_destroy(d->touch);
731 d->touch = NULL;
732 }
Daniel Stone37816df2012-05-16 18:45:18 +0100733}
734
735static const struct wl_seat_listener seat_listener = {
736 seat_handle_capabilities,
737};
738
739static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800740xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
741{
742 xdg_shell_pong(shell, serial);
743}
744
745static const struct xdg_shell_listener xdg_shell_listener = {
746 xdg_shell_ping,
747};
748
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800749#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800750#ifdef static_assert
751static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
752 "Interface version doesn't match implementation version");
753#endif
754
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800755static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400756registry_handle_global(void *data, struct wl_registry *registry,
757 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100758{
759 struct display *d = data;
760
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400761 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400762 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400763 wl_registry_bind(registry, name,
764 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800765 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400766 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800767 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800768 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800769 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100770 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400771 d->seat = wl_registry_bind(registry, name,
772 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400773 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400774 } else if (strcmp(interface, "wl_shm") == 0) {
775 d->shm = wl_registry_bind(registry, name,
776 &wl_shm_interface, 1);
777 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100778 if (!d->cursor_theme) {
779 fprintf(stderr, "unable to load default theme\n");
780 return;
781 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400782 d->default_cursor =
783 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100784 if (!d->default_cursor) {
785 fprintf(stderr, "unable to load default left pointer\n");
786 // TODO: abort ?
787 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900788 } else if (strcmp(interface, "ivi_application") == 0) {
789 d->ivi_application =
790 wl_registry_bind(registry, name,
791 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400792 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100793}
794
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200795static void
796registry_handle_global_remove(void *data, struct wl_registry *registry,
797 uint32_t name)
798{
799}
800
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400801static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200802 registry_handle_global,
803 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400804};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100805
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200806static void
807signal_int(int signum)
808{
809 running = 0;
810}
811
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400812static void
813usage(int error_code)
814{
815 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
816 " -f\tRun in fullscreen mode\n"
817 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700818 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800819 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400820 " -h\tThis help text\n\n");
821
822 exit(error_code);
823}
824
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100825int
826main(int argc, char **argv)
827{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200828 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100829 struct display display = { 0 };
830 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400831 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100832
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100833 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300834 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700835 window.geometry.width = 250;
836 window.geometry.height = 250;
837 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700838 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800839 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100840
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400841 for (i = 1; i < argc; i++) {
842 if (strcmp("-f", argv[i]) == 0)
843 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400844 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300845 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700846 else if (strcmp("-s", argv[i]) == 0)
847 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800848 else if (strcmp("-b", argv[i]) == 0)
849 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400850 else if (strcmp("-h", argv[i]) == 0)
851 usage(EXIT_SUCCESS);
852 else
853 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400854 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300855
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100856 display.display = wl_display_connect(NULL);
857 assert(display.display);
858
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400859 display.registry = wl_display_get_registry(display.display);
860 wl_registry_add_listener(display.registry,
861 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100862
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400863 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200864
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700865 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100866 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500867 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100868
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400869 display.cursor_surface =
870 wl_compositor_create_surface(display.compositor);
871
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200872 sigint.sa_handler = signal_int;
873 sigemptyset(&sigint.sa_mask);
874 sigint.sa_flags = SA_RESETHAND;
875 sigaction(SIGINT, &sigint, NULL);
876
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800877 /* The mainloop here is a little subtle. Redrawing will cause
878 * EGL to read events so we can just call
879 * wl_display_dispatch_pending() to handle any events that got
880 * queued up as a side effect. */
881 while (running && ret != -1) {
882 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800883 redraw(&window, NULL, 0);
884 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500885
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200886 fprintf(stderr, "simple-egl exiting\n");
887
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200888 destroy_surface(&window);
889 fini_egl(&display);
890
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400891 wl_surface_destroy(display.cursor_surface);
892 if (display.cursor_theme)
893 wl_cursor_theme_destroy(display.cursor_theme);
894
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200895 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800896 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200897
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900898 if (display.ivi_application)
899 ivi_application_destroy(display.ivi_application);
900
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200901 if (display.compositor)
902 wl_compositor_destroy(display.compositor);
903
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200904 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200905 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500906 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200907
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100908 return 0;
909}