Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Benjamin Franzke |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #define _GNU_SOURCE |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 30 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 31 | #include <wayland-client.h> |
| 32 | #include <wayland-egl.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 33 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 34 | #include <GLES2/gl2.h> |
| 35 | #include <GLES2/gl2ext.h> |
| 36 | #include <EGL/egl.h> |
| 37 | #include <EGL/eglext.h> |
| 38 | |
| 39 | #include "compositor.h" |
| 40 | |
| 41 | struct wayland_compositor { |
| 42 | struct wlsc_compositor base; |
| 43 | |
| 44 | struct { |
| 45 | struct wl_display *display; |
| 46 | struct wl_compositor *compositor; |
| 47 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 48 | struct wl_output *output; |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 49 | struct wl_visual *visual; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 50 | |
| 51 | struct { |
| 52 | int32_t x, y, width, height; |
| 53 | } screen_allocation; |
| 54 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 55 | struct wl_event_source *wl_source; |
| 56 | uint32_t event_mask; |
| 57 | } parent; |
| 58 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 59 | struct wl_list input_list; |
| 60 | }; |
| 61 | |
| 62 | struct wayland_output { |
| 63 | struct wlsc_output base; |
| 64 | |
| 65 | struct { |
| 66 | struct wl_surface *surface; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 67 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 68 | } parent; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 69 | EGLSurface egl_surface; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 70 | struct wlsc_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct wayland_input { |
| 74 | struct wayland_compositor *compositor; |
| 75 | struct wl_input_device *input_device; |
| 76 | struct wl_list link; |
| 77 | }; |
| 78 | |
| 79 | static int |
| 80 | wayland_input_create(struct wayland_compositor *c) |
| 81 | { |
| 82 | struct wlsc_input_device *input; |
| 83 | |
| 84 | input = malloc(sizeof *input); |
| 85 | if (input == NULL) |
| 86 | return -1; |
| 87 | |
| 88 | memset(input, 0, sizeof *input); |
| 89 | wlsc_input_device_init(input, &c->base); |
| 90 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 91 | c->base.input_device = &input->input_device; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | wayland_compositor_init_egl(struct wayland_compositor *c) |
| 98 | { |
| 99 | EGLint major, minor; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 100 | EGLint n; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 101 | const char *extensions; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 102 | EGLint config_attribs[] = { |
| 103 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 104 | EGL_RED_SIZE, 1, |
| 105 | EGL_GREEN_SIZE, 1, |
| 106 | EGL_BLUE_SIZE, 1, |
| 107 | EGL_DEPTH_SIZE, 1, |
Kristian Høgsberg | d28ab36 | 2011-03-02 11:36:30 -0500 | [diff] [blame] | 108 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 109 | EGL_NONE |
| 110 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 111 | static const EGLint context_attribs[] = { |
| 112 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 113 | EGL_NONE |
| 114 | }; |
| 115 | |
Egbert Eich | e7b8d90 | 2011-05-10 20:00:19 +0000 | [diff] [blame] | 116 | setenv("EGL_PLATFORM", "wayland", 1); |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 117 | c->base.display = eglGetDisplay(c->parent.display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 118 | if (c->base.display == NULL) { |
| 119 | fprintf(stderr, "failed to create display\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (!eglInitialize(c->base.display, &major, &minor)) { |
| 124 | fprintf(stderr, "failed to initialize display\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | extensions = eglQueryString(c->base.display, EGL_EXTENSIONS); |
| 129 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 130 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 135 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 136 | return -1; |
| 137 | } |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 138 | if (!eglChooseConfig(c->base.display, config_attribs, |
| 139 | &c->base.config, 1, &n) || n == 0) { |
| 140 | fprintf(stderr, "failed to choose config: %d\n", n); |
| 141 | return -1; |
| 142 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 143 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 144 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 145 | EGL_NO_CONTEXT, context_attribs); |
| 146 | if (c->base.context == NULL) { |
| 147 | fprintf(stderr, "failed to create context\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE, |
| 152 | EGL_NO_SURFACE, c->base.context)) { |
| 153 | fprintf(stderr, "failed to make context current\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 160 | static int |
| 161 | wayland_output_prepare_render(struct wlsc_output *output_base) |
| 162 | { |
| 163 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 164 | struct wlsc_compositor *ec = output->base.compositor; |
| 165 | |
| 166 | if (!eglMakeCurrent(ec->display, output->egl_surface, |
| 167 | output->egl_surface, ec->context)) { |
| 168 | fprintf(stderr, "failed to make current\n"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 175 | static void |
| 176 | frame_done(void *data, struct wl_callback *wl_callback, uint32_t time) |
| 177 | { |
| 178 | struct wlsc_output *output = data; |
| 179 | |
| 180 | wlsc_output_finish_frame(output, time); |
| 181 | } |
| 182 | |
| 183 | static const struct wl_callback_listener frame_listener = { |
| 184 | frame_done |
| 185 | }; |
| 186 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 187 | static int |
| 188 | wayland_output_present(struct wlsc_output *output_base) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 189 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 190 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 191 | struct wayland_compositor *c = |
| 192 | (struct wayland_compositor *) output->base.compositor; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 193 | struct wl_callback *callback; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 194 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 195 | if (wayland_output_prepare_render(&output->base)) |
| 196 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 197 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 198 | eglSwapBuffers(c->base.display, output->egl_surface); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 199 | callback = wl_surface_frame(output->parent.surface); |
| 200 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 201 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 202 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 206 | wayland_output_prepare_scanout_surface(struct wlsc_output *output_base, |
| 207 | struct wlsc_surface *es) |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 208 | { |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 209 | return -1; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 213 | wayland_output_set_cursor(struct wlsc_output *output_base, |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 214 | struct wlsc_input_device *input) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 215 | { |
| 216 | return -1; |
| 217 | } |
| 218 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 219 | static void |
| 220 | wayland_output_destroy(struct wlsc_output *output_base) |
| 221 | { |
| 222 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 223 | struct wlsc_compositor *ec = output->base.compositor; |
| 224 | |
| 225 | eglDestroySurface(ec->display, output->egl_surface); |
| 226 | wl_egl_window_destroy(output->parent.egl_window); |
| 227 | free(output); |
| 228 | |
| 229 | return; |
| 230 | } |
| 231 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 232 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 233 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 234 | int width, int height) |
| 235 | { |
| 236 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 237 | |
| 238 | output = malloc(sizeof *output); |
| 239 | if (output == NULL) |
| 240 | return -1; |
| 241 | memset(output, 0, sizeof *output); |
| 242 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 243 | output->mode.flags = |
| 244 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 245 | output->mode.width = width; |
| 246 | output->mode.height = height; |
| 247 | output->mode.refresh = 60; |
| 248 | wl_list_init(&output->base.mode_list); |
| 249 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 250 | |
| 251 | output->base.current = &output->mode; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 252 | wlsc_output_init(&output->base, &c->base, 0, 0, width, height, |
| 253 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 254 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 255 | output->parent.surface = |
| 256 | wl_compositor_create_surface(c->parent.compositor); |
| 257 | wl_surface_set_user_data(output->parent.surface, output); |
| 258 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 259 | output->parent.egl_window = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 260 | wl_egl_window_create(output->parent.surface, |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 261 | width, height, c->parent.visual); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 262 | if (!output->parent.egl_window) { |
| 263 | fprintf(stderr, "failure to create wl_egl_window\n"); |
| 264 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 267 | output->egl_surface = |
| 268 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 269 | output->parent.egl_window, NULL); |
| 270 | if (!output->egl_surface) { |
| 271 | fprintf(stderr, "failed to create window surface\n"); |
| 272 | goto cleanup_window; |
| 273 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 274 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 275 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 276 | output->egl_surface, c->base.context)) { |
| 277 | fprintf(stderr, "failed to make surface current\n"); |
| 278 | goto cleanup_surface; |
| 279 | return -1; |
| 280 | } |
| 281 | |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 282 | wl_shell_set_toplevel(c->parent.shell, output->parent.surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 283 | |
| 284 | glClearColor(0, 0, 0, 0.5); |
| 285 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 286 | output->base.prepare_render = wayland_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 287 | output->base.present = wayland_output_present; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 288 | output->base.prepare_scanout_surface = |
| 289 | wayland_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 290 | output->base.set_hardware_cursor = wayland_output_set_cursor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 291 | output->base.destroy = wayland_output_destroy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 292 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 293 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 294 | |
| 295 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 296 | |
| 297 | cleanup_surface: |
| 298 | eglDestroySurface(c->base.display, output->egl_surface); |
| 299 | cleanup_window: |
| 300 | wl_egl_window_destroy(output->parent.egl_window); |
| 301 | cleanup_output: |
| 302 | /* FIXME: cleanup wlsc_output */ |
| 303 | free(output); |
| 304 | |
| 305 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 308 | /* Events received from the wayland-server this compositor is client of: */ |
| 309 | |
| 310 | /* parent output interface */ |
| 311 | static void |
| 312 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 313 | struct wl_output *wl_output, |
| 314 | int x, |
| 315 | int y, |
| 316 | int physical_width, |
| 317 | int physical_height, |
| 318 | int subpixel, |
| 319 | const char *make, |
| 320 | const char *model) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 321 | { |
| 322 | struct wayland_compositor *c = data; |
| 323 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 324 | c->parent.screen_allocation.x = x; |
| 325 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void |
| 329 | display_handle_mode(void *data, |
| 330 | struct wl_output *wl_output, |
| 331 | uint32_t flags, |
| 332 | int width, |
| 333 | int height, |
| 334 | int refresh) |
| 335 | { |
| 336 | struct wayland_compositor *c = data; |
| 337 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 338 | c->parent.screen_allocation.width = width; |
| 339 | c->parent.screen_allocation.height = height; |
| 340 | } |
| 341 | |
| 342 | static const struct wl_output_listener output_listener = { |
| 343 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 344 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | /* parent shell interface */ |
| 348 | static void |
| 349 | handle_configure(void *data, struct wl_shell *shell, |
| 350 | uint32_t time, uint32_t edges, |
Kristian Høgsberg | cbe6f04 | 2010-12-17 09:54:45 -0500 | [diff] [blame] | 351 | struct wl_surface *surface, int32_t width, int32_t height) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 352 | { |
| 353 | #if 0 |
| 354 | struct output *output = wl_surface_get_user_data(surface); |
| 355 | |
| 356 | /* FIXME: add resize? */ |
| 357 | #endif |
| 358 | } |
| 359 | |
| 360 | static const struct wl_shell_listener shell_listener = { |
| 361 | handle_configure, |
| 362 | }; |
| 363 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 364 | /* parent input interface */ |
| 365 | static void |
| 366 | input_handle_motion(void *data, struct wl_input_device *input_device, |
| 367 | uint32_t time, |
| 368 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 369 | { |
| 370 | struct wayland_input *input = data; |
| 371 | struct wayland_compositor *c = input->compositor; |
| 372 | |
| 373 | notify_motion(c->base.input_device, time, sx, sy); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | input_handle_button(void *data, |
| 378 | struct wl_input_device *input_device, |
| 379 | uint32_t time, uint32_t button, uint32_t state) |
| 380 | { |
| 381 | struct wayland_input *input = data; |
| 382 | struct wayland_compositor *c = input->compositor; |
| 383 | |
| 384 | notify_button(c->base.input_device, time, button, state); |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | input_handle_key(void *data, struct wl_input_device *input_device, |
| 389 | uint32_t time, uint32_t key, uint32_t state) |
| 390 | { |
| 391 | struct wayland_input *input = data; |
| 392 | struct wayland_compositor *c = input->compositor; |
| 393 | |
| 394 | notify_key(c->base.input_device, time, key, state); |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | input_handle_pointer_focus(void *data, |
| 399 | struct wl_input_device *input_device, |
| 400 | uint32_t time, struct wl_surface *surface, |
| 401 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 402 | { |
| 403 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 404 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 405 | struct wayland_compositor *c = input->compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 406 | |
| 407 | if (surface) { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 408 | output = wl_surface_get_user_data(surface); |
| 409 | notify_pointer_focus(c->base.input_device, |
| 410 | time, &output->base, sx, sy); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 411 | } else { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 412 | notify_pointer_focus(c->base.input_device, time, NULL, 0, 0); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | input_handle_keyboard_focus(void *data, |
| 418 | struct wl_input_device *input_device, |
| 419 | uint32_t time, |
| 420 | struct wl_surface *surface, |
| 421 | struct wl_array *keys) |
| 422 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 423 | struct wayland_input *input = data; |
| 424 | struct wayland_compositor *c = input->compositor; |
| 425 | struct wayland_output *output; |
| 426 | |
| 427 | if (surface) { |
| 428 | output = wl_surface_get_user_data(surface); |
| 429 | notify_keyboard_focus(c->base.input_device, |
| 430 | time, &output->base, keys); |
| 431 | } else { |
| 432 | notify_keyboard_focus(c->base.input_device, time, NULL, NULL); |
| 433 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static const struct wl_input_device_listener input_device_listener = { |
| 437 | input_handle_motion, |
| 438 | input_handle_button, |
| 439 | input_handle_key, |
| 440 | input_handle_pointer_focus, |
| 441 | input_handle_keyboard_focus, |
| 442 | }; |
| 443 | |
| 444 | static void |
| 445 | display_add_input(struct wayland_compositor *c, uint32_t id) |
| 446 | { |
| 447 | struct wayland_input *input; |
| 448 | |
| 449 | input = malloc(sizeof *input); |
| 450 | if (input == NULL) |
| 451 | return; |
| 452 | |
| 453 | memset(input, 0, sizeof *input); |
| 454 | |
| 455 | input->compositor = c; |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame^] | 456 | input->input_device = wl_display_bind(c->parent.display, |
| 457 | id, &wl_input_device_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 458 | wl_list_insert(c->input_list.prev, &input->link); |
| 459 | |
| 460 | wl_input_device_add_listener(input->input_device, |
| 461 | &input_device_listener, input); |
| 462 | wl_input_device_set_user_data(input->input_device, input); |
| 463 | } |
| 464 | |
| 465 | static void |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 466 | compositor_handle_visual(void *data, |
| 467 | struct wl_compositor *compositor, |
| 468 | uint32_t id, uint32_t token) |
| 469 | { |
| 470 | struct wayland_compositor *c = data; |
| 471 | |
| 472 | switch (token) { |
| 473 | case WL_COMPOSITOR_VISUAL_ARGB32: |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame^] | 474 | c->parent.visual = wl_display_bind(c->parent.display, |
| 475 | id, &wl_visual_interface); |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static const struct wl_compositor_listener compositor_listener = { |
| 481 | compositor_handle_visual, |
| 482 | }; |
| 483 | |
| 484 | static void |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 485 | display_handle_global(struct wl_display *display, uint32_t id, |
| 486 | const char *interface, uint32_t version, void *data) |
| 487 | { |
| 488 | struct wayland_compositor *c = data; |
| 489 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 490 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame^] | 491 | c->parent.compositor = |
| 492 | wl_display_bind(display, id, &wl_compositor_interface); |
Kristian Høgsberg | 7c47667 | 2011-06-18 06:22:05 -0400 | [diff] [blame] | 493 | wl_compositor_add_listener(c->parent.compositor, |
| 494 | &compositor_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 495 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame^] | 496 | c->parent.output = |
| 497 | wl_display_bind(display, id, &wl_output_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 498 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 499 | } else if (strcmp(interface, "wl_input_device") == 0) { |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 500 | display_add_input(c, id); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 501 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame^] | 502 | c->parent.shell = |
| 503 | wl_display_bind(display, id, &wl_shell_interface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 504 | wl_shell_add_listener(c->parent.shell, &shell_listener, c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | static int |
| 509 | update_event_mask(uint32_t mask, void *data) |
| 510 | { |
| 511 | struct wayland_compositor *c = data; |
| 512 | |
| 513 | c->parent.event_mask = mask; |
| 514 | if (c->parent.wl_source) |
| 515 | wl_event_source_fd_update(c->parent.wl_source, mask); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 520 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 521 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 522 | { |
| 523 | struct wayland_compositor *c = data; |
| 524 | |
| 525 | if (mask & WL_EVENT_READABLE) |
| 526 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 527 | if (mask & WL_EVENT_WRITEABLE) |
| 528 | wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 529 | |
| 530 | return 1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 533 | static void |
| 534 | wayland_destroy(struct wlsc_compositor *ec) |
| 535 | { |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 536 | wlsc_compositor_shutdown(ec); |
| 537 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 538 | free(ec); |
| 539 | } |
| 540 | |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 541 | static struct wlsc_compositor * |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 542 | wayland_compositor_create(struct wl_display *display, int width, int height) |
| 543 | { |
| 544 | struct wayland_compositor *c; |
| 545 | struct wl_event_loop *loop; |
| 546 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 547 | |
| 548 | c = malloc(sizeof *c); |
| 549 | if (c == NULL) |
| 550 | return NULL; |
| 551 | |
| 552 | memset(c, 0, sizeof *c); |
| 553 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 554 | c->parent.display = wl_display_connect(NULL); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 555 | |
| 556 | if (c->parent.display == NULL) { |
| 557 | fprintf(stderr, "failed to create display: %m\n"); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
| 561 | wl_list_init(&c->input_list); |
| 562 | wl_display_add_global_listener(c->parent.display, |
| 563 | display_handle_global, c); |
| 564 | |
| 565 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 566 | |
| 567 | c->base.wl_display = display; |
| 568 | if (wayland_compositor_init_egl(c) < 0) |
| 569 | return NULL; |
| 570 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 571 | c->base.destroy = wayland_destroy; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 572 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 573 | /* Can't init base class until we have a current egl context */ |
| 574 | if (wlsc_compositor_init(&c->base, display) < 0) |
| 575 | return NULL; |
| 576 | |
| 577 | if (wayland_compositor_create_output(c, width, height) < 0) |
| 578 | return NULL; |
| 579 | |
| 580 | if (wayland_input_create(c) < 0) |
| 581 | return NULL; |
| 582 | |
| 583 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 584 | |
| 585 | fd = wl_display_get_fd(c->parent.display, update_event_mask, c); |
| 586 | c->parent.wl_source = |
| 587 | wl_event_loop_add_fd(loop, fd, c->parent.event_mask, |
| 588 | wayland_compositor_handle_event, c); |
| 589 | if (c->parent.wl_source == NULL) |
| 590 | return NULL; |
| 591 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 592 | return &c->base; |
| 593 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 594 | |
| 595 | struct wlsc_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 596 | backend_init(struct wl_display *display, char *options); |
| 597 | |
| 598 | WL_EXPORT struct wlsc_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 599 | backend_init(struct wl_display *display, char *options) |
| 600 | { |
| 601 | int width = 1024, height = 640, i; |
| 602 | char *p, *value; |
| 603 | |
| 604 | static char * const tokens[] = { "width", "height", NULL }; |
| 605 | |
| 606 | p = options; |
| 607 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 608 | switch (i) { |
| 609 | case 0: |
| 610 | width = strtol(value, NULL, 0); |
| 611 | break; |
| 612 | case 1: |
| 613 | height = strtol(value, NULL, 0); |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return wayland_compositor_create(display, width, height); |
| 619 | } |