blob: a557fca56e877a31b3c1ab63a902d3bc55c86d20 [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
Adrian Negreanuf5e3ad22013-08-12 16:30:11 +030041#include <src/weston-egl-ext.h>
42
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040043#ifndef EGL_EXT_swap_buffers_with_damage
44#define EGL_EXT_swap_buffers_with_damage 1
45typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
46#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010047
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030048struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010049struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030050
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010051struct display {
52 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040053 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050054 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040055 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040056 struct wl_seat *seat;
57 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070058 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030059 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040060 struct wl_shm *shm;
61 struct wl_cursor_theme *cursor_theme;
62 struct wl_cursor *default_cursor;
63 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010064 struct {
65 EGLDisplay dpy;
66 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050067 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010068 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030069 struct window *window;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040070
71 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010072};
73
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030074struct geometry {
75 int width, height;
76};
77
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010078struct window {
79 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060080 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010081 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010082 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010083 GLuint pos;
84 GLuint col;
85 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050086
87 struct wl_egl_window *native;
88 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020089 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050090 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020091 struct wl_callback *callback;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -040092 int fullscreen, configured, opaque;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010093};
94
95static const char *vert_shader_text =
96 "uniform mat4 rotation;\n"
97 "attribute vec4 pos;\n"
98 "attribute vec4 color;\n"
99 "varying vec4 v_color;\n"
100 "void main() {\n"
101 " gl_Position = rotation * pos;\n"
102 " v_color = color;\n"
103 "}\n";
104
105static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500106 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100107 "varying vec4 v_color;\n"
108 "void main() {\n"
109 " gl_FragColor = v_color;\n"
110 "}\n";
111
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400112static int running = 1;
113
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100114static void
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400115init_egl(struct display *display, int opaque)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100116{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500117 static const EGLint context_attribs[] = {
118 EGL_CONTEXT_CLIENT_VERSION, 2,
119 EGL_NONE
120 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400121 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500122
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300123 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500124 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500125 EGL_RED_SIZE, 1,
126 EGL_GREEN_SIZE, 1,
127 EGL_BLUE_SIZE, 1,
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400128 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500129 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
130 EGL_NONE
131 };
132
133 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100134 EGLBoolean ret;
135
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400136 if (opaque)
137 config_attribs[9] = 0;
138
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400139 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100140 assert(display->egl.dpy);
141
142 ret = eglInitialize(display->egl.dpy, &major, &minor);
143 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500144 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100145 assert(ret == EGL_TRUE);
146
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300147 ret = eglChooseConfig(display->egl.dpy, config_attribs,
148 &display->egl.conf, 1, &n);
149 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500150
151 display->egl.ctx = eglCreateContext(display->egl.dpy,
152 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500153 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100154 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500155
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400156 display->swap_buffers_with_damage = NULL;
157 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
158 if (extensions &&
159 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
160 strstr(extensions, "EGL_EXT_buffer_age"))
161 display->swap_buffers_with_damage =
162 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
163 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
164
165 if (display->swap_buffers_with_damage)
166 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
167
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100168}
169
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200170static void
171fini_egl(struct display *display)
172{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200173 eglTerminate(display->egl.dpy);
174 eglReleaseThread();
175}
176
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100177static GLuint
178create_shader(struct window *window, const char *source, GLenum shader_type)
179{
180 GLuint shader;
181 GLint status;
182
183 shader = glCreateShader(shader_type);
184 assert(shader != 0);
185
186 glShaderSource(shader, 1, (const char **) &source, NULL);
187 glCompileShader(shader);
188
189 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
190 if (!status) {
191 char log[1000];
192 GLsizei len;
193 glGetShaderInfoLog(shader, 1000, &len, log);
194 fprintf(stderr, "Error: compiling %s: %*s\n",
195 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
196 len, log);
197 exit(1);
198 }
199
200 return shader;
201}
202
203static void
204init_gl(struct window *window)
205{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100206 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600207 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100208 GLint status;
209
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100210 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
211 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
212
Scott Moreau3ea23d02012-06-13 17:42:21 -0600213 program = glCreateProgram();
214 glAttachShader(program, frag);
215 glAttachShader(program, vert);
216 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100217
Scott Moreau3ea23d02012-06-13 17:42:21 -0600218 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100219 if (!status) {
220 char log[1000];
221 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600222 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100223 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
224 exit(1);
225 }
226
Scott Moreau3ea23d02012-06-13 17:42:21 -0600227 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100228
229 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600230 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100231
Scott Moreau3ea23d02012-06-13 17:42:21 -0600232 glBindAttribLocation(program, window->gl.pos, "pos");
233 glBindAttribLocation(program, window->gl.col, "color");
234 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100235
236 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600237 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100238}
239
240static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300241handle_ping(void *data, struct wl_shell_surface *shell_surface,
242 uint32_t serial)
243{
244 wl_shell_surface_pong(shell_surface, serial);
245}
246
247static void
248handle_configure(void *data, struct wl_shell_surface *shell_surface,
249 uint32_t edges, int32_t width, int32_t height)
250{
251 struct window *window = data;
252
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300253 if (window->native)
254 wl_egl_window_resize(window->native, width, height, 0, 0);
255
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300256 window->geometry.width = width;
257 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300258
Scott Moreau1ee53e72012-08-30 14:44:15 -0600259 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300260 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300261}
262
263static void
264handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
265{
266}
267
268static const struct wl_shell_surface_listener shell_surface_listener = {
269 handle_ping,
270 handle_configure,
271 handle_popup_done
272};
273
274static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300275redraw(void *data, struct wl_callback *callback, uint32_t time);
276
277static void
278configure_callback(void *data, struct wl_callback *callback, uint32_t time)
279{
280 struct window *window = data;
281
282 wl_callback_destroy(callback);
283
284 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600285
286 if (window->callback == NULL)
287 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300288}
289
290static struct wl_callback_listener configure_callback_listener = {
291 configure_callback,
292};
293
294static void
295toggle_fullscreen(struct window *window, int fullscreen)
296{
297 struct wl_callback *callback;
298
299 window->fullscreen = fullscreen;
300 window->configured = 0;
301
302 if (fullscreen) {
303 wl_shell_surface_set_fullscreen(window->shell_surface,
304 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
305 0, NULL);
306 } else {
307 wl_shell_surface_set_toplevel(window->shell_surface);
308 handle_configure(window, window->shell_surface, 0,
309 window->window_size.width,
310 window->window_size.height);
311 }
312
313 callback = wl_display_sync(window->display->display);
314 wl_callback_add_listener(callback, &configure_callback_listener,
315 window);
316}
317
318static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100319create_surface(struct window *window)
320{
321 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500322 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200323
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500324 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200325 window->shell_surface = wl_shell_get_shell_surface(display->shell,
326 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300327
328 wl_shell_surface_add_listener(window->shell_surface,
329 &shell_surface_listener, window);
330
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500331 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400332 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300333 window->window_size.width,
334 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500335 window->egl_surface =
336 eglCreateWindowSurface(display->egl.dpy,
337 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500338 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100339
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600340 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
341
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500342 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
343 window->egl_surface, window->display->egl.ctx);
344 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300345
346 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100347}
348
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200349static void
350destroy_surface(struct window *window)
351{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000352 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
353 * on eglReleaseThread(). */
354 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
355 EGL_NO_CONTEXT);
356
357 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200358 wl_egl_window_destroy(window->native);
359
360 wl_shell_surface_destroy(window->shell_surface);
361 wl_surface_destroy(window->surface);
362
363 if (window->callback)
364 wl_callback_destroy(window->callback);
365}
366
Kristian Høgsberg33418202011-08-16 23:01:28 -0400367static const struct wl_callback_listener frame_listener;
368
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100369static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400370redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100371{
372 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400373 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100374 static const GLfloat verts[3][2] = {
375 { -0.5, -0.5 },
376 { 0.5, -0.5 },
377 { 0, 0.5 }
378 };
379 static const GLfloat colors[3][3] = {
380 { 1, 0, 0 },
381 { 0, 1, 0 },
382 { 0, 0, 1 }
383 };
384 GLfloat angle;
385 GLfloat rotation[4][4] = {
386 { 1, 0, 0, 0 },
387 { 0, 1, 0, 0 },
388 { 0, 0, 1, 0 },
389 { 0, 0, 0, 1 }
390 };
391 static const int32_t speed_div = 5;
392 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400393 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400394 EGLint rect[4];
395 EGLint buffer_age = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100396
Scott Moreau7e300db2012-08-31 03:18:15 -0600397 assert(window->callback == callback);
398 window->callback = NULL;
399
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300400 if (callback)
401 wl_callback_destroy(callback);
402
403 if (!window->configured)
404 return;
405
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100406 if (start_time == 0)
407 start_time = time;
408
409 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
410 rotation[0][0] = cos(angle);
411 rotation[0][2] = sin(angle);
412 rotation[2][0] = -sin(angle);
413 rotation[2][2] = cos(angle);
414
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400415 if (display->swap_buffers_with_damage)
416 eglQuerySurface(display->egl.dpy, window->egl_surface,
417 EGL_BUFFER_AGE_EXT, &buffer_age);
418
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300419 glViewport(0, 0, window->geometry.width, window->geometry.height);
420
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100421 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
422 (GLfloat *) rotation);
423
424 glClearColor(0.0, 0.0, 0.0, 0.5);
425 glClear(GL_COLOR_BUFFER_BIT);
426
427 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
428 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
429 glEnableVertexAttribArray(window->gl.pos);
430 glEnableVertexAttribArray(window->gl.col);
431
432 glDrawArrays(GL_TRIANGLES, 0, 3);
433
434 glDisableVertexAttribArray(window->gl.pos);
435 glDisableVertexAttribArray(window->gl.col);
436
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300437 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400438 region = wl_compositor_create_region(window->display->compositor);
439 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300440 window->geometry.width,
441 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400442 wl_surface_set_opaque_region(window->surface, region);
443 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700444 } else {
445 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400446 }
447
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200448 window->callback = wl_surface_frame(window->surface);
449 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300450
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400451 if (display->swap_buffers_with_damage && buffer_age > 0) {
452 rect[0] = window->geometry.width / 4 - 1;
453 rect[1] = window->geometry.height / 4 - 1;
454 rect[2] = window->geometry.width / 2 + 2;
455 rect[3] = window->geometry.height / 2 + 2;
456 display->swap_buffers_with_damage(display->egl.dpy,
457 window->egl_surface,
458 rect, 1);
459 } else {
460 eglSwapBuffers(display->egl.dpy, window->egl_surface);
461 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100462}
463
Kristian Høgsberg33418202011-08-16 23:01:28 -0400464static const struct wl_callback_listener frame_listener = {
465 redraw
466};
467
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100468static void
Daniel Stone37816df2012-05-16 18:45:18 +0100469pointer_handle_enter(void *data, struct wl_pointer *pointer,
470 uint32_t serial, struct wl_surface *surface,
471 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300472{
473 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400474 struct wl_buffer *buffer;
475 struct wl_cursor *cursor = display->default_cursor;
476 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300477
478 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300479 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400480 else if (cursor) {
481 image = display->default_cursor->images[0];
482 buffer = wl_cursor_image_get_buffer(image);
483 wl_pointer_set_cursor(pointer, serial,
484 display->cursor_surface,
485 image->hotspot_x,
486 image->hotspot_y);
487 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
488 wl_surface_damage(display->cursor_surface, 0, 0,
489 image->width, image->height);
490 wl_surface_commit(display->cursor_surface);
491 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300492}
493
494static void
Daniel Stone37816df2012-05-16 18:45:18 +0100495pointer_handle_leave(void *data, struct wl_pointer *pointer,
496 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300497{
498}
499
500static void
Daniel Stone37816df2012-05-16 18:45:18 +0100501pointer_handle_motion(void *data, struct wl_pointer *pointer,
502 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300503{
504}
505
506static void
Daniel Stone37816df2012-05-16 18:45:18 +0100507pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
508 uint32_t serial, uint32_t time, uint32_t button,
509 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300510{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300511 struct display *display = data;
512
513 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
514 wl_shell_surface_move(display->window->shell_surface,
515 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300516}
517
518static void
Daniel Stone37816df2012-05-16 18:45:18 +0100519pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100520 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300521{
522}
523
Daniel Stone37816df2012-05-16 18:45:18 +0100524static const struct wl_pointer_listener pointer_listener = {
525 pointer_handle_enter,
526 pointer_handle_leave,
527 pointer_handle_motion,
528 pointer_handle_button,
529 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300530};
531
532static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700533touch_handle_down(void *data, struct wl_touch *wl_touch,
534 uint32_t serial, uint32_t time, struct wl_surface *surface,
535 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
536{
537 struct display *d = (struct display *)data;
538
539 wl_shell_surface_move(d->window->shell_surface, d->seat, serial);
540}
541
542static void
543touch_handle_up(void *data, struct wl_touch *wl_touch,
544 uint32_t serial, uint32_t time, int32_t id)
545{
546}
547
548static void
549touch_handle_motion(void *data, struct wl_touch *wl_touch,
550 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
551{
552}
553
554static void
555touch_handle_frame(void *data, struct wl_touch *wl_touch)
556{
557}
558
559static void
560touch_handle_cancel(void *data, struct wl_touch *wl_touch)
561{
562}
563
564static const struct wl_touch_listener touch_listener = {
565 touch_handle_down,
566 touch_handle_up,
567 touch_handle_motion,
568 touch_handle_frame,
569 touch_handle_cancel,
570};
571
572static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300573keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
574 uint32_t format, int fd, uint32_t size)
575{
576}
577
578static void
579keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
580 uint32_t serial, struct wl_surface *surface,
581 struct wl_array *keys)
582{
583}
584
585static void
586keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
587 uint32_t serial, struct wl_surface *surface)
588{
589}
590
591static void
592keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
593 uint32_t serial, uint32_t time, uint32_t key,
594 uint32_t state)
595{
596 struct display *d = data;
597
598 if (key == KEY_F11 && state)
599 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400600 else if (key == KEY_ESC && state)
601 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300602}
603
604static void
605keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
606 uint32_t serial, uint32_t mods_depressed,
607 uint32_t mods_latched, uint32_t mods_locked,
608 uint32_t group)
609{
610}
611
612static const struct wl_keyboard_listener keyboard_listener = {
613 keyboard_handle_keymap,
614 keyboard_handle_enter,
615 keyboard_handle_leave,
616 keyboard_handle_key,
617 keyboard_handle_modifiers,
618};
619
620static void
Daniel Stone37816df2012-05-16 18:45:18 +0100621seat_handle_capabilities(void *data, struct wl_seat *seat,
622 enum wl_seat_capability caps)
623{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400624 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100625
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400626 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
627 d->pointer = wl_seat_get_pointer(seat);
628 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
629 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
630 wl_pointer_destroy(d->pointer);
631 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100632 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300633
634 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
635 d->keyboard = wl_seat_get_keyboard(seat);
636 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
637 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
638 wl_keyboard_destroy(d->keyboard);
639 d->keyboard = NULL;
640 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700641
642 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
643 d->touch = wl_seat_get_touch(seat);
644 wl_touch_set_user_data(d->touch, d);
645 wl_touch_add_listener(d->touch, &touch_listener, d);
646 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
647 wl_touch_destroy(d->touch);
648 d->touch = NULL;
649 }
Daniel Stone37816df2012-05-16 18:45:18 +0100650}
651
652static const struct wl_seat_listener seat_listener = {
653 seat_handle_capabilities,
654};
655
656static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400657registry_handle_global(void *data, struct wl_registry *registry,
658 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100659{
660 struct display *d = data;
661
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400662 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400663 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400664 wl_registry_bind(registry, name,
665 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400666 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400667 d->shell = wl_registry_bind(registry, name,
668 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100669 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400670 d->seat = wl_registry_bind(registry, name,
671 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400672 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400673 } else if (strcmp(interface, "wl_shm") == 0) {
674 d->shm = wl_registry_bind(registry, name,
675 &wl_shm_interface, 1);
676 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
677 d->default_cursor =
678 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400679 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100680}
681
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200682static void
683registry_handle_global_remove(void *data, struct wl_registry *registry,
684 uint32_t name)
685{
686}
687
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400688static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200689 registry_handle_global,
690 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400691};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100692
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200693static void
694signal_int(int signum)
695{
696 running = 0;
697}
698
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400699static void
700usage(int error_code)
701{
702 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
703 " -f\tRun in fullscreen mode\n"
704 " -o\tCreate an opaque surface\n"
705 " -h\tThis help text\n\n");
706
707 exit(error_code);
708}
709
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100710int
711main(int argc, char **argv)
712{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200713 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100714 struct display display = { 0 };
715 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400716 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100717
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100718 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300719 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300720 window.window_size.width = 250;
721 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100722
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400723 for (i = 1; i < argc; i++) {
724 if (strcmp("-f", argv[i]) == 0)
725 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400726 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300727 window.opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400728 else if (strcmp("-h", argv[i]) == 0)
729 usage(EXIT_SUCCESS);
730 else
731 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400732 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300733
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100734 display.display = wl_display_connect(NULL);
735 assert(display.display);
736
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400737 display.registry = wl_display_get_registry(display.display);
738 wl_registry_add_listener(display.registry,
739 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100740
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400741 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200742
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300743 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100744 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500745 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100746
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400747 display.cursor_surface =
748 wl_compositor_create_surface(display.compositor);
749
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200750 sigint.sa_handler = signal_int;
751 sigemptyset(&sigint.sa_mask);
752 sigint.sa_flags = SA_RESETHAND;
753 sigaction(SIGINT, &sigint, NULL);
754
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400755 while (running && ret != -1)
756 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500757
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200758 fprintf(stderr, "simple-egl exiting\n");
759
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200760 destroy_surface(&window);
761 fini_egl(&display);
762
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400763 wl_surface_destroy(display.cursor_surface);
764 if (display.cursor_theme)
765 wl_cursor_theme_destroy(display.cursor_theme);
766
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200767 if (display.shell)
768 wl_shell_destroy(display.shell);
769
770 if (display.compositor)
771 wl_compositor_destroy(display.compositor);
772
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200773 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200774 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500775 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200776
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100777 return 0;
778}