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" |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 52 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 53 | static char *output_name; |
| 54 | static char *output_mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 55 | static char *output_transform; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 56 | static int option_width; |
| 57 | static int option_height; |
| 58 | static int option_count; |
| 59 | static struct wl_list configured_output_list; |
| 60 | |
| 61 | struct x11_configured_output { |
| 62 | char *name; |
| 63 | int width, height; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 64 | uint32_t transform; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 65 | struct wl_list link; |
| 66 | }; |
| 67 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 68 | struct x11_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 69 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 70 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 71 | EGLSurface dummy_pbuffer; |
| 72 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 73 | Display *dpy; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 74 | xcb_connection_t *conn; |
| 75 | xcb_screen_t *screen; |
| 76 | xcb_cursor_t null_cursor; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 77 | struct wl_array keys; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 78 | struct wl_event_source *xcb_source; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 79 | struct xkb_keymap *xkb_keymap; |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 80 | unsigned int has_xkb; |
| 81 | uint8_t xkb_event_base; |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 82 | |
| 83 | /* We could map multi-pointer X to multiple wayland seats, but |
| 84 | * for now we only support core X input. */ |
| 85 | struct weston_seat core_seat; |
| 86 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 87 | struct { |
| 88 | xcb_atom_t wm_protocols; |
| 89 | xcb_atom_t wm_normal_hints; |
| 90 | xcb_atom_t wm_size_hints; |
| 91 | xcb_atom_t wm_delete_window; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 92 | xcb_atom_t wm_class; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 93 | xcb_atom_t net_wm_name; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 94 | xcb_atom_t net_wm_icon; |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 95 | xcb_atom_t net_wm_state; |
| 96 | xcb_atom_t net_wm_state_fullscreen; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 97 | xcb_atom_t string; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 98 | xcb_atom_t utf8_string; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 99 | xcb_atom_t cardinal; |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 100 | xcb_atom_t xkb_names; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 101 | } atom; |
| 102 | }; |
| 103 | |
| 104 | struct x11_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 105 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 106 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 107 | xcb_window_t window; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 108 | EGLSurface egl_surface; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 109 | struct weston_mode mode; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 110 | struct wl_event_source *finish_frame_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 111 | }; |
| 112 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 113 | static struct xkb_keymap * |
| 114 | x11_compositor_get_keymap(struct x11_compositor *c) |
| 115 | { |
| 116 | xcb_get_property_cookie_t cookie; |
| 117 | xcb_get_property_reply_t *reply; |
| 118 | xcb_generic_error_t *error; |
| 119 | struct xkb_rule_names names; |
| 120 | struct xkb_keymap *ret; |
| 121 | const char *value_all, *value_part; |
| 122 | int length_all, length_part; |
| 123 | |
| 124 | memset(&names, 0, sizeof(names)); |
| 125 | |
| 126 | cookie = xcb_get_property(c->conn, 0, c->screen->root, |
| 127 | c->atom.xkb_names, c->atom.string, 0, 1024); |
| 128 | reply = xcb_get_property_reply(c->conn, cookie, &error); |
| 129 | if (reply == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | value_all = xcb_get_property_value(reply); |
| 133 | length_all = xcb_get_property_value_length(reply); |
| 134 | value_part = value_all; |
| 135 | |
| 136 | #define copy_prop_value(to) \ |
| 137 | length_part = strlen(value_part); \ |
| 138 | if (value_part + length_part < (value_all + length_all) && \ |
| 139 | length_part > 0) \ |
| 140 | names.to = value_part; \ |
| 141 | value_part += length_part + 1; |
| 142 | |
| 143 | copy_prop_value(rules); |
| 144 | copy_prop_value(model); |
| 145 | copy_prop_value(layout); |
| 146 | copy_prop_value(variant); |
| 147 | copy_prop_value(options); |
| 148 | #undef copy_prop_value |
| 149 | |
| 150 | ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0); |
| 151 | |
| 152 | free(reply); |
| 153 | return ret; |
| 154 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 155 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 156 | static void |
| 157 | x11_compositor_setup_xkb(struct x11_compositor *c) |
| 158 | { |
| 159 | #ifndef HAVE_XCB_XKB |
| 160 | weston_log("XCB-XKB not available during build\n"); |
| 161 | c->has_xkb = 0; |
| 162 | c->xkb_event_base = 0; |
| 163 | return; |
| 164 | #else |
| 165 | const xcb_query_extension_reply_t *ext; |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 166 | xcb_generic_error_t *error; |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 167 | xcb_void_cookie_t select; |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 168 | xcb_xkb_per_client_flags_cookie_t pcf; |
| 169 | xcb_xkb_per_client_flags_reply_t *pcf_reply; |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 170 | |
| 171 | c->has_xkb = 0; |
| 172 | c->xkb_event_base = 0; |
| 173 | |
| 174 | ext = xcb_get_extension_data(c->conn, &xcb_xkb_id); |
| 175 | if (!ext) { |
| 176 | weston_log("XKB extension not available on host X11 server\n"); |
| 177 | return; |
| 178 | } |
| 179 | c->xkb_event_base = ext->first_event; |
| 180 | |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 181 | select = xcb_xkb_select_events(c->conn, |
| 182 | XCB_XKB_ID_USE_CORE_KBD, |
| 183 | XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 184 | 0, |
| 185 | XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 186 | 0, |
| 187 | 0, |
| 188 | NULL); |
| 189 | error = xcb_request_check(c->conn, select); |
| 190 | if (error) { |
| 191 | weston_log("error: failed to select for XKB state events\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 195 | pcf = xcb_xkb_per_client_flags(c->conn, |
| 196 | XCB_XKB_ID_USE_CORE_KBD, |
| 197 | XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, |
| 198 | XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, |
| 199 | 0, |
| 200 | 0, |
| 201 | 0); |
| 202 | pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error); |
| 203 | free(pcf_reply); |
| 204 | if (error) { |
| 205 | weston_log("failed to set XKB per-client flags, not using " |
| 206 | "detectable repeat\n"); |
| 207 | return; |
| 208 | } |
| 209 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 210 | c->has_xkb = 1; |
| 211 | #endif |
| 212 | } |
| 213 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 214 | static int |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 215 | x11_input_create(struct x11_compositor *c, int no_input) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 216 | { |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 217 | struct xkb_keymap *keymap; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 218 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 219 | weston_seat_init(&c->core_seat, &c->base); |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 220 | |
| 221 | if (no_input) |
| 222 | return 0; |
| 223 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 224 | weston_seat_init_pointer(&c->core_seat); |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 225 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 226 | x11_compositor_setup_xkb(c); |
| 227 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 228 | keymap = x11_compositor_get_keymap(c); |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 229 | weston_seat_init_keyboard(&c->core_seat, keymap); |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 230 | if (keymap) |
| 231 | xkb_map_unref(keymap); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 232 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 233 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 236 | static void |
| 237 | x11_input_destroy(struct x11_compositor *compositor) |
| 238 | { |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 239 | weston_seat_release(&compositor->core_seat); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 242 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 243 | x11_compositor_init_egl(struct x11_compositor *c) |
| 244 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 245 | EGLint major, minor; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 246 | EGLint n; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 247 | EGLint config_attribs[] = { |
| 248 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 249 | EGL_RED_SIZE, 1, |
| 250 | EGL_GREEN_SIZE, 1, |
| 251 | EGL_BLUE_SIZE, 1, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 252 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 253 | EGL_NONE |
| 254 | }; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 255 | static const EGLint context_attribs[] = { |
| 256 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 257 | EGL_NONE |
| 258 | }; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 259 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 260 | static const EGLint pbuffer_attribs[] = { |
| 261 | EGL_WIDTH, 10, |
| 262 | EGL_HEIGHT, 10, |
| 263 | EGL_NONE |
| 264 | }; |
| 265 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 266 | c->base.egl_display = eglGetDisplay(c->dpy); |
| 267 | if (c->base.egl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 268 | weston_log("failed to create display\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 272 | if (!eglInitialize(c->base.egl_display, &major, &minor)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 273 | weston_log("failed to initialize display\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 277 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 278 | weston_log("failed to bind EGL_OPENGL_ES_API\n"); |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 279 | return -1; |
| 280 | } |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 281 | if (!eglChooseConfig(c->base.egl_display, config_attribs, |
| 282 | &c->base.egl_config, 1, &n) || n == 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 283 | weston_log("failed to choose config: %d\n", n); |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 284 | return -1; |
| 285 | } |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 286 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 287 | c->base.egl_context = |
| 288 | eglCreateContext(c->base.egl_display, c->base.egl_config, |
| 289 | EGL_NO_CONTEXT, context_attribs); |
| 290 | if (c->base.egl_context == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 291 | weston_log("failed to create context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 295 | c->dummy_pbuffer = eglCreatePbufferSurface(c->base.egl_display, |
| 296 | c->base.egl_config, |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 297 | pbuffer_attribs); |
Kristian Høgsberg | c0bc844 | 2012-05-17 09:40:23 -0400 | [diff] [blame] | 298 | if (c->dummy_pbuffer == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 299 | weston_log("failed to create dummy pbuffer\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 300 | return -1; |
| 301 | } |
| 302 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 303 | if (!eglMakeCurrent(c->base.egl_display, c->dummy_pbuffer, |
| 304 | c->dummy_pbuffer, c->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 305 | weston_log("failed to make context current\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 312 | static void |
| 313 | x11_compositor_fini_egl(struct x11_compositor *compositor) |
| 314 | { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 315 | eglMakeCurrent(compositor->base.egl_display, |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 316 | EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 317 | EGL_NO_CONTEXT); |
| 318 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 319 | eglTerminate(compositor->base.egl_display); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 320 | eglReleaseThread(); |
| 321 | } |
| 322 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 323 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 324 | x11_output_repaint(struct weston_output *output_base, |
| 325 | pixman_region32_t *damage) |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 326 | { |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 327 | struct x11_output *output = (struct x11_output *)output_base; |
| 328 | struct x11_compositor *compositor = |
| 329 | (struct x11_compositor *)output->base.compositor; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 330 | struct weston_surface *surface; |
| 331 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 332 | if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface, |
| 333 | output->egl_surface, |
| 334 | compositor->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 335 | weston_log("failed to make current\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 340 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 341 | |
Kristian Høgsberg | e0f832b | 2012-06-20 00:13:18 -0400 | [diff] [blame] | 342 | wl_signal_emit(&output->base.frame_signal, output); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 343 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 344 | eglSwapBuffers(compositor->base.egl_display, output->egl_surface); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 345 | |
| 346 | wl_event_source_timer_update(output->finish_frame_timer, 10); |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 349 | static int |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 350 | finish_frame_handler(void *data) |
| 351 | { |
| 352 | struct x11_output *output = data; |
| 353 | uint32_t msec; |
| 354 | struct timeval tv; |
| 355 | |
| 356 | gettimeofday(&tv, NULL); |
| 357 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 358 | weston_output_finish_frame(&output->base, msec); |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 359 | |
| 360 | return 1; |
| 361 | } |
| 362 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 363 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 364 | x11_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 365 | { |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 366 | struct x11_output *output = (struct x11_output *)output_base; |
| 367 | struct x11_compositor *compositor = |
| 368 | (struct x11_compositor *)output->base.compositor; |
| 369 | |
| 370 | wl_list_remove(&output->base.link); |
| 371 | wl_event_source_remove(output->finish_frame_timer); |
| 372 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 373 | eglDestroySurface(compositor->base.egl_display, output->egl_surface); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 374 | |
| 375 | xcb_destroy_window(compositor->conn, output->window); |
| 376 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 377 | weston_output_destroy(&output->base); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 378 | |
| 379 | free(output); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 380 | } |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 381 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 382 | static void |
| 383 | x11_output_set_wm_protocols(struct x11_output *output) |
| 384 | { |
| 385 | xcb_atom_t list[1]; |
| 386 | struct x11_compositor *c = |
| 387 | (struct x11_compositor *) output->base.compositor; |
| 388 | |
| 389 | list[0] = c->atom.wm_delete_window; |
| 390 | xcb_change_property (c->conn, |
| 391 | XCB_PROP_MODE_REPLACE, |
| 392 | output->window, |
| 393 | c->atom.wm_protocols, |
| 394 | XCB_ATOM_ATOM, |
| 395 | 32, |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 396 | ARRAY_LENGTH(list), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 397 | list); |
| 398 | } |
| 399 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 400 | static void |
| 401 | x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state) |
| 402 | { |
| 403 | xcb_client_message_event_t event; |
| 404 | struct x11_compositor *c = |
| 405 | (struct x11_compositor *) output->base.compositor; |
| 406 | xcb_screen_iterator_t iter; |
| 407 | |
| 408 | #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */ |
| 409 | #define _NET_WM_STATE_ADD 1 /* add/set property */ |
| 410 | #define _NET_WM_STATE_TOGGLE 2 /* toggle property */ |
| 411 | |
| 412 | memset(&event, 0, sizeof event); |
| 413 | event.response_type = XCB_CLIENT_MESSAGE; |
| 414 | event.format = 32; |
| 415 | event.window = output->window; |
| 416 | event.type = c->atom.net_wm_state; |
| 417 | |
| 418 | event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 419 | event.data.data32[1] = state; |
| 420 | event.data.data32[2] = 0; |
| 421 | event.data.data32[3] = 0; |
| 422 | event.data.data32[4] = 0; |
| 423 | |
| 424 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 425 | xcb_send_event(c->conn, 0, iter.data->root, |
| 426 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 427 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 428 | (void *) &event); |
| 429 | } |
| 430 | |
| 431 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 432 | struct wm_normal_hints { |
| 433 | uint32_t flags; |
| 434 | uint32_t pad[4]; |
| 435 | int32_t min_width, min_height; |
| 436 | int32_t max_width, max_height; |
| 437 | int32_t width_inc, height_inc; |
| 438 | int32_t min_aspect_x, min_aspect_y; |
| 439 | int32_t max_aspect_x, max_aspect_y; |
| 440 | int32_t base_width, base_height; |
| 441 | int32_t win_gravity; |
| 442 | }; |
| 443 | |
| 444 | #define WM_NORMAL_HINTS_MIN_SIZE 16 |
| 445 | #define WM_NORMAL_HINTS_MAX_SIZE 32 |
| 446 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 447 | static void |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 448 | x11_output_set_icon(struct x11_compositor *c, |
| 449 | struct x11_output *output, const char *filename) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 450 | { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 451 | uint32_t *icon; |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 452 | int32_t width, height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 453 | pixman_image_t *image; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 454 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 455 | image = load_image(filename); |
| 456 | if (!image) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 457 | return; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 458 | width = pixman_image_get_width(image); |
| 459 | height = pixman_image_get_height(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 460 | icon = malloc(width * height * 4 + 8); |
| 461 | if (!icon) { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 462 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | icon[0] = width; |
| 467 | icon[1] = height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 468 | memcpy(icon + 2, pixman_image_get_data(image), width * height * 4); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 469 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 470 | c->atom.net_wm_icon, c->atom.cardinal, 32, |
| 471 | width * height + 2, icon); |
| 472 | free(icon); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 473 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 476 | static struct x11_output * |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 477 | x11_compositor_create_output(struct x11_compositor *c, int x, int y, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 478 | int width, int height, int fullscreen, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 479 | int no_input, char *configured_name, |
| 480 | uint32_t transform) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 481 | { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 482 | static const char name[] = "Weston Compositor"; |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 483 | static const char class[] = "weston-1\0Weston Compositor"; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 484 | char title[32]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 485 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 486 | xcb_screen_iterator_t iter; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 487 | struct wm_normal_hints normal_hints; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 488 | struct wl_event_loop *loop; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 489 | uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 490 | uint32_t values[2] = { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 491 | XCB_EVENT_MASK_EXPOSURE | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 492 | XCB_EVENT_MASK_STRUCTURE_NOTIFY, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 493 | 0 |
| 494 | }; |
| 495 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 496 | if (configured_name) |
| 497 | sprintf(title, "%s - %s", name, configured_name); |
| 498 | else |
| 499 | strcpy(title, name); |
| 500 | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 501 | if (!no_input) |
| 502 | values[0] |= |
| 503 | XCB_EVENT_MASK_KEY_PRESS | |
| 504 | XCB_EVENT_MASK_KEY_RELEASE | |
| 505 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 506 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 507 | XCB_EVENT_MASK_POINTER_MOTION | |
| 508 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 509 | XCB_EVENT_MASK_LEAVE_WINDOW | |
| 510 | XCB_EVENT_MASK_KEYMAP_STATE | |
| 511 | XCB_EVENT_MASK_FOCUS_CHANGE; |
| 512 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 513 | output = malloc(sizeof *output); |
| 514 | if (output == NULL) |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 515 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 516 | |
| 517 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 518 | |
| 519 | output->mode.flags = |
| 520 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 521 | output->mode.width = width; |
| 522 | output->mode.height = height; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 523 | output->mode.refresh = 60000; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 524 | wl_list_init(&output->base.mode_list); |
| 525 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 526 | |
| 527 | output->base.current = &output->mode; |
Kristian Høgsberg | 1248158 | 2012-07-02 21:24:57 -0400 | [diff] [blame] | 528 | output->base.make = "xwayland"; |
| 529 | output->base.model = "none"; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 530 | weston_output_init(&output->base, &c->base, |
| 531 | x, y, width, height, transform); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 532 | |
| 533 | values[1] = c->null_cursor; |
| 534 | output->window = xcb_generate_id(c->conn); |
| 535 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 536 | xcb_create_window(c->conn, |
| 537 | XCB_COPY_FROM_PARENT, |
| 538 | output->window, |
| 539 | iter.data->root, |
| 540 | 0, 0, |
| 541 | width, height, |
| 542 | 0, |
| 543 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 544 | iter.data->root_visual, |
| 545 | mask, values); |
| 546 | |
| 547 | /* Don't resize me. */ |
| 548 | memset(&normal_hints, 0, sizeof normal_hints); |
| 549 | normal_hints.flags = |
| 550 | WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE; |
| 551 | normal_hints.min_width = width; |
| 552 | normal_hints.min_height = height; |
| 553 | normal_hints.max_width = width; |
| 554 | normal_hints.max_height = height; |
| 555 | xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 556 | c->atom.wm_normal_hints, |
| 557 | c->atom.wm_size_hints, 32, |
| 558 | sizeof normal_hints / 4, |
| 559 | (uint8_t *) &normal_hints); |
| 560 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 561 | /* Set window name. Don't bother with non-EWMH WMs. */ |
| 562 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 563 | c->atom.net_wm_name, c->atom.utf8_string, 8, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 564 | strlen(title), title); |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 565 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 566 | c->atom.wm_class, c->atom.string, 8, |
| 567 | sizeof class, class); |
| 568 | |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 569 | x11_output_set_icon(c, output, DATADIR "/weston/wayland.png"); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 570 | |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 571 | xcb_map_window(c->conn, output->window); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 572 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 573 | x11_output_set_wm_protocols(output); |
| 574 | |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 575 | if (fullscreen) |
| 576 | x11_output_change_state(output, 1, |
| 577 | c->atom.net_wm_state_fullscreen); |
| 578 | |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 579 | output->egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 580 | eglCreateWindowSurface(c->base.egl_display, c->base.egl_config, |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 581 | output->window, NULL); |
| 582 | if (!output->egl_surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 583 | weston_log("failed to create window surface\n"); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 584 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 585 | } |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 586 | if (!eglMakeCurrent(c->base.egl_display, output->egl_surface, |
| 587 | output->egl_surface, c->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 588 | weston_log("failed to make surface current\n"); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 589 | return NULL; |
Benjamin Franzke | 84290d0 | 2011-03-02 10:07:59 +0100 | [diff] [blame] | 590 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 591 | |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 592 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 593 | output->finish_frame_timer = |
| 594 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 595 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 596 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 597 | output->base.repaint = x11_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 598 | output->base.destroy = x11_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 599 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 600 | output->base.set_backlight = NULL; |
| 601 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 602 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 603 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 604 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 605 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 606 | weston_log("x11 output %dx%d, window id %d\n", |
| 607 | width, height, output->window); |
| 608 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 609 | return output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 610 | } |
| 611 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 612 | static struct x11_output * |
| 613 | x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window) |
| 614 | { |
| 615 | struct x11_output *output; |
| 616 | |
| 617 | wl_list_for_each(output, &c->base.output_list, base.link) { |
| 618 | if (output->window == window) |
| 619 | return output; |
| 620 | } |
| 621 | |
| 622 | return NULL; |
| 623 | } |
| 624 | |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 625 | static uint32_t |
| 626 | get_xkb_mod_mask(struct x11_compositor *c, uint32_t in) |
| 627 | { |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 628 | struct weston_xkb_info *info = &c->core_seat.xkb_info; |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 629 | uint32_t ret = 0; |
| 630 | |
| 631 | if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID) |
| 632 | ret |= (1 << info->shift_mod); |
| 633 | if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID) |
| 634 | ret |= (1 << info->caps_mod); |
| 635 | if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID) |
| 636 | ret |= (1 << info->ctrl_mod); |
| 637 | if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID) |
| 638 | ret |= (1 << info->alt_mod); |
| 639 | if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID) |
| 640 | ret |= (1 << info->mod2_mod); |
| 641 | if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID) |
| 642 | ret |= (1 << info->mod3_mod); |
| 643 | if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID) |
| 644 | ret |= (1 << info->super_mod); |
| 645 | if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID) |
| 646 | ret |= (1 << info->mod5_mod); |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 651 | #ifdef HAVE_XCB_XKB |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 652 | static void |
| 653 | update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state) |
| 654 | { |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 655 | xkb_state_update_mask(c->core_seat.xkb_state.state, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 656 | get_xkb_mod_mask(c, state->baseMods), |
| 657 | get_xkb_mod_mask(c, state->latchedMods), |
| 658 | get_xkb_mod_mask(c, state->lockedMods), |
| 659 | 0, |
| 660 | 0, |
| 661 | state->group); |
| 662 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 663 | notify_modifiers(&c->core_seat, |
| 664 | wl_display_next_serial(c->base.wl_display)); |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 665 | } |
| 666 | #endif |
| 667 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 668 | /** |
| 669 | * This is monumentally unpleasant. If we don't have XCB-XKB bindings, |
| 670 | * the best we can do (given that XCB also lacks XI2 support), is to take |
| 671 | * the state from the core key events. Unfortunately that only gives us |
| 672 | * the effective (i.e. union of depressed/latched/locked) state, and we |
| 673 | * need the granularity. |
| 674 | * |
| 675 | * So we still update the state with every key event we see, but also use |
| 676 | * the state field from X11 events as a mask so we don't get any stuck |
| 677 | * modifiers. |
| 678 | */ |
| 679 | static void |
| 680 | update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask) |
| 681 | { |
| 682 | uint32_t mask = get_xkb_mod_mask(c, x11_mask); |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 683 | struct wl_keyboard *keyboard = &c->core_seat.keyboard; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 684 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 685 | xkb_state_update_mask(c->core_seat.xkb_state.state, |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 686 | keyboard->modifiers.mods_depressed & mask, |
| 687 | keyboard->modifiers.mods_latched & mask, |
| 688 | keyboard->modifiers.mods_locked & mask, |
| 689 | 0, |
| 690 | 0, |
| 691 | (x11_mask >> 13) & 3); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 692 | notify_modifiers(&c->core_seat, |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 693 | wl_display_next_serial(c->base.wl_display)); |
| 694 | } |
| 695 | |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 696 | static void |
| 697 | x11_compositor_deliver_button_event(struct x11_compositor *c, |
| 698 | xcb_generic_event_t *event, int state) |
| 699 | { |
| 700 | xcb_button_press_event_t *button_event = |
| 701 | (xcb_button_press_event_t *) event; |
Daniel Stone | 5d66371 | 2012-05-04 11:21:55 +0100 | [diff] [blame] | 702 | uint32_t button; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 703 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 704 | if (!c->has_xkb) |
| 705 | update_xkb_state_from_core(c, button_event->state); |
| 706 | |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 707 | switch (button_event->detail) { |
| 708 | default: |
| 709 | button = button_event->detail + BTN_LEFT - 1; |
| 710 | break; |
| 711 | case 2: |
| 712 | button = BTN_MIDDLE; |
| 713 | break; |
| 714 | case 3: |
| 715 | button = BTN_RIGHT; |
| 716 | break; |
| 717 | case 4: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 718 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 719 | notify_axis(&c->core_seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 720 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 721 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 722 | wl_fixed_from_int(1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 723 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 724 | case 5: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 725 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 726 | notify_axis(&c->core_seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 727 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 728 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 729 | wl_fixed_from_int(-1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 730 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 731 | case 6: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 732 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 733 | notify_axis(&c->core_seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 734 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 735 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 736 | wl_fixed_from_int(1)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 737 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 738 | case 7: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 739 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 740 | notify_axis(&c->core_seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 741 | weston_compositor_get_time(), |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 742 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 743 | wl_fixed_from_int(-1)); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 747 | notify_button(&c->core_seat, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 748 | weston_compositor_get_time(), button, |
| 749 | state ? WL_POINTER_BUTTON_STATE_PRESSED : |
| 750 | WL_POINTER_BUTTON_STATE_RELEASED); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 751 | } |
| 752 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 753 | static void |
| 754 | x11_output_transform_coordinate(struct x11_output *x11_output, |
| 755 | wl_fixed_t *x, wl_fixed_t *y) |
| 756 | { |
| 757 | struct weston_output *output = &x11_output->base; |
| 758 | wl_fixed_t tx, ty; |
| 759 | wl_fixed_t width = wl_fixed_from_int(output->width - 1); |
| 760 | wl_fixed_t height = wl_fixed_from_int(output->height - 1); |
| 761 | |
| 762 | switch(output->transform) { |
| 763 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 764 | default: |
| 765 | tx = *x; |
| 766 | ty = *y; |
| 767 | break; |
| 768 | case WL_OUTPUT_TRANSFORM_90: |
| 769 | tx = *y; |
| 770 | ty = height - *x; |
| 771 | break; |
| 772 | case WL_OUTPUT_TRANSFORM_180: |
| 773 | tx = width - *x; |
| 774 | ty = height - *y; |
| 775 | break; |
| 776 | case WL_OUTPUT_TRANSFORM_270: |
| 777 | tx = width - *y; |
| 778 | ty = *x; |
| 779 | break; |
| 780 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 781 | tx = width - *x; |
| 782 | ty = *y; |
| 783 | break; |
| 784 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 785 | tx = width - *y; |
| 786 | ty = height - *x; |
| 787 | break; |
| 788 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 789 | tx = *x; |
| 790 | ty = height - *y; |
| 791 | break; |
| 792 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 793 | tx = *y; |
| 794 | ty = *x; |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | tx += wl_fixed_from_int(output->x); |
| 799 | ty += wl_fixed_from_int(output->y); |
| 800 | |
| 801 | *x = tx; |
| 802 | *y = ty; |
| 803 | } |
| 804 | |
| 805 | static void |
| 806 | x11_compositor_deliver_motion_event(struct x11_compositor *c, |
| 807 | xcb_generic_event_t *event) |
| 808 | { |
| 809 | struct x11_output *output; |
| 810 | wl_fixed_t x, y; |
| 811 | xcb_motion_notify_event_t *motion_notify = |
| 812 | (xcb_motion_notify_event_t *) event; |
| 813 | |
| 814 | if (!c->has_xkb) |
| 815 | update_xkb_state_from_core(c, motion_notify->state); |
| 816 | output = x11_compositor_find_output(c, motion_notify->event); |
| 817 | x = wl_fixed_from_int(motion_notify->event_x); |
| 818 | y = wl_fixed_from_int(motion_notify->event_y); |
| 819 | x11_output_transform_coordinate(output, &x, &y); |
| 820 | |
| 821 | notify_motion(&c->core_seat, weston_compositor_get_time(), x, y); |
| 822 | } |
| 823 | |
| 824 | static void |
| 825 | x11_compositor_deliver_enter_event(struct x11_compositor *c, |
| 826 | xcb_generic_event_t *event) |
| 827 | { |
| 828 | struct x11_output *output; |
| 829 | wl_fixed_t x, y; |
| 830 | |
| 831 | xcb_enter_notify_event_t *enter_notify = |
| 832 | (xcb_enter_notify_event_t *) event; |
| 833 | if (enter_notify->state >= Button1Mask) |
| 834 | return; |
| 835 | if (!c->has_xkb) |
| 836 | update_xkb_state_from_core(c, enter_notify->state); |
| 837 | output = x11_compositor_find_output(c, enter_notify->event); |
| 838 | x = wl_fixed_from_int(enter_notify->event_x); |
| 839 | y = wl_fixed_from_int(enter_notify->event_y); |
| 840 | x11_output_transform_coordinate(output, &x, &y); |
| 841 | |
| 842 | notify_pointer_focus(&c->core_seat, &output->base, x, y); |
| 843 | } |
| 844 | |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 845 | static int |
| 846 | x11_compositor_next_event(struct x11_compositor *c, |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 847 | xcb_generic_event_t **event, uint32_t mask) |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 848 | { |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 849 | if (mask & WL_EVENT_READABLE) { |
| 850 | *event = xcb_poll_for_event(c->conn); |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 851 | } else { |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 852 | #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 853 | *event = xcb_poll_for_queued_event(c->conn); |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 854 | #else |
| 855 | *event = xcb_poll_for_event(c->conn); |
| 856 | #endif |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | return *event != NULL; |
| 860 | } |
| 861 | |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 862 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 863 | x11_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 864 | { |
| 865 | struct x11_compositor *c = data; |
| 866 | struct x11_output *output; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 867 | xcb_generic_event_t *event, *prev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 868 | xcb_client_message_event_t *client_message; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 869 | xcb_enter_notify_event_t *enter_notify; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 870 | xcb_key_press_event_t *key_press, *key_release; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 871 | xcb_keymap_notify_event_t *keymap_notify; |
| 872 | xcb_focus_in_event_t *focus_in; |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 873 | xcb_expose_event_t *expose; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 874 | xcb_atom_t atom; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 875 | uint32_t *k; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 876 | uint32_t i, set; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 877 | int count; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 878 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 879 | prev = NULL; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 880 | count = 0; |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 881 | while (x11_compositor_next_event(c, &event, mask)) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 882 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 883 | case XCB_KEY_RELEASE: |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 884 | /* Suppress key repeat events; this is only used if we |
| 885 | * don't have XCB XKB support. */ |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 886 | key_release = (xcb_key_press_event_t *) prev; |
| 887 | key_press = (xcb_key_press_event_t *) event; |
| 888 | if ((event->response_type & ~0x80) == XCB_KEY_PRESS && |
Dima Ryazanov | c224748 | 2011-08-16 17:25:32 -0700 | [diff] [blame] | 889 | key_release->time == key_press->time && |
| 890 | key_release->detail == key_press->detail) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 891 | /* Don't deliver the held key release |
| 892 | * event or the new key press event. */ |
| 893 | free(event); |
| 894 | free(prev); |
| 895 | prev = NULL; |
| 896 | continue; |
| 897 | } else { |
| 898 | /* Deliver the held key release now |
| 899 | * and fall through and handle the new |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 900 | * event below. */ |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 901 | update_xkb_state_from_core(c, key_release->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 902 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 903 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 904 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 905 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 906 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 907 | free(prev); |
| 908 | prev = NULL; |
| 909 | break; |
| 910 | } |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 911 | |
| 912 | case XCB_FOCUS_IN: |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 913 | /* assert event is keymap_notify */ |
| 914 | focus_in = (xcb_focus_in_event_t *) prev; |
| 915 | keymap_notify = (xcb_keymap_notify_event_t *) event; |
| 916 | c->keys.size = 0; |
| 917 | for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) { |
| 918 | set = keymap_notify->keys[i >> 3] & |
| 919 | (1 << (i & 7)); |
| 920 | if (set) { |
| 921 | k = wl_array_add(&c->keys, sizeof *k); |
| 922 | *k = i; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | output = x11_compositor_find_output(c, focus_in->event); |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 927 | /* Unfortunately the state only comes with the enter |
| 928 | * event, rather than with the focus event. I'm not |
| 929 | * sure of the exact semantics around it and whether |
| 930 | * we can ensure that we get both? */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 931 | notify_keyboard_focus_in(&c->core_seat, &c->keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 932 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 933 | |
| 934 | free(prev); |
| 935 | prev = NULL; |
| 936 | break; |
| 937 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 938 | default: |
| 939 | /* No previous event held */ |
| 940 | break; |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 943 | switch (event->response_type & ~0x80) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 944 | case XCB_KEY_PRESS: |
| 945 | key_press = (xcb_key_press_event_t *) event; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 946 | if (!c->has_xkb) |
| 947 | update_xkb_state_from_core(c, key_press->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 948 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 949 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 950 | key_press->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 951 | WL_KEYBOARD_KEY_STATE_PRESSED, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 952 | c->has_xkb ? STATE_UPDATE_NONE : |
| 953 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 954 | break; |
| 955 | case XCB_KEY_RELEASE: |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 956 | /* If we don't have XKB, we need to use the lame |
| 957 | * autorepeat detection above. */ |
| 958 | if (!c->has_xkb) { |
| 959 | prev = event; |
| 960 | break; |
| 961 | } |
| 962 | key_release = (xcb_key_press_event_t *) event; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 963 | notify_key(&c->core_seat, |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 964 | weston_compositor_get_time(), |
| 965 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 966 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 967 | STATE_UPDATE_NONE); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 968 | break; |
| 969 | case XCB_BUTTON_PRESS: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 970 | x11_compositor_deliver_button_event(c, event, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 971 | break; |
| 972 | case XCB_BUTTON_RELEASE: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 973 | x11_compositor_deliver_button_event(c, event, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 974 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 975 | case XCB_MOTION_NOTIFY: |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 976 | x11_compositor_deliver_motion_event(c, event); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 977 | break; |
| 978 | |
| 979 | case XCB_EXPOSE: |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 980 | expose = (xcb_expose_event_t *) event; |
| 981 | output = x11_compositor_find_output(c, expose->window); |
| 982 | weston_output_schedule_repaint(&output->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 983 | break; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 984 | |
| 985 | case XCB_ENTER_NOTIFY: |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 986 | x11_compositor_deliver_enter_event(c, event); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 987 | break; |
| 988 | |
| 989 | case XCB_LEAVE_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 990 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 991 | if (enter_notify->state >= Button1Mask) |
| 992 | break; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 993 | if (!c->has_xkb) |
| 994 | update_xkb_state_from_core(c, enter_notify->state); |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 995 | output = x11_compositor_find_output(c, enter_notify->event); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 996 | notify_pointer_focus(&c->core_seat, NULL, 0, 0); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 997 | break; |
| 998 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 999 | case XCB_CLIENT_MESSAGE: |
| 1000 | client_message = (xcb_client_message_event_t *) event; |
| 1001 | atom = client_message->data.data32[0]; |
| 1002 | if (atom == c->atom.wm_delete_window) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1003 | wl_display_terminate(c->base.wl_display); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1004 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1005 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1006 | case XCB_FOCUS_IN: |
| 1007 | focus_in = (xcb_focus_in_event_t *) event; |
| 1008 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 1009 | break; |
| 1010 | |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1011 | prev = event; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1012 | break; |
| 1013 | |
| 1014 | case XCB_FOCUS_OUT: |
| 1015 | focus_in = (xcb_focus_in_event_t *) event; |
Kristian Høgsberg | 42e40ae | 2011-12-19 14:36:50 -0500 | [diff] [blame] | 1016 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED || |
| 1017 | focus_in->mode == XCB_NOTIFY_MODE_UNGRAB) |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1018 | break; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1019 | notify_keyboard_focus_out(&c->core_seat); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1020 | break; |
| 1021 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1022 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 1026 | #ifdef HAVE_XCB_XKB |
| 1027 | if (c->has_xkb && |
| 1028 | (event->response_type & ~0x80) == c->xkb_event_base) { |
| 1029 | xcb_xkb_state_notify_event_t *state = |
| 1030 | (xcb_xkb_state_notify_event_t *) event; |
| 1031 | if (state->xkbType == XCB_XKB_STATE_NOTIFY) |
| 1032 | update_xkb_state(c, state); |
| 1033 | } |
| 1034 | #endif |
| 1035 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1036 | count++; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1037 | if (prev != event) |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 1038 | free (event); |
| 1039 | } |
| 1040 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1041 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 1042 | case XCB_KEY_RELEASE: |
| 1043 | key_release = (xcb_key_press_event_t *) prev; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1044 | update_xkb_state_from_core(c, key_release->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1045 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1046 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1047 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1048 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 1049 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1050 | free(prev); |
| 1051 | prev = NULL; |
| 1052 | break; |
| 1053 | default: |
| 1054 | break; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1055 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1056 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1057 | return count; |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1060 | #define F(field) offsetof(struct x11_compositor, field) |
| 1061 | |
| 1062 | static void |
| 1063 | x11_compositor_get_resources(struct x11_compositor *c) |
| 1064 | { |
| 1065 | static const struct { const char *name; int offset; } atoms[] = { |
| 1066 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 1067 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
| 1068 | { "WM_SIZE_HINTS", F(atom.wm_size_hints) }, |
| 1069 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 1070 | { "WM_CLASS", F(atom.wm_class) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1071 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 1072 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1073 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 1074 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 1075 | { "STRING", F(atom.string) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1076 | { "UTF8_STRING", F(atom.utf8_string) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 1077 | { "CARDINAL", F(atom.cardinal) }, |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 1078 | { "_XKB_RULES_NAMES", F(atom.xkb_names) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1079 | }; |
| 1080 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1081 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1082 | xcb_intern_atom_reply_t *reply; |
| 1083 | xcb_pixmap_t pixmap; |
| 1084 | xcb_gc_t gc; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1085 | unsigned int i; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1086 | uint8_t data[] = { 0, 0, 0, 0 }; |
| 1087 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1088 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1089 | cookies[i] = xcb_intern_atom (c->conn, 0, |
| 1090 | strlen(atoms[i].name), |
| 1091 | atoms[i].name); |
| 1092 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1093 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1094 | reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL); |
| 1095 | *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom; |
| 1096 | free(reply); |
| 1097 | } |
| 1098 | |
| 1099 | pixmap = xcb_generate_id(c->conn); |
| 1100 | gc = xcb_generate_id(c->conn); |
| 1101 | xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1); |
| 1102 | xcb_create_gc(c->conn, gc, pixmap, 0, NULL); |
| 1103 | xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP, |
| 1104 | pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data); |
| 1105 | c->null_cursor = xcb_generate_id(c->conn); |
| 1106 | xcb_create_cursor (c->conn, c->null_cursor, |
| 1107 | pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1); |
| 1108 | xcb_free_gc(c->conn, gc); |
| 1109 | xcb_free_pixmap(c->conn, pixmap); |
| 1110 | } |
| 1111 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1112 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1113 | x11_restore(struct weston_compositor *ec) |
| 1114 | { |
| 1115 | } |
| 1116 | |
| 1117 | static void |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1118 | x11_free_configured_output(struct x11_configured_output *output) |
| 1119 | { |
| 1120 | free(output->name); |
| 1121 | free(output); |
| 1122 | } |
| 1123 | |
| 1124 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1125 | x11_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1126 | { |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1127 | struct x11_compositor *compositor = (struct x11_compositor *)ec; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1128 | struct x11_configured_output *o, *n; |
| 1129 | |
| 1130 | wl_list_for_each_safe(o, n, &configured_output_list, link) |
| 1131 | x11_free_configured_output(o); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1132 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1133 | wl_event_source_remove(compositor->xcb_source); |
| 1134 | x11_input_destroy(compositor); |
| 1135 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 1136 | weston_compositor_shutdown(ec); /* destroys outputs, too */ |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1137 | |
| 1138 | x11_compositor_fini_egl(compositor); |
| 1139 | |
| 1140 | XCloseDisplay(compositor->dpy); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1141 | free(ec); |
| 1142 | } |
| 1143 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1144 | static struct weston_compositor * |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1145 | x11_compositor_create(struct wl_display *display, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1146 | int fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1147 | int no_input, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1148 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1149 | { |
| 1150 | struct x11_compositor *c; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1151 | struct x11_configured_output *o; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1152 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1153 | xcb_screen_iterator_t s; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1154 | int i, x = 0, output_count = 0; |
| 1155 | int width, height, count; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1156 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1157 | weston_log("initializing x11 backend\n"); |
| 1158 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1159 | c = malloc(sizeof *c); |
| 1160 | if (c == NULL) |
| 1161 | return NULL; |
| 1162 | |
| 1163 | memset(c, 0, sizeof *c); |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 1164 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1165 | if (weston_compositor_init(&c->base, display, argc, argv, |
| 1166 | config_file) < 0) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1167 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1168 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 1169 | c->dpy = XOpenDisplay(NULL); |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 1170 | if (c->dpy == NULL) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1171 | goto err_free; |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 1172 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 1173 | c->conn = XGetXCBConnection(c->dpy); |
Benjamin Franzke | e997c5f | 2011-03-25 14:06:37 +0100 | [diff] [blame] | 1174 | XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue); |
| 1175 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1176 | if (xcb_connection_has_error(c->conn)) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1177 | goto err_xdisplay; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1178 | |
| 1179 | s = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 1180 | c->screen = s.data; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1181 | wl_array_init(&c->keys); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1182 | |
| 1183 | x11_compositor_get_resources(c); |
| 1184 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1185 | c->base.wl_display = display; |
Tiago Vignatti | 997ce64 | 2010-11-10 02:42:35 +0200 | [diff] [blame] | 1186 | if (x11_compositor_init_egl(c) < 0) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1187 | goto err_xdisplay; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1188 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1189 | c->base.destroy = x11_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1190 | c->base.restore = x11_restore; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1191 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1192 | if (weston_compositor_init_gl(&c->base) < 0) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1193 | goto err_egl; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1194 | |
Daniel Stone | 22815f9 | 2012-06-22 13:21:34 +0100 | [diff] [blame] | 1195 | if (x11_input_create(c, no_input) < 0) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1196 | goto err_egl; |
Daniel Stone | 22815f9 | 2012-06-22 13:21:34 +0100 | [diff] [blame] | 1197 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1198 | width = option_width ? option_width : 1024; |
| 1199 | height = option_height ? option_height : 640; |
| 1200 | count = option_count ? option_count : 1; |
| 1201 | |
| 1202 | wl_list_for_each(o, &configured_output_list, link) { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1203 | output = x11_compositor_create_output(c, x, 0, |
| 1204 | option_width ? width : |
| 1205 | o->width, |
| 1206 | option_height ? height : |
| 1207 | o->height, |
| 1208 | fullscreen, no_input, |
| 1209 | o->name, o->transform); |
| 1210 | if (output == NULL) |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1211 | goto err_x11_input; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1212 | |
| 1213 | x = pixman_region32_extents(&output->base.region)->x2; |
| 1214 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1215 | output_count++; |
| 1216 | if (option_count && output_count >= option_count) |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | for (i = output_count; i < count; i++) { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1221 | output = x11_compositor_create_output(c, x, 0, width, height, |
| 1222 | fullscreen, no_input, NULL, |
| 1223 | WL_OUTPUT_TRANSFORM_NORMAL); |
| 1224 | if (output == NULL) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1225 | goto err_x11_input; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1226 | x = pixman_region32_extents(&output->base.region)->x2; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 1227 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1228 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1229 | c->xcb_source = |
Kristian Høgsberg | 22ba60e | 2012-03-12 01:18:24 -0400 | [diff] [blame] | 1230 | wl_event_loop_add_fd(c->base.input_loop, |
| 1231 | xcb_get_file_descriptor(c->conn), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1232 | WL_EVENT_READABLE, |
| 1233 | x11_compositor_handle_event, c); |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1234 | wl_event_source_check(c->xcb_source); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1235 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1236 | return &c->base; |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1237 | |
| 1238 | err_x11_input: |
| 1239 | x11_input_destroy(c); |
| 1240 | err_egl: |
| 1241 | x11_compositor_fini_egl(c); |
| 1242 | err_xdisplay: |
| 1243 | XCloseDisplay(c->dpy); |
| 1244 | err_free: |
| 1245 | free(c); |
| 1246 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1247 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1248 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1249 | static void |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1250 | x11_output_set_transform(struct x11_configured_output *output) |
| 1251 | { |
| 1252 | if (!output_transform) { |
| 1253 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | if (!strcmp(output_transform, "normal")) |
| 1258 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1259 | else if (!strcmp(output_transform, "90")) |
| 1260 | output->transform = WL_OUTPUT_TRANSFORM_90; |
| 1261 | else if (!strcmp(output_transform, "180")) |
| 1262 | output->transform = WL_OUTPUT_TRANSFORM_180; |
| 1263 | else if (!strcmp(output_transform, "270")) |
| 1264 | output->transform = WL_OUTPUT_TRANSFORM_270; |
| 1265 | else if (!strcmp(output_transform, "flipped")) |
| 1266 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; |
| 1267 | else if (!strcmp(output_transform, "flipped-90")) |
| 1268 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; |
| 1269 | else if (!strcmp(output_transform, "flipped-180")) |
| 1270 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; |
| 1271 | else if (!strcmp(output_transform, "flipped-270")) |
| 1272 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; |
| 1273 | else { |
| 1274 | weston_log("Invalid transform \"%s\" for output %s\n", |
| 1275 | output_transform, output_name); |
| 1276 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | static void |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1281 | output_section_done(void *data) |
| 1282 | { |
| 1283 | struct x11_configured_output *output; |
| 1284 | |
| 1285 | output = malloc(sizeof *output); |
| 1286 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1287 | if (!output || !output_name || (output_name[0] != 'X') || |
| 1288 | (!output_mode && !output_transform)) { |
| 1289 | if (output_name) |
| 1290 | free(output_name); |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1291 | output_name = NULL; |
Scott Moreau | ca9b09d | 2012-08-06 01:27:06 -0600 | [diff] [blame] | 1292 | goto err_free; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | output->name = output_name; |
| 1296 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1297 | if (output_mode) { |
| 1298 | if (sscanf(output_mode, "%dx%d", &output->width, |
| 1299 | &output->height) != 2) { |
| 1300 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 1301 | output_mode, output_name); |
| 1302 | x11_free_configured_output(output); |
| 1303 | goto err_free; |
| 1304 | } |
| 1305 | } else { |
| 1306 | output->width = 1024; |
| 1307 | output->height = 640; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1308 | } |
| 1309 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1310 | x11_output_set_transform(output); |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1311 | |
Scott Moreau | ca9b09d | 2012-08-06 01:27:06 -0600 | [diff] [blame] | 1312 | wl_list_insert(configured_output_list.prev, &output->link); |
| 1313 | |
| 1314 | err_free: |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1315 | if (output_mode) |
| 1316 | free(output_mode); |
| 1317 | if (output_transform) |
| 1318 | free(output_transform); |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1319 | output_mode = NULL; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1320 | output_transform = NULL; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1321 | } |
| 1322 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1323 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1324 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 1325 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1326 | { |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1327 | int fullscreen = 0; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1328 | int no_input = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1329 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1330 | const struct weston_option x11_options[] = { |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1331 | { WESTON_OPTION_INTEGER, "width", 0, &option_width }, |
| 1332 | { WESTON_OPTION_INTEGER, "height", 0, &option_height }, |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1333 | { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen }, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1334 | { WESTON_OPTION_INTEGER, "output-count", 0, &option_count }, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1335 | { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1336 | }; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1337 | |
| 1338 | parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1339 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1340 | wl_list_init(&configured_output_list); |
| 1341 | |
| 1342 | const struct config_key x11_config_keys[] = { |
| 1343 | { "name", CONFIG_KEY_STRING, &output_name }, |
| 1344 | { "mode", CONFIG_KEY_STRING, &output_mode }, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame^] | 1345 | { "transform", CONFIG_KEY_STRING, &output_transform }, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1346 | }; |
| 1347 | |
| 1348 | const struct config_section config_section[] = { |
| 1349 | { "output", x11_config_keys, |
| 1350 | ARRAY_LENGTH(x11_config_keys), output_section_done }, |
| 1351 | }; |
| 1352 | |
| 1353 | parse_config_file(config_file, config_section, |
| 1354 | ARRAY_LENGTH(config_section), NULL); |
| 1355 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 1356 | return x11_compositor_create(display, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1357 | fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1358 | no_input, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1359 | argc, argv, config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1360 | } |