blob: dad0f09b385f2e71ceb978acba7d02328699c523 [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>
Daniel Stone7dbb0e12016-11-24 15:30:41 +000048#include "ivi-application-client-protocol.h"
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +090049#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;
Eero Tamminen8a888a52017-02-20 15:14:49 +0200103 int fullscreen, opaque, buffer_size, frame_sync, delay;
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
Emil Velikov903cb572016-11-14 17:08:14 +0000460 weston_platform_destroy_egl_surface(window->display->egl.dpy,
461 window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200462 wl_egl_window_destroy(window->native);
463
Jonas Ådahl83630022016-08-11 23:13:20 +0800464 if (window->xdg_toplevel)
465 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900466 if (window->xdg_surface)
Jonas Ådahl83630022016-08-11 23:13:20 +0800467 zxdg_surface_v6_destroy(window->xdg_surface);
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900468 if (window->display->ivi_application)
469 ivi_surface_destroy(window->ivi_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200470 wl_surface_destroy(window->surface);
471
472 if (window->callback)
473 wl_callback_destroy(window->callback);
474}
475
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100476static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400477redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100478{
479 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400480 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100481 static const GLfloat verts[3][2] = {
482 { -0.5, -0.5 },
483 { 0.5, -0.5 },
484 { 0, 0.5 }
485 };
486 static const GLfloat colors[3][3] = {
487 { 1, 0, 0 },
488 { 0, 1, 0 },
489 { 0, 0, 1 }
490 };
491 GLfloat angle;
492 GLfloat rotation[4][4] = {
493 { 1, 0, 0, 0 },
494 { 0, 1, 0, 0 },
495 { 0, 0, 1, 0 },
496 { 0, 0, 0, 1 }
497 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100498 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400499 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400500 EGLint rect[4];
501 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800502 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100503
Scott Moreau7e300db2012-08-31 03:18:15 -0600504 assert(window->callback == callback);
505 window->callback = NULL;
506
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300507 if (callback)
508 wl_callback_destroy(callback);
509
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800510 gettimeofday(&tv, NULL);
511 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
512 if (window->frames == 0)
513 window->benchmark_time = time;
514 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
515 printf("%d frames in %d seconds: %f fps\n",
516 window->frames,
517 benchmark_interval,
518 (float) window->frames / benchmark_interval);
519 window->benchmark_time = time;
520 window->frames = 0;
521 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100522
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800523 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100524 rotation[0][0] = cos(angle);
525 rotation[0][2] = sin(angle);
526 rotation[2][0] = -sin(angle);
527 rotation[2][2] = cos(angle);
528
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400529 if (display->swap_buffers_with_damage)
530 eglQuerySurface(display->egl.dpy, window->egl_surface,
531 EGL_BUFFER_AGE_EXT, &buffer_age);
532
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300533 glViewport(0, 0, window->geometry.width, window->geometry.height);
534
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100535 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
536 (GLfloat *) rotation);
537
538 glClearColor(0.0, 0.0, 0.0, 0.5);
539 glClear(GL_COLOR_BUFFER_BIT);
540
541 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
542 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
543 glEnableVertexAttribArray(window->gl.pos);
544 glEnableVertexAttribArray(window->gl.col);
545
546 glDrawArrays(GL_TRIANGLES, 0, 3);
547
548 glDisableVertexAttribArray(window->gl.pos);
549 glDisableVertexAttribArray(window->gl.col);
550
Eero Tamminen8a888a52017-02-20 15:14:49 +0200551 usleep(window->delay);
552
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300553 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400554 region = wl_compositor_create_region(window->display->compositor);
555 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300556 window->geometry.width,
557 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400558 wl_surface_set_opaque_region(window->surface, region);
559 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700560 } else {
561 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400562 }
563
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400564 if (display->swap_buffers_with_damage && buffer_age > 0) {
565 rect[0] = window->geometry.width / 4 - 1;
566 rect[1] = window->geometry.height / 4 - 1;
567 rect[2] = window->geometry.width / 2 + 2;
568 rect[3] = window->geometry.height / 2 + 2;
569 display->swap_buffers_with_damage(display->egl.dpy,
570 window->egl_surface,
571 rect, 1);
572 } else {
573 eglSwapBuffers(display->egl.dpy, window->egl_surface);
574 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800575 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100576}
577
578static void
Daniel Stone37816df2012-05-16 18:45:18 +0100579pointer_handle_enter(void *data, struct wl_pointer *pointer,
580 uint32_t serial, struct wl_surface *surface,
581 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300582{
583 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400584 struct wl_buffer *buffer;
585 struct wl_cursor *cursor = display->default_cursor;
586 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300587
588 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300589 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400590 else if (cursor) {
591 image = display->default_cursor->images[0];
592 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100593 if (!buffer)
594 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400595 wl_pointer_set_cursor(pointer, serial,
596 display->cursor_surface,
597 image->hotspot_x,
598 image->hotspot_y);
599 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
600 wl_surface_damage(display->cursor_surface, 0, 0,
601 image->width, image->height);
602 wl_surface_commit(display->cursor_surface);
603 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300604}
605
606static void
Daniel Stone37816df2012-05-16 18:45:18 +0100607pointer_handle_leave(void *data, struct wl_pointer *pointer,
608 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300609{
610}
611
612static void
Daniel Stone37816df2012-05-16 18:45:18 +0100613pointer_handle_motion(void *data, struct wl_pointer *pointer,
614 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300615{
616}
617
618static void
Daniel Stone37816df2012-05-16 18:45:18 +0100619pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
620 uint32_t serial, uint32_t time, uint32_t button,
621 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300622{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300623 struct display *display = data;
624
Jonas Ådahl83630022016-08-11 23:13:20 +0800625 if (!display->window->xdg_toplevel)
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900626 return;
627
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300628 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Jonas Ådahl83630022016-08-11 23:13:20 +0800629 zxdg_toplevel_v6_move(display->window->xdg_toplevel,
630 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300631}
632
633static void
Daniel Stone37816df2012-05-16 18:45:18 +0100634pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100635 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300636{
637}
638
Daniel Stone37816df2012-05-16 18:45:18 +0100639static const struct wl_pointer_listener pointer_listener = {
640 pointer_handle_enter,
641 pointer_handle_leave,
642 pointer_handle_motion,
643 pointer_handle_button,
644 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300645};
646
647static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700648touch_handle_down(void *data, struct wl_touch *wl_touch,
649 uint32_t serial, uint32_t time, struct wl_surface *surface,
650 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
651{
652 struct display *d = (struct display *)data;
653
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900654 if (!d->shell)
655 return;
656
Jonas Ådahl83630022016-08-11 23:13:20 +0800657 zxdg_toplevel_v6_move(d->window->xdg_toplevel, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700658}
659
660static void
661touch_handle_up(void *data, struct wl_touch *wl_touch,
662 uint32_t serial, uint32_t time, int32_t id)
663{
664}
665
666static void
667touch_handle_motion(void *data, struct wl_touch *wl_touch,
668 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
669{
670}
671
672static void
673touch_handle_frame(void *data, struct wl_touch *wl_touch)
674{
675}
676
677static void
678touch_handle_cancel(void *data, struct wl_touch *wl_touch)
679{
680}
681
682static const struct wl_touch_listener touch_listener = {
683 touch_handle_down,
684 touch_handle_up,
685 touch_handle_motion,
686 touch_handle_frame,
687 touch_handle_cancel,
688};
689
690static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300691keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
692 uint32_t format, int fd, uint32_t size)
693{
694}
695
696static void
697keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
698 uint32_t serial, struct wl_surface *surface,
699 struct wl_array *keys)
700{
701}
702
703static void
704keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
705 uint32_t serial, struct wl_surface *surface)
706{
707}
708
709static void
710keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
711 uint32_t serial, uint32_t time, uint32_t key,
712 uint32_t state)
713{
714 struct display *d = data;
715
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900716 if (!d->shell)
717 return;
718
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700719 if (key == KEY_F11 && state) {
720 if (d->window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800721 zxdg_toplevel_v6_unset_fullscreen(d->window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700722 else
Jonas Ådahl83630022016-08-11 23:13:20 +0800723 zxdg_toplevel_v6_set_fullscreen(d->window->xdg_toplevel,
724 NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700725 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400726 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300727}
728
729static void
730keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
731 uint32_t serial, uint32_t mods_depressed,
732 uint32_t mods_latched, uint32_t mods_locked,
733 uint32_t group)
734{
735}
736
737static const struct wl_keyboard_listener keyboard_listener = {
738 keyboard_handle_keymap,
739 keyboard_handle_enter,
740 keyboard_handle_leave,
741 keyboard_handle_key,
742 keyboard_handle_modifiers,
743};
744
745static void
Daniel Stone37816df2012-05-16 18:45:18 +0100746seat_handle_capabilities(void *data, struct wl_seat *seat,
747 enum wl_seat_capability caps)
748{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400749 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100750
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400751 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
752 d->pointer = wl_seat_get_pointer(seat);
753 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
754 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
755 wl_pointer_destroy(d->pointer);
756 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100757 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300758
759 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
760 d->keyboard = wl_seat_get_keyboard(seat);
761 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
762 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
763 wl_keyboard_destroy(d->keyboard);
764 d->keyboard = NULL;
765 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700766
767 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
768 d->touch = wl_seat_get_touch(seat);
769 wl_touch_set_user_data(d->touch, d);
770 wl_touch_add_listener(d->touch, &touch_listener, d);
771 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
772 wl_touch_destroy(d->touch);
773 d->touch = NULL;
774 }
Daniel Stone37816df2012-05-16 18:45:18 +0100775}
776
777static const struct wl_seat_listener seat_listener = {
778 seat_handle_capabilities,
779};
780
781static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800782xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800783{
Jonas Ådahl83630022016-08-11 23:13:20 +0800784 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800785}
786
Jonas Ådahl83630022016-08-11 23:13:20 +0800787static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800788 xdg_shell_ping,
789};
790
791static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400792registry_handle_global(void *data, struct wl_registry *registry,
793 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100794{
795 struct display *d = data;
796
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400797 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400798 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400799 wl_registry_bind(registry, name,
Derek Foreman6156d672017-05-19 09:39:06 -0500800 &wl_compositor_interface,
801 MIN(version, 4));
Jonas Ådahl83630022016-08-11 23:13:20 +0800802 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400803 d->shell = wl_registry_bind(registry, name,
Jonas Ådahl83630022016-08-11 23:13:20 +0800804 &zxdg_shell_v6_interface, 1);
805 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Daniel Stone37816df2012-05-16 18:45:18 +0100806 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400807 d->seat = wl_registry_bind(registry, name,
808 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400809 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400810 } else if (strcmp(interface, "wl_shm") == 0) {
811 d->shm = wl_registry_bind(registry, name,
812 &wl_shm_interface, 1);
813 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100814 if (!d->cursor_theme) {
815 fprintf(stderr, "unable to load default theme\n");
816 return;
817 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400818 d->default_cursor =
819 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100820 if (!d->default_cursor) {
821 fprintf(stderr, "unable to load default left pointer\n");
822 // TODO: abort ?
823 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900824 } else if (strcmp(interface, "ivi_application") == 0) {
825 d->ivi_application =
826 wl_registry_bind(registry, name,
827 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400828 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100829}
830
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200831static void
832registry_handle_global_remove(void *data, struct wl_registry *registry,
833 uint32_t name)
834{
835}
836
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400837static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200838 registry_handle_global,
839 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400840};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100841
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200842static void
843signal_int(int signum)
844{
845 running = 0;
846}
847
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400848static void
849usage(int error_code)
850{
851 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
Eero Tamminen8a888a52017-02-20 15:14:49 +0200852 " -d <us>\tBuffer swap delay in microseconds\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400853 " -f\tRun in fullscreen mode\n"
854 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700855 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800856 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400857 " -h\tThis help text\n\n");
858
859 exit(error_code);
860}
861
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100862int
863main(int argc, char **argv)
864{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200865 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100866 struct display display = { 0 };
867 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400868 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100869
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100870 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300871 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700872 window.geometry.width = 250;
873 window.geometry.height = 250;
874 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700875 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800876 window.frame_sync = 1;
Eero Tamminen8a888a52017-02-20 15:14:49 +0200877 window.delay = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100878
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400879 for (i = 1; i < argc; i++) {
Eero Tamminen8a888a52017-02-20 15:14:49 +0200880 if (strcmp("-d", argv[i]) == 0 && i+1 < argc)
881 window.delay = atoi(argv[++i]);
882 else if (strcmp("-f", argv[i]) == 0)
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400883 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400884 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300885 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700886 else if (strcmp("-s", argv[i]) == 0)
887 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800888 else if (strcmp("-b", argv[i]) == 0)
889 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400890 else if (strcmp("-h", argv[i]) == 0)
891 usage(EXIT_SUCCESS);
892 else
893 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400894 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300895
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100896 display.display = wl_display_connect(NULL);
897 assert(display.display);
898
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400899 display.registry = wl_display_get_registry(display.display);
900 wl_registry_add_listener(display.registry,
901 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100902
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100903 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200904
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700905 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100906 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500907 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100908
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400909 display.cursor_surface =
910 wl_compositor_create_surface(display.compositor);
911
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200912 sigint.sa_handler = signal_int;
913 sigemptyset(&sigint.sa_mask);
914 sigint.sa_flags = SA_RESETHAND;
915 sigaction(SIGINT, &sigint, NULL);
916
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800917 /* The mainloop here is a little subtle. Redrawing will cause
918 * EGL to read events so we can just call
919 * wl_display_dispatch_pending() to handle any events that got
920 * queued up as a side effect. */
921 while (running && ret != -1) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800922 if (window.wait_for_configure) {
923 wl_display_dispatch(display.display);
924 } else {
925 wl_display_dispatch_pending(display.display);
926 redraw(&window, NULL, 0);
927 }
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800928 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500929
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200930 fprintf(stderr, "simple-egl exiting\n");
931
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200932 destroy_surface(&window);
933 fini_egl(&display);
934
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400935 wl_surface_destroy(display.cursor_surface);
936 if (display.cursor_theme)
937 wl_cursor_theme_destroy(display.cursor_theme);
938
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200939 if (display.shell)
Jonas Ådahl83630022016-08-11 23:13:20 +0800940 zxdg_shell_v6_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200941
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900942 if (display.ivi_application)
943 ivi_application_destroy(display.ivi_application);
944
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200945 if (display.compositor)
946 wl_compositor_destroy(display.compositor);
947
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200948 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200949 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500950 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200951
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100952 return 0;
953}