blob: 1fd41379807c752405960e0809fe1c56d213aff1 [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010026#include <stdbool.h>
27#include <math.h>
28#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020029#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010030
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030031#include <linux/input.h>
32
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010033#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050034#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040035#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010036
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010037#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040039#include <EGL/eglext.h>
40
41#ifndef EGL_EXT_swap_buffers_with_damage
42#define EGL_EXT_swap_buffers_with_damage 1
43typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
44#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010045
Kristian Høgsberg77787432013-08-22 12:16:51 -070046#ifndef EGL_EXT_buffer_age
47#define EGL_EXT_buffer_age 1
48#define EGL_BUFFER_AGE_EXT 0x313D
49#endif
50
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030051struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010052struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030053
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010054struct display {
55 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040056 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050057 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040058 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040059 struct wl_seat *seat;
60 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070061 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030062 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040063 struct wl_shm *shm;
64 struct wl_cursor_theme *cursor_theme;
65 struct wl_cursor *default_cursor;
66 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010067 struct {
68 EGLDisplay dpy;
69 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050070 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010071 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030072 struct window *window;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040073
74 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010075};
76
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030077struct geometry {
78 int width, height;
79};
80
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010081struct window {
82 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060083 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010084 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010085 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086 GLuint pos;
87 GLuint col;
88 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050089
90 struct wl_egl_window *native;
91 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020092 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050093 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020094 struct wl_callback *callback;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -040095 int fullscreen, configured, opaque;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010096};
97
98static const char *vert_shader_text =
99 "uniform mat4 rotation;\n"
100 "attribute vec4 pos;\n"
101 "attribute vec4 color;\n"
102 "varying vec4 v_color;\n"
103 "void main() {\n"
104 " gl_Position = rotation * pos;\n"
105 " v_color = color;\n"
106 "}\n";
107
108static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500109 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100110 "varying vec4 v_color;\n"
111 "void main() {\n"
112 " gl_FragColor = v_color;\n"
113 "}\n";
114
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400115static int running = 1;
116
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100117static void
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400118init_egl(struct display *display, int opaque)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100119{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500120 static const EGLint context_attribs[] = {
121 EGL_CONTEXT_CLIENT_VERSION, 2,
122 EGL_NONE
123 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400124 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500125
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300126 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500127 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500128 EGL_RED_SIZE, 1,
129 EGL_GREEN_SIZE, 1,
130 EGL_BLUE_SIZE, 1,
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400131 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500132 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
133 EGL_NONE
134 };
135
136 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100137 EGLBoolean ret;
138
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400139 if (opaque)
140 config_attribs[9] = 0;
141
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400142 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100143 assert(display->egl.dpy);
144
145 ret = eglInitialize(display->egl.dpy, &major, &minor);
146 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500147 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100148 assert(ret == EGL_TRUE);
149
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300150 ret = eglChooseConfig(display->egl.dpy, config_attribs,
151 &display->egl.conf, 1, &n);
152 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500153
154 display->egl.ctx = eglCreateContext(display->egl.dpy,
155 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500156 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100157 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500158
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400159 display->swap_buffers_with_damage = NULL;
160 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
161 if (extensions &&
162 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
163 strstr(extensions, "EGL_EXT_buffer_age"))
164 display->swap_buffers_with_damage =
165 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
166 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
167
168 if (display->swap_buffers_with_damage)
169 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
170
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100171}
172
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200173static void
174fini_egl(struct display *display)
175{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200176 eglTerminate(display->egl.dpy);
177 eglReleaseThread();
178}
179
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100180static GLuint
181create_shader(struct window *window, const char *source, GLenum shader_type)
182{
183 GLuint shader;
184 GLint status;
185
186 shader = glCreateShader(shader_type);
187 assert(shader != 0);
188
189 glShaderSource(shader, 1, (const char **) &source, NULL);
190 glCompileShader(shader);
191
192 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
193 if (!status) {
194 char log[1000];
195 GLsizei len;
196 glGetShaderInfoLog(shader, 1000, &len, log);
197 fprintf(stderr, "Error: compiling %s: %*s\n",
198 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
199 len, log);
200 exit(1);
201 }
202
203 return shader;
204}
205
206static void
207init_gl(struct window *window)
208{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100209 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600210 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100211 GLint status;
212
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100213 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
214 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
215
Scott Moreau3ea23d02012-06-13 17:42:21 -0600216 program = glCreateProgram();
217 glAttachShader(program, frag);
218 glAttachShader(program, vert);
219 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100220
Scott Moreau3ea23d02012-06-13 17:42:21 -0600221 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100222 if (!status) {
223 char log[1000];
224 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600225 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100226 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
227 exit(1);
228 }
229
Scott Moreau3ea23d02012-06-13 17:42:21 -0600230 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100231
232 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600233 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100234
Scott Moreau3ea23d02012-06-13 17:42:21 -0600235 glBindAttribLocation(program, window->gl.pos, "pos");
236 glBindAttribLocation(program, window->gl.col, "color");
237 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100238
239 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600240 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100241}
242
243static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300244handle_ping(void *data, struct wl_shell_surface *shell_surface,
245 uint32_t serial)
246{
247 wl_shell_surface_pong(shell_surface, serial);
248}
249
250static void
251handle_configure(void *data, struct wl_shell_surface *shell_surface,
252 uint32_t edges, int32_t width, int32_t height)
253{
254 struct window *window = data;
255
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300256 if (window->native)
257 wl_egl_window_resize(window->native, width, height, 0, 0);
258
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300259 window->geometry.width = width;
260 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300261
Scott Moreau1ee53e72012-08-30 14:44:15 -0600262 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300263 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300264}
265
266static void
267handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
268{
269}
270
271static const struct wl_shell_surface_listener shell_surface_listener = {
272 handle_ping,
273 handle_configure,
274 handle_popup_done
275};
276
277static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300278redraw(void *data, struct wl_callback *callback, uint32_t time);
279
280static void
281configure_callback(void *data, struct wl_callback *callback, uint32_t time)
282{
283 struct window *window = data;
284
285 wl_callback_destroy(callback);
286
287 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600288
289 if (window->callback == NULL)
290 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300291}
292
293static struct wl_callback_listener configure_callback_listener = {
294 configure_callback,
295};
296
297static void
298toggle_fullscreen(struct window *window, int fullscreen)
299{
300 struct wl_callback *callback;
301
302 window->fullscreen = fullscreen;
303 window->configured = 0;
304
305 if (fullscreen) {
306 wl_shell_surface_set_fullscreen(window->shell_surface,
307 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
308 0, NULL);
309 } else {
310 wl_shell_surface_set_toplevel(window->shell_surface);
311 handle_configure(window, window->shell_surface, 0,
312 window->window_size.width,
313 window->window_size.height);
314 }
315
316 callback = wl_display_sync(window->display->display);
317 wl_callback_add_listener(callback, &configure_callback_listener,
318 window);
319}
320
321static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100322create_surface(struct window *window)
323{
324 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500325 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200326
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500327 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200328 window->shell_surface = wl_shell_get_shell_surface(display->shell,
329 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300330
331 wl_shell_surface_add_listener(window->shell_surface,
332 &shell_surface_listener, window);
333
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500334 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400335 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300336 window->window_size.width,
337 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500338 window->egl_surface =
339 eglCreateWindowSurface(display->egl.dpy,
340 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500341 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100342
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600343 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
344
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500345 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
346 window->egl_surface, window->display->egl.ctx);
347 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300348
349 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100350}
351
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200352static void
353destroy_surface(struct window *window)
354{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000355 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
356 * on eglReleaseThread(). */
357 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
358 EGL_NO_CONTEXT);
359
360 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200361 wl_egl_window_destroy(window->native);
362
363 wl_shell_surface_destroy(window->shell_surface);
364 wl_surface_destroy(window->surface);
365
366 if (window->callback)
367 wl_callback_destroy(window->callback);
368}
369
Kristian Høgsberg33418202011-08-16 23:01:28 -0400370static const struct wl_callback_listener frame_listener;
371
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100372static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400373redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100374{
375 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400376 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100377 static const GLfloat verts[3][2] = {
378 { -0.5, -0.5 },
379 { 0.5, -0.5 },
380 { 0, 0.5 }
381 };
382 static const GLfloat colors[3][3] = {
383 { 1, 0, 0 },
384 { 0, 1, 0 },
385 { 0, 0, 1 }
386 };
387 GLfloat angle;
388 GLfloat rotation[4][4] = {
389 { 1, 0, 0, 0 },
390 { 0, 1, 0, 0 },
391 { 0, 0, 1, 0 },
392 { 0, 0, 0, 1 }
393 };
394 static const int32_t speed_div = 5;
395 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400396 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400397 EGLint rect[4];
398 EGLint buffer_age = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100399
Scott Moreau7e300db2012-08-31 03:18:15 -0600400 assert(window->callback == callback);
401 window->callback = NULL;
402
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300403 if (callback)
404 wl_callback_destroy(callback);
405
406 if (!window->configured)
407 return;
408
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100409 if (start_time == 0)
410 start_time = time;
411
412 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
413 rotation[0][0] = cos(angle);
414 rotation[0][2] = sin(angle);
415 rotation[2][0] = -sin(angle);
416 rotation[2][2] = cos(angle);
417
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400418 if (display->swap_buffers_with_damage)
419 eglQuerySurface(display->egl.dpy, window->egl_surface,
420 EGL_BUFFER_AGE_EXT, &buffer_age);
421
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300422 glViewport(0, 0, window->geometry.width, window->geometry.height);
423
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100424 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
425 (GLfloat *) rotation);
426
427 glClearColor(0.0, 0.0, 0.0, 0.5);
428 glClear(GL_COLOR_BUFFER_BIT);
429
430 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
431 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
432 glEnableVertexAttribArray(window->gl.pos);
433 glEnableVertexAttribArray(window->gl.col);
434
435 glDrawArrays(GL_TRIANGLES, 0, 3);
436
437 glDisableVertexAttribArray(window->gl.pos);
438 glDisableVertexAttribArray(window->gl.col);
439
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300440 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400441 region = wl_compositor_create_region(window->display->compositor);
442 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300443 window->geometry.width,
444 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400445 wl_surface_set_opaque_region(window->surface, region);
446 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700447 } else {
448 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400449 }
450
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200451 window->callback = wl_surface_frame(window->surface);
452 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300453
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400454 if (display->swap_buffers_with_damage && buffer_age > 0) {
455 rect[0] = window->geometry.width / 4 - 1;
456 rect[1] = window->geometry.height / 4 - 1;
457 rect[2] = window->geometry.width / 2 + 2;
458 rect[3] = window->geometry.height / 2 + 2;
459 display->swap_buffers_with_damage(display->egl.dpy,
460 window->egl_surface,
461 rect, 1);
462 } else {
463 eglSwapBuffers(display->egl.dpy, window->egl_surface);
464 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100465}
466
Kristian Høgsberg33418202011-08-16 23:01:28 -0400467static const struct wl_callback_listener frame_listener = {
468 redraw
469};
470
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100471static void
Daniel Stone37816df2012-05-16 18:45:18 +0100472pointer_handle_enter(void *data, struct wl_pointer *pointer,
473 uint32_t serial, struct wl_surface *surface,
474 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300475{
476 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400477 struct wl_buffer *buffer;
478 struct wl_cursor *cursor = display->default_cursor;
479 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300480
481 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300482 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400483 else if (cursor) {
484 image = display->default_cursor->images[0];
485 buffer = wl_cursor_image_get_buffer(image);
486 wl_pointer_set_cursor(pointer, serial,
487 display->cursor_surface,
488 image->hotspot_x,
489 image->hotspot_y);
490 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
491 wl_surface_damage(display->cursor_surface, 0, 0,
492 image->width, image->height);
493 wl_surface_commit(display->cursor_surface);
494 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300495}
496
497static void
Daniel Stone37816df2012-05-16 18:45:18 +0100498pointer_handle_leave(void *data, struct wl_pointer *pointer,
499 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300500{
501}
502
503static void
Daniel Stone37816df2012-05-16 18:45:18 +0100504pointer_handle_motion(void *data, struct wl_pointer *pointer,
505 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300506{
507}
508
509static void
Daniel Stone37816df2012-05-16 18:45:18 +0100510pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
511 uint32_t serial, uint32_t time, uint32_t button,
512 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300513{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300514 struct display *display = data;
515
516 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
517 wl_shell_surface_move(display->window->shell_surface,
518 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300519}
520
521static void
Daniel Stone37816df2012-05-16 18:45:18 +0100522pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100523 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300524{
525}
526
Daniel Stone37816df2012-05-16 18:45:18 +0100527static const struct wl_pointer_listener pointer_listener = {
528 pointer_handle_enter,
529 pointer_handle_leave,
530 pointer_handle_motion,
531 pointer_handle_button,
532 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300533};
534
535static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700536touch_handle_down(void *data, struct wl_touch *wl_touch,
537 uint32_t serial, uint32_t time, struct wl_surface *surface,
538 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
539{
540 struct display *d = (struct display *)data;
541
542 wl_shell_surface_move(d->window->shell_surface, d->seat, serial);
543}
544
545static void
546touch_handle_up(void *data, struct wl_touch *wl_touch,
547 uint32_t serial, uint32_t time, int32_t id)
548{
549}
550
551static void
552touch_handle_motion(void *data, struct wl_touch *wl_touch,
553 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
554{
555}
556
557static void
558touch_handle_frame(void *data, struct wl_touch *wl_touch)
559{
560}
561
562static void
563touch_handle_cancel(void *data, struct wl_touch *wl_touch)
564{
565}
566
567static const struct wl_touch_listener touch_listener = {
568 touch_handle_down,
569 touch_handle_up,
570 touch_handle_motion,
571 touch_handle_frame,
572 touch_handle_cancel,
573};
574
575static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300576keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
577 uint32_t format, int fd, uint32_t size)
578{
579}
580
581static void
582keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
583 uint32_t serial, struct wl_surface *surface,
584 struct wl_array *keys)
585{
586}
587
588static void
589keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
590 uint32_t serial, struct wl_surface *surface)
591{
592}
593
594static void
595keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
596 uint32_t serial, uint32_t time, uint32_t key,
597 uint32_t state)
598{
599 struct display *d = data;
600
601 if (key == KEY_F11 && state)
602 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400603 else if (key == KEY_ESC && state)
604 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300605}
606
607static void
608keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
609 uint32_t serial, uint32_t mods_depressed,
610 uint32_t mods_latched, uint32_t mods_locked,
611 uint32_t group)
612{
613}
614
615static const struct wl_keyboard_listener keyboard_listener = {
616 keyboard_handle_keymap,
617 keyboard_handle_enter,
618 keyboard_handle_leave,
619 keyboard_handle_key,
620 keyboard_handle_modifiers,
621};
622
623static void
Daniel Stone37816df2012-05-16 18:45:18 +0100624seat_handle_capabilities(void *data, struct wl_seat *seat,
625 enum wl_seat_capability caps)
626{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400627 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100628
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400629 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
630 d->pointer = wl_seat_get_pointer(seat);
631 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
632 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
633 wl_pointer_destroy(d->pointer);
634 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100635 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300636
637 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
638 d->keyboard = wl_seat_get_keyboard(seat);
639 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
640 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
641 wl_keyboard_destroy(d->keyboard);
642 d->keyboard = NULL;
643 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700644
645 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
646 d->touch = wl_seat_get_touch(seat);
647 wl_touch_set_user_data(d->touch, d);
648 wl_touch_add_listener(d->touch, &touch_listener, d);
649 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
650 wl_touch_destroy(d->touch);
651 d->touch = NULL;
652 }
Daniel Stone37816df2012-05-16 18:45:18 +0100653}
654
655static const struct wl_seat_listener seat_listener = {
656 seat_handle_capabilities,
657};
658
659static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400660registry_handle_global(void *data, struct wl_registry *registry,
661 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100662{
663 struct display *d = data;
664
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400665 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400666 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400667 wl_registry_bind(registry, name,
668 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400669 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400670 d->shell = wl_registry_bind(registry, name,
671 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100672 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400673 d->seat = wl_registry_bind(registry, name,
674 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400675 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400676 } else if (strcmp(interface, "wl_shm") == 0) {
677 d->shm = wl_registry_bind(registry, name,
678 &wl_shm_interface, 1);
679 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
680 d->default_cursor =
681 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400682 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100683}
684
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200685static void
686registry_handle_global_remove(void *data, struct wl_registry *registry,
687 uint32_t name)
688{
689}
690
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400691static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200692 registry_handle_global,
693 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400694};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100695
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200696static void
697signal_int(int signum)
698{
699 running = 0;
700}
701
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400702static void
703usage(int error_code)
704{
705 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
706 " -f\tRun in fullscreen mode\n"
707 " -o\tCreate an opaque surface\n"
708 " -h\tThis help text\n\n");
709
710 exit(error_code);
711}
712
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100713int
714main(int argc, char **argv)
715{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200716 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100717 struct display display = { 0 };
718 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400719 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100720
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100721 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300722 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300723 window.window_size.width = 250;
724 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100725
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400726 for (i = 1; i < argc; i++) {
727 if (strcmp("-f", argv[i]) == 0)
728 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400729 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300730 window.opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400731 else if (strcmp("-h", argv[i]) == 0)
732 usage(EXIT_SUCCESS);
733 else
734 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400735 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300736
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100737 display.display = wl_display_connect(NULL);
738 assert(display.display);
739
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400740 display.registry = wl_display_get_registry(display.display);
741 wl_registry_add_listener(display.registry,
742 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100743
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400744 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200745
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300746 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100747 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500748 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100749
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400750 display.cursor_surface =
751 wl_compositor_create_surface(display.compositor);
752
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200753 sigint.sa_handler = signal_int;
754 sigemptyset(&sigint.sa_mask);
755 sigint.sa_flags = SA_RESETHAND;
756 sigaction(SIGINT, &sigint, NULL);
757
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400758 while (running && ret != -1)
759 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500760
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200761 fprintf(stderr, "simple-egl exiting\n");
762
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200763 destroy_surface(&window);
764 fini_egl(&display);
765
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400766 wl_surface_destroy(display.cursor_surface);
767 if (display.cursor_theme)
768 wl_cursor_theme_destroy(display.cursor_theme);
769
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200770 if (display.shell)
771 wl_shell_destroy(display.shell);
772
773 if (display.compositor)
774 wl_compositor_destroy(display.compositor);
775
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200776 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200777 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500778 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200779
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100780 return 0;
781}