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 |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 4 | * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 5 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 6 | * Permission to use, copy, modify, distribute, and sell this software and |
| 7 | * its documentation for any purpose is hereby granted without fee, provided |
| 8 | * that the above copyright notice appear in all copies and that both that |
| 9 | * copyright notice and this permission notice appear in supporting |
| 10 | * documentation, and that the name of the copyright holders not be used in |
| 11 | * advertising or publicity pertaining to distribution of the software |
| 12 | * without specific, written prior permission. The copyright holders make |
| 13 | * no representations about the suitability of this software for any |
| 14 | * purpose. It is provided "as is" without express or implied warranty. |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 15 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 16 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 17 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 18 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 20 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 21 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 22 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 25 | #include "config.h" |
Chia-I Wu | 1b6c0ed | 2010-10-29 15:20:18 +0800 | [diff] [blame] | 26 | |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 27 | #include <assert.h> |
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> |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 35 | #include <sys/shm.h> |
Kristian Høgsberg | f9112b2 | 2010-06-14 12:53:43 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 37 | |
| 38 | #include <xcb/xcb.h> |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 39 | #include <xcb/shm.h> |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 40 | #ifdef HAVE_XCB_XKB |
| 41 | #include <xcb/xkb.h> |
| 42 | #endif |
| 43 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 44 | #include <X11/Xlib.h> |
| 45 | #include <X11/Xlib-xcb.h> |
| 46 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 47 | #include <xkbcommon/xkbcommon.h> |
| 48 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 49 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 50 | #include "gl-renderer.h" |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 51 | #include "pixman-renderer.h" |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 52 | #include "../shared/config-parser.h" |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 53 | #include "../shared/image-loader.h" |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 54 | |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 55 | #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10) |
| 56 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 57 | static int option_width; |
| 58 | static int option_height; |
| 59 | static int option_count; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 60 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 61 | struct x11_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 62 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 63 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 64 | Display *dpy; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 65 | xcb_connection_t *conn; |
| 66 | xcb_screen_t *screen; |
| 67 | xcb_cursor_t null_cursor; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 68 | struct wl_array keys; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 69 | struct wl_event_source *xcb_source; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 70 | struct xkb_keymap *xkb_keymap; |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 71 | unsigned int has_xkb; |
| 72 | uint8_t xkb_event_base; |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 73 | int use_pixman; |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 74 | |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 75 | int has_net_wm_state_fullscreen; |
| 76 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 77 | /* We could map multi-pointer X to multiple wayland seats, but |
| 78 | * for now we only support core X input. */ |
Kristian Høgsberg | 068b61c | 2013-02-25 17:04:47 -0500 | [diff] [blame] | 79 | struct weston_seat core_seat; |
Kristian Høgsberg | 5006596 | 2013-03-27 15:14:07 -0400 | [diff] [blame] | 80 | wl_fixed_t prev_x; |
| 81 | wl_fixed_t prev_y; |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 82 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 83 | struct { |
| 84 | xcb_atom_t wm_protocols; |
| 85 | xcb_atom_t wm_normal_hints; |
| 86 | xcb_atom_t wm_size_hints; |
| 87 | xcb_atom_t wm_delete_window; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 88 | xcb_atom_t wm_class; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 89 | xcb_atom_t net_wm_name; |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 90 | xcb_atom_t net_supporting_wm_check; |
| 91 | xcb_atom_t net_supported; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 92 | xcb_atom_t net_wm_icon; |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 93 | xcb_atom_t net_wm_state; |
| 94 | xcb_atom_t net_wm_state_fullscreen; |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 95 | xcb_atom_t string; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 96 | xcb_atom_t utf8_string; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 97 | xcb_atom_t cardinal; |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 98 | xcb_atom_t xkb_names; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 99 | } atom; |
| 100 | }; |
| 101 | |
| 102 | struct x11_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 103 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 104 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 105 | xcb_window_t window; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 106 | struct weston_mode mode; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 107 | struct wl_event_source *finish_frame_timer; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 108 | |
| 109 | xcb_gc_t gc; |
| 110 | xcb_shm_seg_t segment; |
| 111 | pixman_image_t *hw_surface; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 112 | int shm_id; |
| 113 | void *buf; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 114 | uint8_t depth; |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 115 | int32_t scale; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 118 | struct gl_renderer_interface *gl_renderer; |
| 119 | |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 120 | static struct xkb_keymap * |
| 121 | x11_compositor_get_keymap(struct x11_compositor *c) |
| 122 | { |
| 123 | xcb_get_property_cookie_t cookie; |
| 124 | xcb_get_property_reply_t *reply; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 125 | struct xkb_rule_names names; |
| 126 | struct xkb_keymap *ret; |
| 127 | const char *value_all, *value_part; |
| 128 | int length_all, length_part; |
| 129 | |
| 130 | memset(&names, 0, sizeof(names)); |
| 131 | |
| 132 | cookie = xcb_get_property(c->conn, 0, c->screen->root, |
| 133 | c->atom.xkb_names, c->atom.string, 0, 1024); |
Ran Benita | 7b5e3cd | 2012-10-31 20:14:56 +0200 | [diff] [blame] | 134 | reply = xcb_get_property_reply(c->conn, cookie, NULL); |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 135 | if (reply == NULL) |
| 136 | return NULL; |
| 137 | |
| 138 | value_all = xcb_get_property_value(reply); |
| 139 | length_all = xcb_get_property_value_length(reply); |
| 140 | value_part = value_all; |
| 141 | |
| 142 | #define copy_prop_value(to) \ |
| 143 | length_part = strlen(value_part); \ |
| 144 | if (value_part + length_part < (value_all + length_all) && \ |
| 145 | length_part > 0) \ |
| 146 | names.to = value_part; \ |
| 147 | value_part += length_part + 1; |
| 148 | |
| 149 | copy_prop_value(rules); |
| 150 | copy_prop_value(model); |
| 151 | copy_prop_value(layout); |
| 152 | copy_prop_value(variant); |
| 153 | copy_prop_value(options); |
| 154 | #undef copy_prop_value |
| 155 | |
| 156 | ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0); |
| 157 | |
| 158 | free(reply); |
| 159 | return ret; |
| 160 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 161 | |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 162 | static uint32_t |
| 163 | get_xkb_mod_mask(struct x11_compositor *c, uint32_t in) |
| 164 | { |
Andrew Wedgbury | a94dc8a | 2013-09-06 08:29:12 +0000 | [diff] [blame] | 165 | struct weston_xkb_info *info = c->core_seat.xkb_info; |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 166 | uint32_t ret = 0; |
| 167 | |
| 168 | if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID) |
| 169 | ret |= (1 << info->shift_mod); |
| 170 | if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID) |
| 171 | ret |= (1 << info->caps_mod); |
| 172 | if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID) |
| 173 | ret |= (1 << info->ctrl_mod); |
| 174 | if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID) |
| 175 | ret |= (1 << info->alt_mod); |
| 176 | if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID) |
| 177 | ret |= (1 << info->mod2_mod); |
| 178 | if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID) |
| 179 | ret |= (1 << info->mod3_mod); |
| 180 | if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID) |
| 181 | ret |= (1 << info->super_mod); |
| 182 | if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID) |
| 183 | ret |= (1 << info->mod5_mod); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 188 | static void |
| 189 | x11_compositor_setup_xkb(struct x11_compositor *c) |
| 190 | { |
| 191 | #ifndef HAVE_XCB_XKB |
| 192 | weston_log("XCB-XKB not available during build\n"); |
| 193 | c->has_xkb = 0; |
| 194 | c->xkb_event_base = 0; |
| 195 | return; |
| 196 | #else |
| 197 | const xcb_query_extension_reply_t *ext; |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 198 | xcb_generic_error_t *error; |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 199 | xcb_void_cookie_t select; |
Ran Benita | 6a39d87 | 2012-10-31 20:14:57 +0200 | [diff] [blame] | 200 | xcb_xkb_use_extension_cookie_t use_ext; |
| 201 | xcb_xkb_use_extension_reply_t *use_ext_reply; |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 202 | xcb_xkb_per_client_flags_cookie_t pcf; |
| 203 | xcb_xkb_per_client_flags_reply_t *pcf_reply; |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 204 | xcb_xkb_get_state_cookie_t state; |
| 205 | xcb_xkb_get_state_reply_t *state_reply; |
Rui Matos | c6c2f8e | 2013-10-10 19:44:20 +0200 | [diff] [blame] | 206 | uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE }; |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 207 | |
| 208 | c->has_xkb = 0; |
| 209 | c->xkb_event_base = 0; |
| 210 | |
| 211 | ext = xcb_get_extension_data(c->conn, &xcb_xkb_id); |
| 212 | if (!ext) { |
| 213 | weston_log("XKB extension not available on host X11 server\n"); |
| 214 | return; |
| 215 | } |
| 216 | c->xkb_event_base = ext->first_event; |
| 217 | |
Ran Benita | 424ae01 | 2012-10-31 00:13:08 +0200 | [diff] [blame] | 218 | select = xcb_xkb_select_events_checked(c->conn, |
| 219 | XCB_XKB_ID_USE_CORE_KBD, |
| 220 | XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 221 | 0, |
| 222 | XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 223 | 0, |
| 224 | 0, |
| 225 | NULL); |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 226 | error = xcb_request_check(c->conn, select); |
| 227 | if (error) { |
| 228 | weston_log("error: failed to select for XKB state events\n"); |
Ran Benita | 7b5e3cd | 2012-10-31 20:14:56 +0200 | [diff] [blame] | 229 | free(error); |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
Ran Benita | 6a39d87 | 2012-10-31 20:14:57 +0200 | [diff] [blame] | 233 | use_ext = xcb_xkb_use_extension(c->conn, |
| 234 | XCB_XKB_MAJOR_VERSION, |
| 235 | XCB_XKB_MINOR_VERSION); |
| 236 | use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL); |
| 237 | if (!use_ext_reply) { |
| 238 | weston_log("couldn't start using XKB extension\n"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (!use_ext_reply->supported) { |
| 243 | weston_log("XKB extension version on the server is too old " |
| 244 | "(want %d.%d, has %d.%d)\n", |
| 245 | XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION, |
| 246 | use_ext_reply->serverMajor, use_ext_reply->serverMinor); |
| 247 | free(use_ext_reply); |
| 248 | return; |
| 249 | } |
| 250 | free(use_ext_reply); |
| 251 | |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 252 | pcf = xcb_xkb_per_client_flags(c->conn, |
| 253 | XCB_XKB_ID_USE_CORE_KBD, |
| 254 | XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, |
| 255 | XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, |
| 256 | 0, |
| 257 | 0, |
| 258 | 0); |
Ran Benita | 7b5e3cd | 2012-10-31 20:14:56 +0200 | [diff] [blame] | 259 | pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL); |
Ran Benita | 7109cc8 | 2012-10-31 20:14:58 +0200 | [diff] [blame] | 260 | if (!pcf_reply || |
| 261 | !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) { |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 262 | weston_log("failed to set XKB per-client flags, not using " |
| 263 | "detectable repeat\n"); |
Ran Benita | 7109cc8 | 2012-10-31 20:14:58 +0200 | [diff] [blame] | 264 | free(pcf_reply); |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 265 | return; |
| 266 | } |
Ran Benita | 7b5e3cd | 2012-10-31 20:14:56 +0200 | [diff] [blame] | 267 | free(pcf_reply); |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 268 | |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 269 | state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD); |
Ran Benita | 7b5e3cd | 2012-10-31 20:14:56 +0200 | [diff] [blame] | 270 | state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL); |
| 271 | if (!state_reply) { |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 272 | weston_log("failed to get initial XKB state\n"); |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | xkb_state_update_mask(c->core_seat.xkb_state.state, |
| 277 | get_xkb_mod_mask(c, state_reply->baseMods), |
| 278 | get_xkb_mod_mask(c, state_reply->latchedMods), |
| 279 | get_xkb_mod_mask(c, state_reply->lockedMods), |
| 280 | 0, |
| 281 | 0, |
| 282 | state_reply->group); |
| 283 | |
| 284 | free(state_reply); |
| 285 | |
Rui Matos | c6c2f8e | 2013-10-10 19:44:20 +0200 | [diff] [blame] | 286 | xcb_change_window_attributes(c->conn, c->screen->root, |
| 287 | XCB_CW_EVENT_MASK, values); |
| 288 | |
Daniel Stone | 62b33b6 | 2012-06-22 13:21:35 +0100 | [diff] [blame] | 289 | c->has_xkb = 1; |
| 290 | #endif |
| 291 | } |
| 292 | |
Jonas Ådahl | f88571f | 2013-10-25 23:18:01 +0200 | [diff] [blame] | 293 | #ifdef HAVE_XCB_XKB |
Rui Matos | c6c2f8e | 2013-10-10 19:44:20 +0200 | [diff] [blame] | 294 | static void |
| 295 | update_xkb_keymap(struct x11_compositor *c) |
| 296 | { |
| 297 | struct xkb_keymap *keymap; |
| 298 | |
| 299 | keymap = x11_compositor_get_keymap(c); |
| 300 | if (!keymap) { |
| 301 | weston_log("failed to get XKB keymap\n"); |
| 302 | return; |
| 303 | } |
| 304 | weston_seat_update_keymap(&c->core_seat, keymap); |
| 305 | xkb_keymap_unref(keymap); |
| 306 | } |
Jonas Ådahl | f88571f | 2013-10-25 23:18:01 +0200 | [diff] [blame] | 307 | #endif |
Rui Matos | c6c2f8e | 2013-10-10 19:44:20 +0200 | [diff] [blame] | 308 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 309 | static int |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 310 | x11_input_create(struct x11_compositor *c, int no_input) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 311 | { |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 312 | struct xkb_keymap *keymap; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 313 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 314 | weston_seat_init(&c->core_seat, &c->base, "default"); |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 315 | |
| 316 | if (no_input) |
| 317 | return 0; |
| 318 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 319 | weston_seat_init_pointer(&c->core_seat); |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 320 | |
| 321 | keymap = x11_compositor_get_keymap(c); |
Kristian Høgsberg | 2f07ef6 | 2013-02-16 14:29:24 -0500 | [diff] [blame] | 322 | if (weston_seat_init_keyboard(&c->core_seat, keymap) < 0) |
| 323 | return -1; |
Daniel Stone | 576cd15 | 2012-06-01 12:14:02 +0100 | [diff] [blame] | 324 | if (keymap) |
| 325 | xkb_map_unref(keymap); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 326 | |
Kristian Høgsberg | aac8693 | 2012-10-29 14:15:40 -0400 | [diff] [blame] | 327 | x11_compositor_setup_xkb(c); |
| 328 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 329 | return 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 332 | static void |
| 333 | x11_input_destroy(struct x11_compositor *compositor) |
| 334 | { |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 335 | weston_seat_release(&compositor->core_seat); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 338 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 339 | x11_output_start_repaint_loop(struct weston_output *output) |
| 340 | { |
| 341 | uint32_t msec; |
| 342 | struct timeval tv; |
| 343 | |
| 344 | gettimeofday(&tv, NULL); |
| 345 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 346 | weston_output_finish_frame(output, msec); |
| 347 | } |
| 348 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 349 | static int |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 350 | x11_output_repaint_gl(struct weston_output *output_base, |
| 351 | pixman_region32_t *damage) |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 352 | { |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 353 | struct x11_output *output = (struct x11_output *)output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 354 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 355 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 356 | ec->renderer->repaint_output(output_base, damage); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 357 | |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 358 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 359 | &ec->primary_plane.damage, damage); |
| 360 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 361 | wl_event_source_timer_update(output->finish_frame_timer, 10); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 362 | return 0; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 365 | static void |
Alexander Larsson | 80f9163 | 2013-05-22 14:41:38 +0200 | [diff] [blame] | 366 | set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region) |
| 367 | { |
| 368 | struct x11_output *output = (struct x11_output *)output_base; |
| 369 | struct weston_compositor *ec = output->base.compositor; |
| 370 | struct x11_compositor *c = (struct x11_compositor *)ec; |
| 371 | pixman_box32_t *rects; |
| 372 | xcb_rectangle_t *output_rects; |
| 373 | pixman_box32_t rect, transformed_rect; |
| 374 | xcb_void_cookie_t cookie; |
| 375 | int width, height, nrects, i; |
| 376 | xcb_generic_error_t *err; |
| 377 | |
| 378 | rects = pixman_region32_rectangles(region, &nrects); |
| 379 | output_rects = calloc(nrects, sizeof(xcb_rectangle_t)); |
| 380 | |
| 381 | if (output_rects == NULL) |
| 382 | return; |
| 383 | |
| 384 | width = output_base->width; |
| 385 | height = output_base->height; |
| 386 | |
| 387 | for (i = 0; i < nrects; i++) { |
| 388 | rect = rects[i]; |
| 389 | rect.x1 -= output_base->x; |
| 390 | rect.y1 -= output_base->y; |
| 391 | rect.x2 -= output_base->x; |
| 392 | rect.y2 -= output_base->y; |
| 393 | |
| 394 | switch (output_base->transform) { |
| 395 | default: |
| 396 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 397 | transformed_rect.x1 = rect.x1; |
| 398 | transformed_rect.y1 = rect.y1; |
| 399 | transformed_rect.x2 = rect.x2; |
| 400 | transformed_rect.y2 = rect.y2; |
| 401 | break; |
| 402 | case WL_OUTPUT_TRANSFORM_90: |
| 403 | transformed_rect.x1 = height - rect.y2; |
| 404 | transformed_rect.y1 = rect.x1; |
| 405 | transformed_rect.x2 = height - rect.y1; |
| 406 | transformed_rect.y2 = rect.x2; |
| 407 | break; |
| 408 | case WL_OUTPUT_TRANSFORM_180: |
| 409 | transformed_rect.x1 = width - rect.x2; |
| 410 | transformed_rect.y1 = height - rect.y2; |
| 411 | transformed_rect.x2 = width - rect.x1; |
| 412 | transformed_rect.y2 = height - rect.y1; |
| 413 | break; |
| 414 | case WL_OUTPUT_TRANSFORM_270: |
| 415 | transformed_rect.x1 = rect.y1; |
| 416 | transformed_rect.y1 = width - rect.x2; |
| 417 | transformed_rect.x2 = rect.y2; |
| 418 | transformed_rect.y2 = width - rect.x1; |
| 419 | break; |
| 420 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 421 | transformed_rect.x1 = width - rect.x2; |
| 422 | transformed_rect.y1 = rect.y1; |
| 423 | transformed_rect.x2 = width - rect.x1; |
| 424 | transformed_rect.y2 = rect.y2; |
| 425 | break; |
| 426 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 427 | transformed_rect.x1 = height - rect.y2; |
| 428 | transformed_rect.y1 = width - rect.x2; |
| 429 | transformed_rect.x2 = height - rect.y1; |
| 430 | transformed_rect.y2 = width - rect.x1; |
| 431 | break; |
| 432 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 433 | transformed_rect.x1 = rect.x1; |
| 434 | transformed_rect.y1 = height - rect.y2; |
| 435 | transformed_rect.x2 = rect.x2; |
| 436 | transformed_rect.y2 = height - rect.y1; |
| 437 | break; |
| 438 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 439 | transformed_rect.x1 = rect.y1; |
| 440 | transformed_rect.y1 = rect.x1; |
| 441 | transformed_rect.x2 = rect.y2; |
| 442 | transformed_rect.y2 = rect.x2; |
| 443 | break; |
| 444 | } |
| 445 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 446 | transformed_rect.x1 *= output_base->current_scale; |
| 447 | transformed_rect.y1 *= output_base->current_scale; |
| 448 | transformed_rect.x2 *= output_base->current_scale; |
| 449 | transformed_rect.y2 *= output_base->current_scale; |
Alexander Larsson | 80f9163 | 2013-05-22 14:41:38 +0200 | [diff] [blame] | 450 | |
| 451 | output_rects[i].x = transformed_rect.x1; |
| 452 | output_rects[i].y = transformed_rect.y1; |
| 453 | output_rects[i].width = transformed_rect.x2 - transformed_rect.x1; |
| 454 | output_rects[i].height = transformed_rect.y2 - transformed_rect.y1; |
| 455 | } |
| 456 | |
| 457 | cookie = xcb_set_clip_rectangles_checked(c->conn, XCB_CLIP_ORDERING_UNSORTED, |
| 458 | output->gc, |
| 459 | 0, 0, nrects, |
| 460 | output_rects); |
| 461 | err = xcb_request_check(c->conn, cookie); |
| 462 | if (err != NULL) { |
| 463 | weston_log("Failed to set clip rects, err: %d\n", err->error_code); |
| 464 | free(err); |
| 465 | } |
| 466 | free(output_rects); |
| 467 | } |
| 468 | |
| 469 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 470 | static int |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 471 | x11_output_repaint_shm(struct weston_output *output_base, |
| 472 | pixman_region32_t *damage) |
| 473 | { |
| 474 | struct x11_output *output = (struct x11_output *)output_base; |
| 475 | struct weston_compositor *ec = output->base.compositor; |
| 476 | struct x11_compositor *c = (struct x11_compositor *)ec; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 477 | xcb_void_cookie_t cookie; |
| 478 | xcb_generic_error_t *err; |
| 479 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 480 | pixman_renderer_output_set_buffer(output_base, output->hw_surface); |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 481 | ec->renderer->repaint_output(output_base, damage); |
| 482 | |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 483 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 484 | &ec->primary_plane.damage, damage); |
Alexander Larsson | 80f9163 | 2013-05-22 14:41:38 +0200 | [diff] [blame] | 485 | set_clip_for_output(output_base, damage); |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 486 | cookie = xcb_shm_put_image_checked(c->conn, output->window, output->gc, |
| 487 | pixman_image_get_width(output->hw_surface), |
| 488 | pixman_image_get_height(output->hw_surface), |
| 489 | 0, 0, |
| 490 | pixman_image_get_width(output->hw_surface), |
| 491 | pixman_image_get_height(output->hw_surface), |
| 492 | 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP, |
| 493 | 0, output->segment, 0); |
| 494 | err = xcb_request_check(c->conn, cookie); |
| 495 | if (err != NULL) { |
| 496 | weston_log("Failed to put shm image, err: %d\n", err->error_code); |
| 497 | free(err); |
| 498 | } |
| 499 | |
| 500 | wl_event_source_timer_update(output->finish_frame_timer, 10); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 501 | return 0; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 502 | } |
| 503 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 504 | static int |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 505 | finish_frame_handler(void *data) |
| 506 | { |
| 507 | struct x11_output *output = data; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 508 | |
| 509 | x11_output_start_repaint_loop(&output->base); |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 510 | |
| 511 | return 1; |
| 512 | } |
| 513 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 514 | static void |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 515 | x11_output_deinit_shm(struct x11_compositor *c, struct x11_output *output) |
| 516 | { |
| 517 | xcb_void_cookie_t cookie; |
| 518 | xcb_generic_error_t *err; |
| 519 | xcb_free_gc(c->conn, output->gc); |
| 520 | |
| 521 | pixman_image_unref(output->hw_surface); |
| 522 | output->hw_surface = NULL; |
| 523 | cookie = xcb_shm_detach_checked(c->conn, output->segment); |
| 524 | err = xcb_request_check(c->conn, cookie); |
| 525 | if (err) { |
| 526 | weston_log("xcb_shm_detach failed, error %d\n", err->error_code); |
| 527 | free(err); |
| 528 | } |
| 529 | shmdt(output->buf); |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 533 | x11_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 534 | { |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 535 | struct x11_output *output = (struct x11_output *)output_base; |
| 536 | struct x11_compositor *compositor = |
| 537 | (struct x11_compositor *)output->base.compositor; |
| 538 | |
| 539 | wl_list_remove(&output->base.link); |
| 540 | wl_event_source_remove(output->finish_frame_timer); |
| 541 | |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 542 | if (compositor->use_pixman) { |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 543 | pixman_renderer_output_destroy(output_base); |
| 544 | x11_output_deinit_shm(compositor, output); |
| 545 | } else |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 546 | gl_renderer->output_destroy(output_base); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 547 | |
| 548 | xcb_destroy_window(compositor->conn, output->window); |
| 549 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 550 | weston_output_destroy(&output->base); |
Pekka Paalanen | 2de99e2 | 2012-01-03 11:51:03 +0200 | [diff] [blame] | 551 | |
| 552 | free(output); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 553 | } |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 554 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 555 | static void |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 556 | x11_output_set_wm_protocols(struct x11_compositor *c, |
| 557 | struct x11_output *output) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 558 | { |
| 559 | xcb_atom_t list[1]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 560 | |
| 561 | list[0] = c->atom.wm_delete_window; |
| 562 | xcb_change_property (c->conn, |
| 563 | XCB_PROP_MODE_REPLACE, |
| 564 | output->window, |
| 565 | c->atom.wm_protocols, |
| 566 | XCB_ATOM_ATOM, |
| 567 | 32, |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 568 | ARRAY_LENGTH(list), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 569 | list); |
| 570 | } |
| 571 | |
| 572 | struct wm_normal_hints { |
| 573 | uint32_t flags; |
| 574 | uint32_t pad[4]; |
| 575 | int32_t min_width, min_height; |
| 576 | int32_t max_width, max_height; |
| 577 | int32_t width_inc, height_inc; |
| 578 | int32_t min_aspect_x, min_aspect_y; |
| 579 | int32_t max_aspect_x, max_aspect_y; |
| 580 | int32_t base_width, base_height; |
| 581 | int32_t win_gravity; |
| 582 | }; |
| 583 | |
| 584 | #define WM_NORMAL_HINTS_MIN_SIZE 16 |
| 585 | #define WM_NORMAL_HINTS_MAX_SIZE 32 |
| 586 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 587 | static void |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 588 | x11_output_set_icon(struct x11_compositor *c, |
| 589 | struct x11_output *output, const char *filename) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 590 | { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 591 | uint32_t *icon; |
Kristian Høgsberg | b41d76c | 2011-04-23 15:03:15 -0400 | [diff] [blame] | 592 | int32_t width, height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 593 | pixman_image_t *image; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 594 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 595 | image = load_image(filename); |
| 596 | if (!image) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 597 | return; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 598 | width = pixman_image_get_width(image); |
| 599 | height = pixman_image_get_height(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 600 | icon = malloc(width * height * 4 + 8); |
| 601 | if (!icon) { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 602 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
| 606 | icon[0] = width; |
| 607 | icon[1] = height; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 608 | memcpy(icon + 2, pixman_image_get_data(image), width * height * 4); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 609 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 610 | c->atom.net_wm_icon, c->atom.cardinal, 32, |
| 611 | width * height + 2, icon); |
| 612 | free(icon); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 613 | pixman_image_unref(image); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 614 | } |
| 615 | |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 616 | static void |
| 617 | x11_output_wait_for_map(struct x11_compositor *c, struct x11_output *output) |
| 618 | { |
| 619 | xcb_map_notify_event_t *map_notify; |
| 620 | xcb_configure_notify_event_t *configure_notify; |
| 621 | xcb_generic_event_t *event; |
Kristian Høgsberg | 8e7adbf | 2013-02-14 21:14:11 -0500 | [diff] [blame] | 622 | int mapped = 0, configured = 0; |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 623 | uint8_t response_type; |
| 624 | |
| 625 | /* This isn't the nicest way to do this. Ideally, we could |
Kristian Høgsberg | 8e7adbf | 2013-02-14 21:14:11 -0500 | [diff] [blame] | 626 | * just go back to the main loop and once we get the configure |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 627 | * notify, we add the output to the compositor. While we do |
| 628 | * support output hotplug, we can't start up with no outputs. |
Kristian Høgsberg | 8e7adbf | 2013-02-14 21:14:11 -0500 | [diff] [blame] | 629 | * We could add the output and then resize once we get the |
| 630 | * configure notify, but we don't want to start up and |
| 631 | * immediately resize the output. |
| 632 | * |
| 633 | * Also, some window managers don't give us our final |
| 634 | * fullscreen size before map_notify, so if we don't get a |
| 635 | * configure_notify before map_notify, we just wait for the |
| 636 | * first one and hope that's our size. */ |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 637 | |
| 638 | xcb_flush(c->conn); |
| 639 | |
Kristian Høgsberg | 8e7adbf | 2013-02-14 21:14:11 -0500 | [diff] [blame] | 640 | while (!mapped || !configured) { |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 641 | event = xcb_wait_for_event(c->conn); |
| 642 | response_type = event->response_type & ~0x80; |
| 643 | |
| 644 | switch (response_type) { |
| 645 | case XCB_MAP_NOTIFY: |
| 646 | map_notify = (xcb_map_notify_event_t *) event; |
| 647 | if (map_notify->window == output->window) |
| 648 | mapped = 1; |
| 649 | break; |
| 650 | |
| 651 | case XCB_CONFIGURE_NOTIFY: |
| 652 | configure_notify = |
| 653 | (xcb_configure_notify_event_t *) event; |
| 654 | |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 655 | |
| 656 | if (configure_notify->width % output->scale != 0 || |
| 657 | configure_notify->height % output->scale != 0) |
| 658 | weston_log("Resolution is not a multiple of screen size, rounding\n"); |
| 659 | output->mode.width = configure_notify->width / output->scale; |
| 660 | output->mode.height = configure_notify->height / output->scale; |
Kristian Høgsberg | 8e7adbf | 2013-02-14 21:14:11 -0500 | [diff] [blame] | 661 | configured = 1; |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 667 | static xcb_visualtype_t * |
| 668 | find_visual_by_id(xcb_screen_t *screen, |
| 669 | xcb_visualid_t id) |
| 670 | { |
| 671 | xcb_depth_iterator_t i; |
| 672 | xcb_visualtype_iterator_t j; |
| 673 | for (i = xcb_screen_allowed_depths_iterator(screen); |
| 674 | i.rem; |
| 675 | xcb_depth_next(&i)) { |
| 676 | for (j = xcb_depth_visuals_iterator(i.data); |
| 677 | j.rem; |
| 678 | xcb_visualtype_next(&j)) { |
| 679 | if (j.data->visual_id == id) |
| 680 | return j.data; |
| 681 | } |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static uint8_t |
| 687 | get_depth_of_visual(xcb_screen_t *screen, |
| 688 | xcb_visualid_t id) |
| 689 | { |
| 690 | xcb_depth_iterator_t i; |
| 691 | xcb_visualtype_iterator_t j; |
| 692 | for (i = xcb_screen_allowed_depths_iterator(screen); |
| 693 | i.rem; |
| 694 | xcb_depth_next(&i)) { |
| 695 | for (j = xcb_depth_visuals_iterator(i.data); |
| 696 | j.rem; |
| 697 | xcb_visualtype_next(&j)) { |
| 698 | if (j.data->visual_id == id) |
| 699 | return i.data->depth; |
| 700 | } |
| 701 | } |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int |
| 706 | x11_output_init_shm(struct x11_compositor *c, struct x11_output *output, |
| 707 | int width, int height) |
| 708 | { |
| 709 | xcb_screen_iterator_t iter; |
| 710 | xcb_visualtype_t *visual_type; |
| 711 | xcb_format_iterator_t fmt; |
| 712 | xcb_void_cookie_t cookie; |
| 713 | xcb_generic_error_t *err; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 714 | const xcb_query_extension_reply_t *ext; |
| 715 | int bitsperpixel = 0; |
| 716 | pixman_format_code_t pixman_format; |
| 717 | |
| 718 | /* Check if SHM is available */ |
| 719 | ext = xcb_get_extension_data(c->conn, &xcb_shm_id); |
| 720 | if (ext == NULL || !ext->present) { |
| 721 | /* SHM is missing */ |
| 722 | weston_log("SHM extension is not available\n"); |
| 723 | errno = ENOENT; |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 728 | visual_type = find_visual_by_id(iter.data, iter.data->root_visual); |
| 729 | if (!visual_type) { |
| 730 | weston_log("Failed to lookup visual for root window\n"); |
| 731 | errno = ENOENT; |
| 732 | return -1; |
| 733 | } |
| 734 | weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n", |
| 735 | visual_type->bits_per_rgb_value, |
| 736 | visual_type->red_mask, |
| 737 | visual_type->green_mask, |
| 738 | visual_type->blue_mask); |
| 739 | output->depth = get_depth_of_visual(iter.data, iter.data->root_visual); |
| 740 | weston_log("Visual depth is %d\n", output->depth); |
| 741 | |
| 742 | for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(c->conn)); |
| 743 | fmt.rem; |
| 744 | xcb_format_next(&fmt)) { |
| 745 | if (fmt.data->depth == output->depth) { |
| 746 | bitsperpixel = fmt.data->bits_per_pixel; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | weston_log("Found format for depth %d, bpp: %d\n", |
| 751 | output->depth, bitsperpixel); |
| 752 | |
| 753 | if (bitsperpixel == 32 && |
| 754 | visual_type->red_mask == 0xff0000 && |
| 755 | visual_type->green_mask == 0x00ff00 && |
| 756 | visual_type->blue_mask == 0x0000ff) { |
| 757 | weston_log("Will use x8r8g8b8 format for SHM surfaces\n"); |
| 758 | pixman_format = PIXMAN_x8r8g8b8; |
| 759 | } else { |
| 760 | weston_log("Can't find appropriate format for SHM pixmap\n"); |
| 761 | errno = ENOTSUP; |
| 762 | return -1; |
| 763 | } |
| 764 | |
| 765 | |
| 766 | /* Create SHM segment and attach it */ |
| 767 | output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU); |
| 768 | if (output->shm_id == -1) { |
| 769 | weston_log("x11shm: failed to allocate SHM segment\n"); |
| 770 | return -1; |
| 771 | } |
| 772 | output->buf = shmat(output->shm_id, NULL, 0 /* read/write */); |
| 773 | if (-1 == (long)output->buf) { |
| 774 | weston_log("x11shm: failed to attach SHM segment\n"); |
| 775 | return -1; |
| 776 | } |
| 777 | output->segment = xcb_generate_id(c->conn); |
| 778 | cookie = xcb_shm_attach_checked(c->conn, output->segment, output->shm_id, 1); |
| 779 | err = xcb_request_check(c->conn, cookie); |
| 780 | if (err) { |
| 781 | weston_log("x11shm: xcb_shm_attach error %d\n", err->error_code); |
| 782 | free(err); |
| 783 | return -1; |
| 784 | } |
| 785 | |
| 786 | shmctl(output->shm_id, IPC_RMID, NULL); |
| 787 | |
| 788 | /* Now create pixman image */ |
| 789 | output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf, |
| 790 | width * (bitsperpixel / 8)); |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 791 | |
| 792 | output->gc = xcb_generate_id(c->conn); |
| 793 | xcb_create_gc(c->conn, output->gc, output->window, 0, NULL); |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 798 | static struct x11_output * |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 799 | x11_compositor_create_output(struct x11_compositor *c, int x, int y, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 800 | int width, int height, int fullscreen, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 801 | int no_input, char *configured_name, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 802 | uint32_t transform, int32_t scale) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 803 | { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 804 | static const char name[] = "Weston Compositor"; |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 805 | static const char class[] = "weston-1\0Weston Compositor"; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 806 | char title[32]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 807 | struct x11_output *output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 808 | xcb_screen_iterator_t iter; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 809 | struct wm_normal_hints normal_hints; |
Kristian Høgsberg | 06a670f | 2011-10-29 14:39:13 -0400 | [diff] [blame] | 810 | struct wl_event_loop *loop; |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 811 | int output_width, output_height; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 812 | int ret; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 813 | uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR; |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 814 | xcb_atom_t atom_list[1]; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 815 | uint32_t values[2] = { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 816 | XCB_EVENT_MASK_EXPOSURE | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 817 | XCB_EVENT_MASK_STRUCTURE_NOTIFY, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 818 | 0 |
| 819 | }; |
| 820 | |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 821 | output_width = width * scale; |
| 822 | output_height = height * scale; |
| 823 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 824 | if (configured_name) |
| 825 | sprintf(title, "%s - %s", name, configured_name); |
| 826 | else |
| 827 | strcpy(title, name); |
| 828 | |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 829 | if (!no_input) |
| 830 | values[0] |= |
| 831 | XCB_EVENT_MASK_KEY_PRESS | |
| 832 | XCB_EVENT_MASK_KEY_RELEASE | |
| 833 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 834 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 835 | XCB_EVENT_MASK_POINTER_MOTION | |
| 836 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 837 | XCB_EVENT_MASK_LEAVE_WINDOW | |
| 838 | XCB_EVENT_MASK_KEYMAP_STATE | |
| 839 | XCB_EVENT_MASK_FOCUS_CHANGE; |
| 840 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 841 | output = zalloc(sizeof *output); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 842 | if (output == NULL) |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 843 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 844 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 845 | output->mode.flags = |
| 846 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 847 | |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 848 | output->mode.width = output_width; |
| 849 | output->mode.height = output_height; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 850 | output->mode.refresh = 60000; |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 851 | output->scale = scale; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 852 | wl_list_init(&output->base.mode_list); |
| 853 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 854 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 855 | values[1] = c->null_cursor; |
| 856 | output->window = xcb_generate_id(c->conn); |
| 857 | iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 858 | xcb_create_window(c->conn, |
| 859 | XCB_COPY_FROM_PARENT, |
| 860 | output->window, |
| 861 | iter.data->root, |
| 862 | 0, 0, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 863 | output_width, output_height, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 864 | 0, |
| 865 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 866 | iter.data->root_visual, |
| 867 | mask, values); |
| 868 | |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 869 | if (fullscreen) { |
| 870 | atom_list[0] = c->atom.net_wm_state_fullscreen; |
| 871 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, |
| 872 | output->window, |
| 873 | c->atom.net_wm_state, |
| 874 | XCB_ATOM_ATOM, 32, |
| 875 | ARRAY_LENGTH(atom_list), atom_list); |
| 876 | } else { |
| 877 | /* Don't resize me. */ |
| 878 | memset(&normal_hints, 0, sizeof normal_hints); |
| 879 | normal_hints.flags = |
| 880 | WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE; |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 881 | normal_hints.min_width = output_width; |
| 882 | normal_hints.min_height = output_height; |
| 883 | normal_hints.max_width = output_width; |
| 884 | normal_hints.max_height = output_height; |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 885 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 886 | c->atom.wm_normal_hints, |
| 887 | c->atom.wm_size_hints, 32, |
| 888 | sizeof normal_hints / 4, |
| 889 | (uint8_t *) &normal_hints); |
| 890 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 891 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 892 | /* Set window name. Don't bother with non-EWMH WMs. */ |
| 893 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 894 | c->atom.net_wm_name, c->atom.utf8_string, 8, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 895 | strlen(title), title); |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 896 | xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window, |
| 897 | c->atom.wm_class, c->atom.string, 8, |
| 898 | sizeof class, class); |
| 899 | |
Kristian Høgsberg | 8600040 | 2012-01-03 23:24:14 -0500 | [diff] [blame] | 900 | x11_output_set_icon(c, output, DATADIR "/weston/wayland.png"); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 901 | |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 902 | x11_output_set_wm_protocols(c, output); |
| 903 | |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 904 | xcb_map_window(c->conn, output->window); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 905 | |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 906 | if (fullscreen) |
| 907 | x11_output_wait_for_map(c, output); |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 908 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 909 | output->base.start_repaint_loop = x11_output_start_repaint_loop; |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 910 | if (c->use_pixman) |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 911 | output->base.repaint = x11_output_repaint_shm; |
| 912 | else |
| 913 | output->base.repaint = x11_output_repaint_gl; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 914 | output->base.destroy = x11_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 915 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 916 | output->base.set_backlight = NULL; |
| 917 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 918 | output->base.switch_mode = NULL; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 919 | output->base.current_mode = &output->mode; |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 920 | output->base.make = "xwayland"; |
| 921 | output->base.model = "none"; |
| 922 | weston_output_init(&output->base, &c->base, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 923 | x, y, width, height, transform, scale); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 924 | |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 925 | if (c->use_pixman) { |
Kristian Høgsberg | b5a4f98 | 2013-10-23 23:38:50 -0700 | [diff] [blame] | 926 | if (x11_output_init_shm(c, output, |
| 927 | output->mode.width, |
| 928 | output->mode.height) < 0) |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 929 | return NULL; |
| 930 | if (pixman_renderer_output_create(&output->base) < 0) { |
| 931 | x11_output_deinit_shm(c, output); |
| 932 | return NULL; |
| 933 | } |
| 934 | } else { |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 935 | ret = gl_renderer->output_create(&output->base, |
| 936 | (EGLNativeWindowType) output->window); |
| 937 | if (ret < 0) |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 938 | return NULL; |
| 939 | } |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 940 | |
| 941 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 942 | output->finish_frame_timer = |
| 943 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 944 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 945 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 946 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 947 | weston_log("x11 output %dx%d, window id %d\n", |
| 948 | width, height, output->window); |
| 949 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 950 | return output; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 951 | } |
| 952 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 953 | static struct x11_output * |
| 954 | x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window) |
| 955 | { |
| 956 | struct x11_output *output; |
| 957 | |
| 958 | wl_list_for_each(output, &c->base.output_list, base.link) { |
| 959 | if (output->window == window) |
| 960 | return output; |
| 961 | } |
| 962 | |
Kristian Høgsberg | 87d3841 | 2013-07-25 16:42:30 -0700 | [diff] [blame] | 963 | assert(0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 966 | #ifdef HAVE_XCB_XKB |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 967 | static void |
| 968 | update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state) |
| 969 | { |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 970 | xkb_state_update_mask(c->core_seat.xkb_state.state, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 971 | get_xkb_mod_mask(c, state->baseMods), |
| 972 | get_xkb_mod_mask(c, state->latchedMods), |
| 973 | get_xkb_mod_mask(c, state->lockedMods), |
| 974 | 0, |
| 975 | 0, |
| 976 | state->group); |
| 977 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 978 | notify_modifiers(&c->core_seat, |
| 979 | wl_display_next_serial(c->base.wl_display)); |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 980 | } |
| 981 | #endif |
| 982 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 983 | /** |
| 984 | * This is monumentally unpleasant. If we don't have XCB-XKB bindings, |
| 985 | * the best we can do (given that XCB also lacks XI2 support), is to take |
| 986 | * the state from the core key events. Unfortunately that only gives us |
| 987 | * the effective (i.e. union of depressed/latched/locked) state, and we |
| 988 | * need the granularity. |
| 989 | * |
| 990 | * So we still update the state with every key event we see, but also use |
| 991 | * the state field from X11 events as a mask so we don't get any stuck |
| 992 | * modifiers. |
| 993 | */ |
| 994 | static void |
| 995 | update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask) |
| 996 | { |
| 997 | uint32_t mask = get_xkb_mod_mask(c, x11_mask); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 998 | struct weston_keyboard *keyboard = c->core_seat.keyboard; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 999 | |
Kristian Høgsberg | 4f92c53 | 2012-08-10 10:04:36 -0400 | [diff] [blame] | 1000 | xkb_state_update_mask(c->core_seat.xkb_state.state, |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1001 | keyboard->modifiers.mods_depressed & mask, |
| 1002 | keyboard->modifiers.mods_latched & mask, |
| 1003 | keyboard->modifiers.mods_locked & mask, |
| 1004 | 0, |
| 1005 | 0, |
| 1006 | (x11_mask >> 13) & 3); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1007 | notify_modifiers(&c->core_seat, |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1008 | wl_display_next_serial(c->base.wl_display)); |
| 1009 | } |
| 1010 | |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1011 | static void |
| 1012 | x11_compositor_deliver_button_event(struct x11_compositor *c, |
| 1013 | xcb_generic_event_t *event, int state) |
| 1014 | { |
| 1015 | xcb_button_press_event_t *button_event = |
| 1016 | (xcb_button_press_event_t *) event; |
Daniel Stone | 5d66371 | 2012-05-04 11:21:55 +0100 | [diff] [blame] | 1017 | uint32_t button; |
Kristian Høgsberg | 7330862 | 2012-10-30 11:04:52 -0400 | [diff] [blame] | 1018 | struct x11_output *output; |
| 1019 | |
| 1020 | output = x11_compositor_find_output(c, button_event->event); |
| 1021 | |
| 1022 | if (state) |
| 1023 | xcb_grab_pointer(c->conn, 0, output->window, |
| 1024 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 1025 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 1026 | XCB_EVENT_MASK_POINTER_MOTION | |
| 1027 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 1028 | XCB_EVENT_MASK_LEAVE_WINDOW, |
| 1029 | XCB_GRAB_MODE_ASYNC, |
| 1030 | XCB_GRAB_MODE_ASYNC, |
| 1031 | output->window, XCB_CURSOR_NONE, |
| 1032 | button_event->time); |
| 1033 | else |
| 1034 | xcb_ungrab_pointer(c->conn, button_event->time); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1035 | |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1036 | if (!c->has_xkb) |
| 1037 | update_xkb_state_from_core(c, button_event->state); |
| 1038 | |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1039 | switch (button_event->detail) { |
| 1040 | default: |
| 1041 | button = button_event->detail + BTN_LEFT - 1; |
| 1042 | break; |
| 1043 | case 2: |
| 1044 | button = BTN_MIDDLE; |
| 1045 | break; |
| 1046 | case 3: |
| 1047 | button = BTN_RIGHT; |
| 1048 | break; |
| 1049 | case 4: |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 1050 | /* Axis are measured in pixels, but the xcb events are discrete |
| 1051 | * steps. Therefore move the axis by some pixels every step. */ |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1052 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1053 | notify_axis(&c->core_seat, |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 1054 | weston_compositor_get_time(), |
| 1055 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 1056 | -DEFAULT_AXIS_STEP_DISTANCE); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1057 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1058 | case 5: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1059 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1060 | notify_axis(&c->core_seat, |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 1061 | weston_compositor_get_time(), |
| 1062 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 1063 | DEFAULT_AXIS_STEP_DISTANCE); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1064 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1065 | case 6: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1066 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1067 | notify_axis(&c->core_seat, |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 1068 | weston_compositor_get_time(), |
| 1069 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 1070 | -DEFAULT_AXIS_STEP_DISTANCE); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1071 | return; |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1072 | case 7: |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 1073 | if (state) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1074 | notify_axis(&c->core_seat, |
Jonas Ådahl | 62efe20 | 2012-09-27 18:40:41 +0200 | [diff] [blame] | 1075 | weston_compositor_get_time(), |
| 1076 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 1077 | DEFAULT_AXIS_STEP_DISTANCE); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1078 | return; |
| 1079 | } |
| 1080 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1081 | notify_button(&c->core_seat, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1082 | weston_compositor_get_time(), button, |
| 1083 | state ? WL_POINTER_BUTTON_STATE_PRESSED : |
| 1084 | WL_POINTER_BUTTON_STATE_RELEASED); |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1087 | static void |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1088 | x11_compositor_deliver_motion_event(struct x11_compositor *c, |
| 1089 | xcb_generic_event_t *event) |
| 1090 | { |
| 1091 | struct x11_output *output; |
| 1092 | wl_fixed_t x, y; |
| 1093 | xcb_motion_notify_event_t *motion_notify = |
| 1094 | (xcb_motion_notify_event_t *) event; |
| 1095 | |
| 1096 | if (!c->has_xkb) |
| 1097 | update_xkb_state_from_core(c, motion_notify->state); |
| 1098 | output = x11_compositor_find_output(c, motion_notify->event); |
Kristian Høgsberg | 98c774f | 2013-07-22 14:33:42 -0700 | [diff] [blame] | 1099 | weston_output_transform_coordinate(&output->base, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame^] | 1100 | wl_fixed_from_int(motion_notify->event_x), |
| 1101 | wl_fixed_from_int(motion_notify->event_y), |
| 1102 | &x, &y); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1103 | |
Kristian Høgsberg | 5006596 | 2013-03-27 15:14:07 -0400 | [diff] [blame] | 1104 | notify_motion(&c->core_seat, weston_compositor_get_time(), |
| 1105 | x - c->prev_x, y - c->prev_y); |
| 1106 | |
| 1107 | c->prev_x = x; |
| 1108 | c->prev_y = y; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | static void |
| 1112 | x11_compositor_deliver_enter_event(struct x11_compositor *c, |
| 1113 | xcb_generic_event_t *event) |
| 1114 | { |
| 1115 | struct x11_output *output; |
| 1116 | wl_fixed_t x, y; |
| 1117 | |
| 1118 | xcb_enter_notify_event_t *enter_notify = |
| 1119 | (xcb_enter_notify_event_t *) event; |
| 1120 | if (enter_notify->state >= Button1Mask) |
| 1121 | return; |
| 1122 | if (!c->has_xkb) |
| 1123 | update_xkb_state_from_core(c, enter_notify->state); |
| 1124 | output = x11_compositor_find_output(c, enter_notify->event); |
Kristian Høgsberg | 98c774f | 2013-07-22 14:33:42 -0700 | [diff] [blame] | 1125 | weston_output_transform_coordinate(&output->base, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame^] | 1126 | wl_fixed_from_int(enter_notify->event_x), |
| 1127 | wl_fixed_from_int(enter_notify->event_y), &x, &y); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1128 | |
| 1129 | notify_pointer_focus(&c->core_seat, &output->base, x, y); |
Kristian Høgsberg | 5006596 | 2013-03-27 15:14:07 -0400 | [diff] [blame] | 1130 | |
| 1131 | c->prev_x = x; |
| 1132 | c->prev_y = y; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1133 | } |
| 1134 | |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1135 | static int |
| 1136 | x11_compositor_next_event(struct x11_compositor *c, |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1137 | xcb_generic_event_t **event, uint32_t mask) |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1138 | { |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1139 | if (mask & WL_EVENT_READABLE) { |
| 1140 | *event = xcb_poll_for_event(c->conn); |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1141 | } else { |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 1142 | #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1143 | *event = xcb_poll_for_queued_event(c->conn); |
Kristian Høgsberg | 82ed042 | 2011-04-25 15:41:59 -0400 | [diff] [blame] | 1144 | #else |
| 1145 | *event = xcb_poll_for_event(c->conn); |
| 1146 | #endif |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | return *event != NULL; |
| 1150 | } |
| 1151 | |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1152 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1153 | x11_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 1154 | { |
| 1155 | struct x11_compositor *c = data; |
| 1156 | struct x11_output *output; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1157 | xcb_generic_event_t *event, *prev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1158 | xcb_client_message_event_t *client_message; |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1159 | xcb_enter_notify_event_t *enter_notify; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1160 | xcb_key_press_event_t *key_press, *key_release; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1161 | xcb_keymap_notify_event_t *keymap_notify; |
| 1162 | xcb_focus_in_event_t *focus_in; |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 1163 | xcb_expose_event_t *expose; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1164 | xcb_atom_t atom; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1165 | uint32_t *k; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1166 | uint32_t i, set; |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 1167 | uint8_t response_type; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1168 | int count; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1169 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1170 | prev = NULL; |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1171 | count = 0; |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1172 | while (x11_compositor_next_event(c, &event, mask)) { |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 1173 | response_type = event->response_type & ~0x80; |
| 1174 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1175 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 1176 | case XCB_KEY_RELEASE: |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 1177 | /* Suppress key repeat events; this is only used if we |
| 1178 | * don't have XCB XKB support. */ |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1179 | key_release = (xcb_key_press_event_t *) prev; |
| 1180 | key_press = (xcb_key_press_event_t *) event; |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 1181 | if (response_type == XCB_KEY_PRESS && |
Dima Ryazanov | c224748 | 2011-08-16 17:25:32 -0700 | [diff] [blame] | 1182 | key_release->time == key_press->time && |
| 1183 | key_release->detail == key_press->detail) { |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1184 | /* Don't deliver the held key release |
| 1185 | * event or the new key press event. */ |
| 1186 | free(event); |
| 1187 | free(prev); |
| 1188 | prev = NULL; |
| 1189 | continue; |
| 1190 | } else { |
| 1191 | /* Deliver the held key release now |
| 1192 | * and fall through and handle the new |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1193 | * event below. */ |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1194 | update_xkb_state_from_core(c, key_release->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1195 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1196 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1197 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1198 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 1199 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1200 | free(prev); |
| 1201 | prev = NULL; |
| 1202 | break; |
| 1203 | } |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1204 | |
| 1205 | case XCB_FOCUS_IN: |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 1206 | assert(response_type == XCB_KEYMAP_NOTIFY); |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1207 | keymap_notify = (xcb_keymap_notify_event_t *) event; |
| 1208 | c->keys.size = 0; |
| 1209 | for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) { |
| 1210 | set = keymap_notify->keys[i >> 3] & |
| 1211 | (1 << (i & 7)); |
| 1212 | if (set) { |
| 1213 | k = wl_array_add(&c->keys, sizeof *k); |
| 1214 | *k = i; |
| 1215 | } |
| 1216 | } |
| 1217 | |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1218 | /* Unfortunately the state only comes with the enter |
| 1219 | * event, rather than with the focus event. I'm not |
| 1220 | * sure of the exact semantics around it and whether |
| 1221 | * we can ensure that we get both? */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1222 | notify_keyboard_focus_in(&c->core_seat, &c->keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1223 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1224 | |
| 1225 | free(prev); |
| 1226 | prev = NULL; |
| 1227 | break; |
| 1228 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1229 | default: |
| 1230 | /* No previous event held */ |
| 1231 | break; |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
John Kåre Alsaker | 143a898 | 2012-10-12 12:25:07 +0200 | [diff] [blame] | 1234 | switch (response_type) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1235 | case XCB_KEY_PRESS: |
| 1236 | key_press = (xcb_key_press_event_t *) event; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1237 | if (!c->has_xkb) |
| 1238 | update_xkb_state_from_core(c, key_press->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1239 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1240 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1241 | key_press->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1242 | WL_KEYBOARD_KEY_STATE_PRESSED, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 1243 | c->has_xkb ? STATE_UPDATE_NONE : |
| 1244 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1245 | break; |
| 1246 | case XCB_KEY_RELEASE: |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 1247 | /* If we don't have XKB, we need to use the lame |
| 1248 | * autorepeat detection above. */ |
| 1249 | if (!c->has_xkb) { |
| 1250 | prev = event; |
| 1251 | break; |
| 1252 | } |
| 1253 | key_release = (xcb_key_press_event_t *) event; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1254 | notify_key(&c->core_seat, |
Daniel Stone | 3ee91e1 | 2012-06-22 13:21:36 +0100 | [diff] [blame] | 1255 | weston_compositor_get_time(), |
| 1256 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1257 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 1258 | STATE_UPDATE_NONE); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1259 | break; |
| 1260 | case XCB_BUTTON_PRESS: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1261 | x11_compositor_deliver_button_event(c, event, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1262 | break; |
| 1263 | case XCB_BUTTON_RELEASE: |
Tiago Vignatti | a3a7162 | 2011-12-08 17:03:17 +0200 | [diff] [blame] | 1264 | x11_compositor_deliver_button_event(c, event, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1265 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1266 | case XCB_MOTION_NOTIFY: |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1267 | x11_compositor_deliver_motion_event(c, event); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1268 | break; |
| 1269 | |
| 1270 | case XCB_EXPOSE: |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 1271 | expose = (xcb_expose_event_t *) event; |
| 1272 | output = x11_compositor_find_output(c, expose->window); |
| 1273 | weston_output_schedule_repaint(&output->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1274 | break; |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 1275 | |
| 1276 | case XCB_ENTER_NOTIFY: |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1277 | x11_compositor_deliver_enter_event(c, event); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 1278 | break; |
| 1279 | |
| 1280 | case XCB_LEAVE_NOTIFY: |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1281 | enter_notify = (xcb_enter_notify_event_t *) event; |
Kristian Høgsberg | 2dfe626 | 2011-02-08 11:59:53 -0500 | [diff] [blame] | 1282 | if (enter_notify->state >= Button1Mask) |
| 1283 | break; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1284 | if (!c->has_xkb) |
| 1285 | update_xkb_state_from_core(c, enter_notify->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1286 | notify_pointer_focus(&c->core_seat, NULL, 0, 0); |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 1287 | break; |
| 1288 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1289 | case XCB_CLIENT_MESSAGE: |
| 1290 | client_message = (xcb_client_message_event_t *) event; |
| 1291 | atom = client_message->data.data32[0]; |
| 1292 | if (atom == c->atom.wm_delete_window) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1293 | wl_display_terminate(c->base.wl_display); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1294 | break; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1295 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1296 | case XCB_FOCUS_IN: |
| 1297 | focus_in = (xcb_focus_in_event_t *) event; |
| 1298 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED) |
| 1299 | break; |
| 1300 | |
Kristian Høgsberg | 025f7b8 | 2011-04-19 12:38:22 -0400 | [diff] [blame] | 1301 | prev = event; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1302 | break; |
| 1303 | |
| 1304 | case XCB_FOCUS_OUT: |
| 1305 | focus_in = (xcb_focus_in_event_t *) event; |
Kristian Høgsberg | 42e40ae | 2011-12-19 14:36:50 -0500 | [diff] [blame] | 1306 | if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED || |
| 1307 | focus_in->mode == XCB_NOTIFY_MODE_UNGRAB) |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1308 | break; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1309 | notify_keyboard_focus_out(&c->core_seat); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1310 | break; |
| 1311 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1312 | default: |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1313 | break; |
| 1314 | } |
| 1315 | |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 1316 | #ifdef HAVE_XCB_XKB |
Rui Matos | c6c2f8e | 2013-10-10 19:44:20 +0200 | [diff] [blame] | 1317 | if (c->has_xkb) { |
| 1318 | if (response_type == c->xkb_event_base) { |
| 1319 | xcb_xkb_state_notify_event_t *state = |
| 1320 | (xcb_xkb_state_notify_event_t *) event; |
| 1321 | if (state->xkbType == XCB_XKB_STATE_NOTIFY) |
| 1322 | update_xkb_state(c, state); |
| 1323 | } else if (response_type == XCB_PROPERTY_NOTIFY) { |
| 1324 | xcb_property_notify_event_t *prop_notify = |
| 1325 | (xcb_property_notify_event_t *) event; |
| 1326 | if (prop_notify->window == c->screen->root && |
| 1327 | prop_notify->atom == c->atom.xkb_names && |
| 1328 | prop_notify->state == XCB_PROPERTY_NEW_VALUE) |
| 1329 | update_xkb_keymap(c); |
| 1330 | } |
Daniel Stone | e2faa12 | 2012-06-22 13:21:39 +0100 | [diff] [blame] | 1331 | } |
| 1332 | #endif |
| 1333 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1334 | count++; |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1335 | if (prev != event) |
Kristian Høgsberg | 3ddd148 | 2011-04-15 15:48:07 -0400 | [diff] [blame] | 1336 | free (event); |
| 1337 | } |
| 1338 | |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1339 | switch (prev ? prev->response_type & ~0x80 : 0x80) { |
| 1340 | case XCB_KEY_RELEASE: |
| 1341 | key_release = (xcb_key_press_event_t *) prev; |
Daniel Stone | 154c34c | 2012-06-22 13:21:40 +0100 | [diff] [blame] | 1342 | update_xkb_state_from_core(c, key_release->state); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1343 | notify_key(&c->core_seat, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1344 | weston_compositor_get_time(), |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1345 | key_release->detail - 8, |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1346 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 1347 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 94da7e1 | 2011-04-19 09:23:29 -0400 | [diff] [blame] | 1348 | free(prev); |
| 1349 | prev = NULL; |
| 1350 | break; |
| 1351 | default: |
| 1352 | break; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1353 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1354 | |
Bill Spitzak | b715cec | 2012-06-05 17:08:23 -0400 | [diff] [blame] | 1355 | return count; |
Kristian Høgsberg | ee72482 | 2011-04-21 14:51:44 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1358 | #define F(field) offsetof(struct x11_compositor, field) |
| 1359 | |
| 1360 | static void |
| 1361 | x11_compositor_get_resources(struct x11_compositor *c) |
| 1362 | { |
| 1363 | static const struct { const char *name; int offset; } atoms[] = { |
| 1364 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 1365 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
| 1366 | { "WM_SIZE_HINTS", F(atom.wm_size_hints) }, |
| 1367 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 1368 | { "WM_CLASS", F(atom.wm_class) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1369 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 1370 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1371 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 1372 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 1373 | { "_NET_SUPPORTING_WM_CHECK", |
| 1374 | F(atom.net_supporting_wm_check) }, |
| 1375 | { "_NET_SUPPORTED", F(atom.net_supported) }, |
Kristian Høgsberg | 24ed621 | 2011-01-26 14:02:31 -0500 | [diff] [blame] | 1376 | { "STRING", F(atom.string) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1377 | { "UTF8_STRING", F(atom.utf8_string) }, |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 1378 | { "CARDINAL", F(atom.cardinal) }, |
Daniel Stone | 855028f | 2012-05-01 20:37:10 +0100 | [diff] [blame] | 1379 | { "_XKB_RULES_NAMES", F(atom.xkb_names) }, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1380 | }; |
| 1381 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1382 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1383 | xcb_intern_atom_reply_t *reply; |
| 1384 | xcb_pixmap_t pixmap; |
| 1385 | xcb_gc_t gc; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1386 | unsigned int i; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1387 | uint8_t data[] = { 0, 0, 0, 0 }; |
| 1388 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1389 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1390 | cookies[i] = xcb_intern_atom (c->conn, 0, |
| 1391 | strlen(atoms[i].name), |
| 1392 | atoms[i].name); |
| 1393 | |
Kristian Høgsberg | 09e2692 | 2011-12-23 13:33:45 -0500 | [diff] [blame] | 1394 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1395 | reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL); |
| 1396 | *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom; |
| 1397 | free(reply); |
| 1398 | } |
| 1399 | |
| 1400 | pixmap = xcb_generate_id(c->conn); |
| 1401 | gc = xcb_generate_id(c->conn); |
| 1402 | xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1); |
| 1403 | xcb_create_gc(c->conn, gc, pixmap, 0, NULL); |
| 1404 | xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP, |
| 1405 | pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data); |
| 1406 | c->null_cursor = xcb_generate_id(c->conn); |
| 1407 | xcb_create_cursor (c->conn, c->null_cursor, |
| 1408 | pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1); |
| 1409 | xcb_free_gc(c->conn, gc); |
| 1410 | xcb_free_pixmap(c->conn, pixmap); |
| 1411 | } |
| 1412 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1413 | static void |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 1414 | x11_compositor_get_wm_info(struct x11_compositor *c) |
| 1415 | { |
| 1416 | xcb_get_property_cookie_t cookie; |
| 1417 | xcb_get_property_reply_t *reply; |
| 1418 | xcb_atom_t *atom; |
| 1419 | unsigned int i; |
| 1420 | |
| 1421 | cookie = xcb_get_property(c->conn, 0, c->screen->root, |
| 1422 | c->atom.net_supported, |
| 1423 | XCB_ATOM_ATOM, 0, 1024); |
| 1424 | reply = xcb_get_property_reply(c->conn, cookie, NULL); |
| 1425 | if (reply == NULL) |
| 1426 | return; |
| 1427 | |
| 1428 | atom = (xcb_atom_t *) xcb_get_property_value(reply); |
| 1429 | |
| 1430 | for (i = 0; i < reply->value_len; i++) { |
| 1431 | if (atom[i] == c->atom.net_wm_state_fullscreen) |
| 1432 | c->has_net_wm_state_fullscreen = 1; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1437 | x11_restore(struct weston_compositor *ec) |
| 1438 | { |
| 1439 | } |
| 1440 | |
| 1441 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1442 | x11_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1443 | { |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1444 | struct x11_compositor *compositor = (struct x11_compositor *)ec; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1445 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1446 | wl_event_source_remove(compositor->xcb_source); |
| 1447 | x11_input_destroy(compositor); |
| 1448 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 1449 | ec->renderer->destroy(ec); |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1450 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 1451 | weston_compositor_shutdown(ec); /* destroys outputs, too */ |
| 1452 | |
Pekka Paalanen | 43c61d8 | 2012-01-03 11:58:34 +0200 | [diff] [blame] | 1453 | XCloseDisplay(compositor->dpy); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1454 | free(ec); |
| 1455 | } |
| 1456 | |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1457 | static uint32_t |
| 1458 | parse_transform(const char *transform, const char *output_name) |
| 1459 | { |
| 1460 | static const struct { const char *name; uint32_t token; } names[] = { |
| 1461 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 1462 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 1463 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 1464 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 1465 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 1466 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 1467 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 1468 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 1469 | }; |
| 1470 | unsigned int i; |
| 1471 | |
| 1472 | for (i = 0; i < ARRAY_LENGTH(names); i++) |
| 1473 | if (strcmp(names[i].name, transform) == 0) |
| 1474 | return names[i].token; |
| 1475 | |
| 1476 | weston_log("Invalid transform \"%s\" for output %s\n", |
| 1477 | transform, output_name); |
| 1478 | |
| 1479 | return WL_OUTPUT_TRANSFORM_NORMAL; |
| 1480 | } |
| 1481 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1482 | static int |
| 1483 | init_gl_renderer(struct x11_compositor *c) |
| 1484 | { |
| 1485 | int ret; |
| 1486 | |
| 1487 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1488 | "gl_renderer_interface"); |
| 1489 | if (!gl_renderer) |
| 1490 | return -1; |
| 1491 | |
| 1492 | ret = gl_renderer->create(&c->base, (EGLNativeDisplayType) c->dpy, |
| 1493 | gl_renderer->opaque_attribs, NULL); |
| 1494 | |
| 1495 | return ret; |
| 1496 | } |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1497 | static struct weston_compositor * |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1498 | x11_compositor_create(struct wl_display *display, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1499 | int fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1500 | int no_input, |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1501 | int use_pixman, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1502 | int *argc, char *argv[], |
| 1503 | struct weston_config *config) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1504 | { |
| 1505 | struct x11_compositor *c; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1506 | struct x11_output *output; |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1507 | struct weston_config_section *section; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1508 | xcb_screen_iterator_t s; |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1509 | int i, x = 0, output_count = 0; |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1510 | int width, height, count, scale; |
| 1511 | const char *section_name; |
| 1512 | char *name, *t, *mode; |
| 1513 | uint32_t transform; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1514 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1515 | weston_log("initializing x11 backend\n"); |
| 1516 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1517 | c = zalloc(sizeof *c); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1518 | if (c == NULL) |
| 1519 | return NULL; |
| 1520 | |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1521 | if (weston_compositor_init(&c->base, display, argc, argv, config) < 0) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1522 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1523 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 1524 | c->dpy = XOpenDisplay(NULL); |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 1525 | if (c->dpy == NULL) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1526 | goto err_free; |
Cyril Brulebois | 2079829 | 2011-04-06 18:05:40 +0200 | [diff] [blame] | 1527 | |
Benjamin Franzke | 3b288af | 2011-02-20 19:58:42 +0100 | [diff] [blame] | 1528 | c->conn = XGetXCBConnection(c->dpy); |
Benjamin Franzke | e997c5f | 2011-03-25 14:06:37 +0100 | [diff] [blame] | 1529 | XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue); |
| 1530 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1531 | if (xcb_connection_has_error(c->conn)) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1532 | goto err_xdisplay; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1533 | |
| 1534 | s = xcb_setup_roots_iterator(xcb_get_setup(c->conn)); |
| 1535 | c->screen = s.data; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1536 | wl_array_init(&c->keys); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1537 | |
| 1538 | x11_compositor_get_resources(c); |
Kristian Høgsberg | 7cc3d84 | 2013-02-19 21:19:04 -0500 | [diff] [blame] | 1539 | x11_compositor_get_wm_info(c); |
| 1540 | |
| 1541 | if (!c->has_net_wm_state_fullscreen && fullscreen) { |
| 1542 | weston_log("Can not fullscreen without window manager support" |
| 1543 | "(need _NET_WM_STATE_FULLSCREEN)\n"); |
| 1544 | fullscreen = 0; |
| 1545 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1546 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1547 | c->base.wl_display = display; |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1548 | c->use_pixman = use_pixman; |
| 1549 | if (c->use_pixman) { |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 1550 | if (pixman_renderer_init(&c->base) < 0) |
| 1551 | goto err_xdisplay; |
| 1552 | } |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1553 | else if (init_gl_renderer(c) < 0) { |
| 1554 | goto err_xdisplay; |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 1555 | } |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1556 | weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1557 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1558 | c->base.destroy = x11_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1559 | c->base.restore = x11_restore; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1560 | |
Daniel Stone | 22815f9 | 2012-06-22 13:21:34 +0100 | [diff] [blame] | 1561 | if (x11_input_create(c, no_input) < 0) |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 1562 | goto err_renderer; |
Daniel Stone | 22815f9 | 2012-06-22 13:21:34 +0100 | [diff] [blame] | 1563 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1564 | width = option_width ? option_width : 1024; |
| 1565 | height = option_height ? option_height : 640; |
| 1566 | count = option_count ? option_count : 1; |
| 1567 | |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1568 | section = NULL; |
| 1569 | while (weston_config_next_section(c->base.config, |
| 1570 | §ion, §ion_name)) { |
| 1571 | if (strcmp(section_name, "output") != 0) |
| 1572 | continue; |
| 1573 | weston_config_section_get_string(section, "name", &name, NULL); |
Rob Bradford | 5c4245d | 2013-07-26 16:29:44 +0100 | [diff] [blame] | 1574 | if (name == NULL || name[0] != 'X') { |
| 1575 | free(name); |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1576 | continue; |
Rob Bradford | 5c4245d | 2013-07-26 16:29:44 +0100 | [diff] [blame] | 1577 | } |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1578 | |
| 1579 | weston_config_section_get_string(section, |
| 1580 | "mode", &mode, "1024x600"); |
| 1581 | if (sscanf(mode, "%dx%d", &width, &height) != 2) { |
| 1582 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 1583 | mode, name); |
| 1584 | width = 1024; |
| 1585 | height = 600; |
| 1586 | } |
| 1587 | free(mode); |
| 1588 | |
| 1589 | if (option_width) |
| 1590 | width = option_width; |
| 1591 | if (option_height) |
| 1592 | height = option_height; |
| 1593 | |
| 1594 | weston_config_section_get_int(section, "scale", &scale, 1); |
| 1595 | weston_config_section_get_string(section, |
| 1596 | "transform", &t, "normal"); |
| 1597 | transform = parse_transform(t, name); |
| 1598 | free(t); |
| 1599 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1600 | output = x11_compositor_create_output(c, x, 0, |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1601 | width, height, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1602 | fullscreen, no_input, |
Kristian Høgsberg | 8f6fcbf | 2013-05-26 21:21:23 -0400 | [diff] [blame] | 1603 | name, transform, scale); |
| 1604 | free(name); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1605 | if (output == NULL) |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1606 | goto err_x11_input; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1607 | |
| 1608 | x = pixman_region32_extents(&output->base.region)->x2; |
| 1609 | |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1610 | output_count++; |
| 1611 | if (option_count && output_count >= option_count) |
| 1612 | break; |
| 1613 | } |
| 1614 | |
| 1615 | for (i = output_count; i < count; i++) { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1616 | output = x11_compositor_create_output(c, x, 0, width, height, |
| 1617 | fullscreen, no_input, NULL, |
Pekka Paalanen | daaddc6 | 2013-05-23 13:31:39 +0300 | [diff] [blame] | 1618 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1619 | if (output == NULL) |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1620 | goto err_x11_input; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1621 | x = pixman_region32_extents(&output->base.region)->x2; |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 1622 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1623 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1624 | c->xcb_source = |
Kristian Høgsberg | 22ba60e | 2012-03-12 01:18:24 -0400 | [diff] [blame] | 1625 | wl_event_loop_add_fd(c->base.input_loop, |
| 1626 | xcb_get_file_descriptor(c->conn), |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1627 | WL_EVENT_READABLE, |
| 1628 | x11_compositor_handle_event, c); |
Kristian Høgsberg | 127d0f0 | 2011-04-22 12:18:13 -0400 | [diff] [blame] | 1629 | wl_event_source_check(c->xcb_source); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1630 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1631 | return &c->base; |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1632 | |
| 1633 | err_x11_input: |
| 1634 | x11_input_destroy(c); |
Vasily Khoruzhick | b3add19 | 2013-01-07 20:39:50 +0300 | [diff] [blame] | 1635 | err_renderer: |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 1636 | c->base.renderer->destroy(&c->base); |
Martin Olsson | 11434bb | 2012-07-08 03:03:44 +0200 | [diff] [blame] | 1637 | err_xdisplay: |
| 1638 | XCloseDisplay(c->dpy); |
| 1639 | err_free: |
| 1640 | free(c); |
| 1641 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1642 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1643 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1644 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 1645 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1646 | struct weston_config *config) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1647 | { |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1648 | int fullscreen = 0; |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1649 | int no_input = 0; |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1650 | int use_pixman = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1651 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1652 | const struct weston_option x11_options[] = { |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1653 | { WESTON_OPTION_INTEGER, "width", 0, &option_width }, |
| 1654 | { WESTON_OPTION_INTEGER, "height", 0, &option_height }, |
Kristian Høgsberg | d6f09a7 | 2012-11-09 11:12:21 -0500 | [diff] [blame] | 1655 | { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen }, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1656 | { WESTON_OPTION_INTEGER, "output-count", 0, &option_count }, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1657 | { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input }, |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1658 | { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman }, |
Kristian Høgsberg | 83eeacb | 2011-06-18 04:20:54 -0400 | [diff] [blame] | 1659 | }; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1660 | |
| 1661 | parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1662 | |
Kristian Høgsberg | 1ccd9d2 | 2011-07-21 10:22:13 -0700 | [diff] [blame] | 1663 | return x11_compositor_create(display, |
Scott Moreau | 248aaec | 2012-08-03 14:19:52 -0600 | [diff] [blame] | 1664 | fullscreen, |
Pekka Paalanen | 36f155f | 2012-06-07 15:07:05 +0300 | [diff] [blame] | 1665 | no_input, |
Vasily Khoruzhick | d894351 | 2013-01-07 22:36:02 +0300 | [diff] [blame] | 1666 | use_pixman, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1667 | argc, argv, config); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1668 | } |