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