Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2010 Kristian Høgsberg |
| 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 | |
Chia-I Wu | 1b6c0ed | 2010-10-29 15:20:18 +0800 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 23 | #include <stddef.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/time.h> |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 31 | #include <linux/input.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 32 | |
| 33 | #include <xcb/xcb.h> |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 34 | #include <X11/Xlib.h> |
| 35 | #include <X11/Xlib-xcb.h> |
| 36 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 37 | #include <GLES2/gl2.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 38 | #include <EGL/egl.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 39 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 40 | #include "compositor.h" |
| 41 | |
| 42 | #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) |
| 43 | |
| 44 | struct x11_compositor { |
| 45 | struct wlsc_compositor base; |
| 46 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 47 | Display *dpy; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 48 | xcb_connection_t *conn; |
| 49 | xcb_screen_t *screen; |
| 50 | xcb_cursor_t null_cursor; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 51 | struct wl_array keys; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 52 | struct wl_event_source *xcb_source; |
| 53 | struct { |
| 54 | xcb_atom_t wm_protocols; |
| 55 | xcb_atom_t wm_normal_hints; |
| 56 | xcb_atom_t wm_size_hints; |
| 57 | xcb_atom_t wm_delete_window; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 58 | xcb_atom_t wm_class; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 59 | xcb_atom_t net_wm_name; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 60 | xcb_atom_t net_wm_icon; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 61 | xcb_atom_t string; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 62 | xcb_atom_t utf8_string; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 63 | xcb_atom_t cardinal; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 64 | } atom; |
| 65 | }; |
| 66 | |
| 67 | struct x11_output { |
| 68 | struct wlsc_output base; |
| 69 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 70 | xcb_window_t window; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 71 | EGLSurface egl_surface; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct x11_input { |
| 75 | struct wlsc_input_device base; |
| 76 | }; |
| 77 | |
| 78 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 79 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 80 | x11_input_create(struct x11_compositor *c) |
| 81 | { |
| 82 | struct x11_input *input; |
| 83 | |
| 84 | input = malloc(sizeof *input); |
| 85 | if (input == NULL) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 86 | return -1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 87 | |
| 88 | memset(input, 0, sizeof *input); |
| 89 | wlsc_input_device_init(&input->base, &c->base); |
| 90 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 91 | c->base.input_device = &input->base.input_device; |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 92 | |
| 93 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 96 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 97 | x11_compositor_init_egl(struct x11_compositor *c) |
| 98 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 99 | EGLint major, minor; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 100 | EGLint n; |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 101 | const char *extensions; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +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, |
| 108 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 109 | EGL_NONE |
| 110 | }; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 111 | static const EGLint context_attribs[] = { |
| 112 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 113 | EGL_NONE |
| 114 | }; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 115 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 116 | c->base.display = eglGetDisplay(c->dpy); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [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 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 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"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 133 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 134 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 135 | return -1; |
| 136 | } |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +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 | } |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 142 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 143 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 144 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 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 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 159 | static int |
| 160 | x11_output_prepare_render(struct wlsc_output *output_base) |
| 161 | { |
| 162 | struct x11_output *output = (struct x11_output *) output_base; |
| 163 | struct wlsc_compositor *ec = output->base.compositor; |
| 164 | |
| 165 | if (!eglMakeCurrent(ec->display, output->egl_surface, |
| 166 | output->egl_surface, ec->context)) { |
| 167 | fprintf(stderr, "failed to make current\n"); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 174 | static int |
| 175 | x11_output_present(struct wlsc_output *output_base) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 176 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 177 | struct x11_output *output = (struct x11_output *) output_base; |
| 178 | struct wlsc_compositor *ec = output->base.compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 179 | struct timeval tv; |
| 180 | uint32_t msec; |
| 181 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 182 | if (x11_output_prepare_render(&output->base)) |
| 183 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 184 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 185 | eglSwapBuffers(ec->display, output->egl_surface); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 186 | |
| 187 | gettimeofday(&tv, NULL); |
| 188 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 189 | wlsc_output_finish_frame(&output->base, msec); |
| 190 | |
| 191 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 194 | static int |
| 195 | x11_output_image_is_scanoutable(struct wlsc_output *output_base, |
| 196 | EGLImageKHR image) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame^] | 201 | static int |
| 202 | x11_output_set_cursor(struct wlsc_output *output_base, |
| 203 | struct wl_input_device *input) |
| 204 | { |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 209 | static void |
| 210 | x11_output_set_wm_protocols(struct x11_output *output) |
| 211 | { |
| 212 | xcb_atom_t list[1]; |
| 213 | struct x11_compositor *c = |
| 214 | (struct x11_compositor *) output->base.compositor; |
| 215 | |
| 216 | list[0] = c->atom.wm_delete_window; |
| 217 | xcb_change_property (c->conn, |
| 218 | XCB_PROP_MODE_REPLACE, |
| 219 | output->window, |
| 220 | c->atom.wm_protocols, |
| 221 | XCB_ATOM_ATOM, |
| 222 | 32, |
| 223 | ARRAY_SIZE(list), |
| 224 | list); |
| 225 | } |
| 226 | |
| 227 | struct wm_normal_hints { |
| 228 | uint32_t flags; |
| 229 | uint32_t pad[4]; |
| 230 | int32_t min_width, min_height; |
| 231 | int32_t max_width, max_height; |
| 232 | int32_t width_inc, height_inc; |
| 233 | int32_t min_aspect_x, min_aspect_y; |
| 234 | int32_t max_aspect_x, max_aspect_y; |
| 235 | int32_t base_width, base_height; |
| 236 | int32_t win_gravity; |
| 237 | }; |
| 238 | |
| 239 | #define WM_NORMAL_HINTS_MIN_SIZE 16 |
| 240 | #define WM_NORMAL_HINTS_MAX_SIZE 32 |
| 241 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 242 | static void |
| 243 | x11_output_set_icon(struct x11_compositor *c, struct x11_output *output, |
| 244 | const char *filename, int width, int height) |
| 245 | { |
| 246 | uint32_t *icon, *pixels; |
| 247 | |
| 248 | pixels = wlsc_load_image(filename, width, height); |
| 249 | if (!pixels) |
| 250 | return; |
| 251 | icon = malloc(width * height * 4 + 8); |
| 252 | if (!icon) { |
| 253 | free(pixels); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | icon[0] = width; |
| 258 | icon[1] = height; |
| 259 | memcpy(icon + 2, pixels, width * height * 4); |
| 260 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 261 | c->atom.net_wm_icon, c->atom.cardinal, 32, |
| 262 | width * height + 2, icon); |
| 263 | free(icon); |
| 264 | free(pixels); |
| 265 | } |
| 266 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 267 | static int |
| 268 | x11_compositor_create_output(struct x11_compositor *c, int width, int height) |
| 269 | { |
| 270 | static const char name[] = "Wayland Compositor"; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 271 | static const char class[] = "wayland-1\0Wayland Compositor"; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 272 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 273 | xcb_screen_iterator_t iter; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 274 | struct wm_normal_hints normal_hints; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 275 | uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR; |
| 276 | uint32_t values[2] = { |
| 277 | XCB_EVENT_MASK_KEY_PRESS | |
| 278 | XCB_EVENT_MASK_KEY_RELEASE | |
| 279 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 280 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 281 | XCB_EVENT_MASK_POINTER_MOTION | |
| 282 | XCB_EVENT_MASK_EXPOSURE | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 283 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | |
| 284 | XCB_EVENT_MASK_ENTER_WINDOW | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 285 | XCB_EVENT_MASK_LEAVE_WINDOW | |
| 286 | XCB_EVENT_MASK_KEYMAP_STATE | |
| 287 | XCB_EVENT_MASK_FOCUS_CHANGE, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 288 | 0 |
| 289 | }; |
| 290 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 291 | output = malloc(sizeof *output); |
| 292 | if (output == NULL) |
| 293 | return -1; |
| 294 | |
| 295 | memset(output, 0, sizeof *output); |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 296 | wlsc_output_init(&output->base, &c->base, 0, 0, width, height, |
| 297 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 298 | |
| 299 | values[1] = c->null_cursor; |
| 300 | output->window = xcb_generate_id(c->conn); |
| 301 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 302 | xcb_create_window(c->conn, |
| 303 | XCB_COPY_FROM_PARENT, |
| 304 | output->window, |
| 305 | iter.data->root, |
| 306 | 0, 0, |
| 307 | width, height, |
| 308 | 0, |
| 309 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 310 | iter.data->root_visual, |
| 311 | mask, values); |
| 312 | |
| 313 | /* Don't resize me. */ |
| 314 | memset(&normal_hints, 0, sizeof normal_hints); |
| 315 | normal_hints.flags = |
| 316 | WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE; |
| 317 | normal_hints.min_width = width; |
| 318 | normal_hints.min_height = height; |
| 319 | normal_hints.max_width = width; |
| 320 | normal_hints.max_height = height; |
| 321 | xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 322 | c->atom.wm_normal_hints, |
| 323 | c->atom.wm_size_hints, 32, |
| 324 | sizeof normal_hints / 4, |
| 325 | (uint8_t *) &normal_hints); |
| 326 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 327 | /* Set window name. Don't bother with non-EWMH WMs. */ |
| 328 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 329 | c->atom.net_wm_name, c->atom.utf8_string, 8, |
| 330 | strlen(name), name); |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 331 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 332 | c->atom.wm_class, c->atom.string, 8, |
| 333 | sizeof class, class); |
| 334 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 335 | x11_output_set_icon(c, output, |
| 336 | DATADIR "/wayland/wayland.png", 128, 128); |
| 337 | |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 338 | xcb_map_window(c->conn, output->window); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 339 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 340 | x11_output_set_wm_protocols(output); |
| 341 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 342 | output->egl_surface = |
| 343 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 344 | output->window, NULL); |
| 345 | if (!output->egl_surface) { |
| 346 | fprintf(stderr, "failed to create window surface\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 347 | return -1; |
| 348 | } |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 349 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 350 | output->egl_surface, c->base.context)) { |
| 351 | fprintf(stderr, "failed to make surface current\n"); |
| 352 | return -1; |
| 353 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 354 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 355 | output->base.prepare_render = x11_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 356 | output->base.present = x11_output_present; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 357 | output->base.image_is_scanoutable = x11_output_image_is_scanoutable; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame^] | 358 | output->base.set_hardware_cursor = x11_output_set_cursor; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 359 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 360 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 365 | static struct x11_output * |
| 366 | x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window) |
| 367 | { |
| 368 | struct x11_output *output; |
| 369 | |
| 370 | wl_list_for_each(output, &c->base.output_list, base.link) { |
| 371 | if (output->window == window) |
| 372 | return output; |
| 373 | } |
| 374 | |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | x11_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 380 | { |
| 381 | struct x11_compositor *c = data; |
| 382 | struct x11_output *output; |
| 383 | xcb_generic_event_t *event; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 384 | xcb_client_message_event_t *client_message; |
| 385 | xcb_motion_notify_event_t *motion_notify; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 386 | xcb_enter_notify_event_t *enter_notify; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 387 | xcb_key_press_event_t *key_press; |
| 388 | xcb_button_press_event_t *button_press; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 389 | xcb_keymap_notify_event_t *keymap_notify; |
| 390 | xcb_focus_in_event_t *focus_in; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 391 | xcb_atom_t atom; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 392 | uint32_t *k; |
| 393 | int i, set; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 394 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 395 | while (event = xcb_poll_for_event (c->conn), event != NULL) { |
| 396 | switch (event->response_type & ~0x80) { |
| 397 | |
| 398 | case XCB_KEY_PRESS: |
| 399 | key_press = (xcb_key_press_event_t *) event; |
| 400 | notify_key(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 401 | key_press->time, key_press->detail - 8, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 402 | break; |
| 403 | case XCB_KEY_RELEASE: |
| 404 | key_press = (xcb_key_press_event_t *) event; |
| 405 | notify_key(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 406 | key_press->time, key_press->detail - 8, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 407 | break; |
| 408 | case XCB_BUTTON_PRESS: |
| 409 | button_press = (xcb_button_press_event_t *) event; |
| 410 | notify_button(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 411 | button_press->time, |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 412 | button_press->detail + BTN_LEFT - 1, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 413 | break; |
| 414 | case XCB_BUTTON_RELEASE: |
| 415 | button_press = (xcb_button_press_event_t *) event; |
| 416 | notify_button(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 417 | button_press->time, |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 418 | button_press->detail + BTN_LEFT - 1, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 419 | break; |
| 420 | |
| 421 | case XCB_MOTION_NOTIFY: |
| 422 | motion_notify = (xcb_motion_notify_event_t *) event; |
| 423 | notify_motion(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 424 | motion_notify->time, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 425 | motion_notify->event_x, |
| 426 | motion_notify->event_y); |
| 427 | break; |
| 428 | |
| 429 | case XCB_EXPOSE: |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 430 | /* FIXME: schedule output repaint */ |
| 431 | /* output = x11_compositor_find_output(c, expose->window); */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 432 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 433 | wlsc_compositor_schedule_repaint(&c->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 434 | break; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 435 | |
| 436 | case XCB_ENTER_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 437 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 438 | if (enter_notify->state >= Button1Mask) |
| 439 | break; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 440 | output = x11_compositor_find_output(c, enter_notify->event); |
| 441 | notify_pointer_focus(c->base.input_device, |
| 442 | enter_notify->time, |
| 443 | &output->base, |
| 444 | enter_notify->event_x, |
| 445 | enter_notify->event_y); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 446 | break; |
| 447 | |
| 448 | case XCB_LEAVE_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 449 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 450 | if (enter_notify->state >= Button1Mask) |
| 451 | break; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 452 | notify_pointer_focus(c->base.input_device, |
| 453 | enter_notify->time, |
| 454 | NULL, |
| 455 | enter_notify->event_x, |
| 456 | enter_notify->event_y); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 457 | break; |
| 458 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 459 | case XCB_CLIENT_MESSAGE: |
| 460 | client_message = (xcb_client_message_event_t *) event; |
| 461 | atom = client_message->data.data32[0]; |
| 462 | if (atom == c->atom.wm_delete_window) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 463 | wl_display_terminate(c->base.wl_display); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 464 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 465 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 466 | case XCB_FOCUS_IN: |
| 467 | focus_in = (xcb_focus_in_event_t *) event; |
| 468 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 469 | break; |
| 470 | |
| 471 | output = x11_compositor_find_output(c, focus_in->event); |
| 472 | notify_keyboard_focus(c->base.input_device, |
| 473 | get_time(), |
| 474 | &output->base, &c->keys); |
| 475 | break; |
| 476 | |
| 477 | case XCB_FOCUS_OUT: |
| 478 | focus_in = (xcb_focus_in_event_t *) event; |
| 479 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 480 | break; |
| 481 | notify_keyboard_focus(c->base.input_device, |
| 482 | get_time(), NULL, NULL); |
| 483 | break; |
| 484 | |
| 485 | case XCB_KEYMAP_NOTIFY: |
| 486 | keymap_notify = (xcb_keymap_notify_event_t *) event; |
| 487 | c->keys.size = 0; |
| 488 | for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) { |
| 489 | set = keymap_notify->keys[i >> 3] & |
| 490 | (1 << (i & 7)); |
| 491 | if (set) { |
| 492 | k = wl_array_add(&c->keys, sizeof *k); |
| 493 | *k = i; |
| 494 | } |
| 495 | } |
| 496 | break; |
| 497 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | |
| 501 | free (event); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 502 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | #define F(field) offsetof(struct x11_compositor, field) |
| 506 | |
| 507 | static void |
| 508 | x11_compositor_get_resources(struct x11_compositor *c) |
| 509 | { |
| 510 | static const struct { const char *name; int offset; } atoms[] = { |
| 511 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 512 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
| 513 | { "WM_SIZE_HINTS", F(atom.wm_size_hints) }, |
| 514 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 515 | { "WM_CLASS", F(atom.wm_class) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 516 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 517 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 518 | { "STRING", F(atom.string) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 519 | { "UTF8_STRING", F(atom.utf8_string) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 520 | { "CARDINAL", F(atom.cardinal) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)]; |
| 524 | xcb_intern_atom_reply_t *reply; |
| 525 | xcb_pixmap_t pixmap; |
| 526 | xcb_gc_t gc; |
| 527 | int i; |
| 528 | uint8_t data[] = { 0, 0, 0, 0 }; |
| 529 | |
| 530 | for (i = 0; i < ARRAY_SIZE(atoms); i++) |
| 531 | cookies[i] = xcb_intern_atom (c->conn, 0, |
| 532 | strlen(atoms[i].name), |
| 533 | atoms[i].name); |
| 534 | |
| 535 | for (i = 0; i < ARRAY_SIZE(atoms); i++) { |
| 536 | reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL); |
| 537 | *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom; |
| 538 | free(reply); |
| 539 | } |
| 540 | |
| 541 | pixmap = xcb_generate_id(c->conn); |
| 542 | gc = xcb_generate_id(c->conn); |
| 543 | xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1); |
| 544 | xcb_create_gc(c->conn, gc, pixmap, 0, NULL); |
| 545 | xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP, |
| 546 | pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data); |
| 547 | c->null_cursor = xcb_generate_id(c->conn); |
| 548 | xcb_create_cursor (c->conn, c->null_cursor, |
| 549 | pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1); |
| 550 | xcb_free_gc(c->conn, gc); |
| 551 | xcb_free_pixmap(c->conn, pixmap); |
| 552 | } |
| 553 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 554 | static void |
| 555 | x11_destroy(struct wlsc_compositor *ec) |
| 556 | { |
| 557 | free(ec); |
| 558 | } |
| 559 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 560 | struct wlsc_compositor * |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 561 | x11_compositor_create(struct wl_display *display, int width, int height) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 562 | { |
| 563 | struct x11_compositor *c; |
| 564 | struct wl_event_loop *loop; |
| 565 | xcb_screen_iterator_t s; |
| 566 | |
| 567 | c = malloc(sizeof *c); |
| 568 | if (c == NULL) |
| 569 | return NULL; |
| 570 | |
| 571 | memset(c, 0, sizeof *c); |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 572 | |
| 573 | c->dpy = XOpenDisplay(NULL); |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 574 | |
| 575 | if (c->dpy == NULL) |
| 576 | return NULL; |
| 577 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 578 | c->conn = XGetXCBConnection(c->dpy); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 579 | |
| 580 | if (xcb_connection_has_error(c->conn)) |
| 581 | return NULL; |
| 582 | |
| 583 | s = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 584 | c->screen = s.data; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 585 | wl_array_init(&c->keys); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 586 | |
| 587 | x11_compositor_get_resources(c); |
| 588 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 589 | c->base.wl_display = display; |
Tiago Vignatti | 997ce64 | 2010-11-10 02:42:35 +0200 | [diff] [blame] | 590 | if (x11_compositor_init_egl(c) < 0) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 591 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 592 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 593 | c->base.destroy = x11_destroy; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 594 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 595 | /* Can't init base class until we have a current egl context */ |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 596 | if (wlsc_compositor_init(&c->base, display) < 0) |
| 597 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 598 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 599 | if (x11_compositor_create_output(c, width, height) < 0) |
| 600 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 601 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 602 | if (x11_input_create(c) < 0) |
| 603 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 604 | |
| 605 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 606 | |
| 607 | c->xcb_source = |
| 608 | wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn), |
| 609 | WL_EVENT_READABLE, |
| 610 | x11_compositor_handle_event, c); |
| 611 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 612 | return &c->base; |
| 613 | } |