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