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> |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 39 | #include <EGL/eglext.h> |
| 40 | |
| 41 | #ifndef EGL_EXT_swap_buffers_with_damage |
| 42 | #define EGL_EXT_swap_buffers_with_damage 1 |
| 43 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects); |
| 44 | #endif |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 45 | |
Kristian Høgsberg | 7778743 | 2013-08-22 12:16:51 -0700 | [diff] [blame] | 46 | #ifndef EGL_EXT_buffer_age |
| 47 | #define EGL_EXT_buffer_age 1 |
| 48 | #define EGL_BUFFER_AGE_EXT 0x313D |
| 49 | #endif |
| 50 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 51 | struct window; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 52 | struct seat; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 53 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 54 | struct display { |
| 55 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 56 | struct wl_registry *registry; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 57 | struct wl_compositor *compositor; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 58 | struct wl_shell *shell; |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 59 | struct wl_seat *seat; |
| 60 | struct wl_pointer *pointer; |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 61 | struct wl_touch *touch; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 62 | struct wl_keyboard *keyboard; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 63 | struct wl_shm *shm; |
| 64 | struct wl_cursor_theme *cursor_theme; |
| 65 | struct wl_cursor *default_cursor; |
| 66 | struct wl_surface *cursor_surface; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 67 | struct { |
| 68 | EGLDisplay dpy; |
| 69 | EGLContext ctx; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 70 | EGLConfig conf; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 71 | } egl; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 72 | struct window *window; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 73 | |
| 74 | PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 77 | struct geometry { |
| 78 | int width, height; |
| 79 | }; |
| 80 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 81 | struct window { |
| 82 | struct display *display; |
Scott Moreau | 1ee53e7 | 2012-08-30 14:44:15 -0600 | [diff] [blame] | 83 | struct geometry geometry, window_size; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 84 | struct { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 85 | GLuint rotation_uniform; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 86 | GLuint pos; |
| 87 | GLuint col; |
| 88 | } gl; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 89 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 90 | uint32_t benchmark_time, frames; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 91 | struct wl_egl_window *native; |
| 92 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 93 | struct wl_shell_surface *shell_surface; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 94 | EGLSurface egl_surface; |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 95 | struct wl_callback *callback; |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 96 | int fullscreen, configured, opaque, buffer_size, frame_sync; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static const char *vert_shader_text = |
| 100 | "uniform mat4 rotation;\n" |
| 101 | "attribute vec4 pos;\n" |
| 102 | "attribute vec4 color;\n" |
| 103 | "varying vec4 v_color;\n" |
| 104 | "void main() {\n" |
| 105 | " gl_Position = rotation * pos;\n" |
| 106 | " v_color = color;\n" |
| 107 | "}\n"; |
| 108 | |
| 109 | static const char *frag_shader_text = |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 110 | "precision mediump float;\n" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 111 | "varying vec4 v_color;\n" |
| 112 | "void main() {\n" |
| 113 | " gl_FragColor = v_color;\n" |
| 114 | "}\n"; |
| 115 | |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 116 | static int running = 1; |
| 117 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 118 | static void |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 119 | init_egl(struct display *display, struct window *window) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 120 | { |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 121 | static const EGLint context_attribs[] = { |
| 122 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 123 | EGL_NONE |
| 124 | }; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 125 | const char *extensions; |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 126 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 127 | EGLint config_attribs[] = { |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 128 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 129 | EGL_RED_SIZE, 1, |
| 130 | EGL_GREEN_SIZE, 1, |
| 131 | EGL_BLUE_SIZE, 1, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 132 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 133 | EGL_NONE |
| 134 | }; |
| 135 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 136 | EGLint major, minor, n, count, i, size; |
| 137 | EGLConfig *configs; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 138 | EGLBoolean ret; |
| 139 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 140 | if (window->opaque || window->buffer_size == 16) |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 141 | config_attribs[9] = 0; |
| 142 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 143 | display->egl.dpy = eglGetDisplay(display->display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 144 | assert(display->egl.dpy); |
| 145 | |
| 146 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 147 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 148 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 149 | assert(ret == EGL_TRUE); |
| 150 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 151 | if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1) |
| 152 | assert(0); |
| 153 | |
| 154 | configs = calloc(count, sizeof *configs); |
| 155 | assert(configs); |
| 156 | |
Pekka Paalanen | b79b635 | 2012-06-12 17:42:24 +0300 | [diff] [blame] | 157 | ret = eglChooseConfig(display->egl.dpy, config_attribs, |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 158 | configs, count, &n); |
| 159 | assert(ret && n >= 1); |
| 160 | |
| 161 | for (i = 0; i < n; i++) { |
| 162 | eglGetConfigAttrib(display->egl.dpy, |
| 163 | configs[i], EGL_BUFFER_SIZE, &size); |
| 164 | if (window->buffer_size == size) { |
| 165 | display->egl.conf = configs[i]; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | free(configs); |
| 170 | if (display->egl.conf == NULL) { |
| 171 | fprintf(stderr, "did not find config with buffer size %d\n", |
| 172 | window->buffer_size); |
| 173 | exit(EXIT_FAILURE); |
| 174 | } |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 175 | |
| 176 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 177 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 178 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 179 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 180 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 181 | display->swap_buffers_with_damage = NULL; |
| 182 | extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS); |
| 183 | if (extensions && |
| 184 | strstr(extensions, "EGL_EXT_swap_buffers_with_damage") && |
| 185 | strstr(extensions, "EGL_EXT_buffer_age")) |
| 186 | display->swap_buffers_with_damage = |
| 187 | (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC) |
| 188 | eglGetProcAddress("eglSwapBuffersWithDamageEXT"); |
| 189 | |
| 190 | if (display->swap_buffers_with_damage) |
| 191 | printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n"); |
| 192 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 195 | static void |
| 196 | fini_egl(struct display *display) |
| 197 | { |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 198 | eglTerminate(display->egl.dpy); |
| 199 | eglReleaseThread(); |
| 200 | } |
| 201 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 202 | static GLuint |
| 203 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 204 | { |
| 205 | GLuint shader; |
| 206 | GLint status; |
| 207 | |
| 208 | shader = glCreateShader(shader_type); |
| 209 | assert(shader != 0); |
| 210 | |
| 211 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 212 | glCompileShader(shader); |
| 213 | |
| 214 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 215 | if (!status) { |
| 216 | char log[1000]; |
| 217 | GLsizei len; |
| 218 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 219 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 220 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 221 | len, log); |
| 222 | exit(1); |
| 223 | } |
| 224 | |
| 225 | return shader; |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | init_gl(struct window *window) |
| 230 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 231 | GLuint frag, vert; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 232 | GLuint program; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 233 | GLint status; |
| 234 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 235 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 236 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 237 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 238 | program = glCreateProgram(); |
| 239 | glAttachShader(program, frag); |
| 240 | glAttachShader(program, vert); |
| 241 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 242 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 243 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 244 | if (!status) { |
| 245 | char log[1000]; |
| 246 | GLsizei len; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 247 | glGetProgramInfoLog(program, 1000, &len, log); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 248 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 249 | exit(1); |
| 250 | } |
| 251 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 252 | glUseProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 253 | |
| 254 | window->gl.pos = 0; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 255 | window->gl.col = 1; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 256 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 257 | glBindAttribLocation(program, window->gl.pos, "pos"); |
| 258 | glBindAttribLocation(program, window->gl.col, "color"); |
| 259 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 260 | |
| 261 | window->gl.rotation_uniform = |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 262 | glGetUniformLocation(program, "rotation"); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 266 | handle_ping(void *data, struct wl_shell_surface *shell_surface, |
| 267 | uint32_t serial) |
| 268 | { |
| 269 | wl_shell_surface_pong(shell_surface, serial); |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | handle_configure(void *data, struct wl_shell_surface *shell_surface, |
| 274 | uint32_t edges, int32_t width, int32_t height) |
| 275 | { |
| 276 | struct window *window = data; |
| 277 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 278 | if (window->native) |
| 279 | wl_egl_window_resize(window->native, width, height, 0, 0); |
| 280 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 281 | window->geometry.width = width; |
| 282 | window->geometry.height = height; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 283 | |
Scott Moreau | 1ee53e7 | 2012-08-30 14:44:15 -0600 | [diff] [blame] | 284 | if (!window->fullscreen) |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 285 | window->window_size = window->geometry; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void |
| 289 | handle_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 294 | handle_ping, |
| 295 | handle_configure, |
| 296 | handle_popup_done |
| 297 | }; |
| 298 | |
| 299 | static void |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 300 | configure_callback(void *data, struct wl_callback *callback, uint32_t time) |
| 301 | { |
| 302 | struct window *window = data; |
| 303 | |
| 304 | wl_callback_destroy(callback); |
| 305 | |
| 306 | window->configured = 1; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static struct wl_callback_listener configure_callback_listener = { |
| 310 | configure_callback, |
| 311 | }; |
| 312 | |
| 313 | static void |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 314 | set_fullscreen(struct window *window, int fullscreen) |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 315 | { |
| 316 | struct wl_callback *callback; |
| 317 | |
| 318 | window->fullscreen = fullscreen; |
| 319 | window->configured = 0; |
| 320 | |
| 321 | if (fullscreen) { |
| 322 | wl_shell_surface_set_fullscreen(window->shell_surface, |
| 323 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 324 | 0, NULL); |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 325 | callback = wl_display_sync(window->display->display); |
| 326 | wl_callback_add_listener(callback, |
| 327 | &configure_callback_listener, |
| 328 | window); |
| 329 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 330 | } else { |
| 331 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 332 | handle_configure(window, window->shell_surface, 0, |
| 333 | window->window_size.width, |
| 334 | window->window_size.height); |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 335 | window->configured = 1; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 336 | } |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 340 | create_surface(struct window *window) |
| 341 | { |
| 342 | struct display *display = window->display; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 343 | EGLBoolean ret; |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 344 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 345 | window->surface = wl_compositor_create_surface(display->compositor); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 346 | window->shell_surface = wl_shell_get_shell_surface(display->shell, |
| 347 | window->surface); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 348 | |
| 349 | wl_shell_surface_add_listener(window->shell_surface, |
| 350 | &shell_surface_listener, window); |
| 351 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 352 | window->native = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 353 | wl_egl_window_create(window->surface, |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 354 | window->window_size.width, |
| 355 | window->window_size.height); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 356 | window->egl_surface = |
| 357 | eglCreateWindowSurface(display->egl.dpy, |
| 358 | display->egl.conf, |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 359 | window->native, NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 360 | |
Scott Moreau | 01a9f1b | 2012-10-07 08:56:30 -0600 | [diff] [blame] | 361 | wl_shell_surface_set_title(window->shell_surface, "simple-egl"); |
| 362 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 363 | ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, |
| 364 | window->egl_surface, window->display->egl.ctx); |
| 365 | assert(ret == EGL_TRUE); |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 366 | |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 367 | if (!window->frame_sync) |
| 368 | eglSwapInterval(display->egl.dpy, 0); |
| 369 | |
| 370 | set_fullscreen(window, window->fullscreen); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 373 | static void |
| 374 | destroy_surface(struct window *window) |
| 375 | { |
Yeh, Sinclair | 952e6df | 2013-04-19 17:49:12 +0000 | [diff] [blame] | 376 | /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() |
| 377 | * on eglReleaseThread(). */ |
| 378 | eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 379 | EGL_NO_CONTEXT); |
| 380 | |
| 381 | eglDestroySurface(window->display->egl.dpy, window->egl_surface); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 382 | wl_egl_window_destroy(window->native); |
| 383 | |
| 384 | wl_shell_surface_destroy(window->shell_surface); |
| 385 | wl_surface_destroy(window->surface); |
| 386 | |
| 387 | if (window->callback) |
| 388 | wl_callback_destroy(window->callback); |
| 389 | } |
| 390 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 391 | static const struct wl_callback_listener frame_listener; |
| 392 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 393 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 394 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 395 | { |
| 396 | struct window *window = data; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 397 | struct display *display = window->display; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 398 | static const GLfloat verts[3][2] = { |
| 399 | { -0.5, -0.5 }, |
| 400 | { 0.5, -0.5 }, |
| 401 | { 0, 0.5 } |
| 402 | }; |
| 403 | static const GLfloat colors[3][3] = { |
| 404 | { 1, 0, 0 }, |
| 405 | { 0, 1, 0 }, |
| 406 | { 0, 0, 1 } |
| 407 | }; |
| 408 | GLfloat angle; |
| 409 | GLfloat rotation[4][4] = { |
| 410 | { 1, 0, 0, 0 }, |
| 411 | { 0, 1, 0, 0 }, |
| 412 | { 0, 0, 1, 0 }, |
| 413 | { 0, 0, 0, 1 } |
| 414 | }; |
Jonas Ådahl | 82fced4 | 2014-01-03 19:46:50 +0100 | [diff] [blame^] | 415 | static const uint32_t speed_div = 5, benchmark_interval = 5; |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 416 | struct wl_region *region; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 417 | EGLint rect[4]; |
| 418 | EGLint buffer_age = 0; |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 419 | struct timeval tv; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 420 | |
Scott Moreau | 7e300db | 2012-08-31 03:18:15 -0600 | [diff] [blame] | 421 | assert(window->callback == callback); |
| 422 | window->callback = NULL; |
| 423 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 424 | if (callback) |
| 425 | wl_callback_destroy(callback); |
| 426 | |
| 427 | if (!window->configured) |
| 428 | return; |
| 429 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 430 | gettimeofday(&tv, NULL); |
| 431 | time = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 432 | if (window->frames == 0) |
| 433 | window->benchmark_time = time; |
| 434 | if (time - window->benchmark_time > (benchmark_interval * 1000)) { |
| 435 | printf("%d frames in %d seconds: %f fps\n", |
| 436 | window->frames, |
| 437 | benchmark_interval, |
| 438 | (float) window->frames / benchmark_interval); |
| 439 | window->benchmark_time = time; |
| 440 | window->frames = 0; |
| 441 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 442 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 443 | angle = (time / speed_div) % 360 * M_PI / 180.0; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 444 | rotation[0][0] = cos(angle); |
| 445 | rotation[0][2] = sin(angle); |
| 446 | rotation[2][0] = -sin(angle); |
| 447 | rotation[2][2] = cos(angle); |
| 448 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 449 | if (display->swap_buffers_with_damage) |
| 450 | eglQuerySurface(display->egl.dpy, window->egl_surface, |
| 451 | EGL_BUFFER_AGE_EXT, &buffer_age); |
| 452 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 453 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
| 454 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 455 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 456 | (GLfloat *) rotation); |
| 457 | |
| 458 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 459 | glClear(GL_COLOR_BUFFER_BIT); |
| 460 | |
| 461 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 462 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 463 | glEnableVertexAttribArray(window->gl.pos); |
| 464 | glEnableVertexAttribArray(window->gl.col); |
| 465 | |
| 466 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 467 | |
| 468 | glDisableVertexAttribArray(window->gl.pos); |
| 469 | glDisableVertexAttribArray(window->gl.col); |
| 470 | |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 471 | if (window->opaque || window->fullscreen) { |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 472 | region = wl_compositor_create_region(window->display->compositor); |
| 473 | wl_region_add(region, 0, 0, |
Ander Conselvan de Oliveira | edce9c2 | 2012-09-07 17:32:16 +0300 | [diff] [blame] | 474 | window->geometry.width, |
| 475 | window->geometry.height); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 476 | wl_surface_set_opaque_region(window->surface, region); |
| 477 | wl_region_destroy(region); |
Scott Moreau | 6655e00 | 2012-11-19 14:17:52 -0700 | [diff] [blame] | 478 | } else { |
| 479 | wl_surface_set_opaque_region(window->surface, NULL); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 480 | } |
| 481 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 482 | if (display->swap_buffers_with_damage && buffer_age > 0) { |
| 483 | rect[0] = window->geometry.width / 4 - 1; |
| 484 | rect[1] = window->geometry.height / 4 - 1; |
| 485 | rect[2] = window->geometry.width / 2 + 2; |
| 486 | rect[3] = window->geometry.height / 2 + 2; |
| 487 | display->swap_buffers_with_damage(display->egl.dpy, |
| 488 | window->egl_surface, |
| 489 | rect, 1); |
| 490 | } else { |
| 491 | eglSwapBuffers(display->egl.dpy, window->egl_surface); |
| 492 | } |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 493 | window->frames++; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 496 | static const struct wl_callback_listener frame_listener = { |
| 497 | redraw |
| 498 | }; |
| 499 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 500 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 501 | pointer_handle_enter(void *data, struct wl_pointer *pointer, |
| 502 | uint32_t serial, struct wl_surface *surface, |
| 503 | wl_fixed_t sx, wl_fixed_t sy) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 504 | { |
| 505 | struct display *display = data; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 506 | struct wl_buffer *buffer; |
| 507 | struct wl_cursor *cursor = display->default_cursor; |
| 508 | struct wl_cursor_image *image; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 509 | |
| 510 | if (display->window->fullscreen) |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 511 | wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 512 | else if (cursor) { |
| 513 | image = display->default_cursor->images[0]; |
| 514 | buffer = wl_cursor_image_get_buffer(image); |
| 515 | wl_pointer_set_cursor(pointer, serial, |
| 516 | display->cursor_surface, |
| 517 | image->hotspot_x, |
| 518 | image->hotspot_y); |
| 519 | wl_surface_attach(display->cursor_surface, buffer, 0, 0); |
| 520 | wl_surface_damage(display->cursor_surface, 0, 0, |
| 521 | image->width, image->height); |
| 522 | wl_surface_commit(display->cursor_surface); |
| 523 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 527 | pointer_handle_leave(void *data, struct wl_pointer *pointer, |
| 528 | uint32_t serial, struct wl_surface *surface) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 529 | { |
| 530 | } |
| 531 | |
| 532 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 533 | pointer_handle_motion(void *data, struct wl_pointer *pointer, |
| 534 | 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] | 535 | { |
| 536 | } |
| 537 | |
| 538 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 539 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 540 | uint32_t serial, uint32_t time, uint32_t button, |
| 541 | uint32_t state) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 542 | { |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 543 | struct display *display = data; |
| 544 | |
| 545 | if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 546 | wl_shell_surface_move(display->window->shell_surface, |
| 547 | display->seat, serial); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 551 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 552 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 553 | { |
| 554 | } |
| 555 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 556 | static const struct wl_pointer_listener pointer_listener = { |
| 557 | pointer_handle_enter, |
| 558 | pointer_handle_leave, |
| 559 | pointer_handle_motion, |
| 560 | pointer_handle_button, |
| 561 | pointer_handle_axis, |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | static void |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 565 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 566 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 567 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 568 | { |
| 569 | struct display *d = (struct display *)data; |
| 570 | |
| 571 | wl_shell_surface_move(d->window->shell_surface, d->seat, serial); |
| 572 | } |
| 573 | |
| 574 | static void |
| 575 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 576 | uint32_t serial, uint32_t time, int32_t id) |
| 577 | { |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 582 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 583 | { |
| 584 | } |
| 585 | |
| 586 | static void |
| 587 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 588 | { |
| 589 | } |
| 590 | |
| 591 | static void |
| 592 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 593 | { |
| 594 | } |
| 595 | |
| 596 | static const struct wl_touch_listener touch_listener = { |
| 597 | touch_handle_down, |
| 598 | touch_handle_up, |
| 599 | touch_handle_motion, |
| 600 | touch_handle_frame, |
| 601 | touch_handle_cancel, |
| 602 | }; |
| 603 | |
| 604 | static void |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 605 | keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, |
| 606 | uint32_t format, int fd, uint32_t size) |
| 607 | { |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, |
| 612 | uint32_t serial, struct wl_surface *surface, |
| 613 | struct wl_array *keys) |
| 614 | { |
| 615 | } |
| 616 | |
| 617 | static void |
| 618 | keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, |
| 619 | uint32_t serial, struct wl_surface *surface) |
| 620 | { |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | keyboard_handle_key(void *data, struct wl_keyboard *keyboard, |
| 625 | uint32_t serial, uint32_t time, uint32_t key, |
| 626 | uint32_t state) |
| 627 | { |
| 628 | struct display *d = data; |
| 629 | |
| 630 | if (key == KEY_F11 && state) |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 631 | set_fullscreen(d->window, d->window->fullscreen ^ 1); |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 632 | else if (key == KEY_ESC && state) |
| 633 | running = 0; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static void |
| 637 | keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 638 | uint32_t serial, uint32_t mods_depressed, |
| 639 | uint32_t mods_latched, uint32_t mods_locked, |
| 640 | uint32_t group) |
| 641 | { |
| 642 | } |
| 643 | |
| 644 | static const struct wl_keyboard_listener keyboard_listener = { |
| 645 | keyboard_handle_keymap, |
| 646 | keyboard_handle_enter, |
| 647 | keyboard_handle_leave, |
| 648 | keyboard_handle_key, |
| 649 | keyboard_handle_modifiers, |
| 650 | }; |
| 651 | |
| 652 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 653 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 654 | enum wl_seat_capability caps) |
| 655 | { |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 656 | struct display *d = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 657 | |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 658 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) { |
| 659 | d->pointer = wl_seat_get_pointer(seat); |
| 660 | wl_pointer_add_listener(d->pointer, &pointer_listener, d); |
| 661 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) { |
| 662 | wl_pointer_destroy(d->pointer); |
| 663 | d->pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 664 | } |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 665 | |
| 666 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) { |
| 667 | d->keyboard = wl_seat_get_keyboard(seat); |
| 668 | wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); |
| 669 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) { |
| 670 | wl_keyboard_destroy(d->keyboard); |
| 671 | d->keyboard = NULL; |
| 672 | } |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 673 | |
| 674 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) { |
| 675 | d->touch = wl_seat_get_touch(seat); |
| 676 | wl_touch_set_user_data(d->touch, d); |
| 677 | wl_touch_add_listener(d->touch, &touch_listener, d); |
| 678 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) { |
| 679 | wl_touch_destroy(d->touch); |
| 680 | d->touch = NULL; |
| 681 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static const struct wl_seat_listener seat_listener = { |
| 685 | seat_handle_capabilities, |
| 686 | }; |
| 687 | |
| 688 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 689 | registry_handle_global(void *data, struct wl_registry *registry, |
| 690 | uint32_t name, const char *interface, uint32_t version) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 691 | { |
| 692 | struct display *d = data; |
| 693 | |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 694 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 695 | d->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 696 | wl_registry_bind(registry, name, |
| 697 | &wl_compositor_interface, 1); |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 698 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 699 | d->shell = wl_registry_bind(registry, name, |
| 700 | &wl_shell_interface, 1); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 701 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 702 | d->seat = wl_registry_bind(registry, name, |
| 703 | &wl_seat_interface, 1); |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 704 | wl_seat_add_listener(d->seat, &seat_listener, d); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 705 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 706 | d->shm = wl_registry_bind(registry, name, |
| 707 | &wl_shm_interface, 1); |
| 708 | d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm); |
| 709 | d->default_cursor = |
| 710 | wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr"); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 711 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 714 | static void |
| 715 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 716 | uint32_t name) |
| 717 | { |
| 718 | } |
| 719 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 720 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 721 | registry_handle_global, |
| 722 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 723 | }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 724 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 725 | static void |
| 726 | signal_int(int signum) |
| 727 | { |
| 728 | running = 0; |
| 729 | } |
| 730 | |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 731 | static void |
| 732 | usage(int error_code) |
| 733 | { |
| 734 | fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n" |
| 735 | " -f\tRun in fullscreen mode\n" |
| 736 | " -o\tCreate an opaque surface\n" |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 737 | " -s\tUse a 16 bpp EGL config\n" |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 738 | " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n" |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 739 | " -h\tThis help text\n\n"); |
| 740 | |
| 741 | exit(error_code); |
| 742 | } |
| 743 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 744 | int |
| 745 | main(int argc, char **argv) |
| 746 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 747 | struct sigaction sigint; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 748 | struct display display = { 0 }; |
| 749 | struct window window = { 0 }; |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 750 | int i, ret = 0; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 751 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 752 | window.display = &display; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 753 | display.window = &window; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 754 | window.window_size.width = 250; |
| 755 | window.window_size.height = 250; |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 756 | window.buffer_size = 32; |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 757 | window.frame_sync = 1; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 758 | |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 759 | for (i = 1; i < argc; i++) { |
| 760 | if (strcmp("-f", argv[i]) == 0) |
| 761 | window.fullscreen = 1; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 762 | else if (strcmp("-o", argv[i]) == 0) |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 763 | window.opaque = 1; |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 764 | else if (strcmp("-s", argv[i]) == 0) |
| 765 | window.buffer_size = 16; |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 766 | else if (strcmp("-b", argv[i]) == 0) |
| 767 | window.frame_sync = 0; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 768 | else if (strcmp("-h", argv[i]) == 0) |
| 769 | usage(EXIT_SUCCESS); |
| 770 | else |
| 771 | usage(EXIT_FAILURE); |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 772 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 773 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 774 | display.display = wl_display_connect(NULL); |
| 775 | assert(display.display); |
| 776 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 777 | display.registry = wl_display_get_registry(display.display); |
| 778 | wl_registry_add_listener(display.registry, |
| 779 | ®istry_listener, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 780 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 781 | wl_display_dispatch(display.display); |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 782 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 783 | init_egl(&display, &window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 784 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 785 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 786 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 787 | display.cursor_surface = |
| 788 | wl_compositor_create_surface(display.compositor); |
| 789 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 790 | sigint.sa_handler = signal_int; |
| 791 | sigemptyset(&sigint.sa_mask); |
| 792 | sigint.sa_flags = SA_RESETHAND; |
| 793 | sigaction(SIGINT, &sigint, NULL); |
| 794 | |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 795 | /* The mainloop here is a little subtle. Redrawing will cause |
| 796 | * EGL to read events so we can just call |
| 797 | * wl_display_dispatch_pending() to handle any events that got |
| 798 | * queued up as a side effect. */ |
| 799 | while (running && ret != -1) { |
| 800 | wl_display_dispatch_pending(display.display); |
| 801 | while (!window.configured) |
| 802 | wl_display_dispatch(display.display); |
| 803 | redraw(&window, NULL, 0); |
| 804 | } |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 805 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 806 | fprintf(stderr, "simple-egl exiting\n"); |
| 807 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 808 | destroy_surface(&window); |
| 809 | fini_egl(&display); |
| 810 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 811 | wl_surface_destroy(display.cursor_surface); |
| 812 | if (display.cursor_theme) |
| 813 | wl_cursor_theme_destroy(display.cursor_theme); |
| 814 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 815 | if (display.shell) |
| 816 | wl_shell_destroy(display.shell); |
| 817 | |
| 818 | if (display.compositor) |
| 819 | wl_compositor_destroy(display.compositor); |
| 820 | |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 821 | wl_registry_destroy(display.registry); |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 822 | wl_display_flush(display.display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 823 | wl_display_disconnect(display.display); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 824 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 825 | return 0; |
| 826 | } |