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" |
| 39 | #include "xserver-server-protocol.h" |
| 40 | #include "hash.h" |
| 41 | |
| 42 | struct motif_wm_hints { |
| 43 | uint32_t flags; |
| 44 | uint32_t functions; |
| 45 | uint32_t decorations; |
| 46 | int32_t input_mode; |
| 47 | uint32_t status; |
| 48 | }; |
| 49 | |
| 50 | #define MWM_HINTS_FUNCTIONS (1L << 0) |
| 51 | #define MWM_HINTS_DECORATIONS (1L << 1) |
| 52 | #define MWM_HINTS_INPUT_MODE (1L << 2) |
| 53 | #define MWM_HINTS_STATUS (1L << 3) |
| 54 | |
| 55 | #define MWM_FUNC_ALL (1L << 0) |
| 56 | #define MWM_FUNC_RESIZE (1L << 1) |
| 57 | #define MWM_FUNC_MOVE (1L << 2) |
| 58 | #define MWM_FUNC_MINIMIZE (1L << 3) |
| 59 | #define MWM_FUNC_MAXIMIZE (1L << 4) |
| 60 | #define MWM_FUNC_CLOSE (1L << 5) |
| 61 | |
| 62 | #define MWM_DECOR_ALL (1L << 0) |
| 63 | #define MWM_DECOR_BORDER (1L << 1) |
| 64 | #define MWM_DECOR_RESIZEH (1L << 2) |
| 65 | #define MWM_DECOR_TITLE (1L << 3) |
| 66 | #define MWM_DECOR_MENU (1L << 4) |
| 67 | #define MWM_DECOR_MINIMIZE (1L << 5) |
| 68 | #define MWM_DECOR_MAXIMIZE (1L << 6) |
| 69 | |
| 70 | #define MWM_INPUT_MODELESS 0 |
| 71 | #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1 |
| 72 | #define MWM_INPUT_SYSTEM_MODAL 2 |
| 73 | #define MWM_INPUT_FULL_APPLICATION_MODAL 3 |
| 74 | #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL |
| 75 | |
| 76 | #define MWM_TEAROFF_WINDOW (1L<<0) |
| 77 | |
| 78 | #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0 |
| 79 | #define _NET_WM_MOVERESIZE_SIZE_TOP 1 |
| 80 | #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2 |
| 81 | #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3 |
| 82 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4 |
| 83 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5 |
| 84 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6 |
| 85 | #define _NET_WM_MOVERESIZE_SIZE_LEFT 7 |
| 86 | #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */ |
| 87 | #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */ |
| 88 | #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */ |
| 89 | #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */ |
| 90 | |
| 91 | |
| 92 | |
| 93 | struct weston_wm_window { |
| 94 | struct weston_wm *wm; |
| 95 | xcb_window_t id; |
| 96 | xcb_window_t frame_id; |
| 97 | cairo_surface_t *cairo_surface; |
| 98 | struct weston_surface *surface; |
| 99 | struct shell_surface *shsurf; |
| 100 | struct wl_listener surface_destroy_listener; |
| 101 | struct wl_event_source *repaint_source; |
| 102 | int properties_dirty; |
| 103 | char *class; |
| 104 | char *name; |
| 105 | struct weston_wm_window *transient_for; |
| 106 | uint32_t protocols; |
| 107 | xcb_atom_t type; |
| 108 | int width, height; |
| 109 | int x, y; |
| 110 | int decorate; |
| 111 | }; |
| 112 | |
| 113 | static struct weston_wm_window * |
| 114 | get_wm_window(struct weston_surface *surface); |
| 115 | |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 116 | static void |
| 117 | weston_wm_window_schedule_repaint(struct weston_wm_window *window); |
| 118 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 119 | const char * |
| 120 | get_atom_name(xcb_connection_t *c, xcb_atom_t atom) |
| 121 | { |
| 122 | xcb_get_atom_name_cookie_t cookie; |
| 123 | xcb_get_atom_name_reply_t *reply; |
| 124 | xcb_generic_error_t *e; |
| 125 | static char buffer[64]; |
| 126 | |
| 127 | if (atom == XCB_ATOM_NONE) |
| 128 | return "None"; |
| 129 | |
| 130 | cookie = xcb_get_atom_name (c, atom); |
| 131 | reply = xcb_get_atom_name_reply (c, cookie, &e); |
| 132 | snprintf(buffer, sizeof buffer, "%.*s", |
| 133 | xcb_get_atom_name_name_length (reply), |
| 134 | xcb_get_atom_name_name (reply)); |
| 135 | free(reply); |
| 136 | |
| 137 | return buffer; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | dump_property(struct weston_wm *wm, xcb_atom_t property, |
| 142 | xcb_get_property_reply_t *reply) |
| 143 | { |
| 144 | int32_t *incr_value; |
| 145 | const char *text_value, *name; |
| 146 | xcb_atom_t *atom_value; |
| 147 | int width, len; |
| 148 | uint32_t i; |
| 149 | |
| 150 | width = fprintf(stderr, " %s: ", get_atom_name(wm->conn, property)); |
| 151 | if (reply == NULL) { |
| 152 | fprintf(stderr, "(no reply)\n"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | width += fprintf(stderr, |
| 157 | "type %s, format %d, length %d (value_len %d): ", |
| 158 | get_atom_name(wm->conn, reply->type), |
| 159 | reply->format, |
| 160 | xcb_get_property_value_length(reply), |
| 161 | reply->value_len); |
| 162 | |
| 163 | if (reply->type == wm->atom.incr) { |
| 164 | incr_value = xcb_get_property_value(reply); |
| 165 | fprintf(stderr, "%d\n", *incr_value); |
| 166 | } else if (reply->type == wm->atom.utf8_string || |
| 167 | reply->type == wm->atom.string) { |
| 168 | text_value = xcb_get_property_value(reply); |
| 169 | if (reply->value_len > 40) |
| 170 | len = 40; |
| 171 | else |
| 172 | len = reply->value_len; |
| 173 | fprintf(stderr, "\"%.*s\"\n", len, text_value); |
| 174 | } else if (reply->type == XCB_ATOM_ATOM) { |
| 175 | atom_value = xcb_get_property_value(reply); |
| 176 | for (i = 0; i < reply->value_len; i++) { |
| 177 | name = get_atom_name(wm->conn, atom_value[i]); |
| 178 | if (width + strlen(name) + 2 > 78) { |
| 179 | fprintf(stderr, "\n "); |
| 180 | width = 4; |
| 181 | } else if (i > 0) { |
| 182 | width += fprintf(stderr, ", "); |
| 183 | } |
| 184 | |
| 185 | width += fprintf(stderr, "%s", name); |
| 186 | } |
| 187 | fprintf(stderr, "\n"); |
| 188 | } else { |
| 189 | fprintf(stderr, "huh?\n"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static void |
| 194 | dump_window_properties(struct weston_wm *wm, xcb_window_t window) |
| 195 | { |
| 196 | xcb_list_properties_cookie_t list_cookie; |
| 197 | xcb_list_properties_reply_t *list_reply; |
| 198 | xcb_get_property_cookie_t property_cookie; |
| 199 | xcb_get_property_reply_t *property_reply; |
| 200 | xcb_atom_t *atoms; |
| 201 | int i, length; |
| 202 | |
| 203 | list_cookie = xcb_list_properties(wm->conn, window); |
| 204 | list_reply = xcb_list_properties_reply(wm->conn, list_cookie, NULL); |
| 205 | if (!list_reply) |
| 206 | /* Bad window, typically */ |
| 207 | return; |
| 208 | |
| 209 | length = xcb_list_properties_atoms_length(list_reply); |
| 210 | atoms = xcb_list_properties_atoms(list_reply); |
| 211 | |
| 212 | for (i = 0; i < length; i++) { |
| 213 | property_cookie = |
| 214 | xcb_get_property(wm->conn, |
| 215 | 0, /* delete */ |
| 216 | window, |
| 217 | atoms[i], |
| 218 | XCB_ATOM_ANY, |
| 219 | 0, 2048); |
| 220 | |
| 221 | property_reply = xcb_get_property_reply(wm->conn, |
| 222 | property_cookie, NULL); |
| 223 | dump_property(wm, atoms[i], property_reply); |
| 224 | |
| 225 | free(property_reply); |
| 226 | } |
| 227 | |
| 228 | free(list_reply); |
| 229 | } |
| 230 | |
| 231 | /* We reuse some predefined, but otherwise useles atoms */ |
| 232 | #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0 |
| 233 | #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1 |
| 234 | |
| 235 | static void |
| 236 | weston_wm_window_read_properties(struct weston_wm_window *window) |
| 237 | { |
| 238 | struct weston_wm *wm = window->wm; |
| 239 | |
| 240 | #define F(field) offsetof(struct weston_wm_window, field) |
| 241 | const struct { |
| 242 | xcb_atom_t atom; |
| 243 | xcb_atom_t type; |
| 244 | int offset; |
| 245 | } props[] = { |
| 246 | { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) }, |
| 247 | { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) }, |
| 248 | { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) }, |
| 249 | { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) }, |
| 250 | { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) }, |
| 251 | { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) }, |
| 252 | { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 }, |
| 253 | }; |
| 254 | #undef F |
| 255 | |
| 256 | xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)]; |
| 257 | xcb_get_property_reply_t *reply; |
| 258 | void *p; |
| 259 | uint32_t *xid; |
| 260 | xcb_atom_t *atom; |
| 261 | uint32_t i; |
| 262 | struct motif_wm_hints *hints; |
| 263 | |
| 264 | if (!window->properties_dirty) |
| 265 | return; |
| 266 | window->properties_dirty = 0; |
| 267 | |
| 268 | dump_window_properties(wm, window->id); |
| 269 | |
| 270 | for (i = 0; i < ARRAY_LENGTH(props); i++) |
| 271 | cookie[i] = xcb_get_property(wm->conn, |
| 272 | 0, /* delete */ |
| 273 | window->id, |
| 274 | props[i].atom, |
| 275 | XCB_ATOM_ANY, 0, 2048); |
| 276 | |
| 277 | window->decorate = 1; |
| 278 | for (i = 0; i < ARRAY_LENGTH(props); i++) { |
| 279 | reply = xcb_get_property_reply(wm->conn, cookie[i], NULL); |
| 280 | if (!reply) |
| 281 | /* Bad window, typically */ |
| 282 | continue; |
| 283 | if (reply->type == XCB_ATOM_NONE) { |
| 284 | /* No such property */ |
| 285 | free(reply); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | p = ((char *) window + props[i].offset); |
| 290 | |
| 291 | switch (props[i].type) { |
| 292 | case XCB_ATOM_STRING: |
| 293 | /* FIXME: We're using this for both string and |
| 294 | utf8_string */ |
| 295 | if (*(char **) p) |
| 296 | free(*(char **) p); |
| 297 | |
| 298 | *(char **) p = |
| 299 | strndup(xcb_get_property_value(reply), |
| 300 | xcb_get_property_value_length(reply)); |
| 301 | break; |
| 302 | case XCB_ATOM_WINDOW: |
| 303 | xid = xcb_get_property_value(reply); |
| 304 | *(struct weston_wm_window **) p = |
| 305 | hash_table_lookup(wm->window_hash, *xid); |
| 306 | break; |
| 307 | case XCB_ATOM_ATOM: |
| 308 | atom = xcb_get_property_value(reply); |
| 309 | *(xcb_atom_t *) p = *atom; |
| 310 | break; |
| 311 | case TYPE_WM_PROTOCOLS: |
| 312 | break; |
| 313 | case TYPE_MOTIF_WM_HINTS: |
| 314 | hints = xcb_get_property_value(reply); |
| 315 | if (hints->flags & MWM_HINTS_DECORATIONS) |
| 316 | window->decorate = hints->decorations > 0; |
| 317 | break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | free(reply); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 326 | weston_wm_window_get_frame_size(struct weston_wm_window *window, |
| 327 | int *width, int *height) |
| 328 | { |
| 329 | struct theme *t = window->wm->theme; |
| 330 | |
| 331 | if (window->decorate) { |
| 332 | *width = window->width + (t->margin + t->width) * 2; |
| 333 | *height = window->height + |
| 334 | t->margin * 2 + t->width + t->titlebar_height; |
| 335 | } else { |
| 336 | *width = window->width + t->margin * 2; |
| 337 | *height = window->height + t->margin * 2; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | weston_wm_window_get_child_position(struct weston_wm_window *window, |
| 343 | int *x, int *y) |
| 344 | { |
| 345 | struct theme *t = window->wm->theme; |
| 346 | |
| 347 | if (window->decorate) { |
| 348 | *x = t->margin + t->width; |
| 349 | *y = t->margin + t->titlebar_height; |
| 350 | } else { |
| 351 | *x = t->margin; |
| 352 | *y = t->margin; |
| 353 | } |
| 354 | } |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 355 | |
| 356 | static void |
| 357 | weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 358 | { |
| 359 | xcb_configure_request_event_t *configure_request = |
| 360 | (xcb_configure_request_event_t *) event; |
| 361 | struct weston_wm_window *window; |
| 362 | uint32_t mask, values[16]; |
| 363 | int x, y, width, height, i = 0; |
| 364 | |
| 365 | fprintf(stderr, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n", |
| 366 | configure_request->window, |
| 367 | configure_request->x, configure_request->y, |
| 368 | configure_request->width, configure_request->height); |
| 369 | |
| 370 | window = hash_table_lookup(wm->window_hash, configure_request->window); |
| 371 | |
| 372 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH) |
| 373 | window->width = configure_request->width; |
| 374 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT) |
| 375 | window->height = configure_request->height; |
| 376 | |
| 377 | weston_wm_window_get_child_position(window, &x, &y); |
| 378 | values[i++] = x; |
| 379 | values[i++] = y; |
| 380 | values[i++] = window->width; |
| 381 | values[i++] = window->height; |
| 382 | values[i++] = 0; |
| 383 | mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | |
| 384 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | |
| 385 | XCB_CONFIG_WINDOW_BORDER_WIDTH; |
| 386 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) { |
| 387 | values[i++] = configure_request->sibling; |
| 388 | mask |= XCB_CONFIG_WINDOW_SIBLING; |
| 389 | } |
| 390 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) { |
| 391 | values[i++] = configure_request->stack_mode; |
| 392 | mask |= XCB_CONFIG_WINDOW_STACK_MODE; |
| 393 | } |
| 394 | |
| 395 | xcb_configure_window(wm->conn, window->id, mask, values); |
| 396 | |
| 397 | weston_wm_window_get_frame_size(window, &width, &height); |
| 398 | values[0] = width; |
| 399 | values[1] = height; |
| 400 | mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; |
| 401 | xcb_configure_window(wm->conn, window->frame_id, mask, values); |
| 402 | |
| 403 | weston_wm_window_schedule_repaint(window); |
| 404 | } |
| 405 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 406 | static void |
| 407 | weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 408 | { |
| 409 | xcb_configure_notify_event_t *configure_notify = |
| 410 | (xcb_configure_notify_event_t *) event; |
| 411 | struct weston_wm_window *window; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 412 | |
| 413 | window = hash_table_lookup(wm->window_hash, configure_notify->window); |
| 414 | |
| 415 | fprintf(stderr, "XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n", |
| 416 | configure_notify->window == window->id ? "client" : "frame", |
| 417 | configure_notify->window, |
| 418 | configure_notify->x, configure_notify->y, |
| 419 | configure_notify->width, configure_notify->height); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void |
| 423 | weston_wm_activate(struct weston_wm *wm, |
| 424 | struct weston_wm_window *window, xcb_timestamp_t time) |
| 425 | { |
| 426 | xcb_client_message_event_t client_message; |
| 427 | |
| 428 | client_message.response_type = XCB_CLIENT_MESSAGE; |
| 429 | client_message.format = 32; |
| 430 | client_message.window = window->id; |
| 431 | client_message.type = wm->atom.wm_protocols; |
| 432 | client_message.data.data32[0] = wm->atom.wm_take_focus; |
| 433 | client_message.data.data32[1] = XCB_TIME_CURRENT_TIME; |
| 434 | |
| 435 | xcb_send_event(wm->conn, 0, window->id, |
| 436 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 437 | (char *) &client_message); |
| 438 | |
| 439 | xcb_set_input_focus (wm->conn, |
| 440 | XCB_INPUT_FOCUS_POINTER_ROOT, window->id, time); |
| 441 | } |
| 442 | |
| 443 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 444 | weston_wm_window_activate(struct wl_listener *listener, void *data) |
| 445 | { |
| 446 | struct weston_surface *surface = data; |
| 447 | struct weston_wm_window *window = get_wm_window(surface); |
Scott Moreau | 1b3984c | 2012-05-21 15:49:13 -0600 | [diff] [blame^] | 448 | struct weston_wm *wm = container_of(listener, struct weston_wm, activate_listener); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 449 | |
| 450 | if (window) |
| 451 | weston_wm_activate(wm, window, XCB_TIME_CURRENT_TIME); |
| 452 | else |
| 453 | xcb_set_input_focus (wm->conn, |
| 454 | XCB_INPUT_FOCUS_POINTER_ROOT, |
| 455 | XCB_NONE, |
| 456 | XCB_TIME_CURRENT_TIME); |
| 457 | |
| 458 | if (wm->focus_window) |
| 459 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 460 | wm->focus_window = window; |
| 461 | if (wm->focus_window) |
| 462 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 463 | } |
| 464 | |
| 465 | static int |
| 466 | our_resource(struct weston_wm *wm, uint32_t id) |
| 467 | { |
| 468 | const xcb_setup_t *setup; |
| 469 | |
| 470 | setup = xcb_get_setup(wm->conn); |
| 471 | |
| 472 | return (id & ~setup->resource_id_mask) == setup->resource_id_base; |
| 473 | } |
| 474 | |
| 475 | static void |
| 476 | weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 477 | { |
| 478 | xcb_map_request_event_t *map_request = |
| 479 | (xcb_map_request_event_t *) event; |
| 480 | struct weston_wm_window *window; |
| 481 | uint32_t values[1]; |
| 482 | int x, y, width, height; |
| 483 | |
| 484 | if (our_resource(wm, map_request->window)) { |
| 485 | fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n", |
| 486 | map_request->window); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | window = hash_table_lookup(wm->window_hash, map_request->window); |
| 491 | |
| 492 | weston_wm_window_read_properties(window); |
| 493 | |
| 494 | weston_wm_window_get_frame_size(window, &width, &height); |
| 495 | weston_wm_window_get_child_position(window, &x, &y); |
| 496 | |
| 497 | values[0] = |
| 498 | XCB_EVENT_MASK_KEY_PRESS | |
| 499 | XCB_EVENT_MASK_KEY_RELEASE | |
| 500 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 501 | XCB_EVENT_MASK_BUTTON_RELEASE | |
| 502 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 503 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | |
| 504 | XCB_EVENT_MASK_RESIZE_REDIRECT | |
| 505 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 506 | XCB_EVENT_MASK_EXPOSURE; |
| 507 | |
| 508 | window->frame_id = xcb_generate_id(wm->conn); |
| 509 | xcb_create_window(wm->conn, |
| 510 | XCB_COPY_FROM_PARENT, |
| 511 | window->frame_id, |
| 512 | wm->screen->root, |
| 513 | 0, 0, |
| 514 | width, height, |
| 515 | 0, |
| 516 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 517 | wm->screen->root_visual, |
| 518 | XCB_CW_EVENT_MASK, values); |
| 519 | xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y); |
| 520 | |
| 521 | values[0] = 0; |
| 522 | xcb_configure_window(wm->conn, window->id, |
| 523 | XCB_CONFIG_WINDOW_BORDER_WIDTH, values); |
| 524 | |
| 525 | fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n", |
| 526 | window->id, window, window->frame_id); |
| 527 | |
| 528 | xcb_change_save_set(wm->conn, XCB_SET_MODE_DELETE, window->id); |
| 529 | |
| 530 | xcb_map_window(wm->conn, map_request->window); |
| 531 | xcb_map_window(wm->conn, window->frame_id); |
| 532 | |
| 533 | window->cairo_surface = |
| 534 | cairo_xcb_surface_create_with_xrender_format(wm->conn, |
| 535 | wm->screen, |
| 536 | window->frame_id, |
| 537 | &wm->render_format, |
| 538 | width, height); |
| 539 | |
| 540 | hash_table_insert(wm->window_hash, window->frame_id, window); |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 545 | { |
| 546 | xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event; |
| 547 | |
| 548 | if (our_resource(wm, map_notify->window)) { |
| 549 | fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n", |
| 550 | map_notify->window); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window); |
| 555 | } |
| 556 | |
| 557 | static void |
| 558 | weston_wm_window_draw_decoration(void *data) |
| 559 | { |
| 560 | struct weston_wm_window *window = data; |
| 561 | struct weston_wm *wm = window->wm; |
| 562 | struct theme *t = wm->theme; |
| 563 | cairo_t *cr; |
| 564 | int x, y, width, height; |
| 565 | const char *title; |
| 566 | uint32_t flags = 0; |
| 567 | |
| 568 | weston_wm_window_read_properties(window); |
| 569 | |
| 570 | window->repaint_source = NULL; |
| 571 | |
| 572 | weston_wm_window_get_frame_size(window, &width, &height); |
| 573 | weston_wm_window_get_child_position(window, &x, &y); |
| 574 | |
| 575 | cairo_xcb_surface_set_size(window->cairo_surface, width, height); |
| 576 | cr = cairo_create(window->cairo_surface); |
| 577 | |
| 578 | if (window->decorate) { |
| 579 | if (wm->focus_window == window) |
| 580 | flags |= THEME_FRAME_ACTIVE; |
| 581 | |
| 582 | if (window->name) |
| 583 | title = window->name; |
| 584 | else |
| 585 | title = "untitled"; |
| 586 | |
| 587 | theme_render_frame(t, cr, width, height, title, flags); |
| 588 | } else { |
| 589 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 590 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 591 | cairo_paint(cr); |
| 592 | |
| 593 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 594 | cairo_set_source_rgba(cr, 0, 0, 0, 0.45); |
| 595 | tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64); |
| 596 | } |
| 597 | |
| 598 | cairo_destroy(cr); |
| 599 | |
| 600 | if (window->surface) { |
| 601 | /* We leave an extra pixel around the X window area to |
| 602 | * make sure we don't sample from the undefined alpha |
| 603 | * channel when filtering. */ |
| 604 | window->surface->opaque_rect[0] = |
| 605 | (double) (x - 1) / width; |
| 606 | window->surface->opaque_rect[1] = |
| 607 | (double) (x + window->width + 1) / width; |
| 608 | window->surface->opaque_rect[2] = |
| 609 | (double) (y - 1) / height; |
| 610 | window->surface->opaque_rect[3] = |
| 611 | (double) (y + window->height + 1) / height; |
| 612 | |
| 613 | pixman_region32_init_rect(&window->surface->input, |
| 614 | t->margin, t->margin, |
| 615 | width - 2 * t->margin, |
| 616 | height - 2 * t->margin); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | static void |
| 621 | weston_wm_window_schedule_repaint(struct weston_wm_window *window) |
| 622 | { |
| 623 | struct weston_wm *wm = window->wm; |
| 624 | |
| 625 | if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source) |
| 626 | return; |
| 627 | |
| 628 | window->repaint_source = |
| 629 | wl_event_loop_add_idle(wm->server->loop, |
| 630 | weston_wm_window_draw_decoration, |
| 631 | window); |
| 632 | } |
| 633 | |
| 634 | static void |
| 635 | weston_wm_handle_expose(struct weston_wm *wm, xcb_generic_event_t *event) |
| 636 | { |
| 637 | struct weston_wm_window *window; |
| 638 | xcb_expose_event_t *expose = (xcb_expose_event_t *) event; |
| 639 | |
| 640 | window = hash_table_lookup(wm->window_hash, expose->window); |
| 641 | fprintf(stderr, "XCB_EXPOSE (window %d, title %s, surface %p)\n", |
| 642 | window->id, window->name, window->surface); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 643 | } |
| 644 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 645 | static void |
| 646 | weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 647 | { |
| 648 | xcb_property_notify_event_t *property_notify = |
| 649 | (xcb_property_notify_event_t *) event; |
| 650 | struct weston_wm_window *window; |
| 651 | |
| 652 | window = hash_table_lookup(wm->window_hash, property_notify->window); |
| 653 | if (window) |
| 654 | window->properties_dirty = 1; |
| 655 | |
| 656 | if (property_notify->atom == XCB_ATOM_WM_CLASS) { |
| 657 | fprintf(stderr, "wm_class changed\n"); |
| 658 | } else if (property_notify->atom == XCB_ATOM_WM_TRANSIENT_FOR) { |
| 659 | fprintf(stderr, "wm_transient_for changed\n"); |
| 660 | } else if (property_notify->atom == wm->atom.wm_protocols) { |
| 661 | fprintf(stderr, "wm_protocols changed\n"); |
| 662 | } else if (property_notify->atom == wm->atom.net_wm_name) { |
| 663 | fprintf(stderr, "_net_wm_name changed\n"); |
| 664 | weston_wm_window_schedule_repaint(window); |
| 665 | } else if (property_notify->atom == wm->atom.net_wm_user_time) { |
| 666 | fprintf(stderr, "_net_wm_user_time changed\n"); |
| 667 | } else if (property_notify->atom == wm->atom.net_wm_icon_name) { |
| 668 | fprintf(stderr, "_net_wm_icon_name changed\n"); |
| 669 | } else if (property_notify->atom == XCB_ATOM_WM_NAME) { |
| 670 | fprintf(stderr, "wm_name changed\n"); |
| 671 | weston_wm_window_schedule_repaint(window); |
| 672 | } else if (property_notify->atom == XCB_ATOM_WM_ICON_NAME) { |
| 673 | fprintf(stderr, "wm_icon_name changed\n"); |
| 674 | } else { |
| 675 | fprintf(stderr, "XCB_PROPERTY_NOTIFY: " |
| 676 | "unhandled property change: %s\n", |
| 677 | get_atom_name(wm->conn, property_notify->atom)); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | static void |
| 682 | weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 683 | { |
| 684 | xcb_create_notify_event_t *create_notify = |
| 685 | (xcb_create_notify_event_t *) event; |
| 686 | struct weston_wm_window *window; |
| 687 | uint32_t values[1]; |
| 688 | |
| 689 | fprintf(stderr, |
| 690 | "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n", |
| 691 | create_notify->window, |
| 692 | create_notify->width, create_notify->height, |
| 693 | create_notify->override_redirect ? ", override" : "", |
| 694 | our_resource(wm, create_notify->window) ? ", ours" : ""); |
| 695 | |
| 696 | if (our_resource(wm, create_notify->window)) |
| 697 | return; |
| 698 | |
| 699 | window = malloc(sizeof *window); |
| 700 | if (window == NULL) { |
| 701 | fprintf(stderr, "failed to allocate window\n"); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 706 | xcb_change_window_attributes(wm->conn, create_notify->window, |
| 707 | XCB_CW_EVENT_MASK, values); |
| 708 | |
| 709 | memset(window, 0, sizeof *window); |
| 710 | window->wm = wm; |
| 711 | window->id = create_notify->window; |
| 712 | window->properties_dirty = 1; |
| 713 | |
| 714 | window->width = create_notify->width; |
| 715 | window->height = create_notify->height; |
| 716 | |
| 717 | hash_table_insert(wm->window_hash, window->id, window); |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 722 | { |
| 723 | xcb_destroy_notify_event_t *destroy_notify = |
| 724 | (xcb_destroy_notify_event_t *) event; |
| 725 | struct weston_wm_window *window; |
| 726 | |
| 727 | if (our_resource(wm, destroy_notify->window)) { |
| 728 | fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (ours)\n", |
| 729 | destroy_notify->window); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | window = hash_table_lookup(wm->window_hash, destroy_notify->window); |
| 734 | |
| 735 | fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (%p)\n", |
| 736 | destroy_notify->window, window); |
| 737 | |
| 738 | if (window->repaint_source) |
| 739 | wl_event_source_remove(window->repaint_source); |
| 740 | if (window->cairo_surface) |
| 741 | cairo_surface_destroy(window->cairo_surface); |
| 742 | |
| 743 | hash_table_remove(wm->window_hash, window->id); |
| 744 | hash_table_remove(wm->window_hash, window->frame_id); |
| 745 | xcb_destroy_window(wm->conn, window->frame_id); |
| 746 | if (window->surface) |
| 747 | wl_list_remove(&window->surface_destroy_listener.link); |
| 748 | free(window); |
| 749 | } |
| 750 | |
| 751 | static void |
| 752 | weston_wm_handle_client_message(struct weston_wm *wm, |
| 753 | xcb_generic_event_t *event) |
| 754 | { |
| 755 | xcb_client_message_event_t *client_message = |
| 756 | (xcb_client_message_event_t *) event; |
| 757 | struct weston_shell_interface *shell_interface = |
| 758 | &wm->server->compositor->shell_interface; |
| 759 | struct weston_wm_window *window; |
| 760 | struct weston_seat *seat; |
| 761 | |
| 762 | window = hash_table_lookup(wm->window_hash, client_message->window); |
| 763 | |
| 764 | fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n", |
| 765 | get_atom_name(wm->conn, client_message->type), |
| 766 | client_message->data.data32[0], |
| 767 | client_message->data.data32[1], |
| 768 | client_message->data.data32[2], |
| 769 | client_message->data.data32[3], |
| 770 | client_message->data.data32[4]); |
| 771 | |
| 772 | seat = wm->server->compositor->seat; |
| 773 | if (client_message->type == wm->atom.net_wm_moveresize && |
| 774 | client_message->data.data32[2] == _NET_WM_MOVERESIZE_MOVE && |
| 775 | seat->seat.pointer->button_count == 1 && |
| 776 | seat->seat.pointer->focus == &window->surface->surface) |
| 777 | shell_interface->move(window->shsurf, seat); |
| 778 | } |
| 779 | |
| 780 | static void |
| 781 | weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event) |
| 782 | { |
| 783 | xcb_button_press_event_t *button = (xcb_button_press_event_t *) event; |
| 784 | struct weston_shell_interface *shell_interface = |
| 785 | &wm->server->compositor->shell_interface; |
| 786 | struct weston_wm_window *window; |
| 787 | |
| 788 | fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n", |
| 789 | button->response_type == XCB_BUTTON_PRESS ? |
| 790 | "PRESS" : "RELEASE", button->detail); |
| 791 | |
| 792 | window = hash_table_lookup(wm->window_hash, button->event); |
| 793 | if (button->response_type == XCB_BUTTON_PRESS && |
| 794 | button->detail == 1) |
| 795 | shell_interface->move(window->shsurf, |
| 796 | wm->server->compositor->seat); |
| 797 | } |
| 798 | |
| 799 | static int |
| 800 | weston_wm_handle_event(int fd, uint32_t mask, void *data) |
| 801 | { |
| 802 | struct weston_wm *wm = data; |
| 803 | xcb_generic_event_t *event; |
| 804 | int count = 0; |
| 805 | |
| 806 | while (event = xcb_poll_for_event(wm->conn), event != NULL) { |
| 807 | if (weston_wm_handle_selection_event(wm, event)) { |
| 808 | fprintf(stderr, "handled by selection code\n"); |
| 809 | free(event); |
| 810 | count++; |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | switch (event->response_type & ~0x80) { |
| 815 | case XCB_BUTTON_PRESS: |
| 816 | case XCB_BUTTON_RELEASE: |
| 817 | weston_wm_handle_button(wm, event); |
| 818 | break; |
| 819 | case XCB_EXPOSE: |
| 820 | weston_wm_handle_expose(wm, event); |
| 821 | break; |
| 822 | case XCB_CREATE_NOTIFY: |
| 823 | weston_wm_handle_create_notify(wm, event); |
| 824 | break; |
| 825 | case XCB_MAP_REQUEST: |
| 826 | weston_wm_handle_map_request(wm, event); |
| 827 | break; |
| 828 | case XCB_MAP_NOTIFY: |
| 829 | weston_wm_handle_map_notify(wm, event); |
| 830 | break; |
| 831 | case XCB_UNMAP_NOTIFY: |
| 832 | fprintf(stderr, "XCB_UNMAP_NOTIFY\n"); |
| 833 | break; |
| 834 | case XCB_CONFIGURE_REQUEST: |
| 835 | weston_wm_handle_configure_request(wm, event); |
| 836 | break; |
| 837 | case XCB_CONFIGURE_NOTIFY: |
| 838 | weston_wm_handle_configure_notify(wm, event); |
| 839 | break; |
| 840 | case XCB_DESTROY_NOTIFY: |
| 841 | weston_wm_handle_destroy_notify(wm, event); |
| 842 | break; |
| 843 | case XCB_MAPPING_NOTIFY: |
| 844 | fprintf(stderr, "XCB_MAPPING_NOTIFY\n"); |
| 845 | break; |
| 846 | case XCB_PROPERTY_NOTIFY: |
| 847 | weston_wm_handle_property_notify(wm, event); |
| 848 | break; |
| 849 | case XCB_CLIENT_MESSAGE: |
| 850 | weston_wm_handle_client_message(wm, event); |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | free(event); |
| 855 | count++; |
| 856 | } |
| 857 | |
| 858 | xcb_flush(wm->conn); |
| 859 | |
| 860 | return count; |
| 861 | } |
| 862 | |
| 863 | static void |
| 864 | wxs_wm_get_resources(struct weston_wm *wm) |
| 865 | { |
| 866 | |
| 867 | #define F(field) offsetof(struct weston_wm, field) |
| 868 | |
| 869 | static const struct { const char *name; int offset; } atoms[] = { |
| 870 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
| 871 | { "WM_TAKE_FOCUS", F(atom.wm_take_focus) }, |
| 872 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
| 873 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
| 874 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
| 875 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 876 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
| 877 | { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) }, |
| 878 | { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) }, |
| 879 | { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) }, |
| 880 | |
| 881 | { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) }, |
| 882 | { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) }, |
| 883 | { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) }, |
| 884 | { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) }, |
| 885 | { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) }, |
| 886 | { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) }, |
| 887 | { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) }, |
| 888 | { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) }, |
| 889 | { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) }, |
| 890 | { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) }, |
| 891 | { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) }, |
| 892 | { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) }, |
| 893 | { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) }, |
| 894 | { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) }, |
| 895 | |
| 896 | { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) }, |
| 897 | { "_NET_SUPPORTING_WM_CHECK", |
| 898 | F(atom.net_supporting_wm_check) }, |
| 899 | { "_NET_SUPPORTED", F(atom.net_supported) }, |
| 900 | { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) }, |
| 901 | { "CLIPBOARD", F(atom.clipboard) }, |
| 902 | { "TARGETS", F(atom.targets) }, |
| 903 | { "UTF8_STRING", F(atom.utf8_string) }, |
| 904 | { "_WL_SELECTION", F(atom.wl_selection) }, |
| 905 | { "INCR", F(atom.incr) }, |
| 906 | { "TIMESTAMP", F(atom.timestamp) }, |
| 907 | { "MULTIPLE", F(atom.multiple) }, |
| 908 | { "UTF8_STRING" , F(atom.utf8_string) }, |
| 909 | { "COMPOUND_TEXT", F(atom.compound_text) }, |
| 910 | { "TEXT", F(atom.text) }, |
| 911 | { "STRING", F(atom.string) }, |
| 912 | { "text/plain;charset=utf-8", F(atom.text_plain_utf8) }, |
| 913 | { "text/plain", F(atom.text_plain) }, |
| 914 | }; |
| 915 | #undef F |
| 916 | |
| 917 | xcb_xfixes_query_version_cookie_t xfixes_cookie; |
| 918 | xcb_xfixes_query_version_reply_t *xfixes_reply; |
| 919 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
| 920 | xcb_intern_atom_reply_t *reply; |
| 921 | xcb_render_query_pict_formats_reply_t *formats_reply; |
| 922 | xcb_render_query_pict_formats_cookie_t formats_cookie; |
| 923 | xcb_render_pictforminfo_t *formats; |
| 924 | uint32_t i; |
| 925 | |
| 926 | xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id); |
| 927 | |
| 928 | formats_cookie = xcb_render_query_pict_formats(wm->conn); |
| 929 | |
| 930 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
| 931 | cookies[i] = xcb_intern_atom (wm->conn, 0, |
| 932 | strlen(atoms[i].name), |
| 933 | atoms[i].name); |
| 934 | |
| 935 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
| 936 | reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL); |
| 937 | *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom; |
| 938 | free(reply); |
| 939 | } |
| 940 | |
| 941 | wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id); |
| 942 | if (!wm->xfixes || !wm->xfixes->present) |
| 943 | fprintf(stderr, "xfixes not available\n"); |
| 944 | |
| 945 | xfixes_cookie = xcb_xfixes_query_version(wm->conn, |
| 946 | XCB_XFIXES_MAJOR_VERSION, |
| 947 | XCB_XFIXES_MINOR_VERSION); |
| 948 | xfixes_reply = xcb_xfixes_query_version_reply(wm->conn, |
| 949 | xfixes_cookie, NULL); |
| 950 | |
| 951 | printf("xfixes version: %d.%d\n", |
| 952 | xfixes_reply->major_version, xfixes_reply->minor_version); |
| 953 | |
| 954 | free(xfixes_reply); |
| 955 | |
| 956 | formats_reply = xcb_render_query_pict_formats_reply(wm->conn, |
| 957 | formats_cookie, 0); |
| 958 | if (formats_reply == NULL) |
| 959 | return; |
| 960 | |
| 961 | formats = xcb_render_query_pict_formats_formats(formats_reply); |
| 962 | for (i = 0; i < formats_reply->length; i++) |
| 963 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 964 | formats[i].depth == 24) |
| 965 | wm->render_format = formats[i]; |
| 966 | |
| 967 | free(formats_reply); |
| 968 | } |
| 969 | |
| 970 | static void |
| 971 | weston_wm_create_wm_window(struct weston_wm *wm) |
| 972 | { |
| 973 | static const char name[] = "Weston WM"; |
| 974 | |
| 975 | wm->wm_window = xcb_generate_id(wm->conn); |
| 976 | xcb_create_window(wm->conn, |
| 977 | XCB_COPY_FROM_PARENT, |
| 978 | wm->wm_window, |
| 979 | wm->screen->root, |
| 980 | 0, 0, |
| 981 | 10, 10, |
| 982 | 0, |
| 983 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 984 | wm->screen->root_visual, |
| 985 | 0, NULL); |
| 986 | |
| 987 | xcb_change_property(wm->conn, |
| 988 | XCB_PROP_MODE_REPLACE, |
| 989 | wm->wm_window, |
| 990 | wm->atom.net_supporting_wm_check, |
| 991 | XCB_ATOM_WINDOW, |
| 992 | 32, /* format */ |
| 993 | 1, &wm->wm_window); |
| 994 | |
| 995 | xcb_change_property(wm->conn, |
| 996 | XCB_PROP_MODE_REPLACE, |
| 997 | wm->wm_window, |
| 998 | wm->atom.net_wm_name, |
| 999 | wm->atom.utf8_string, |
| 1000 | 8, /* format */ |
| 1001 | strlen(name), name); |
| 1002 | |
| 1003 | xcb_change_property(wm->conn, |
| 1004 | XCB_PROP_MODE_REPLACE, |
| 1005 | wm->screen->root, |
| 1006 | wm->atom.net_supporting_wm_check, |
| 1007 | XCB_ATOM_WINDOW, |
| 1008 | 32, /* format */ |
| 1009 | 1, &wm->wm_window); |
| 1010 | |
| 1011 | } |
| 1012 | |
| 1013 | struct weston_wm * |
| 1014 | weston_wm_create(struct weston_xserver *wxs) |
| 1015 | { |
| 1016 | struct wl_seat *seat; |
| 1017 | struct weston_wm *wm; |
| 1018 | struct wl_event_loop *loop; |
| 1019 | xcb_screen_iterator_t s; |
| 1020 | uint32_t values[1], mask; |
| 1021 | int sv[2]; |
| 1022 | xcb_atom_t supported[1]; |
| 1023 | |
| 1024 | wm = malloc(sizeof *wm); |
| 1025 | if (wm == NULL) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | memset(wm, 0, sizeof *wm); |
| 1029 | wm->server = wxs; |
| 1030 | wm->window_hash = hash_table_create(); |
| 1031 | if (wm->window_hash == NULL) { |
| 1032 | free(wm); |
| 1033 | return NULL; |
| 1034 | } |
| 1035 | |
| 1036 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
| 1037 | fprintf(stderr, "socketpair failed\n"); |
| 1038 | hash_table_destroy(wm->window_hash); |
| 1039 | free(wm); |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | xserver_send_client(wxs->resource, sv[1]); |
| 1044 | wl_client_flush(wxs->resource->client); |
| 1045 | close(sv[1]); |
| 1046 | |
| 1047 | /* xcb_connect_to_fd takes ownership of the fd. */ |
| 1048 | wm->conn = xcb_connect_to_fd(sv[0], NULL); |
| 1049 | if (xcb_connection_has_error(wm->conn)) { |
| 1050 | fprintf(stderr, "xcb_connect_to_fd failed\n"); |
| 1051 | close(sv[0]); |
| 1052 | hash_table_destroy(wm->window_hash); |
| 1053 | free(wm); |
| 1054 | return NULL; |
| 1055 | } |
| 1056 | |
| 1057 | s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn)); |
| 1058 | wm->screen = s.data; |
| 1059 | |
| 1060 | loop = wl_display_get_event_loop(wxs->wl_display); |
| 1061 | wm->source = |
| 1062 | wl_event_loop_add_fd(loop, sv[0], |
| 1063 | WL_EVENT_READABLE, |
| 1064 | weston_wm_handle_event, wm); |
| 1065 | wl_event_source_check(wm->source); |
| 1066 | |
| 1067 | wxs_wm_get_resources(wm); |
| 1068 | |
| 1069 | values[0] = |
| 1070 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | |
| 1071 | XCB_EVENT_MASK_RESIZE_REDIRECT | |
| 1072 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 1073 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
| 1074 | XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1075 | xcb_change_window_attributes(wm->conn, wm->screen->root, |
| 1076 | XCB_CW_EVENT_MASK, values); |
| 1077 | wm->theme = theme_create(); |
| 1078 | |
| 1079 | weston_wm_create_wm_window(wm); |
| 1080 | |
| 1081 | supported[0] = wm->atom.net_wm_moveresize; |
| 1082 | xcb_change_property(wm->conn, |
| 1083 | XCB_PROP_MODE_REPLACE, |
| 1084 | wm->screen->root, |
| 1085 | wm->atom.net_supported, |
| 1086 | XCB_ATOM_ATOM, |
| 1087 | 32, /* format */ |
| 1088 | ARRAY_LENGTH(supported), supported); |
| 1089 | |
| 1090 | wm->selection_request.requestor = XCB_NONE; |
| 1091 | |
| 1092 | wm->selection_window = xcb_generate_id(wm->conn); |
| 1093 | xcb_create_window(wm->conn, |
| 1094 | XCB_COPY_FROM_PARENT, |
| 1095 | wm->selection_window, |
| 1096 | wm->screen->root, |
| 1097 | 0, 0, |
| 1098 | 10, 10, |
| 1099 | 0, |
| 1100 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1101 | wm->screen->root_visual, |
| 1102 | XCB_CW_EVENT_MASK, values); |
| 1103 | |
| 1104 | mask = |
| 1105 | XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | |
| 1106 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | |
| 1107 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; |
| 1108 | |
| 1109 | xcb_xfixes_select_selection_input(wm->conn, wm->selection_window, |
| 1110 | wm->atom.clipboard, mask); |
| 1111 | |
| 1112 | xcb_flush(wm->conn); |
| 1113 | |
| 1114 | seat = &wxs->compositor->seat->seat; |
| 1115 | wm->selection_listener.notify = weston_wm_set_selection; |
| 1116 | wl_signal_add(&seat->selection_signal, &wm->selection_listener); |
| 1117 | |
| 1118 | wm->activate_listener.notify = weston_wm_window_activate; |
| 1119 | wl_signal_add(&wxs->compositor->activate_signal, |
| 1120 | &wm->activate_listener); |
| 1121 | |
| 1122 | fprintf(stderr, "created wm\n"); |
| 1123 | |
| 1124 | return wm; |
| 1125 | } |
| 1126 | |
| 1127 | void |
| 1128 | weston_wm_destroy(struct weston_wm *wm) |
| 1129 | { |
| 1130 | /* FIXME: Free windows in hash. */ |
| 1131 | hash_table_destroy(wm->window_hash); |
| 1132 | xcb_disconnect(wm->conn); |
| 1133 | wl_event_source_remove(wm->source); |
| 1134 | wl_list_remove(&wm->selection_listener.link); |
| 1135 | wl_list_remove(&wm->activate_listener.link); |
| 1136 | |
| 1137 | free(wm); |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | surface_destroy(struct wl_listener *listener, void *data) |
| 1142 | { |
| 1143 | struct weston_wm_window *window = |
| 1144 | container_of(listener, |
| 1145 | struct weston_wm_window, surface_destroy_listener); |
| 1146 | |
| 1147 | fprintf(stderr, "surface for xid %d destroyed\n", window->id); |
| 1148 | } |
| 1149 | |
| 1150 | static struct weston_wm_window * |
| 1151 | get_wm_window(struct weston_surface *surface) |
| 1152 | { |
| 1153 | struct wl_resource *resource = &surface->surface.resource; |
| 1154 | struct wl_listener *listener; |
| 1155 | |
| 1156 | listener = wl_signal_get(&resource->destroy_signal, surface_destroy); |
| 1157 | if (listener) |
| 1158 | return container_of(listener, struct weston_wm_window, |
| 1159 | surface_destroy_listener); |
| 1160 | |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
| 1164 | static void |
| 1165 | xserver_map_shell_surface(struct weston_wm *wm, |
| 1166 | struct weston_wm_window *window) |
| 1167 | { |
| 1168 | struct weston_shell_interface *shell_interface = |
| 1169 | &wm->server->compositor->shell_interface; |
| 1170 | struct weston_wm_window *parent; |
| 1171 | struct theme *t = window->wm->theme; |
| 1172 | |
| 1173 | if (!shell_interface->create_shell_surface) |
| 1174 | return; |
| 1175 | |
| 1176 | window->shsurf = |
| 1177 | shell_interface->create_shell_surface(shell_interface->shell, |
| 1178 | window->surface); |
| 1179 | |
| 1180 | if (!window->transient_for) { |
| 1181 | shell_interface->set_toplevel(window->shsurf); |
| 1182 | return; |
| 1183 | } |
| 1184 | |
| 1185 | parent = hash_table_lookup(wm->window_hash, window->transient_for->id); |
| 1186 | shell_interface->set_transient(window->shsurf, parent->shsurf, |
| 1187 | window->x - parent->x + t->margin + t->width, |
| 1188 | window->y - parent->y + t->margin + t->titlebar_height, |
| 1189 | WL_SHELL_SURFACE_TRANSIENT_INACTIVE); |
| 1190 | } |
| 1191 | |
| 1192 | static void |
| 1193 | xserver_set_window_id(struct wl_client *client, struct wl_resource *resource, |
| 1194 | struct wl_resource *surface_resource, uint32_t id) |
| 1195 | { |
| 1196 | struct weston_xserver *wxs = resource->data; |
| 1197 | struct weston_wm *wm = wxs->wm; |
| 1198 | struct wl_surface *surface = surface_resource->data; |
| 1199 | struct weston_wm_window *window; |
| 1200 | |
| 1201 | if (client != wxs->client) |
| 1202 | return; |
| 1203 | |
| 1204 | window = hash_table_lookup(wm->window_hash, id); |
| 1205 | if (window == NULL) { |
| 1206 | fprintf(stderr, "set_window_id for unknown window %d\n", id); |
| 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | fprintf(stderr, "set_window_id %d for surface %p\n", id, surface); |
| 1211 | |
| 1212 | weston_wm_window_read_properties(window); |
| 1213 | |
| 1214 | window->surface = (struct weston_surface *) surface; |
| 1215 | window->surface_destroy_listener.notify = surface_destroy; |
| 1216 | wl_signal_add(&surface->resource.destroy_signal, |
| 1217 | &window->surface_destroy_listener); |
| 1218 | |
| 1219 | weston_wm_window_schedule_repaint(window); |
| 1220 | xserver_map_shell_surface(wm, window); |
| 1221 | } |
| 1222 | |
| 1223 | const struct xserver_interface xserver_implementation = { |
| 1224 | xserver_set_window_id |
| 1225 | }; |