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> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
| 33 | #include <errno.h> |
| 34 | #include <sys/time.h> |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 35 | #include <linux/input.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 36 | |
| 37 | #include <xcb/xcb.h> |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame^] | 38 | #ifdef HAVE_XCB_XKB |
| 39 | #include <xcb/xkb.h> |
| 40 | #endif |
| 41 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 42 | #include <X11/Xlib.h> |
| 43 | #include <X11/Xlib-xcb.h> |
| 44 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 45 | #include <xkbcommon/xkbcommon.h> |
| 46 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 47 | #include <GLES2/gl2.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 48 | #include <EGL/egl.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 49 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 50 | #include "compositor.h" |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 51 | #include "../shared/config-parser.h" |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 52 | #include "log.h" |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 53 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 54 | struct x11_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 55 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 56 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 57 | EGLSurface dummy_pbuffer; |
| 58 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 59 | Display *dpy; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 60 | xcb_connection_t *conn; |
| 61 | xcb_screen_t *screen; |
| 62 | xcb_cursor_t null_cursor; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 63 | struct wl_array keys; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 64 | struct wl_event_source *xcb_source; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 65 | struct xkb_keymap *xkb_keymap; |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame^] | 66 | unsigned int has_xkb; |
| 67 | uint8_t xkb_event_base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 68 | struct { |
| 69 | xcb_atom_t wm_protocols; |
| 70 | xcb_atom_t wm_normal_hints; |
| 71 | xcb_atom_t wm_size_hints; |
| 72 | xcb_atom_t wm_delete_window; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 73 | xcb_atom_t wm_class; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 74 | xcb_atom_t net_wm_name; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 75 | xcb_atom_t net_wm_icon; |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 76 | xcb_atom_t net_wm_state; |
| 77 | xcb_atom_t net_wm_state_fullscreen; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 78 | xcb_atom_t string; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 79 | xcb_atom_t utf8_string; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 80 | xcb_atom_t cardinal; |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 81 | xcb_atom_t xkb_names; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 82 | } atom; |
| 83 | }; |
| 84 | |
| 85 | struct x11_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 86 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 87 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 88 | xcb_window_t window; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 89 | EGLSurface egl_surface; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 90 | struct weston_mode mode; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 91 | struct wl_event_source *finish_frame_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct x11_input { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 95 | struct weston_seat base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 98 | static struct xkb_keymap * |
| 99 | x11_compositor_get_keymap(struct x11_compositor *c) |
| 100 | { |
| 101 | xcb_get_property_cookie_t cookie; |
| 102 | xcb_get_property_reply_t *reply; |
| 103 | xcb_generic_error_t *error; |
| 104 | struct xkb_rule_names names; |
| 105 | struct xkb_keymap *ret; |
| 106 | const char *value_all, *value_part; |
| 107 | int length_all, length_part; |
| 108 | |
| 109 | memset(&names, 0, sizeof(names)); |
| 110 | |
| 111 | cookie = xcb_get_property(c->conn, 0, c->screen->root, |
| 112 | c->atom.xkb_names, c->atom.string, 0, 1024); |
| 113 | reply = xcb_get_property_reply(c->conn, cookie, &error); |
| 114 | if (reply == NULL) |
| 115 | return NULL; |
| 116 | |
| 117 | value_all = xcb_get_property_value(reply); |
| 118 | length_all = xcb_get_property_value_length(reply); |
| 119 | value_part = value_all; |
| 120 | |
| 121 | #define copy_prop_value(to) \ |
| 122 | length_part = strlen(value_part); \ |
| 123 | if (value_part + length_part < (value_all + length_all) && \ |
| 124 | length_part > 0) \ |
| 125 | names.to = value_part; \ |
| 126 | value_part += length_part + 1; |
| 127 | |
| 128 | copy_prop_value(rules); |
| 129 | copy_prop_value(model); |
| 130 | copy_prop_value(layout); |
| 131 | copy_prop_value(variant); |
| 132 | copy_prop_value(options); |
| 133 | #undef copy_prop_value |
| 134 | |
| 135 | ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0); |
| 136 | |
| 137 | free(reply); |
| 138 | return ret; |
| 139 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 140 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame^] | 141 | static void |
| 142 | x11_compositor_setup_xkb(struct x11_compositor *c) |
| 143 | { |
| 144 | #ifndef HAVE_XCB_XKB |
| 145 | weston_log("XCB-XKB not available during build\n"); |
| 146 | c->has_xkb = 0; |
| 147 | c->xkb_event_base = 0; |
| 148 | return; |
| 149 | #else |
| 150 | const xcb_query_extension_reply_t *ext; |
| 151 | |
| 152 | c->has_xkb = 0; |
| 153 | c->xkb_event_base = 0; |
| 154 | |
| 155 | ext = xcb_get_extension_data(c->conn, &xcb_xkb_id); |
| 156 | if (!ext) { |
| 157 | weston_log("XKB extension not available on host X11 server\n"); |
| 158 | return; |
| 159 | } |
| 160 | c->xkb_event_base = ext->first_event; |
| 161 | |
| 162 | c->has_xkb = 1; |
| 163 | #endif |
| 164 | } |
| 165 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 166 | static int |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 167 | x11_input_create(struct x11_compositor *c, int no_input) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 168 | { |
| 169 | struct x11_input *input; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 170 | struct xkb_keymap *keymap; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 171 | |
| 172 | input = malloc(sizeof *input); |
| 173 | if (input == NULL) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 174 | return -1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 175 | |
| 176 | memset(input, 0, sizeof *input); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 177 | weston_seat_init(&input->base, &c->base); |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 178 | c->base.seat = &input->base; |
| 179 | |
| 180 | if (no_input) |
| 181 | return 0; |
| 182 | |
Daniel Stone | 74419a2 | 2012-05-30 16:32:02 +0100 | [diff] [blame] | 183 | weston_seat_init_pointer(&input->base); |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 184 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame^] | 185 | x11_compositor_setup_xkb(c); |
| 186 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 187 | keymap = x11_compositor_get_keymap(c); |
| 188 | weston_seat_init_keyboard(&input->base, keymap); |
| 189 | if (keymap) |
| 190 | xkb_map_unref(keymap); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 191 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 192 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 195 | static void |
| 196 | x11_input_destroy(struct x11_compositor *compositor) |
| 197 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 198 | struct x11_input *input = container_of(compositor->base.seat, |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 199 | struct x11_input, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 200 | base); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 201 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 202 | weston_seat_release(&input->base); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 203 | free(input); |
| 204 | } |
| 205 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 206 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 207 | x11_compositor_init_egl(struct x11_compositor *c) |
| 208 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 209 | EGLint major, minor; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 210 | EGLint n; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 211 | EGLint config_attribs[] = { |
| 212 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 213 | EGL_RED_SIZE, 1, |
| 214 | EGL_GREEN_SIZE, 1, |
| 215 | EGL_BLUE_SIZE, 1, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 216 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 217 | EGL_NONE |
| 218 | }; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 219 | static const EGLint context_attribs[] = { |
| 220 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 221 | EGL_NONE |
| 222 | }; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 223 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 224 | static const EGLint pbuffer_attribs[] = { |
| 225 | EGL_WIDTH, 10, |
| 226 | EGL_HEIGHT, 10, |
| 227 | EGL_NONE |
| 228 | }; |
| 229 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 230 | c->base.egl_display = eglGetDisplay(c->dpy); |
| 231 | if (c->base.egl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 232 | weston_log("failed to create display\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 236 | if (!eglInitialize(c->base.egl_display, &major, &minor)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 237 | weston_log("failed to initialize display\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 238 | return -1; |
| 239 | } |
| 240 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 241 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 242 | weston_log("failed to bind EGL_OPENGL_ES_API\n"); |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 243 | return -1; |
| 244 | } |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 245 | if (!eglChooseConfig(c->base.egl_display, config_attribs, |
| 246 | &c->base.egl_config, 1, &n) || n == 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 247 | weston_log("failed to choose config: %d\n", n); |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 248 | return -1; |
| 249 | } |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 250 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 251 | c->base.egl_context = |
| 252 | eglCreateContext(c->base.egl_display, c->base.egl_config, |
| 253 | EGL_NO_CONTEXT, context_attribs); |
| 254 | if (c->base.egl_context == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 255 | weston_log("failed to create context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 256 | return -1; |
| 257 | } |
| 258 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 259 | c->dummy_pbuffer = eglCreatePbufferSurface(c->base.egl_display, |
| 260 | c->base.egl_config, |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 261 | pbuffer_attribs); |
Kristian Høgsberg | c0bc844 | 2012-05-17 09:40:23 -0400 | [diff] [blame] | 262 | if (c->dummy_pbuffer == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 263 | weston_log("failed to create dummy pbuffer\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 267 | if (!eglMakeCurrent(c->base.egl_display, c->dummy_pbuffer, |
| 268 | c->dummy_pbuffer, c->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 269 | weston_log("failed to make context current\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 276 | static void |
| 277 | x11_compositor_fini_egl(struct x11_compositor *compositor) |
| 278 | { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 279 | eglMakeCurrent(compositor->base.egl_display, |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 280 | EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 281 | EGL_NO_CONTEXT); |
| 282 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 283 | eglTerminate(compositor->base.egl_display); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 284 | eglReleaseThread(); |
| 285 | } |
| 286 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 287 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 288 | x11_output_repaint(struct weston_output *output_base, |
| 289 | pixman_region32_t *damage) |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 290 | { |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 291 | struct x11_output *output = (struct x11_output *)output_base; |
| 292 | struct x11_compositor *compositor = |
| 293 | (struct x11_compositor *)output->base.compositor; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 294 | struct weston_surface *surface; |
| 295 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 296 | if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface, |
| 297 | output->egl_surface, |
| 298 | compositor->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 299 | weston_log("failed to make current\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
| 303 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 304 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 305 | |
Kristian Høgsberg | e0f832b | 2012-06-20 00:13:18 -0400 | [diff] [blame] | 306 | wl_signal_emit(&output->base.frame_signal, output); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 307 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 308 | eglSwapBuffers(compositor->base.egl_display, output->egl_surface); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 309 | |
| 310 | wl_event_source_timer_update(output->finish_frame_timer, 10); |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 313 | static int |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 314 | finish_frame_handler(void *data) |
| 315 | { |
| 316 | struct x11_output *output = data; |
| 317 | uint32_t msec; |
| 318 | struct timeval tv; |
| 319 | |
| 320 | gettimeofday(&tv, NULL); |
| 321 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 322 | weston_output_finish_frame(&output->base, msec); |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 323 | |
| 324 | return 1; |
| 325 | } |
| 326 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 327 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 328 | x11_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 329 | { |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 330 | struct x11_output *output = (struct x11_output *)output_base; |
| 331 | struct x11_compositor *compositor = |
| 332 | (struct x11_compositor *)output->base.compositor; |
| 333 | |
| 334 | wl_list_remove(&output->base.link); |
| 335 | wl_event_source_remove(output->finish_frame_timer); |
| 336 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 337 | eglDestroySurface(compositor->base.egl_display, output->egl_surface); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 338 | |
| 339 | xcb_destroy_window(compositor->conn, output->window); |
| 340 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 341 | weston_output_destroy(&output->base); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 342 | |
| 343 | free(output); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 344 | } |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 345 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 346 | static void |
| 347 | x11_output_set_wm_protocols(struct x11_output *output) |
| 348 | { |
| 349 | xcb_atom_t list[1]; |
| 350 | struct x11_compositor *c = |
| 351 | (struct x11_compositor *) output->base.compositor; |
| 352 | |
| 353 | list[0] = c->atom.wm_delete_window; |
| 354 | xcb_change_property (c->conn, |
| 355 | XCB_PROP_MODE_REPLACE, |
| 356 | output->window, |
| 357 | c->atom.wm_protocols, |
| 358 | XCB_ATOM_ATOM, |
| 359 | 32, |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 360 | ARRAY_LENGTH(list), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 361 | list); |
| 362 | } |
| 363 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 364 | static void |
| 365 | x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state) |
| 366 | { |
| 367 | xcb_client_message_event_t event; |
| 368 | struct x11_compositor *c = |
| 369 | (struct x11_compositor *) output->base.compositor; |
| 370 | xcb_screen_iterator_t iter; |
| 371 | |
| 372 | #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */ |
| 373 | #define _NET_WM_STATE_ADD 1 /* add/set property */ |
| 374 | #define _NET_WM_STATE_TOGGLE 2 /* toggle property */ |
| 375 | |
| 376 | memset(&event, 0, sizeof event); |
| 377 | event.response_type = XCB_CLIENT_MESSAGE; |
| 378 | event.format = 32; |
| 379 | event.window = output->window; |
| 380 | event.type = c->atom.net_wm_state; |
| 381 | |
| 382 | event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 383 | event.data.data32[1] = state; |
| 384 | event.data.data32[2] = 0; |
| 385 | event.data.data32[3] = 0; |
| 386 | event.data.data32[4] = 0; |
| 387 | |
| 388 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 389 | xcb_send_event(c->conn, 0, iter.data->root, |
| 390 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 391 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 392 | (void *) &event); |
| 393 | } |
| 394 | |
| 395 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 396 | struct wm_normal_hints { |
| 397 | uint32_t flags; |
| 398 | uint32_t pad[4]; |
| 399 | int32_t min_width, min_height; |
| 400 | int32_t max_width, max_height; |
| 401 | int32_t width_inc, height_inc; |
| 402 | int32_t min_aspect_x, min_aspect_y; |
| 403 | int32_t max_aspect_x, max_aspect_y; |
| 404 | int32_t base_width, base_height; |
| 405 | int32_t win_gravity; |
| 406 | }; |
| 407 | |
| 408 | #define WM_NORMAL_HINTS_MIN_SIZE 16 |
| 409 | #define WM_NORMAL_HINTS_MAX_SIZE 32 |
| 410 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 411 | static void |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 412 | x11_output_set_icon(struct x11_compositor *c, |
| 413 | struct x11_output *output, const char *filename) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 414 | { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 415 | uint32_t *icon; |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 416 | int32_t width, height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 417 | pixman_image_t *image; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 418 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 419 | image = load_image(filename); |
| 420 | if (!image) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 421 | return; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 422 | width = pixman_image_get_width(image); |
| 423 | height = pixman_image_get_height(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 424 | icon = malloc(width * height * 4 + 8); |
| 425 | if (!icon) { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 426 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
| 430 | icon[0] = width; |
| 431 | icon[1] = height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 432 | memcpy(icon + 2, pixman_image_get_data(image), width * height * 4); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 433 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 434 | c->atom.net_wm_icon, c->atom.cardinal, 32, |
| 435 | width * height + 2, icon); |
| 436 | free(icon); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 437 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 438 | } |
| 439 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 440 | static int |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 441 | x11_compositor_create_output(struct x11_compositor *c, int x, int y, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 442 | int width, int height, int fullscreen, |
| 443 | int no_input) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 444 | { |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 445 | static const char name[] = "Weston Compositor"; |
| 446 | static const char class[] = "weston-1\0Weston Compositor"; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 447 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 448 | xcb_screen_iterator_t iter; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 449 | struct wm_normal_hints normal_hints; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 450 | struct wl_event_loop *loop; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 451 | uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 452 | uint32_t values[2] = { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 453 | XCB_EVENT_MASK_EXPOSURE | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 454 | XCB_EVENT_MASK_STRUCTURE_NOTIFY, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 455 | 0 |
| 456 | }; |
| 457 | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 458 | if (!no_input) |
| 459 | values[0] |= |
| 460 | XCB_EVENT_MASK_KEY_PRESS | |
| 461 | XCB_EVENT_MASK_KEY_RELEASE | |
| 462 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 463 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 464 | XCB_EVENT_MASK_POINTER_MOTION | |
| 465 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 466 | XCB_EVENT_MASK_LEAVE_WINDOW | |
| 467 | XCB_EVENT_MASK_KEYMAP_STATE | |
| 468 | XCB_EVENT_MASK_FOCUS_CHANGE; |
| 469 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 470 | output = malloc(sizeof *output); |
| 471 | if (output == NULL) |
| 472 | return -1; |
| 473 | |
| 474 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 475 | |
| 476 | output->mode.flags = |
| 477 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 478 | output->mode.width = width; |
| 479 | output->mode.height = height; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 480 | output->mode.refresh = 60000; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 481 | wl_list_init(&output->base.mode_list); |
| 482 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 483 | |
| 484 | output->base.current = &output->mode; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 485 | weston_output_init(&output->base, &c->base, x, y, width, height, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 486 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 487 | |
| 488 | values[1] = c->null_cursor; |
| 489 | output->window = xcb_generate_id(c->conn); |
| 490 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 491 | xcb_create_window(c->conn, |
| 492 | XCB_COPY_FROM_PARENT, |
| 493 | output->window, |
| 494 | iter.data->root, |
| 495 | 0, 0, |
| 496 | width, height, |
| 497 | 0, |
| 498 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 499 | iter.data->root_visual, |
| 500 | mask, values); |
| 501 | |
| 502 | /* Don't resize me. */ |
| 503 | memset(&normal_hints, 0, sizeof normal_hints); |
| 504 | normal_hints.flags = |
| 505 | WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE; |
| 506 | normal_hints.min_width = width; |
| 507 | normal_hints.min_height = height; |
| 508 | normal_hints.max_width = width; |
| 509 | normal_hints.max_height = height; |
| 510 | xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 511 | c->atom.wm_normal_hints, |
| 512 | c->atom.wm_size_hints, 32, |
| 513 | sizeof normal_hints / 4, |
| 514 | (uint8_t *) &normal_hints); |
| 515 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 516 | /* Set window name. Don't bother with non-EWMH WMs. */ |
| 517 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 518 | c->atom.net_wm_name, c->atom.utf8_string, 8, |
| 519 | strlen(name), name); |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 520 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 521 | c->atom.wm_class, c->atom.string, 8, |
| 522 | sizeof class, class); |
| 523 | |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 524 | x11_output_set_icon(c, output, DATADIR "/weston/wayland.png"); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 525 | |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 526 | xcb_map_window(c->conn, output->window); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 527 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 528 | x11_output_set_wm_protocols(output); |
| 529 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 530 | if (fullscreen) |
| 531 | x11_output_change_state(output, 1, |
| 532 | c->atom.net_wm_state_fullscreen); |
| 533 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 534 | output->egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 535 | eglCreateWindowSurface(c->base.egl_display, c->base.egl_config, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 536 | output->window, NULL); |
| 537 | if (!output->egl_surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 538 | weston_log("failed to create window surface\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 539 | return -1; |
| 540 | } |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 541 | if (!eglMakeCurrent(c->base.egl_display, output->egl_surface, |
| 542 | output->egl_surface, c->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 543 | weston_log("failed to make surface current\n"); |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 544 | return -1; |
| 545 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 546 | |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 547 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 548 | output->finish_frame_timer = |
| 549 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 550 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 551 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 552 | output->base.repaint = x11_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 553 | output->base.destroy = x11_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 554 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 555 | output->base.set_backlight = NULL; |
| 556 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 557 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 558 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 559 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 560 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 561 | weston_log("x11 output %dx%d, window id %d\n", |
| 562 | width, height, output->window); |
| 563 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 567 | static struct x11_output * |
| 568 | x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window) |
| 569 | { |
| 570 | struct x11_output *output; |
| 571 | |
| 572 | wl_list_for_each(output, &c->base.output_list, base.link) { |
| 573 | if (output->window == window) |
| 574 | return output; |
| 575 | } |
| 576 | |
| 577 | return NULL; |
| 578 | } |
| 579 | |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 580 | static void |
| 581 | x11_compositor_deliver_button_event(struct x11_compositor *c, |
| 582 | xcb_generic_event_t *event, int state) |
| 583 | { |
| 584 | xcb_button_press_event_t *button_event = |
| 585 | (xcb_button_press_event_t *) event; |
Daniel Stone | 5d66371 | 2012-05-04 11:21:55 +0100 | [diff] [blame] | 586 | uint32_t button; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 587 | |
| 588 | switch (button_event->detail) { |
| 589 | default: |
| 590 | button = button_event->detail + BTN_LEFT - 1; |
| 591 | break; |
| 592 | case 2: |
| 593 | button = BTN_MIDDLE; |
| 594 | break; |
| 595 | case 3: |
| 596 | button = BTN_RIGHT; |
| 597 | break; |
| 598 | case 4: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 599 | if (state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 600 | notify_axis(&c->base.seat->seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 601 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 602 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 603 | wl_fixed_from_int(1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 604 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 605 | case 5: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 606 | if (state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 607 | notify_axis(&c->base.seat->seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 608 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 609 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 610 | wl_fixed_from_int(-1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 611 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 612 | case 6: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 613 | if (state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 614 | notify_axis(&c->base.seat->seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 615 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 616 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 617 | wl_fixed_from_int(1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 618 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 619 | case 7: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 620 | if (state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 621 | notify_axis(&c->base.seat->seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 622 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 623 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 624 | wl_fixed_from_int(-1)); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 628 | notify_button(&c->base.seat->seat, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 629 | weston_compositor_get_time(), button, |
| 630 | state ? WL_POINTER_BUTTON_STATE_PRESSED : |
| 631 | WL_POINTER_BUTTON_STATE_RELEASED); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 634 | static int |
| 635 | x11_compositor_next_event(struct x11_compositor *c, |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 636 | xcb_generic_event_t **event, uint32_t mask) |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 637 | { |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 638 | if (mask & WL_EVENT_READABLE) { |
| 639 | *event = xcb_poll_for_event(c->conn); |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 640 | } else { |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 641 | #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 642 | *event = xcb_poll_for_queued_event(c->conn); |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 643 | #else |
| 644 | *event = xcb_poll_for_event(c->conn); |
| 645 | #endif |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | return *event != NULL; |
| 649 | } |
| 650 | |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 651 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 652 | x11_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 653 | { |
| 654 | struct x11_compositor *c = data; |
| 655 | struct x11_output *output; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 656 | xcb_generic_event_t *event, *prev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 657 | xcb_client_message_event_t *client_message; |
| 658 | xcb_motion_notify_event_t *motion_notify; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 659 | xcb_enter_notify_event_t *enter_notify; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 660 | xcb_key_press_event_t *key_press, *key_release; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 661 | xcb_keymap_notify_event_t *keymap_notify; |
| 662 | xcb_focus_in_event_t *focus_in; |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 663 | xcb_expose_event_t *expose; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 664 | xcb_atom_t atom; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 665 | uint32_t *k; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 666 | uint32_t i, set; |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 667 | wl_fixed_t x, y; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 668 | int count; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 669 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 670 | prev = NULL; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 671 | count = 0; |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 672 | while (x11_compositor_next_event(c, &event, mask)) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 673 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 674 | case XCB_KEY_RELEASE: |
| 675 | key_release = (xcb_key_press_event_t *) prev; |
| 676 | key_press = (xcb_key_press_event_t *) event; |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 677 | /* XXX use XkbSetDetectableAutoRepeat */ |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 678 | if ((event->response_type & ~0x80) == XCB_KEY_PRESS && |
Dima Ryazanov | c224748 | 2011-08-16 17:25:32 -0700 | [diff] [blame] | 679 | key_release->time == key_press->time && |
| 680 | key_release->detail == key_press->detail) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 681 | /* Don't deliver the held key release |
| 682 | * event or the new key press event. */ |
| 683 | free(event); |
| 684 | free(prev); |
| 685 | prev = NULL; |
| 686 | continue; |
| 687 | } else { |
| 688 | /* Deliver the held key release now |
| 689 | * and fall through and handle the new |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 690 | * event below. */ |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 691 | notify_key(&c->base.seat->seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 692 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 693 | key_release->detail - 8, |
| 694 | WL_KEYBOARD_KEY_STATE_RELEASED); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 695 | free(prev); |
| 696 | prev = NULL; |
| 697 | break; |
| 698 | } |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 699 | |
| 700 | case XCB_FOCUS_IN: |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 701 | /* assert event is keymap_notify */ |
| 702 | focus_in = (xcb_focus_in_event_t *) prev; |
| 703 | keymap_notify = (xcb_keymap_notify_event_t *) event; |
| 704 | c->keys.size = 0; |
| 705 | for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) { |
| 706 | set = keymap_notify->keys[i >> 3] & |
| 707 | (1 << (i & 7)); |
| 708 | if (set) { |
| 709 | k = wl_array_add(&c->keys, sizeof *k); |
| 710 | *k = i; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | output = x11_compositor_find_output(c, focus_in->event); |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 715 | /* Unfortunately the state only comes with the enter |
| 716 | * event, rather than with the focus event. I'm not |
| 717 | * sure of the exact semantics around it and whether |
| 718 | * we can ensure that we get both? */ |
| 719 | notify_keyboard_focus_in(&c->base.seat->seat, &c->keys, |
| 720 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 721 | |
| 722 | free(prev); |
| 723 | prev = NULL; |
| 724 | break; |
| 725 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 726 | default: |
| 727 | /* No previous event held */ |
| 728 | break; |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 729 | } |
| 730 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 731 | switch (event->response_type & ~0x80) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 732 | case XCB_KEY_PRESS: |
| 733 | key_press = (xcb_key_press_event_t *) event; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 734 | notify_key(&c->base.seat->seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 735 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 736 | key_press->detail - 8, |
| 737 | WL_KEYBOARD_KEY_STATE_PRESSED); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 738 | break; |
| 739 | case XCB_KEY_RELEASE: |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 740 | prev = event; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 741 | break; |
| 742 | case XCB_BUTTON_PRESS: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 743 | x11_compositor_deliver_button_event(c, event, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 744 | break; |
| 745 | case XCB_BUTTON_RELEASE: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 746 | x11_compositor_deliver_button_event(c, event, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 747 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 748 | case XCB_MOTION_NOTIFY: |
| 749 | motion_notify = (xcb_motion_notify_event_t *) event; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 750 | output = x11_compositor_find_output(c, motion_notify->event); |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 751 | x = wl_fixed_from_int(output->base.x + motion_notify->event_x); |
| 752 | y = wl_fixed_from_int(output->base.y + motion_notify->event_y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 753 | notify_motion(&c->base.seat->seat, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 754 | weston_compositor_get_time(), x, y); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 755 | break; |
| 756 | |
| 757 | case XCB_EXPOSE: |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 758 | expose = (xcb_expose_event_t *) event; |
| 759 | output = x11_compositor_find_output(c, expose->window); |
| 760 | weston_output_schedule_repaint(&output->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 761 | break; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 762 | |
| 763 | case XCB_ENTER_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 764 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 765 | if (enter_notify->state >= Button1Mask) |
| 766 | break; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 767 | output = x11_compositor_find_output(c, enter_notify->event); |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 768 | x = wl_fixed_from_int(output->base.x + enter_notify->event_x); |
| 769 | y = wl_fixed_from_int(output->base.y + enter_notify->event_y); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 770 | /* XXX notify_modifiers() */ |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 771 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 772 | notify_pointer_focus(&c->base.seat->seat, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 773 | &output->base, x, y); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | case XCB_LEAVE_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 777 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 778 | if (enter_notify->state >= Button1Mask) |
| 779 | break; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 780 | output = x11_compositor_find_output(c, enter_notify->event); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 781 | notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 782 | break; |
| 783 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 784 | case XCB_CLIENT_MESSAGE: |
| 785 | client_message = (xcb_client_message_event_t *) event; |
| 786 | atom = client_message->data.data32[0]; |
| 787 | if (atom == c->atom.wm_delete_window) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 788 | wl_display_terminate(c->base.wl_display); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 789 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 790 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 791 | case XCB_FOCUS_IN: |
| 792 | focus_in = (xcb_focus_in_event_t *) event; |
| 793 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 794 | break; |
| 795 | |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 796 | prev = event; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case XCB_FOCUS_OUT: |
| 800 | focus_in = (xcb_focus_in_event_t *) event; |
Kristian Høgsberg | 42e40ae | 2011-12-19 14:36:50 -0500 | [diff] [blame] | 801 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED || |
| 802 | focus_in->mode == XCB_NOTIFY_MODE_UNGRAB) |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 803 | break; |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 804 | notify_keyboard_focus_out(&c->base.seat->seat); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 805 | break; |
| 806 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 807 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 808 | break; |
| 809 | } |
| 810 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 811 | count++; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 812 | if (prev != event) |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 813 | free (event); |
| 814 | } |
| 815 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 816 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 817 | case XCB_KEY_RELEASE: |
| 818 | key_release = (xcb_key_press_event_t *) prev; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 819 | notify_key(&c->base.seat->seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 820 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 821 | key_release->detail - 8, |
| 822 | WL_KEYBOARD_KEY_STATE_RELEASED); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 823 | free(prev); |
| 824 | prev = NULL; |
| 825 | break; |
| 826 | default: |
| 827 | break; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 828 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 829 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 830 | return count; |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 831 | } |
| 832 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 833 | #define F(field) offsetof(struct x11_compositor, field) |
| 834 | |
| 835 | static void |
| 836 | x11_compositor_get_resources(struct x11_compositor *c) |
| 837 | { |
| 838 | static const struct { const char *name; int offset; } atoms[] = { |
| 839 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 840 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
| 841 | { "WM_SIZE_HINTS", F(atom.wm_size_hints) }, |
| 842 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 843 | { "WM_CLASS", F(atom.wm_class) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 844 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 845 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 846 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 847 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 848 | { "STRING", F(atom.string) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 849 | { "UTF8_STRING", F(atom.utf8_string) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 850 | { "CARDINAL", F(atom.cardinal) }, |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 851 | { "_XKB_RULES_NAMES", F(atom.xkb_names) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 852 | }; |
| 853 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 854 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 855 | xcb_intern_atom_reply_t *reply; |
| 856 | xcb_pixmap_t pixmap; |
| 857 | xcb_gc_t gc; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 858 | unsigned int i; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 859 | uint8_t data[] = { 0, 0, 0, 0 }; |
| 860 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 861 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 862 | cookies[i] = xcb_intern_atom (c->conn, 0, |
| 863 | strlen(atoms[i].name), |
| 864 | atoms[i].name); |
| 865 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 866 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 867 | reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL); |
| 868 | *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom; |
| 869 | free(reply); |
| 870 | } |
| 871 | |
| 872 | pixmap = xcb_generate_id(c->conn); |
| 873 | gc = xcb_generate_id(c->conn); |
| 874 | xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1); |
| 875 | xcb_create_gc(c->conn, gc, pixmap, 0, NULL); |
| 876 | xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP, |
| 877 | pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data); |
| 878 | c->null_cursor = xcb_generate_id(c->conn); |
| 879 | xcb_create_cursor (c->conn, c->null_cursor, |
| 880 | pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1); |
| 881 | xcb_free_gc(c->conn, gc); |
| 882 | xcb_free_pixmap(c->conn, pixmap); |
| 883 | } |
| 884 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 885 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 886 | x11_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 887 | { |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 888 | struct x11_compositor *compositor = (struct x11_compositor *)ec; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 889 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 890 | wl_event_source_remove(compositor->xcb_source); |
| 891 | x11_input_destroy(compositor); |
| 892 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 893 | weston_compositor_shutdown(ec); /* destroys outputs, too */ |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 894 | |
| 895 | x11_compositor_fini_egl(compositor); |
| 896 | |
| 897 | XCloseDisplay(compositor->dpy); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 898 | free(ec); |
| 899 | } |
| 900 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 901 | static struct weston_compositor * |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 902 | x11_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 903 | int width, int height, int count, int fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 904 | int no_input, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 905 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 906 | { |
| 907 | struct x11_compositor *c; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 908 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 909 | int i, x; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 910 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 911 | weston_log("initializing x11 backend\n"); |
| 912 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 913 | c = malloc(sizeof *c); |
| 914 | if (c == NULL) |
| 915 | return NULL; |
| 916 | |
| 917 | memset(c, 0, sizeof *c); |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 918 | |
| 919 | c->dpy = XOpenDisplay(NULL); |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 920 | if (c->dpy == NULL) |
| 921 | return NULL; |
| 922 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 923 | c->conn = XGetXCBConnection(c->dpy); |
Benjamin Franzke | e997c5f | 2011-03-25 14:06:37 +0100 | [diff] [blame] | 924 | XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue); |
| 925 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 926 | if (xcb_connection_has_error(c->conn)) |
| 927 | return NULL; |
| 928 | |
| 929 | s = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 930 | c->screen = s.data; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 931 | wl_array_init(&c->keys); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 932 | |
| 933 | x11_compositor_get_resources(c); |
| 934 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 935 | c->base.wl_display = display; |
Tiago Vignatti | 997ce64 | 2010-11-10 02:42:35 +0200 | [diff] [blame] | 936 | if (x11_compositor_init_egl(c) < 0) |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 937 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 938 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 939 | c->base.destroy = x11_destroy; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 940 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 941 | /* Can't init base class until we have a current egl context */ |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 942 | if (weston_compositor_init(&c->base, display, argc, argv, |
| 943 | config_file) < 0) |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 944 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 945 | |
Daniel Stone | 22815f9 | 2012-06-22 13:21:34 +0100 | [diff] [blame] | 946 | if (x11_input_create(c, no_input) < 0) |
| 947 | return NULL; |
| 948 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 949 | for (i = 0, x = 0; i < count; i++) { |
| 950 | if (x11_compositor_create_output(c, x, 0, width, height, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 951 | fullscreen, no_input) < 0) |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 952 | return NULL; |
| 953 | x += width; |
| 954 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 955 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 956 | c->xcb_source = |
Kristian Høgsberg | 22ba60e | 2012-03-12 01:18:24 -0400 | [diff] [blame] | 957 | wl_event_loop_add_fd(c->base.input_loop, |
| 958 | xcb_get_file_descriptor(c->conn), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 959 | WL_EVENT_READABLE, |
| 960 | x11_compositor_handle_event, c); |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 961 | wl_event_source_check(c->xcb_source); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 962 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 963 | return &c->base; |
| 964 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 965 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 966 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 967 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 968 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 969 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 970 | int width = 1024, height = 640, fullscreen = 0, count = 1; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 971 | int no_input = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 972 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 973 | const struct weston_option x11_options[] = { |
| 974 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 975 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 976 | { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen }, |
| 977 | { WESTON_OPTION_INTEGER, "output-count", 0, &count }, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 978 | { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 979 | }; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 980 | |
| 981 | parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 982 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 983 | return x11_compositor_create(display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 984 | width, height, count, fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 985 | no_input, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 986 | argc, argv, config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 987 | } |