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