blob: 808c2eea02129a238038b3d417752cd882b4e136 [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;
Scott Moreau7e300db2012-08-31 03:18:15 -0600265
266 if (window->callback == NULL)
267 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300268}
269
270static struct wl_callback_listener configure_callback_listener = {
271 configure_callback,
272};
273
274static void
275toggle_fullscreen(struct window *window, int fullscreen)
276{
277 struct wl_callback *callback;
278
279 window->fullscreen = fullscreen;
280 window->configured = 0;
281
282 if (fullscreen) {
Scott Moreau6a615d22012-08-30 14:44:16 -0600283 window->opaque = 1;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300284 wl_shell_surface_set_fullscreen(window->shell_surface,
285 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
286 0, NULL);
287 } else {
Scott Moreau6a615d22012-08-30 14:44:16 -0600288 window->opaque = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300289 wl_shell_surface_set_toplevel(window->shell_surface);
290 handle_configure(window, window->shell_surface, 0,
291 window->window_size.width,
292 window->window_size.height);
293 }
294
295 callback = wl_display_sync(window->display->display);
296 wl_callback_add_listener(callback, &configure_callback_listener,
297 window);
298}
299
300static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100301create_surface(struct window *window)
302{
303 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500304 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200305
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500306 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200307 window->shell_surface = wl_shell_get_shell_surface(display->shell,
308 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300309
310 wl_shell_surface_add_listener(window->shell_surface,
311 &shell_surface_listener, window);
312
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500313 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400314 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300315 window->window_size.width,
316 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500317 window->egl_surface =
318 eglCreateWindowSurface(display->egl.dpy,
319 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500320 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100321
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500322 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
323 window->egl_surface, window->display->egl.ctx);
324 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300325
326 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100327}
328
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200329static void
330destroy_surface(struct window *window)
331{
332 wl_egl_window_destroy(window->native);
333
334 wl_shell_surface_destroy(window->shell_surface);
335 wl_surface_destroy(window->surface);
336
337 if (window->callback)
338 wl_callback_destroy(window->callback);
339}
340
Kristian Høgsberg33418202011-08-16 23:01:28 -0400341static const struct wl_callback_listener frame_listener;
342
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100343static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400344redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100345{
346 struct window *window = data;
347 static const GLfloat verts[3][2] = {
348 { -0.5, -0.5 },
349 { 0.5, -0.5 },
350 { 0, 0.5 }
351 };
352 static const GLfloat colors[3][3] = {
353 { 1, 0, 0 },
354 { 0, 1, 0 },
355 { 0, 0, 1 }
356 };
357 GLfloat angle;
358 GLfloat rotation[4][4] = {
359 { 1, 0, 0, 0 },
360 { 0, 1, 0, 0 },
361 { 0, 0, 1, 0 },
362 { 0, 0, 0, 1 }
363 };
364 static const int32_t speed_div = 5;
365 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400366 struct wl_region *region;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100367
Scott Moreau7e300db2012-08-31 03:18:15 -0600368 assert(window->callback == callback);
369 window->callback = NULL;
370
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300371 if (callback)
372 wl_callback_destroy(callback);
373
374 if (!window->configured)
375 return;
376
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100377 if (start_time == 0)
378 start_time = time;
379
380 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
381 rotation[0][0] = cos(angle);
382 rotation[0][2] = sin(angle);
383 rotation[2][0] = -sin(angle);
384 rotation[2][2] = cos(angle);
385
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300386 glViewport(0, 0, window->geometry.width, window->geometry.height);
387
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100388 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
389 (GLfloat *) rotation);
390
391 glClearColor(0.0, 0.0, 0.0, 0.5);
392 glClear(GL_COLOR_BUFFER_BIT);
393
394 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
395 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
396 glEnableVertexAttribArray(window->gl.pos);
397 glEnableVertexAttribArray(window->gl.col);
398
399 glDrawArrays(GL_TRIANGLES, 0, 3);
400
401 glDisableVertexAttribArray(window->gl.pos);
402 glDisableVertexAttribArray(window->gl.col);
403
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500404 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400405
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400406 if (window->opaque) {
407 region = wl_compositor_create_region(window->display->compositor);
408 wl_region_add(region, 0, 0,
409 window->window_size.width,
410 window->window_size.height);
411 wl_surface_set_opaque_region(window->surface, region);
412 wl_region_destroy(region);
413 }
414
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200415 window->callback = wl_surface_frame(window->surface);
416 wl_callback_add_listener(window->callback, &frame_listener, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100417}
418
Kristian Høgsberg33418202011-08-16 23:01:28 -0400419static const struct wl_callback_listener frame_listener = {
420 redraw
421};
422
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100423static void
Daniel Stone37816df2012-05-16 18:45:18 +0100424pointer_handle_enter(void *data, struct wl_pointer *pointer,
425 uint32_t serial, struct wl_surface *surface,
426 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300427{
428 struct display *display = data;
429
430 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300431 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300432}
433
434static void
Daniel Stone37816df2012-05-16 18:45:18 +0100435pointer_handle_leave(void *data, struct wl_pointer *pointer,
436 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300437{
438}
439
440static void
Daniel Stone37816df2012-05-16 18:45:18 +0100441pointer_handle_motion(void *data, struct wl_pointer *pointer,
442 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300443{
444}
445
446static void
Daniel Stone37816df2012-05-16 18:45:18 +0100447pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
448 uint32_t serial, uint32_t time, uint32_t button,
449 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300450{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300451 struct display *display = data;
452
453 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
454 wl_shell_surface_move(display->window->shell_surface,
455 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300456}
457
458static void
Daniel Stone37816df2012-05-16 18:45:18 +0100459pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100460 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300461{
462}
463
Daniel Stone37816df2012-05-16 18:45:18 +0100464static const struct wl_pointer_listener pointer_listener = {
465 pointer_handle_enter,
466 pointer_handle_leave,
467 pointer_handle_motion,
468 pointer_handle_button,
469 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300470};
471
472static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300473keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
474 uint32_t format, int fd, uint32_t size)
475{
476}
477
478static void
479keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
480 uint32_t serial, struct wl_surface *surface,
481 struct wl_array *keys)
482{
483}
484
485static void
486keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
487 uint32_t serial, struct wl_surface *surface)
488{
489}
490
491static void
492keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
493 uint32_t serial, uint32_t time, uint32_t key,
494 uint32_t state)
495{
496 struct display *d = data;
497
498 if (key == KEY_F11 && state)
499 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400500 else if (key == KEY_ESC && state)
501 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300502}
503
504static void
505keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
506 uint32_t serial, uint32_t mods_depressed,
507 uint32_t mods_latched, uint32_t mods_locked,
508 uint32_t group)
509{
510}
511
512static const struct wl_keyboard_listener keyboard_listener = {
513 keyboard_handle_keymap,
514 keyboard_handle_enter,
515 keyboard_handle_leave,
516 keyboard_handle_key,
517 keyboard_handle_modifiers,
518};
519
520static void
Daniel Stone37816df2012-05-16 18:45:18 +0100521seat_handle_capabilities(void *data, struct wl_seat *seat,
522 enum wl_seat_capability caps)
523{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400524 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100525
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400526 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
527 d->pointer = wl_seat_get_pointer(seat);
528 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
529 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
530 wl_pointer_destroy(d->pointer);
531 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100532 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300533
534 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
535 d->keyboard = wl_seat_get_keyboard(seat);
536 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
537 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
538 wl_keyboard_destroy(d->keyboard);
539 d->keyboard = NULL;
540 }
Daniel Stone37816df2012-05-16 18:45:18 +0100541}
542
543static const struct wl_seat_listener seat_listener = {
544 seat_handle_capabilities,
545};
546
547static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100548display_handle_global(struct wl_display *display, uint32_t id,
549 const char *interface, uint32_t version, void *data)
550{
551 struct display *d = data;
552
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400553 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400554 d->compositor =
555 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400556 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400557 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone37816df2012-05-16 18:45:18 +0100558 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400559 d->seat = wl_display_bind(d->display, id, &wl_seat_interface);
560 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400561 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100562}
563
564static int
565event_mask_update(uint32_t mask, void *data)
566{
567 struct display *d = data;
568
569 d->mask = mask;
570
571 return 0;
572}
573
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200574static void
575signal_int(int signum)
576{
577 running = 0;
578}
579
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400580static void
581usage(int error_code)
582{
583 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
584 " -f\tRun in fullscreen mode\n"
585 " -o\tCreate an opaque surface\n"
586 " -h\tThis help text\n\n");
587
588 exit(error_code);
589}
590
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100591int
592main(int argc, char **argv)
593{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200594 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100595 struct display display = { 0 };
596 struct window window = { 0 };
Scott Moreau6a615d22012-08-30 14:44:16 -0600597 int i, opaque = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100598
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100599 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300600 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300601 window.window_size.width = 250;
602 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100603
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400604 for (i = 1; i < argc; i++) {
605 if (strcmp("-f", argv[i]) == 0)
606 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400607 else if (strcmp("-o", argv[i]) == 0)
Scott Moreau6a615d22012-08-30 14:44:16 -0600608 opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400609 else if (strcmp("-h", argv[i]) == 0)
610 usage(EXIT_SUCCESS);
611 else
612 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400613 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300614
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100615 display.display = wl_display_connect(NULL);
616 assert(display.display);
617
618 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500619 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100620
Benjamin Franzke65e50512011-05-31 11:36:31 +0200621 wl_display_get_fd(display.display, event_mask_update, &display);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400622 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200623
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400624 if (window.fullscreen)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400625 window.opaque = 1;
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400626
Scott Moreau6a615d22012-08-30 14:44:16 -0600627 init_egl(&display, opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100628 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500629 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100630
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200631 sigint.sa_handler = signal_int;
632 sigemptyset(&sigint.sa_mask);
633 sigint.sa_flags = SA_RESETHAND;
634 sigaction(SIGINT, &sigint, NULL);
635
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200636 while (running)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100637 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500638
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200639 fprintf(stderr, "simple-egl exiting\n");
640
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200641 destroy_surface(&window);
642 fini_egl(&display);
643
644 if (display.shell)
645 wl_shell_destroy(display.shell);
646
647 if (display.compositor)
648 wl_compositor_destroy(display.compositor);
649
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200650 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500651 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200652
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100653 return 0;
654}