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