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 | |
| 49 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 50 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 51 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 52 | struct wl_compositor *compositor; |
| 53 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 54 | struct wl_output *output; |
| 55 | |
| 56 | struct { |
| 57 | int32_t x, y, width, height; |
| 58 | } screen_allocation; |
| 59 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 60 | struct wl_event_source *wl_source; |
| 61 | uint32_t event_mask; |
| 62 | } parent; |
| 63 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 64 | struct { |
| 65 | int32_t top, bottom, left, right; |
| 66 | GLuint texture; |
| 67 | int32_t width, height; |
| 68 | } border; |
| 69 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 70 | struct wl_list input_list; |
| 71 | }; |
| 72 | |
| 73 | struct wayland_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 74 | struct weston_output base; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 75 | struct wl_listener frame_listener; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 76 | struct { |
| 77 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 78 | struct wl_shell_surface *shell_surface; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 79 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 80 | } parent; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 81 | struct weston_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 85 | struct weston_seat base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 86 | struct wayland_compositor *compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 87 | struct wl_seat *seat; |
| 88 | struct wl_pointer *pointer; |
| 89 | struct wl_keyboard *keyboard; |
| 90 | struct wl_touch *touch; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 91 | struct wl_list link; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 92 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 93 | uint32_t enter_serial; |
| 94 | int focus; |
| 95 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 98 | |
| 99 | static int |
| 100 | texture_border(struct wayland_output *output) |
| 101 | { |
| 102 | struct wayland_compositor *c = |
| 103 | (struct wayland_compositor *) output->base.compositor; |
| 104 | GLfloat *d; |
| 105 | unsigned int *p; |
| 106 | int i, j, k, n; |
| 107 | GLfloat x[4], y[4], u[4], v[4]; |
| 108 | |
| 109 | x[0] = -c->border.left; |
| 110 | x[1] = 0; |
| 111 | x[2] = output->base.current->width; |
| 112 | x[3] = output->base.current->width + c->border.right; |
| 113 | |
| 114 | y[0] = -c->border.top; |
| 115 | y[1] = 0; |
| 116 | y[2] = output->base.current->height; |
| 117 | y[3] = output->base.current->height + c->border.bottom; |
| 118 | |
| 119 | u[0] = 0.0; |
| 120 | u[1] = (GLfloat) c->border.left / c->border.width; |
| 121 | u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width; |
| 122 | u[3] = 1.0; |
| 123 | |
| 124 | v[0] = 0.0; |
| 125 | v[1] = (GLfloat) c->border.top / c->border.height; |
| 126 | v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height; |
| 127 | v[3] = 1.0; |
| 128 | |
| 129 | n = 8; |
| 130 | d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d); |
| 131 | p = wl_array_add(&c->base.indices, n * 6 * sizeof *p); |
| 132 | |
| 133 | k = 0; |
| 134 | for (i = 0; i < 3; i++) |
| 135 | for (j = 0; j < 3; j++) { |
| 136 | |
| 137 | if (i == 1 && j == 1) |
| 138 | continue; |
| 139 | |
| 140 | d[ 0] = x[i]; |
| 141 | d[ 1] = y[j]; |
| 142 | d[ 2] = u[i]; |
| 143 | d[ 3] = v[j]; |
| 144 | |
| 145 | d[ 4] = x[i]; |
| 146 | d[ 5] = y[j + 1]; |
| 147 | d[ 6] = u[i]; |
| 148 | d[ 7] = v[j + 1]; |
| 149 | |
| 150 | d[ 8] = x[i + 1]; |
| 151 | d[ 9] = y[j]; |
| 152 | d[10] = u[i + 1]; |
| 153 | d[11] = v[j]; |
| 154 | |
| 155 | d[12] = x[i + 1]; |
| 156 | d[13] = y[j + 1]; |
| 157 | d[14] = u[i + 1]; |
| 158 | d[15] = v[j + 1]; |
| 159 | |
| 160 | p[0] = k + 0; |
| 161 | p[1] = k + 1; |
| 162 | p[2] = k + 2; |
| 163 | p[3] = k + 2; |
| 164 | p[4] = k + 1; |
| 165 | p[5] = k + 3; |
| 166 | |
| 167 | d += 16; |
| 168 | p += 6; |
| 169 | k += 4; |
| 170 | } |
| 171 | |
| 172 | return k / 4; |
| 173 | } |
| 174 | |
| 175 | static void |
| 176 | draw_border(struct wayland_output *output) |
| 177 | { |
| 178 | struct wayland_compositor *c = |
| 179 | (struct wayland_compositor *) output->base.compositor; |
Gwenole Beauchesne | 6d03049 | 2012-04-20 11:15:51 +0200 | [diff] [blame] | 180 | struct weston_shader *shader = &c->base.texture_shader_rgba; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 181 | GLfloat *v; |
| 182 | int n; |
| 183 | |
| 184 | glDisable(GL_BLEND); |
| 185 | glUseProgram(shader->program); |
| 186 | c->base.current_shader = shader; |
| 187 | |
| 188 | glUniformMatrix4fv(shader->proj_uniform, |
| 189 | 1, GL_FALSE, output->base.matrix.d); |
| 190 | |
Gwenole Beauchesne | 28f59b0 | 2012-04-20 11:44:06 +0200 | [diff] [blame] | 191 | glUniform1i(shader->tex_uniforms[0], 0); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 192 | glUniform1f(shader->alpha_uniform, 1); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 193 | |
| 194 | n = texture_border(output); |
| 195 | |
| 196 | glBindTexture(GL_TEXTURE_2D, c->border.texture); |
| 197 | |
| 198 | v = c->base.vertices.data; |
| 199 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 200 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
| 201 | glEnableVertexAttribArray(0); |
| 202 | glEnableVertexAttribArray(1); |
| 203 | |
| 204 | glDrawElements(GL_TRIANGLES, n * 6, |
| 205 | GL_UNSIGNED_INT, c->base.indices.data); |
| 206 | |
| 207 | glDisableVertexAttribArray(1); |
| 208 | glDisableVertexAttribArray(0); |
| 209 | |
| 210 | c->base.vertices.size = 0; |
| 211 | c->base.indices.size = 0; |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | create_border(struct wayland_compositor *c) |
| 216 | { |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 217 | pixman_image_t *image; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 218 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 219 | image = load_image(DATADIR "/weston/border.png"); |
| 220 | if (!image) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 221 | weston_log("could'nt load border image\n"); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 225 | c->border.width = pixman_image_get_width(image); |
| 226 | c->border.height = pixman_image_get_height(image); |
| 227 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 228 | glGenTextures(1, &c->border.texture); |
| 229 | glBindTexture(GL_TEXTURE_2D, c->border.texture); |
| 230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 232 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 233 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 234 | |
| 235 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 236 | c->border.width, |
| 237 | c->border.height, |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 238 | 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 239 | pixman_image_get_data(image)); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 240 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 241 | pixman_image_unref(image); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 244 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 245 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 246 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 247 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 248 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 249 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 250 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static const struct wl_callback_listener frame_listener = { |
| 254 | frame_done |
| 255 | }; |
| 256 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 257 | static void |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 258 | wayland_output_frame_notify(struct wl_listener *listener, void *data) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 259 | { |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 260 | struct wayland_output *output = |
| 261 | container_of(listener, |
| 262 | struct wayland_output, frame_listener); |
Pekka Paalanen | 07c91f8 | 2012-08-30 16:47:21 -0500 | [diff] [blame] | 263 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 264 | draw_border(output); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 265 | } |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 266 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 267 | static void |
| 268 | wayland_output_repaint(struct weston_output *output_base, |
| 269 | pixman_region32_t *damage) |
| 270 | { |
| 271 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 272 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 273 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 274 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 275 | callback = wl_surface_frame(output->parent.surface); |
| 276 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 277 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 278 | ec->renderer->repaint_output(&output->base, damage); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 281 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 282 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 283 | { |
| 284 | struct wayland_output *output = (struct wayland_output *) output_base; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 285 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 286 | gles2_renderer_output_destroy(output_base); |
| 287 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 288 | wl_egl_window_destroy(output->parent.egl_window); |
| 289 | free(output); |
| 290 | |
| 291 | return; |
| 292 | } |
| 293 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 294 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 295 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 296 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 297 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 298 | int width, int height) |
| 299 | { |
| 300 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 301 | |
| 302 | output = malloc(sizeof *output); |
| 303 | if (output == NULL) |
| 304 | return -1; |
| 305 | memset(output, 0, sizeof *output); |
| 306 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 307 | output->mode.flags = |
| 308 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 309 | output->mode.width = width; |
| 310 | output->mode.height = height; |
| 311 | output->mode.refresh = 60; |
| 312 | wl_list_init(&output->base.mode_list); |
| 313 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 314 | |
| 315 | output->base.current = &output->mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 316 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
| 317 | WL_OUTPUT_TRANSFORM_NORMAL); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 318 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 319 | output->base.border.top = c->border.top; |
| 320 | output->base.border.bottom = c->border.bottom; |
| 321 | output->base.border.left = c->border.left; |
| 322 | output->base.border.right = c->border.right; |
Kristian Høgsberg | 90bc88c | 2012-08-13 23:34:04 -0400 | [diff] [blame] | 323 | output->base.make = "waywayland"; |
| 324 | output->base.model = "none"; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 325 | |
| 326 | weston_output_move(&output->base, 0, 0); |
| 327 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 328 | output->parent.surface = |
| 329 | wl_compositor_create_surface(c->parent.compositor); |
| 330 | wl_surface_set_user_data(output->parent.surface, output); |
| 331 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 332 | output->parent.egl_window = |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 333 | wl_egl_window_create(output->parent.surface, |
| 334 | width + c->border.left + c->border.right, |
| 335 | height + c->border.top + c->border.bottom); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 336 | if (!output->parent.egl_window) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 337 | weston_log("failure to create wl_egl_window\n"); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 338 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 339 | } |
| 340 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 341 | if (gles2_renderer_output_create(&output->base, |
| 342 | output->parent.egl_window) < 0) |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 343 | goto cleanup_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 344 | |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 345 | output->parent.shell_surface = |
| 346 | wl_shell_get_shell_surface(c->parent.shell, |
| 347 | output->parent.surface); |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 348 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 349 | &shell_surface_listener, output); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 350 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 351 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 352 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 353 | output->base.repaint = wayland_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 354 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 355 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 356 | output->base.set_backlight = NULL; |
| 357 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 358 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 359 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 360 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 361 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 362 | output->frame_listener.notify = wayland_output_frame_notify; |
| 363 | wl_signal_add(&output->base.frame_signal, &output->frame_listener); |
| 364 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 365 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 366 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 367 | cleanup_window: |
| 368 | wl_egl_window_destroy(output->parent.egl_window); |
| 369 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 370 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 371 | free(output); |
| 372 | |
| 373 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 376 | static void |
| 377 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 378 | uint32_t serial) |
| 379 | { |
| 380 | wl_shell_surface_pong(shell_surface, serial); |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 385 | uint32_t edges, int32_t width, int32_t height) |
| 386 | { |
| 387 | /* FIXME: implement resizing */ |
| 388 | } |
| 389 | |
| 390 | static void |
| 391 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 392 | { |
| 393 | } |
| 394 | |
| 395 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 396 | shell_surface_ping, |
| 397 | shell_surface_configure, |
| 398 | shell_surface_popup_done |
| 399 | }; |
| 400 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 401 | /* Events received from the wayland-server this compositor is client of: */ |
| 402 | |
| 403 | /* parent output interface */ |
| 404 | static void |
| 405 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 406 | struct wl_output *wl_output, |
| 407 | int x, |
| 408 | int y, |
| 409 | int physical_width, |
| 410 | int physical_height, |
| 411 | int subpixel, |
| 412 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 413 | const char *model, |
| 414 | int transform) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 415 | { |
| 416 | struct wayland_compositor *c = data; |
| 417 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 418 | c->parent.screen_allocation.x = x; |
| 419 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void |
| 423 | display_handle_mode(void *data, |
| 424 | struct wl_output *wl_output, |
| 425 | uint32_t flags, |
| 426 | int width, |
| 427 | int height, |
| 428 | int refresh) |
| 429 | { |
| 430 | struct wayland_compositor *c = data; |
| 431 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 432 | c->parent.screen_allocation.width = width; |
| 433 | c->parent.screen_allocation.height = height; |
| 434 | } |
| 435 | |
| 436 | static const struct wl_output_listener output_listener = { |
| 437 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 438 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 439 | }; |
| 440 | |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 441 | static void |
| 442 | check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y) |
| 443 | { |
| 444 | struct wayland_compositor *c = input->compositor; |
| 445 | int width, height, inside; |
| 446 | |
| 447 | width = input->output->mode.width; |
| 448 | height = input->output->mode.height; |
| 449 | |
| 450 | inside = c->border.left <= wl_fixed_to_int(x) && |
| 451 | wl_fixed_to_int(x) < width + c->border.left && |
| 452 | c->border.top <= wl_fixed_to_int(y) && |
| 453 | wl_fixed_to_int(y) < height + c->border.top; |
| 454 | |
| 455 | if (!input->focus && inside) { |
| 456 | notify_pointer_focus(&input->base, &input->output->base, |
| 457 | x - wl_fixed_from_int(c->border.left), |
| 458 | y = wl_fixed_from_int(c->border.top)); |
| 459 | wl_pointer_set_cursor(input->pointer, |
| 460 | input->enter_serial, NULL, 0, 0); |
| 461 | } else if (input->focus && !inside) { |
| 462 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 463 | /* FIXME: Should set default cursor here. */ |
| 464 | } |
| 465 | |
| 466 | input->focus = inside; |
| 467 | } |
| 468 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 469 | /* parent input interface */ |
| 470 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 471 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 472 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 473 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 474 | { |
| 475 | struct wayland_input *input = data; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 476 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 477 | /* XXX: If we get a modifier event immediately before the focus, |
| 478 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 479 | input->enter_serial = serial; |
| 480 | input->output = wl_surface_get_user_data(surface); |
| 481 | check_focus(input, x, y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 485 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 486 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 487 | { |
| 488 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 489 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 490 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 491 | input->output = NULL; |
| 492 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 496 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 497 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 498 | { |
| 499 | struct wayland_input *input = data; |
| 500 | struct wayland_compositor *c = input->compositor; |
| 501 | |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 502 | check_focus(input, x, y); |
| 503 | if (input->focus) |
| 504 | notify_motion(&input->base, time, |
| 505 | x - wl_fixed_from_int(c->border.left), |
| 506 | y - wl_fixed_from_int(c->border.top)); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static void |
| 510 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 511 | uint32_t serial, uint32_t time, uint32_t button, |
| 512 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 513 | { |
| 514 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 515 | enum wl_pointer_button_state state = state_w; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 516 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 517 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void |
| 521 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 522 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 523 | { |
| 524 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 525 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 526 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static const struct wl_pointer_listener pointer_listener = { |
| 530 | input_handle_pointer_enter, |
| 531 | input_handle_pointer_leave, |
| 532 | input_handle_motion, |
| 533 | input_handle_button, |
| 534 | input_handle_axis, |
| 535 | }; |
| 536 | |
| 537 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 538 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 539 | int fd, uint32_t size) |
| 540 | { |
| 541 | struct wayland_input *input = data; |
| 542 | struct xkb_keymap *keymap; |
| 543 | char *map_str; |
| 544 | |
| 545 | if (!data) { |
| 546 | close(fd); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 551 | close(fd); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 556 | if (map_str == MAP_FAILED) { |
| 557 | close(fd); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 562 | map_str, |
| 563 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 564 | 0); |
| 565 | munmap(map_str, size); |
| 566 | close(fd); |
| 567 | |
| 568 | if (!keymap) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 569 | weston_log("failed to compile keymap\n"); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 573 | weston_seat_init_keyboard(&input->base, keymap); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 574 | xkb_map_unref(keymap); |
| 575 | } |
| 576 | |
| 577 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 578 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 579 | struct wl_keyboard *keyboard, |
| 580 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 581 | struct wl_surface *surface, |
| 582 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 583 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 584 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 585 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 586 | /* XXX: If we get a modifier event immediately before the focus, |
| 587 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 588 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 589 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static void |
| 593 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 594 | struct wl_keyboard *keyboard, |
| 595 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 596 | struct wl_surface *surface) |
| 597 | { |
| 598 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 599 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 600 | notify_keyboard_focus_out(&input->base); |
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; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 608 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 609 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 610 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 611 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 612 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 613 | STATE_UPDATE_NONE); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 616 | static void |
| 617 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 618 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 619 | uint32_t mods_latched, uint32_t mods_locked, |
| 620 | uint32_t group) |
| 621 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 622 | struct wayland_input *input = data; |
| 623 | struct wayland_compositor *c = input->compositor; |
| 624 | uint32_t serial_out; |
| 625 | |
| 626 | /* If we get a key event followed by a modifier event with the |
| 627 | * same serial number, then we try to preserve those semantics by |
| 628 | * reusing the same serial number on the way out too. */ |
| 629 | if (serial_in == input->key_serial) |
| 630 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 631 | else |
| 632 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 633 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 634 | xkb_state_update_mask(input->base.xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 635 | mods_depressed, mods_latched, |
| 636 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 637 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 640 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 641 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 642 | input_handle_keyboard_enter, |
| 643 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 644 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 645 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 649 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 650 | enum wl_seat_capability caps) |
| 651 | { |
| 652 | struct wayland_input *input = data; |
| 653 | |
| 654 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 655 | input->pointer = wl_seat_get_pointer(seat); |
| 656 | wl_pointer_set_user_data(input->pointer, input); |
| 657 | wl_pointer_add_listener(input->pointer, &pointer_listener, |
| 658 | input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 659 | weston_seat_init_pointer(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 660 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 661 | wl_pointer_destroy(input->pointer); |
| 662 | input->pointer = NULL; |
| 663 | } |
| 664 | |
| 665 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 666 | input->keyboard = wl_seat_get_keyboard(seat); |
| 667 | wl_keyboard_set_user_data(input->keyboard, input); |
| 668 | wl_keyboard_add_listener(input->keyboard, &keyboard_listener, |
| 669 | input); |
| 670 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 671 | wl_keyboard_destroy(input->keyboard); |
| 672 | input->keyboard = NULL; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static const struct wl_seat_listener seat_listener = { |
| 677 | input_handle_capabilities, |
| 678 | }; |
| 679 | |
| 680 | static void |
| 681 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 682 | { |
| 683 | struct wayland_input *input; |
| 684 | |
| 685 | input = malloc(sizeof *input); |
| 686 | if (input == NULL) |
| 687 | return; |
| 688 | |
| 689 | memset(input, 0, sizeof *input); |
| 690 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 691 | weston_seat_init(&input->base, &c->base); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 692 | input->compositor = c; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 693 | input->seat = wl_registry_bind(c->parent.registry, id, |
| 694 | &wl_seat_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 695 | wl_list_insert(c->input_list.prev, &input->link); |
| 696 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 697 | wl_seat_add_listener(input->seat, &seat_listener, input); |
| 698 | wl_seat_set_user_data(input->seat, input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 702 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 703 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 704 | { |
| 705 | struct wayland_compositor *c = data; |
| 706 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 707 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 708 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 709 | wl_registry_bind(registry, name, |
| 710 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 711 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 712 | c->parent.output = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 713 | wl_registry_bind(registry, name, |
| 714 | &wl_output_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 715 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 716 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 717 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 718 | wl_registry_bind(registry, name, |
| 719 | &wl_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 720 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 721 | display_add_seat(c, name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 725 | static const struct wl_registry_listener registry_listener = { |
| 726 | registry_handle_global |
| 727 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 728 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 729 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 730 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 731 | { |
| 732 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 733 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 734 | |
| 735 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 736 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 737 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 738 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 739 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 740 | if (mask == 0) { |
| 741 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 742 | wl_display_flush(c->parent.wl_display); |
| 743 | } |
| 744 | |
| 745 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 748 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 749 | wayland_restore(struct weston_compositor *ec) |
| 750 | { |
| 751 | } |
| 752 | |
| 753 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 754 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 755 | { |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 756 | gles2_renderer_destroy(ec); |
| 757 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 758 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 759 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 760 | free(ec); |
| 761 | } |
| 762 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 763 | static struct weston_compositor * |
Kristian Høgsberg | 2e28b10 | 2012-02-07 09:57:25 -0500 | [diff] [blame] | 764 | wayland_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 765 | int width, int height, const char *display_name, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 766 | int argc, char *argv[], const char *config_file) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 767 | { |
| 768 | struct wayland_compositor *c; |
| 769 | struct wl_event_loop *loop; |
| 770 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 771 | |
| 772 | c = malloc(sizeof *c); |
| 773 | if (c == NULL) |
| 774 | return NULL; |
| 775 | |
| 776 | memset(c, 0, sizeof *c); |
| 777 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 778 | if (weston_compositor_init(&c->base, display, argc, argv, |
| 779 | config_file) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 780 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 781 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 782 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 783 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 784 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 785 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 786 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | wl_list_init(&c->input_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 790 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 791 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
| 792 | wl_display_dispatch(c->parent.wl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 793 | |
| 794 | c->base.wl_display = display; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame^] | 795 | if (gles2_renderer_create(&c->base, c->parent.wl_display, |
| 796 | gles2_renderer_alpha_attribs, |
| 797 | NULL) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 798 | goto err_display; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 799 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 800 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 801 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 802 | |
John Kåre Alsaker | c37b32d | 2012-10-04 20:42:17 +0200 | [diff] [blame] | 803 | c->border.top = 30; |
| 804 | c->border.bottom = 24; |
John Kåre Alsaker | b9b8712 | 2012-10-04 20:42:16 +0200 | [diff] [blame] | 805 | c->border.left = 25; |
John Kåre Alsaker | c37b32d | 2012-10-04 20:42:17 +0200 | [diff] [blame] | 806 | c->border.right = 26; |
John Kåre Alsaker | b9b8712 | 2012-10-04 20:42:16 +0200 | [diff] [blame] | 807 | |
| 808 | /* requires border fields */ |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 809 | if (wayland_compositor_create_output(c, width, height) < 0) |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame^] | 810 | goto err_gles2; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 811 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame^] | 812 | /* requires gles2_renderer_output_state_create called |
| 813 | * by wayland_compositor_create_output */ |
John Kåre Alsaker | 4b3081a | 2012-10-03 18:02:22 +0200 | [diff] [blame] | 814 | create_border(c); |
| 815 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 816 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 817 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 818 | fd = wl_display_get_fd(c->parent.wl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 819 | c->parent.wl_source = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 820 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 821 | wayland_compositor_handle_event, c); |
| 822 | if (c->parent.wl_source == NULL) |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame^] | 823 | goto err_gles2; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 824 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 825 | wl_event_source_check(c->parent.wl_source); |
| 826 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 827 | return &c->base; |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 828 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame^] | 829 | err_gles2: |
| 830 | gles2_renderer_destroy(&c->base); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 831 | err_display: |
| 832 | wl_display_disconnect(c->parent.wl_display); |
| 833 | err_compositor: |
| 834 | weston_compositor_shutdown(&c->base); |
| 835 | err_free: |
| 836 | free(c); |
| 837 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 838 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 839 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 840 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 841 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 842 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 843 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 844 | int width = 1024, height = 640; |
| 845 | char *display_name = NULL; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 846 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 847 | const struct weston_option wayland_options[] = { |
| 848 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 849 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 850 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 851 | }; |
| 852 | |
| 853 | parse_options(wayland_options, |
| 854 | ARRAY_LENGTH(wayland_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 855 | |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 856 | return wayland_compositor_create(display, width, height, display_name, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 857 | argc, argv, config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 858 | } |