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 | |
| 48 | struct { |
| 49 | struct wl_display *display; |
| 50 | struct wl_compositor *compositor; |
| 51 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 52 | struct wl_output *output; |
| 53 | |
| 54 | struct { |
| 55 | int32_t x, y, width, height; |
| 56 | } screen_allocation; |
| 57 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 58 | struct wl_event_source *wl_source; |
| 59 | uint32_t event_mask; |
| 60 | } parent; |
| 61 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 62 | struct wl_list input_list; |
| 63 | }; |
| 64 | |
| 65 | struct wayland_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 66 | struct weston_output base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 67 | |
| 68 | struct { |
| 69 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 70 | struct wl_shell_surface *shell_surface; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 71 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 72 | } parent; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 73 | EGLSurface egl_surface; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 74 | struct weston_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct wayland_input { |
| 78 | struct wayland_compositor *compositor; |
| 79 | struct wl_input_device *input_device; |
| 80 | struct wl_list link; |
| 81 | }; |
| 82 | |
| 83 | static int |
| 84 | wayland_input_create(struct wayland_compositor *c) |
| 85 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 86 | struct weston_input_device *input; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 87 | |
| 88 | input = malloc(sizeof *input); |
| 89 | if (input == NULL) |
| 90 | return -1; |
| 91 | |
| 92 | memset(input, 0, sizeof *input); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 93 | weston_input_device_init(input, &c->base); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 94 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 95 | c->base.input_device = &input->input_device; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | wayland_compositor_init_egl(struct wayland_compositor *c) |
| 102 | { |
| 103 | EGLint major, minor; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 104 | EGLint n; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 105 | const char *extensions; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 106 | EGLint config_attribs[] = { |
| 107 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 108 | EGL_RED_SIZE, 1, |
| 109 | EGL_GREEN_SIZE, 1, |
| 110 | EGL_BLUE_SIZE, 1, |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 111 | EGL_ALPHA_SIZE, 0, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 112 | EGL_DEPTH_SIZE, 1, |
Kristian Høgsberg | d28ab36 | 2011-03-02 11:36:30 -0500 | [diff] [blame] | 113 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 114 | EGL_NONE |
| 115 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 116 | static const EGLint context_attribs[] = { |
| 117 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 118 | EGL_NONE |
| 119 | }; |
| 120 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 121 | c->base.display = eglGetDisplay(c->parent.display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 122 | if (c->base.display == NULL) { |
| 123 | fprintf(stderr, "failed to create display\n"); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | if (!eglInitialize(c->base.display, &major, &minor)) { |
| 128 | fprintf(stderr, "failed to initialize display\n"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | extensions = eglQueryString(c->base.display, EGL_EXTENSIONS); |
Ander Conselvan de Oliveira | ef7c8d9 | 2011-11-01 16:37:41 +0200 | [diff] [blame] | 133 | if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) { |
| 134 | fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 139 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 140 | return -1; |
| 141 | } |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 142 | if (!eglChooseConfig(c->base.display, config_attribs, |
| 143 | &c->base.config, 1, &n) || n == 0) { |
| 144 | fprintf(stderr, "failed to choose config: %d\n", n); |
| 145 | return -1; |
| 146 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 147 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 148 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 149 | EGL_NO_CONTEXT, context_attribs); |
| 150 | if (c->base.context == NULL) { |
| 151 | fprintf(stderr, "failed to create context\n"); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE, |
| 156 | EGL_NO_SURFACE, c->base.context)) { |
| 157 | fprintf(stderr, "failed to make context current\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 164 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 165 | frame_done(void *data, struct wl_callback *wl_callback, uint32_t time) |
| 166 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 167 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 168 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 169 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static const struct wl_callback_listener frame_listener = { |
| 173 | frame_done |
| 174 | }; |
| 175 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 176 | static void |
| 177 | wayland_output_repaint(struct weston_output *output_base) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 178 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 179 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 180 | struct wayland_compositor *compositor = |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 181 | (struct wayland_compositor *) output->base.compositor; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 182 | struct wl_callback *callback; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 183 | struct weston_surface *surface; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 184 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 185 | if (!eglMakeCurrent(compositor->base.display, output->egl_surface, |
| 186 | output->egl_surface, compositor->base.context)) { |
| 187 | fprintf(stderr, "failed to make current\n"); |
| 188 | return; |
| 189 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 190 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 191 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
| 192 | weston_surface_draw(surface, &output->base); |
| 193 | |
| 194 | eglSwapBuffers(compositor->base.display, output->egl_surface); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 195 | callback = wl_surface_frame(output->parent.surface); |
| 196 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 197 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame^] | 198 | return; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 202 | wayland_output_prepare_scanout_surface(struct weston_output *output_base, |
| 203 | struct weston_surface *es) |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 204 | { |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 205 | return -1; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 209 | wayland_output_set_cursor(struct weston_output *output_base, |
| 210 | struct weston_input_device *input) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 211 | { |
| 212 | return -1; |
| 213 | } |
| 214 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 215 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 216 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 217 | { |
| 218 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 219 | struct weston_compositor *ec = output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 220 | |
| 221 | eglDestroySurface(ec->display, output->egl_surface); |
| 222 | wl_egl_window_destroy(output->parent.egl_window); |
| 223 | free(output); |
| 224 | |
| 225 | return; |
| 226 | } |
| 227 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 228 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 229 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 230 | int width, int height) |
| 231 | { |
| 232 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 233 | |
| 234 | output = malloc(sizeof *output); |
| 235 | if (output == NULL) |
| 236 | return -1; |
| 237 | memset(output, 0, sizeof *output); |
| 238 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 239 | output->mode.flags = |
| 240 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 241 | output->mode.width = width; |
| 242 | output->mode.height = height; |
| 243 | output->mode.refresh = 60; |
| 244 | wl_list_init(&output->base.mode_list); |
| 245 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 246 | |
| 247 | output->base.current = &output->mode; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 248 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 249 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 250 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 251 | output->parent.surface = |
| 252 | wl_compositor_create_surface(c->parent.compositor); |
| 253 | wl_surface_set_user_data(output->parent.surface, output); |
| 254 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 255 | output->parent.egl_window = |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 256 | wl_egl_window_create(output->parent.surface, width, height); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 257 | if (!output->parent.egl_window) { |
| 258 | fprintf(stderr, "failure to create wl_egl_window\n"); |
| 259 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 262 | output->egl_surface = |
| 263 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 264 | output->parent.egl_window, NULL); |
| 265 | if (!output->egl_surface) { |
| 266 | fprintf(stderr, "failed to create window surface\n"); |
| 267 | goto cleanup_window; |
| 268 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 269 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 270 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 271 | output->egl_surface, c->base.context)) { |
| 272 | fprintf(stderr, "failed to make surface current\n"); |
| 273 | goto cleanup_surface; |
| 274 | return -1; |
| 275 | } |
| 276 | |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 277 | output->parent.shell_surface = |
| 278 | wl_shell_get_shell_surface(c->parent.shell, |
| 279 | output->parent.surface); |
| 280 | /* FIXME: add shell_surface listener for resizing */ |
| 281 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 282 | |
| 283 | glClearColor(0, 0, 0, 0.5); |
| 284 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 285 | output->base.repaint = wayland_output_repaint; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 286 | output->base.prepare_scanout_surface = |
| 287 | wayland_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 288 | output->base.set_hardware_cursor = wayland_output_set_cursor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 289 | output->base.destroy = wayland_output_destroy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 290 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 291 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 292 | |
| 293 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 294 | |
| 295 | cleanup_surface: |
| 296 | eglDestroySurface(c->base.display, output->egl_surface); |
| 297 | cleanup_window: |
| 298 | wl_egl_window_destroy(output->parent.egl_window); |
| 299 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 300 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 301 | free(output); |
| 302 | |
| 303 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 306 | /* Events received from the wayland-server this compositor is client of: */ |
| 307 | |
| 308 | /* parent output interface */ |
| 309 | static void |
| 310 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 311 | struct wl_output *wl_output, |
| 312 | int x, |
| 313 | int y, |
| 314 | int physical_width, |
| 315 | int physical_height, |
| 316 | int subpixel, |
| 317 | const char *make, |
| 318 | const char *model) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 319 | { |
| 320 | struct wayland_compositor *c = data; |
| 321 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 322 | c->parent.screen_allocation.x = x; |
| 323 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void |
| 327 | display_handle_mode(void *data, |
| 328 | struct wl_output *wl_output, |
| 329 | uint32_t flags, |
| 330 | int width, |
| 331 | int height, |
| 332 | int refresh) |
| 333 | { |
| 334 | struct wayland_compositor *c = data; |
| 335 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 336 | c->parent.screen_allocation.width = width; |
| 337 | c->parent.screen_allocation.height = height; |
| 338 | } |
| 339 | |
| 340 | static const struct wl_output_listener output_listener = { |
| 341 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 342 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 343 | }; |
| 344 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 345 | /* parent input interface */ |
| 346 | static void |
| 347 | input_handle_motion(void *data, struct wl_input_device *input_device, |
| 348 | uint32_t time, |
| 349 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 350 | { |
| 351 | struct wayland_input *input = data; |
| 352 | struct wayland_compositor *c = input->compositor; |
| 353 | |
| 354 | notify_motion(c->base.input_device, time, sx, sy); |
| 355 | } |
| 356 | |
| 357 | static void |
| 358 | input_handle_button(void *data, |
| 359 | struct wl_input_device *input_device, |
| 360 | uint32_t time, uint32_t button, uint32_t state) |
| 361 | { |
| 362 | struct wayland_input *input = data; |
| 363 | struct wayland_compositor *c = input->compositor; |
| 364 | |
| 365 | notify_button(c->base.input_device, time, button, state); |
| 366 | } |
| 367 | |
| 368 | static void |
| 369 | input_handle_key(void *data, struct wl_input_device *input_device, |
| 370 | uint32_t time, uint32_t key, uint32_t state) |
| 371 | { |
| 372 | struct wayland_input *input = data; |
| 373 | struct wayland_compositor *c = input->compositor; |
| 374 | |
| 375 | notify_key(c->base.input_device, time, key, state); |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | input_handle_pointer_focus(void *data, |
| 380 | struct wl_input_device *input_device, |
| 381 | uint32_t time, struct wl_surface *surface, |
| 382 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 383 | { |
| 384 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 385 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 386 | struct wayland_compositor *c = input->compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 387 | |
| 388 | if (surface) { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 389 | output = wl_surface_get_user_data(surface); |
| 390 | notify_pointer_focus(c->base.input_device, |
| 391 | time, &output->base, sx, sy); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 392 | } else { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 393 | notify_pointer_focus(c->base.input_device, time, NULL, 0, 0); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | input_handle_keyboard_focus(void *data, |
| 399 | struct wl_input_device *input_device, |
| 400 | uint32_t time, |
| 401 | struct wl_surface *surface, |
| 402 | struct wl_array *keys) |
| 403 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 404 | struct wayland_input *input = data; |
| 405 | struct wayland_compositor *c = input->compositor; |
| 406 | struct wayland_output *output; |
| 407 | |
| 408 | if (surface) { |
| 409 | output = wl_surface_get_user_data(surface); |
| 410 | notify_keyboard_focus(c->base.input_device, |
| 411 | time, &output->base, keys); |
| 412 | } else { |
| 413 | notify_keyboard_focus(c->base.input_device, time, NULL, NULL); |
| 414 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static const struct wl_input_device_listener input_device_listener = { |
| 418 | input_handle_motion, |
| 419 | input_handle_button, |
| 420 | input_handle_key, |
| 421 | input_handle_pointer_focus, |
| 422 | input_handle_keyboard_focus, |
| 423 | }; |
| 424 | |
| 425 | static void |
| 426 | display_add_input(struct wayland_compositor *c, uint32_t id) |
| 427 | { |
| 428 | struct wayland_input *input; |
| 429 | |
| 430 | input = malloc(sizeof *input); |
| 431 | if (input == NULL) |
| 432 | return; |
| 433 | |
| 434 | memset(input, 0, sizeof *input); |
| 435 | |
| 436 | input->compositor = c; |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 437 | input->input_device = wl_display_bind(c->parent.display, |
| 438 | id, &wl_input_device_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 439 | wl_list_insert(c->input_list.prev, &input->link); |
| 440 | |
| 441 | wl_input_device_add_listener(input->input_device, |
| 442 | &input_device_listener, input); |
| 443 | wl_input_device_set_user_data(input->input_device, input); |
| 444 | } |
| 445 | |
| 446 | static void |
| 447 | display_handle_global(struct wl_display *display, uint32_t id, |
| 448 | const char *interface, uint32_t version, void *data) |
| 449 | { |
| 450 | struct wayland_compositor *c = data; |
| 451 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 452 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 453 | c->parent.compositor = |
| 454 | wl_display_bind(display, id, &wl_compositor_interface); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 455 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 456 | c->parent.output = |
| 457 | wl_display_bind(display, id, &wl_output_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 458 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 459 | } else if (strcmp(interface, "wl_input_device") == 0) { |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 460 | display_add_input(c, id); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 461 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 462 | c->parent.shell = |
| 463 | wl_display_bind(display, id, &wl_shell_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | static int |
| 468 | update_event_mask(uint32_t mask, void *data) |
| 469 | { |
| 470 | struct wayland_compositor *c = data; |
| 471 | |
| 472 | c->parent.event_mask = mask; |
| 473 | if (c->parent.wl_source) |
| 474 | wl_event_source_fd_update(c->parent.wl_source, mask); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 479 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 480 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 481 | { |
| 482 | struct wayland_compositor *c = data; |
| 483 | |
| 484 | if (mask & WL_EVENT_READABLE) |
| 485 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 486 | if (mask & WL_EVENT_WRITABLE) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 487 | wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 488 | |
| 489 | return 1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 492 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 493 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 494 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 495 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 496 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 497 | free(ec); |
| 498 | } |
| 499 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 500 | static struct weston_compositor * |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 501 | wayland_compositor_create(struct wl_display *display, int width, int height) |
| 502 | { |
| 503 | struct wayland_compositor *c; |
| 504 | struct wl_event_loop *loop; |
| 505 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 506 | |
| 507 | c = malloc(sizeof *c); |
| 508 | if (c == NULL) |
| 509 | return NULL; |
| 510 | |
| 511 | memset(c, 0, sizeof *c); |
| 512 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 513 | c->parent.display = wl_display_connect(NULL); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 514 | |
| 515 | if (c->parent.display == NULL) { |
| 516 | fprintf(stderr, "failed to create display: %m\n"); |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | wl_list_init(&c->input_list); |
| 521 | wl_display_add_global_listener(c->parent.display, |
| 522 | display_handle_global, c); |
| 523 | |
| 524 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 525 | |
| 526 | c->base.wl_display = display; |
| 527 | if (wayland_compositor_init_egl(c) < 0) |
| 528 | return NULL; |
| 529 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 530 | c->base.destroy = wayland_destroy; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 531 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 532 | /* 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] | 533 | if (weston_compositor_init(&c->base, display) < 0) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 534 | return NULL; |
| 535 | |
| 536 | if (wayland_compositor_create_output(c, width, height) < 0) |
| 537 | return NULL; |
| 538 | |
| 539 | if (wayland_input_create(c) < 0) |
| 540 | return NULL; |
| 541 | |
| 542 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 543 | |
| 544 | fd = wl_display_get_fd(c->parent.display, update_event_mask, c); |
| 545 | c->parent.wl_source = |
| 546 | wl_event_loop_add_fd(loop, fd, c->parent.event_mask, |
| 547 | wayland_compositor_handle_event, c); |
| 548 | if (c->parent.wl_source == NULL) |
| 549 | return NULL; |
| 550 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 551 | return &c->base; |
| 552 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 553 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 554 | struct weston_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 555 | backend_init(struct wl_display *display, char *options); |
| 556 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 557 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 558 | backend_init(struct wl_display *display, char *options) |
| 559 | { |
| 560 | int width = 1024, height = 640, i; |
| 561 | char *p, *value; |
| 562 | |
| 563 | static char * const tokens[] = { "width", "height", NULL }; |
| 564 | |
| 565 | p = options; |
| 566 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 567 | switch (i) { |
| 568 | case 0: |
| 569 | width = strtol(value, NULL, 0); |
| 570 | break; |
| 571 | case 1: |
| 572 | height = strtol(value, NULL, 0); |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return wayland_compositor_create(display, width, height); |
| 578 | } |