blob: 9d401f962d45edebc168215cc7929606b183e320 [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 Ådahl83630022016-08-11 23:13:20 +080045#include "xdg-shell-unstable-v6-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;
Jonas Ådahl83630022016-08-11 23:13:20 +080061 struct zxdg_shell_v6 *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;
Jonas Ådahl83630022016-08-11 23:13:20 +080097 struct zxdg_surface_v6 *xdg_surface;
98 struct zxdg_toplevel_v6 *xdg_toplevel;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090099 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500100 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200101 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500102 int fullscreen, opaque, buffer_size, frame_sync;
Jonas Ådahl83630022016-08-11 23:13:20 +0800103 bool wait_for_configure;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100104};
105
106static const char *vert_shader_text =
107 "uniform mat4 rotation;\n"
108 "attribute vec4 pos;\n"
109 "attribute vec4 color;\n"
110 "varying vec4 v_color;\n"
111 "void main() {\n"
112 " gl_Position = rotation * pos;\n"
113 " v_color = color;\n"
114 "}\n";
115
116static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500117 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100118 "varying vec4 v_color;\n"
119 "void main() {\n"
120 " gl_FragColor = v_color;\n"
121 "}\n";
122
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400123static int running = 1;
124
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100125static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700126init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100127{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500128 static const EGLint context_attribs[] = {
129 EGL_CONTEXT_CLIENT_VERSION, 2,
130 EGL_NONE
131 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400132 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500133
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300134 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500135 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500136 EGL_RED_SIZE, 1,
137 EGL_GREEN_SIZE, 1,
138 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200139 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500140 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
141 EGL_NONE
142 };
143
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700144 EGLint major, minor, n, count, i, size;
145 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100146 EGLBoolean ret;
147
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700148 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400149 config_attribs[9] = 0;
150
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100151 display->egl.dpy =
152 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
153 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100154 assert(display->egl.dpy);
155
156 ret = eglInitialize(display->egl.dpy, &major, &minor);
157 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500158 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100159 assert(ret == EGL_TRUE);
160
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700161 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
162 assert(0);
163
164 configs = calloc(count, sizeof *configs);
165 assert(configs);
166
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300167 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700168 configs, count, &n);
169 assert(ret && n >= 1);
170
171 for (i = 0; i < n; i++) {
172 eglGetConfigAttrib(display->egl.dpy,
173 configs[i], EGL_BUFFER_SIZE, &size);
174 if (window->buffer_size == size) {
175 display->egl.conf = configs[i];
176 break;
177 }
178 }
179 free(configs);
180 if (display->egl.conf == NULL) {
181 fprintf(stderr, "did not find config with buffer size %d\n",
182 window->buffer_size);
183 exit(EXIT_FAILURE);
184 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500185
186 display->egl.ctx = eglCreateContext(display->egl.dpy,
187 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500188 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100189 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500190
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400191 display->swap_buffers_with_damage = NULL;
192 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
193 if (extensions &&
Emil Velikov025ad932016-07-04 15:34:21 +0100194 weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage") &&
195 weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400196 display->swap_buffers_with_damage =
197 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
198 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
199
200 if (display->swap_buffers_with_damage)
201 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
202
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100203}
204
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200205static void
206fini_egl(struct display *display)
207{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200208 eglTerminate(display->egl.dpy);
209 eglReleaseThread();
210}
211
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100212static GLuint
213create_shader(struct window *window, const char *source, GLenum shader_type)
214{
215 GLuint shader;
216 GLint status;
217
218 shader = glCreateShader(shader_type);
219 assert(shader != 0);
220
221 glShaderSource(shader, 1, (const char **) &source, NULL);
222 glCompileShader(shader);
223
224 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
225 if (!status) {
226 char log[1000];
227 GLsizei len;
228 glGetShaderInfoLog(shader, 1000, &len, log);
229 fprintf(stderr, "Error: compiling %s: %*s\n",
230 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
231 len, log);
232 exit(1);
233 }
234
235 return shader;
236}
237
238static void
239init_gl(struct window *window)
240{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100241 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600242 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100243 GLint status;
244
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100245 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
246 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
247
Scott Moreau3ea23d02012-06-13 17:42:21 -0600248 program = glCreateProgram();
249 glAttachShader(program, frag);
250 glAttachShader(program, vert);
251 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100252
Scott Moreau3ea23d02012-06-13 17:42:21 -0600253 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100254 if (!status) {
255 char log[1000];
256 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600257 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100258 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
259 exit(1);
260 }
261
Scott Moreau3ea23d02012-06-13 17:42:21 -0600262 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900263
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100264 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600265 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100266
Scott Moreau3ea23d02012-06-13 17:42:21 -0600267 glBindAttribLocation(program, window->gl.pos, "pos");
268 glBindAttribLocation(program, window->gl.col, "color");
269 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100270
271 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600272 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100273}
274
275static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800276handle_surface_configure(void *data, struct zxdg_surface_v6 *surface,
277 uint32_t serial)
278{
279 struct window *window = data;
280
281 zxdg_surface_v6_ack_configure(surface, serial);
282
283 window->wait_for_configure = false;
284}
285
286static const struct zxdg_surface_v6_listener xdg_surface_listener = {
287 handle_surface_configure
288};
289
290static void
291handle_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
292 int32_t width, int32_t height,
293 struct wl_array *states)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300294{
295 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700296 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300297
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700298 window->fullscreen = 0;
299 wl_array_for_each(p, states) {
300 uint32_t state = *p;
301 switch (state) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800302 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700303 window->fullscreen = 1;
304 break;
305 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500306 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800307
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700308 if (width > 0 && height > 0) {
309 if (!window->fullscreen) {
310 window->window_size.width = width;
311 window->window_size.height = height;
312 }
313 window->geometry.width = width;
314 window->geometry.height = height;
315 } else if (!window->fullscreen) {
316 window->geometry = window->window_size;
317 }
318
319 if (window->native)
320 wl_egl_window_resize(window->native,
321 window->geometry.width,
322 window->geometry.height, 0, 0);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800323}
324
325static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800326handle_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500327{
328 running = 0;
329}
330
Jonas Ådahl83630022016-08-11 23:13:20 +0800331static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
332 handle_toplevel_configure,
333 handle_toplevel_close,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300334};
335
336static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900337handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
338 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100339{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900340 struct window *window = data;
341
342 wl_egl_window_resize(window->native, width, height, 0, 0);
343
344 window->geometry.width = width;
345 window->geometry.height = height;
346
347 if (!window->fullscreen)
348 window->window_size = window->geometry;
349}
350
351static const struct ivi_surface_listener ivi_surface_listener = {
352 handle_ivi_surface_configure,
353};
354
355static void
356create_xdg_surface(struct window *window, struct display *display)
357{
Jonas Ådahl83630022016-08-11 23:13:20 +0800358 window->xdg_surface = zxdg_shell_v6_get_xdg_surface(display->shell,
359 window->surface);
360 zxdg_surface_v6_add_listener(window->xdg_surface,
361 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300362
Jonas Ådahl83630022016-08-11 23:13:20 +0800363 window->xdg_toplevel =
364 zxdg_surface_v6_get_toplevel(window->xdg_surface);
365 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
366 &xdg_toplevel_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300367
Jonas Ådahl83630022016-08-11 23:13:20 +0800368 zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-egl");
369
370 window->wait_for_configure = true;
371 wl_surface_commit(window->surface);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900372}
373
374static void
375create_ivi_surface(struct window *window, struct display *display)
376{
377 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
378 window->ivi_surface =
379 ivi_application_surface_create(display->ivi_application,
380 id_ivisurf, window->surface);
381
382 if (window->ivi_surface == NULL) {
383 fprintf(stderr, "Failed to create ivi_client_surface\n");
384 abort();
385 }
386
387 ivi_surface_add_listener(window->ivi_surface,
388 &ivi_surface_listener, window);
389}
390
391static void
392create_surface(struct window *window)
393{
394 struct display *display = window->display;
395 EGLBoolean ret;
396
397 window->surface = wl_compositor_create_surface(display->compositor);
398
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500399 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400400 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700401 window->geometry.width,
402 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500403 window->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100404 weston_platform_create_egl_surface(display->egl.dpy,
405 display->egl.conf,
406 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100407
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100408
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900409 if (display->shell) {
410 create_xdg_surface(window, display);
411 } else if (display->ivi_application ) {
412 create_ivi_surface(window, display);
413 } else {
414 assert(0);
415 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600416
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500417 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
418 window->egl_surface, window->display->egl.ctx);
419 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300420
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800421 if (!window->frame_sync)
422 eglSwapInterval(display->egl.dpy, 0);
423
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900424 if (!display->shell)
425 return;
426
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700427 if (window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800428 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100429}
430
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200431static void
432destroy_surface(struct window *window)
433{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000434 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
435 * on eglReleaseThread(). */
436 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
437 EGL_NO_CONTEXT);
438
439 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200440 wl_egl_window_destroy(window->native);
441
Jonas Ådahl83630022016-08-11 23:13:20 +0800442 if (window->xdg_toplevel)
443 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900444 if (window->xdg_surface)
Jonas Ådahl83630022016-08-11 23:13:20 +0800445 zxdg_surface_v6_destroy(window->xdg_surface);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900446 if (window->display->ivi_application)
447 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200448 wl_surface_destroy(window->surface);
449
450 if (window->callback)
451 wl_callback_destroy(window->callback);
452}
453
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100454static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400455redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100456{
457 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400458 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100459 static const GLfloat verts[3][2] = {
460 { -0.5, -0.5 },
461 { 0.5, -0.5 },
462 { 0, 0.5 }
463 };
464 static const GLfloat colors[3][3] = {
465 { 1, 0, 0 },
466 { 0, 1, 0 },
467 { 0, 0, 1 }
468 };
469 GLfloat angle;
470 GLfloat rotation[4][4] = {
471 { 1, 0, 0, 0 },
472 { 0, 1, 0, 0 },
473 { 0, 0, 1, 0 },
474 { 0, 0, 0, 1 }
475 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100476 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400477 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400478 EGLint rect[4];
479 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800480 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100481
Scott Moreau7e300db2012-08-31 03:18:15 -0600482 assert(window->callback == callback);
483 window->callback = NULL;
484
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300485 if (callback)
486 wl_callback_destroy(callback);
487
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800488 gettimeofday(&tv, NULL);
489 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
490 if (window->frames == 0)
491 window->benchmark_time = time;
492 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
493 printf("%d frames in %d seconds: %f fps\n",
494 window->frames,
495 benchmark_interval,
496 (float) window->frames / benchmark_interval);
497 window->benchmark_time = time;
498 window->frames = 0;
499 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100500
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800501 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100502 rotation[0][0] = cos(angle);
503 rotation[0][2] = sin(angle);
504 rotation[2][0] = -sin(angle);
505 rotation[2][2] = cos(angle);
506
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400507 if (display->swap_buffers_with_damage)
508 eglQuerySurface(display->egl.dpy, window->egl_surface,
509 EGL_BUFFER_AGE_EXT, &buffer_age);
510
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300511 glViewport(0, 0, window->geometry.width, window->geometry.height);
512
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100513 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
514 (GLfloat *) rotation);
515
516 glClearColor(0.0, 0.0, 0.0, 0.5);
517 glClear(GL_COLOR_BUFFER_BIT);
518
519 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
520 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
521 glEnableVertexAttribArray(window->gl.pos);
522 glEnableVertexAttribArray(window->gl.col);
523
524 glDrawArrays(GL_TRIANGLES, 0, 3);
525
526 glDisableVertexAttribArray(window->gl.pos);
527 glDisableVertexAttribArray(window->gl.col);
528
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300529 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400530 region = wl_compositor_create_region(window->display->compositor);
531 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300532 window->geometry.width,
533 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400534 wl_surface_set_opaque_region(window->surface, region);
535 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700536 } else {
537 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400538 }
539
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400540 if (display->swap_buffers_with_damage && buffer_age > 0) {
541 rect[0] = window->geometry.width / 4 - 1;
542 rect[1] = window->geometry.height / 4 - 1;
543 rect[2] = window->geometry.width / 2 + 2;
544 rect[3] = window->geometry.height / 2 + 2;
545 display->swap_buffers_with_damage(display->egl.dpy,
546 window->egl_surface,
547 rect, 1);
548 } else {
549 eglSwapBuffers(display->egl.dpy, window->egl_surface);
550 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800551 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100552}
553
554static void
Daniel Stone37816df2012-05-16 18:45:18 +0100555pointer_handle_enter(void *data, struct wl_pointer *pointer,
556 uint32_t serial, struct wl_surface *surface,
557 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300558{
559 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400560 struct wl_buffer *buffer;
561 struct wl_cursor *cursor = display->default_cursor;
562 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300563
564 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300565 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400566 else if (cursor) {
567 image = display->default_cursor->images[0];
568 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100569 if (!buffer)
570 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400571 wl_pointer_set_cursor(pointer, serial,
572 display->cursor_surface,
573 image->hotspot_x,
574 image->hotspot_y);
575 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
576 wl_surface_damage(display->cursor_surface, 0, 0,
577 image->width, image->height);
578 wl_surface_commit(display->cursor_surface);
579 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300580}
581
582static void
Daniel Stone37816df2012-05-16 18:45:18 +0100583pointer_handle_leave(void *data, struct wl_pointer *pointer,
584 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300585{
586}
587
588static void
Daniel Stone37816df2012-05-16 18:45:18 +0100589pointer_handle_motion(void *data, struct wl_pointer *pointer,
590 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300591{
592}
593
594static void
Daniel Stone37816df2012-05-16 18:45:18 +0100595pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
596 uint32_t serial, uint32_t time, uint32_t button,
597 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300598{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300599 struct display *display = data;
600
Jonas Ådahl83630022016-08-11 23:13:20 +0800601 if (!display->window->xdg_toplevel)
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900602 return;
603
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300604 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Jonas Ådahl83630022016-08-11 23:13:20 +0800605 zxdg_toplevel_v6_move(display->window->xdg_toplevel,
606 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300607}
608
609static void
Daniel Stone37816df2012-05-16 18:45:18 +0100610pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100611 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300612{
613}
614
Daniel Stone37816df2012-05-16 18:45:18 +0100615static const struct wl_pointer_listener pointer_listener = {
616 pointer_handle_enter,
617 pointer_handle_leave,
618 pointer_handle_motion,
619 pointer_handle_button,
620 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300621};
622
623static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700624touch_handle_down(void *data, struct wl_touch *wl_touch,
625 uint32_t serial, uint32_t time, struct wl_surface *surface,
626 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
627{
628 struct display *d = (struct display *)data;
629
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900630 if (!d->shell)
631 return;
632
Jonas Ådahl83630022016-08-11 23:13:20 +0800633 zxdg_toplevel_v6_move(d->window->xdg_toplevel, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700634}
635
636static void
637touch_handle_up(void *data, struct wl_touch *wl_touch,
638 uint32_t serial, uint32_t time, int32_t id)
639{
640}
641
642static void
643touch_handle_motion(void *data, struct wl_touch *wl_touch,
644 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
645{
646}
647
648static void
649touch_handle_frame(void *data, struct wl_touch *wl_touch)
650{
651}
652
653static void
654touch_handle_cancel(void *data, struct wl_touch *wl_touch)
655{
656}
657
658static const struct wl_touch_listener touch_listener = {
659 touch_handle_down,
660 touch_handle_up,
661 touch_handle_motion,
662 touch_handle_frame,
663 touch_handle_cancel,
664};
665
666static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300667keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
668 uint32_t format, int fd, uint32_t size)
669{
670}
671
672static void
673keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
674 uint32_t serial, struct wl_surface *surface,
675 struct wl_array *keys)
676{
677}
678
679static void
680keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
681 uint32_t serial, struct wl_surface *surface)
682{
683}
684
685static void
686keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
687 uint32_t serial, uint32_t time, uint32_t key,
688 uint32_t state)
689{
690 struct display *d = data;
691
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900692 if (!d->shell)
693 return;
694
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700695 if (key == KEY_F11 && state) {
696 if (d->window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800697 zxdg_toplevel_v6_unset_fullscreen(d->window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700698 else
Jonas Ådahl83630022016-08-11 23:13:20 +0800699 zxdg_toplevel_v6_set_fullscreen(d->window->xdg_toplevel,
700 NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700701 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400702 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300703}
704
705static void
706keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
707 uint32_t serial, uint32_t mods_depressed,
708 uint32_t mods_latched, uint32_t mods_locked,
709 uint32_t group)
710{
711}
712
713static const struct wl_keyboard_listener keyboard_listener = {
714 keyboard_handle_keymap,
715 keyboard_handle_enter,
716 keyboard_handle_leave,
717 keyboard_handle_key,
718 keyboard_handle_modifiers,
719};
720
721static void
Daniel Stone37816df2012-05-16 18:45:18 +0100722seat_handle_capabilities(void *data, struct wl_seat *seat,
723 enum wl_seat_capability caps)
724{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400725 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100726
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400727 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
728 d->pointer = wl_seat_get_pointer(seat);
729 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
730 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
731 wl_pointer_destroy(d->pointer);
732 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100733 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300734
735 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
736 d->keyboard = wl_seat_get_keyboard(seat);
737 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
738 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
739 wl_keyboard_destroy(d->keyboard);
740 d->keyboard = NULL;
741 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700742
743 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
744 d->touch = wl_seat_get_touch(seat);
745 wl_touch_set_user_data(d->touch, d);
746 wl_touch_add_listener(d->touch, &touch_listener, d);
747 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
748 wl_touch_destroy(d->touch);
749 d->touch = NULL;
750 }
Daniel Stone37816df2012-05-16 18:45:18 +0100751}
752
753static const struct wl_seat_listener seat_listener = {
754 seat_handle_capabilities,
755};
756
757static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800758xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800759{
Jonas Ådahl83630022016-08-11 23:13:20 +0800760 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800761}
762
Jonas Ådahl83630022016-08-11 23:13:20 +0800763static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800764 xdg_shell_ping,
765};
766
767static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400768registry_handle_global(void *data, struct wl_registry *registry,
769 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100770{
771 struct display *d = data;
772
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400773 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400774 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400775 wl_registry_bind(registry, name,
776 &wl_compositor_interface, 1);
Jonas Ådahl83630022016-08-11 23:13:20 +0800777 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400778 d->shell = wl_registry_bind(registry, name,
Jonas Ådahl83630022016-08-11 23:13:20 +0800779 &zxdg_shell_v6_interface, 1);
780 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Daniel Stone37816df2012-05-16 18:45:18 +0100781 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400782 d->seat = wl_registry_bind(registry, name,
783 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400784 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400785 } else if (strcmp(interface, "wl_shm") == 0) {
786 d->shm = wl_registry_bind(registry, name,
787 &wl_shm_interface, 1);
788 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100789 if (!d->cursor_theme) {
790 fprintf(stderr, "unable to load default theme\n");
791 return;
792 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400793 d->default_cursor =
794 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100795 if (!d->default_cursor) {
796 fprintf(stderr, "unable to load default left pointer\n");
797 // TODO: abort ?
798 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900799 } else if (strcmp(interface, "ivi_application") == 0) {
800 d->ivi_application =
801 wl_registry_bind(registry, name,
802 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400803 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100804}
805
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200806static void
807registry_handle_global_remove(void *data, struct wl_registry *registry,
808 uint32_t name)
809{
810}
811
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400812static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200813 registry_handle_global,
814 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400815};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100816
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200817static void
818signal_int(int signum)
819{
820 running = 0;
821}
822
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400823static void
824usage(int error_code)
825{
826 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
827 " -f\tRun in fullscreen mode\n"
828 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700829 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800830 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400831 " -h\tThis help text\n\n");
832
833 exit(error_code);
834}
835
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100836int
837main(int argc, char **argv)
838{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200839 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100840 struct display display = { 0 };
841 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400842 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100843
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100844 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300845 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700846 window.geometry.width = 250;
847 window.geometry.height = 250;
848 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700849 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800850 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100851
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400852 for (i = 1; i < argc; i++) {
853 if (strcmp("-f", argv[i]) == 0)
854 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400855 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300856 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700857 else if (strcmp("-s", argv[i]) == 0)
858 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800859 else if (strcmp("-b", argv[i]) == 0)
860 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400861 else if (strcmp("-h", argv[i]) == 0)
862 usage(EXIT_SUCCESS);
863 else
864 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400865 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300866
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100867 display.display = wl_display_connect(NULL);
868 assert(display.display);
869
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400870 display.registry = wl_display_get_registry(display.display);
871 wl_registry_add_listener(display.registry,
872 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100873
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100874 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200875
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700876 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100877 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500878 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100879
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400880 display.cursor_surface =
881 wl_compositor_create_surface(display.compositor);
882
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200883 sigint.sa_handler = signal_int;
884 sigemptyset(&sigint.sa_mask);
885 sigint.sa_flags = SA_RESETHAND;
886 sigaction(SIGINT, &sigint, NULL);
887
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800888 /* The mainloop here is a little subtle. Redrawing will cause
889 * EGL to read events so we can just call
890 * wl_display_dispatch_pending() to handle any events that got
891 * queued up as a side effect. */
892 while (running && ret != -1) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800893 if (window.wait_for_configure) {
894 wl_display_dispatch(display.display);
895 } else {
896 wl_display_dispatch_pending(display.display);
897 redraw(&window, NULL, 0);
898 }
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800899 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500900
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200901 fprintf(stderr, "simple-egl exiting\n");
902
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200903 destroy_surface(&window);
904 fini_egl(&display);
905
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400906 wl_surface_destroy(display.cursor_surface);
907 if (display.cursor_theme)
908 wl_cursor_theme_destroy(display.cursor_theme);
909
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200910 if (display.shell)
Jonas Ådahl83630022016-08-11 23:13:20 +0800911 zxdg_shell_v6_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200912
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900913 if (display.ivi_application)
914 ivi_application_destroy(display.ivi_application);
915
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200916 if (display.compositor)
917 wl_compositor_destroy(display.compositor);
918
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200919 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200920 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500921 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200922
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100923 return 0;
924}