blob: fcbea7570a0dbb6fe1bc7773560ee34aed4bfccc [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 {
71 GLuint fbo;
72 GLuint color_rbo;
73
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010074 GLuint rotation_uniform;
75
76 GLuint pos;
77 GLuint col;
78 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050079
80 struct wl_egl_window *native;
81 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020082 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050083 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020084 struct wl_callback *callback;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -040085 int fullscreen, configured, opaque;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086};
87
88static const char *vert_shader_text =
89 "uniform mat4 rotation;\n"
90 "attribute vec4 pos;\n"
91 "attribute vec4 color;\n"
92 "varying vec4 v_color;\n"
93 "void main() {\n"
94 " gl_Position = rotation * pos;\n"
95 " v_color = color;\n"
96 "}\n";
97
98static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050099 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100100 "varying vec4 v_color;\n"
101 "void main() {\n"
102 " gl_FragColor = v_color;\n"
103 "}\n";
104
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400105static int running = 1;
106
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100107static void
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400108init_egl(struct display *display, int opaque)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100109{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500110 static const EGLint context_attribs[] = {
111 EGL_CONTEXT_CLIENT_VERSION, 2,
112 EGL_NONE
113 };
114
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300115 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500116 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500117 EGL_RED_SIZE, 1,
118 EGL_GREEN_SIZE, 1,
119 EGL_BLUE_SIZE, 1,
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400120 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500121 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
122 EGL_NONE
123 };
124
125 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100126 EGLBoolean ret;
127
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400128 if (opaque)
129 config_attribs[9] = 0;
130
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400131 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100132 assert(display->egl.dpy);
133
134 ret = eglInitialize(display->egl.dpy, &major, &minor);
135 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500136 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100137 assert(ret == EGL_TRUE);
138
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300139 ret = eglChooseConfig(display->egl.dpy, config_attribs,
140 &display->egl.conf, 1, &n);
141 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500142
143 display->egl.ctx = eglCreateContext(display->egl.dpy,
144 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500145 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100146 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500147
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100148}
149
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200150static void
151fini_egl(struct display *display)
152{
153 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
154 * on eglReleaseThread(). */
155 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
156 EGL_NO_CONTEXT);
157
158 eglTerminate(display->egl.dpy);
159 eglReleaseThread();
160}
161
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100162static GLuint
163create_shader(struct window *window, const char *source, GLenum shader_type)
164{
165 GLuint shader;
166 GLint status;
167
168 shader = glCreateShader(shader_type);
169 assert(shader != 0);
170
171 glShaderSource(shader, 1, (const char **) &source, NULL);
172 glCompileShader(shader);
173
174 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
175 if (!status) {
176 char log[1000];
177 GLsizei len;
178 glGetShaderInfoLog(shader, 1000, &len, log);
179 fprintf(stderr, "Error: compiling %s: %*s\n",
180 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
181 len, log);
182 exit(1);
183 }
184
185 return shader;
186}
187
188static void
189init_gl(struct window *window)
190{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100191 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600192 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100193 GLint status;
194
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100195 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
196 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
197
Scott Moreau3ea23d02012-06-13 17:42:21 -0600198 program = glCreateProgram();
199 glAttachShader(program, frag);
200 glAttachShader(program, vert);
201 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100202
Scott Moreau3ea23d02012-06-13 17:42:21 -0600203 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100204 if (!status) {
205 char log[1000];
206 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600207 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100208 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
209 exit(1);
210 }
211
Scott Moreau3ea23d02012-06-13 17:42:21 -0600212 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100213
214 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600215 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100216
Scott Moreau3ea23d02012-06-13 17:42:21 -0600217 glBindAttribLocation(program, window->gl.pos, "pos");
218 glBindAttribLocation(program, window->gl.col, "color");
219 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100220
221 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600222 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100223}
224
225static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300226handle_ping(void *data, struct wl_shell_surface *shell_surface,
227 uint32_t serial)
228{
229 wl_shell_surface_pong(shell_surface, serial);
230}
231
232static void
233handle_configure(void *data, struct wl_shell_surface *shell_surface,
234 uint32_t edges, int32_t width, int32_t height)
235{
236 struct window *window = data;
237
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300238 if (window->native)
239 wl_egl_window_resize(window->native, width, height, 0, 0);
240
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300241 window->geometry.width = width;
242 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300243
Scott Moreau1ee53e72012-08-30 14:44:15 -0600244 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300245 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300246}
247
248static void
249handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
250{
251}
252
253static const struct wl_shell_surface_listener shell_surface_listener = {
254 handle_ping,
255 handle_configure,
256 handle_popup_done
257};
258
259static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300260redraw(void *data, struct wl_callback *callback, uint32_t time);
261
262static void
263configure_callback(void *data, struct wl_callback *callback, uint32_t time)
264{
265 struct window *window = data;
266
267 wl_callback_destroy(callback);
268
269 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600270
271 if (window->callback == NULL)
272 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300273}
274
275static struct wl_callback_listener configure_callback_listener = {
276 configure_callback,
277};
278
279static void
280toggle_fullscreen(struct window *window, int fullscreen)
281{
282 struct wl_callback *callback;
283
284 window->fullscreen = fullscreen;
285 window->configured = 0;
286
287 if (fullscreen) {
288 wl_shell_surface_set_fullscreen(window->shell_surface,
289 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
290 0, NULL);
291 } else {
292 wl_shell_surface_set_toplevel(window->shell_surface);
293 handle_configure(window, window->shell_surface, 0,
294 window->window_size.width,
295 window->window_size.height);
296 }
297
298 callback = wl_display_sync(window->display->display);
299 wl_callback_add_listener(callback, &configure_callback_listener,
300 window);
301}
302
303static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100304create_surface(struct window *window)
305{
306 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500307 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200308
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500309 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200310 window->shell_surface = wl_shell_get_shell_surface(display->shell,
311 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300312
313 wl_shell_surface_add_listener(window->shell_surface,
314 &shell_surface_listener, window);
315
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500316 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400317 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300318 window->window_size.width,
319 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500320 window->egl_surface =
321 eglCreateWindowSurface(display->egl.dpy,
322 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500323 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100324
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600325 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
326
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500327 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
328 window->egl_surface, window->display->egl.ctx);
329 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300330
331 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100332}
333
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200334static void
335destroy_surface(struct window *window)
336{
337 wl_egl_window_destroy(window->native);
338
339 wl_shell_surface_destroy(window->shell_surface);
340 wl_surface_destroy(window->surface);
341
342 if (window->callback)
343 wl_callback_destroy(window->callback);
344}
345
Kristian Høgsberg33418202011-08-16 23:01:28 -0400346static const struct wl_callback_listener frame_listener;
347
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100348static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400349redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100350{
351 struct window *window = data;
352 static const GLfloat verts[3][2] = {
353 { -0.5, -0.5 },
354 { 0.5, -0.5 },
355 { 0, 0.5 }
356 };
357 static const GLfloat colors[3][3] = {
358 { 1, 0, 0 },
359 { 0, 1, 0 },
360 { 0, 0, 1 }
361 };
362 GLfloat angle;
363 GLfloat rotation[4][4] = {
364 { 1, 0, 0, 0 },
365 { 0, 1, 0, 0 },
366 { 0, 0, 1, 0 },
367 { 0, 0, 0, 1 }
368 };
369 static const int32_t speed_div = 5;
370 static uint32_t start_time = 0;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400371 struct wl_region *region;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100372
Scott Moreau7e300db2012-08-31 03:18:15 -0600373 assert(window->callback == callback);
374 window->callback = NULL;
375
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300376 if (callback)
377 wl_callback_destroy(callback);
378
379 if (!window->configured)
380 return;
381
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100382 if (start_time == 0)
383 start_time = time;
384
385 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
386 rotation[0][0] = cos(angle);
387 rotation[0][2] = sin(angle);
388 rotation[2][0] = -sin(angle);
389 rotation[2][2] = cos(angle);
390
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300391 glViewport(0, 0, window->geometry.width, window->geometry.height);
392
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100393 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
394 (GLfloat *) rotation);
395
396 glClearColor(0.0, 0.0, 0.0, 0.5);
397 glClear(GL_COLOR_BUFFER_BIT);
398
399 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
400 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
401 glEnableVertexAttribArray(window->gl.pos);
402 glEnableVertexAttribArray(window->gl.col);
403
404 glDrawArrays(GL_TRIANGLES, 0, 3);
405
406 glDisableVertexAttribArray(window->gl.pos);
407 glDisableVertexAttribArray(window->gl.col);
408
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300409 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400410 region = wl_compositor_create_region(window->display->compositor);
411 wl_region_add(region, 0, 0,
412 window->window_size.width,
413 window->window_size.height);
414 wl_surface_set_opaque_region(window->surface, region);
415 wl_region_destroy(region);
416 }
417
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200418 window->callback = wl_surface_frame(window->surface);
419 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300420
421 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100422}
423
Kristian Høgsberg33418202011-08-16 23:01:28 -0400424static const struct wl_callback_listener frame_listener = {
425 redraw
426};
427
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100428static void
Daniel Stone37816df2012-05-16 18:45:18 +0100429pointer_handle_enter(void *data, struct wl_pointer *pointer,
430 uint32_t serial, struct wl_surface *surface,
431 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300432{
433 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400434 struct wl_buffer *buffer;
435 struct wl_cursor *cursor = display->default_cursor;
436 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300437
438 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300439 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400440 else if (cursor) {
441 image = display->default_cursor->images[0];
442 buffer = wl_cursor_image_get_buffer(image);
443 wl_pointer_set_cursor(pointer, serial,
444 display->cursor_surface,
445 image->hotspot_x,
446 image->hotspot_y);
447 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
448 wl_surface_damage(display->cursor_surface, 0, 0,
449 image->width, image->height);
450 wl_surface_commit(display->cursor_surface);
451 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300452}
453
454static void
Daniel Stone37816df2012-05-16 18:45:18 +0100455pointer_handle_leave(void *data, struct wl_pointer *pointer,
456 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300457{
458}
459
460static void
Daniel Stone37816df2012-05-16 18:45:18 +0100461pointer_handle_motion(void *data, struct wl_pointer *pointer,
462 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300463{
464}
465
466static void
Daniel Stone37816df2012-05-16 18:45:18 +0100467pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
468 uint32_t serial, uint32_t time, uint32_t button,
469 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300470{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300471 struct display *display = data;
472
473 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
474 wl_shell_surface_move(display->window->shell_surface,
475 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300476}
477
478static void
Daniel Stone37816df2012-05-16 18:45:18 +0100479pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100480 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300481{
482}
483
Daniel Stone37816df2012-05-16 18:45:18 +0100484static const struct wl_pointer_listener pointer_listener = {
485 pointer_handle_enter,
486 pointer_handle_leave,
487 pointer_handle_motion,
488 pointer_handle_button,
489 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300490};
491
492static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300493keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
494 uint32_t format, int fd, uint32_t size)
495{
496}
497
498static void
499keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
500 uint32_t serial, struct wl_surface *surface,
501 struct wl_array *keys)
502{
503}
504
505static void
506keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
507 uint32_t serial, struct wl_surface *surface)
508{
509}
510
511static void
512keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
513 uint32_t serial, uint32_t time, uint32_t key,
514 uint32_t state)
515{
516 struct display *d = data;
517
518 if (key == KEY_F11 && state)
519 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400520 else if (key == KEY_ESC && state)
521 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300522}
523
524static void
525keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
526 uint32_t serial, uint32_t mods_depressed,
527 uint32_t mods_latched, uint32_t mods_locked,
528 uint32_t group)
529{
530}
531
532static const struct wl_keyboard_listener keyboard_listener = {
533 keyboard_handle_keymap,
534 keyboard_handle_enter,
535 keyboard_handle_leave,
536 keyboard_handle_key,
537 keyboard_handle_modifiers,
538};
539
540static void
Daniel Stone37816df2012-05-16 18:45:18 +0100541seat_handle_capabilities(void *data, struct wl_seat *seat,
542 enum wl_seat_capability caps)
543{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400544 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100545
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400546 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
547 d->pointer = wl_seat_get_pointer(seat);
548 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
549 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
550 wl_pointer_destroy(d->pointer);
551 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100552 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300553
554 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
555 d->keyboard = wl_seat_get_keyboard(seat);
556 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
557 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
558 wl_keyboard_destroy(d->keyboard);
559 d->keyboard = NULL;
560 }
Daniel Stone37816df2012-05-16 18:45:18 +0100561}
562
563static const struct wl_seat_listener seat_listener = {
564 seat_handle_capabilities,
565};
566
567static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400568registry_handle_global(void *data, struct wl_registry *registry,
569 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100570{
571 struct display *d = data;
572
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400573 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400574 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400575 wl_registry_bind(registry, name,
576 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400577 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400578 d->shell = wl_registry_bind(registry, name,
579 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100580 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400581 d->seat = wl_registry_bind(registry, name,
582 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400583 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400584 } else if (strcmp(interface, "wl_shm") == 0) {
585 d->shm = wl_registry_bind(registry, name,
586 &wl_shm_interface, 1);
587 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
588 d->default_cursor =
589 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400590 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100591}
592
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400593static const struct wl_registry_listener registry_listener = {
594 registry_handle_global
595};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100596
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200597static void
598signal_int(int signum)
599{
600 running = 0;
601}
602
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400603static void
604usage(int error_code)
605{
606 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
607 " -f\tRun in fullscreen mode\n"
608 " -o\tCreate an opaque surface\n"
609 " -h\tThis help text\n\n");
610
611 exit(error_code);
612}
613
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100614int
615main(int argc, char **argv)
616{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200617 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100618 struct display display = { 0 };
619 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400620 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100621
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100622 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300623 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300624 window.window_size.width = 250;
625 window.window_size.height = 250;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100626
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400627 for (i = 1; i < argc; i++) {
628 if (strcmp("-f", argv[i]) == 0)
629 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400630 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300631 window.opaque = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400632 else if (strcmp("-h", argv[i]) == 0)
633 usage(EXIT_SUCCESS);
634 else
635 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400636 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300637
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100638 display.display = wl_display_connect(NULL);
639 assert(display.display);
640
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400641 display.registry = wl_display_get_registry(display.display);
642 wl_registry_add_listener(display.registry,
643 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100644
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400645 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200646
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300647 init_egl(&display, window.opaque);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100648 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500649 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100650
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400651 display.cursor_surface =
652 wl_compositor_create_surface(display.compositor);
653
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200654 sigint.sa_handler = signal_int;
655 sigemptyset(&sigint.sa_mask);
656 sigint.sa_flags = SA_RESETHAND;
657 sigaction(SIGINT, &sigint, NULL);
658
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400659 while (running && ret != -1)
660 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500661
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200662 fprintf(stderr, "simple-egl exiting\n");
663
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200664 destroy_surface(&window);
665 fini_egl(&display);
666
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400667 wl_surface_destroy(display.cursor_surface);
668 if (display.cursor_theme)
669 wl_cursor_theme_destroy(display.cursor_theme);
670
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200671 if (display.shell)
672 wl_shell_destroy(display.shell);
673
674 if (display.compositor)
675 wl_compositor_destroy(display.compositor);
676
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200677 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500678 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200679
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100680 return 0;
681}