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 | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 61 | xcb_atom_t net_wm_state; |
| 62 | xcb_atom_t net_wm_state_fullscreen; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 63 | xcb_atom_t string; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 64 | xcb_atom_t utf8_string; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 65 | xcb_atom_t cardinal; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 66 | } atom; |
| 67 | }; |
| 68 | |
| 69 | struct x11_output { |
| 70 | struct wlsc_output base; |
| 71 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 72 | xcb_window_t window; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 73 | EGLSurface egl_surface; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 74 | struct wlsc_mode mode; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct x11_input { |
| 78 | struct wlsc_input_device base; |
| 79 | }; |
| 80 | |
| 81 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 82 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 83 | x11_input_create(struct x11_compositor *c) |
| 84 | { |
| 85 | struct x11_input *input; |
| 86 | |
| 87 | input = malloc(sizeof *input); |
| 88 | if (input == NULL) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 89 | return -1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 90 | |
| 91 | memset(input, 0, sizeof *input); |
| 92 | wlsc_input_device_init(&input->base, &c->base); |
| 93 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 94 | c->base.input_device = &input->base.input_device; |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 95 | |
| 96 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 99 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 100 | x11_compositor_init_egl(struct x11_compositor *c) |
| 101 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 102 | EGLint major, minor; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 103 | EGLint n; |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 104 | const char *extensions; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 105 | EGLint config_attribs[] = { |
| 106 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 107 | EGL_RED_SIZE, 1, |
| 108 | EGL_GREEN_SIZE, 1, |
| 109 | EGL_BLUE_SIZE, 1, |
| 110 | EGL_DEPTH_SIZE, 1, |
| 111 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 112 | EGL_NONE |
| 113 | }; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 114 | static const EGLint context_attribs[] = { |
| 115 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 116 | EGL_NONE |
| 117 | }; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 118 | |
Egbert Eich | e7b8d90 | 2011-05-10 20:00:19 +0000 | [diff] [blame] | 119 | setenv("EGL_PLATFORM", "x11", 1); |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 120 | c->base.display = eglGetDisplay(c->dpy); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 121 | if (c->base.display == NULL) { |
| 122 | fprintf(stderr, "failed to create display\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | if (!eglInitialize(c->base.display, &major, &minor)) { |
| 127 | fprintf(stderr, "failed to initialize display\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 131 | extensions = eglQueryString(c->base.display, EGL_EXTENSIONS); |
| 132 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 133 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 137 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 138 | fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n"); |
| 139 | return -1; |
| 140 | } |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 141 | if (!eglChooseConfig(c->base.display, config_attribs, |
| 142 | &c->base.config, 1, &n) || n == 0) { |
| 143 | fprintf(stderr, "failed to choose config: %d\n", n); |
| 144 | return -1; |
| 145 | } |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 146 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 147 | c->base.context = eglCreateContext(c->base.display, c->base.config, |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 148 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 149 | if (c->base.context == NULL) { |
| 150 | fprintf(stderr, "failed to create context\n"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE, |
| 155 | EGL_NO_SURFACE, c->base.context)) { |
| 156 | fprintf(stderr, "failed to make context current\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 163 | static int |
| 164 | x11_output_prepare_render(struct wlsc_output *output_base) |
| 165 | { |
| 166 | struct x11_output *output = (struct x11_output *) output_base; |
| 167 | struct wlsc_compositor *ec = output->base.compositor; |
| 168 | |
| 169 | if (!eglMakeCurrent(ec->display, output->egl_surface, |
| 170 | output->egl_surface, ec->context)) { |
| 171 | fprintf(stderr, "failed to make current\n"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 178 | static int |
| 179 | x11_output_present(struct wlsc_output *output_base) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 180 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 181 | struct x11_output *output = (struct x11_output *) output_base; |
| 182 | struct wlsc_compositor *ec = output->base.compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 183 | struct timeval tv; |
| 184 | uint32_t msec; |
| 185 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 186 | if (x11_output_prepare_render(&output->base)) |
| 187 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 188 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 189 | eglSwapBuffers(ec->display, output->egl_surface); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 190 | |
| 191 | gettimeofday(&tv, NULL); |
| 192 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 193 | wlsc_output_finish_frame(&output->base, msec); |
| 194 | |
| 195 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 198 | static int |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 199 | x11_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 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 205 | static int |
| 206 | x11_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 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame^] | 212 | static void |
| 213 | x11_output_destroy(struct wlsc_output *output_base) |
| 214 | { |
| 215 | return; |
| 216 | } |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 217 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 218 | static void |
| 219 | x11_output_set_wm_protocols(struct x11_output *output) |
| 220 | { |
| 221 | xcb_atom_t list[1]; |
| 222 | struct x11_compositor *c = |
| 223 | (struct x11_compositor *) output->base.compositor; |
| 224 | |
| 225 | list[0] = c->atom.wm_delete_window; |
| 226 | xcb_change_property (c->conn, |
| 227 | XCB_PROP_MODE_REPLACE, |
| 228 | output->window, |
| 229 | c->atom.wm_protocols, |
| 230 | XCB_ATOM_ATOM, |
| 231 | 32, |
| 232 | ARRAY_SIZE(list), |
| 233 | list); |
| 234 | } |
| 235 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 236 | static void |
| 237 | x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state) |
| 238 | { |
| 239 | xcb_client_message_event_t event; |
| 240 | struct x11_compositor *c = |
| 241 | (struct x11_compositor *) output->base.compositor; |
| 242 | xcb_screen_iterator_t iter; |
| 243 | |
| 244 | #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */ |
| 245 | #define _NET_WM_STATE_ADD 1 /* add/set property */ |
| 246 | #define _NET_WM_STATE_TOGGLE 2 /* toggle property */ |
| 247 | |
| 248 | memset(&event, 0, sizeof event); |
| 249 | event.response_type = XCB_CLIENT_MESSAGE; |
| 250 | event.format = 32; |
| 251 | event.window = output->window; |
| 252 | event.type = c->atom.net_wm_state; |
| 253 | |
| 254 | event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 255 | event.data.data32[1] = state; |
| 256 | event.data.data32[2] = 0; |
| 257 | event.data.data32[3] = 0; |
| 258 | event.data.data32[4] = 0; |
| 259 | |
| 260 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 261 | xcb_send_event(c->conn, 0, iter.data->root, |
| 262 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 263 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 264 | (void *) &event); |
| 265 | } |
| 266 | |
| 267 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 268 | struct wm_normal_hints { |
| 269 | uint32_t flags; |
| 270 | uint32_t pad[4]; |
| 271 | int32_t min_width, min_height; |
| 272 | int32_t max_width, max_height; |
| 273 | int32_t width_inc, height_inc; |
| 274 | int32_t min_aspect_x, min_aspect_y; |
| 275 | int32_t max_aspect_x, max_aspect_y; |
| 276 | int32_t base_width, base_height; |
| 277 | int32_t win_gravity; |
| 278 | }; |
| 279 | |
| 280 | #define WM_NORMAL_HINTS_MIN_SIZE 16 |
| 281 | #define WM_NORMAL_HINTS_MAX_SIZE 32 |
| 282 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 283 | static void |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 284 | x11_output_set_icon(struct x11_compositor *c, |
| 285 | struct x11_output *output, const char *filename) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 286 | { |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 287 | uint32_t *icon, *pixels, stride; |
| 288 | int32_t width, height; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 289 | |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 290 | pixels = wlsc_load_image(filename, &width, &height, &stride); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 291 | if (!pixels) |
| 292 | return; |
| 293 | icon = malloc(width * height * 4 + 8); |
| 294 | if (!icon) { |
| 295 | free(pixels); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | icon[0] = width; |
| 300 | icon[1] = height; |
| 301 | memcpy(icon + 2, pixels, width * height * 4); |
| 302 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 303 | c->atom.net_wm_icon, c->atom.cardinal, 32, |
| 304 | width * height + 2, icon); |
| 305 | free(icon); |
| 306 | free(pixels); |
| 307 | } |
| 308 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 309 | static int |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 310 | x11_compositor_create_output(struct x11_compositor *c, int x, int y, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 311 | int width, int height, int fullscreen) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 312 | { |
| 313 | static const char name[] = "Wayland Compositor"; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 314 | static const char class[] = "wayland-1\0Wayland Compositor"; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 315 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 316 | xcb_screen_iterator_t iter; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 317 | struct wm_normal_hints normal_hints; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 318 | uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR; |
| 319 | uint32_t values[2] = { |
| 320 | XCB_EVENT_MASK_KEY_PRESS | |
| 321 | XCB_EVENT_MASK_KEY_RELEASE | |
| 322 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 323 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 324 | XCB_EVENT_MASK_POINTER_MOTION | |
| 325 | XCB_EVENT_MASK_EXPOSURE | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 326 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | |
| 327 | XCB_EVENT_MASK_ENTER_WINDOW | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 328 | XCB_EVENT_MASK_LEAVE_WINDOW | |
| 329 | XCB_EVENT_MASK_KEYMAP_STATE | |
| 330 | XCB_EVENT_MASK_FOCUS_CHANGE, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 331 | 0 |
| 332 | }; |
| 333 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 334 | output = malloc(sizeof *output); |
| 335 | if (output == NULL) |
| 336 | return -1; |
| 337 | |
| 338 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 339 | |
| 340 | output->mode.flags = |
| 341 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 342 | output->mode.width = width; |
| 343 | output->mode.height = height; |
| 344 | output->mode.refresh = 60; |
| 345 | wl_list_init(&output->base.mode_list); |
| 346 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 347 | |
| 348 | output->base.current = &output->mode; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 349 | wlsc_output_init(&output->base, &c->base, x, y, width, height, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 350 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 351 | |
| 352 | values[1] = c->null_cursor; |
| 353 | output->window = xcb_generate_id(c->conn); |
| 354 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 355 | xcb_create_window(c->conn, |
| 356 | XCB_COPY_FROM_PARENT, |
| 357 | output->window, |
| 358 | iter.data->root, |
| 359 | 0, 0, |
| 360 | width, height, |
| 361 | 0, |
| 362 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 363 | iter.data->root_visual, |
| 364 | mask, values); |
| 365 | |
| 366 | /* Don't resize me. */ |
| 367 | memset(&normal_hints, 0, sizeof normal_hints); |
| 368 | normal_hints.flags = |
| 369 | WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE; |
| 370 | normal_hints.min_width = width; |
| 371 | normal_hints.min_height = height; |
| 372 | normal_hints.max_width = width; |
| 373 | normal_hints.max_height = height; |
| 374 | xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 375 | c->atom.wm_normal_hints, |
| 376 | c->atom.wm_size_hints, 32, |
| 377 | sizeof normal_hints / 4, |
| 378 | (uint8_t *) &normal_hints); |
| 379 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 380 | /* Set window name. Don't bother with non-EWMH WMs. */ |
| 381 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 382 | c->atom.net_wm_name, c->atom.utf8_string, 8, |
| 383 | strlen(name), name); |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 384 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 385 | c->atom.wm_class, c->atom.string, 8, |
| 386 | sizeof class, class); |
| 387 | |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 388 | x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png"); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 389 | |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 390 | xcb_map_window(c->conn, output->window); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 391 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 392 | x11_output_set_wm_protocols(output); |
| 393 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 394 | if (fullscreen) |
| 395 | x11_output_change_state(output, 1, |
| 396 | c->atom.net_wm_state_fullscreen); |
| 397 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 398 | output->egl_surface = |
| 399 | eglCreateWindowSurface(c->base.display, c->base.config, |
| 400 | output->window, NULL); |
| 401 | if (!output->egl_surface) { |
| 402 | fprintf(stderr, "failed to create window surface\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 403 | return -1; |
| 404 | } |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 405 | if (!eglMakeCurrent(c->base.display, output->egl_surface, |
| 406 | output->egl_surface, c->base.context)) { |
| 407 | fprintf(stderr, "failed to make surface current\n"); |
| 408 | return -1; |
| 409 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 410 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 411 | output->base.prepare_render = x11_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 412 | output->base.present = x11_output_present; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 413 | output->base.prepare_scanout_surface = |
| 414 | x11_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 415 | output->base.set_hardware_cursor = x11_output_set_cursor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame^] | 416 | output->base.destroy = x11_output_destroy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 417 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 418 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 423 | static struct x11_output * |
| 424 | x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window) |
| 425 | { |
| 426 | struct x11_output *output; |
| 427 | |
| 428 | wl_list_for_each(output, &c->base.output_list, base.link) { |
| 429 | if (output->window == window) |
| 430 | return output; |
| 431 | } |
| 432 | |
| 433 | return NULL; |
| 434 | } |
| 435 | |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 436 | static int |
| 437 | x11_compositor_next_event(struct x11_compositor *c, |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 438 | xcb_generic_event_t **event, uint32_t mask) |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 439 | { |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 440 | if (mask & WL_EVENT_READABLE) { |
| 441 | *event = xcb_poll_for_event(c->conn); |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 442 | } else { |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 443 | #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 444 | *event = xcb_poll_for_queued_event(c->conn); |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 445 | #else |
| 446 | *event = xcb_poll_for_event(c->conn); |
| 447 | #endif |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return *event != NULL; |
| 451 | } |
| 452 | |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 453 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 454 | x11_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 455 | { |
| 456 | struct x11_compositor *c = data; |
| 457 | struct x11_output *output; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 458 | xcb_generic_event_t *event, *prev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 459 | xcb_client_message_event_t *client_message; |
| 460 | xcb_motion_notify_event_t *motion_notify; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 461 | xcb_enter_notify_event_t *enter_notify; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 462 | xcb_key_press_event_t *key_press, *key_release; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 463 | xcb_button_press_event_t *button_press; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 464 | xcb_keymap_notify_event_t *keymap_notify; |
| 465 | xcb_focus_in_event_t *focus_in; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 466 | xcb_atom_t atom; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 467 | uint32_t *k; |
| 468 | int i, set; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 469 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 470 | prev = NULL; |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 471 | while (x11_compositor_next_event(c, &event, mask)) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 472 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 473 | case XCB_KEY_RELEASE: |
| 474 | key_release = (xcb_key_press_event_t *) prev; |
| 475 | key_press = (xcb_key_press_event_t *) event; |
| 476 | if ((event->response_type & ~0x80) == XCB_KEY_PRESS && |
Dima Ryazanov | c224748 | 2011-08-16 17:25:32 -0700 | [diff] [blame] | 477 | key_release->time == key_press->time && |
| 478 | key_release->detail == key_press->detail) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 479 | /* Don't deliver the held key release |
| 480 | * event or the new key press event. */ |
| 481 | free(event); |
| 482 | free(prev); |
| 483 | prev = NULL; |
| 484 | continue; |
| 485 | } else { |
| 486 | /* Deliver the held key release now |
| 487 | * and fall through and handle the new |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 488 | * event below. */ |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 489 | notify_key(c->base.input_device, |
| 490 | key_release->time, |
| 491 | key_release->detail - 8, 0); |
| 492 | free(prev); |
| 493 | prev = NULL; |
| 494 | break; |
| 495 | } |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 496 | |
| 497 | case XCB_FOCUS_IN: |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 498 | /* assert event is keymap_notify */ |
| 499 | focus_in = (xcb_focus_in_event_t *) prev; |
| 500 | keymap_notify = (xcb_keymap_notify_event_t *) event; |
| 501 | c->keys.size = 0; |
| 502 | for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) { |
| 503 | set = keymap_notify->keys[i >> 3] & |
| 504 | (1 << (i & 7)); |
| 505 | if (set) { |
| 506 | k = wl_array_add(&c->keys, sizeof *k); |
| 507 | *k = i; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | output = x11_compositor_find_output(c, focus_in->event); |
| 512 | notify_keyboard_focus(c->base.input_device, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 513 | wlsc_compositor_get_time(), |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 514 | &output->base, &c->keys); |
| 515 | |
| 516 | free(prev); |
| 517 | prev = NULL; |
| 518 | break; |
| 519 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 520 | default: |
| 521 | /* No previous event held */ |
| 522 | break; |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 525 | switch (event->response_type & ~0x80) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 526 | case XCB_KEY_PRESS: |
| 527 | key_press = (xcb_key_press_event_t *) event; |
| 528 | notify_key(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 529 | key_press->time, key_press->detail - 8, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 530 | break; |
| 531 | case XCB_KEY_RELEASE: |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 532 | prev = event; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 533 | break; |
| 534 | case XCB_BUTTON_PRESS: |
| 535 | button_press = (xcb_button_press_event_t *) event; |
| 536 | notify_button(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 537 | button_press->time, |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 538 | button_press->detail + BTN_LEFT - 1, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 539 | break; |
| 540 | case XCB_BUTTON_RELEASE: |
| 541 | button_press = (xcb_button_press_event_t *) event; |
| 542 | notify_button(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 543 | button_press->time, |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 544 | button_press->detail + BTN_LEFT - 1, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 545 | break; |
| 546 | |
| 547 | case XCB_MOTION_NOTIFY: |
| 548 | motion_notify = (xcb_motion_notify_event_t *) event; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 549 | output = x11_compositor_find_output(c, motion_notify->event); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 550 | notify_motion(c->base.input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 551 | motion_notify->time, |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 552 | output->base.x + motion_notify->event_x, |
| 553 | output->base.y + motion_notify->event_y); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | case XCB_EXPOSE: |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 557 | /* FIXME: schedule output repaint */ |
| 558 | /* output = x11_compositor_find_output(c, expose->window); */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 559 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 560 | wlsc_compositor_schedule_repaint(&c->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 561 | break; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 562 | |
| 563 | case XCB_ENTER_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 564 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 565 | if (enter_notify->state >= Button1Mask) |
| 566 | break; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 567 | output = x11_compositor_find_output(c, enter_notify->event); |
| 568 | notify_pointer_focus(c->base.input_device, |
| 569 | enter_notify->time, |
| 570 | &output->base, |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 571 | output->base.x + enter_notify->event_x, |
| 572 | output->base.y + enter_notify->event_y); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | case XCB_LEAVE_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 576 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 577 | if (enter_notify->state >= Button1Mask) |
| 578 | break; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 579 | output = x11_compositor_find_output(c, enter_notify->event); |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 580 | notify_pointer_focus(c->base.input_device, |
| 581 | enter_notify->time, |
| 582 | NULL, |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 583 | output->base.x + enter_notify->event_x, |
| 584 | output->base.y + enter_notify->event_y); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 585 | break; |
| 586 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 587 | case XCB_CLIENT_MESSAGE: |
| 588 | client_message = (xcb_client_message_event_t *) event; |
| 589 | atom = client_message->data.data32[0]; |
| 590 | if (atom == c->atom.wm_delete_window) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 591 | wl_display_terminate(c->base.wl_display); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 592 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 593 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 594 | case XCB_FOCUS_IN: |
| 595 | focus_in = (xcb_focus_in_event_t *) event; |
| 596 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 597 | break; |
| 598 | |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 599 | prev = event; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 600 | break; |
| 601 | |
| 602 | case XCB_FOCUS_OUT: |
| 603 | focus_in = (xcb_focus_in_event_t *) event; |
| 604 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 605 | break; |
| 606 | notify_keyboard_focus(c->base.input_device, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 607 | wlsc_compositor_get_time(), |
| 608 | NULL, NULL); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 609 | break; |
| 610 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 611 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 612 | break; |
| 613 | } |
| 614 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 615 | if (prev != event) |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 616 | free (event); |
| 617 | } |
| 618 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 619 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 620 | case XCB_KEY_RELEASE: |
| 621 | key_release = (xcb_key_press_event_t *) prev; |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 622 | notify_key(c->base.input_device, |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 623 | key_release->time, key_release->detail - 8, 0); |
| 624 | free(prev); |
| 625 | prev = NULL; |
| 626 | break; |
| 627 | default: |
| 628 | break; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 629 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 630 | |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 631 | return event != NULL; |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 634 | #define F(field) offsetof(struct x11_compositor, field) |
| 635 | |
| 636 | static void |
| 637 | x11_compositor_get_resources(struct x11_compositor *c) |
| 638 | { |
| 639 | static const struct { const char *name; int offset; } atoms[] = { |
| 640 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 641 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
| 642 | { "WM_SIZE_HINTS", F(atom.wm_size_hints) }, |
| 643 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 644 | { "WM_CLASS", F(atom.wm_class) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 645 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 646 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 647 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 648 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 649 | { "STRING", F(atom.string) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 650 | { "UTF8_STRING", F(atom.utf8_string) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 651 | { "CARDINAL", F(atom.cardinal) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)]; |
| 655 | xcb_intern_atom_reply_t *reply; |
| 656 | xcb_pixmap_t pixmap; |
| 657 | xcb_gc_t gc; |
| 658 | int i; |
| 659 | uint8_t data[] = { 0, 0, 0, 0 }; |
| 660 | |
| 661 | for (i = 0; i < ARRAY_SIZE(atoms); i++) |
| 662 | cookies[i] = xcb_intern_atom (c->conn, 0, |
| 663 | strlen(atoms[i].name), |
| 664 | atoms[i].name); |
| 665 | |
| 666 | for (i = 0; i < ARRAY_SIZE(atoms); i++) { |
| 667 | reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL); |
| 668 | *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom; |
| 669 | free(reply); |
| 670 | } |
| 671 | |
| 672 | pixmap = xcb_generate_id(c->conn); |
| 673 | gc = xcb_generate_id(c->conn); |
| 674 | xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1); |
| 675 | xcb_create_gc(c->conn, gc, pixmap, 0, NULL); |
| 676 | xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP, |
| 677 | pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data); |
| 678 | c->null_cursor = xcb_generate_id(c->conn); |
| 679 | xcb_create_cursor (c->conn, c->null_cursor, |
| 680 | pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1); |
| 681 | xcb_free_gc(c->conn, gc); |
| 682 | xcb_free_pixmap(c->conn, pixmap); |
| 683 | } |
| 684 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 685 | static void |
| 686 | x11_destroy(struct wlsc_compositor *ec) |
| 687 | { |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame^] | 688 | wlsc_compositor_shutdown(ec); |
| 689 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 690 | free(ec); |
| 691 | } |
| 692 | |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 693 | static struct wlsc_compositor * |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 694 | x11_compositor_create(struct wl_display *display, |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 695 | int width, int height, int count, int fullscreen) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 696 | { |
| 697 | struct x11_compositor *c; |
| 698 | struct wl_event_loop *loop; |
| 699 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 700 | int i, x; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 701 | |
| 702 | c = malloc(sizeof *c); |
| 703 | if (c == NULL) |
| 704 | return NULL; |
| 705 | |
| 706 | memset(c, 0, sizeof *c); |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 707 | |
| 708 | c->dpy = XOpenDisplay(NULL); |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 709 | if (c->dpy == NULL) |
| 710 | return NULL; |
| 711 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 712 | c->conn = XGetXCBConnection(c->dpy); |
Benjamin Franzke | e997c5f | 2011-03-25 14:06:37 +0100 | [diff] [blame] | 713 | XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue); |
| 714 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 715 | if (xcb_connection_has_error(c->conn)) |
| 716 | return NULL; |
| 717 | |
| 718 | s = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 719 | c->screen = s.data; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 720 | wl_array_init(&c->keys); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 721 | |
| 722 | x11_compositor_get_resources(c); |
| 723 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 724 | c->base.wl_display = display; |
Tiago Vignatti | 997ce64 | 2010-11-10 02:42:35 +0200 | [diff] [blame] | 725 | if (x11_compositor_init_egl(c) < 0) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 726 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 727 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 728 | c->base.destroy = x11_destroy; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 729 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 730 | /* 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] | 731 | if (wlsc_compositor_init(&c->base, display) < 0) |
| 732 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 733 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 734 | for (i = 0, x = 0; i < count; i++) { |
| 735 | if (x11_compositor_create_output(c, x, 0, width, height, |
| 736 | fullscreen) < 0) |
| 737 | return NULL; |
| 738 | x += width; |
| 739 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 740 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 741 | if (x11_input_create(c) < 0) |
| 742 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 743 | |
| 744 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 745 | |
| 746 | c->xcb_source = |
| 747 | wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn), |
| 748 | WL_EVENT_READABLE, |
| 749 | x11_compositor_handle_event, c); |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 750 | wl_event_source_check(c->xcb_source); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 751 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 752 | return &c->base; |
| 753 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 754 | |
| 755 | struct wlsc_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 756 | backend_init(struct wl_display *display, char *options); |
| 757 | |
| 758 | WL_EXPORT struct wlsc_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 759 | backend_init(struct wl_display *display, char *options) |
| 760 | { |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 761 | int width = 1024, height = 640, fullscreen = 0, count = 1, i; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 762 | char *p, *value; |
| 763 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 764 | static char * const tokens[] = { |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 765 | "width", "height", "fullscreen", "output-count", NULL |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 766 | }; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 767 | |
| 768 | p = options; |
| 769 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 770 | switch (i) { |
| 771 | case 0: |
| 772 | width = strtol(value, NULL, 0); |
| 773 | break; |
| 774 | case 1: |
| 775 | height = strtol(value, NULL, 0); |
| 776 | break; |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 777 | case 2: |
| 778 | fullscreen = 1; |
| 779 | break; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 780 | case 3: |
| 781 | count = strtol(value, NULL, 0); |
| 782 | break; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 786 | return x11_compositor_create(display, |
| 787 | width, height, count, fullscreen); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 788 | } |