Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define _GNU_SOURCE |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/un.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <errno.h> |
| 32 | #include <unistd.h> |
| 33 | #include <signal.h> |
| 34 | |
| 35 | #include "xwayland.h" |
| 36 | |
| 37 | #include "../../shared/cairo-util.h" |
| 38 | #include "../compositor.h" |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 39 | #include "../log.h" |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 40 | #include "xserver-server-protocol.h" |
| 41 | #include "hash.h" |
| 42 | |
| 43 | struct motif_wm_hints { |
| 44 | uint32_t flags; |
| 45 | uint32_t functions; |
| 46 | uint32_t decorations; |
| 47 | int32_t input_mode; |
| 48 | uint32_t status; |
| 49 | }; |
| 50 | |
| 51 | #define MWM_HINTS_FUNCTIONS (1L << 0) |
| 52 | #define MWM_HINTS_DECORATIONS (1L << 1) |
| 53 | #define MWM_HINTS_INPUT_MODE (1L << 2) |
| 54 | #define MWM_HINTS_STATUS (1L << 3) |
| 55 | |
| 56 | #define MWM_FUNC_ALL (1L << 0) |
| 57 | #define MWM_FUNC_RESIZE (1L << 1) |
| 58 | #define MWM_FUNC_MOVE (1L << 2) |
| 59 | #define MWM_FUNC_MINIMIZE (1L << 3) |
| 60 | #define MWM_FUNC_MAXIMIZE (1L << 4) |
| 61 | #define MWM_FUNC_CLOSE (1L << 5) |
| 62 | |
| 63 | #define MWM_DECOR_ALL (1L << 0) |
| 64 | #define MWM_DECOR_BORDER (1L << 1) |
| 65 | #define MWM_DECOR_RESIZEH (1L << 2) |
| 66 | #define MWM_DECOR_TITLE (1L << 3) |
| 67 | #define MWM_DECOR_MENU (1L << 4) |
| 68 | #define MWM_DECOR_MINIMIZE (1L << 5) |
| 69 | #define MWM_DECOR_MAXIMIZE (1L << 6) |
| 70 | |
| 71 | #define MWM_INPUT_MODELESS 0 |
| 72 | #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1 |
| 73 | #define MWM_INPUT_SYSTEM_MODAL 2 |
| 74 | #define MWM_INPUT_FULL_APPLICATION_MODAL 3 |
| 75 | #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL |
| 76 | |
| 77 | #define MWM_TEAROFF_WINDOW (1L<<0) |
| 78 | |
| 79 | #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0 |
| 80 | #define _NET_WM_MOVERESIZE_SIZE_TOP 1 |
| 81 | #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2 |
| 82 | #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3 |
| 83 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4 |
| 84 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5 |
| 85 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6 |
| 86 | #define _NET_WM_MOVERESIZE_SIZE_LEFT 7 |
| 87 | #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */ |
| 88 | #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */ |
| 89 | #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */ |
| 90 | #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */ |
| 91 | |
| 92 | |
| 93 | |
| 94 | struct weston_wm_window { |
| 95 | struct weston_wm *wm; |
| 96 | xcb_window_t id; |
| 97 | xcb_window_t frame_id; |
| 98 | cairo_surface_t *cairo_surface; |
| 99 | struct weston_surface *surface; |
| 100 | struct shell_surface *shsurf; |
| 101 | struct wl_listener surface_destroy_listener; |
| 102 | struct wl_event_source *repaint_source; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 103 | struct wl_event_source *configure_source; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 104 | int properties_dirty; |
| 105 | char *class; |
| 106 | char *name; |
| 107 | struct weston_wm_window *transient_for; |
| 108 | uint32_t protocols; |
| 109 | xcb_atom_t type; |
| 110 | int width, height; |
| 111 | int x, y; |
| 112 | int decorate; |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 113 | int override_redirect; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static struct weston_wm_window * |
| 117 | get_wm_window(struct weston_surface *surface); |
| 118 | |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 119 | static void |
| 120 | weston_wm_window_schedule_repaint(struct weston_wm_window *window); |
| 121 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 122 | const char * |
| 123 | get_atom_name(xcb_connection_t *c, xcb_atom_t atom) |
| 124 | { |
| 125 | xcb_get_atom_name_cookie_t cookie; |
| 126 | xcb_get_atom_name_reply_t *reply; |
| 127 | xcb_generic_error_t *e; |
| 128 | static char buffer[64]; |
| 129 | |
| 130 | if (atom == XCB_ATOM_NONE) |
| 131 | return "None"; |
| 132 | |
| 133 | cookie = xcb_get_atom_name (c, atom); |
| 134 | reply = xcb_get_atom_name_reply (c, cookie, &e); |
| 135 | snprintf(buffer, sizeof buffer, "%.*s", |
| 136 | xcb_get_atom_name_name_length (reply), |
| 137 | xcb_get_atom_name_name (reply)); |
| 138 | free(reply); |
| 139 | |
| 140 | return buffer; |
| 141 | } |
| 142 | |
| 143 | void |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 144 | dump_property(struct weston_wm *wm, |
| 145 | xcb_atom_t property, xcb_get_property_reply_t *reply) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 146 | { |
| 147 | int32_t *incr_value; |
| 148 | const char *text_value, *name; |
| 149 | xcb_atom_t *atom_value; |
| 150 | int width, len; |
| 151 | uint32_t i; |
| 152 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 153 | width = weston_log_continue("%s: ", get_atom_name(wm->conn, property)); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 154 | if (reply == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 155 | weston_log_continue("(no reply)\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 159 | width += weston_log_continue( |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 160 | "%s/%d, length %d (value_len %d): ", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 161 | get_atom_name(wm->conn, reply->type), |
| 162 | reply->format, |
| 163 | xcb_get_property_value_length(reply), |
| 164 | reply->value_len); |
| 165 | |
| 166 | if (reply->type == wm->atom.incr) { |
| 167 | incr_value = xcb_get_property_value(reply); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 168 | weston_log_continue("%d\n", *incr_value); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 169 | } else if (reply->type == wm->atom.utf8_string || |
| 170 | reply->type == wm->atom.string) { |
| 171 | text_value = xcb_get_property_value(reply); |
| 172 | if (reply->value_len > 40) |
| 173 | len = 40; |
| 174 | else |
| 175 | len = reply->value_len; |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 176 | weston_log_continue("\"%.*s\"\n", len, text_value); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 177 | } else if (reply->type == XCB_ATOM_ATOM) { |
| 178 | atom_value = xcb_get_property_value(reply); |
| 179 | for (i = 0; i < reply->value_len; i++) { |
| 180 | name = get_atom_name(wm->conn, atom_value[i]); |
| 181 | if (width + strlen(name) + 2 > 78) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 182 | weston_log_continue("\n "); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 183 | width = 4; |
| 184 | } else if (i > 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 185 | width += weston_log_continue(", "); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 188 | width += weston_log_continue("%s", name); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 189 | } |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 190 | weston_log_continue("\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 191 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 192 | weston_log_continue("huh?\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 196 | void |
| 197 | read_and_dump_property(struct weston_wm *wm, |
| 198 | xcb_window_t window, xcb_atom_t property) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 199 | { |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 200 | xcb_get_property_reply_t *reply; |
| 201 | xcb_get_property_cookie_t cookie; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 202 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 203 | cookie = xcb_get_property(wm->conn, 0, window, |
| 204 | property, XCB_ATOM_ANY, 0, 2048); |
| 205 | reply = xcb_get_property_reply(wm->conn, cookie, NULL); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 206 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 207 | dump_property(wm, property, reply); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 208 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 209 | free(reply); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* We reuse some predefined, but otherwise useles atoms */ |
| 213 | #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0 |
| 214 | #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1 |
| 215 | |
| 216 | static void |
| 217 | weston_wm_window_read_properties(struct weston_wm_window *window) |
| 218 | { |
| 219 | struct weston_wm *wm = window->wm; |
| 220 | |
| 221 | #define F(field) offsetof(struct weston_wm_window, field) |
| 222 | const struct { |
| 223 | xcb_atom_t atom; |
| 224 | xcb_atom_t type; |
| 225 | int offset; |
| 226 | } props[] = { |
| 227 | { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) }, |
| 228 | { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) }, |
| 229 | { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) }, |
| 230 | { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) }, |
| 231 | { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) }, |
| 232 | { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) }, |
| 233 | { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 }, |
| 234 | }; |
| 235 | #undef F |
| 236 | |
| 237 | xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)]; |
| 238 | xcb_get_property_reply_t *reply; |
| 239 | void *p; |
| 240 | uint32_t *xid; |
| 241 | xcb_atom_t *atom; |
| 242 | uint32_t i; |
| 243 | struct motif_wm_hints *hints; |
| 244 | |
| 245 | if (!window->properties_dirty) |
| 246 | return; |
| 247 | window->properties_dirty = 0; |
| 248 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 249 | for (i = 0; i < ARRAY_LENGTH(props); i++) |
| 250 | cookie[i] = xcb_get_property(wm->conn, |
| 251 | 0, /* delete */ |
| 252 | window->id, |
| 253 | props[i].atom, |
| 254 | XCB_ATOM_ANY, 0, 2048); |
| 255 | |
| 256 | window->decorate = 1; |
| 257 | for (i = 0; i < ARRAY_LENGTH(props); i++) { |
| 258 | reply = xcb_get_property_reply(wm->conn, cookie[i], NULL); |
| 259 | if (!reply) |
| 260 | /* Bad window, typically */ |
| 261 | continue; |
| 262 | if (reply->type == XCB_ATOM_NONE) { |
| 263 | /* No such property */ |
| 264 | free(reply); |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | p = ((char *) window + props[i].offset); |
| 269 | |
| 270 | switch (props[i].type) { |
| 271 | case XCB_ATOM_STRING: |
| 272 | /* FIXME: We're using this for both string and |
| 273 | utf8_string */ |
| 274 | if (*(char **) p) |
| 275 | free(*(char **) p); |
| 276 | |
| 277 | *(char **) p = |
| 278 | strndup(xcb_get_property_value(reply), |
| 279 | xcb_get_property_value_length(reply)); |
| 280 | break; |
| 281 | case XCB_ATOM_WINDOW: |
| 282 | xid = xcb_get_property_value(reply); |
| 283 | *(struct weston_wm_window **) p = |
| 284 | hash_table_lookup(wm->window_hash, *xid); |
| 285 | break; |
| 286 | case XCB_ATOM_ATOM: |
| 287 | atom = xcb_get_property_value(reply); |
| 288 | *(xcb_atom_t *) p = *atom; |
| 289 | break; |
| 290 | case TYPE_WM_PROTOCOLS: |
| 291 | break; |
| 292 | case TYPE_MOTIF_WM_HINTS: |
| 293 | hints = xcb_get_property_value(reply); |
| 294 | if (hints->flags & MWM_HINTS_DECORATIONS) |
| 295 | window->decorate = hints->decorations > 0; |
| 296 | break; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | free(reply); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 305 | weston_wm_window_get_frame_size(struct weston_wm_window *window, |
| 306 | int *width, int *height) |
| 307 | { |
| 308 | struct theme *t = window->wm->theme; |
| 309 | |
| 310 | if (window->decorate) { |
| 311 | *width = window->width + (t->margin + t->width) * 2; |
| 312 | *height = window->height + |
| 313 | t->margin * 2 + t->width + t->titlebar_height; |
| 314 | } else { |
| 315 | *width = window->width + t->margin * 2; |
| 316 | *height = window->height + t->margin * 2; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static void |
| 321 | weston_wm_window_get_child_position(struct weston_wm_window *window, |
| 322 | int *x, int *y) |
| 323 | { |
| 324 | struct theme *t = window->wm->theme; |
| 325 | |
| 326 | if (window->decorate) { |
| 327 | *x = t->margin + t->width; |
| 328 | *y = t->margin + t->titlebar_height; |
| 329 | } else { |
| 330 | *x = t->margin; |
| 331 | *y = t->margin; |
| 332 | } |
| 333 | } |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 334 | |
| 335 | static void |
| 336 | weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 337 | { |
| 338 | xcb_configure_request_event_t *configure_request = |
| 339 | (xcb_configure_request_event_t *) event; |
| 340 | struct weston_wm_window *window; |
| 341 | uint32_t mask, values[16]; |
| 342 | int x, y, width, height, i = 0; |
| 343 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 344 | weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n", |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 345 | configure_request->window, |
| 346 | configure_request->x, configure_request->y, |
| 347 | configure_request->width, configure_request->height); |
| 348 | |
| 349 | window = hash_table_lookup(wm->window_hash, configure_request->window); |
| 350 | |
| 351 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH) |
| 352 | window->width = configure_request->width; |
| 353 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT) |
| 354 | window->height = configure_request->height; |
| 355 | |
| 356 | weston_wm_window_get_child_position(window, &x, &y); |
| 357 | values[i++] = x; |
| 358 | values[i++] = y; |
| 359 | values[i++] = window->width; |
| 360 | values[i++] = window->height; |
| 361 | values[i++] = 0; |
| 362 | mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | |
| 363 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | |
| 364 | XCB_CONFIG_WINDOW_BORDER_WIDTH; |
| 365 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) { |
| 366 | values[i++] = configure_request->sibling; |
| 367 | mask |= XCB_CONFIG_WINDOW_SIBLING; |
| 368 | } |
| 369 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) { |
| 370 | values[i++] = configure_request->stack_mode; |
| 371 | mask |= XCB_CONFIG_WINDOW_STACK_MODE; |
| 372 | } |
| 373 | |
| 374 | xcb_configure_window(wm->conn, window->id, mask, values); |
| 375 | |
| 376 | weston_wm_window_get_frame_size(window, &width, &height); |
| 377 | values[0] = width; |
| 378 | values[1] = height; |
| 379 | mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; |
| 380 | xcb_configure_window(wm->conn, window->frame_id, mask, values); |
| 381 | |
| 382 | weston_wm_window_schedule_repaint(window); |
| 383 | } |
| 384 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 385 | static void |
| 386 | weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 387 | { |
| 388 | xcb_configure_notify_event_t *configure_notify = |
| 389 | (xcb_configure_notify_event_t *) event; |
| 390 | struct weston_wm_window *window; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 391 | |
| 392 | window = hash_table_lookup(wm->window_hash, configure_notify->window); |
| 393 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 394 | weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 395 | configure_notify->window == window->id ? "client" : "frame", |
| 396 | configure_notify->window, |
| 397 | configure_notify->x, configure_notify->y, |
| 398 | configure_notify->width, configure_notify->height); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 402 | weston_wm_window_activate(struct wl_listener *listener, void *data) |
| 403 | { |
| 404 | struct weston_surface *surface = data; |
| 405 | struct weston_wm_window *window = get_wm_window(surface); |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 406 | struct weston_wm *wm = |
| 407 | container_of(listener, struct weston_wm, activate_listener); |
| 408 | xcb_client_message_event_t client_message; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 409 | |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 410 | if (window) { |
| 411 | client_message.response_type = XCB_CLIENT_MESSAGE; |
| 412 | client_message.format = 32; |
| 413 | client_message.window = window->id; |
| 414 | client_message.type = wm->atom.wm_protocols; |
| 415 | client_message.data.data32[0] = wm->atom.wm_take_focus; |
| 416 | client_message.data.data32[1] = XCB_TIME_CURRENT_TIME; |
| 417 | |
| 418 | xcb_send_event(wm->conn, 0, window->id, |
| 419 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 420 | (char *) &client_message); |
| 421 | |
| 422 | xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT, |
| 423 | window->id, XCB_TIME_CURRENT_TIME); |
| 424 | } else { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 425 | xcb_set_input_focus (wm->conn, |
| 426 | XCB_INPUT_FOCUS_POINTER_ROOT, |
| 427 | XCB_NONE, |
| 428 | XCB_TIME_CURRENT_TIME); |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 429 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 430 | |
| 431 | if (wm->focus_window) |
| 432 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 433 | wm->focus_window = window; |
| 434 | if (wm->focus_window) |
| 435 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 436 | } |
| 437 | |
| 438 | static int |
| 439 | our_resource(struct weston_wm *wm, uint32_t id) |
| 440 | { |
| 441 | const xcb_setup_t *setup; |
| 442 | |
| 443 | setup = xcb_get_setup(wm->conn); |
| 444 | |
| 445 | return (id & ~setup->resource_id_mask) == setup->resource_id_base; |
| 446 | } |
| 447 | |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 448 | #define ICCCM_WITHDRAWN_STATE 0 |
| 449 | #define ICCCM_NORMAL_STATE 1 |
| 450 | #define ICCCM_ICONIC_STATE 3 |
| 451 | |
| 452 | static void |
| 453 | weston_wm_window_set_state(struct weston_wm_window *window, int32_t state) |
| 454 | { |
| 455 | struct weston_wm *wm = window->wm; |
| 456 | uint32_t property[2]; |
| 457 | |
| 458 | property[0] = state; |
| 459 | property[1] = XCB_WINDOW_NONE; |
| 460 | |
| 461 | xcb_change_property(wm->conn, |
| 462 | XCB_PROP_MODE_REPLACE, |
| 463 | window->id, |
| 464 | wm->atom.wm_state, |
| 465 | wm->atom.wm_state, |
| 466 | 32, /* format */ |
| 467 | 2, property); |
| 468 | } |
| 469 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 470 | static void |
| 471 | weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 472 | { |
| 473 | xcb_map_request_event_t *map_request = |
| 474 | (xcb_map_request_event_t *) event; |
| 475 | struct weston_wm_window *window; |
| 476 | uint32_t values[1]; |
| 477 | int x, y, width, height; |
| 478 | |
| 479 | if (our_resource(wm, map_request->window)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 480 | weston_log("XCB_MAP_REQUEST (window %d, ours)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 481 | map_request->window); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | window = hash_table_lookup(wm->window_hash, map_request->window); |
| 486 | |
Kristian Høgsberg | bc6e162 | 2012-05-30 09:59:56 -0400 | [diff] [blame] | 487 | if (window->frame_id) |
| 488 | return; |
| 489 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 490 | weston_wm_window_read_properties(window); |
| 491 | |
| 492 | weston_wm_window_get_frame_size(window, &width, &height); |
| 493 | weston_wm_window_get_child_position(window, &x, &y); |
| 494 | |
| 495 | values[0] = |
| 496 | XCB_EVENT_MASK_KEY_PRESS | |
| 497 | XCB_EVENT_MASK_KEY_RELEASE | |
| 498 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 499 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 500 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
Kristian Høgsberg | 44c2013 | 2012-05-30 10:09:21 -0400 | [diff] [blame] | 501 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 502 | |
| 503 | window->frame_id = xcb_generate_id(wm->conn); |
| 504 | xcb_create_window(wm->conn, |
| 505 | XCB_COPY_FROM_PARENT, |
| 506 | window->frame_id, |
| 507 | wm->screen->root, |
| 508 | 0, 0, |
| 509 | width, height, |
| 510 | 0, |
| 511 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 512 | wm->screen->root_visual, |
| 513 | XCB_CW_EVENT_MASK, values); |
| 514 | xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y); |
| 515 | |
| 516 | values[0] = 0; |
| 517 | xcb_configure_window(wm->conn, window->id, |
| 518 | XCB_CONFIG_WINDOW_BORDER_WIDTH, values); |
| 519 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 520 | weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 521 | window->id, window, window->frame_id); |
| 522 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 523 | xcb_map_window(wm->conn, map_request->window); |
| 524 | xcb_map_window(wm->conn, window->frame_id); |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 525 | weston_wm_window_set_state(window, ICCCM_NORMAL_STATE); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 526 | |
| 527 | window->cairo_surface = |
| 528 | cairo_xcb_surface_create_with_xrender_format(wm->conn, |
| 529 | wm->screen, |
| 530 | window->frame_id, |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame^] | 531 | &wm->format_rgb, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 532 | width, height); |
| 533 | |
| 534 | hash_table_insert(wm->window_hash, window->frame_id, window); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 539 | { |
| 540 | xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event; |
| 541 | |
| 542 | if (our_resource(wm, map_notify->window)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 543 | weston_log("XCB_MAP_NOTIFY (window %d, ours)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 544 | map_notify->window); |
| 545 | return; |
| 546 | } |
| 547 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 548 | weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static void |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 552 | weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 553 | { |
| 554 | xcb_unmap_notify_event_t *unmap_notify = |
| 555 | (xcb_unmap_notify_event_t *) event; |
| 556 | struct weston_wm_window *window; |
| 557 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 558 | weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n", |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 559 | unmap_notify->window, |
| 560 | unmap_notify->event, |
| 561 | our_resource(wm, unmap_notify->window) ? ", ours" : ""); |
| 562 | |
| 563 | if (our_resource(wm, unmap_notify->window)) |
| 564 | return; |
| 565 | |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 566 | if (unmap_notify->response_type & 0x80) |
| 567 | /* We just ignore the ICCCM 4.1.4 synthetic unmap notify |
| 568 | * as it may come in after we've destroyed the window. */ |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 569 | return; |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 570 | |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 571 | window = hash_table_lookup(wm->window_hash, unmap_notify->window); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 572 | if (window->repaint_source) |
| 573 | wl_event_source_remove(window->repaint_source); |
| 574 | if (window->cairo_surface) |
| 575 | cairo_surface_destroy(window->cairo_surface); |
| 576 | |
Kristian Høgsberg | be375b3 | 2012-06-05 11:46:08 -0400 | [diff] [blame] | 577 | if (window->frame_id) { |
| 578 | xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0); |
| 579 | xcb_destroy_window(wm->conn, window->frame_id); |
| 580 | weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE); |
| 581 | hash_table_remove(wm->window_hash, window->frame_id); |
| 582 | window->frame_id = XCB_WINDOW_NONE; |
| 583 | } |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 584 | |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 585 | if (wm->focus_window == window) |
| 586 | wm->focus_window = NULL; |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 587 | if (window->surface) |
| 588 | wl_list_remove(&window->surface_destroy_listener.link); |
| 589 | window->surface = NULL; |
| 590 | } |
| 591 | |
| 592 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 593 | weston_wm_window_draw_decoration(void *data) |
| 594 | { |
| 595 | struct weston_wm_window *window = data; |
| 596 | struct weston_wm *wm = window->wm; |
| 597 | struct theme *t = wm->theme; |
| 598 | cairo_t *cr; |
| 599 | int x, y, width, height; |
| 600 | const char *title; |
| 601 | uint32_t flags = 0; |
| 602 | |
| 603 | weston_wm_window_read_properties(window); |
| 604 | |
| 605 | window->repaint_source = NULL; |
| 606 | |
| 607 | weston_wm_window_get_frame_size(window, &width, &height); |
| 608 | weston_wm_window_get_child_position(window, &x, &y); |
| 609 | |
| 610 | cairo_xcb_surface_set_size(window->cairo_surface, width, height); |
| 611 | cr = cairo_create(window->cairo_surface); |
| 612 | |
| 613 | if (window->decorate) { |
| 614 | if (wm->focus_window == window) |
| 615 | flags |= THEME_FRAME_ACTIVE; |
| 616 | |
| 617 | if (window->name) |
| 618 | title = window->name; |
| 619 | else |
| 620 | title = "untitled"; |
| 621 | |
| 622 | theme_render_frame(t, cr, width, height, title, flags); |
| 623 | } else { |
| 624 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 625 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 626 | cairo_paint(cr); |
| 627 | |
| 628 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 629 | cairo_set_source_rgba(cr, 0, 0, 0, 0.45); |
| 630 | tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64); |
| 631 | } |
| 632 | |
| 633 | cairo_destroy(cr); |
| 634 | |
| 635 | if (window->surface) { |
| 636 | /* We leave an extra pixel around the X window area to |
| 637 | * make sure we don't sample from the undefined alpha |
| 638 | * channel when filtering. */ |
| 639 | window->surface->opaque_rect[0] = |
| 640 | (double) (x - 1) / width; |
| 641 | window->surface->opaque_rect[1] = |
| 642 | (double) (x + window->width + 1) / width; |
| 643 | window->surface->opaque_rect[2] = |
| 644 | (double) (y - 1) / height; |
| 645 | window->surface->opaque_rect[3] = |
| 646 | (double) (y + window->height + 1) / height; |
| 647 | |
| 648 | pixman_region32_init_rect(&window->surface->input, |
| 649 | t->margin, t->margin, |
| 650 | width - 2 * t->margin, |
| 651 | height - 2 * t->margin); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | weston_wm_window_schedule_repaint(struct weston_wm_window *window) |
| 657 | { |
| 658 | struct weston_wm *wm = window->wm; |
| 659 | |
| 660 | if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source) |
| 661 | return; |
| 662 | |
| 663 | window->repaint_source = |
| 664 | wl_event_loop_add_idle(wm->server->loop, |
| 665 | weston_wm_window_draw_decoration, |
| 666 | window); |
| 667 | } |
| 668 | |
| 669 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 670 | weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 671 | { |
| 672 | xcb_property_notify_event_t *property_notify = |
| 673 | (xcb_property_notify_event_t *) event; |
| 674 | struct weston_wm_window *window; |
| 675 | |
| 676 | window = hash_table_lookup(wm->window_hash, property_notify->window); |
| 677 | if (window) |
| 678 | window->properties_dirty = 1; |
| 679 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 680 | weston_log("XCB_PROPERTY_NOTIFY: window %d, ", |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 681 | property_notify->window); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 682 | if (property_notify->state == XCB_PROPERTY_DELETE) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 683 | weston_log("deleted\n"); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 684 | else |
| 685 | read_and_dump_property(wm, property_notify->window, |
| 686 | property_notify->atom); |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 687 | |
| 688 | if (property_notify->atom == wm->atom.net_wm_name || |
| 689 | property_notify->atom == XCB_ATOM_WM_NAME) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 690 | weston_wm_window_schedule_repaint(window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static void |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 694 | weston_wm_window_create(struct weston_wm *wm, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 695 | xcb_window_t id, int width, int height, int override) |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 696 | { |
| 697 | struct weston_wm_window *window; |
| 698 | uint32_t values[1]; |
| 699 | |
| 700 | window = malloc(sizeof *window); |
| 701 | if (window == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 702 | weston_log("failed to allocate window\n"); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 703 | return; |
| 704 | } |
| 705 | |
| 706 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 707 | xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values); |
| 708 | |
| 709 | memset(window, 0, sizeof *window); |
| 710 | window->wm = wm; |
| 711 | window->id = id; |
| 712 | window->properties_dirty = 1; |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 713 | window->override_redirect = override; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 714 | window->width = width; |
| 715 | window->height = height; |
| 716 | |
| 717 | hash_table_insert(wm->window_hash, id, window); |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | weston_wm_window_destroy(struct weston_wm_window *window) |
| 722 | { |
| 723 | hash_table_remove(window->wm->window_hash, window->id); |
| 724 | free(window); |
| 725 | } |
| 726 | |
| 727 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 728 | weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 729 | { |
| 730 | xcb_create_notify_event_t *create_notify = |
| 731 | (xcb_create_notify_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 732 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 733 | weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 734 | create_notify->window, |
| 735 | create_notify->width, create_notify->height, |
| 736 | create_notify->override_redirect ? ", override" : "", |
| 737 | our_resource(wm, create_notify->window) ? ", ours" : ""); |
| 738 | |
| 739 | if (our_resource(wm, create_notify->window)) |
| 740 | return; |
| 741 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 742 | weston_wm_window_create(wm, create_notify->window, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 743 | create_notify->width, create_notify->height, |
| 744 | create_notify->override_redirect); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void |
| 748 | weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 749 | { |
| 750 | xcb_destroy_notify_event_t *destroy_notify = |
| 751 | (xcb_destroy_notify_event_t *) event; |
| 752 | struct weston_wm_window *window; |
| 753 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 754 | weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n", |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 755 | destroy_notify->window, |
| 756 | destroy_notify->event, |
| 757 | our_resource(wm, destroy_notify->window) ? ", ours" : ""); |
| 758 | |
| 759 | if (our_resource(wm, destroy_notify->window)) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 760 | return; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 761 | |
| 762 | window = hash_table_lookup(wm->window_hash, destroy_notify->window); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 763 | weston_wm_window_destroy(window); |
| 764 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 765 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 766 | static void |
| 767 | weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 768 | { |
| 769 | xcb_reparent_notify_event_t *reparent_notify = |
| 770 | (xcb_reparent_notify_event_t *) event; |
| 771 | struct weston_wm_window *window; |
Kristian Høgsberg | c9571fb | 2012-05-29 15:35:29 -0400 | [diff] [blame] | 772 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 773 | weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n", |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 774 | reparent_notify->window, |
| 775 | reparent_notify->parent, |
| 776 | reparent_notify->event); |
| 777 | |
| 778 | if (reparent_notify->parent == wm->screen->root) { |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 779 | weston_wm_window_create(wm, reparent_notify->window, 10, 10, |
| 780 | reparent_notify->override_redirect); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 781 | } else if (!our_resource(wm, reparent_notify->parent)) { |
| 782 | window = hash_table_lookup(wm->window_hash, |
| 783 | reparent_notify->window); |
| 784 | weston_wm_window_destroy(window); |
| 785 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static void |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 789 | weston_wm_window_handle_moveresize(struct weston_wm_window *window, |
| 790 | xcb_client_message_event_t *client_message) |
| 791 | { |
| 792 | static const int map[] = { |
| 793 | THEME_LOCATION_RESIZING_TOP_LEFT, |
| 794 | THEME_LOCATION_RESIZING_TOP, |
| 795 | THEME_LOCATION_RESIZING_TOP_RIGHT, |
| 796 | THEME_LOCATION_RESIZING_RIGHT, |
| 797 | THEME_LOCATION_RESIZING_BOTTOM_RIGHT, |
| 798 | THEME_LOCATION_RESIZING_BOTTOM, |
| 799 | THEME_LOCATION_RESIZING_BOTTOM_LEFT, |
| 800 | THEME_LOCATION_RESIZING_LEFT |
| 801 | }; |
| 802 | |
| 803 | struct weston_wm *wm = window->wm; |
| 804 | struct weston_seat *seat = wm->server->compositor->seat; |
| 805 | int detail; |
| 806 | struct weston_shell_interface *shell_interface = |
| 807 | &wm->server->compositor->shell_interface; |
| 808 | |
| 809 | if (seat->seat.pointer->button_count != 1 || |
| 810 | seat->seat.pointer->focus != &window->surface->surface) |
| 811 | return; |
| 812 | |
| 813 | detail = client_message->data.data32[2]; |
| 814 | switch (detail) { |
| 815 | case _NET_WM_MOVERESIZE_MOVE: |
| 816 | shell_interface->move(window->shsurf, seat); |
| 817 | break; |
| 818 | case _NET_WM_MOVERESIZE_SIZE_TOPLEFT: |
| 819 | case _NET_WM_MOVERESIZE_SIZE_TOP: |
| 820 | case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT: |
| 821 | case _NET_WM_MOVERESIZE_SIZE_RIGHT: |
| 822 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT: |
| 823 | case _NET_WM_MOVERESIZE_SIZE_BOTTOM: |
| 824 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT: |
| 825 | case _NET_WM_MOVERESIZE_SIZE_LEFT: |
| 826 | shell_interface->resize(window->shsurf, seat, map[detail]); |
| 827 | break; |
| 828 | case _NET_WM_MOVERESIZE_CANCEL: |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 834 | weston_wm_handle_client_message(struct weston_wm *wm, |
| 835 | xcb_generic_event_t *event) |
| 836 | { |
| 837 | xcb_client_message_event_t *client_message = |
| 838 | (xcb_client_message_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 839 | struct weston_wm_window *window; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 840 | |
| 841 | window = hash_table_lookup(wm->window_hash, client_message->window); |
| 842 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 843 | weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 844 | get_atom_name(wm->conn, client_message->type), |
| 845 | client_message->data.data32[0], |
| 846 | client_message->data.data32[1], |
| 847 | client_message->data.data32[2], |
| 848 | client_message->data.data32[3], |
| 849 | client_message->data.data32[4]); |
| 850 | |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 851 | if (client_message->type == wm->atom.net_wm_moveresize) |
| 852 | weston_wm_window_handle_moveresize(window, client_message); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static void |
| 856 | weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event) |
| 857 | { |
| 858 | xcb_button_press_event_t *button = (xcb_button_press_event_t *) event; |
| 859 | struct weston_shell_interface *shell_interface = |
| 860 | &wm->server->compositor->shell_interface; |
| 861 | struct weston_wm_window *window; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 862 | enum theme_location location; |
| 863 | struct theme *t = wm->theme; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 864 | int width, height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 865 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 866 | weston_log("XCB_BUTTON_%s (detail %d)\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 867 | button->response_type == XCB_BUTTON_PRESS ? |
| 868 | "PRESS" : "RELEASE", button->detail); |
| 869 | |
| 870 | window = hash_table_lookup(wm->window_hash, button->event); |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 871 | weston_wm_window_get_frame_size(window, &width, &height); |
| 872 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 873 | if (button->response_type == XCB_BUTTON_PRESS && |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 874 | button->detail == 1) { |
| 875 | location = theme_get_location(t, |
| 876 | button->event_x, |
| 877 | button->event_y, |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 878 | width, height); |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 879 | |
| 880 | switch (location) { |
| 881 | case THEME_LOCATION_TITLEBAR: |
| 882 | shell_interface->move(window->shsurf, |
| 883 | wm->server->compositor->seat); |
| 884 | break; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 885 | case THEME_LOCATION_RESIZING_TOP: |
| 886 | case THEME_LOCATION_RESIZING_BOTTOM: |
| 887 | case THEME_LOCATION_RESIZING_LEFT: |
| 888 | case THEME_LOCATION_RESIZING_RIGHT: |
| 889 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
| 890 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
| 891 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
| 892 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
| 893 | shell_interface->resize(window->shsurf, |
| 894 | wm->server->compositor->seat, |
| 895 | location); |
| 896 | break; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static int |
| 904 | weston_wm_handle_event(int fd, uint32_t mask, void *data) |
| 905 | { |
| 906 | struct weston_wm *wm = data; |
| 907 | xcb_generic_event_t *event; |
| 908 | int count = 0; |
| 909 | |
| 910 | while (event = xcb_poll_for_event(wm->conn), event != NULL) { |
| 911 | if (weston_wm_handle_selection_event(wm, event)) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 912 | free(event); |
| 913 | count++; |
| 914 | continue; |
| 915 | } |
| 916 | |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 917 | switch (event->response_type & ~0x80) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 918 | case XCB_BUTTON_PRESS: |
| 919 | case XCB_BUTTON_RELEASE: |
| 920 | weston_wm_handle_button(wm, event); |
| 921 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 922 | case XCB_CREATE_NOTIFY: |
| 923 | weston_wm_handle_create_notify(wm, event); |
| 924 | break; |
| 925 | case XCB_MAP_REQUEST: |
| 926 | weston_wm_handle_map_request(wm, event); |
| 927 | break; |
| 928 | case XCB_MAP_NOTIFY: |
| 929 | weston_wm_handle_map_notify(wm, event); |
| 930 | break; |
| 931 | case XCB_UNMAP_NOTIFY: |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 932 | weston_wm_handle_unmap_notify(wm, event); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 933 | break; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 934 | case XCB_REPARENT_NOTIFY: |
| 935 | weston_wm_handle_reparent_notify(wm, event); |
| 936 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 937 | case XCB_CONFIGURE_REQUEST: |
| 938 | weston_wm_handle_configure_request(wm, event); |
| 939 | break; |
| 940 | case XCB_CONFIGURE_NOTIFY: |
| 941 | weston_wm_handle_configure_notify(wm, event); |
| 942 | break; |
| 943 | case XCB_DESTROY_NOTIFY: |
| 944 | weston_wm_handle_destroy_notify(wm, event); |
| 945 | break; |
| 946 | case XCB_MAPPING_NOTIFY: |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 947 | weston_log("XCB_MAPPING_NOTIFY\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 948 | break; |
| 949 | case XCB_PROPERTY_NOTIFY: |
| 950 | weston_wm_handle_property_notify(wm, event); |
| 951 | break; |
| 952 | case XCB_CLIENT_MESSAGE: |
| 953 | weston_wm_handle_client_message(wm, event); |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | free(event); |
| 958 | count++; |
| 959 | } |
| 960 | |
| 961 | xcb_flush(wm->conn); |
| 962 | |
| 963 | return count; |
| 964 | } |
| 965 | |
| 966 | static void |
| 967 | wxs_wm_get_resources(struct weston_wm *wm) |
| 968 | { |
| 969 | |
| 970 | #define F(field) offsetof(struct weston_wm, field) |
| 971 | |
| 972 | static const struct { const char *name; int offset; } atoms[] = { |
| 973 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 974 | { "WM_TAKE_FOCUS", F(atom.wm_take_focus) }, |
| 975 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 976 | { "WM_STATE", F(atom.wm_state) }, |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 977 | { "WM_S0", F(atom.wm_s0) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 978 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
| 979 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
| 980 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 981 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
| 982 | { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) }, |
| 983 | { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) }, |
| 984 | { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) }, |
| 985 | |
| 986 | { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) }, |
| 987 | { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) }, |
| 988 | { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) }, |
| 989 | { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) }, |
| 990 | { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) }, |
| 991 | { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) }, |
| 992 | { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) }, |
Tiago Vignatti | bf1e866 | 2012-06-12 14:07:49 +0300 | [diff] [blame] | 993 | { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) }, |
| 994 | { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 995 | { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) }, |
| 996 | { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) }, |
| 997 | { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) }, |
| 998 | { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) }, |
| 999 | { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) }, |
| 1000 | |
| 1001 | { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) }, |
| 1002 | { "_NET_SUPPORTING_WM_CHECK", |
| 1003 | F(atom.net_supporting_wm_check) }, |
| 1004 | { "_NET_SUPPORTED", F(atom.net_supported) }, |
| 1005 | { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) }, |
| 1006 | { "CLIPBOARD", F(atom.clipboard) }, |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 1007 | { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1008 | { "TARGETS", F(atom.targets) }, |
| 1009 | { "UTF8_STRING", F(atom.utf8_string) }, |
| 1010 | { "_WL_SELECTION", F(atom.wl_selection) }, |
| 1011 | { "INCR", F(atom.incr) }, |
| 1012 | { "TIMESTAMP", F(atom.timestamp) }, |
| 1013 | { "MULTIPLE", F(atom.multiple) }, |
| 1014 | { "UTF8_STRING" , F(atom.utf8_string) }, |
| 1015 | { "COMPOUND_TEXT", F(atom.compound_text) }, |
| 1016 | { "TEXT", F(atom.text) }, |
| 1017 | { "STRING", F(atom.string) }, |
| 1018 | { "text/plain;charset=utf-8", F(atom.text_plain_utf8) }, |
| 1019 | { "text/plain", F(atom.text_plain) }, |
| 1020 | }; |
| 1021 | #undef F |
| 1022 | |
| 1023 | xcb_xfixes_query_version_cookie_t xfixes_cookie; |
| 1024 | xcb_xfixes_query_version_reply_t *xfixes_reply; |
| 1025 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
| 1026 | xcb_intern_atom_reply_t *reply; |
| 1027 | xcb_render_query_pict_formats_reply_t *formats_reply; |
| 1028 | xcb_render_query_pict_formats_cookie_t formats_cookie; |
| 1029 | xcb_render_pictforminfo_t *formats; |
| 1030 | uint32_t i; |
| 1031 | |
| 1032 | xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id); |
| 1033 | |
| 1034 | formats_cookie = xcb_render_query_pict_formats(wm->conn); |
| 1035 | |
| 1036 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
| 1037 | cookies[i] = xcb_intern_atom (wm->conn, 0, |
| 1038 | strlen(atoms[i].name), |
| 1039 | atoms[i].name); |
| 1040 | |
| 1041 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
| 1042 | reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL); |
| 1043 | *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom; |
| 1044 | free(reply); |
| 1045 | } |
| 1046 | |
| 1047 | wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id); |
| 1048 | if (!wm->xfixes || !wm->xfixes->present) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1049 | weston_log("xfixes not available\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1050 | |
| 1051 | xfixes_cookie = xcb_xfixes_query_version(wm->conn, |
| 1052 | XCB_XFIXES_MAJOR_VERSION, |
| 1053 | XCB_XFIXES_MINOR_VERSION); |
| 1054 | xfixes_reply = xcb_xfixes_query_version_reply(wm->conn, |
| 1055 | xfixes_cookie, NULL); |
| 1056 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1057 | weston_log("xfixes version: %d.%d\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1058 | xfixes_reply->major_version, xfixes_reply->minor_version); |
| 1059 | |
| 1060 | free(xfixes_reply); |
| 1061 | |
| 1062 | formats_reply = xcb_render_query_pict_formats_reply(wm->conn, |
| 1063 | formats_cookie, 0); |
| 1064 | if (formats_reply == NULL) |
| 1065 | return; |
| 1066 | |
| 1067 | formats = xcb_render_query_pict_formats_formats(formats_reply); |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame^] | 1068 | for (i = 0; i < formats_reply->num_formats; i++) { |
| 1069 | if (formats[i].direct.red_mask != 0xff && |
| 1070 | formats[i].direct.red_shift != 16) |
| 1071 | continue; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1072 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1073 | formats[i].depth == 24) |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame^] | 1074 | wm->format_rgb = formats[i]; |
| 1075 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1076 | formats[i].depth == 32 && |
| 1077 | formats[i].direct.alpha_mask == 0xff && |
| 1078 | formats[i].direct.alpha_shift == 24) |
| 1079 | wm->format_rgba = formats[i]; |
| 1080 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1081 | |
| 1082 | free(formats_reply); |
| 1083 | } |
| 1084 | |
| 1085 | static void |
| 1086 | weston_wm_create_wm_window(struct weston_wm *wm) |
| 1087 | { |
| 1088 | static const char name[] = "Weston WM"; |
| 1089 | |
| 1090 | wm->wm_window = xcb_generate_id(wm->conn); |
| 1091 | xcb_create_window(wm->conn, |
| 1092 | XCB_COPY_FROM_PARENT, |
| 1093 | wm->wm_window, |
| 1094 | wm->screen->root, |
| 1095 | 0, 0, |
| 1096 | 10, 10, |
| 1097 | 0, |
| 1098 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1099 | wm->screen->root_visual, |
| 1100 | 0, NULL); |
| 1101 | |
| 1102 | xcb_change_property(wm->conn, |
| 1103 | XCB_PROP_MODE_REPLACE, |
| 1104 | wm->wm_window, |
| 1105 | wm->atom.net_supporting_wm_check, |
| 1106 | XCB_ATOM_WINDOW, |
| 1107 | 32, /* format */ |
| 1108 | 1, &wm->wm_window); |
| 1109 | |
| 1110 | xcb_change_property(wm->conn, |
| 1111 | XCB_PROP_MODE_REPLACE, |
| 1112 | wm->wm_window, |
| 1113 | wm->atom.net_wm_name, |
| 1114 | wm->atom.utf8_string, |
| 1115 | 8, /* format */ |
| 1116 | strlen(name), name); |
| 1117 | |
| 1118 | xcb_change_property(wm->conn, |
| 1119 | XCB_PROP_MODE_REPLACE, |
| 1120 | wm->screen->root, |
| 1121 | wm->atom.net_supporting_wm_check, |
| 1122 | XCB_ATOM_WINDOW, |
| 1123 | 32, /* format */ |
| 1124 | 1, &wm->wm_window); |
| 1125 | |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 1126 | /* Claim the WM_S0 selection even though we don't suport |
| 1127 | * the --replace functionality. */ |
| 1128 | xcb_set_selection_owner(wm->conn, |
| 1129 | wm->wm_window, |
| 1130 | wm->atom.wm_s0, |
| 1131 | XCB_TIME_CURRENT_TIME); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | struct weston_wm * |
| 1135 | weston_wm_create(struct weston_xserver *wxs) |
| 1136 | { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1137 | struct weston_wm *wm; |
| 1138 | struct wl_event_loop *loop; |
| 1139 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1140 | uint32_t values[1]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1141 | int sv[2]; |
| 1142 | xcb_atom_t supported[1]; |
| 1143 | |
| 1144 | wm = malloc(sizeof *wm); |
| 1145 | if (wm == NULL) |
| 1146 | return NULL; |
| 1147 | |
| 1148 | memset(wm, 0, sizeof *wm); |
| 1149 | wm->server = wxs; |
| 1150 | wm->window_hash = hash_table_create(); |
| 1151 | if (wm->window_hash == NULL) { |
| 1152 | free(wm); |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | |
| 1156 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1157 | weston_log("socketpair failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1158 | hash_table_destroy(wm->window_hash); |
| 1159 | free(wm); |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | xserver_send_client(wxs->resource, sv[1]); |
| 1164 | wl_client_flush(wxs->resource->client); |
| 1165 | close(sv[1]); |
| 1166 | |
| 1167 | /* xcb_connect_to_fd takes ownership of the fd. */ |
| 1168 | wm->conn = xcb_connect_to_fd(sv[0], NULL); |
| 1169 | if (xcb_connection_has_error(wm->conn)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1170 | weston_log("xcb_connect_to_fd failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1171 | close(sv[0]); |
| 1172 | hash_table_destroy(wm->window_hash); |
| 1173 | free(wm); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn)); |
| 1178 | wm->screen = s.data; |
| 1179 | |
| 1180 | loop = wl_display_get_event_loop(wxs->wl_display); |
| 1181 | wm->source = |
| 1182 | wl_event_loop_add_fd(loop, sv[0], |
| 1183 | WL_EVENT_READABLE, |
| 1184 | weston_wm_handle_event, wm); |
| 1185 | wl_event_source_check(wm->source); |
| 1186 | |
| 1187 | wxs_wm_get_resources(wm); |
| 1188 | |
| 1189 | values[0] = |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1190 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 1191 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
| 1192 | XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1193 | xcb_change_window_attributes(wm->conn, wm->screen->root, |
| 1194 | XCB_CW_EVENT_MASK, values); |
| 1195 | wm->theme = theme_create(); |
| 1196 | |
| 1197 | weston_wm_create_wm_window(wm); |
| 1198 | |
| 1199 | supported[0] = wm->atom.net_wm_moveresize; |
| 1200 | xcb_change_property(wm->conn, |
| 1201 | XCB_PROP_MODE_REPLACE, |
| 1202 | wm->screen->root, |
| 1203 | wm->atom.net_supported, |
| 1204 | XCB_ATOM_ATOM, |
| 1205 | 32, /* format */ |
| 1206 | ARRAY_LENGTH(supported), supported); |
| 1207 | |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1208 | weston_wm_selection_init(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1209 | |
| 1210 | xcb_flush(wm->conn); |
| 1211 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1212 | wm->activate_listener.notify = weston_wm_window_activate; |
| 1213 | wl_signal_add(&wxs->compositor->activate_signal, |
| 1214 | &wm->activate_listener); |
| 1215 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1216 | weston_log("created wm\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1217 | |
| 1218 | return wm; |
| 1219 | } |
| 1220 | |
| 1221 | void |
| 1222 | weston_wm_destroy(struct weston_wm *wm) |
| 1223 | { |
| 1224 | /* FIXME: Free windows in hash. */ |
| 1225 | hash_table_destroy(wm->window_hash); |
| 1226 | xcb_disconnect(wm->conn); |
| 1227 | wl_event_source_remove(wm->source); |
| 1228 | wl_list_remove(&wm->selection_listener.link); |
| 1229 | wl_list_remove(&wm->activate_listener.link); |
| 1230 | |
| 1231 | free(wm); |
| 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | surface_destroy(struct wl_listener *listener, void *data) |
| 1236 | { |
| 1237 | struct weston_wm_window *window = |
| 1238 | container_of(listener, |
| 1239 | struct weston_wm_window, surface_destroy_listener); |
| 1240 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1241 | weston_log("surface for xid %d destroyed\n", window->id); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | static struct weston_wm_window * |
| 1245 | get_wm_window(struct weston_surface *surface) |
| 1246 | { |
| 1247 | struct wl_resource *resource = &surface->surface.resource; |
| 1248 | struct wl_listener *listener; |
| 1249 | |
| 1250 | listener = wl_signal_get(&resource->destroy_signal, surface_destroy); |
| 1251 | if (listener) |
| 1252 | return container_of(listener, struct weston_wm_window, |
| 1253 | surface_destroy_listener); |
| 1254 | |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | |
| 1258 | static void |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1259 | weston_wm_window_configure(void *data) |
| 1260 | { |
| 1261 | struct weston_wm_window *window = data; |
| 1262 | struct weston_wm *wm = window->wm; |
| 1263 | uint32_t values[2]; |
| 1264 | int width, height; |
| 1265 | |
| 1266 | values[0] = window->width; |
| 1267 | values[1] = window->height; |
| 1268 | xcb_configure_window(wm->conn, |
| 1269 | window->id, |
| 1270 | XCB_CONFIG_WINDOW_WIDTH | |
| 1271 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1272 | values); |
| 1273 | |
| 1274 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1275 | values[0] = width; |
| 1276 | values[1] = height; |
| 1277 | xcb_configure_window(wm->conn, |
| 1278 | window->frame_id, |
| 1279 | XCB_CONFIG_WINDOW_WIDTH | |
| 1280 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1281 | values); |
| 1282 | |
| 1283 | window->configure_source = NULL; |
| 1284 | |
| 1285 | weston_wm_window_schedule_repaint(window); |
| 1286 | } |
| 1287 | |
| 1288 | static void |
| 1289 | send_configure(struct weston_surface *surface, |
| 1290 | uint32_t edges, int32_t width, int32_t height) |
| 1291 | { |
| 1292 | struct weston_wm_window *window = get_wm_window(surface); |
| 1293 | struct weston_wm *wm = window->wm; |
| 1294 | struct theme *t = window->wm->theme; |
| 1295 | |
| 1296 | if (window->decorate) { |
| 1297 | window->width = width - 2 * (t->margin + t->width); |
| 1298 | window->height = height - 2 * t->margin - |
| 1299 | t->titlebar_height - t->width; |
| 1300 | } else { |
| 1301 | window->width = width - 2 * t->margin; |
| 1302 | window->height = height - 2 * t->margin; |
| 1303 | } |
| 1304 | |
| 1305 | if (window->configure_source) |
| 1306 | return; |
| 1307 | |
| 1308 | window->configure_source = |
| 1309 | wl_event_loop_add_idle(wm->server->loop, |
| 1310 | weston_wm_window_configure, window); |
| 1311 | } |
| 1312 | |
| 1313 | static const struct weston_shell_client shell_client = { |
| 1314 | send_configure |
| 1315 | }; |
| 1316 | |
| 1317 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1318 | xserver_map_shell_surface(struct weston_wm *wm, |
| 1319 | struct weston_wm_window *window) |
| 1320 | { |
| 1321 | struct weston_shell_interface *shell_interface = |
| 1322 | &wm->server->compositor->shell_interface; |
| 1323 | struct weston_wm_window *parent; |
| 1324 | struct theme *t = window->wm->theme; |
| 1325 | |
| 1326 | if (!shell_interface->create_shell_surface) |
| 1327 | return; |
| 1328 | |
| 1329 | window->shsurf = |
| 1330 | shell_interface->create_shell_surface(shell_interface->shell, |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1331 | window->surface, |
| 1332 | &shell_client); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1333 | |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1334 | /* ICCCM 4.1.1 */ |
Kristian Høgsberg | cd80f10 | 2012-06-15 15:40:18 -0400 | [diff] [blame] | 1335 | if (!window->override_redirect || !window->transient_for) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1336 | shell_interface->set_toplevel(window->shsurf); |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | parent = hash_table_lookup(wm->window_hash, window->transient_for->id); |
Kristian Høgsberg | 8150b19 | 2012-06-27 10:22:58 -0400 | [diff] [blame] | 1341 | shell_interface->set_transient(window->shsurf, parent->surface, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1342 | window->x - parent->x + t->margin + t->width, |
| 1343 | window->y - parent->y + t->margin + t->titlebar_height, |
| 1344 | WL_SHELL_SURFACE_TRANSIENT_INACTIVE); |
| 1345 | } |
| 1346 | |
| 1347 | static void |
| 1348 | xserver_set_window_id(struct wl_client *client, struct wl_resource *resource, |
| 1349 | struct wl_resource *surface_resource, uint32_t id) |
| 1350 | { |
| 1351 | struct weston_xserver *wxs = resource->data; |
| 1352 | struct weston_wm *wm = wxs->wm; |
| 1353 | struct wl_surface *surface = surface_resource->data; |
| 1354 | struct weston_wm_window *window; |
| 1355 | |
| 1356 | if (client != wxs->client) |
| 1357 | return; |
| 1358 | |
| 1359 | window = hash_table_lookup(wm->window_hash, id); |
| 1360 | if (window == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1361 | weston_log("set_window_id for unknown window %d\n", id); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1362 | return; |
| 1363 | } |
| 1364 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1365 | weston_log("set_window_id %d for surface %p\n", id, surface); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1366 | |
| 1367 | weston_wm_window_read_properties(window); |
| 1368 | |
| 1369 | window->surface = (struct weston_surface *) surface; |
| 1370 | window->surface_destroy_listener.notify = surface_destroy; |
| 1371 | wl_signal_add(&surface->resource.destroy_signal, |
| 1372 | &window->surface_destroy_listener); |
| 1373 | |
| 1374 | weston_wm_window_schedule_repaint(window); |
| 1375 | xserver_map_shell_surface(wm, window); |
| 1376 | } |
| 1377 | |
| 1378 | const struct xserver_interface xserver_implementation = { |
| 1379 | xserver_set_window_id |
| 1380 | }; |