Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Benjamin Franzke |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 26 | #include <stdint.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 30 | #include <stdbool.h> |
| 31 | #include <math.h> |
| 32 | #include <assert.h> |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 33 | #include <signal.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 34 | |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 35 | #include <linux/input.h> |
| 36 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 37 | #include <wayland-client.h> |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 38 | #include <wayland-egl.h> |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 39 | #include <wayland-cursor.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 40 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 41 | #include <GLES2/gl2.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 42 | #include <EGL/egl.h> |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 43 | #include <EGL/eglext.h> |
| 44 | |
Jonas Ådahl | 2a22933 | 2015-11-17 16:00:32 +0800 | [diff] [blame] | 45 | #include "xdg-shell-unstable-v5-client-protocol.h" |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <unistd.h> |
| 48 | #include "protocol/ivi-application-client-protocol.h" |
| 49 | #define IVI_SURFACE_ID 9000 |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 50 | |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 51 | #include "shared/platform.h" |
Emil Velikov | 0725cf1 | 2016-07-04 15:27:15 +0100 | [diff] [blame] | 52 | #include "weston-egl-ext.h" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 53 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 54 | struct window; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 55 | struct seat; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 56 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 57 | struct display { |
| 58 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 59 | struct wl_registry *registry; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 60 | struct wl_compositor *compositor; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 61 | struct xdg_shell *shell; |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 62 | struct wl_seat *seat; |
| 63 | struct wl_pointer *pointer; |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 64 | struct wl_touch *touch; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 65 | struct wl_keyboard *keyboard; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 66 | struct wl_shm *shm; |
| 67 | struct wl_cursor_theme *cursor_theme; |
| 68 | struct wl_cursor *default_cursor; |
| 69 | struct wl_surface *cursor_surface; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 70 | struct { |
| 71 | EGLDisplay dpy; |
| 72 | EGLContext ctx; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 73 | EGLConfig conf; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 74 | } egl; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 75 | struct window *window; |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 76 | struct ivi_application *ivi_application; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 77 | |
| 78 | PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 81 | struct geometry { |
| 82 | int width, height; |
| 83 | }; |
| 84 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 85 | struct window { |
| 86 | struct display *display; |
Scott Moreau | 1ee53e7 | 2012-08-30 14:44:15 -0600 | [diff] [blame] | 87 | struct geometry geometry, window_size; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 88 | struct { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 89 | GLuint rotation_uniform; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 90 | GLuint pos; |
| 91 | GLuint col; |
| 92 | } gl; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 93 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 94 | uint32_t benchmark_time, frames; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 95 | struct wl_egl_window *native; |
| 96 | struct wl_surface *surface; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 97 | struct xdg_surface *xdg_surface; |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 98 | struct ivi_surface *ivi_surface; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 99 | EGLSurface egl_surface; |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 100 | struct wl_callback *callback; |
Jasper St. Pierre | 8c6aa45 | 2014-02-08 18:29:49 -0500 | [diff] [blame] | 101 | int fullscreen, opaque, buffer_size, frame_sync; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static const char *vert_shader_text = |
| 105 | "uniform mat4 rotation;\n" |
| 106 | "attribute vec4 pos;\n" |
| 107 | "attribute vec4 color;\n" |
| 108 | "varying vec4 v_color;\n" |
| 109 | "void main() {\n" |
| 110 | " gl_Position = rotation * pos;\n" |
| 111 | " v_color = color;\n" |
| 112 | "}\n"; |
| 113 | |
| 114 | static const char *frag_shader_text = |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 115 | "precision mediump float;\n" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 116 | "varying vec4 v_color;\n" |
| 117 | "void main() {\n" |
| 118 | " gl_FragColor = v_color;\n" |
| 119 | "}\n"; |
| 120 | |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 121 | static int running = 1; |
| 122 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 123 | static void |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 124 | init_egl(struct display *display, struct window *window) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 125 | { |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 126 | static const EGLint context_attribs[] = { |
| 127 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 128 | EGL_NONE |
| 129 | }; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 130 | const char *extensions; |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 131 | |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 132 | EGLint config_attribs[] = { |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 133 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 134 | EGL_RED_SIZE, 1, |
| 135 | EGL_GREEN_SIZE, 1, |
| 136 | EGL_BLUE_SIZE, 1, |
Arnaud Vrac | 488b7cd | 2014-08-25 20:56:48 +0200 | [diff] [blame] | 137 | EGL_ALPHA_SIZE, 1, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 138 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 139 | EGL_NONE |
| 140 | }; |
| 141 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 142 | EGLint major, minor, n, count, i, size; |
| 143 | EGLConfig *configs; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 144 | EGLBoolean ret; |
| 145 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 146 | if (window->opaque || window->buffer_size == 16) |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 147 | config_attribs[9] = 0; |
| 148 | |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 149 | display->egl.dpy = |
| 150 | weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, |
| 151 | display->display, NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 152 | assert(display->egl.dpy); |
| 153 | |
| 154 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 155 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 156 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 157 | assert(ret == EGL_TRUE); |
| 158 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 159 | if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1) |
| 160 | assert(0); |
| 161 | |
| 162 | configs = calloc(count, sizeof *configs); |
| 163 | assert(configs); |
| 164 | |
Pekka Paalanen | b79b635 | 2012-06-12 17:42:24 +0300 | [diff] [blame] | 165 | ret = eglChooseConfig(display->egl.dpy, config_attribs, |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 166 | configs, count, &n); |
| 167 | assert(ret && n >= 1); |
| 168 | |
| 169 | for (i = 0; i < n; i++) { |
| 170 | eglGetConfigAttrib(display->egl.dpy, |
| 171 | configs[i], EGL_BUFFER_SIZE, &size); |
| 172 | if (window->buffer_size == size) { |
| 173 | display->egl.conf = configs[i]; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | free(configs); |
| 178 | if (display->egl.conf == NULL) { |
| 179 | fprintf(stderr, "did not find config with buffer size %d\n", |
| 180 | window->buffer_size); |
| 181 | exit(EXIT_FAILURE); |
| 182 | } |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 183 | |
| 184 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 185 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 186 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 187 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 188 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 189 | display->swap_buffers_with_damage = NULL; |
| 190 | extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS); |
| 191 | if (extensions && |
Emil Velikov | 025ad93 | 2016-07-04 15:34:21 +0100 | [diff] [blame] | 192 | weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage") && |
| 193 | weston_check_egl_extension(extensions, "EGL_EXT_buffer_age")) |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 194 | display->swap_buffers_with_damage = |
| 195 | (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC) |
| 196 | eglGetProcAddress("eglSwapBuffersWithDamageEXT"); |
| 197 | |
| 198 | if (display->swap_buffers_with_damage) |
| 199 | printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n"); |
| 200 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 203 | static void |
| 204 | fini_egl(struct display *display) |
| 205 | { |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 206 | eglTerminate(display->egl.dpy); |
| 207 | eglReleaseThread(); |
| 208 | } |
| 209 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 210 | static GLuint |
| 211 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 212 | { |
| 213 | GLuint shader; |
| 214 | GLint status; |
| 215 | |
| 216 | shader = glCreateShader(shader_type); |
| 217 | assert(shader != 0); |
| 218 | |
| 219 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 220 | glCompileShader(shader); |
| 221 | |
| 222 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 223 | if (!status) { |
| 224 | char log[1000]; |
| 225 | GLsizei len; |
| 226 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 227 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 228 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 229 | len, log); |
| 230 | exit(1); |
| 231 | } |
| 232 | |
| 233 | return shader; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | init_gl(struct window *window) |
| 238 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 239 | GLuint frag, vert; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 240 | GLuint program; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 241 | GLint status; |
| 242 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 243 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 244 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 245 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 246 | program = glCreateProgram(); |
| 247 | glAttachShader(program, frag); |
| 248 | glAttachShader(program, vert); |
| 249 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 250 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 251 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 252 | if (!status) { |
| 253 | char log[1000]; |
| 254 | GLsizei len; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 255 | glGetProgramInfoLog(program, 1000, &len, log); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 256 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 257 | exit(1); |
| 258 | } |
| 259 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 260 | glUseProgram(program); |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 261 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 262 | window->gl.pos = 0; |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 263 | window->gl.col = 1; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 264 | |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 265 | glBindAttribLocation(program, window->gl.pos, "pos"); |
| 266 | glBindAttribLocation(program, window->gl.col, "color"); |
| 267 | glLinkProgram(program); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 268 | |
| 269 | window->gl.rotation_uniform = |
Scott Moreau | 3ea23d0 | 2012-06-13 17:42:21 -0600 | [diff] [blame] | 270 | glGetUniformLocation(program, "rotation"); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 274 | handle_surface_configure(void *data, struct xdg_surface *surface, |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 275 | int32_t width, int32_t height, |
| 276 | struct wl_array *states, uint32_t serial) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 277 | { |
| 278 | struct window *window = data; |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 279 | uint32_t *p; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 280 | |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 281 | window->fullscreen = 0; |
| 282 | wl_array_for_each(p, states) { |
| 283 | uint32_t state = *p; |
| 284 | switch (state) { |
| 285 | case XDG_SURFACE_STATE_FULLSCREEN: |
| 286 | window->fullscreen = 1; |
| 287 | break; |
| 288 | } |
Jasper St. Pierre | 8c6aa45 | 2014-02-08 18:29:49 -0500 | [diff] [blame] | 289 | } |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 290 | |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 291 | if (width > 0 && height > 0) { |
| 292 | if (!window->fullscreen) { |
| 293 | window->window_size.width = width; |
| 294 | window->window_size.height = height; |
| 295 | } |
| 296 | window->geometry.width = width; |
| 297 | window->geometry.height = height; |
| 298 | } else if (!window->fullscreen) { |
| 299 | window->geometry = window->window_size; |
| 300 | } |
| 301 | |
| 302 | if (window->native) |
| 303 | wl_egl_window_resize(window->native, |
| 304 | window->geometry.width, |
| 305 | window->geometry.height, 0, 0); |
| 306 | |
| 307 | xdg_surface_ack_configure(surface, serial); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 311 | handle_surface_delete(void *data, struct xdg_surface *xdg_surface) |
| 312 | { |
| 313 | running = 0; |
| 314 | } |
| 315 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 316 | static const struct xdg_surface_listener xdg_surface_listener = { |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 317 | handle_surface_configure, |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 318 | handle_surface_delete, |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | static void |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 322 | handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface, |
| 323 | int32_t width, int32_t height) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 324 | { |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 325 | struct window *window = data; |
| 326 | |
| 327 | wl_egl_window_resize(window->native, width, height, 0, 0); |
| 328 | |
| 329 | window->geometry.width = width; |
| 330 | window->geometry.height = height; |
| 331 | |
| 332 | if (!window->fullscreen) |
| 333 | window->window_size = window->geometry; |
| 334 | } |
| 335 | |
| 336 | static const struct ivi_surface_listener ivi_surface_listener = { |
| 337 | handle_ivi_surface_configure, |
| 338 | }; |
| 339 | |
| 340 | static void |
| 341 | create_xdg_surface(struct window *window, struct display *display) |
| 342 | { |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 343 | window->xdg_surface = xdg_shell_get_xdg_surface(display->shell, |
| 344 | window->surface); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 345 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 346 | xdg_surface_add_listener(window->xdg_surface, |
| 347 | &xdg_surface_listener, window); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 348 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 349 | xdg_surface_set_title(window->xdg_surface, "simple-egl"); |
| 350 | } |
| 351 | |
| 352 | static void |
| 353 | create_ivi_surface(struct window *window, struct display *display) |
| 354 | { |
| 355 | uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid(); |
| 356 | window->ivi_surface = |
| 357 | ivi_application_surface_create(display->ivi_application, |
| 358 | id_ivisurf, window->surface); |
| 359 | |
| 360 | if (window->ivi_surface == NULL) { |
| 361 | fprintf(stderr, "Failed to create ivi_client_surface\n"); |
| 362 | abort(); |
| 363 | } |
| 364 | |
| 365 | ivi_surface_add_listener(window->ivi_surface, |
| 366 | &ivi_surface_listener, window); |
| 367 | } |
| 368 | |
| 369 | static void |
| 370 | create_surface(struct window *window) |
| 371 | { |
| 372 | struct display *display = window->display; |
| 373 | EGLBoolean ret; |
| 374 | |
| 375 | window->surface = wl_compositor_create_surface(display->compositor); |
| 376 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 377 | window->native = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 378 | wl_egl_window_create(window->surface, |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 379 | window->geometry.width, |
| 380 | window->geometry.height); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 381 | window->egl_surface = |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 382 | weston_platform_create_egl_surface(display->egl.dpy, |
| 383 | display->egl.conf, |
| 384 | window->native, NULL); |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 385 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 386 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 387 | if (display->shell) { |
| 388 | create_xdg_surface(window, display); |
| 389 | } else if (display->ivi_application ) { |
| 390 | create_ivi_surface(window, display); |
| 391 | } else { |
| 392 | assert(0); |
| 393 | } |
Scott Moreau | 01a9f1b | 2012-10-07 08:56:30 -0600 | [diff] [blame] | 394 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 395 | ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, |
| 396 | window->egl_surface, window->display->egl.ctx); |
| 397 | assert(ret == EGL_TRUE); |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 398 | |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 399 | if (!window->frame_sync) |
| 400 | eglSwapInterval(display->egl.dpy, 0); |
| 401 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 402 | if (!display->shell) |
| 403 | return; |
| 404 | |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 405 | if (window->fullscreen) |
| 406 | xdg_surface_set_fullscreen(window->xdg_surface, NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 409 | static void |
| 410 | destroy_surface(struct window *window) |
| 411 | { |
Yeh, Sinclair | 952e6df | 2013-04-19 17:49:12 +0000 | [diff] [blame] | 412 | /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() |
| 413 | * on eglReleaseThread(). */ |
| 414 | eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 415 | EGL_NO_CONTEXT); |
| 416 | |
| 417 | eglDestroySurface(window->display->egl.dpy, window->egl_surface); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 418 | wl_egl_window_destroy(window->native); |
| 419 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 420 | if (window->xdg_surface) |
| 421 | xdg_surface_destroy(window->xdg_surface); |
| 422 | if (window->display->ivi_application) |
| 423 | ivi_surface_destroy(window->ivi_surface); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 424 | wl_surface_destroy(window->surface); |
| 425 | |
| 426 | if (window->callback) |
| 427 | wl_callback_destroy(window->callback); |
| 428 | } |
| 429 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 430 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 431 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 432 | { |
| 433 | struct window *window = data; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 434 | struct display *display = window->display; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 435 | static const GLfloat verts[3][2] = { |
| 436 | { -0.5, -0.5 }, |
| 437 | { 0.5, -0.5 }, |
| 438 | { 0, 0.5 } |
| 439 | }; |
| 440 | static const GLfloat colors[3][3] = { |
| 441 | { 1, 0, 0 }, |
| 442 | { 0, 1, 0 }, |
| 443 | { 0, 0, 1 } |
| 444 | }; |
| 445 | GLfloat angle; |
| 446 | GLfloat rotation[4][4] = { |
| 447 | { 1, 0, 0, 0 }, |
| 448 | { 0, 1, 0, 0 }, |
| 449 | { 0, 0, 1, 0 }, |
| 450 | { 0, 0, 0, 1 } |
| 451 | }; |
Jonas Ådahl | 82fced4 | 2014-01-03 19:46:50 +0100 | [diff] [blame] | 452 | static const uint32_t speed_div = 5, benchmark_interval = 5; |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 453 | struct wl_region *region; |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 454 | EGLint rect[4]; |
| 455 | EGLint buffer_age = 0; |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 456 | struct timeval tv; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 457 | |
Scott Moreau | 7e300db | 2012-08-31 03:18:15 -0600 | [diff] [blame] | 458 | assert(window->callback == callback); |
| 459 | window->callback = NULL; |
| 460 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 461 | if (callback) |
| 462 | wl_callback_destroy(callback); |
| 463 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 464 | gettimeofday(&tv, NULL); |
| 465 | time = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 466 | if (window->frames == 0) |
| 467 | window->benchmark_time = time; |
| 468 | if (time - window->benchmark_time > (benchmark_interval * 1000)) { |
| 469 | printf("%d frames in %d seconds: %f fps\n", |
| 470 | window->frames, |
| 471 | benchmark_interval, |
| 472 | (float) window->frames / benchmark_interval); |
| 473 | window->benchmark_time = time; |
| 474 | window->frames = 0; |
| 475 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 476 | |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 477 | angle = (time / speed_div) % 360 * M_PI / 180.0; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 478 | rotation[0][0] = cos(angle); |
| 479 | rotation[0][2] = sin(angle); |
| 480 | rotation[2][0] = -sin(angle); |
| 481 | rotation[2][2] = cos(angle); |
| 482 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 483 | if (display->swap_buffers_with_damage) |
| 484 | eglQuerySurface(display->egl.dpy, window->egl_surface, |
| 485 | EGL_BUFFER_AGE_EXT, &buffer_age); |
| 486 | |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 487 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
| 488 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 489 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 490 | (GLfloat *) rotation); |
| 491 | |
| 492 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 493 | glClear(GL_COLOR_BUFFER_BIT); |
| 494 | |
| 495 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 496 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 497 | glEnableVertexAttribArray(window->gl.pos); |
| 498 | glEnableVertexAttribArray(window->gl.col); |
| 499 | |
| 500 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 501 | |
| 502 | glDisableVertexAttribArray(window->gl.pos); |
| 503 | glDisableVertexAttribArray(window->gl.col); |
| 504 | |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 505 | if (window->opaque || window->fullscreen) { |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 506 | region = wl_compositor_create_region(window->display->compositor); |
| 507 | wl_region_add(region, 0, 0, |
Ander Conselvan de Oliveira | edce9c2 | 2012-09-07 17:32:16 +0300 | [diff] [blame] | 508 | window->geometry.width, |
| 509 | window->geometry.height); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 510 | wl_surface_set_opaque_region(window->surface, region); |
| 511 | wl_region_destroy(region); |
Scott Moreau | 6655e00 | 2012-11-19 14:17:52 -0700 | [diff] [blame] | 512 | } else { |
| 513 | wl_surface_set_opaque_region(window->surface, NULL); |
Kristian Høgsberg | 45ce988 | 2012-08-03 15:27:14 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Kristian Høgsberg | 9e885d4 | 2013-05-08 11:37:28 -0400 | [diff] [blame] | 516 | if (display->swap_buffers_with_damage && buffer_age > 0) { |
| 517 | rect[0] = window->geometry.width / 4 - 1; |
| 518 | rect[1] = window->geometry.height / 4 - 1; |
| 519 | rect[2] = window->geometry.width / 2 + 2; |
| 520 | rect[3] = window->geometry.height / 2 + 2; |
| 521 | display->swap_buffers_with_damage(display->egl.dpy, |
| 522 | window->egl_surface, |
| 523 | rect, 1); |
| 524 | } else { |
| 525 | eglSwapBuffers(display->egl.dpy, window->egl_surface); |
| 526 | } |
Kristian Høgsberg | deb3222 | 2013-12-06 22:02:45 -0800 | [diff] [blame] | 527 | window->frames++; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 531 | pointer_handle_enter(void *data, struct wl_pointer *pointer, |
| 532 | uint32_t serial, struct wl_surface *surface, |
| 533 | wl_fixed_t sx, wl_fixed_t sy) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 534 | { |
| 535 | struct display *display = data; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 536 | struct wl_buffer *buffer; |
| 537 | struct wl_cursor *cursor = display->default_cursor; |
| 538 | struct wl_cursor_image *image; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 539 | |
| 540 | if (display->window->fullscreen) |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 541 | wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 542 | else if (cursor) { |
| 543 | image = display->default_cursor->images[0]; |
| 544 | buffer = wl_cursor_image_get_buffer(image); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 545 | if (!buffer) |
| 546 | return; |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 547 | wl_pointer_set_cursor(pointer, serial, |
| 548 | display->cursor_surface, |
| 549 | image->hotspot_x, |
| 550 | image->hotspot_y); |
| 551 | wl_surface_attach(display->cursor_surface, buffer, 0, 0); |
| 552 | wl_surface_damage(display->cursor_surface, 0, 0, |
| 553 | image->width, image->height); |
| 554 | wl_surface_commit(display->cursor_surface); |
| 555 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 559 | pointer_handle_leave(void *data, struct wl_pointer *pointer, |
| 560 | uint32_t serial, struct wl_surface *surface) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 561 | { |
| 562 | } |
| 563 | |
| 564 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 565 | pointer_handle_motion(void *data, struct wl_pointer *pointer, |
| 566 | 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] | 567 | { |
| 568 | } |
| 569 | |
| 570 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 571 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 572 | uint32_t serial, uint32_t time, uint32_t button, |
| 573 | uint32_t state) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 574 | { |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 575 | struct display *display = data; |
| 576 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 577 | if (!display->window->xdg_surface) |
| 578 | return; |
| 579 | |
Ander Conselvan de Oliveira | 57e0ce1 | 2012-06-26 17:09:11 +0300 | [diff] [blame] | 580 | if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 581 | xdg_surface_move(display->window->xdg_surface, |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 582 | display->seat, serial); |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 586 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 587 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 588 | { |
| 589 | } |
| 590 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 591 | static const struct wl_pointer_listener pointer_listener = { |
| 592 | pointer_handle_enter, |
| 593 | pointer_handle_leave, |
| 594 | pointer_handle_motion, |
| 595 | pointer_handle_button, |
| 596 | pointer_handle_axis, |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | static void |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 600 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 601 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 602 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 603 | { |
| 604 | struct display *d = (struct display *)data; |
| 605 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 606 | if (!d->shell) |
| 607 | return; |
| 608 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 609 | xdg_surface_move(d->window->xdg_surface, d->seat, serial); |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static void |
| 613 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 614 | uint32_t serial, uint32_t time, int32_t id) |
| 615 | { |
| 616 | } |
| 617 | |
| 618 | static void |
| 619 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 620 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 621 | { |
| 622 | } |
| 623 | |
| 624 | static void |
| 625 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 626 | { |
| 627 | } |
| 628 | |
| 629 | static void |
| 630 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 631 | { |
| 632 | } |
| 633 | |
| 634 | static const struct wl_touch_listener touch_listener = { |
| 635 | touch_handle_down, |
| 636 | touch_handle_up, |
| 637 | touch_handle_motion, |
| 638 | touch_handle_frame, |
| 639 | touch_handle_cancel, |
| 640 | }; |
| 641 | |
| 642 | static void |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 643 | keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, |
| 644 | uint32_t format, int fd, uint32_t size) |
| 645 | { |
| 646 | } |
| 647 | |
| 648 | static void |
| 649 | keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, |
| 650 | uint32_t serial, struct wl_surface *surface, |
| 651 | struct wl_array *keys) |
| 652 | { |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, |
| 657 | uint32_t serial, struct wl_surface *surface) |
| 658 | { |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | keyboard_handle_key(void *data, struct wl_keyboard *keyboard, |
| 663 | uint32_t serial, uint32_t time, uint32_t key, |
| 664 | uint32_t state) |
| 665 | { |
| 666 | struct display *d = data; |
| 667 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 668 | if (!d->shell) |
| 669 | return; |
| 670 | |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 671 | if (key == KEY_F11 && state) { |
| 672 | if (d->window->fullscreen) |
| 673 | xdg_surface_unset_fullscreen(d->window->xdg_surface); |
| 674 | else |
| 675 | xdg_surface_set_fullscreen(d->window->xdg_surface, NULL); |
| 676 | } else if (key == KEY_ESC && state) |
Kristian Høgsberg | 321e8b7 | 2012-07-30 15:40:57 -0400 | [diff] [blame] | 677 | running = 0; |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static void |
| 681 | keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 682 | uint32_t serial, uint32_t mods_depressed, |
| 683 | uint32_t mods_latched, uint32_t mods_locked, |
| 684 | uint32_t group) |
| 685 | { |
| 686 | } |
| 687 | |
| 688 | static const struct wl_keyboard_listener keyboard_listener = { |
| 689 | keyboard_handle_keymap, |
| 690 | keyboard_handle_enter, |
| 691 | keyboard_handle_leave, |
| 692 | keyboard_handle_key, |
| 693 | keyboard_handle_modifiers, |
| 694 | }; |
| 695 | |
| 696 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 697 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 698 | enum wl_seat_capability caps) |
| 699 | { |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 700 | struct display *d = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 701 | |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 702 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) { |
| 703 | d->pointer = wl_seat_get_pointer(seat); |
| 704 | wl_pointer_add_listener(d->pointer, &pointer_listener, d); |
| 705 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) { |
| 706 | wl_pointer_destroy(d->pointer); |
| 707 | d->pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 708 | } |
Ander Conselvan de Oliveira | 69f9840 | 2012-07-27 17:18:13 +0300 | [diff] [blame] | 709 | |
| 710 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) { |
| 711 | d->keyboard = wl_seat_get_keyboard(seat); |
| 712 | wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); |
| 713 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) { |
| 714 | wl_keyboard_destroy(d->keyboard); |
| 715 | d->keyboard = NULL; |
| 716 | } |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 717 | |
| 718 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) { |
| 719 | d->touch = wl_seat_get_touch(seat); |
| 720 | wl_touch_set_user_data(d->touch, d); |
| 721 | wl_touch_add_listener(d->touch, &touch_listener, d); |
| 722 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) { |
| 723 | wl_touch_destroy(d->touch); |
| 724 | d->touch = NULL; |
| 725 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static const struct wl_seat_listener seat_listener = { |
| 729 | seat_handle_capabilities, |
| 730 | }; |
| 731 | |
| 732 | static void |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 733 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 734 | { |
| 735 | xdg_shell_pong(shell, serial); |
| 736 | } |
| 737 | |
| 738 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 739 | xdg_shell_ping, |
| 740 | }; |
| 741 | |
Jasper St. Pierre | 5ba1e1d | 2015-02-13 14:02:02 +0800 | [diff] [blame] | 742 | #define XDG_VERSION 5 /* The version of xdg-shell that we implement */ |
Kristian Høgsberg | 239902b | 2014-02-11 13:50:08 -0800 | [diff] [blame] | 743 | #ifdef static_assert |
| 744 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 745 | "Interface version doesn't match implementation version"); |
| 746 | #endif |
| 747 | |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 748 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 749 | registry_handle_global(void *data, struct wl_registry *registry, |
| 750 | uint32_t name, const char *interface, uint32_t version) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 751 | { |
| 752 | struct display *d = data; |
| 753 | |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 754 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 755 | d->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 756 | wl_registry_bind(registry, name, |
| 757 | &wl_compositor_interface, 1); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 758 | } else if (strcmp(interface, "xdg_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 759 | d->shell = wl_registry_bind(registry, name, |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 760 | &xdg_shell_interface, 1); |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 761 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
Kristian Høgsberg | 239902b | 2014-02-11 13:50:08 -0800 | [diff] [blame] | 762 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 763 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 764 | d->seat = wl_registry_bind(registry, name, |
| 765 | &wl_seat_interface, 1); |
Kristian Høgsberg | b84108d | 2012-05-16 16:16:19 -0400 | [diff] [blame] | 766 | wl_seat_add_listener(d->seat, &seat_listener, d); |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 767 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 768 | d->shm = wl_registry_bind(registry, name, |
| 769 | &wl_shm_interface, 1); |
| 770 | d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 771 | if (!d->cursor_theme) { |
| 772 | fprintf(stderr, "unable to load default theme\n"); |
| 773 | return; |
| 774 | } |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 775 | d->default_cursor = |
| 776 | wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr"); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 777 | if (!d->default_cursor) { |
| 778 | fprintf(stderr, "unable to load default left pointer\n"); |
| 779 | // TODO: abort ? |
| 780 | } |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 781 | } else if (strcmp(interface, "ivi_application") == 0) { |
| 782 | d->ivi_application = |
| 783 | wl_registry_bind(registry, name, |
| 784 | &ivi_application_interface, 1); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 785 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 788 | static void |
| 789 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 790 | uint32_t name) |
| 791 | { |
| 792 | } |
| 793 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 794 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 795 | registry_handle_global, |
| 796 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 797 | }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 798 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 799 | static void |
| 800 | signal_int(int signum) |
| 801 | { |
| 802 | running = 0; |
| 803 | } |
| 804 | |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 805 | static void |
| 806 | usage(int error_code) |
| 807 | { |
| 808 | fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n" |
| 809 | " -f\tRun in fullscreen mode\n" |
| 810 | " -o\tCreate an opaque surface\n" |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 811 | " -s\tUse a 16 bpp EGL config\n" |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 812 | " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n" |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 813 | " -h\tThis help text\n\n"); |
| 814 | |
| 815 | exit(error_code); |
| 816 | } |
| 817 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 818 | int |
| 819 | main(int argc, char **argv) |
| 820 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 821 | struct sigaction sigint; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 822 | struct display display = { 0 }; |
| 823 | struct window window = { 0 }; |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 824 | int i, ret = 0; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 825 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 826 | window.display = &display; |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 827 | display.window = &window; |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 828 | window.geometry.width = 250; |
| 829 | window.geometry.height = 250; |
| 830 | window.window_size = window.geometry; |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 831 | window.buffer_size = 32; |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 832 | window.frame_sync = 1; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 833 | |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 834 | for (i = 1; i < argc; i++) { |
| 835 | if (strcmp("-f", argv[i]) == 0) |
| 836 | window.fullscreen = 1; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 837 | else if (strcmp("-o", argv[i]) == 0) |
Ander Conselvan de Oliveira | d7f282b | 2012-09-10 15:55:53 +0300 | [diff] [blame] | 838 | window.opaque = 1; |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 839 | else if (strcmp("-s", argv[i]) == 0) |
| 840 | window.buffer_size = 16; |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 841 | else if (strcmp("-b", argv[i]) == 0) |
| 842 | window.frame_sync = 0; |
Kristian Høgsberg | bcf4864 | 2012-08-03 15:29:08 -0400 | [diff] [blame] | 843 | else if (strcmp("-h", argv[i]) == 0) |
| 844 | usage(EXIT_SUCCESS); |
| 845 | else |
| 846 | usage(EXIT_FAILURE); |
Kristian Høgsberg | 3593f81 | 2012-05-10 20:40:51 -0400 | [diff] [blame] | 847 | } |
Ander Conselvan de Oliveira | d51624f | 2012-05-02 16:42:23 +0300 | [diff] [blame] | 848 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 849 | display.display = wl_display_connect(NULL); |
| 850 | assert(display.display); |
| 851 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 852 | display.registry = wl_display_get_registry(display.display); |
| 853 | wl_registry_add_listener(display.registry, |
| 854 | ®istry_listener, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 855 | |
Marek Chalupa | c21cb3d | 2016-03-14 11:40:38 +0100 | [diff] [blame] | 856 | wl_display_roundtrip(display.display); |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame] | 857 | |
Kristian Høgsberg | 78fe753 | 2013-10-15 22:03:31 -0700 | [diff] [blame] | 858 | init_egl(&display, &window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 859 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 860 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 861 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 862 | display.cursor_surface = |
| 863 | wl_compositor_create_surface(display.compositor); |
| 864 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 865 | sigint.sa_handler = signal_int; |
| 866 | sigemptyset(&sigint.sa_mask); |
| 867 | sigint.sa_flags = SA_RESETHAND; |
| 868 | sigaction(SIGINT, &sigint, NULL); |
| 869 | |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 870 | /* The mainloop here is a little subtle. Redrawing will cause |
| 871 | * EGL to read events so we can just call |
| 872 | * wl_display_dispatch_pending() to handle any events that got |
| 873 | * queued up as a side effect. */ |
| 874 | while (running && ret != -1) { |
| 875 | wl_display_dispatch_pending(display.display); |
Kristian Høgsberg | 1e65840 | 2013-12-07 22:25:56 -0800 | [diff] [blame] | 876 | redraw(&window, NULL, 0); |
| 877 | } |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 878 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 879 | fprintf(stderr, "simple-egl exiting\n"); |
| 880 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 881 | destroy_surface(&window); |
| 882 | fini_egl(&display); |
| 883 | |
Kristian Høgsberg | 191e0ee | 2012-10-29 17:41:46 -0400 | [diff] [blame] | 884 | wl_surface_destroy(display.cursor_surface); |
| 885 | if (display.cursor_theme) |
| 886 | wl_cursor_theme_destroy(display.cursor_theme); |
| 887 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 888 | if (display.shell) |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 889 | xdg_shell_destroy(display.shell); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 890 | |
Nobuhiko Tanibata | 4f01a0b | 2014-11-27 13:24:42 +0900 | [diff] [blame] | 891 | if (display.ivi_application) |
| 892 | ivi_application_destroy(display.ivi_application); |
| 893 | |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 894 | if (display.compositor) |
| 895 | wl_compositor_destroy(display.compositor); |
| 896 | |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 897 | wl_registry_destroy(display.registry); |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 898 | wl_display_flush(display.display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 899 | wl_display_disconnect(display.display); |
Pekka Paalanen | 2c2c106 | 2011-12-13 14:50:25 +0200 | [diff] [blame] | 900 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 901 | return 0; |
| 902 | } |