Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | #include <math.h> |
| 28 | #include <assert.h> |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 30 | |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 31 | #include <linux/input.h> |
| 32 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 33 | #include <wayland-client.h> |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 34 | #include <wayland-egl.h> |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 35 | #include <wayland-cursor.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 36 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 37 | #include <GLES2/gl2.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 38 | #include <EGL/egl.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 39 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 40 | struct window; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 41 | struct seat; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 42 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 43 | struct display { |
| 44 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 45 | struct wl_registry *registry; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 46 | struct wl_compositor *compositor; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 47 | struct wl_shell *shell; |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 48 | struct wl_seat *seat; |
| 49 | struct wl_pointer *pointer; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 50 | struct wl_keyboard *keyboard; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 51 | struct wl_shm *shm; |
| 52 | struct wl_cursor_theme *cursor_theme; |
| 53 | struct wl_cursor *default_cursor; |
| 54 | struct wl_surface *cursor_surface; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 55 | struct { |
| 56 | EGLDisplay dpy; |
| 57 | EGLContext ctx; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 58 | EGLConfig conf; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 59 | } egl; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 60 | struct window *window; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 63 | struct geometry { |
| 64 | int width, height; |
| 65 | }; |
| 66 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 67 | struct window { |
| 68 | struct display *display; |
Scott Moreau | 1ee53e7 | 2012-08-30 14:44:15 -0600 | [diff] [blame] | 69 | struct geometry geometry, window_size; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 70 | struct { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 71 | GLuint rotation_uniform; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 72 | GLuint pos; |
| 73 | GLuint col; |
| 74 | } gl; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 75 | |
| 76 | struct wl_egl_window *native; |
| 77 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 78 | struct wl_shell_surface *shell_surface; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 79 | EGLSurface egl_surface; |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 80 | struct wl_callback *callback; |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 81 | int fullscreen, configured, opaque; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static 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 | |
| 94 | static const char *frag_shader_text = |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 95 | "precision mediump float;\n" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 96 | "varying vec4 v_color;\n" |
| 97 | "void main() {\n" |
| 98 | " gl_FragColor = v_color;\n" |
| 99 | "}\n"; |
| 100 | |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 101 | static int running = 1; |
| 102 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 103 | static void |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 104 | init_egl(struct display *display, int opaque) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 105 | { |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 106 | static const EGLint context_attribs[] = { |
| 107 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 108 | EGL_NONE |
| 109 | }; |
| 110 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 111 | EGLint config_attribs[] = { |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 112 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 113 | EGL_RED_SIZE, 1, |
| 114 | EGL_GREEN_SIZE, 1, |
| 115 | EGL_BLUE_SIZE, 1, |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 116 | EGL_ALPHA_SIZE, 1, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 117 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 118 | EGL_NONE |
| 119 | }; |
| 120 | |
| 121 | EGLint major, minor, n; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 122 | EGLBoolean ret; |
| 123 | |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 124 | if (opaque) |
| 125 | config_attribs[9] = 0; |
| 126 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 127 | display->egl.dpy = eglGetDisplay(display->display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 128 | assert(display->egl.dpy); |
| 129 | |
| 130 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 131 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 132 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 133 | assert(ret == EGL_TRUE); |
| 134 | |
Pekka Paalanen | b79b635 | 2012-06-12 17:42:24 +0300 | [diff] [blame] | 135 | ret = eglChooseConfig(display->egl.dpy, config_attribs, |
| 136 | &display->egl.conf, 1, &n); |
| 137 | assert(ret && n == 1); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 138 | |
| 139 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 140 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 141 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 142 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 143 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 146 | static void |
| 147 | fini_egl(struct display *display) |
| 148 | { |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 149 | eglTerminate(display->egl.dpy); |
| 150 | eglReleaseThread(); |
| 151 | } |
| 152 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 153 | static GLuint |
| 154 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 155 | { |
| 156 | GLuint shader; |
| 157 | GLint status; |
| 158 | |
| 159 | shader = glCreateShader(shader_type); |
| 160 | assert(shader != 0); |
| 161 | |
| 162 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 163 | glCompileShader(shader); |
| 164 | |
| 165 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 166 | if (!status) { |
| 167 | char log[1000]; |
| 168 | GLsizei len; |
| 169 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 170 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 171 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 172 | len, log); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | return shader; |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | init_gl(struct window *window) |
| 181 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 182 | GLuint frag, vert; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 183 | GLuint program; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 184 | GLint status; |
| 185 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 186 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 187 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 188 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 189 | program = glCreateProgram(); |
| 190 | glAttachShader(program, frag); |
| 191 | glAttachShader(program, vert); |
| 192 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 193 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 194 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 195 | if (!status) { |
| 196 | char log[1000]; |
| 197 | GLsizei len; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 198 | glGetProgramInfoLog(program, 1000, &len, log); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 199 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 203 | glUseProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 204 | |
| 205 | window->gl.pos = 0; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 206 | window->gl.col = 1; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 207 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 208 | glBindAttribLocation(program, window->gl.pos, "pos"); |
| 209 | glBindAttribLocation(program, window->gl.col, "color"); |
| 210 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 211 | |
| 212 | window->gl.rotation_uniform = |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 213 | glGetUniformLocation(program, "rotation"); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 217 | handle_ping(void *data, struct wl_shell_surface *shell_surface, |
| 218 | uint32_t serial) |
| 219 | { |
| 220 | wl_shell_surface_pong(shell_surface, serial); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | handle_configure(void *data, struct wl_shell_surface *shell_surface, |
| 225 | uint32_t edges, int32_t width, int32_t height) |
| 226 | { |
| 227 | struct window *window = data; |
| 228 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 229 | if (window->native) |
| 230 | wl_egl_window_resize(window->native, width, height, 0, 0); |
| 231 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 232 | window->geometry.width = width; |
| 233 | window->geometry.height = height; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 234 | |
Scott Moreau | 1ee53e7 | 2012-08-30 14:44:15 -0600 | [diff] [blame] | 235 | if (!window->fullscreen) |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 236 | window->window_size = window->geometry; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void |
| 240 | handle_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 245 | handle_ping, |
| 246 | handle_configure, |
| 247 | handle_popup_done |
| 248 | }; |
| 249 | |
| 250 | static void |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 251 | redraw(void *data, struct wl_callback *callback, uint32_t time); |
| 252 | |
| 253 | static void |
| 254 | configure_callback(void *data, struct wl_callback *callback, uint32_t time) |
| 255 | { |
| 256 | struct window *window = data; |
| 257 | |
| 258 | wl_callback_destroy(callback); |
| 259 | |
| 260 | window->configured = 1; |
Scott Moreau | 7e300db | 2012-08-31 03:18:15 -0600 | [diff] [blame] | 261 | |
| 262 | if (window->callback == NULL) |
| 263 | redraw(data, NULL, time); |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static struct wl_callback_listener configure_callback_listener = { |
| 267 | configure_callback, |
| 268 | }; |
| 269 | |
| 270 | static void |
| 271 | toggle_fullscreen(struct window *window, int fullscreen) |
| 272 | { |
| 273 | struct wl_callback *callback; |
| 274 | |
| 275 | window->fullscreen = fullscreen; |
| 276 | window->configured = 0; |
| 277 | |
| 278 | if (fullscreen) { |
| 279 | wl_shell_surface_set_fullscreen(window->shell_surface, |
| 280 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 281 | 0, NULL); |
| 282 | } else { |
| 283 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 284 | handle_configure(window, window->shell_surface, 0, |
| 285 | window->window_size.width, |
| 286 | window->window_size.height); |
| 287 | } |
| 288 | |
| 289 | callback = wl_display_sync(window->display->display); |
| 290 | wl_callback_add_listener(callback, &configure_callback_listener, |
| 291 | window); |
| 292 | } |
| 293 | |
| 294 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 295 | create_surface(struct window *window) |
| 296 | { |
| 297 | struct display *display = window->display; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 298 | EGLBoolean ret; |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 299 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 300 | window->surface = wl_compositor_create_surface(display->compositor); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 301 | window->shell_surface = wl_shell_get_shell_surface(display->shell, |
| 302 | window->surface); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 303 | |
| 304 | wl_shell_surface_add_listener(window->shell_surface, |
| 305 | &shell_surface_listener, window); |
| 306 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 307 | window->native = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 308 | wl_egl_window_create(window->surface, |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 309 | window->window_size.width, |
| 310 | window->window_size.height); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 311 | window->egl_surface = |
| 312 | eglCreateWindowSurface(display->egl.dpy, |
| 313 | display->egl.conf, |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 314 | window->native, NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 315 | |
Scott Moreau | 01a9f1b | 2012-10-07 08:56:30 -0600 | [diff] [blame] | 316 | wl_shell_surface_set_title(window->shell_surface, "simple-egl"); |
| 317 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 318 | 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 Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 321 | |
| 322 | toggle_fullscreen(window, window->fullscreen); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 325 | static void |
| 326 | destroy_surface(struct window *window) |
| 327 | { |
Yeh, Sinclair | 952e6df | 2013-04-19 17:49:12 +0000 | [diff] [blame^] | 328 | /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() |
| 329 | * on eglReleaseThread(). */ |
| 330 | eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 331 | EGL_NO_CONTEXT); |
| 332 | |
| 333 | eglDestroySurface(window->display->egl.dpy, window->egl_surface); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 334 | wl_egl_window_destroy(window->native); |
| 335 | |
| 336 | wl_shell_surface_destroy(window->shell_surface); |
| 337 | wl_surface_destroy(window->surface); |
| 338 | |
| 339 | if (window->callback) |
| 340 | wl_callback_destroy(window->callback); |
| 341 | } |
| 342 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 343 | static const struct wl_callback_listener frame_listener; |
| 344 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 345 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 346 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 347 | { |
| 348 | struct window *window = data; |
| 349 | static const GLfloat verts[3][2] = { |
| 350 | { -0.5, -0.5 }, |
| 351 | { 0.5, -0.5 }, |
| 352 | { 0, 0.5 } |
| 353 | }; |
| 354 | static const GLfloat colors[3][3] = { |
| 355 | { 1, 0, 0 }, |
| 356 | { 0, 1, 0 }, |
| 357 | { 0, 0, 1 } |
| 358 | }; |
| 359 | GLfloat angle; |
| 360 | GLfloat rotation[4][4] = { |
| 361 | { 1, 0, 0, 0 }, |
| 362 | { 0, 1, 0, 0 }, |
| 363 | { 0, 0, 1, 0 }, |
| 364 | { 0, 0, 0, 1 } |
| 365 | }; |
| 366 | static const int32_t speed_div = 5; |
| 367 | static uint32_t start_time = 0; |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 368 | struct wl_region *region; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 369 | |
Scott Moreau | 7e300db | 2012-08-31 03:18:15 -0600 | [diff] [blame] | 370 | assert(window->callback == callback); |
| 371 | window->callback = NULL; |
| 372 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 373 | if (callback) |
| 374 | wl_callback_destroy(callback); |
| 375 | |
| 376 | if (!window->configured) |
| 377 | return; |
| 378 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 379 | if (start_time == 0) |
| 380 | start_time = time; |
| 381 | |
| 382 | angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
| 383 | rotation[0][0] = cos(angle); |
| 384 | rotation[0][2] = sin(angle); |
| 385 | rotation[2][0] = -sin(angle); |
| 386 | rotation[2][2] = cos(angle); |
| 387 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 388 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
| 389 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 390 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 391 | (GLfloat *) rotation); |
| 392 | |
| 393 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 394 | glClear(GL_COLOR_BUFFER_BIT); |
| 395 | |
| 396 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 397 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 398 | glEnableVertexAttribArray(window->gl.pos); |
| 399 | glEnableVertexAttribArray(window->gl.col); |
| 400 | |
| 401 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 402 | |
| 403 | glDisableVertexAttribArray(window->gl.pos); |
| 404 | glDisableVertexAttribArray(window->gl.col); |
| 405 | |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 406 | if (window->opaque || window->fullscreen) { |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 407 | region = wl_compositor_create_region(window->display->compositor); |
| 408 | wl_region_add(region, 0, 0, |
Ander Conselvan de Oliveira | edce9c2 | 2012-09-07 17:32:16 +0300 | [diff] [blame] | 409 | window->geometry.width, |
| 410 | window->geometry.height); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 411 | wl_surface_set_opaque_region(window->surface, region); |
| 412 | wl_region_destroy(region); |
Scott Moreau | 6655e00 | 2012-11-19 14:17:52 -0700 | [diff] [blame] | 413 | } else { |
| 414 | wl_surface_set_opaque_region(window->surface, NULL); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 417 | window->callback = wl_surface_frame(window->surface); |
| 418 | wl_callback_add_listener(window->callback, &frame_listener, window); |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 419 | |
| 420 | eglSwapBuffers(window->display->egl.dpy, window->egl_surface); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 423 | static const struct wl_callback_listener frame_listener = { |
| 424 | redraw |
| 425 | }; |
| 426 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 427 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 428 | pointer_handle_enter(void *data, struct wl_pointer *pointer, |
| 429 | uint32_t serial, struct wl_surface *surface, |
| 430 | wl_fixed_t sx, wl_fixed_t sy) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 431 | { |
| 432 | struct display *display = data; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 433 | struct wl_buffer *buffer; |
| 434 | struct wl_cursor *cursor = display->default_cursor; |
| 435 | struct wl_cursor_image *image; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 436 | |
| 437 | if (display->window->fullscreen) |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 438 | wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 439 | else if (cursor) { |
| 440 | image = display->default_cursor->images[0]; |
| 441 | buffer = wl_cursor_image_get_buffer(image); |
| 442 | wl_pointer_set_cursor(pointer, serial, |
| 443 | display->cursor_surface, |
| 444 | image->hotspot_x, |
| 445 | image->hotspot_y); |
| 446 | wl_surface_attach(display->cursor_surface, buffer, 0, 0); |
| 447 | wl_surface_damage(display->cursor_surface, 0, 0, |
| 448 | image->width, image->height); |
| 449 | wl_surface_commit(display->cursor_surface); |
| 450 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 454 | pointer_handle_leave(void *data, struct wl_pointer *pointer, |
| 455 | uint32_t serial, struct wl_surface *surface) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 456 | { |
| 457 | } |
| 458 | |
| 459 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 460 | pointer_handle_motion(void *data, struct wl_pointer *pointer, |
| 461 | uint32_t time, wl_fixed_t sx, wl_fixed_t sy) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 462 | { |
| 463 | } |
| 464 | |
| 465 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 466 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 467 | uint32_t serial, uint32_t time, uint32_t button, |
| 468 | uint32_t state) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 469 | { |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 470 | struct display *display = data; |
| 471 | |
| 472 | if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 473 | wl_shell_surface_move(display->window->shell_surface, |
| 474 | display->seat, serial); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 478 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 479 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 480 | { |
| 481 | } |
| 482 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 483 | static const struct wl_pointer_listener pointer_listener = { |
| 484 | pointer_handle_enter, |
| 485 | pointer_handle_leave, |
| 486 | pointer_handle_motion, |
| 487 | pointer_handle_button, |
| 488 | pointer_handle_axis, |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | static void |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 492 | keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, |
| 493 | uint32_t format, int fd, uint32_t size) |
| 494 | { |
| 495 | } |
| 496 | |
| 497 | static void |
| 498 | keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, |
| 499 | uint32_t serial, struct wl_surface *surface, |
| 500 | struct wl_array *keys) |
| 501 | { |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, |
| 506 | uint32_t serial, struct wl_surface *surface) |
| 507 | { |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | keyboard_handle_key(void *data, struct wl_keyboard *keyboard, |
| 512 | uint32_t serial, uint32_t time, uint32_t key, |
| 513 | uint32_t state) |
| 514 | { |
| 515 | struct display *d = data; |
| 516 | |
| 517 | if (key == KEY_F11 && state) |
| 518 | toggle_fullscreen(d->window, d->window->fullscreen ^ 1); |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 519 | else if (key == KEY_ESC && state) |
| 520 | running = 0; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static void |
| 524 | keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 525 | uint32_t serial, uint32_t mods_depressed, |
| 526 | uint32_t mods_latched, uint32_t mods_locked, |
| 527 | uint32_t group) |
| 528 | { |
| 529 | } |
| 530 | |
| 531 | static const struct wl_keyboard_listener keyboard_listener = { |
| 532 | keyboard_handle_keymap, |
| 533 | keyboard_handle_enter, |
| 534 | keyboard_handle_leave, |
| 535 | keyboard_handle_key, |
| 536 | keyboard_handle_modifiers, |
| 537 | }; |
| 538 | |
| 539 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 540 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 541 | enum wl_seat_capability caps) |
| 542 | { |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 543 | struct display *d = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 544 | |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 545 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) { |
| 546 | d->pointer = wl_seat_get_pointer(seat); |
| 547 | wl_pointer_add_listener(d->pointer, &pointer_listener, d); |
| 548 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) { |
| 549 | wl_pointer_destroy(d->pointer); |
| 550 | d->pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 551 | } |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 552 | |
| 553 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) { |
| 554 | d->keyboard = wl_seat_get_keyboard(seat); |
| 555 | wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); |
| 556 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) { |
| 557 | wl_keyboard_destroy(d->keyboard); |
| 558 | d->keyboard = NULL; |
| 559 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static const struct wl_seat_listener seat_listener = { |
| 563 | seat_handle_capabilities, |
| 564 | }; |
| 565 | |
| 566 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 567 | registry_handle_global(void *data, struct wl_registry *registry, |
| 568 | uint32_t name, const char *interface, uint32_t version) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 569 | { |
| 570 | struct display *d = data; |
| 571 | |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 572 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 573 | d->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 574 | wl_registry_bind(registry, name, |
| 575 | &wl_compositor_interface, 1); |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 576 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 577 | d->shell = wl_registry_bind(registry, name, |
| 578 | &wl_shell_interface, 1); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 579 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 580 | d->seat = wl_registry_bind(registry, name, |
| 581 | &wl_seat_interface, 1); |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 582 | wl_seat_add_listener(d->seat, &seat_listener, d); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 583 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 584 | d->shm = wl_registry_bind(registry, name, |
| 585 | &wl_shm_interface, 1); |
| 586 | d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm); |
| 587 | d->default_cursor = |
| 588 | wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr"); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 589 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 592 | static void |
| 593 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 594 | uint32_t name) |
| 595 | { |
| 596 | } |
| 597 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 598 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 599 | registry_handle_global, |
| 600 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 601 | }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 602 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 603 | static void |
| 604 | signal_int(int signum) |
| 605 | { |
| 606 | running = 0; |
| 607 | } |
| 608 | |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 609 | static void |
| 610 | usage(int error_code) |
| 611 | { |
| 612 | fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n" |
| 613 | " -f\tRun in fullscreen mode\n" |
| 614 | " -o\tCreate an opaque surface\n" |
| 615 | " -h\tThis help text\n\n"); |
| 616 | |
| 617 | exit(error_code); |
| 618 | } |
| 619 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 620 | int |
| 621 | main(int argc, char **argv) |
| 622 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 623 | struct sigaction sigint; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 624 | struct display display = { 0 }; |
| 625 | struct window window = { 0 }; |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 626 | int i, ret = 0; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 627 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 628 | window.display = &display; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 629 | display.window = &window; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 630 | window.window_size.width = 250; |
| 631 | window.window_size.height = 250; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 632 | |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 633 | for (i = 1; i < argc; i++) { |
| 634 | if (strcmp("-f", argv[i]) == 0) |
| 635 | window.fullscreen = 1; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 636 | else if (strcmp("-o", argv[i]) == 0) |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 637 | window.opaque = 1; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 638 | else if (strcmp("-h", argv[i]) == 0) |
| 639 | usage(EXIT_SUCCESS); |
| 640 | else |
| 641 | usage(EXIT_FAILURE); |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 642 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 643 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 644 | display.display = wl_display_connect(NULL); |
| 645 | assert(display.display); |
| 646 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 647 | display.registry = wl_display_get_registry(display.display); |
| 648 | wl_registry_add_listener(display.registry, |
| 649 | ®istry_listener, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 650 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 651 | wl_display_dispatch(display.display); |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 652 | |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 653 | init_egl(&display, window.opaque); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 654 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 655 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 656 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 657 | display.cursor_surface = |
| 658 | wl_compositor_create_surface(display.compositor); |
| 659 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 660 | sigint.sa_handler = signal_int; |
| 661 | sigemptyset(&sigint.sa_mask); |
| 662 | sigint.sa_flags = SA_RESETHAND; |
| 663 | sigaction(SIGINT, &sigint, NULL); |
| 664 | |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 665 | while (running && ret != -1) |
| 666 | ret = wl_display_dispatch(display.display); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 667 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 668 | fprintf(stderr, "simple-egl exiting\n"); |
| 669 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 670 | destroy_surface(&window); |
| 671 | fini_egl(&display); |
| 672 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 673 | wl_surface_destroy(display.cursor_surface); |
| 674 | if (display.cursor_theme) |
| 675 | wl_cursor_theme_destroy(display.cursor_theme); |
| 676 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 677 | if (display.shell) |
| 678 | wl_shell_destroy(display.shell); |
| 679 | |
| 680 | if (display.compositor) |
| 681 | wl_compositor_destroy(display.compositor); |
| 682 | |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 683 | wl_registry_destroy(display.registry); |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 684 | wl_display_flush(display.display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 685 | wl_display_disconnect(display.display); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 686 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 687 | return 0; |
| 688 | } |