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