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; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 46 | struct wl_egl_display *egl_display; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 47 | struct wl_compositor *compositor; |
| 48 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 49 | struct wl_output *output; |
| 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 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 115 | c->base.display = eglGetDisplay(c->parent.egl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 116 | if (c->base.display == NULL) { |
| 117 | fprintf(stderr, "failed to create display\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if (!eglInitialize(c->base.display, &major, &minor)) { |
| 122 | fprintf(stderr, "failed to initialize display\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | extensions = eglQueryString(c->base.display, EGL_EXTENSIONS); |
| 127 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 128 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 133 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 134 | return -1; |
| 135 | } |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 136 | if (!eglChooseConfig(c->base.display, config_attribs, |
| 137 | &c->base.config, 1, &n) || n == 0) { |
| 138 | fprintf(stderr, "failed to choose config: %d\n", n); |
| 139 | return -1; |
| 140 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 141 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 142 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 143 | EGL_NO_CONTEXT, context_attribs); |
| 144 | if (c->base.context == NULL) { |
| 145 | fprintf(stderr, "failed to create context\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE, |
| 150 | EGL_NO_SURFACE, c->base.context)) { |
| 151 | fprintf(stderr, "failed to make context current\n"); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 159 | frame_callback(struct wl_surface *surface, void *data, uint32_t time) |
Kristian Høgsberg | 3ada7ec | 2010-12-01 09:42:10 -0500 | [diff] [blame] | 160 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 161 | struct wlsc_output *output = data; |
Kristian Høgsberg | 3ada7ec | 2010-12-01 09:42:10 -0500 | [diff] [blame] | 162 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 163 | wlsc_output_finish_frame(output, time); |
Kristian Høgsberg | 3ada7ec | 2010-12-01 09:42:10 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 166 | static int |
| 167 | wayland_output_prepare_render(struct wlsc_output *output_base) |
| 168 | { |
| 169 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 170 | struct wlsc_compositor *ec = output->base.compositor; |
| 171 | |
| 172 | if (!eglMakeCurrent(ec->display, output->egl_surface, |
| 173 | output->egl_surface, ec->context)) { |
| 174 | fprintf(stderr, "failed to make current\n"); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 181 | static int |
| 182 | wayland_output_present(struct wlsc_output *output_base) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 183 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 184 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 185 | struct wayland_compositor *c = |
| 186 | (struct wayland_compositor *) output->base.compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 187 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 188 | if (wayland_output_prepare_render(&output->base)) |
| 189 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 190 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 191 | eglSwapBuffers(c->base.display, output->egl_surface); |
| 192 | wl_display_frame_callback(c->parent.display, |
| 193 | output->parent.surface, |
| 194 | frame_callback, &output->base); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 195 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 196 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame^] | 200 | wayland_output_prepare_scanout_surface(struct wlsc_output *output_base, |
| 201 | struct wlsc_surface *es) |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 202 | { |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame^] | 203 | return -1; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 207 | wayland_output_set_cursor(struct wlsc_output *output_base, |
| 208 | struct wl_input_device *input) |
| 209 | { |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 214 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 215 | int width, int height) |
| 216 | { |
| 217 | struct wayland_output *output; |
| 218 | struct wl_visual *visual; |
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 | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 231 | visual = wl_display_get_premultiplied_argb_visual(c->parent.display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 232 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 233 | output->parent.egl_window = |
| 234 | wl_egl_window_create(c->parent.egl_display, |
| 235 | output->parent.surface, |
| 236 | width, height, visual); |
| 237 | if (!output->parent.egl_window) { |
| 238 | fprintf(stderr, "failure to create wl_egl_window\n"); |
| 239 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 242 | output->egl_surface = |
| 243 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 244 | output->parent.egl_window, NULL); |
| 245 | if (!output->egl_surface) { |
| 246 | fprintf(stderr, "failed to create window surface\n"); |
| 247 | goto cleanup_window; |
| 248 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 249 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 250 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 251 | output->egl_surface, c->base.context)) { |
| 252 | fprintf(stderr, "failed to make surface current\n"); |
| 253 | goto cleanup_surface; |
| 254 | return -1; |
| 255 | } |
| 256 | |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 257 | wl_surface_map_toplevel(output->parent.surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 258 | |
| 259 | glClearColor(0, 0, 0, 0.5); |
| 260 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 261 | output->base.prepare_render = wayland_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 262 | output->base.present = wayland_output_present; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame^] | 263 | output->base.prepare_scanout_surface = |
| 264 | wayland_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 265 | output->base.set_hardware_cursor = wayland_output_set_cursor; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 266 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 267 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 268 | |
| 269 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 270 | |
| 271 | cleanup_surface: |
| 272 | eglDestroySurface(c->base.display, output->egl_surface); |
| 273 | cleanup_window: |
| 274 | wl_egl_window_destroy(output->parent.egl_window); |
| 275 | cleanup_output: |
| 276 | /* FIXME: cleanup wlsc_output */ |
| 277 | free(output); |
| 278 | |
| 279 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 282 | /* Events received from the wayland-server this compositor is client of: */ |
| 283 | |
| 284 | /* parent output interface */ |
| 285 | static void |
| 286 | display_handle_geometry(void *data, |
| 287 | struct wl_output *output, |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 288 | int32_t x, int32_t y, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 289 | int32_t width, int32_t height) |
| 290 | { |
| 291 | struct wayland_compositor *c = data; |
| 292 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 293 | c->parent.screen_allocation.x = x; |
| 294 | c->parent.screen_allocation.y = y; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 295 | c->parent.screen_allocation.width = width; |
| 296 | c->parent.screen_allocation.height = height; |
| 297 | } |
| 298 | |
| 299 | static const struct wl_output_listener output_listener = { |
| 300 | display_handle_geometry, |
| 301 | }; |
| 302 | |
| 303 | /* parent shell interface */ |
| 304 | static void |
| 305 | handle_configure(void *data, struct wl_shell *shell, |
| 306 | uint32_t time, uint32_t edges, |
Kristian Høgsberg | cbe6f04 | 2010-12-17 09:54:45 -0500 | [diff] [blame] | 307 | struct wl_surface *surface, int32_t width, int32_t height) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 308 | { |
| 309 | #if 0 |
| 310 | struct output *output = wl_surface_get_user_data(surface); |
| 311 | |
| 312 | /* FIXME: add resize? */ |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | static const struct wl_shell_listener shell_listener = { |
| 317 | handle_configure, |
| 318 | }; |
| 319 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 320 | /* parent input interface */ |
| 321 | static void |
| 322 | input_handle_motion(void *data, struct wl_input_device *input_device, |
| 323 | uint32_t time, |
| 324 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 325 | { |
| 326 | struct wayland_input *input = data; |
| 327 | struct wayland_compositor *c = input->compositor; |
| 328 | |
| 329 | notify_motion(c->base.input_device, time, sx, sy); |
| 330 | } |
| 331 | |
| 332 | static void |
| 333 | input_handle_button(void *data, |
| 334 | struct wl_input_device *input_device, |
| 335 | uint32_t time, uint32_t button, uint32_t state) |
| 336 | { |
| 337 | struct wayland_input *input = data; |
| 338 | struct wayland_compositor *c = input->compositor; |
| 339 | |
| 340 | notify_button(c->base.input_device, time, button, state); |
| 341 | } |
| 342 | |
| 343 | static void |
| 344 | input_handle_key(void *data, struct wl_input_device *input_device, |
| 345 | uint32_t time, uint32_t key, uint32_t state) |
| 346 | { |
| 347 | struct wayland_input *input = data; |
| 348 | struct wayland_compositor *c = input->compositor; |
| 349 | |
| 350 | notify_key(c->base.input_device, time, key, state); |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | input_handle_pointer_focus(void *data, |
| 355 | struct wl_input_device *input_device, |
| 356 | uint32_t time, struct wl_surface *surface, |
| 357 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 358 | { |
| 359 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 360 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 361 | struct wayland_compositor *c = input->compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 362 | |
| 363 | if (surface) { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 364 | output = wl_surface_get_user_data(surface); |
| 365 | notify_pointer_focus(c->base.input_device, |
| 366 | time, &output->base, sx, sy); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 367 | } else { |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 368 | notify_pointer_focus(c->base.input_device, time, NULL, 0, 0); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | input_handle_keyboard_focus(void *data, |
| 374 | struct wl_input_device *input_device, |
| 375 | uint32_t time, |
| 376 | struct wl_surface *surface, |
| 377 | struct wl_array *keys) |
| 378 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 379 | struct wayland_input *input = data; |
| 380 | struct wayland_compositor *c = input->compositor; |
| 381 | struct wayland_output *output; |
| 382 | |
| 383 | if (surface) { |
| 384 | output = wl_surface_get_user_data(surface); |
| 385 | notify_keyboard_focus(c->base.input_device, |
| 386 | time, &output->base, keys); |
| 387 | } else { |
| 388 | notify_keyboard_focus(c->base.input_device, time, NULL, NULL); |
| 389 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static const struct wl_input_device_listener input_device_listener = { |
| 393 | input_handle_motion, |
| 394 | input_handle_button, |
| 395 | input_handle_key, |
| 396 | input_handle_pointer_focus, |
| 397 | input_handle_keyboard_focus, |
| 398 | }; |
| 399 | |
| 400 | static void |
| 401 | display_add_input(struct wayland_compositor *c, uint32_t id) |
| 402 | { |
| 403 | struct wayland_input *input; |
| 404 | |
| 405 | input = malloc(sizeof *input); |
| 406 | if (input == NULL) |
| 407 | return; |
| 408 | |
| 409 | memset(input, 0, sizeof *input); |
| 410 | |
| 411 | input->compositor = c; |
| 412 | input->input_device = wl_input_device_create(c->parent.display, id); |
| 413 | wl_list_insert(c->input_list.prev, &input->link); |
| 414 | |
| 415 | wl_input_device_add_listener(input->input_device, |
| 416 | &input_device_listener, input); |
| 417 | wl_input_device_set_user_data(input->input_device, input); |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | display_handle_global(struct wl_display *display, uint32_t id, |
| 422 | const char *interface, uint32_t version, void *data) |
| 423 | { |
| 424 | struct wayland_compositor *c = data; |
| 425 | |
| 426 | if (strcmp(interface, "compositor") == 0) { |
| 427 | c->parent.compositor = wl_compositor_create(display, id); |
| 428 | } else if (strcmp(interface, "output") == 0) { |
| 429 | c->parent.output = wl_output_create(display, id); |
| 430 | wl_output_add_listener(c->parent.output, &output_listener, c); |
| 431 | } else if (strcmp(interface, "input_device") == 0) { |
| 432 | display_add_input(c, id); |
| 433 | } else if (strcmp(interface, "shell") == 0) { |
| 434 | c->parent.shell = wl_shell_create(display, id); |
| 435 | wl_shell_add_listener(c->parent.shell, &shell_listener, c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | update_event_mask(uint32_t mask, void *data) |
| 441 | { |
| 442 | struct wayland_compositor *c = data; |
| 443 | |
| 444 | c->parent.event_mask = mask; |
| 445 | if (c->parent.wl_source) |
| 446 | wl_event_source_fd_update(c->parent.wl_source, mask); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static void |
| 452 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 453 | { |
| 454 | struct wayland_compositor *c = data; |
| 455 | |
| 456 | if (mask & WL_EVENT_READABLE) |
| 457 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 458 | if (mask & WL_EVENT_WRITEABLE) |
| 459 | wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE); |
| 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 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 468 | struct wlsc_compositor * |
| 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); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 489 | c->parent.egl_display = wl_egl_display_create(c->parent.display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 490 | wl_display_add_global_listener(c->parent.display, |
| 491 | display_handle_global, c); |
| 492 | |
| 493 | wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE); |
| 494 | |
| 495 | c->base.wl_display = display; |
| 496 | if (wayland_compositor_init_egl(c) < 0) |
| 497 | return NULL; |
| 498 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 499 | c->base.destroy = wayland_destroy; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 500 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 501 | /* Can't init base class until we have a current egl context */ |
| 502 | if (wlsc_compositor_init(&c->base, display) < 0) |
| 503 | return NULL; |
| 504 | |
| 505 | if (wayland_compositor_create_output(c, width, height) < 0) |
| 506 | return NULL; |
| 507 | |
| 508 | if (wayland_input_create(c) < 0) |
| 509 | return NULL; |
| 510 | |
| 511 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 512 | |
| 513 | fd = wl_display_get_fd(c->parent.display, update_event_mask, c); |
| 514 | c->parent.wl_source = |
| 515 | wl_event_loop_add_fd(loop, fd, c->parent.event_mask, |
| 516 | wayland_compositor_handle_event, c); |
| 517 | if (c->parent.wl_source == NULL) |
| 518 | return NULL; |
| 519 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 520 | return &c->base; |
| 521 | } |