blob: ca884723e5ae208036042e35e974dc305110c6e8 [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
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
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300551 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400552 region = wl_compositor_create_region(window->display->compositor);
553 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300554 window->geometry.width,
555 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400556 wl_surface_set_opaque_region(window->surface, region);
557 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700558 } else {
559 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400560 }
561
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400562 if (display->swap_buffers_with_damage && buffer_age > 0) {
563 rect[0] = window->geometry.width / 4 - 1;
564 rect[1] = window->geometry.height / 4 - 1;
565 rect[2] = window->geometry.width / 2 + 2;
566 rect[3] = window->geometry.height / 2 + 2;
567 display->swap_buffers_with_damage(display->egl.dpy,
568 window->egl_surface,
569 rect, 1);
570 } else {
571 eglSwapBuffers(display->egl.dpy, window->egl_surface);
572 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800573 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100574}
575
576static void
Daniel Stone37816df2012-05-16 18:45:18 +0100577pointer_handle_enter(void *data, struct wl_pointer *pointer,
578 uint32_t serial, struct wl_surface *surface,
579 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300580{
581 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400582 struct wl_buffer *buffer;
583 struct wl_cursor *cursor = display->default_cursor;
584 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300585
586 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300587 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400588 else if (cursor) {
589 image = display->default_cursor->images[0];
590 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100591 if (!buffer)
592 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400593 wl_pointer_set_cursor(pointer, serial,
594 display->cursor_surface,
595 image->hotspot_x,
596 image->hotspot_y);
597 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
598 wl_surface_damage(display->cursor_surface, 0, 0,
599 image->width, image->height);
600 wl_surface_commit(display->cursor_surface);
601 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300602}
603
604static void
Daniel Stone37816df2012-05-16 18:45:18 +0100605pointer_handle_leave(void *data, struct wl_pointer *pointer,
606 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300607{
608}
609
610static void
Daniel Stone37816df2012-05-16 18:45:18 +0100611pointer_handle_motion(void *data, struct wl_pointer *pointer,
612 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300613{
614}
615
616static void
Daniel Stone37816df2012-05-16 18:45:18 +0100617pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
618 uint32_t serial, uint32_t time, uint32_t button,
619 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300620{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300621 struct display *display = data;
622
Jonas Ådahl83630022016-08-11 23:13:20 +0800623 if (!display->window->xdg_toplevel)
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900624 return;
625
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300626 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Jonas Ådahl83630022016-08-11 23:13:20 +0800627 zxdg_toplevel_v6_move(display->window->xdg_toplevel,
628 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300629}
630
631static void
Daniel Stone37816df2012-05-16 18:45:18 +0100632pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100633 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300634{
635}
636
Daniel Stone37816df2012-05-16 18:45:18 +0100637static const struct wl_pointer_listener pointer_listener = {
638 pointer_handle_enter,
639 pointer_handle_leave,
640 pointer_handle_motion,
641 pointer_handle_button,
642 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300643};
644
645static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700646touch_handle_down(void *data, struct wl_touch *wl_touch,
647 uint32_t serial, uint32_t time, struct wl_surface *surface,
648 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
649{
650 struct display *d = (struct display *)data;
651
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900652 if (!d->shell)
653 return;
654
Jonas Ådahl83630022016-08-11 23:13:20 +0800655 zxdg_toplevel_v6_move(d->window->xdg_toplevel, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700656}
657
658static void
659touch_handle_up(void *data, struct wl_touch *wl_touch,
660 uint32_t serial, uint32_t time, int32_t id)
661{
662}
663
664static void
665touch_handle_motion(void *data, struct wl_touch *wl_touch,
666 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
667{
668}
669
670static void
671touch_handle_frame(void *data, struct wl_touch *wl_touch)
672{
673}
674
675static void
676touch_handle_cancel(void *data, struct wl_touch *wl_touch)
677{
678}
679
680static const struct wl_touch_listener touch_listener = {
681 touch_handle_down,
682 touch_handle_up,
683 touch_handle_motion,
684 touch_handle_frame,
685 touch_handle_cancel,
686};
687
688static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300689keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
690 uint32_t format, int fd, uint32_t size)
691{
692}
693
694static void
695keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
696 uint32_t serial, struct wl_surface *surface,
697 struct wl_array *keys)
698{
699}
700
701static void
702keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
703 uint32_t serial, struct wl_surface *surface)
704{
705}
706
707static void
708keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
709 uint32_t serial, uint32_t time, uint32_t key,
710 uint32_t state)
711{
712 struct display *d = data;
713
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900714 if (!d->shell)
715 return;
716
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700717 if (key == KEY_F11 && state) {
718 if (d->window->fullscreen)
Jonas Ådahl83630022016-08-11 23:13:20 +0800719 zxdg_toplevel_v6_unset_fullscreen(d->window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700720 else
Jonas Ådahl83630022016-08-11 23:13:20 +0800721 zxdg_toplevel_v6_set_fullscreen(d->window->xdg_toplevel,
722 NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700723 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400724 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300725}
726
727static void
728keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
729 uint32_t serial, uint32_t mods_depressed,
730 uint32_t mods_latched, uint32_t mods_locked,
731 uint32_t group)
732{
733}
734
735static const struct wl_keyboard_listener keyboard_listener = {
736 keyboard_handle_keymap,
737 keyboard_handle_enter,
738 keyboard_handle_leave,
739 keyboard_handle_key,
740 keyboard_handle_modifiers,
741};
742
743static void
Daniel Stone37816df2012-05-16 18:45:18 +0100744seat_handle_capabilities(void *data, struct wl_seat *seat,
745 enum wl_seat_capability caps)
746{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400747 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100748
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400749 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
750 d->pointer = wl_seat_get_pointer(seat);
751 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
752 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
753 wl_pointer_destroy(d->pointer);
754 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100755 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300756
757 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
758 d->keyboard = wl_seat_get_keyboard(seat);
759 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
760 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
761 wl_keyboard_destroy(d->keyboard);
762 d->keyboard = NULL;
763 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700764
765 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
766 d->touch = wl_seat_get_touch(seat);
767 wl_touch_set_user_data(d->touch, d);
768 wl_touch_add_listener(d->touch, &touch_listener, d);
769 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
770 wl_touch_destroy(d->touch);
771 d->touch = NULL;
772 }
Daniel Stone37816df2012-05-16 18:45:18 +0100773}
774
775static const struct wl_seat_listener seat_listener = {
776 seat_handle_capabilities,
777};
778
779static void
Jonas Ådahl83630022016-08-11 23:13:20 +0800780xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800781{
Jonas Ådahl83630022016-08-11 23:13:20 +0800782 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800783}
784
Jonas Ådahl83630022016-08-11 23:13:20 +0800785static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800786 xdg_shell_ping,
787};
788
789static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400790registry_handle_global(void *data, struct wl_registry *registry,
791 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100792{
793 struct display *d = data;
794
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400795 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400796 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400797 wl_registry_bind(registry, name,
798 &wl_compositor_interface, 1);
Jonas Ådahl83630022016-08-11 23:13:20 +0800799 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400800 d->shell = wl_registry_bind(registry, name,
Jonas Ådahl83630022016-08-11 23:13:20 +0800801 &zxdg_shell_v6_interface, 1);
802 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Daniel Stone37816df2012-05-16 18:45:18 +0100803 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400804 d->seat = wl_registry_bind(registry, name,
805 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400806 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400807 } else if (strcmp(interface, "wl_shm") == 0) {
808 d->shm = wl_registry_bind(registry, name,
809 &wl_shm_interface, 1);
810 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100811 if (!d->cursor_theme) {
812 fprintf(stderr, "unable to load default theme\n");
813 return;
814 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400815 d->default_cursor =
816 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100817 if (!d->default_cursor) {
818 fprintf(stderr, "unable to load default left pointer\n");
819 // TODO: abort ?
820 }
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900821 } else if (strcmp(interface, "ivi_application") == 0) {
822 d->ivi_application =
823 wl_registry_bind(registry, name,
824 &ivi_application_interface, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400825 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100826}
827
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200828static void
829registry_handle_global_remove(void *data, struct wl_registry *registry,
830 uint32_t name)
831{
832}
833
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400834static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200835 registry_handle_global,
836 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400837};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100838
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200839static void
840signal_int(int signum)
841{
842 running = 0;
843}
844
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400845static void
846usage(int error_code)
847{
848 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
849 " -f\tRun in fullscreen mode\n"
850 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700851 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800852 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400853 " -h\tThis help text\n\n");
854
855 exit(error_code);
856}
857
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100858int
859main(int argc, char **argv)
860{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200861 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100862 struct display display = { 0 };
863 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400864 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100865
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100866 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300867 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700868 window.geometry.width = 250;
869 window.geometry.height = 250;
870 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700871 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800872 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100873
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400874 for (i = 1; i < argc; i++) {
875 if (strcmp("-f", argv[i]) == 0)
876 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400877 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300878 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700879 else if (strcmp("-s", argv[i]) == 0)
880 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800881 else if (strcmp("-b", argv[i]) == 0)
882 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400883 else if (strcmp("-h", argv[i]) == 0)
884 usage(EXIT_SUCCESS);
885 else
886 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400887 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300888
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100889 display.display = wl_display_connect(NULL);
890 assert(display.display);
891
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400892 display.registry = wl_display_get_registry(display.display);
893 wl_registry_add_listener(display.registry,
894 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100895
Marek Chalupac21cb3d2016-03-14 11:40:38 +0100896 wl_display_roundtrip(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200897
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700898 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100899 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500900 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100901
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400902 display.cursor_surface =
903 wl_compositor_create_surface(display.compositor);
904
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200905 sigint.sa_handler = signal_int;
906 sigemptyset(&sigint.sa_mask);
907 sigint.sa_flags = SA_RESETHAND;
908 sigaction(SIGINT, &sigint, NULL);
909
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800910 /* The mainloop here is a little subtle. Redrawing will cause
911 * EGL to read events so we can just call
912 * wl_display_dispatch_pending() to handle any events that got
913 * queued up as a side effect. */
914 while (running && ret != -1) {
Jonas Ådahl83630022016-08-11 23:13:20 +0800915 if (window.wait_for_configure) {
916 wl_display_dispatch(display.display);
917 } else {
918 wl_display_dispatch_pending(display.display);
919 redraw(&window, NULL, 0);
920 }
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800921 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500922
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200923 fprintf(stderr, "simple-egl exiting\n");
924
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200925 destroy_surface(&window);
926 fini_egl(&display);
927
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400928 wl_surface_destroy(display.cursor_surface);
929 if (display.cursor_theme)
930 wl_cursor_theme_destroy(display.cursor_theme);
931
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200932 if (display.shell)
Jonas Ådahl83630022016-08-11 23:13:20 +0800933 zxdg_shell_v6_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200934
Nobuhiko Tanibata4f01a0b2014-11-27 13:24:42 +0900935 if (display.ivi_application)
936 ivi_application_destroy(display.ivi_application);
937
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200938 if (display.compositor)
939 wl_compositor_destroy(display.compositor);
940
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200941 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200942 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500943 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200944
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100945 return 0;
946}