blob: 26ebe5ce725a722146ccfb0bc38be8182a3a902b [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{
149 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
150 * on eglReleaseThread(). */
151 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
152 EGL_NO_CONTEXT);
153
154 eglTerminate(display->egl.dpy);
155 eglReleaseThread();
156}
157
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100158static GLuint
159create_shader(struct window *window, const char *source, GLenum shader_type)
160{
161 GLuint shader;
162 GLint status;
163
164 shader = glCreateShader(shader_type);
165 assert(shader != 0);
166
167 glShaderSource(shader, 1, (const char **) &source, NULL);
168 glCompileShader(shader);
169
170 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
171 if (!status) {
172 char log[1000];
173 GLsizei len;
174 glGetShaderInfoLog(shader, 1000, &len, log);
175 fprintf(stderr, "Error: compiling %s: %*s\n",
176 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
177 len, log);
178 exit(1);
179 }
180
181 return shader;
182}
183
184static void
185init_gl(struct window *window)
186{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100187 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600188 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100189 GLint status;
190
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100191 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
192 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
193
Scott Moreau3ea23d02012-06-13 17:42:21 -0600194 program = glCreateProgram();
195 glAttachShader(program, frag);
196 glAttachShader(program, vert);
197 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100198
Scott Moreau3ea23d02012-06-13 17:42:21 -0600199 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100200 if (!status) {
201 char log[1000];
202 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600203 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100204 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
205 exit(1);
206 }
207
Scott Moreau3ea23d02012-06-13 17:42:21 -0600208 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100209
210 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600211 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100212
Scott Moreau3ea23d02012-06-13 17:42:21 -0600213 glBindAttribLocation(program, window->gl.pos, "pos");
214 glBindAttribLocation(program, window->gl.col, "color");
215 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100216
217 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600218 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100219}
220
221static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300222handle_ping(void *data, struct wl_shell_surface *shell_surface,
223 uint32_t serial)
224{
225 wl_shell_surface_pong(shell_surface, serial);
226}
227
228static void
229handle_configure(void *data, struct wl_shell_surface *shell_surface,
230 uint32_t edges, int32_t width, int32_t height)
231{
232 struct window *window = data;
233
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300234 if (window->native)
235 wl_egl_window_resize(window->native, width, height, 0, 0);
236
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300237 window->geometry.width = width;
238 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300239
Scott Moreau1ee53e72012-08-30 14:44:15 -0600240 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300241 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300242}
243
244static void
245handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
246{
247}
248
249static const struct wl_shell_surface_listener shell_surface_listener = {
250 handle_ping,
251 handle_configure,
252 handle_popup_done
253};
254
255static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300256redraw(void *data, struct wl_callback *callback, uint32_t time);
257
258static void
259configure_callback(void *data, struct wl_callback *callback, uint32_t time)
260{
261 struct window *window = data;
262
263 wl_callback_destroy(callback);
264
265 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600266
267 if (window->callback == NULL)
268 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300269}
270
271static struct wl_callback_listener configure_callback_listener = {
272 configure_callback,
273};
274
275static void
276toggle_fullscreen(struct window *window, int fullscreen)
277{
278 struct wl_callback *callback;
279
280 window->fullscreen = fullscreen;
281 window->configured = 0;
282
283 if (fullscreen) {
284 wl_shell_surface_set_fullscreen(window->shell_surface,
285 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
286 0, NULL);
287 } else {
288 wl_shell_surface_set_toplevel(window->shell_surface);
289 handle_configure(window, window->shell_surface, 0,
290 window->window_size.width,
291 window->window_size.height);
292 }
293
294 callback = wl_display_sync(window->display->display);
295 wl_callback_add_listener(callback, &configure_callback_listener,
296 window);
297}
298
299static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100300create_surface(struct window *window)
301{
302 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500303 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200304
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500305 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200306 window->shell_surface = wl_shell_get_shell_surface(display->shell,
307 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300308
309 wl_shell_surface_add_listener(window->shell_surface,
310 &shell_surface_listener, window);
311
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500312 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400313 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300314 window->window_size.width,
315 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500316 window->egl_surface =
317 eglCreateWindowSurface(display->egl.dpy,
318 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500319 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100320
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600321 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
322
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500323 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
324 window->egl_surface, window->display->egl.ctx);
325 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300326
327 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100328}
329
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200330static void
331destroy_surface(struct window *window)
332{
333 wl_egl_window_destroy(window->native);
334
335 wl_shell_surface_destroy(window->shell_surface);
336 wl_surface_destroy(window->surface);
337
338 if (window->callback)
339 wl_callback_destroy(window->callback);
340}
341
Kristian Høgsberg33418202011-08-16 23:01:28 -0400342static const struct wl_callback_listener frame_listener;
343
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100344static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400345redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100346{
347 struct window *window = data;
348 static const GLfloat verts[3][2] = {
349 { -0.5, -0.5 },
350 { 0.5, -0.5 },
351 { 0, 0.5 }
352 };
353 static const GLfloat colors[3][3] = {
354 { 1, 0, 0 },
355 { 0, 1, 0 },
356 { 0, 0, 1 }
357 };
358 GLfloat angle;
359 GLfloat rotation[4][4] = {
360 { 1, 0, 0, 0 },
361 { 0, 1, 0, 0 },
362 { 0, 0, 1, 0 },
363 { 0, 0, 0, 1 }
364 };
365 static const int32_t speed_div = 5;
366 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400367 struct wl_region *region;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100368
Scott Moreau7e300db2012-08-31 03:18:15 -0600369 assert(window->callback == callback);
370 window->callback = NULL;
371
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300372 if (callback)
373 wl_callback_destroy(callback);
374
375 if (!window->configured)
376 return;
377
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100378 if (start_time == 0)
379 start_time = time;
380
381 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
382 rotation[0][0] = cos(angle);
383 rotation[0][2] = sin(angle);
384 rotation[2][0] = -sin(angle);
385 rotation[2][2] = cos(angle);
386
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300387 glViewport(0, 0, window->geometry.width, window->geometry.height);
388
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100389 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
390 (GLfloat *) rotation);
391
392 glClearColor(0.0, 0.0, 0.0, 0.5);
393 glClear(GL_COLOR_BUFFER_BIT);
394
395 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
396 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
397 glEnableVertexAttribArray(window->gl.pos);
398 glEnableVertexAttribArray(window->gl.col);
399
400 glDrawArrays(GL_TRIANGLES, 0, 3);
401
402 glDisableVertexAttribArray(window->gl.pos);
403 glDisableVertexAttribArray(window->gl.col);
404
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300405 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400406 region = wl_compositor_create_region(window->display->compositor);
407 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300408 window->geometry.width,
409 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400410 wl_surface_set_opaque_region(window->surface, region);
411 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700412 } else {
413 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400414 }
415
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200416 window->callback = wl_surface_frame(window->surface);
417 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300418
419 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100420}
421
Kristian Høgsberg33418202011-08-16 23:01:28 -0400422static const struct wl_callback_listener frame_listener = {
423 redraw
424};
425
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100426static void
Daniel Stone37816df2012-05-16 18:45:18 +0100427pointer_handle_enter(void *data, struct wl_pointer *pointer,
428 uint32_t serial, struct wl_surface *surface,
429 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300430{
431 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400432 struct wl_buffer *buffer;
433 struct wl_cursor *cursor = display->default_cursor;
434 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300435
436 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300437 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400438 else if (cursor) {
439 image = display->default_cursor->images[0];
440 buffer = wl_cursor_image_get_buffer(image);
441 wl_pointer_set_cursor(pointer, serial,
442 display->cursor_surface,
443 image->hotspot_x,
444 image->hotspot_y);
445 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
446 wl_surface_damage(display->cursor_surface, 0, 0,
447 image->width, image->height);
448 wl_surface_commit(display->cursor_surface);
449 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300450}
451
452static void
Daniel Stone37816df2012-05-16 18:45:18 +0100453pointer_handle_leave(void *data, struct wl_pointer *pointer,
454 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300455{
456}
457
458static void
Daniel Stone37816df2012-05-16 18:45:18 +0100459pointer_handle_motion(void *data, struct wl_pointer *pointer,
460 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300461{
462}
463
464static void
Daniel Stone37816df2012-05-16 18:45:18 +0100465pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
466 uint32_t serial, uint32_t time, uint32_t button,
467 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300468{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300469 struct display *display = data;
470
471 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
472 wl_shell_surface_move(display->window->shell_surface,
473 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300474}
475
476static void
Daniel Stone37816df2012-05-16 18:45:18 +0100477pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100478 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300479{
480}
481
Daniel Stone37816df2012-05-16 18:45:18 +0100482static const struct wl_pointer_listener pointer_listener = {
483 pointer_handle_enter,
484 pointer_handle_leave,
485 pointer_handle_motion,
486 pointer_handle_button,
487 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300488};
489
490static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300491keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
492 uint32_t format, int fd, uint32_t size)
493{
494}
495
496static void
497keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
498 uint32_t serial, struct wl_surface *surface,
499 struct wl_array *keys)
500{
501}
502
503static void
504keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
505 uint32_t serial, struct wl_surface *surface)
506{
507}
508
509static void
510keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
511 uint32_t serial, uint32_t time, uint32_t key,
512 uint32_t state)
513{
514 struct display *d = data;
515
516 if (key == KEY_F11 && state)
517 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400518 else if (key == KEY_ESC && state)
519 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300520}
521
522static void
523keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
524 uint32_t serial, uint32_t mods_depressed,
525 uint32_t mods_latched, uint32_t mods_locked,
526 uint32_t group)
527{
528}
529
530static const struct wl_keyboard_listener keyboard_listener = {
531 keyboard_handle_keymap,
532 keyboard_handle_enter,
533 keyboard_handle_leave,
534 keyboard_handle_key,
535 keyboard_handle_modifiers,
536};
537
538static void
Daniel Stone37816df2012-05-16 18:45:18 +0100539seat_handle_capabilities(void *data, struct wl_seat *seat,
540 enum wl_seat_capability caps)
541{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400542 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100543
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400544 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
545 d->pointer = wl_seat_get_pointer(seat);
546 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
547 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
548 wl_pointer_destroy(d->pointer);
549 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100550 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300551
552 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
553 d->keyboard = wl_seat_get_keyboard(seat);
554 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
555 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
556 wl_keyboard_destroy(d->keyboard);
557 d->keyboard = NULL;
558 }
Daniel Stone37816df2012-05-16 18:45:18 +0100559}
560
561static const struct wl_seat_listener seat_listener = {
562 seat_handle_capabilities,
563};
564
565static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400566registry_handle_global(void *data, struct wl_registry *registry,
567 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100568{
569 struct display *d = data;
570
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400571 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400572 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400573 wl_registry_bind(registry, name,
574 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400575 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400576 d->shell = wl_registry_bind(registry, name,
577 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100578 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400579 d->seat = wl_registry_bind(registry, name,
580 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400581 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400582 } else if (strcmp(interface, "wl_shm") == 0) {
583 d->shm = wl_registry_bind(registry, name,
584 &wl_shm_interface, 1);
585 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
586 d->default_cursor =
587 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400588 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100589}
590
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200591static void
592registry_handle_global_remove(void *data, struct wl_registry *registry,
593 uint32_t name)
594{
595}
596
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400597static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200598 registry_handle_global,
599 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400600};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100601
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200602static void
603signal_int(int signum)
604{
605 running = 0;
606}
607
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400608static void
609usage(int error_code)
610{
611 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
612 " -f\tRun in fullscreen mode\n"
613 " -o\tCreate an opaque surface\n"
614 " -h\tThis help text\n\n");
615
616 exit(error_code);
617}
618
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100619int
620main(int argc, char **argv)
621{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200622 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100623 struct display display = { 0 };
624 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400625 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100626
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100627 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300628 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300629 window.window_size.width = 250;
630 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100631
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400632 for (i = 1; i < argc; i++) {
633 if (strcmp("-f", argv[i]) == 0)
634 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400635 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300636 window.opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400637 else if (strcmp("-h", argv[i]) == 0)
638 usage(EXIT_SUCCESS);
639 else
640 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400641 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300642
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100643 display.display = wl_display_connect(NULL);
644 assert(display.display);
645
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400646 display.registry = wl_display_get_registry(display.display);
647 wl_registry_add_listener(display.registry,
648 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100649
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400650 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200651
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300652 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100653 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500654 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100655
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400656 display.cursor_surface =
657 wl_compositor_create_surface(display.compositor);
658
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200659 sigint.sa_handler = signal_int;
660 sigemptyset(&sigint.sa_mask);
661 sigint.sa_flags = SA_RESETHAND;
662 sigaction(SIGINT, &sigint, NULL);
663
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400664 while (running && ret != -1)
665 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500666
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200667 fprintf(stderr, "simple-egl exiting\n");
668
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200669 destroy_surface(&window);
670 fini_egl(&display);
671
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400672 wl_surface_destroy(display.cursor_surface);
673 if (display.cursor_theme)
674 wl_cursor_theme_destroy(display.cursor_theme);
675
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200676 if (display.shell)
677 wl_shell_destroy(display.shell);
678
679 if (display.compositor)
680 wl_compositor_destroy(display.compositor);
681
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200682 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200683 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500684 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200685
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100686 return 0;
687}