blob: c23987aaff31420b9e26e2eedf6555c77f169c84 [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
Emil Velikovc77d30c2016-11-14 17:27:14 +000051#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070052#include "shared/platform.h"
Emil Velikov0725cf12016-07-04 15:27:15 +010053#include "weston-egl-ext.h"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010054
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030055struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010056struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030057
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010058struct display {
59 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040060 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050061 struct wl_compositor *compositor;
Jonas Ådahl83630022016-08-11 23:13:20 +080062 struct zxdg_shell_v6 *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040063 struct wl_seat *seat;
64 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070065 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030066 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040067 struct wl_shm *shm;
68 struct wl_cursor_theme *cursor_theme;
69 struct wl_cursor *default_cursor;
70 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010071 struct {
72 EGLDisplay dpy;
73 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050074 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010075 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030076 struct window *window;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090077 struct ivi_application *ivi_application;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040078
79 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010080};
81
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030082struct geometry {
83 int width, height;
84};
85
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086struct window {
87 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060088 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010089 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010090 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010091 GLuint pos;
92 GLuint col;
93 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050094
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080095 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050096 struct wl_egl_window *native;
97 struct wl_surface *surface;
Jonas Ådahl83630022016-08-11 23:13:20 +080098 struct zxdg_surface_v6 *xdg_surface;
99 struct zxdg_toplevel_v6 *xdg_toplevel;
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900100 struct ivi_surface *ivi_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500101 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200102 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500103 int fullscreen, opaque, buffer_size, frame_sync;
Jonas Ådahl83630022016-08-11 23:13:20 +0800104 bool wait_for_configure;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100105};
106
107static const char *vert_shader_text =
108 "uniform mat4 rotation;\n"
109 "attribute vec4 pos;\n"
110 "attribute vec4 color;\n"
111 "varying vec4 v_color;\n"
112 "void main() {\n"
113 " gl_Position = rotation * pos;\n"
114 " v_color = color;\n"
115 "}\n";
116
117static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500118 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100119 "varying vec4 v_color;\n"
120 "void main() {\n"
121 " gl_FragColor = v_color;\n"
122 "}\n";
123
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400124static int running = 1;
125
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100126static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700127init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100128{
Emil Velikovc77d30c2016-11-14 17:27:14 +0000129 static const struct {
130 char *extension, *entrypoint;
131 } swap_damage_ext_to_entrypoint[] = {
132 {
133 .extension = "EGL_EXT_swap_buffers_with_damage",
134 .entrypoint = "eglSwapBuffersWithDamageEXT",
135 },
136 {
137 .extension = "EGL_KHR_swap_buffers_with_damage",
138 .entrypoint = "eglSwapBuffersWithDamageKHR",
139 },
140 };
141
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500142 static const EGLint context_attribs[] = {
143 EGL_CONTEXT_CLIENT_VERSION, 2,
144 EGL_NONE
145 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400146 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500147
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300148 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500149 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500150 EGL_RED_SIZE, 1,
151 EGL_GREEN_SIZE, 1,
152 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200153 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500154 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
155 EGL_NONE
156 };
157
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700158 EGLint major, minor, n, count, i, size;
159 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100160 EGLBoolean ret;
161
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700162 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400163 config_attribs[9] = 0;
164
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100165 display->egl.dpy =
166 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
167 display->display, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100168 assert(display->egl.dpy);
169
170 ret = eglInitialize(display->egl.dpy, &major, &minor);
171 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500172 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100173 assert(ret == EGL_TRUE);
174
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700175 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
176 assert(0);
177
178 configs = calloc(count, sizeof *configs);
179 assert(configs);
180
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300181 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700182 configs, count, &n);
183 assert(ret && n >= 1);
184
185 for (i = 0; i < n; i++) {
186 eglGetConfigAttrib(display->egl.dpy,
187 configs[i], EGL_BUFFER_SIZE, &size);
188 if (window->buffer_size == size) {
189 display->egl.conf = configs[i];
190 break;
191 }
192 }
193 free(configs);
194 if (display->egl.conf == NULL) {
195 fprintf(stderr, "did not find config with buffer size %d\n",
196 window->buffer_size);
197 exit(EXIT_FAILURE);
198 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500199
200 display->egl.ctx = eglCreateContext(display->egl.dpy,
201 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500202 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100203 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500204
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400205 display->swap_buffers_with_damage = NULL;
206 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
207 if (extensions &&
Emil Velikovc77d30c2016-11-14 17:27:14 +0000208 weston_check_egl_extension(extensions, "EGL_EXT_buffer_age")) {
209 for (i = 0; i < (int) ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) {
210 if (weston_check_egl_extension(extensions,
211 swap_damage_ext_to_entrypoint[i].extension)) {
212 /* The EXTPROC is identical to the KHR one */
213 display->swap_buffers_with_damage =
214 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
215 eglGetProcAddress(swap_damage_ext_to_entrypoint[i].entrypoint);
216 break;
217 }
218 }
219 }
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400220
221 if (display->swap_buffers_with_damage)
Emil Velikovc77d30c2016-11-14 17:27:14 +0000222 printf("has EGL_EXT_buffer_age and %s\n", swap_damage_ext_to_entrypoint[i].extension);
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400223
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100224}
225
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200226static void
227fini_egl(struct display *display)
228{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200229 eglTerminate(display->egl.dpy);
230 eglReleaseThread();
231}
232
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100233static GLuint
234create_shader(struct window *window, const char *source, GLenum shader_type)
235{
236 GLuint shader;
237 GLint status;
238
239 shader = glCreateShader(shader_type);
240 assert(shader != 0);
241
242 glShaderSource(shader, 1, (const char **) &source, NULL);
243 glCompileShader(shader);
244
245 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
246 if (!status) {
247 char log[1000];
248 GLsizei len;
249 glGetShaderInfoLog(shader, 1000, &len, log);
250 fprintf(stderr, "Error: compiling %s: %*s\n",
251 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
252 len, log);
253 exit(1);
254 }
255
256 return shader;
257}
258
259static void
260init_gl(struct window *window)
261{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100262 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600263 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100264 GLint status;
265
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100266 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
267 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
268
Scott Moreau3ea23d02012-06-13 17:42:21 -0600269 program = glCreateProgram();
270 glAttachShader(program, frag);
271 glAttachShader(program, vert);
272 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100273
Scott Moreau3ea23d02012-06-13 17:42:21 -0600274 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100275 if (!status) {
276 char log[1000];
277 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600278 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100279 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
280 exit(1);
281 }
282
Scott Moreau3ea23d02012-06-13 17:42:21 -0600283 glUseProgram(program);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900284
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100285 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600286 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100287
Scott Moreau3ea23d02012-06-13 17:42:21 -0600288 glBindAttribLocation(program, window->gl.pos, "pos");
289 glBindAttribLocation(program, window->gl.col, "color");
290 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100291
292 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600293 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100294}
295
296static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800297handle_surface_configure(void *data, struct zxdg_surface_v6 *surface,
298 uint32_t serial)
299{
300 struct window *window = data;
301
302 zxdg_surface_v6_ack_configure(surface, serial);
303
304 window->wait_for_configure = false;
305}
306
307static const struct zxdg_surface_v6_listener xdg_surface_listener = {
308 handle_surface_configure
309};
310
311static void
312handle_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
313 int32_t width, int32_t height,
314 struct wl_array *states)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300315{
316 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700317 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300318
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700319 window->fullscreen = 0;
320 wl_array_for_each(p, states) {
321 uint32_t state = *p;
322 switch (state) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800323 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700324 window->fullscreen = 1;
325 break;
326 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500327 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800328
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700329 if (width > 0 && height > 0) {
330 if (!window->fullscreen) {
331 window->window_size.width = width;
332 window->window_size.height = height;
333 }
334 window->geometry.width = width;
335 window->geometry.height = height;
336 } else if (!window->fullscreen) {
337 window->geometry = window->window_size;
338 }
339
340 if (window->native)
341 wl_egl_window_resize(window->native,
342 window->geometry.width,
343 window->geometry.height, 0, 0);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800344}
345
346static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800347handle_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500348{
349 running = 0;
350}
351
Jonas Ådahl83630022016-08-11 23:13:20 +0800352static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
353 handle_toplevel_configure,
354 handle_toplevel_close,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300355};
356
357static void
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900358handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
359 int32_t width, int32_t height)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100360{
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900361 struct window *window = data;
362
363 wl_egl_window_resize(window->native, width, height, 0, 0);
364
365 window->geometry.width = width;
366 window->geometry.height = height;
367
368 if (!window->fullscreen)
369 window->window_size = window->geometry;
370}
371
372static const struct ivi_surface_listener ivi_surface_listener = {
373 handle_ivi_surface_configure,
374};
375
376static void
377create_xdg_surface(struct window *window, struct display *display)
378{
Jonas Ådahl83630022016-08-11 23:13:20 +0800379 window->xdg_surface = zxdg_shell_v6_get_xdg_surface(display->shell,
380 window->surface);
381 zxdg_surface_v6_add_listener(window->xdg_surface,
382 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300383
Jonas Ådahl83630022016-08-11 23:13:20 +0800384 window->xdg_toplevel =
385 zxdg_surface_v6_get_toplevel(window->xdg_surface);
386 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
387 &xdg_toplevel_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300388
Jonas Ådahl83630022016-08-11 23:13:20 +0800389 zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-egl");
390
391 window->wait_for_configure = true;
392 wl_surface_commit(window->surface);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900393}
394
395static void
396create_ivi_surface(struct window *window, struct display *display)
397{
398 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
399 window->ivi_surface =
400 ivi_application_surface_create(display->ivi_application,
401 id_ivisurf, window->surface);
402
403 if (window->ivi_surface == NULL) {
404 fprintf(stderr, "Failed to create ivi_client_surface\n");
405 abort();
406 }
407
408 ivi_surface_add_listener(window->ivi_surface,
409 &ivi_surface_listener, window);
410}
411
412static void
413create_surface(struct window *window)
414{
415 struct display *display = window->display;
416 EGLBoolean ret;
417
418 window->surface = wl_compositor_create_surface(display->compositor);
419
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500420 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400421 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700422 window->geometry.width,
423 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500424 window->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100425 weston_platform_create_egl_surface(display->egl.dpy,
426 display->egl.conf,
427 window->native, NULL);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100428
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100429
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900430 if (display->shell) {
431 create_xdg_surface(window, display);
432 } else if (display->ivi_application ) {
433 create_ivi_surface(window, display);
434 } else {
435 assert(0);
436 }
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600437
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500438 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
439 window->egl_surface, window->display->egl.ctx);
440 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300441
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800442 if (!window->frame_sync)
443 eglSwapInterval(display->egl.dpy, 0);
444
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900445 if (!display->shell)
446 return;
447
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700448 if (window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800449 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100450}
451
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200452static void
453destroy_surface(struct window *window)
454{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000455 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
456 * on eglReleaseThread(). */
457 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
458 EGL_NO_CONTEXT);
459
460 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200461 wl_egl_window_destroy(window->native);
462
Jonas Ådahl83630022016-08-11 23:13:20 +0800463 if (window->xdg_toplevel)
464 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900465 if (window->xdg_surface)
Jonas Ådahl83630022016-08-11 23:13:20 +0800466 zxdg_surface_v6_destroy(window->xdg_surface);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900467 if (window->display->ivi_application)
468 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200469 wl_surface_destroy(window->surface);
470
471 if (window->callback)
472 wl_callback_destroy(window->callback);
473}
474
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100475static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400476redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100477{
478 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400479 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100480 static const GLfloat verts[3][2] = {
481 { -0.5, -0.5 },
482 { 0.5, -0.5 },
483 { 0, 0.5 }
484 };
485 static const GLfloat colors[3][3] = {
486 { 1, 0, 0 },
487 { 0, 1, 0 },
488 { 0, 0, 1 }
489 };
490 GLfloat angle;
491 GLfloat rotation[4][4] = {
492 { 1, 0, 0, 0 },
493 { 0, 1, 0, 0 },
494 { 0, 0, 1, 0 },
495 { 0, 0, 0, 1 }
496 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100497 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400498 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400499 EGLint rect[4];
500 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800501 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100502
Scott Moreau7e300db2012-08-31 03:18:15 -0600503 assert(window->callback == callback);
504 window->callback = NULL;
505
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300506 if (callback)
507 wl_callback_destroy(callback);
508
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800509 gettimeofday(&tv, NULL);
510 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
511 if (window->frames == 0)
512 window->benchmark_time = time;
513 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
514 printf("%d frames in %d seconds: %f fps\n",
515 window->frames,
516 benchmark_interval,
517 (float) window->frames / benchmark_interval);
518 window->benchmark_time = time;
519 window->frames = 0;
520 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100521
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800522 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100523 rotation[0][0] = cos(angle);
524 rotation[0][2] = sin(angle);
525 rotation[2][0] = -sin(angle);
526 rotation[2][2] = cos(angle);
527
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400528 if (display->swap_buffers_with_damage)
529 eglQuerySurface(display->egl.dpy, window->egl_surface,
530 EGL_BUFFER_AGE_EXT, &buffer_age);
531
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300532 glViewport(0, 0, window->geometry.width, window->geometry.height);
533
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100534 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
535 (GLfloat *) rotation);
536
537 glClearColor(0.0, 0.0, 0.0, 0.5);
538 glClear(GL_COLOR_BUFFER_BIT);
539
540 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
541 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
542 glEnableVertexAttribArray(window->gl.pos);
543 glEnableVertexAttribArray(window->gl.col);
544
545 glDrawArrays(GL_TRIANGLES, 0, 3);
546
547 glDisableVertexAttribArray(window->gl.pos);
548 glDisableVertexAttribArray(window->gl.col);
549
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300550 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400551 region = wl_compositor_create_region(window->display->compositor);
552 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300553 window->geometry.width,
554 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400555 wl_surface_set_opaque_region(window->surface, region);
556 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700557 } else {
558 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400559 }
560
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400561 if (display->swap_buffers_with_damage && buffer_age > 0) {
562 rect[0] = window->geometry.width / 4 - 1;
563 rect[1] = window->geometry.height / 4 - 1;
564 rect[2] = window->geometry.width / 2 + 2;
565 rect[3] = window->geometry.height / 2 + 2;
566 display->swap_buffers_with_damage(display->egl.dpy,
567 window->egl_surface,
568 rect, 1);
569 } else {
570 eglSwapBuffers(display->egl.dpy, window->egl_surface);
571 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800572 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100573}
574
575static void
Daniel Stone37816df2012-05-16 18:45:18 +0100576pointer_handle_enter(void *data, struct wl_pointer *pointer,
577 uint32_t serial, struct wl_surface *surface,
578 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300579{
580 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400581 struct wl_buffer *buffer;
582 struct wl_cursor *cursor = display->default_cursor;
583 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300584
585 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300586 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400587 else if (cursor) {
588 image = display->default_cursor->images[0];
589 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100590 if (!buffer)
591 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400592 wl_pointer_set_cursor(pointer, serial,
593 display->cursor_surface,
594 image->hotspot_x,
595 image->hotspot_y);
596 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
597 wl_surface_damage(display->cursor_surface, 0, 0,
598 image->width, image->height);
599 wl_surface_commit(display->cursor_surface);
600 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300601}
602
603static void
Daniel Stone37816df2012-05-16 18:45:18 +0100604pointer_handle_leave(void *data, struct wl_pointer *pointer,
605 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300606{
607}
608
609static void
Daniel Stone37816df2012-05-16 18:45:18 +0100610pointer_handle_motion(void *data, struct wl_pointer *pointer,
611 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300612{
613}
614
615static void
Daniel Stone37816df2012-05-16 18:45:18 +0100616pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
617 uint32_t serial, uint32_t time, uint32_t button,
618 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300619{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300620 struct display *display = data;
621
Jonas Ådahl83630022016-08-11 23:13:20 +0800622 if (!display->window->xdg_toplevel)
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900623 return;
624
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300625 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Jonas Ådahl83630022016-08-11 23:13:20 +0800626 zxdg_toplevel_v6_move(display->window->xdg_toplevel,
627 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300628}
629
630static void
Daniel Stone37816df2012-05-16 18:45:18 +0100631pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100632 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300633{
634}
635
Daniel Stone37816df2012-05-16 18:45:18 +0100636static const struct wl_pointer_listener pointer_listener = {
637 pointer_handle_enter,
638 pointer_handle_leave,
639 pointer_handle_motion,
640 pointer_handle_button,
641 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300642};
643
644static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700645touch_handle_down(void *data, struct wl_touch *wl_touch,
646 uint32_t serial, uint32_t time, struct wl_surface *surface,
647 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
648{
649 struct display *d = (struct display *)data;
650
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900651 if (!d->shell)
652 return;
653
Jonas Ådahl83630022016-08-11 23:13:20 +0800654 zxdg_toplevel_v6_move(d->window->xdg_toplevel, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700655}
656
657static void
658touch_handle_up(void *data, struct wl_touch *wl_touch,
659 uint32_t serial, uint32_t time, int32_t id)
660{
661}
662
663static void
664touch_handle_motion(void *data, struct wl_touch *wl_touch,
665 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
666{
667}
668
669static void
670touch_handle_frame(void *data, struct wl_touch *wl_touch)
671{
672}
673
674static void
675touch_handle_cancel(void *data, struct wl_touch *wl_touch)
676{
677}
678
679static const struct wl_touch_listener touch_listener = {
680 touch_handle_down,
681 touch_handle_up,
682 touch_handle_motion,
683 touch_handle_frame,
684 touch_handle_cancel,
685};
686
687static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300688keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
689 uint32_t format, int fd, uint32_t size)
690{
691}
692
693static void
694keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
695 uint32_t serial, struct wl_surface *surface,
696 struct wl_array *keys)
697{
698}
699
700static void
701keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
702 uint32_t serial, struct wl_surface *surface)
703{
704}
705
706static void
707keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
708 uint32_t serial, uint32_t time, uint32_t key,
709 uint32_t state)
710{
711 struct display *d = data;
712
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900713 if (!d->shell)
714 return;
715
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700716 if (key == KEY_F11 && state) {
717 if (d->window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800718 zxdg_toplevel_v6_unset_fullscreen(d->window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700719 else
Jonas Ådahl83630022016-08-11 23:13:20 +0800720 zxdg_toplevel_v6_set_fullscreen(d->window->xdg_toplevel,
721 NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700722 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400723 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300724}
725
726static void
727keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
728 uint32_t serial, uint32_t mods_depressed,
729 uint32_t mods_latched, uint32_t mods_locked,
730 uint32_t group)
731{
732}
733
734static const struct wl_keyboard_listener keyboard_listener = {
735 keyboard_handle_keymap,
736 keyboard_handle_enter,
737 keyboard_handle_leave,
738 keyboard_handle_key,
739 keyboard_handle_modifiers,
740};
741
742static void
Daniel Stone37816df2012-05-16 18:45:18 +0100743seat_handle_capabilities(void *data, struct wl_seat *seat,
744 enum wl_seat_capability caps)
745{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400746 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100747
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400748 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
749 d->pointer = wl_seat_get_pointer(seat);
750 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
751 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
752 wl_pointer_destroy(d->pointer);
753 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100754 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300755
756 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
757 d->keyboard = wl_seat_get_keyboard(seat);
758 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
759 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
760 wl_keyboard_destroy(d->keyboard);
761 d->keyboard = NULL;
762 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700763
764 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
765 d->touch = wl_seat_get_touch(seat);
766 wl_touch_set_user_data(d->touch, d);
767 wl_touch_add_listener(d->touch, &touch_listener, d);
768 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
769 wl_touch_destroy(d->touch);
770 d->touch = NULL;
771 }
Daniel Stone37816df2012-05-16 18:45:18 +0100772}
773
774static const struct wl_seat_listener seat_listener = {
775 seat_handle_capabilities,
776};
777
778static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800779xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800780{
Jonas Ådahl83630022016-08-11 23:13:20 +0800781 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800782}
783
Jonas Ådahl83630022016-08-11 23:13:20 +0800784static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800785 xdg_shell_ping,
786};
787
788static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400789registry_handle_global(void *data, struct wl_registry *registry,
790 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100791{
792 struct display *d = data;
793
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400794 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400795 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400796 wl_registry_bind(registry, name,
797 &wl_compositor_interface, 1);
Jonas Ådahl83630022016-08-11 23:13:20 +0800798 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400799 d->shell = wl_registry_bind(registry, name,
Jonas Ådahl83630022016-08-11 23:13:20 +0800800 &zxdg_shell_v6_interface, 1);
801 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Daniel Stone37816df2012-05-16 18:45:18 +0100802 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400803 d->seat = wl_registry_bind(registry, name,
804 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400805 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400806 } else if (strcmp(interface, "wl_shm") == 0) {
807 d->shm = wl_registry_bind(registry, name,
808 &wl_shm_interface, 1);
809 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100810 if (!d->cursor_theme) {
811 fprintf(stderr, "unable to load default theme\n");
812 return;
813 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400814 d->default_cursor =
815 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100816 if (!d->default_cursor) {
817 fprintf(stderr, "unable to load default left pointer\n");
818 // TODO: abort ?
819 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900820 } else if (strcmp(interface, "ivi_application") == 0) {
821 d->ivi_application =
822 wl_registry_bind(registry, name,
823 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400824 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100825}
826
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200827static void
828registry_handle_global_remove(void *data, struct wl_registry *registry,
829 uint32_t name)
830{
831}
832
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400833static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200834 registry_handle_global,
835 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400836};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100837
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200838static void
839signal_int(int signum)
840{
841 running = 0;
842}
843
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400844static void
845usage(int error_code)
846{
847 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
848 " -f\tRun in fullscreen mode\n"
849 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700850 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800851 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400852 " -h\tThis help text\n\n");
853
854 exit(error_code);
855}
856
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100857int
858main(int argc, char **argv)
859{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200860 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100861 struct display display = { 0 };
862 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400863 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100864
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100865 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300866 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700867 window.geometry.width = 250;
868 window.geometry.height = 250;
869 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700870 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800871 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100872
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400873 for (i = 1; i < argc; i++) {
874 if (strcmp("-f", argv[i]) == 0)
875 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400876 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300877 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700878 else if (strcmp("-s", argv[i]) == 0)
879 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800880 else if (strcmp("-b", argv[i]) == 0)
881 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400882 else if (strcmp("-h", argv[i]) == 0)
883 usage(EXIT_SUCCESS);
884 else
885 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400886 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300887
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100888 display.display = wl_display_connect(NULL);
889 assert(display.display);
890
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400891 display.registry = wl_display_get_registry(display.display);
892 wl_registry_add_listener(display.registry,
893 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100894
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100895 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200896
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700897 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100898 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500899 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100900
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400901 display.cursor_surface =
902 wl_compositor_create_surface(display.compositor);
903
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200904 sigint.sa_handler = signal_int;
905 sigemptyset(&sigint.sa_mask);
906 sigint.sa_flags = SA_RESETHAND;
907 sigaction(SIGINT, &sigint, NULL);
908
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800909 /* The mainloop here is a little subtle. Redrawing will cause
910 * EGL to read events so we can just call
911 * wl_display_dispatch_pending() to handle any events that got
912 * queued up as a side effect. */
913 while (running && ret != -1) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800914 if (window.wait_for_configure) {
915 wl_display_dispatch(display.display);
916 } else {
917 wl_display_dispatch_pending(display.display);
918 redraw(&window, NULL, 0);
919 }
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800920 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500921
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200922 fprintf(stderr, "simple-egl exiting\n");
923
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200924 destroy_surface(&window);
925 fini_egl(&display);
926
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400927 wl_surface_destroy(display.cursor_surface);
928 if (display.cursor_theme)
929 wl_cursor_theme_destroy(display.cursor_theme);
930
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200931 if (display.shell)
Jonas Ådahl83630022016-08-11 23:13:20 +0800932 zxdg_shell_v6_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200933
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900934 if (display.ivi_application)
935 ivi_application_destroy(display.ivi_application);
936
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200937 if (display.compositor)
938 wl_compositor_destroy(display.compositor);
939
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200940 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200941 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500942 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200943
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100944 return 0;
945}