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