blob: f4468b7a010ece560b02479db99e1c20ddbcb077 [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>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030040struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010041struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030042
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010043struct display {
44 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040045 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050046 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040047 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040048 struct wl_seat *seat;
49 struct wl_pointer *pointer;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030050 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040051 struct wl_shm *shm;
52 struct wl_cursor_theme *cursor_theme;
53 struct wl_cursor *default_cursor;
54 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010055 struct {
56 EGLDisplay dpy;
57 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050058 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010059 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030060 struct window *window;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010061};
62
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030063struct geometry {
64 int width, height;
65};
66
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010067struct window {
68 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060069 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010070 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010071 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010072 GLuint pos;
73 GLuint col;
74 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050075
76 struct wl_egl_window *native;
77 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020078 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050079 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020080 struct wl_callback *callback;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -040081 int fullscreen, configured, opaque;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010082};
83
84static const char *vert_shader_text =
85 "uniform mat4 rotation;\n"
86 "attribute vec4 pos;\n"
87 "attribute vec4 color;\n"
88 "varying vec4 v_color;\n"
89 "void main() {\n"
90 " gl_Position = rotation * pos;\n"
91 " v_color = color;\n"
92 "}\n";
93
94static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050095 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010096 "varying vec4 v_color;\n"
97 "void main() {\n"
98 " gl_FragColor = v_color;\n"
99 "}\n";
100
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400101static int running = 1;
102
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100103static void
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400104init_egl(struct display *display, int opaque)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100105{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500106 static const EGLint context_attribs[] = {
107 EGL_CONTEXT_CLIENT_VERSION, 2,
108 EGL_NONE
109 };
110
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300111 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500112 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500113 EGL_RED_SIZE, 1,
114 EGL_GREEN_SIZE, 1,
115 EGL_BLUE_SIZE, 1,
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400116 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500117 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
118 EGL_NONE
119 };
120
121 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100122 EGLBoolean ret;
123
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400124 if (opaque)
125 config_attribs[9] = 0;
126
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400127 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100128 assert(display->egl.dpy);
129
130 ret = eglInitialize(display->egl.dpy, &major, &minor);
131 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500132 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100133 assert(ret == EGL_TRUE);
134
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300135 ret = eglChooseConfig(display->egl.dpy, config_attribs,
136 &display->egl.conf, 1, &n);
137 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500138
139 display->egl.ctx = eglCreateContext(display->egl.dpy,
140 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500141 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100142 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500143
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100144}
145
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200146static void
147fini_egl(struct display *display)
148{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200149 eglTerminate(display->egl.dpy);
150 eglReleaseThread();
151}
152
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100153static GLuint
154create_shader(struct window *window, const char *source, GLenum shader_type)
155{
156 GLuint shader;
157 GLint status;
158
159 shader = glCreateShader(shader_type);
160 assert(shader != 0);
161
162 glShaderSource(shader, 1, (const char **) &source, NULL);
163 glCompileShader(shader);
164
165 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
166 if (!status) {
167 char log[1000];
168 GLsizei len;
169 glGetShaderInfoLog(shader, 1000, &len, log);
170 fprintf(stderr, "Error: compiling %s: %*s\n",
171 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
172 len, log);
173 exit(1);
174 }
175
176 return shader;
177}
178
179static void
180init_gl(struct window *window)
181{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100182 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600183 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100184 GLint status;
185
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100186 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
187 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
188
Scott Moreau3ea23d02012-06-13 17:42:21 -0600189 program = glCreateProgram();
190 glAttachShader(program, frag);
191 glAttachShader(program, vert);
192 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100193
Scott Moreau3ea23d02012-06-13 17:42:21 -0600194 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100195 if (!status) {
196 char log[1000];
197 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600198 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100199 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
200 exit(1);
201 }
202
Scott Moreau3ea23d02012-06-13 17:42:21 -0600203 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100204
205 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600206 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100207
Scott Moreau3ea23d02012-06-13 17:42:21 -0600208 glBindAttribLocation(program, window->gl.pos, "pos");
209 glBindAttribLocation(program, window->gl.col, "color");
210 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100211
212 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600213 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100214}
215
216static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300217handle_ping(void *data, struct wl_shell_surface *shell_surface,
218 uint32_t serial)
219{
220 wl_shell_surface_pong(shell_surface, serial);
221}
222
223static void
224handle_configure(void *data, struct wl_shell_surface *shell_surface,
225 uint32_t edges, int32_t width, int32_t height)
226{
227 struct window *window = data;
228
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300229 if (window->native)
230 wl_egl_window_resize(window->native, width, height, 0, 0);
231
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300232 window->geometry.width = width;
233 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300234
Scott Moreau1ee53e72012-08-30 14:44:15 -0600235 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300236 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300237}
238
239static void
240handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
241{
242}
243
244static const struct wl_shell_surface_listener shell_surface_listener = {
245 handle_ping,
246 handle_configure,
247 handle_popup_done
248};
249
250static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300251redraw(void *data, struct wl_callback *callback, uint32_t time);
252
253static void
254configure_callback(void *data, struct wl_callback *callback, uint32_t time)
255{
256 struct window *window = data;
257
258 wl_callback_destroy(callback);
259
260 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600261
262 if (window->callback == NULL)
263 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300264}
265
266static struct wl_callback_listener configure_callback_listener = {
267 configure_callback,
268};
269
270static void
271toggle_fullscreen(struct window *window, int fullscreen)
272{
273 struct wl_callback *callback;
274
275 window->fullscreen = fullscreen;
276 window->configured = 0;
277
278 if (fullscreen) {
279 wl_shell_surface_set_fullscreen(window->shell_surface,
280 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
281 0, NULL);
282 } else {
283 wl_shell_surface_set_toplevel(window->shell_surface);
284 handle_configure(window, window->shell_surface, 0,
285 window->window_size.width,
286 window->window_size.height);
287 }
288
289 callback = wl_display_sync(window->display->display);
290 wl_callback_add_listener(callback, &configure_callback_listener,
291 window);
292}
293
294static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100295create_surface(struct window *window)
296{
297 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500298 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200299
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500300 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200301 window->shell_surface = wl_shell_get_shell_surface(display->shell,
302 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300303
304 wl_shell_surface_add_listener(window->shell_surface,
305 &shell_surface_listener, window);
306
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500307 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400308 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300309 window->window_size.width,
310 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500311 window->egl_surface =
312 eglCreateWindowSurface(display->egl.dpy,
313 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500314 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100315
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600316 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
317
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500318 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
319 window->egl_surface, window->display->egl.ctx);
320 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300321
322 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100323}
324
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200325static void
326destroy_surface(struct window *window)
327{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000328 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
329 * on eglReleaseThread(). */
330 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
331 EGL_NO_CONTEXT);
332
333 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200334 wl_egl_window_destroy(window->native);
335
336 wl_shell_surface_destroy(window->shell_surface);
337 wl_surface_destroy(window->surface);
338
339 if (window->callback)
340 wl_callback_destroy(window->callback);
341}
342
Kristian Høgsberg33418202011-08-16 23:01:28 -0400343static const struct wl_callback_listener frame_listener;
344
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100345static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400346redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100347{
348 struct window *window = data;
349 static const GLfloat verts[3][2] = {
350 { -0.5, -0.5 },
351 { 0.5, -0.5 },
352 { 0, 0.5 }
353 };
354 static const GLfloat colors[3][3] = {
355 { 1, 0, 0 },
356 { 0, 1, 0 },
357 { 0, 0, 1 }
358 };
359 GLfloat angle;
360 GLfloat rotation[4][4] = {
361 { 1, 0, 0, 0 },
362 { 0, 1, 0, 0 },
363 { 0, 0, 1, 0 },
364 { 0, 0, 0, 1 }
365 };
366 static const int32_t speed_div = 5;
367 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400368 struct wl_region *region;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100369
Scott Moreau7e300db2012-08-31 03:18:15 -0600370 assert(window->callback == callback);
371 window->callback = NULL;
372
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300373 if (callback)
374 wl_callback_destroy(callback);
375
376 if (!window->configured)
377 return;
378
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100379 if (start_time == 0)
380 start_time = time;
381
382 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
383 rotation[0][0] = cos(angle);
384 rotation[0][2] = sin(angle);
385 rotation[2][0] = -sin(angle);
386 rotation[2][2] = cos(angle);
387
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300388 glViewport(0, 0, window->geometry.width, window->geometry.height);
389
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100390 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
391 (GLfloat *) rotation);
392
393 glClearColor(0.0, 0.0, 0.0, 0.5);
394 glClear(GL_COLOR_BUFFER_BIT);
395
396 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
397 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
398 glEnableVertexAttribArray(window->gl.pos);
399 glEnableVertexAttribArray(window->gl.col);
400
401 glDrawArrays(GL_TRIANGLES, 0, 3);
402
403 glDisableVertexAttribArray(window->gl.pos);
404 glDisableVertexAttribArray(window->gl.col);
405
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300406 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400407 region = wl_compositor_create_region(window->display->compositor);
408 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300409 window->geometry.width,
410 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400411 wl_surface_set_opaque_region(window->surface, region);
412 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700413 } else {
414 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400415 }
416
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200417 window->callback = wl_surface_frame(window->surface);
418 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300419
420 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100421}
422
Kristian Høgsberg33418202011-08-16 23:01:28 -0400423static const struct wl_callback_listener frame_listener = {
424 redraw
425};
426
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100427static void
Daniel Stone37816df2012-05-16 18:45:18 +0100428pointer_handle_enter(void *data, struct wl_pointer *pointer,
429 uint32_t serial, struct wl_surface *surface,
430 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300431{
432 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400433 struct wl_buffer *buffer;
434 struct wl_cursor *cursor = display->default_cursor;
435 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300436
437 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300438 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400439 else if (cursor) {
440 image = display->default_cursor->images[0];
441 buffer = wl_cursor_image_get_buffer(image);
442 wl_pointer_set_cursor(pointer, serial,
443 display->cursor_surface,
444 image->hotspot_x,
445 image->hotspot_y);
446 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
447 wl_surface_damage(display->cursor_surface, 0, 0,
448 image->width, image->height);
449 wl_surface_commit(display->cursor_surface);
450 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300451}
452
453static void
Daniel Stone37816df2012-05-16 18:45:18 +0100454pointer_handle_leave(void *data, struct wl_pointer *pointer,
455 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300456{
457}
458
459static void
Daniel Stone37816df2012-05-16 18:45:18 +0100460pointer_handle_motion(void *data, struct wl_pointer *pointer,
461 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300462{
463}
464
465static void
Daniel Stone37816df2012-05-16 18:45:18 +0100466pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
467 uint32_t serial, uint32_t time, uint32_t button,
468 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300469{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300470 struct display *display = data;
471
472 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
473 wl_shell_surface_move(display->window->shell_surface,
474 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300475}
476
477static void
Daniel Stone37816df2012-05-16 18:45:18 +0100478pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100479 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300480{
481}
482
Daniel Stone37816df2012-05-16 18:45:18 +0100483static const struct wl_pointer_listener pointer_listener = {
484 pointer_handle_enter,
485 pointer_handle_leave,
486 pointer_handle_motion,
487 pointer_handle_button,
488 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300489};
490
491static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300492keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
493 uint32_t format, int fd, uint32_t size)
494{
495}
496
497static void
498keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
499 uint32_t serial, struct wl_surface *surface,
500 struct wl_array *keys)
501{
502}
503
504static void
505keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
506 uint32_t serial, struct wl_surface *surface)
507{
508}
509
510static void
511keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
512 uint32_t serial, uint32_t time, uint32_t key,
513 uint32_t state)
514{
515 struct display *d = data;
516
517 if (key == KEY_F11 && state)
518 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400519 else if (key == KEY_ESC && state)
520 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300521}
522
523static void
524keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
525 uint32_t serial, uint32_t mods_depressed,
526 uint32_t mods_latched, uint32_t mods_locked,
527 uint32_t group)
528{
529}
530
531static const struct wl_keyboard_listener keyboard_listener = {
532 keyboard_handle_keymap,
533 keyboard_handle_enter,
534 keyboard_handle_leave,
535 keyboard_handle_key,
536 keyboard_handle_modifiers,
537};
538
539static void
Daniel Stone37816df2012-05-16 18:45:18 +0100540seat_handle_capabilities(void *data, struct wl_seat *seat,
541 enum wl_seat_capability caps)
542{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400543 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100544
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400545 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
546 d->pointer = wl_seat_get_pointer(seat);
547 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
548 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
549 wl_pointer_destroy(d->pointer);
550 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100551 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300552
553 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
554 d->keyboard = wl_seat_get_keyboard(seat);
555 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
556 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
557 wl_keyboard_destroy(d->keyboard);
558 d->keyboard = NULL;
559 }
Daniel Stone37816df2012-05-16 18:45:18 +0100560}
561
562static const struct wl_seat_listener seat_listener = {
563 seat_handle_capabilities,
564};
565
566static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400567registry_handle_global(void *data, struct wl_registry *registry,
568 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100569{
570 struct display *d = data;
571
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400572 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400573 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400574 wl_registry_bind(registry, name,
575 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400576 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400577 d->shell = wl_registry_bind(registry, name,
578 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100579 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400580 d->seat = wl_registry_bind(registry, name,
581 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400582 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400583 } else if (strcmp(interface, "wl_shm") == 0) {
584 d->shm = wl_registry_bind(registry, name,
585 &wl_shm_interface, 1);
586 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
587 d->default_cursor =
588 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400589 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100590}
591
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200592static void
593registry_handle_global_remove(void *data, struct wl_registry *registry,
594 uint32_t name)
595{
596}
597
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400598static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200599 registry_handle_global,
600 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400601};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100602
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200603static void
604signal_int(int signum)
605{
606 running = 0;
607}
608
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400609static void
610usage(int error_code)
611{
612 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
613 " -f\tRun in fullscreen mode\n"
614 " -o\tCreate an opaque surface\n"
615 " -h\tThis help text\n\n");
616
617 exit(error_code);
618}
619
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100620int
621main(int argc, char **argv)
622{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200623 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100624 struct display display = { 0 };
625 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400626 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100627
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100628 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300629 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300630 window.window_size.width = 250;
631 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100632
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400633 for (i = 1; i < argc; i++) {
634 if (strcmp("-f", argv[i]) == 0)
635 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400636 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300637 window.opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400638 else if (strcmp("-h", argv[i]) == 0)
639 usage(EXIT_SUCCESS);
640 else
641 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400642 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300643
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100644 display.display = wl_display_connect(NULL);
645 assert(display.display);
646
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400647 display.registry = wl_display_get_registry(display.display);
648 wl_registry_add_listener(display.registry,
649 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100650
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400651 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200652
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300653 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100654 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500655 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100656
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400657 display.cursor_surface =
658 wl_compositor_create_surface(display.compositor);
659
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200660 sigint.sa_handler = signal_int;
661 sigemptyset(&sigint.sa_mask);
662 sigint.sa_flags = SA_RESETHAND;
663 sigaction(SIGINT, &sigint, NULL);
664
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400665 while (running && ret != -1)
666 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500667
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200668 fprintf(stderr, "simple-egl exiting\n");
669
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200670 destroy_surface(&window);
671 fini_egl(&display);
672
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400673 wl_surface_destroy(display.cursor_surface);
674 if (display.cursor_theme)
675 wl_cursor_theme_destroy(display.cursor_theme);
676
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200677 if (display.shell)
678 wl_shell_destroy(display.shell);
679
680 if (display.compositor)
681 wl_compositor_destroy(display.compositor);
682
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200683 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200684 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500685 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200686
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100687 return 0;
688}