blob: 5b77b9dd79f780ba86c7b46fc2b1d12813e91175 [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>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010036#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010037#include <EGL/egl.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030039struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010040struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030041
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010042struct display {
43 struct wl_display *display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050044 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040045 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040046 struct wl_seat *seat;
47 struct wl_pointer *pointer;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030048 struct wl_keyboard *keyboard;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010049 struct {
50 EGLDisplay dpy;
51 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050052 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010053 } egl;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010054 uint32_t mask;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030055 struct window *window;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010056};
57
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030058struct geometry {
59 int width, height;
60};
61
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010062struct window {
63 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060064 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010065 struct {
66 GLuint fbo;
67 GLuint color_rbo;
68
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010069 GLuint rotation_uniform;
70
71 GLuint pos;
72 GLuint col;
73 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050074
75 struct wl_egl_window *native;
76 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020077 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050078 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020079 struct wl_callback *callback;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -040080 int fullscreen, configured, opaque;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010081};
82
83static const char *vert_shader_text =
84 "uniform mat4 rotation;\n"
85 "attribute vec4 pos;\n"
86 "attribute vec4 color;\n"
87 "varying vec4 v_color;\n"
88 "void main() {\n"
89 " gl_Position = rotation * pos;\n"
90 " v_color = color;\n"
91 "}\n";
92
93static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050094 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010095 "varying vec4 v_color;\n"
96 "void main() {\n"
97 " gl_FragColor = v_color;\n"
98 "}\n";
99
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400100static int running = 1;
101
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100102static void
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400103init_egl(struct display *display, int opaque)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100104{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500105 static const EGLint context_attribs[] = {
106 EGL_CONTEXT_CLIENT_VERSION, 2,
107 EGL_NONE
108 };
109
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300110 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500111 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500112 EGL_RED_SIZE, 1,
113 EGL_GREEN_SIZE, 1,
114 EGL_BLUE_SIZE, 1,
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400115 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500116 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
117 EGL_NONE
118 };
119
120 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100121 EGLBoolean ret;
122
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400123 if (opaque)
124 config_attribs[9] = 0;
125
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400126 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100127 assert(display->egl.dpy);
128
129 ret = eglInitialize(display->egl.dpy, &major, &minor);
130 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500131 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100132 assert(ret == EGL_TRUE);
133
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300134 ret = eglChooseConfig(display->egl.dpy, config_attribs,
135 &display->egl.conf, 1, &n);
136 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500137
138 display->egl.ctx = eglCreateContext(display->egl.dpy,
139 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500140 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100141 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500142
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100143}
144
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200145static void
146fini_egl(struct display *display)
147{
148 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
149 * on eglReleaseThread(). */
150 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
151 EGL_NO_CONTEXT);
152
153 eglTerminate(display->egl.dpy);
154 eglReleaseThread();
155}
156
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100157static GLuint
158create_shader(struct window *window, const char *source, GLenum shader_type)
159{
160 GLuint shader;
161 GLint status;
162
163 shader = glCreateShader(shader_type);
164 assert(shader != 0);
165
166 glShaderSource(shader, 1, (const char **) &source, NULL);
167 glCompileShader(shader);
168
169 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
170 if (!status) {
171 char log[1000];
172 GLsizei len;
173 glGetShaderInfoLog(shader, 1000, &len, log);
174 fprintf(stderr, "Error: compiling %s: %*s\n",
175 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
176 len, log);
177 exit(1);
178 }
179
180 return shader;
181}
182
183static void
184init_gl(struct window *window)
185{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100186 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600187 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100188 GLint status;
189
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100190 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
191 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
192
Scott Moreau3ea23d02012-06-13 17:42:21 -0600193 program = glCreateProgram();
194 glAttachShader(program, frag);
195 glAttachShader(program, vert);
196 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100197
Scott Moreau3ea23d02012-06-13 17:42:21 -0600198 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100199 if (!status) {
200 char log[1000];
201 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600202 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100203 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
204 exit(1);
205 }
206
Scott Moreau3ea23d02012-06-13 17:42:21 -0600207 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100208
209 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600210 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100211
Scott Moreau3ea23d02012-06-13 17:42:21 -0600212 glBindAttribLocation(program, window->gl.pos, "pos");
213 glBindAttribLocation(program, window->gl.col, "color");
214 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100215
216 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600217 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100218}
219
220static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300221handle_ping(void *data, struct wl_shell_surface *shell_surface,
222 uint32_t serial)
223{
224 wl_shell_surface_pong(shell_surface, serial);
225}
226
227static void
228handle_configure(void *data, struct wl_shell_surface *shell_surface,
229 uint32_t edges, int32_t width, int32_t height)
230{
231 struct window *window = data;
232
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300233 if (window->native)
234 wl_egl_window_resize(window->native, width, height, 0, 0);
235
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300236 window->geometry.width = width;
237 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300238
Scott Moreau1ee53e72012-08-30 14:44:15 -0600239 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300240 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300241}
242
243static void
244handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
245{
246}
247
248static const struct wl_shell_surface_listener shell_surface_listener = {
249 handle_ping,
250 handle_configure,
251 handle_popup_done
252};
253
254static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300255redraw(void *data, struct wl_callback *callback, uint32_t time);
256
257static void
258configure_callback(void *data, struct wl_callback *callback, uint32_t time)
259{
260 struct window *window = data;
261
262 wl_callback_destroy(callback);
263
264 window->configured = 1;
265 redraw(data, NULL, time);
266}
267
268static struct wl_callback_listener configure_callback_listener = {
269 configure_callback,
270};
271
272static void
273toggle_fullscreen(struct window *window, int fullscreen)
274{
275 struct wl_callback *callback;
276
277 window->fullscreen = fullscreen;
278 window->configured = 0;
279
280 if (fullscreen) {
281 wl_shell_surface_set_fullscreen(window->shell_surface,
282 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
283 0, NULL);
284 } else {
285 wl_shell_surface_set_toplevel(window->shell_surface);
286 handle_configure(window, window->shell_surface, 0,
287 window->window_size.width,
288 window->window_size.height);
289 }
290
291 callback = wl_display_sync(window->display->display);
292 wl_callback_add_listener(callback, &configure_callback_listener,
293 window);
294}
295
296static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100297create_surface(struct window *window)
298{
299 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500300 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200301
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500302 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200303 window->shell_surface = wl_shell_get_shell_surface(display->shell,
304 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300305
306 wl_shell_surface_add_listener(window->shell_surface,
307 &shell_surface_listener, window);
308
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500309 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400310 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300311 window->window_size.width,
312 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500313 window->egl_surface =
314 eglCreateWindowSurface(display->egl.dpy,
315 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500316 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100317
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{
328 wl_egl_window_destroy(window->native);
329
330 wl_shell_surface_destroy(window->shell_surface);
331 wl_surface_destroy(window->surface);
332
333 if (window->callback)
334 wl_callback_destroy(window->callback);
335}
336
Kristian Høgsberg33418202011-08-16 23:01:28 -0400337static const struct wl_callback_listener frame_listener;
338
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100339static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400340redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100341{
342 struct window *window = data;
343 static const GLfloat verts[3][2] = {
344 { -0.5, -0.5 },
345 { 0.5, -0.5 },
346 { 0, 0.5 }
347 };
348 static const GLfloat colors[3][3] = {
349 { 1, 0, 0 },
350 { 0, 1, 0 },
351 { 0, 0, 1 }
352 };
353 GLfloat angle;
354 GLfloat rotation[4][4] = {
355 { 1, 0, 0, 0 },
356 { 0, 1, 0, 0 },
357 { 0, 0, 1, 0 },
358 { 0, 0, 0, 1 }
359 };
360 static const int32_t speed_div = 5;
361 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400362 struct wl_region *region;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100363
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300364 if (callback)
365 wl_callback_destroy(callback);
366
367 if (!window->configured)
368 return;
369
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100370 if (start_time == 0)
371 start_time = time;
372
373 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
374 rotation[0][0] = cos(angle);
375 rotation[0][2] = sin(angle);
376 rotation[2][0] = -sin(angle);
377 rotation[2][2] = cos(angle);
378
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300379 glViewport(0, 0, window->geometry.width, window->geometry.height);
380
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100381 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
382 (GLfloat *) rotation);
383
384 glClearColor(0.0, 0.0, 0.0, 0.5);
385 glClear(GL_COLOR_BUFFER_BIT);
386
387 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
388 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
389 glEnableVertexAttribArray(window->gl.pos);
390 glEnableVertexAttribArray(window->gl.col);
391
392 glDrawArrays(GL_TRIANGLES, 0, 3);
393
394 glDisableVertexAttribArray(window->gl.pos);
395 glDisableVertexAttribArray(window->gl.col);
396
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500397 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400398
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400399 if (window->opaque) {
400 region = wl_compositor_create_region(window->display->compositor);
401 wl_region_add(region, 0, 0,
402 window->window_size.width,
403 window->window_size.height);
404 wl_surface_set_opaque_region(window->surface, region);
405 wl_region_destroy(region);
406 }
407
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200408 window->callback = wl_surface_frame(window->surface);
409 wl_callback_add_listener(window->callback, &frame_listener, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100410}
411
Kristian Høgsberg33418202011-08-16 23:01:28 -0400412static const struct wl_callback_listener frame_listener = {
413 redraw
414};
415
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100416static void
Daniel Stone37816df2012-05-16 18:45:18 +0100417pointer_handle_enter(void *data, struct wl_pointer *pointer,
418 uint32_t serial, struct wl_surface *surface,
419 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300420{
421 struct display *display = data;
422
423 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300424 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300425}
426
427static void
Daniel Stone37816df2012-05-16 18:45:18 +0100428pointer_handle_leave(void *data, struct wl_pointer *pointer,
429 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300430{
431}
432
433static void
Daniel Stone37816df2012-05-16 18:45:18 +0100434pointer_handle_motion(void *data, struct wl_pointer *pointer,
435 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300436{
437}
438
439static void
Daniel Stone37816df2012-05-16 18:45:18 +0100440pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
441 uint32_t serial, uint32_t time, uint32_t button,
442 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300443{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300444 struct display *display = data;
445
446 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
447 wl_shell_surface_move(display->window->shell_surface,
448 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300449}
450
451static void
Daniel Stone37816df2012-05-16 18:45:18 +0100452pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100453 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300454{
455}
456
Daniel Stone37816df2012-05-16 18:45:18 +0100457static const struct wl_pointer_listener pointer_listener = {
458 pointer_handle_enter,
459 pointer_handle_leave,
460 pointer_handle_motion,
461 pointer_handle_button,
462 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300463};
464
465static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300466keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
467 uint32_t format, int fd, uint32_t size)
468{
469}
470
471static void
472keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
473 uint32_t serial, struct wl_surface *surface,
474 struct wl_array *keys)
475{
476}
477
478static void
479keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
480 uint32_t serial, struct wl_surface *surface)
481{
482}
483
484static void
485keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
486 uint32_t serial, uint32_t time, uint32_t key,
487 uint32_t state)
488{
489 struct display *d = data;
490
491 if (key == KEY_F11 && state)
492 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400493 else if (key == KEY_ESC && state)
494 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300495}
496
497static void
498keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
499 uint32_t serial, uint32_t mods_depressed,
500 uint32_t mods_latched, uint32_t mods_locked,
501 uint32_t group)
502{
503}
504
505static const struct wl_keyboard_listener keyboard_listener = {
506 keyboard_handle_keymap,
507 keyboard_handle_enter,
508 keyboard_handle_leave,
509 keyboard_handle_key,
510 keyboard_handle_modifiers,
511};
512
513static void
Daniel Stone37816df2012-05-16 18:45:18 +0100514seat_handle_capabilities(void *data, struct wl_seat *seat,
515 enum wl_seat_capability caps)
516{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400517 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100518
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400519 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
520 d->pointer = wl_seat_get_pointer(seat);
521 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
522 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
523 wl_pointer_destroy(d->pointer);
524 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100525 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300526
527 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
528 d->keyboard = wl_seat_get_keyboard(seat);
529 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
530 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
531 wl_keyboard_destroy(d->keyboard);
532 d->keyboard = NULL;
533 }
Daniel Stone37816df2012-05-16 18:45:18 +0100534}
535
536static const struct wl_seat_listener seat_listener = {
537 seat_handle_capabilities,
538};
539
540static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100541display_handle_global(struct wl_display *display, uint32_t id,
542 const char *interface, uint32_t version, void *data)
543{
544 struct display *d = data;
545
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400546 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400547 d->compositor =
548 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400549 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400550 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone37816df2012-05-16 18:45:18 +0100551 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400552 d->seat = wl_display_bind(d->display, id, &wl_seat_interface);
553 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400554 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100555}
556
557static int
558event_mask_update(uint32_t mask, void *data)
559{
560 struct display *d = data;
561
562 d->mask = mask;
563
564 return 0;
565}
566
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200567static void
568signal_int(int signum)
569{
570 running = 0;
571}
572
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400573static void
574usage(int error_code)
575{
576 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
577 " -f\tRun in fullscreen mode\n"
578 " -o\tCreate an opaque surface\n"
579 " -h\tThis help text\n\n");
580
581 exit(error_code);
582}
583
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100584int
585main(int argc, char **argv)
586{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200587 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100588 struct display display = { 0 };
589 struct window window = { 0 };
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400590 int i;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100591
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100592 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300593 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300594 window.window_size.width = 250;
595 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100596
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400597 for (i = 1; i < argc; i++) {
598 if (strcmp("-f", argv[i]) == 0)
599 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400600 else if (strcmp("-o", argv[i]) == 0)
601 window.opaque = 1;
602 else if (strcmp("-h", argv[i]) == 0)
603 usage(EXIT_SUCCESS);
604 else
605 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400606 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300607
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100608 display.display = wl_display_connect(NULL);
609 assert(display.display);
610
611 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500612 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100613
Benjamin Franzke65e50512011-05-31 11:36:31 +0200614 wl_display_get_fd(display.display, event_mask_update, &display);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400615 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200616
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400617 if (window.fullscreen)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400618 window.opaque = 1;
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400619
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400620 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100621 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500622 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100623
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200624 sigint.sa_handler = signal_int;
625 sigemptyset(&sigint.sa_mask);
626 sigint.sa_flags = SA_RESETHAND;
627 sigaction(SIGINT, &sigint, NULL);
628
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200629 while (running)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100630 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500631
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200632 fprintf(stderr, "simple-egl exiting\n");
633
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200634 destroy_surface(&window);
635 fini_egl(&display);
636
637 if (display.shell)
638 wl_shell_destroy(display.shell);
639
640 if (display.compositor)
641 wl_compositor_destroy(display.compositor);
642
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200643 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500644 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200645
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100646 return 0;
647}