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