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