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 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 31 | #include <wayland-client.h> |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 32 | #include <wayland-egl.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 33 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 34 | #include <GLES2/gl2.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 35 | #include <EGL/egl.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 36 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 37 | struct window; |
| 38 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 39 | struct display { |
| 40 | struct wl_display *display; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 41 | struct wl_compositor *compositor; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 42 | struct wl_shell *shell; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 43 | struct wl_input_device *input; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 44 | struct { |
| 45 | EGLDisplay dpy; |
| 46 | EGLContext ctx; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 47 | EGLConfig conf; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 48 | } egl; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 49 | uint32_t mask; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 50 | struct window *window; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct window { |
| 54 | struct display *display; |
| 55 | struct { |
| 56 | int width, height; |
| 57 | } geometry; |
| 58 | struct { |
| 59 | GLuint fbo; |
| 60 | GLuint color_rbo; |
| 61 | |
| 62 | GLuint program; |
| 63 | GLuint rotation_uniform; |
| 64 | |
| 65 | GLuint pos; |
| 66 | GLuint col; |
| 67 | } gl; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 68 | |
| 69 | struct wl_egl_window *native; |
| 70 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 71 | struct wl_shell_surface *shell_surface; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 72 | EGLSurface egl_surface; |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 73 | struct wl_callback *callback; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 74 | int fullscreen, configured; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static const char *vert_shader_text = |
| 78 | "uniform mat4 rotation;\n" |
| 79 | "attribute vec4 pos;\n" |
| 80 | "attribute vec4 color;\n" |
| 81 | "varying vec4 v_color;\n" |
| 82 | "void main() {\n" |
| 83 | " gl_Position = rotation * pos;\n" |
| 84 | " v_color = color;\n" |
| 85 | "}\n"; |
| 86 | |
| 87 | static const char *frag_shader_text = |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 88 | "precision mediump float;\n" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 89 | "varying vec4 v_color;\n" |
| 90 | "void main() {\n" |
| 91 | " gl_FragColor = v_color;\n" |
| 92 | "}\n"; |
| 93 | |
| 94 | static void |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 95 | init_egl(struct display *display, EGLint alpha_size) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 96 | { |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 97 | static const EGLint context_attribs[] = { |
| 98 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 99 | EGL_NONE |
| 100 | }; |
| 101 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 102 | EGLint config_attribs[] = { |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 103 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 104 | EGL_RED_SIZE, 1, |
| 105 | EGL_GREEN_SIZE, 1, |
| 106 | EGL_BLUE_SIZE, 1, |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 107 | EGL_ALPHA_SIZE, alpha_size, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 108 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 109 | EGL_NONE |
| 110 | }; |
| 111 | |
| 112 | EGLint major, minor, n; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 113 | EGLBoolean ret; |
| 114 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 115 | display->egl.dpy = eglGetDisplay(display->display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 116 | assert(display->egl.dpy); |
| 117 | |
| 118 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 119 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 120 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 121 | assert(ret == EGL_TRUE); |
| 122 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 123 | assert(eglChooseConfig(display->egl.dpy, config_attribs, |
| 124 | &display->egl.conf, 1, &n) && n == 1); |
| 125 | |
| 126 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 127 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 128 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 129 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 130 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 133 | static void |
| 134 | fini_egl(struct display *display) |
| 135 | { |
| 136 | /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() |
| 137 | * on eglReleaseThread(). */ |
| 138 | eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 139 | EGL_NO_CONTEXT); |
| 140 | |
| 141 | eglTerminate(display->egl.dpy); |
| 142 | eglReleaseThread(); |
| 143 | } |
| 144 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 145 | static GLuint |
| 146 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 147 | { |
| 148 | GLuint shader; |
| 149 | GLint status; |
| 150 | |
| 151 | shader = glCreateShader(shader_type); |
| 152 | assert(shader != 0); |
| 153 | |
| 154 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 155 | glCompileShader(shader); |
| 156 | |
| 157 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 158 | if (!status) { |
| 159 | char log[1000]; |
| 160 | GLsizei len; |
| 161 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 162 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 163 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 164 | len, log); |
| 165 | exit(1); |
| 166 | } |
| 167 | |
| 168 | return shader; |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | init_gl(struct window *window) |
| 173 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 174 | GLuint frag, vert; |
| 175 | GLint status; |
| 176 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 177 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 178 | |
| 179 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 180 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 181 | |
| 182 | window->gl.program = glCreateProgram(); |
| 183 | glAttachShader(window->gl.program, frag); |
| 184 | glAttachShader(window->gl.program, vert); |
| 185 | glLinkProgram(window->gl.program); |
| 186 | |
| 187 | glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); |
| 188 | if (!status) { |
| 189 | char log[1000]; |
| 190 | GLsizei len; |
| 191 | glGetProgramInfoLog(window->gl.program, 1000, &len, log); |
| 192 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 193 | exit(1); |
| 194 | } |
| 195 | |
| 196 | glUseProgram(window->gl.program); |
| 197 | |
| 198 | window->gl.pos = 0; |
| 199 | window->gl.pos = 1; |
| 200 | |
| 201 | glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); |
| 202 | glBindAttribLocation(window->gl.program, window->gl.col, "color"); |
| 203 | glLinkProgram(window->gl.program); |
| 204 | |
| 205 | window->gl.rotation_uniform = |
| 206 | glGetUniformLocation(window->gl.program, "rotation"); |
| 207 | } |
| 208 | |
| 209 | static void |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 210 | handle_ping(void *data, struct wl_shell_surface *shell_surface, |
| 211 | uint32_t serial) |
| 212 | { |
| 213 | wl_shell_surface_pong(shell_surface, serial); |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | handle_configure(void *data, struct wl_shell_surface *shell_surface, |
| 218 | uint32_t edges, int32_t width, int32_t height) |
| 219 | { |
| 220 | struct window *window = data; |
| 221 | |
| 222 | window->geometry.width = width; |
| 223 | window->geometry.height = height; |
| 224 | window->configured = 1; |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | handle_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 233 | handle_ping, |
| 234 | handle_configure, |
| 235 | handle_popup_done |
| 236 | }; |
| 237 | |
| 238 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 239 | create_surface(struct window *window) |
| 240 | { |
| 241 | struct display *display = window->display; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 242 | EGLBoolean ret; |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 243 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 244 | window->surface = wl_compositor_create_surface(display->compositor); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 245 | window->shell_surface = wl_shell_get_shell_surface(display->shell, |
| 246 | window->surface); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 247 | |
| 248 | wl_shell_surface_add_listener(window->shell_surface, |
| 249 | &shell_surface_listener, window); |
| 250 | |
| 251 | if (window->fullscreen) { |
| 252 | window->configured = 0; |
| 253 | wl_shell_surface_set_fullscreen(window->shell_surface, |
| 254 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 255 | 0, NULL); |
| 256 | |
| 257 | while (!window->configured) |
| 258 | wl_display_iterate(display->display, display->mask); |
| 259 | } |
| 260 | else |
| 261 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 262 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 263 | window->native = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 264 | wl_egl_window_create(window->surface, |
Kristian Høgsberg | bfb8e61 | 2011-02-07 10:30:38 -0500 | [diff] [blame] | 265 | window->geometry.width, |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 266 | window->geometry.height); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 267 | window->egl_surface = |
| 268 | eglCreateWindowSurface(display->egl.dpy, |
| 269 | display->egl.conf, |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 270 | window->native, NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 271 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 272 | ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, |
| 273 | window->egl_surface, window->display->egl.ctx); |
| 274 | assert(ret == EGL_TRUE); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 277 | static void |
| 278 | destroy_surface(struct window *window) |
| 279 | { |
| 280 | wl_egl_window_destroy(window->native); |
| 281 | |
| 282 | wl_shell_surface_destroy(window->shell_surface); |
| 283 | wl_surface_destroy(window->surface); |
| 284 | |
| 285 | if (window->callback) |
| 286 | wl_callback_destroy(window->callback); |
| 287 | } |
| 288 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 289 | static const struct wl_callback_listener frame_listener; |
| 290 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 291 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 292 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 293 | { |
| 294 | struct window *window = data; |
| 295 | static const GLfloat verts[3][2] = { |
| 296 | { -0.5, -0.5 }, |
| 297 | { 0.5, -0.5 }, |
| 298 | { 0, 0.5 } |
| 299 | }; |
| 300 | static const GLfloat colors[3][3] = { |
| 301 | { 1, 0, 0 }, |
| 302 | { 0, 1, 0 }, |
| 303 | { 0, 0, 1 } |
| 304 | }; |
| 305 | GLfloat angle; |
| 306 | GLfloat rotation[4][4] = { |
| 307 | { 1, 0, 0, 0 }, |
| 308 | { 0, 1, 0, 0 }, |
| 309 | { 0, 0, 1, 0 }, |
| 310 | { 0, 0, 0, 1 } |
| 311 | }; |
| 312 | static const int32_t speed_div = 5; |
| 313 | static uint32_t start_time = 0; |
| 314 | |
| 315 | if (start_time == 0) |
| 316 | start_time = time; |
| 317 | |
| 318 | angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
| 319 | rotation[0][0] = cos(angle); |
| 320 | rotation[0][2] = sin(angle); |
| 321 | rotation[2][0] = -sin(angle); |
| 322 | rotation[2][2] = cos(angle); |
| 323 | |
| 324 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 325 | (GLfloat *) rotation); |
| 326 | |
| 327 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 328 | glClear(GL_COLOR_BUFFER_BIT); |
| 329 | |
| 330 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 331 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 332 | glEnableVertexAttribArray(window->gl.pos); |
| 333 | glEnableVertexAttribArray(window->gl.col); |
| 334 | |
| 335 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 336 | |
| 337 | glDisableVertexAttribArray(window->gl.pos); |
| 338 | glDisableVertexAttribArray(window->gl.col); |
| 339 | |
| 340 | glFlush(); |
| 341 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 342 | eglSwapBuffers(window->display->egl.dpy, window->egl_surface); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 343 | if (callback) |
| 344 | wl_callback_destroy(callback); |
| 345 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 346 | window->callback = wl_surface_frame(window->surface); |
| 347 | wl_callback_add_listener(window->callback, &frame_listener, window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 350 | static const struct wl_callback_listener frame_listener = { |
| 351 | redraw |
| 352 | }; |
| 353 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 354 | static void |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 355 | input_handle_motion(void *data, struct wl_input_device *input_device, |
| 356 | uint32_t time, int32_t sx, int32_t sy) |
| 357 | { |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | input_handle_button(void *data, |
| 362 | struct wl_input_device *input_device, uint32_t serial, |
| 363 | uint32_t time, uint32_t button, uint32_t state) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | static void |
| 368 | input_handle_axis(void *data, |
| 369 | struct wl_input_device *input_device, |
| 370 | uint32_t time, uint32_t axis, int32_t value) |
| 371 | { |
| 372 | } |
| 373 | |
| 374 | static void |
| 375 | input_handle_key(void *data, struct wl_input_device *input_device, |
| 376 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 377 | { |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | input_handle_pointer_enter(void *data, |
| 382 | struct wl_input_device *input_device, |
| 383 | uint32_t serial, struct wl_surface *surface, |
| 384 | int32_t sx, int32_t sy) |
| 385 | { |
| 386 | struct display *display = data; |
| 387 | |
| 388 | if (display->window->fullscreen) |
| 389 | wl_input_device_attach(input_device, serial, NULL, 0, 0); |
| 390 | } |
| 391 | |
| 392 | static void |
| 393 | input_handle_pointer_leave(void *data, |
| 394 | struct wl_input_device *input_device, |
| 395 | uint32_t serial, struct wl_surface *surface) |
| 396 | { |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | input_handle_keyboard_enter(void *data, |
| 401 | struct wl_input_device *input_device, |
| 402 | uint32_t serial, |
| 403 | struct wl_surface *surface, |
| 404 | struct wl_array *keys) |
| 405 | { |
| 406 | } |
| 407 | |
| 408 | static void |
| 409 | input_handle_keyboard_leave(void *data, |
| 410 | struct wl_input_device *input_device, |
| 411 | uint32_t serial, |
| 412 | struct wl_surface *surface) |
| 413 | { |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | input_handle_touch_down(void *data, |
| 418 | struct wl_input_device *wl_input_device, |
| 419 | uint32_t serial, uint32_t time, |
| 420 | struct wl_surface *surface, |
| 421 | int32_t id, int32_t x, int32_t y) |
| 422 | { |
| 423 | } |
| 424 | |
| 425 | static void |
| 426 | input_handle_touch_up(void *data, |
| 427 | struct wl_input_device *wl_input_device, |
| 428 | uint32_t serial, uint32_t time, int32_t id) |
| 429 | { |
| 430 | } |
| 431 | |
| 432 | static void |
| 433 | input_handle_touch_motion(void *data, |
| 434 | struct wl_input_device *wl_input_device, |
| 435 | uint32_t time, int32_t id, int32_t x, int32_t y) |
| 436 | { |
| 437 | } |
| 438 | |
| 439 | static void |
| 440 | input_handle_touch_frame(void *data, |
| 441 | struct wl_input_device *wl_input_device) |
| 442 | { |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | input_handle_touch_cancel(void *data, |
| 447 | struct wl_input_device *wl_input_device) |
| 448 | { |
| 449 | } |
| 450 | |
| 451 | static const struct wl_input_device_listener input_listener = { |
| 452 | input_handle_motion, |
| 453 | input_handle_button, |
| 454 | input_handle_axis, |
| 455 | input_handle_key, |
| 456 | input_handle_pointer_enter, |
| 457 | input_handle_pointer_leave, |
| 458 | input_handle_keyboard_enter, |
| 459 | input_handle_keyboard_leave, |
| 460 | input_handle_touch_down, |
| 461 | input_handle_touch_up, |
| 462 | input_handle_touch_motion, |
| 463 | input_handle_touch_frame, |
| 464 | input_handle_touch_cancel, |
| 465 | }; |
| 466 | |
| 467 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 468 | display_handle_global(struct wl_display *display, uint32_t id, |
| 469 | const char *interface, uint32_t version, void *data) |
| 470 | { |
| 471 | struct display *d = data; |
| 472 | |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 473 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 474 | d->compositor = |
| 475 | wl_display_bind(display, id, &wl_compositor_interface); |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 476 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 477 | d->shell = wl_display_bind(display, id, &wl_shell_interface); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 478 | } else if (strcmp(interface, "wl_input_device") == 0) { |
| 479 | d->input = wl_display_bind(display, id, &wl_input_device_interface); |
| 480 | wl_input_device_add_listener(d->input, &input_listener, d); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 481 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static int |
| 485 | event_mask_update(uint32_t mask, void *data) |
| 486 | { |
| 487 | struct display *d = data; |
| 488 | |
| 489 | d->mask = mask; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 494 | static int running = 1; |
| 495 | |
| 496 | static void |
| 497 | signal_int(int signum) |
| 498 | { |
| 499 | running = 0; |
| 500 | } |
| 501 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 502 | int |
| 503 | main(int argc, char **argv) |
| 504 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 505 | struct sigaction sigint; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 506 | struct display display = { 0 }; |
| 507 | struct window window = { 0 }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 508 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 509 | window.display = &display; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 510 | display.window = &window; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 511 | window.geometry.width = 250; |
| 512 | window.geometry.height = 250; |
| 513 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 514 | if (argc >= 2 && strcmp("-f", argv[0])) |
| 515 | window.fullscreen = 1; |
| 516 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 517 | display.display = wl_display_connect(NULL); |
| 518 | assert(display.display); |
| 519 | |
| 520 | wl_display_add_global_listener(display.display, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 521 | display_handle_global, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 522 | |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 523 | wl_display_get_fd(display.display, event_mask_update, &display); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 524 | wl_display_iterate(display.display, WL_DISPLAY_READABLE); |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 525 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame^] | 526 | init_egl(&display, window.fullscreen ? 0 : 1); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 527 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 528 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 529 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 530 | sigint.sa_handler = signal_int; |
| 531 | sigemptyset(&sigint.sa_mask); |
| 532 | sigint.sa_flags = SA_RESETHAND; |
| 533 | sigaction(SIGINT, &sigint, NULL); |
| 534 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 535 | redraw(&window, NULL, 0); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 536 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 537 | while (running) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 538 | wl_display_iterate(display.display, display.mask); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 539 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 540 | fprintf(stderr, "simple-egl exiting\n"); |
| 541 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 542 | destroy_surface(&window); |
| 543 | fini_egl(&display); |
| 544 | |
| 545 | if (display.shell) |
| 546 | wl_shell_destroy(display.shell); |
| 547 | |
| 548 | if (display.compositor) |
| 549 | wl_compositor_destroy(display.compositor); |
| 550 | |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 551 | wl_display_flush(display.display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 552 | wl_display_disconnect(display.display); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 553 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |