2 * Copyright © 2011 Intel Corporation
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.
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.
28 #include <sys/socket.h>
37 #include "../../shared/cairo-util.h"
38 #include "../compositor.h"
40 #include "xserver-server-protocol.h"
43 struct motif_wm_hints {
51 #define MWM_HINTS_FUNCTIONS (1L << 0)
52 #define MWM_HINTS_DECORATIONS (1L << 1)
53 #define MWM_HINTS_INPUT_MODE (1L << 2)
54 #define MWM_HINTS_STATUS (1L << 3)
56 #define MWM_FUNC_ALL (1L << 0)
57 #define MWM_FUNC_RESIZE (1L << 1)
58 #define MWM_FUNC_MOVE (1L << 2)
59 #define MWM_FUNC_MINIMIZE (1L << 3)
60 #define MWM_FUNC_MAXIMIZE (1L << 4)
61 #define MWM_FUNC_CLOSE (1L << 5)
63 #define MWM_DECOR_ALL (1L << 0)
64 #define MWM_DECOR_BORDER (1L << 1)
65 #define MWM_DECOR_RESIZEH (1L << 2)
66 #define MWM_DECOR_TITLE (1L << 3)
67 #define MWM_DECOR_MENU (1L << 4)
68 #define MWM_DECOR_MINIMIZE (1L << 5)
69 #define MWM_DECOR_MAXIMIZE (1L << 6)
71 #define MWM_INPUT_MODELESS 0
72 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
73 #define MWM_INPUT_SYSTEM_MODAL 2
74 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
75 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
77 #define MWM_TEAROFF_WINDOW (1L<<0)
79 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
80 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
81 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
82 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
83 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
84 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
85 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
86 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
87 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
88 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
89 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
90 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
94 struct weston_wm_window {
97 xcb_window_t frame_id;
98 cairo_surface_t *cairo_surface;
99 struct weston_surface *surface;
100 struct shell_surface *shsurf;
101 struct wl_listener surface_destroy_listener;
102 struct wl_event_source *repaint_source;
103 struct wl_event_source *configure_source;
104 int properties_dirty;
107 struct weston_wm_window *transient_for;
113 int override_redirect;
116 static struct weston_wm_window *
117 get_wm_window(struct weston_surface *surface);
120 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
123 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
125 xcb_get_atom_name_cookie_t cookie;
126 xcb_get_atom_name_reply_t *reply;
127 xcb_generic_error_t *e;
128 static char buffer[64];
130 if (atom == XCB_ATOM_NONE)
133 cookie = xcb_get_atom_name (c, atom);
134 reply = xcb_get_atom_name_reply (c, cookie, &e);
135 snprintf(buffer, sizeof buffer, "%.*s",
136 xcb_get_atom_name_name_length (reply),
137 xcb_get_atom_name_name (reply));
144 dump_property(struct weston_wm *wm,
145 xcb_atom_t property, xcb_get_property_reply_t *reply)
148 const char *text_value, *name;
149 xcb_atom_t *atom_value;
153 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
155 weston_log_continue("(no reply)\n");
159 width += weston_log_continue(
160 "%s/%d, length %d (value_len %d): ",
161 get_atom_name(wm->conn, reply->type),
163 xcb_get_property_value_length(reply),
166 if (reply->type == wm->atom.incr) {
167 incr_value = xcb_get_property_value(reply);
168 weston_log_continue("%d\n", *incr_value);
169 } else if (reply->type == wm->atom.utf8_string ||
170 reply->type == wm->atom.string) {
171 text_value = xcb_get_property_value(reply);
172 if (reply->value_len > 40)
175 len = reply->value_len;
176 weston_log_continue("\"%.*s\"\n", len, text_value);
177 } else if (reply->type == XCB_ATOM_ATOM) {
178 atom_value = xcb_get_property_value(reply);
179 for (i = 0; i < reply->value_len; i++) {
180 name = get_atom_name(wm->conn, atom_value[i]);
181 if (width + strlen(name) + 2 > 78) {
182 weston_log_continue("\n ");
185 width += weston_log_continue(", ");
188 width += weston_log_continue("%s", name);
190 weston_log_continue("\n");
192 weston_log_continue("huh?\n");
197 read_and_dump_property(struct weston_wm *wm,
198 xcb_window_t window, xcb_atom_t property)
200 xcb_get_property_reply_t *reply;
201 xcb_get_property_cookie_t cookie;
203 cookie = xcb_get_property(wm->conn, 0, window,
204 property, XCB_ATOM_ANY, 0, 2048);
205 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
207 dump_property(wm, property, reply);
212 /* We reuse some predefined, but otherwise useles atoms */
213 #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
214 #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
217 weston_wm_window_read_properties(struct weston_wm_window *window)
219 struct weston_wm *wm = window->wm;
221 #define F(field) offsetof(struct weston_wm_window, field)
227 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
228 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
229 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
230 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
231 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
232 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
233 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
237 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
238 xcb_get_property_reply_t *reply;
243 struct motif_wm_hints *hints;
245 if (!window->properties_dirty)
247 window->properties_dirty = 0;
249 for (i = 0; i < ARRAY_LENGTH(props); i++)
250 cookie[i] = xcb_get_property(wm->conn,
254 XCB_ATOM_ANY, 0, 2048);
256 window->decorate = 1;
257 for (i = 0; i < ARRAY_LENGTH(props); i++) {
258 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
260 /* Bad window, typically */
262 if (reply->type == XCB_ATOM_NONE) {
263 /* No such property */
268 p = ((char *) window + props[i].offset);
270 switch (props[i].type) {
271 case XCB_ATOM_STRING:
272 /* FIXME: We're using this for both string and
278 strndup(xcb_get_property_value(reply),
279 xcb_get_property_value_length(reply));
281 case XCB_ATOM_WINDOW:
282 xid = xcb_get_property_value(reply);
283 *(struct weston_wm_window **) p =
284 hash_table_lookup(wm->window_hash, *xid);
287 atom = xcb_get_property_value(reply);
288 *(xcb_atom_t *) p = *atom;
290 case TYPE_WM_PROTOCOLS:
292 case TYPE_MOTIF_WM_HINTS:
293 hints = xcb_get_property_value(reply);
294 if (hints->flags & MWM_HINTS_DECORATIONS)
295 window->decorate = hints->decorations > 0;
305 weston_wm_window_get_frame_size(struct weston_wm_window *window,
306 int *width, int *height)
308 struct theme *t = window->wm->theme;
310 if (window->decorate) {
311 *width = window->width + (t->margin + t->width) * 2;
312 *height = window->height +
313 t->margin * 2 + t->width + t->titlebar_height;
315 *width = window->width + t->margin * 2;
316 *height = window->height + t->margin * 2;
321 weston_wm_window_get_child_position(struct weston_wm_window *window,
324 struct theme *t = window->wm->theme;
326 if (window->decorate) {
327 *x = t->margin + t->width;
328 *y = t->margin + t->titlebar_height;
336 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
338 xcb_configure_request_event_t *configure_request =
339 (xcb_configure_request_event_t *) event;
340 struct weston_wm_window *window;
341 uint32_t mask, values[16];
342 int x, y, width, height, i = 0;
344 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
345 configure_request->window,
346 configure_request->x, configure_request->y,
347 configure_request->width, configure_request->height);
349 window = hash_table_lookup(wm->window_hash, configure_request->window);
351 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
352 window->width = configure_request->width;
353 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
354 window->height = configure_request->height;
356 weston_wm_window_get_child_position(window, &x, &y);
359 values[i++] = window->width;
360 values[i++] = window->height;
362 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
363 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
364 XCB_CONFIG_WINDOW_BORDER_WIDTH;
365 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
366 values[i++] = configure_request->sibling;
367 mask |= XCB_CONFIG_WINDOW_SIBLING;
369 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
370 values[i++] = configure_request->stack_mode;
371 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
374 xcb_configure_window(wm->conn, window->id, mask, values);
376 weston_wm_window_get_frame_size(window, &width, &height);
379 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
380 xcb_configure_window(wm->conn, window->frame_id, mask, values);
382 weston_wm_window_schedule_repaint(window);
386 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
388 xcb_configure_notify_event_t *configure_notify =
389 (xcb_configure_notify_event_t *) event;
390 struct weston_wm_window *window;
392 window = hash_table_lookup(wm->window_hash, configure_notify->window);
394 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
395 configure_notify->window == window->id ? "client" : "frame",
396 configure_notify->window,
397 configure_notify->x, configure_notify->y,
398 configure_notify->width, configure_notify->height);
402 weston_wm_window_activate(struct wl_listener *listener, void *data)
404 struct weston_surface *surface = data;
405 struct weston_wm_window *window = get_wm_window(surface);
406 struct weston_wm *wm =
407 container_of(listener, struct weston_wm, activate_listener);
408 xcb_client_message_event_t client_message;
411 client_message.response_type = XCB_CLIENT_MESSAGE;
412 client_message.format = 32;
413 client_message.window = window->id;
414 client_message.type = wm->atom.wm_protocols;
415 client_message.data.data32[0] = wm->atom.wm_take_focus;
416 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
418 xcb_send_event(wm->conn, 0, window->id,
419 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
420 (char *) &client_message);
422 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
423 window->id, XCB_TIME_CURRENT_TIME);
425 xcb_set_input_focus (wm->conn,
426 XCB_INPUT_FOCUS_POINTER_ROOT,
428 XCB_TIME_CURRENT_TIME);
431 if (wm->focus_window)
432 weston_wm_window_schedule_repaint(wm->focus_window);
433 wm->focus_window = window;
434 if (wm->focus_window)
435 weston_wm_window_schedule_repaint(wm->focus_window);
439 our_resource(struct weston_wm *wm, uint32_t id)
441 const xcb_setup_t *setup;
443 setup = xcb_get_setup(wm->conn);
445 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
448 #define ICCCM_WITHDRAWN_STATE 0
449 #define ICCCM_NORMAL_STATE 1
450 #define ICCCM_ICONIC_STATE 3
453 weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
455 struct weston_wm *wm = window->wm;
456 uint32_t property[2];
459 property[1] = XCB_WINDOW_NONE;
461 xcb_change_property(wm->conn,
462 XCB_PROP_MODE_REPLACE,
471 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
473 xcb_map_request_event_t *map_request =
474 (xcb_map_request_event_t *) event;
475 struct weston_wm_window *window;
477 int x, y, width, height;
479 if (our_resource(wm, map_request->window)) {
480 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
481 map_request->window);
485 window = hash_table_lookup(wm->window_hash, map_request->window);
487 if (window->frame_id)
490 weston_wm_window_read_properties(window);
492 weston_wm_window_get_frame_size(window, &width, &height);
493 weston_wm_window_get_child_position(window, &x, &y);
496 XCB_EVENT_MASK_KEY_PRESS |
497 XCB_EVENT_MASK_KEY_RELEASE |
498 XCB_EVENT_MASK_BUTTON_PRESS |
499 XCB_EVENT_MASK_BUTTON_RELEASE |
500 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
501 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
503 window->frame_id = xcb_generate_id(wm->conn);
504 xcb_create_window(wm->conn,
505 XCB_COPY_FROM_PARENT,
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);
517 xcb_configure_window(wm->conn, window->id,
518 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
520 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
521 window->id, window, window->frame_id);
523 xcb_map_window(wm->conn, map_request->window);
524 xcb_map_window(wm->conn, window->frame_id);
525 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
527 window->cairo_surface =
528 cairo_xcb_surface_create_with_xrender_format(wm->conn,
534 hash_table_insert(wm->window_hash, window->frame_id, window);
538 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
540 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
542 if (our_resource(wm, map_notify->window)) {
543 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
548 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
552 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
554 xcb_unmap_notify_event_t *unmap_notify =
555 (xcb_unmap_notify_event_t *) event;
556 struct weston_wm_window *window;
558 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
559 unmap_notify->window,
561 our_resource(wm, unmap_notify->window) ? ", ours" : "");
563 if (our_resource(wm, unmap_notify->window))
566 if (unmap_notify->response_type & 0x80)
567 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
568 * as it may come in after we've destroyed the window. */
571 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
572 if (window->repaint_source)
573 wl_event_source_remove(window->repaint_source);
574 if (window->cairo_surface)
575 cairo_surface_destroy(window->cairo_surface);
577 if (window->frame_id) {
578 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
579 xcb_destroy_window(wm->conn, window->frame_id);
580 weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE);
581 hash_table_remove(wm->window_hash, window->frame_id);
582 window->frame_id = XCB_WINDOW_NONE;
585 if (wm->focus_window == window)
586 wm->focus_window = NULL;
588 wl_list_remove(&window->surface_destroy_listener.link);
589 window->surface = NULL;
593 weston_wm_window_draw_decoration(void *data)
595 struct weston_wm_window *window = data;
596 struct weston_wm *wm = window->wm;
597 struct theme *t = wm->theme;
599 int x, y, width, height;
603 weston_wm_window_read_properties(window);
605 window->repaint_source = NULL;
607 weston_wm_window_get_frame_size(window, &width, &height);
608 weston_wm_window_get_child_position(window, &x, &y);
610 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
611 cr = cairo_create(window->cairo_surface);
613 if (window->decorate) {
614 if (wm->focus_window == window)
615 flags |= THEME_FRAME_ACTIVE;
618 title = window->name;
622 theme_render_frame(t, cr, width, height, title, flags);
624 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
625 cairo_set_source_rgba(cr, 0, 0, 0, 0);
628 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
629 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
630 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
635 if (window->surface) {
636 /* We leave an extra pixel around the X window area to
637 * make sure we don't sample from the undefined alpha
638 * channel when filtering. */
639 window->surface->opaque_rect[0] =
640 (double) (x - 1) / width;
641 window->surface->opaque_rect[1] =
642 (double) (x + window->width + 1) / width;
643 window->surface->opaque_rect[2] =
644 (double) (y - 1) / height;
645 window->surface->opaque_rect[3] =
646 (double) (y + window->height + 1) / height;
648 pixman_region32_init_rect(&window->surface->input,
649 t->margin, t->margin,
650 width - 2 * t->margin,
651 height - 2 * t->margin);
656 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
658 struct weston_wm *wm = window->wm;
660 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
663 window->repaint_source =
664 wl_event_loop_add_idle(wm->server->loop,
665 weston_wm_window_draw_decoration,
670 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
672 xcb_property_notify_event_t *property_notify =
673 (xcb_property_notify_event_t *) event;
674 struct weston_wm_window *window;
676 window = hash_table_lookup(wm->window_hash, property_notify->window);
678 window->properties_dirty = 1;
680 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
681 property_notify->window);
682 if (property_notify->state == XCB_PROPERTY_DELETE)
683 weston_log("deleted\n");
685 read_and_dump_property(wm, property_notify->window,
686 property_notify->atom);
688 if (property_notify->atom == wm->atom.net_wm_name ||
689 property_notify->atom == XCB_ATOM_WM_NAME)
690 weston_wm_window_schedule_repaint(window);
694 weston_wm_window_create(struct weston_wm *wm,
695 xcb_window_t id, int width, int height, int override)
697 struct weston_wm_window *window;
700 window = malloc(sizeof *window);
701 if (window == NULL) {
702 weston_log("failed to allocate window\n");
706 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
707 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
709 memset(window, 0, sizeof *window);
712 window->properties_dirty = 1;
713 window->override_redirect = override;
714 window->width = width;
715 window->height = height;
717 hash_table_insert(wm->window_hash, id, window);
721 weston_wm_window_destroy(struct weston_wm_window *window)
723 hash_table_remove(window->wm->window_hash, window->id);
728 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
730 xcb_create_notify_event_t *create_notify =
731 (xcb_create_notify_event_t *) event;
733 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
734 create_notify->window,
735 create_notify->width, create_notify->height,
736 create_notify->override_redirect ? ", override" : "",
737 our_resource(wm, create_notify->window) ? ", ours" : "");
739 if (our_resource(wm, create_notify->window))
742 weston_wm_window_create(wm, create_notify->window,
743 create_notify->width, create_notify->height,
744 create_notify->override_redirect);
748 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
750 xcb_destroy_notify_event_t *destroy_notify =
751 (xcb_destroy_notify_event_t *) event;
752 struct weston_wm_window *window;
754 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
755 destroy_notify->window,
756 destroy_notify->event,
757 our_resource(wm, destroy_notify->window) ? ", ours" : "");
759 if (our_resource(wm, destroy_notify->window))
762 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
763 weston_wm_window_destroy(window);
767 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
769 xcb_reparent_notify_event_t *reparent_notify =
770 (xcb_reparent_notify_event_t *) event;
771 struct weston_wm_window *window;
773 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
774 reparent_notify->window,
775 reparent_notify->parent,
776 reparent_notify->event);
778 if (reparent_notify->parent == wm->screen->root) {
779 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
780 reparent_notify->override_redirect);
781 } else if (!our_resource(wm, reparent_notify->parent)) {
782 window = hash_table_lookup(wm->window_hash,
783 reparent_notify->window);
784 weston_wm_window_destroy(window);
789 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
790 xcb_client_message_event_t *client_message)
792 static const int map[] = {
793 THEME_LOCATION_RESIZING_TOP_LEFT,
794 THEME_LOCATION_RESIZING_TOP,
795 THEME_LOCATION_RESIZING_TOP_RIGHT,
796 THEME_LOCATION_RESIZING_RIGHT,
797 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
798 THEME_LOCATION_RESIZING_BOTTOM,
799 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
800 THEME_LOCATION_RESIZING_LEFT
803 struct weston_wm *wm = window->wm;
804 struct weston_seat *seat = wm->server->compositor->seat;
806 struct weston_shell_interface *shell_interface =
807 &wm->server->compositor->shell_interface;
809 if (seat->seat.pointer->button_count != 1 ||
810 seat->seat.pointer->focus != &window->surface->surface)
813 detail = client_message->data.data32[2];
815 case _NET_WM_MOVERESIZE_MOVE:
816 shell_interface->move(window->shsurf, seat);
818 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
819 case _NET_WM_MOVERESIZE_SIZE_TOP:
820 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
821 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
822 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
823 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
824 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
825 case _NET_WM_MOVERESIZE_SIZE_LEFT:
826 shell_interface->resize(window->shsurf, seat, map[detail]);
828 case _NET_WM_MOVERESIZE_CANCEL:
834 weston_wm_handle_client_message(struct weston_wm *wm,
835 xcb_generic_event_t *event)
837 xcb_client_message_event_t *client_message =
838 (xcb_client_message_event_t *) event;
839 struct weston_wm_window *window;
841 window = hash_table_lookup(wm->window_hash, client_message->window);
843 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
844 get_atom_name(wm->conn, client_message->type),
845 client_message->data.data32[0],
846 client_message->data.data32[1],
847 client_message->data.data32[2],
848 client_message->data.data32[3],
849 client_message->data.data32[4]);
851 if (client_message->type == wm->atom.net_wm_moveresize)
852 weston_wm_window_handle_moveresize(window, client_message);
856 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
858 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
859 struct weston_shell_interface *shell_interface =
860 &wm->server->compositor->shell_interface;
861 struct weston_wm_window *window;
862 enum theme_location location;
863 struct theme *t = wm->theme;
866 weston_log("XCB_BUTTON_%s (detail %d)\n",
867 button->response_type == XCB_BUTTON_PRESS ?
868 "PRESS" : "RELEASE", button->detail);
870 window = hash_table_lookup(wm->window_hash, button->event);
871 weston_wm_window_get_frame_size(window, &width, &height);
873 if (button->response_type == XCB_BUTTON_PRESS &&
874 button->detail == 1) {
875 location = theme_get_location(t,
881 case THEME_LOCATION_TITLEBAR:
882 shell_interface->move(window->shsurf,
883 wm->server->compositor->seat);
885 case THEME_LOCATION_RESIZING_TOP:
886 case THEME_LOCATION_RESIZING_BOTTOM:
887 case THEME_LOCATION_RESIZING_LEFT:
888 case THEME_LOCATION_RESIZING_RIGHT:
889 case THEME_LOCATION_RESIZING_TOP_LEFT:
890 case THEME_LOCATION_RESIZING_TOP_RIGHT:
891 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
892 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
893 shell_interface->resize(window->shsurf,
894 wm->server->compositor->seat,
904 weston_wm_handle_event(int fd, uint32_t mask, void *data)
906 struct weston_wm *wm = data;
907 xcb_generic_event_t *event;
910 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
911 if (weston_wm_handle_selection_event(wm, event)) {
917 switch (event->response_type & ~0x80) {
918 case XCB_BUTTON_PRESS:
919 case XCB_BUTTON_RELEASE:
920 weston_wm_handle_button(wm, event);
922 case XCB_CREATE_NOTIFY:
923 weston_wm_handle_create_notify(wm, event);
925 case XCB_MAP_REQUEST:
926 weston_wm_handle_map_request(wm, event);
929 weston_wm_handle_map_notify(wm, event);
931 case XCB_UNMAP_NOTIFY:
932 weston_wm_handle_unmap_notify(wm, event);
934 case XCB_REPARENT_NOTIFY:
935 weston_wm_handle_reparent_notify(wm, event);
937 case XCB_CONFIGURE_REQUEST:
938 weston_wm_handle_configure_request(wm, event);
940 case XCB_CONFIGURE_NOTIFY:
941 weston_wm_handle_configure_notify(wm, event);
943 case XCB_DESTROY_NOTIFY:
944 weston_wm_handle_destroy_notify(wm, event);
946 case XCB_MAPPING_NOTIFY:
947 weston_log("XCB_MAPPING_NOTIFY\n");
949 case XCB_PROPERTY_NOTIFY:
950 weston_wm_handle_property_notify(wm, event);
952 case XCB_CLIENT_MESSAGE:
953 weston_wm_handle_client_message(wm, event);
967 wxs_wm_get_resources(struct weston_wm *wm)
970 #define F(field) offsetof(struct weston_wm, field)
972 static const struct { const char *name; int offset; } atoms[] = {
973 { "WM_PROTOCOLS", F(atom.wm_protocols) },
974 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
975 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
976 { "WM_STATE", F(atom.wm_state) },
977 { "WM_S0", F(atom.wm_s0) },
978 { "_NET_WM_NAME", F(atom.net_wm_name) },
979 { "_NET_WM_ICON", F(atom.net_wm_icon) },
980 { "_NET_WM_STATE", F(atom.net_wm_state) },
981 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
982 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
983 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
984 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
986 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
987 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
988 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
989 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
990 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
991 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
992 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
993 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
994 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
995 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
996 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
997 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
998 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
999 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1001 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1002 { "_NET_SUPPORTING_WM_CHECK",
1003 F(atom.net_supporting_wm_check) },
1004 { "_NET_SUPPORTED", F(atom.net_supported) },
1005 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1006 { "CLIPBOARD", F(atom.clipboard) },
1007 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
1008 { "TARGETS", F(atom.targets) },
1009 { "UTF8_STRING", F(atom.utf8_string) },
1010 { "_WL_SELECTION", F(atom.wl_selection) },
1011 { "INCR", F(atom.incr) },
1012 { "TIMESTAMP", F(atom.timestamp) },
1013 { "MULTIPLE", F(atom.multiple) },
1014 { "UTF8_STRING" , F(atom.utf8_string) },
1015 { "COMPOUND_TEXT", F(atom.compound_text) },
1016 { "TEXT", F(atom.text) },
1017 { "STRING", F(atom.string) },
1018 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1019 { "text/plain", F(atom.text_plain) },
1023 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1024 xcb_xfixes_query_version_reply_t *xfixes_reply;
1025 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1026 xcb_intern_atom_reply_t *reply;
1027 xcb_render_query_pict_formats_reply_t *formats_reply;
1028 xcb_render_query_pict_formats_cookie_t formats_cookie;
1029 xcb_render_pictforminfo_t *formats;
1032 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1034 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1036 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1037 cookies[i] = xcb_intern_atom (wm->conn, 0,
1038 strlen(atoms[i].name),
1041 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1042 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1043 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1047 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1048 if (!wm->xfixes || !wm->xfixes->present)
1049 weston_log("xfixes not available\n");
1051 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1052 XCB_XFIXES_MAJOR_VERSION,
1053 XCB_XFIXES_MINOR_VERSION);
1054 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1055 xfixes_cookie, NULL);
1057 weston_log("xfixes version: %d.%d\n",
1058 xfixes_reply->major_version, xfixes_reply->minor_version);
1062 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1064 if (formats_reply == NULL)
1067 formats = xcb_render_query_pict_formats_formats(formats_reply);
1068 for (i = 0; i < formats_reply->length; i++)
1069 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1070 formats[i].depth == 24)
1071 wm->render_format = formats[i];
1073 free(formats_reply);
1077 weston_wm_create_wm_window(struct weston_wm *wm)
1079 static const char name[] = "Weston WM";
1081 wm->wm_window = xcb_generate_id(wm->conn);
1082 xcb_create_window(wm->conn,
1083 XCB_COPY_FROM_PARENT,
1089 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1090 wm->screen->root_visual,
1093 xcb_change_property(wm->conn,
1094 XCB_PROP_MODE_REPLACE,
1096 wm->atom.net_supporting_wm_check,
1101 xcb_change_property(wm->conn,
1102 XCB_PROP_MODE_REPLACE,
1104 wm->atom.net_wm_name,
1105 wm->atom.utf8_string,
1107 strlen(name), name);
1109 xcb_change_property(wm->conn,
1110 XCB_PROP_MODE_REPLACE,
1112 wm->atom.net_supporting_wm_check,
1117 /* Claim the WM_S0 selection even though we don't suport
1118 * the --replace functionality. */
1119 xcb_set_selection_owner(wm->conn,
1122 XCB_TIME_CURRENT_TIME);
1126 weston_wm_create(struct weston_xserver *wxs)
1128 struct weston_wm *wm;
1129 struct wl_event_loop *loop;
1130 xcb_screen_iterator_t s;
1133 xcb_atom_t supported[1];
1135 wm = malloc(sizeof *wm);
1139 memset(wm, 0, sizeof *wm);
1141 wm->window_hash = hash_table_create();
1142 if (wm->window_hash == NULL) {
1147 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1148 weston_log("socketpair failed\n");
1149 hash_table_destroy(wm->window_hash);
1154 xserver_send_client(wxs->resource, sv[1]);
1155 wl_client_flush(wxs->resource->client);
1158 /* xcb_connect_to_fd takes ownership of the fd. */
1159 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1160 if (xcb_connection_has_error(wm->conn)) {
1161 weston_log("xcb_connect_to_fd failed\n");
1163 hash_table_destroy(wm->window_hash);
1168 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1169 wm->screen = s.data;
1171 loop = wl_display_get_event_loop(wxs->wl_display);
1173 wl_event_loop_add_fd(loop, sv[0],
1175 weston_wm_handle_event, wm);
1176 wl_event_source_check(wm->source);
1178 wxs_wm_get_resources(wm);
1181 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1182 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1183 XCB_EVENT_MASK_PROPERTY_CHANGE;
1184 xcb_change_window_attributes(wm->conn, wm->screen->root,
1185 XCB_CW_EVENT_MASK, values);
1186 wm->theme = theme_create();
1188 weston_wm_create_wm_window(wm);
1190 supported[0] = wm->atom.net_wm_moveresize;
1191 xcb_change_property(wm->conn,
1192 XCB_PROP_MODE_REPLACE,
1194 wm->atom.net_supported,
1197 ARRAY_LENGTH(supported), supported);
1199 weston_wm_selection_init(wm);
1201 xcb_flush(wm->conn);
1203 wm->activate_listener.notify = weston_wm_window_activate;
1204 wl_signal_add(&wxs->compositor->activate_signal,
1205 &wm->activate_listener);
1207 weston_log("created wm\n");
1213 weston_wm_destroy(struct weston_wm *wm)
1215 /* FIXME: Free windows in hash. */
1216 hash_table_destroy(wm->window_hash);
1217 xcb_disconnect(wm->conn);
1218 wl_event_source_remove(wm->source);
1219 wl_list_remove(&wm->selection_listener.link);
1220 wl_list_remove(&wm->activate_listener.link);
1226 surface_destroy(struct wl_listener *listener, void *data)
1228 struct weston_wm_window *window =
1229 container_of(listener,
1230 struct weston_wm_window, surface_destroy_listener);
1232 weston_log("surface for xid %d destroyed\n", window->id);
1235 static struct weston_wm_window *
1236 get_wm_window(struct weston_surface *surface)
1238 struct wl_resource *resource = &surface->surface.resource;
1239 struct wl_listener *listener;
1241 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1243 return container_of(listener, struct weston_wm_window,
1244 surface_destroy_listener);
1250 weston_wm_window_configure(void *data)
1252 struct weston_wm_window *window = data;
1253 struct weston_wm *wm = window->wm;
1257 values[0] = window->width;
1258 values[1] = window->height;
1259 xcb_configure_window(wm->conn,
1261 XCB_CONFIG_WINDOW_WIDTH |
1262 XCB_CONFIG_WINDOW_HEIGHT,
1265 weston_wm_window_get_frame_size(window, &width, &height);
1268 xcb_configure_window(wm->conn,
1270 XCB_CONFIG_WINDOW_WIDTH |
1271 XCB_CONFIG_WINDOW_HEIGHT,
1274 window->configure_source = NULL;
1276 weston_wm_window_schedule_repaint(window);
1280 send_configure(struct weston_surface *surface,
1281 uint32_t edges, int32_t width, int32_t height)
1283 struct weston_wm_window *window = get_wm_window(surface);
1284 struct weston_wm *wm = window->wm;
1285 struct theme *t = window->wm->theme;
1287 if (window->decorate) {
1288 window->width = width - 2 * (t->margin + t->width);
1289 window->height = height - 2 * t->margin -
1290 t->titlebar_height - t->width;
1292 window->width = width - 2 * t->margin;
1293 window->height = height - 2 * t->margin;
1296 if (window->configure_source)
1299 window->configure_source =
1300 wl_event_loop_add_idle(wm->server->loop,
1301 weston_wm_window_configure, window);
1304 static const struct weston_shell_client shell_client = {
1309 xserver_map_shell_surface(struct weston_wm *wm,
1310 struct weston_wm_window *window)
1312 struct weston_shell_interface *shell_interface =
1313 &wm->server->compositor->shell_interface;
1314 struct weston_wm_window *parent;
1315 struct theme *t = window->wm->theme;
1317 if (!shell_interface->create_shell_surface)
1321 shell_interface->create_shell_surface(shell_interface->shell,
1326 if (!window->override_redirect) {
1327 shell_interface->set_toplevel(window->shsurf);
1331 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1332 shell_interface->set_transient(window->shsurf, parent->shsurf,
1333 window->x - parent->x + t->margin + t->width,
1334 window->y - parent->y + t->margin + t->titlebar_height,
1335 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1339 xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1340 struct wl_resource *surface_resource, uint32_t id)
1342 struct weston_xserver *wxs = resource->data;
1343 struct weston_wm *wm = wxs->wm;
1344 struct wl_surface *surface = surface_resource->data;
1345 struct weston_wm_window *window;
1347 if (client != wxs->client)
1350 window = hash_table_lookup(wm->window_hash, id);
1351 if (window == NULL) {
1352 weston_log("set_window_id for unknown window %d\n", id);
1356 weston_log("set_window_id %d for surface %p\n", id, surface);
1358 weston_wm_window_read_properties(window);
1360 window->surface = (struct weston_surface *) surface;
1361 window->surface_destroy_listener.notify = surface_destroy;
1362 wl_signal_add(&surface->resource.destroy_signal,
1363 &window->surface_destroy_listener);
1365 weston_wm_window_schedule_repaint(window);
1366 xserver_map_shell_surface(wm, window);
1369 const struct xserver_interface xserver_implementation = {
1370 xserver_set_window_id