Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2010-2011 Benjamin Franzke |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 3 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 13 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #define _GNU_SOURCE |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 34 | #include <sys/mman.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 35 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 36 | #include <wayland-client.h> |
| 37 | #include <wayland-egl.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 38 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 39 | #include <GLES2/gl2.h> |
| 40 | #include <GLES2/gl2ext.h> |
| 41 | #include <EGL/egl.h> |
| 42 | #include <EGL/eglext.h> |
| 43 | |
| 44 | #include "compositor.h" |
| 45 | |
| 46 | struct wayland_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 47 | struct weston_compositor base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 48 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 49 | struct wl_egl_pixmap *dummy_pixmap; |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 50 | EGLSurface dummy_egl_surface; |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 51 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 52 | struct { |
| 53 | struct wl_display *display; |
| 54 | struct wl_compositor *compositor; |
| 55 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 56 | struct wl_output *output; |
| 57 | |
| 58 | struct { |
| 59 | int32_t x, y, width, height; |
| 60 | } screen_allocation; |
| 61 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 62 | struct wl_event_source *wl_source; |
| 63 | uint32_t event_mask; |
| 64 | } parent; |
| 65 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 66 | struct { |
| 67 | int32_t top, bottom, left, right; |
| 68 | GLuint texture; |
| 69 | int32_t width, height; |
| 70 | } border; |
| 71 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 72 | struct wl_list input_list; |
| 73 | }; |
| 74 | |
| 75 | struct wayland_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 76 | struct weston_output base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 77 | |
| 78 | struct { |
| 79 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 80 | struct wl_shell_surface *shell_surface; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 81 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 82 | } parent; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 83 | EGLSurface egl_surface; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 84 | struct weston_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct wayland_input { |
| 88 | struct wayland_compositor *compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 89 | struct wl_seat *seat; |
| 90 | struct wl_pointer *pointer; |
| 91 | struct wl_keyboard *keyboard; |
| 92 | struct wl_touch *touch; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 93 | struct wl_list link; |
| 94 | }; |
| 95 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 96 | |
| 97 | static int |
| 98 | texture_border(struct wayland_output *output) |
| 99 | { |
| 100 | struct wayland_compositor *c = |
| 101 | (struct wayland_compositor *) output->base.compositor; |
| 102 | GLfloat *d; |
| 103 | unsigned int *p; |
| 104 | int i, j, k, n; |
| 105 | GLfloat x[4], y[4], u[4], v[4]; |
| 106 | |
| 107 | x[0] = -c->border.left; |
| 108 | x[1] = 0; |
| 109 | x[2] = output->base.current->width; |
| 110 | x[3] = output->base.current->width + c->border.right; |
| 111 | |
| 112 | y[0] = -c->border.top; |
| 113 | y[1] = 0; |
| 114 | y[2] = output->base.current->height; |
| 115 | y[3] = output->base.current->height + c->border.bottom; |
| 116 | |
| 117 | u[0] = 0.0; |
| 118 | u[1] = (GLfloat) c->border.left / c->border.width; |
| 119 | u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width; |
| 120 | u[3] = 1.0; |
| 121 | |
| 122 | v[0] = 0.0; |
| 123 | v[1] = (GLfloat) c->border.top / c->border.height; |
| 124 | v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height; |
| 125 | v[3] = 1.0; |
| 126 | |
| 127 | n = 8; |
| 128 | d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d); |
| 129 | p = wl_array_add(&c->base.indices, n * 6 * sizeof *p); |
| 130 | |
| 131 | k = 0; |
| 132 | for (i = 0; i < 3; i++) |
| 133 | for (j = 0; j < 3; j++) { |
| 134 | |
| 135 | if (i == 1 && j == 1) |
| 136 | continue; |
| 137 | |
| 138 | d[ 0] = x[i]; |
| 139 | d[ 1] = y[j]; |
| 140 | d[ 2] = u[i]; |
| 141 | d[ 3] = v[j]; |
| 142 | |
| 143 | d[ 4] = x[i]; |
| 144 | d[ 5] = y[j + 1]; |
| 145 | d[ 6] = u[i]; |
| 146 | d[ 7] = v[j + 1]; |
| 147 | |
| 148 | d[ 8] = x[i + 1]; |
| 149 | d[ 9] = y[j]; |
| 150 | d[10] = u[i + 1]; |
| 151 | d[11] = v[j]; |
| 152 | |
| 153 | d[12] = x[i + 1]; |
| 154 | d[13] = y[j + 1]; |
| 155 | d[14] = u[i + 1]; |
| 156 | d[15] = v[j + 1]; |
| 157 | |
| 158 | p[0] = k + 0; |
| 159 | p[1] = k + 1; |
| 160 | p[2] = k + 2; |
| 161 | p[3] = k + 2; |
| 162 | p[4] = k + 1; |
| 163 | p[5] = k + 3; |
| 164 | |
| 165 | d += 16; |
| 166 | p += 6; |
| 167 | k += 4; |
| 168 | } |
| 169 | |
| 170 | return k / 4; |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | draw_border(struct wayland_output *output) |
| 175 | { |
| 176 | struct wayland_compositor *c = |
| 177 | (struct wayland_compositor *) output->base.compositor; |
| 178 | struct weston_shader *shader = &c->base.texture_shader; |
| 179 | GLfloat *v; |
| 180 | int n; |
| 181 | |
| 182 | glDisable(GL_BLEND); |
| 183 | glUseProgram(shader->program); |
| 184 | c->base.current_shader = shader; |
| 185 | |
| 186 | glUniformMatrix4fv(shader->proj_uniform, |
| 187 | 1, GL_FALSE, output->base.matrix.d); |
| 188 | |
| 189 | glUniform1i(shader->tex_uniform, 0); |
| 190 | glUniform1f(shader->alpha_uniform, 1); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 191 | glUniform1f(shader->texwidth_uniform, 1); |
| 192 | |
| 193 | n = texture_border(output); |
| 194 | |
| 195 | glBindTexture(GL_TEXTURE_2D, c->border.texture); |
| 196 | |
| 197 | v = c->base.vertices.data; |
| 198 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 199 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
| 200 | glEnableVertexAttribArray(0); |
| 201 | glEnableVertexAttribArray(1); |
| 202 | |
| 203 | glDrawElements(GL_TRIANGLES, n * 6, |
| 204 | GL_UNSIGNED_INT, c->base.indices.data); |
| 205 | |
| 206 | glDisableVertexAttribArray(1); |
| 207 | glDisableVertexAttribArray(0); |
| 208 | |
| 209 | c->base.vertices.size = 0; |
| 210 | c->base.indices.size = 0; |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | create_border(struct wayland_compositor *c) |
| 215 | { |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 216 | pixman_image_t *image; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 217 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 218 | image = load_image(DATADIR "/weston/border.png"); |
| 219 | if (!image) { |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 220 | fprintf(stderr, "could'nt load border image\n"); |
| 221 | return; |
| 222 | } |
| 223 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 224 | c->border.width = pixman_image_get_width(image); |
| 225 | c->border.height = pixman_image_get_height(image); |
| 226 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 227 | glGenTextures(1, &c->border.texture); |
| 228 | glBindTexture(GL_TEXTURE_2D, c->border.texture); |
| 229 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 232 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 233 | |
| 234 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 235 | c->border.width, |
| 236 | c->border.height, |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 237 | 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 238 | pixman_image_get_data(image)); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 239 | |
| 240 | c->border.top = 25; |
| 241 | c->border.bottom = 50; |
| 242 | c->border.left = 25; |
| 243 | c->border.right = 25; |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 244 | |
| 245 | pixman_image_unref(image); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 248 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 249 | wayland_compositor_init_egl(struct wayland_compositor *c) |
| 250 | { |
| 251 | EGLint major, minor; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 252 | EGLint n; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 253 | EGLint config_attribs[] = { |
| 254 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 255 | EGL_RED_SIZE, 1, |
| 256 | EGL_GREEN_SIZE, 1, |
| 257 | EGL_BLUE_SIZE, 1, |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 258 | EGL_ALPHA_SIZE, 1, |
Kristian Høgsberg | d28ab36 | 2011-03-02 11:36:30 -0500 | [diff] [blame] | 259 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 260 | EGL_NONE |
| 261 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 262 | static const EGLint context_attribs[] = { |
| 263 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 264 | EGL_NONE |
| 265 | }; |
| 266 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 267 | c->base.display = eglGetDisplay(c->parent.display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 268 | if (c->base.display == NULL) { |
| 269 | fprintf(stderr, "failed to create display\n"); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | if (!eglInitialize(c->base.display, &major, &minor)) { |
| 274 | fprintf(stderr, "failed to initialize display\n"); |
| 275 | return -1; |
| 276 | } |
| 277 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 278 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 279 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 280 | return -1; |
| 281 | } |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 282 | if (!eglChooseConfig(c->base.display, config_attribs, |
| 283 | &c->base.config, 1, &n) || n == 0) { |
| 284 | fprintf(stderr, "failed to choose config: %d\n", n); |
| 285 | return -1; |
| 286 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 287 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 288 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 289 | EGL_NO_CONTEXT, context_attribs); |
| 290 | if (c->base.context == NULL) { |
| 291 | fprintf(stderr, "failed to create context\n"); |
| 292 | return -1; |
| 293 | } |
| 294 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 295 | c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0); |
| 296 | if (!c->dummy_pixmap) { |
| 297 | fprintf(stderr, "failure to create dummy_pixmap\n"); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | c->dummy_egl_surface = |
| 302 | eglCreatePixmapSurface(c->base.display, c->base.config, |
| 303 | c->dummy_pixmap, NULL); |
| 304 | if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface, |
| 305 | c->dummy_egl_surface, c->base.context)) { |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 306 | fprintf(stderr, "failed to make context current\n"); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 313 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 314 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 315 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 316 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 317 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 318 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 319 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static const struct wl_callback_listener frame_listener = { |
| 323 | frame_done |
| 324 | }; |
| 325 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 326 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 327 | wayland_output_repaint(struct weston_output *output_base, |
| 328 | pixman_region32_t *damage) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 329 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 330 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 331 | struct wayland_compositor *compositor = |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 332 | (struct wayland_compositor *) output->base.compositor; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 333 | struct wl_callback *callback; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 334 | struct weston_surface *surface; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 335 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 336 | if (!eglMakeCurrent(compositor->base.display, output->egl_surface, |
| 337 | output->egl_surface, compositor->base.context)) { |
| 338 | fprintf(stderr, "failed to make current\n"); |
| 339 | return; |
| 340 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 341 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 342 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 343 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 344 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 345 | draw_border(output); |
| 346 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 347 | weston_output_do_read_pixels(&output->base); |
| 348 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 349 | eglSwapBuffers(compositor->base.display, output->egl_surface); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 350 | callback = wl_surface_frame(output->parent.surface); |
| 351 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 352 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 353 | return; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 356 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 357 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 358 | { |
| 359 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 360 | struct weston_compositor *ec = output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 361 | |
| 362 | eglDestroySurface(ec->display, output->egl_surface); |
| 363 | wl_egl_window_destroy(output->parent.egl_window); |
| 364 | free(output); |
| 365 | |
| 366 | return; |
| 367 | } |
| 368 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 369 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 370 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 371 | int width, int height) |
| 372 | { |
| 373 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 374 | |
| 375 | output = malloc(sizeof *output); |
| 376 | if (output == NULL) |
| 377 | return -1; |
| 378 | memset(output, 0, sizeof *output); |
| 379 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 380 | output->mode.flags = |
| 381 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 382 | output->mode.width = width; |
| 383 | output->mode.height = height; |
| 384 | output->mode.refresh = 60; |
| 385 | wl_list_init(&output->base.mode_list); |
| 386 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 387 | |
| 388 | output->base.current = &output->mode; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 389 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 390 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 391 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 392 | output->base.border.top = c->border.top; |
| 393 | output->base.border.bottom = c->border.bottom; |
| 394 | output->base.border.left = c->border.left; |
| 395 | output->base.border.right = c->border.right; |
| 396 | |
| 397 | weston_output_move(&output->base, 0, 0); |
| 398 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 399 | output->parent.surface = |
| 400 | wl_compositor_create_surface(c->parent.compositor); |
| 401 | wl_surface_set_user_data(output->parent.surface, output); |
| 402 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 403 | output->parent.egl_window = |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 404 | wl_egl_window_create(output->parent.surface, |
| 405 | width + c->border.left + c->border.right, |
| 406 | height + c->border.top + c->border.bottom); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 407 | if (!output->parent.egl_window) { |
| 408 | fprintf(stderr, "failure to create wl_egl_window\n"); |
| 409 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 412 | output->egl_surface = |
| 413 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 414 | output->parent.egl_window, NULL); |
| 415 | if (!output->egl_surface) { |
| 416 | fprintf(stderr, "failed to create window surface\n"); |
| 417 | goto cleanup_window; |
| 418 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 419 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 420 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 421 | output->egl_surface, c->base.context)) { |
| 422 | fprintf(stderr, "failed to make surface current\n"); |
| 423 | goto cleanup_surface; |
| 424 | return -1; |
| 425 | } |
| 426 | |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 427 | output->parent.shell_surface = |
| 428 | wl_shell_get_shell_surface(c->parent.shell, |
| 429 | output->parent.surface); |
| 430 | /* FIXME: add shell_surface listener for resizing */ |
| 431 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 432 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 433 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 434 | output->base.repaint = wayland_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 435 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 436 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 437 | output->base.set_backlight = NULL; |
| 438 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 439 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 440 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 441 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 442 | |
| 443 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 444 | |
| 445 | cleanup_surface: |
| 446 | eglDestroySurface(c->base.display, output->egl_surface); |
| 447 | cleanup_window: |
| 448 | wl_egl_window_destroy(output->parent.egl_window); |
| 449 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 450 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 451 | free(output); |
| 452 | |
| 453 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 456 | /* Events received from the wayland-server this compositor is client of: */ |
| 457 | |
| 458 | /* parent output interface */ |
| 459 | static void |
| 460 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 461 | struct wl_output *wl_output, |
| 462 | int x, |
| 463 | int y, |
| 464 | int physical_width, |
| 465 | int physical_height, |
| 466 | int subpixel, |
| 467 | const char *make, |
| 468 | const char *model) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 469 | { |
| 470 | struct wayland_compositor *c = data; |
| 471 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 472 | c->parent.screen_allocation.x = x; |
| 473 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void |
| 477 | display_handle_mode(void *data, |
| 478 | struct wl_output *wl_output, |
| 479 | uint32_t flags, |
| 480 | int width, |
| 481 | int height, |
| 482 | int refresh) |
| 483 | { |
| 484 | struct wayland_compositor *c = data; |
| 485 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 486 | c->parent.screen_allocation.width = width; |
| 487 | c->parent.screen_allocation.height = height; |
| 488 | } |
| 489 | |
| 490 | static const struct wl_output_listener output_listener = { |
| 491 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 492 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 493 | }; |
| 494 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 495 | /* parent input interface */ |
| 496 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 497 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 498 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 499 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 500 | { |
| 501 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 502 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 503 | struct wayland_compositor *c = input->compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 504 | |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 505 | output = wl_surface_get_user_data(surface); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 506 | notify_pointer_focus(&c->base.seat->seat, &output->base, x, y); |
| 507 | wl_pointer_attach(input->pointer, serial, NULL, 0, 0); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 511 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 512 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 513 | { |
| 514 | struct wayland_input *input = data; |
| 515 | struct wayland_compositor *c = input->compositor; |
| 516 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 517 | notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 521 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 522 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 523 | { |
| 524 | struct wayland_input *input = data; |
| 525 | struct wayland_compositor *c = input->compositor; |
| 526 | |
| 527 | notify_motion(&c->base.seat->seat, time, |
| 528 | x - wl_fixed_from_int(c->border.left), |
| 529 | y - wl_fixed_from_int(c->border.top)); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 534 | uint32_t serial, uint32_t time, uint32_t button, |
| 535 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 536 | { |
| 537 | struct wayland_input *input = data; |
| 538 | struct wayland_compositor *c = input->compositor; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 539 | enum wl_pointer_button_state state = state_w; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 540 | |
| 541 | notify_button(&c->base.seat->seat, time, button, state); |
| 542 | } |
| 543 | |
| 544 | static void |
| 545 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 546 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 547 | { |
| 548 | struct wayland_input *input = data; |
| 549 | struct wayland_compositor *c = input->compositor; |
| 550 | |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 551 | notify_axis(&c->base.seat->seat, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static const struct wl_pointer_listener pointer_listener = { |
| 555 | input_handle_pointer_enter, |
| 556 | input_handle_pointer_leave, |
| 557 | input_handle_motion, |
| 558 | input_handle_button, |
| 559 | input_handle_axis, |
| 560 | }; |
| 561 | |
| 562 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 563 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 564 | int fd, uint32_t size) |
| 565 | { |
| 566 | struct wayland_input *input = data; |
| 567 | struct xkb_keymap *keymap; |
| 568 | char *map_str; |
| 569 | |
| 570 | if (!data) { |
| 571 | close(fd); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 576 | close(fd); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 581 | if (map_str == MAP_FAILED) { |
| 582 | close(fd); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 587 | map_str, |
| 588 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 589 | 0); |
| 590 | munmap(map_str, size); |
| 591 | close(fd); |
| 592 | |
| 593 | if (!keymap) { |
| 594 | fprintf(stderr, "failed to compile keymap\n"); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | weston_seat_init_keyboard(input->compositor->base.seat, keymap); |
| 599 | xkb_map_unref(keymap); |
| 600 | } |
| 601 | |
| 602 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 603 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 604 | struct wl_keyboard *keyboard, |
| 605 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 606 | struct wl_surface *surface, |
| 607 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 608 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 609 | struct wayland_input *input = data; |
| 610 | struct wayland_compositor *c = input->compositor; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 611 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 612 | notify_keyboard_focus(&c->base.seat->seat, keys); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static void |
| 616 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 617 | struct wl_keyboard *keyboard, |
| 618 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 619 | struct wl_surface *surface) |
| 620 | { |
| 621 | struct wayland_input *input = data; |
| 622 | struct wayland_compositor *c = input->compositor; |
| 623 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 624 | notify_keyboard_focus(&c->base.seat->seat, NULL); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 627 | static void |
| 628 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 629 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 630 | { |
| 631 | struct wayland_input *input = data; |
| 632 | struct wayland_compositor *c = input->compositor; |
| 633 | |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 634 | notify_key(&c->base.seat->seat, time, key, |
| 635 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
| 636 | WL_KEYBOARD_KEY_STATE_RELEASED); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 639 | static void |
| 640 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 641 | uint32_t serial, uint32_t mods_depressed, |
| 642 | uint32_t mods_latched, uint32_t mods_locked, |
| 643 | uint32_t group) |
| 644 | { |
| 645 | /* XXX notify_modifiers(); */ |
| 646 | } |
| 647 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 648 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 649 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 650 | input_handle_keyboard_enter, |
| 651 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 652 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 653 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 654 | }; |
| 655 | |
| 656 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 657 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 658 | enum wl_seat_capability caps) |
| 659 | { |
| 660 | struct wayland_input *input = data; |
| 661 | |
| 662 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 663 | input->pointer = wl_seat_get_pointer(seat); |
| 664 | wl_pointer_set_user_data(input->pointer, input); |
| 665 | wl_pointer_add_listener(input->pointer, &pointer_listener, |
| 666 | input); |
Daniel Stone | cd981dd | 2012-06-01 12:14:00 +0100 | [diff] [blame] | 667 | weston_seat_init_pointer(input->compositor->base.seat); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 668 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 669 | wl_pointer_destroy(input->pointer); |
| 670 | input->pointer = NULL; |
| 671 | } |
| 672 | |
| 673 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 674 | input->keyboard = wl_seat_get_keyboard(seat); |
| 675 | wl_keyboard_set_user_data(input->keyboard, input); |
| 676 | wl_keyboard_add_listener(input->keyboard, &keyboard_listener, |
| 677 | input); |
| 678 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 679 | wl_keyboard_destroy(input->keyboard); |
| 680 | input->keyboard = NULL; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | static const struct wl_seat_listener seat_listener = { |
| 685 | input_handle_capabilities, |
| 686 | }; |
| 687 | |
| 688 | static void |
| 689 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 690 | { |
| 691 | struct wayland_input *input; |
| 692 | |
| 693 | input = malloc(sizeof *input); |
| 694 | if (input == NULL) |
| 695 | return; |
| 696 | |
| 697 | memset(input, 0, sizeof *input); |
| 698 | |
| 699 | input->compositor = c; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 700 | input->seat = wl_display_bind(c->parent.display, id, |
| 701 | &wl_seat_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 702 | wl_list_insert(c->input_list.prev, &input->link); |
| 703 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 704 | wl_seat_add_listener(input->seat, &seat_listener, input); |
| 705 | wl_seat_set_user_data(input->seat, input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Daniel Stone | a497d57 | 2012-06-04 11:40:46 +0100 | [diff] [blame] | 708 | /* We can't start adding input devices until weston_compositor_init, but |
| 709 | * also can't call weston_compositor_init until we've handled some of the |
| 710 | * base display code first. So, we have a separate global handler for |
| 711 | * seats. */ |
| 712 | static void |
| 713 | display_handle_global_input(struct wl_display *display, uint32_t id, |
| 714 | const char *interface, uint32_t version, |
| 715 | void *data) |
| 716 | { |
| 717 | struct wayland_compositor *c = data; |
| 718 | |
| 719 | if (strcmp(interface, "wl_seat") == 0) |
| 720 | display_add_seat(c, id); |
| 721 | } |
| 722 | |
| 723 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 724 | static void |
| 725 | display_handle_global(struct wl_display *display, uint32_t id, |
| 726 | const char *interface, uint32_t version, void *data) |
| 727 | { |
| 728 | struct wayland_compositor *c = data; |
| 729 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 730 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 731 | c->parent.compositor = |
| 732 | wl_display_bind(display, id, &wl_compositor_interface); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 733 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 734 | c->parent.output = |
| 735 | wl_display_bind(display, id, &wl_output_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 736 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 737 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 738 | c->parent.shell = |
| 739 | wl_display_bind(display, id, &wl_shell_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | update_event_mask(uint32_t mask, void *data) |
| 745 | { |
| 746 | struct wayland_compositor *c = data; |
| 747 | |
| 748 | c->parent.event_mask = mask; |
| 749 | if (c->parent.wl_source) |
| 750 | wl_event_source_fd_update(c->parent.wl_source, mask); |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 755 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 756 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 757 | { |
| 758 | struct wayland_compositor *c = data; |
| 759 | |
| 760 | if (mask & WL_EVENT_READABLE) |
| 761 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 762 | if (mask & WL_EVENT_WRITABLE) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 763 | wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 764 | |
| 765 | return 1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Daniel Stone | a497d57 | 2012-06-04 11:40:46 +0100 | [diff] [blame] | 768 | static int |
| 769 | wayland_input_create(struct wayland_compositor *c) |
| 770 | { |
| 771 | struct weston_seat *seat; |
| 772 | |
| 773 | seat = malloc(sizeof *seat); |
| 774 | if (seat == NULL) |
| 775 | return -1; |
| 776 | |
| 777 | memset(seat, 0, sizeof *seat); |
| 778 | weston_seat_init(seat, &c->base); |
| 779 | |
| 780 | c->base.seat = seat; |
| 781 | |
| 782 | wl_display_add_global_listener(c->parent.display, |
| 783 | display_handle_global_input, |
| 784 | c); |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 789 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 790 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 791 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 792 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 793 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 794 | free(ec); |
| 795 | } |
| 796 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 797 | static struct weston_compositor * |
Kristian Høgsberg | 2e28b10 | 2012-02-07 09:57:25 -0500 | [diff] [blame] | 798 | wayland_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 799 | int width, int height, const char *display_name, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 800 | int argc, char *argv[], const char *config_file) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 801 | { |
| 802 | struct wayland_compositor *c; |
| 803 | struct wl_event_loop *loop; |
| 804 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 805 | |
| 806 | c = malloc(sizeof *c); |
| 807 | if (c == NULL) |
| 808 | return NULL; |
| 809 | |
| 810 | memset(c, 0, sizeof *c); |
| 811 | |
Kristian Høgsberg | 2e28b10 | 2012-02-07 09:57:25 -0500 | [diff] [blame] | 812 | c->parent.display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 813 | |
| 814 | if (c->parent.display == NULL) { |
| 815 | fprintf(stderr, "failed to create display: %m\n"); |
| 816 | return NULL; |
| 817 | } |
| 818 | |
| 819 | wl_list_init(&c->input_list); |
| 820 | wl_display_add_global_listener(c->parent.display, |
| 821 | display_handle_global, c); |
| 822 | |
| 823 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 824 | |
| 825 | c->base.wl_display = display; |
| 826 | if (wayland_compositor_init_egl(c) < 0) |
| 827 | return NULL; |
| 828 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 829 | c->base.destroy = wayland_destroy; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 830 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 831 | /* Can't init base class until we have a current egl context */ |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 832 | if (weston_compositor_init(&c->base, display, argc, argv, |
| 833 | config_file) < 0) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 834 | return NULL; |
| 835 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 836 | create_border(c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 837 | if (wayland_compositor_create_output(c, width, height) < 0) |
| 838 | return NULL; |
| 839 | |
| 840 | if (wayland_input_create(c) < 0) |
| 841 | return NULL; |
| 842 | |
| 843 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 844 | |
| 845 | fd = wl_display_get_fd(c->parent.display, update_event_mask, c); |
| 846 | c->parent.wl_source = |
| 847 | wl_event_loop_add_fd(loop, fd, c->parent.event_mask, |
| 848 | wayland_compositor_handle_event, c); |
| 849 | if (c->parent.wl_source == NULL) |
| 850 | return NULL; |
| 851 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 852 | return &c->base; |
| 853 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 854 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 855 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 856 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 857 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 858 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 859 | int width = 1024, height = 640; |
| 860 | char *display_name = NULL; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 861 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 862 | const struct weston_option wayland_options[] = { |
| 863 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 864 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 865 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 866 | }; |
| 867 | |
| 868 | parse_options(wayland_options, |
| 869 | ARRAY_LENGTH(wayland_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 870 | |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 871 | return wayland_compositor_create(display, width, height, display_name, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 872 | argc, argv, config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 873 | } |