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