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>
34 #include <X11/Xcursor/Xcursor.h>
35 #include <linux/input.h>
39 #include "cairo-util.h"
40 #include "compositor.h"
43 struct wm_size_hints {
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
58 #define USPosition (1L << 0)
59 #define USSize (1L << 1)
60 #define PPosition (1L << 2)
61 #define PSize (1L << 3)
62 #define PMinSize (1L << 4)
63 #define PMaxSize (1L << 5)
64 #define PResizeInc (1L << 6)
65 #define PAspect (1L << 7)
66 #define PBaseSize (1L << 8)
67 #define PWinGravity (1L << 9)
69 struct motif_wm_hints {
77 #define MWM_HINTS_FUNCTIONS (1L << 0)
78 #define MWM_HINTS_DECORATIONS (1L << 1)
79 #define MWM_HINTS_INPUT_MODE (1L << 2)
80 #define MWM_HINTS_STATUS (1L << 3)
82 #define MWM_FUNC_ALL (1L << 0)
83 #define MWM_FUNC_RESIZE (1L << 1)
84 #define MWM_FUNC_MOVE (1L << 2)
85 #define MWM_FUNC_MINIMIZE (1L << 3)
86 #define MWM_FUNC_MAXIMIZE (1L << 4)
87 #define MWM_FUNC_CLOSE (1L << 5)
89 #define MWM_DECOR_ALL (1L << 0)
90 #define MWM_DECOR_BORDER (1L << 1)
91 #define MWM_DECOR_RESIZEH (1L << 2)
92 #define MWM_DECOR_TITLE (1L << 3)
93 #define MWM_DECOR_MENU (1L << 4)
94 #define MWM_DECOR_MINIMIZE (1L << 5)
95 #define MWM_DECOR_MAXIMIZE (1L << 6)
97 #define MWM_INPUT_MODELESS 0
98 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99 #define MWM_INPUT_SYSTEM_MODAL 2
100 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
101 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
103 #define MWM_TEAROFF_WINDOW (1L<<0)
105 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
107 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
118 struct weston_wm_window {
119 struct weston_wm *wm;
121 xcb_window_t frame_id;
123 cairo_surface_t *cairo_surface;
125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
127 struct weston_view *view;
128 struct wl_listener surface_destroy_listener;
129 struct wl_event_source *repaint_source;
130 struct wl_event_source *configure_source;
131 int properties_dirty;
136 struct weston_wm_window *transient_for;
141 int saved_width, saved_height;
143 int override_redirect;
147 struct wm_size_hints size_hints;
148 struct motif_wm_hints motif_hints;
152 static struct weston_wm_window *
153 get_wm_window(struct weston_surface *surface);
156 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
159 xserver_map_shell_surface(struct weston_wm_window *window,
160 struct weston_surface *surface);
162 static int __attribute__ ((format (printf, 1, 2)))
163 wm_log(const char *fmt, ...)
170 l = weston_vlog(fmt, argp);
179 static int __attribute__ ((format (printf, 1, 2)))
180 wm_log_continue(const char *fmt, ...)
187 l = weston_vlog_continue(fmt, argp);
198 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
200 xcb_get_atom_name_cookie_t cookie;
201 xcb_get_atom_name_reply_t *reply;
202 xcb_generic_error_t *e;
203 static char buffer[64];
205 if (atom == XCB_ATOM_NONE)
208 cookie = xcb_get_atom_name (c, atom);
209 reply = xcb_get_atom_name_reply (c, cookie, &e);
212 snprintf(buffer, sizeof buffer, "%.*s",
213 xcb_get_atom_name_name_length (reply),
214 xcb_get_atom_name_name (reply));
216 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
225 xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
227 xcb_connection_t *c = wm->conn;
228 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
229 xcb_screen_t *screen = s.data;
232 xcb_render_picture_t pic;
234 int stride = img->width * 4;
236 pix = xcb_generate_id(c);
237 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
239 pic = xcb_generate_id(c);
240 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
242 gc = xcb_generate_id(c);
243 xcb_create_gc(c, gc, pix, 0, 0);
245 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
246 img->width, img->height, 0, 0, 0, 32,
247 stride * img->height, (uint8_t *) img->pixels);
250 cursor = xcb_generate_id(c);
251 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
253 xcb_render_free_picture(c, pic);
254 xcb_free_pixmap(c, pix);
260 xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
262 /* TODO: treat animated cursors as well */
263 if (images->nimage != 1)
266 return xcb_cursor_image_load_cursor(wm, images->images[0]);
270 xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
273 XcursorImages *images;
280 v = getenv ("XCURSOR_SIZE");
287 images = XcursorLibraryLoadImages (file, NULL, size);
291 cursor = xcb_cursor_images_load_cursor (wm, images);
292 XcursorImagesDestroy (images);
298 dump_property(struct weston_wm *wm,
299 xcb_atom_t property, xcb_get_property_reply_t *reply)
302 const char *text_value, *name;
303 xcb_atom_t *atom_value;
307 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
309 wm_log_continue("(no reply)\n");
313 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
314 get_atom_name(wm->conn, reply->type),
316 xcb_get_property_value_length(reply),
319 if (reply->type == wm->atom.incr) {
320 incr_value = xcb_get_property_value(reply);
321 wm_log_continue("%d\n", *incr_value);
322 } else if (reply->type == wm->atom.utf8_string ||
323 reply->type == wm->atom.string) {
324 text_value = xcb_get_property_value(reply);
325 if (reply->value_len > 40)
328 len = reply->value_len;
329 wm_log_continue("\"%.*s\"\n", len, text_value);
330 } else if (reply->type == XCB_ATOM_ATOM) {
331 atom_value = xcb_get_property_value(reply);
332 for (i = 0; i < reply->value_len; i++) {
333 name = get_atom_name(wm->conn, atom_value[i]);
334 if (width + strlen(name) + 2 > 78) {
335 wm_log_continue("\n ");
338 width += wm_log_continue(", ");
341 width += wm_log_continue("%s", name);
343 wm_log_continue("\n");
345 wm_log_continue("huh?\n");
350 read_and_dump_property(struct weston_wm *wm,
351 xcb_window_t window, xcb_atom_t property)
353 xcb_get_property_reply_t *reply;
354 xcb_get_property_cookie_t cookie;
356 cookie = xcb_get_property(wm->conn, 0, window,
357 property, XCB_ATOM_ANY, 0, 2048);
358 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
360 dump_property(wm, property, reply);
365 /* We reuse some predefined, but otherwise useles atoms */
366 #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
367 #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
368 #define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
369 #define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
372 weston_wm_window_read_properties(struct weston_wm_window *window)
374 struct weston_wm *wm = window->wm;
375 struct weston_shell_interface *shell_interface =
376 &wm->server->compositor->shell_interface;
378 #define F(field) offsetof(struct weston_wm_window, field)
384 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
385 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
386 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
387 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
388 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
389 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
390 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
391 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
392 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
393 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
394 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
398 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
399 xcb_get_property_reply_t *reply;
405 if (!window->properties_dirty)
407 window->properties_dirty = 0;
409 for (i = 0; i < ARRAY_LENGTH(props); i++)
410 cookie[i] = xcb_get_property(wm->conn,
414 XCB_ATOM_ANY, 0, 2048);
416 window->decorate = !window->override_redirect;
417 window->size_hints.flags = 0;
418 window->motif_hints.flags = 0;
419 window->delete_window = 0;
421 for (i = 0; i < ARRAY_LENGTH(props); i++) {
422 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
424 /* Bad window, typically */
426 if (reply->type == XCB_ATOM_NONE) {
427 /* No such property */
432 p = ((char *) window + props[i].offset);
434 switch (props[i].type) {
435 case XCB_ATOM_WM_CLIENT_MACHINE:
436 case XCB_ATOM_STRING:
437 /* FIXME: We're using this for both string and
443 strndup(xcb_get_property_value(reply),
444 xcb_get_property_value_length(reply));
446 case XCB_ATOM_WINDOW:
447 xid = xcb_get_property_value(reply);
448 *(struct weston_wm_window **) p =
449 hash_table_lookup(wm->window_hash, *xid);
451 case XCB_ATOM_CARDINAL:
453 atom = xcb_get_property_value(reply);
454 *(xcb_atom_t *) p = *atom;
456 case TYPE_WM_PROTOCOLS:
457 atom = xcb_get_property_value(reply);
458 for (i = 0; i < reply->value_len; i++)
459 if (atom[i] == wm->atom.wm_delete_window)
460 window->delete_window = 1;
464 case TYPE_WM_NORMAL_HINTS:
465 memcpy(&window->size_hints,
466 xcb_get_property_value(reply),
467 sizeof window->size_hints);
469 case TYPE_NET_WM_STATE:
470 window->fullscreen = 0;
471 atom = xcb_get_property_value(reply);
472 for (i = 0; i < reply->value_len; i++)
473 if (atom[i] == wm->atom.net_wm_state_fullscreen)
474 window->fullscreen = 1;
476 case TYPE_MOTIF_WM_HINTS:
477 memcpy(&window->motif_hints,
478 xcb_get_property_value(reply),
479 sizeof window->motif_hints);
480 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
482 window->motif_hints.decorations > 0;
490 if (window->shsurf && window->name)
491 shell_interface->set_title(window->shsurf, window->name);
492 if (window->frame && window->name)
493 frame_set_title(window->frame, window->name);
497 weston_wm_window_get_frame_size(struct weston_wm_window *window,
498 int *width, int *height)
500 struct theme *t = window->wm->theme;
502 if (window->fullscreen) {
503 *width = window->width;
504 *height = window->height;
505 } else if (window->decorate && window->frame) {
506 *width = frame_width(window->frame);
507 *height = frame_height(window->frame);
509 *width = window->width + t->margin * 2;
510 *height = window->height + t->margin * 2;
515 weston_wm_window_get_child_position(struct weston_wm_window *window,
518 struct theme *t = window->wm->theme;
520 if (window->fullscreen) {
523 } else if (window->decorate && window->frame) {
524 frame_interior(window->frame, x, y, NULL, NULL);
532 weston_wm_window_send_configure_notify(struct weston_wm_window *window)
534 xcb_configure_notify_event_t configure_notify;
535 struct weston_wm *wm = window->wm;
538 weston_wm_window_get_child_position(window, &x, &y);
539 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
540 configure_notify.pad0 = 0;
541 configure_notify.event = window->id;
542 configure_notify.window = window->id;
543 configure_notify.above_sibling = XCB_WINDOW_NONE;
544 configure_notify.x = x;
545 configure_notify.y = y;
546 configure_notify.width = window->width;
547 configure_notify.height = window->height;
548 configure_notify.border_width = 0;
549 configure_notify.override_redirect = 0;
550 configure_notify.pad1 = 0;
552 xcb_send_event(wm->conn, 0, window->id,
553 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
554 (char *) &configure_notify);
558 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
560 xcb_configure_request_event_t *configure_request =
561 (xcb_configure_request_event_t *) event;
562 struct weston_wm_window *window;
563 uint32_t mask, values[16];
564 int x, y, width, height, i = 0;
566 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
567 configure_request->window,
568 configure_request->x, configure_request->y,
569 configure_request->width, configure_request->height);
571 window = hash_table_lookup(wm->window_hash, configure_request->window);
573 if (window->fullscreen) {
574 weston_wm_window_send_configure_notify(window);
578 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
579 window->width = configure_request->width;
580 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
581 window->height = configure_request->height;
584 frame_resize_inside(window->frame, window->width, window->height);
586 weston_wm_window_get_child_position(window, &x, &y);
589 values[i++] = window->width;
590 values[i++] = window->height;
592 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
593 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
594 XCB_CONFIG_WINDOW_BORDER_WIDTH;
595 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
596 values[i++] = configure_request->sibling;
597 mask |= XCB_CONFIG_WINDOW_SIBLING;
599 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
600 values[i++] = configure_request->stack_mode;
601 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
604 xcb_configure_window(wm->conn, window->id, mask, values);
606 weston_wm_window_get_frame_size(window, &width, &height);
609 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
610 xcb_configure_window(wm->conn, window->frame_id, mask, values);
612 weston_wm_window_schedule_repaint(window);
616 our_resource(struct weston_wm *wm, uint32_t id)
618 const xcb_setup_t *setup;
620 setup = xcb_get_setup(wm->conn);
622 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
626 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
628 xcb_configure_notify_event_t *configure_notify =
629 (xcb_configure_notify_event_t *) event;
630 struct weston_wm_window *window;
632 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
633 configure_notify->window,
634 configure_notify->x, configure_notify->y,
635 configure_notify->width, configure_notify->height);
637 window = hash_table_lookup(wm->window_hash, configure_notify->window);
638 window->x = configure_notify->x;
639 window->y = configure_notify->y;
640 if (window->override_redirect) {
641 window->width = configure_notify->width;
642 window->height = configure_notify->height;
644 frame_resize_inside(window->frame,
645 window->width, window->height);
650 weston_wm_kill_client(struct wl_listener *listener, void *data)
652 struct weston_surface *surface = data;
653 struct weston_wm_window *window = get_wm_window(surface);
659 gethostname(name, 1024);
661 /* this is only one heuristic to guess the PID of a client is valid,
662 * assuming it's compliant with icccm and ewmh. Non-compliants and
663 * remote applications of course fail. */
664 if (!strcmp(window->machine, name) && window->pid != 0)
665 kill(window->pid, SIGKILL);
669 weston_wm_create_surface(struct wl_listener *listener, void *data)
671 struct weston_surface *surface = data;
672 struct weston_wm *wm =
673 container_of(listener,
674 struct weston_wm, create_surface_listener);
675 struct weston_wm_window *window;
677 if (wl_resource_get_client(surface->resource) != wm->server->client)
680 wl_list_for_each(window, &wm->unpaired_window_list, link)
681 if (window->surface_id ==
682 wl_resource_get_id(surface->resource)) {
683 xserver_map_shell_surface(window, surface);
684 window->surface_id = 0;
685 wl_list_remove(&window->link);
691 weston_wm_window_activate(struct wl_listener *listener, void *data)
693 struct weston_surface *surface = data;
694 struct weston_wm_window *window = NULL;
695 struct weston_wm *wm =
696 container_of(listener, struct weston_wm, activate_listener);
697 xcb_client_message_event_t client_message;
700 window = get_wm_window(surface);
704 client_message.response_type = XCB_CLIENT_MESSAGE;
705 client_message.format = 32;
706 client_message.window = window->id;
707 client_message.type = wm->atom.wm_protocols;
708 client_message.data.data32[0] = wm->atom.wm_take_focus;
709 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
711 xcb_send_event(wm->conn, 0, window->id,
712 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
713 (char *) &client_message);
715 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
716 window->id, XCB_TIME_CURRENT_TIME);
718 xcb_set_input_focus (wm->conn,
719 XCB_INPUT_FOCUS_POINTER_ROOT,
721 XCB_TIME_CURRENT_TIME);
724 if (wm->focus_window) {
725 if (wm->focus_window->frame)
726 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
727 weston_wm_window_schedule_repaint(wm->focus_window);
729 wm->focus_window = window;
730 if (wm->focus_window) {
731 if (wm->focus_window->frame)
732 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
733 weston_wm_window_schedule_repaint(wm->focus_window);
738 weston_wm_window_transform(struct wl_listener *listener, void *data)
740 struct weston_surface *surface = data;
741 struct weston_wm_window *window = get_wm_window(surface);
742 struct weston_wm *wm =
743 container_of(listener, struct weston_wm, transform_listener);
744 uint32_t mask, values[2];
749 if (!window->view || !weston_view_is_mapped(window->view))
752 if (window->x != window->view->geometry.x ||
753 window->y != window->view->geometry.y) {
754 values[0] = window->view->geometry.x;
755 values[1] = window->view->geometry.y;
756 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
758 xcb_configure_window(wm->conn, window->frame_id, mask, values);
763 #define ICCCM_WITHDRAWN_STATE 0
764 #define ICCCM_NORMAL_STATE 1
765 #define ICCCM_ICONIC_STATE 3
768 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
770 struct weston_wm *wm = window->wm;
771 uint32_t property[2];
774 property[1] = XCB_WINDOW_NONE;
776 xcb_change_property(wm->conn,
777 XCB_PROP_MODE_REPLACE,
786 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
788 struct weston_wm *wm = window->wm;
789 uint32_t property[1];
793 if (window->fullscreen)
794 property[i++] = wm->atom.net_wm_state_fullscreen;
796 xcb_change_property(wm->conn,
797 XCB_PROP_MODE_REPLACE,
799 wm->atom.net_wm_state,
806 weston_wm_window_create_frame(struct weston_wm_window *window)
808 struct weston_wm *wm = window->wm;
810 int x, y, width, height;
812 window->frame = frame_create(window->wm->theme,
813 window->width, window->height,
814 FRAME_BUTTON_CLOSE, window->name);
815 frame_resize_inside(window->frame, window->width, window->height);
817 weston_wm_window_get_frame_size(window, &width, &height);
818 weston_wm_window_get_child_position(window, &x, &y);
820 values[0] = wm->screen->black_pixel;
822 XCB_EVENT_MASK_KEY_PRESS |
823 XCB_EVENT_MASK_KEY_RELEASE |
824 XCB_EVENT_MASK_BUTTON_PRESS |
825 XCB_EVENT_MASK_BUTTON_RELEASE |
826 XCB_EVENT_MASK_POINTER_MOTION |
827 XCB_EVENT_MASK_ENTER_WINDOW |
828 XCB_EVENT_MASK_LEAVE_WINDOW |
829 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
830 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
831 values[2] = wm->colormap;
833 window->frame_id = xcb_generate_id(wm->conn);
834 xcb_create_window(wm->conn,
841 XCB_WINDOW_CLASS_INPUT_OUTPUT,
843 XCB_CW_BORDER_PIXEL |
845 XCB_CW_COLORMAP, values);
847 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
850 xcb_configure_window(wm->conn, window->id,
851 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
853 window->cairo_surface =
854 cairo_xcb_surface_create_with_xrender_format(wm->conn,
860 hash_table_insert(wm->window_hash, window->frame_id, window);
864 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
866 xcb_map_request_event_t *map_request =
867 (xcb_map_request_event_t *) event;
868 struct weston_wm_window *window;
870 if (our_resource(wm, map_request->window)) {
871 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
872 map_request->window);
876 window = hash_table_lookup(wm->window_hash, map_request->window);
878 weston_wm_window_read_properties(window);
880 if (window->frame_id == XCB_WINDOW_NONE)
881 weston_wm_window_create_frame(window);
883 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
884 window->id, window, window->frame_id);
886 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
887 weston_wm_window_set_net_wm_state(window);
889 xcb_map_window(wm->conn, map_request->window);
890 xcb_map_window(wm->conn, window->frame_id);
894 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
896 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
898 if (our_resource(wm, map_notify->window)) {
899 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
904 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
908 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
910 xcb_unmap_notify_event_t *unmap_notify =
911 (xcb_unmap_notify_event_t *) event;
912 struct weston_wm_window *window;
914 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
915 unmap_notify->window,
917 our_resource(wm, unmap_notify->window) ? ", ours" : "");
919 if (our_resource(wm, unmap_notify->window))
922 if (unmap_notify->response_type & SEND_EVENT_MASK)
923 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
924 * as it may come in after we've destroyed the window. */
927 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
928 if (wm->focus_window == window)
929 wm->focus_window = NULL;
931 wl_list_remove(&window->surface_destroy_listener.link);
932 window->surface = NULL;
933 window->shsurf = NULL;
935 xcb_unmap_window(wm->conn, window->frame_id);
939 weston_wm_window_draw_decoration(void *data)
941 struct weston_wm_window *window = data;
942 struct weston_wm *wm = window->wm;
943 struct theme *t = wm->theme;
945 int x, y, width, height;
946 int32_t input_x, input_y, input_w, input_h;
947 struct weston_shell_interface *shell_interface =
948 &wm->server->compositor->shell_interface;
951 weston_wm_window_read_properties(window);
953 window->repaint_source = NULL;
955 weston_wm_window_get_frame_size(window, &width, &height);
956 weston_wm_window_get_child_position(window, &x, &y);
958 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
959 cr = cairo_create(window->cairo_surface);
961 if (window->fullscreen) {
963 } else if (window->decorate) {
964 if (wm->focus_window == window)
965 flags |= THEME_FRAME_ACTIVE;
967 frame_repaint(window->frame, cr);
969 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
970 cairo_set_source_rgba(cr, 0, 0, 0, 0);
973 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
974 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
975 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
980 if (window->surface) {
981 pixman_region32_fini(&window->surface->pending.opaque);
982 if(window->has_alpha) {
983 pixman_region32_init(&window->surface->pending.opaque);
985 /* We leave an extra pixel around the X window area to
986 * make sure we don't sample from the undefined alpha
987 * channel when filtering. */
988 pixman_region32_init_rect(&window->surface->pending.opaque,
994 weston_view_geometry_dirty(window->view);
996 pixman_region32_fini(&window->surface->pending.input);
998 if (window->fullscreen) {
1001 input_w = window->width;
1002 input_h = window->height;
1003 } else if (window->decorate) {
1004 frame_input_rect(window->frame, &input_x, &input_y,
1005 &input_w, &input_h);
1008 pixman_region32_init_rect(&window->surface->pending.input,
1009 input_x, input_y, input_w, input_h);
1011 shell_interface->set_margin(window->shsurf,
1013 width - input_w - input_x,
1015 height - input_h - input_y);
1020 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1022 struct weston_wm *wm = window->wm;
1025 if (window->frame_id == XCB_WINDOW_NONE) {
1026 if (window->surface != NULL) {
1027 weston_wm_window_get_frame_size(window, &width, &height);
1028 pixman_region32_fini(&window->surface->pending.opaque);
1029 if(window->has_alpha) {
1030 pixman_region32_init(&window->surface->pending.opaque);
1032 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1036 weston_view_geometry_dirty(window->view);
1041 if (window->repaint_source)
1044 window->repaint_source =
1045 wl_event_loop_add_idle(wm->server->loop,
1046 weston_wm_window_draw_decoration,
1051 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1053 xcb_property_notify_event_t *property_notify =
1054 (xcb_property_notify_event_t *) event;
1055 struct weston_wm_window *window;
1057 window = hash_table_lookup(wm->window_hash, property_notify->window);
1061 window->properties_dirty = 1;
1063 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
1064 if (property_notify->state == XCB_PROPERTY_DELETE)
1065 wm_log("deleted\n");
1067 read_and_dump_property(wm, property_notify->window,
1068 property_notify->atom);
1070 if (property_notify->atom == wm->atom.net_wm_name ||
1071 property_notify->atom == XCB_ATOM_WM_NAME)
1072 weston_wm_window_schedule_repaint(window);
1076 weston_wm_window_create(struct weston_wm *wm,
1077 xcb_window_t id, int width, int height, int x, int y, int override)
1079 struct weston_wm_window *window;
1081 xcb_get_geometry_cookie_t geometry_cookie;
1082 xcb_get_geometry_reply_t *geometry_reply;
1084 window = zalloc(sizeof *window);
1085 if (window == NULL) {
1086 wm_log("failed to allocate window\n");
1090 geometry_cookie = xcb_get_geometry(wm->conn, id);
1092 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1093 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1097 window->properties_dirty = 1;
1098 window->override_redirect = override;
1099 window->width = width;
1100 window->height = height;
1104 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1105 /* technically we should use XRender and check the visual format's
1106 alpha_mask, but checking depth is simpler and works in all known cases */
1107 if(geometry_reply != NULL)
1108 window->has_alpha = geometry_reply->depth == 32;
1109 free(geometry_reply);
1111 hash_table_insert(wm->window_hash, id, window);
1115 weston_wm_window_destroy(struct weston_wm_window *window)
1117 struct weston_wm *wm = window->wm;
1119 if (window->repaint_source)
1120 wl_event_source_remove(window->repaint_source);
1121 if (window->cairo_surface)
1122 cairo_surface_destroy(window->cairo_surface);
1124 if (window->frame_id) {
1125 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1126 xcb_destroy_window(wm->conn, window->frame_id);
1127 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1128 hash_table_remove(wm->window_hash, window->frame_id);
1129 window->frame_id = XCB_WINDOW_NONE;
1132 if (window->surface_id)
1133 wl_list_remove(&window->link);
1135 hash_table_remove(window->wm->window_hash, window->id);
1140 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1142 xcb_create_notify_event_t *create_notify =
1143 (xcb_create_notify_event_t *) event;
1145 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1146 create_notify->window,
1147 create_notify->width, create_notify->height,
1148 create_notify->override_redirect ? ", override" : "",
1149 our_resource(wm, create_notify->window) ? ", ours" : "");
1151 if (our_resource(wm, create_notify->window))
1154 weston_wm_window_create(wm, create_notify->window,
1155 create_notify->width, create_notify->height,
1156 create_notify->x, create_notify->y,
1157 create_notify->override_redirect);
1161 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1163 xcb_destroy_notify_event_t *destroy_notify =
1164 (xcb_destroy_notify_event_t *) event;
1165 struct weston_wm_window *window;
1167 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1168 destroy_notify->window,
1169 destroy_notify->event,
1170 our_resource(wm, destroy_notify->window) ? ", ours" : "");
1172 if (our_resource(wm, destroy_notify->window))
1175 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
1176 weston_wm_window_destroy(window);
1180 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1182 xcb_reparent_notify_event_t *reparent_notify =
1183 (xcb_reparent_notify_event_t *) event;
1184 struct weston_wm_window *window;
1186 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1187 reparent_notify->window,
1188 reparent_notify->parent,
1189 reparent_notify->event);
1191 if (reparent_notify->parent == wm->screen->root) {
1192 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1193 reparent_notify->x, reparent_notify->y,
1194 reparent_notify->override_redirect);
1195 } else if (!our_resource(wm, reparent_notify->parent)) {
1196 window = hash_table_lookup(wm->window_hash,
1197 reparent_notify->window);
1198 weston_wm_window_destroy(window);
1202 struct weston_seat *
1203 weston_wm_pick_seat(struct weston_wm *wm)
1205 return container_of(wm->server->compositor->seat_list.next,
1206 struct weston_seat, link);
1209 static struct weston_seat *
1210 weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1212 struct weston_wm *wm = window->wm;
1213 struct weston_seat *seat, *s;
1216 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
1217 if (s->pointer != NULL &&
1218 s->pointer->focus == window->view &&
1219 s->pointer->button_count > 0 &&
1221 s->pointer->grab_serial -
1222 seat->pointer->grab_serial < (1 << 30)))
1230 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1231 xcb_client_message_event_t *client_message)
1233 static const int map[] = {
1234 THEME_LOCATION_RESIZING_TOP_LEFT,
1235 THEME_LOCATION_RESIZING_TOP,
1236 THEME_LOCATION_RESIZING_TOP_RIGHT,
1237 THEME_LOCATION_RESIZING_RIGHT,
1238 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1239 THEME_LOCATION_RESIZING_BOTTOM,
1240 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1241 THEME_LOCATION_RESIZING_LEFT
1244 struct weston_wm *wm = window->wm;
1245 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
1247 struct weston_shell_interface *shell_interface =
1248 &wm->server->compositor->shell_interface;
1250 if (seat->pointer->button_count != 1 || !window->view
1251 || seat->pointer->focus != window->view)
1254 detail = client_message->data.data32[2];
1256 case _NET_WM_MOVERESIZE_MOVE:
1257 shell_interface->move(window->shsurf, seat);
1259 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1260 case _NET_WM_MOVERESIZE_SIZE_TOP:
1261 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1262 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1263 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1264 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1265 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1266 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1267 shell_interface->resize(window->shsurf, seat, map[detail]);
1269 case _NET_WM_MOVERESIZE_CANCEL:
1274 #define _NET_WM_STATE_REMOVE 0
1275 #define _NET_WM_STATE_ADD 1
1276 #define _NET_WM_STATE_TOGGLE 2
1279 update_state(int action, int *state)
1281 int new_state, changed;
1284 case _NET_WM_STATE_REMOVE:
1287 case _NET_WM_STATE_ADD:
1290 case _NET_WM_STATE_TOGGLE:
1291 new_state = !*state;
1297 changed = (*state != new_state);
1304 weston_wm_window_configure(void *data);
1307 weston_wm_window_handle_state(struct weston_wm_window *window,
1308 xcb_client_message_event_t *client_message)
1310 struct weston_wm *wm = window->wm;
1311 struct weston_shell_interface *shell_interface =
1312 &wm->server->compositor->shell_interface;
1313 uint32_t action, property;
1315 action = client_message->data.data32[0];
1316 property = client_message->data.data32[1];
1318 if (property == wm->atom.net_wm_state_fullscreen &&
1319 update_state(action, &window->fullscreen)) {
1320 weston_wm_window_set_net_wm_state(window);
1321 if (window->fullscreen) {
1322 window->saved_width = window->width;
1323 window->saved_height = window->height;
1326 shell_interface->set_fullscreen(window->shsurf,
1327 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1330 shell_interface->set_toplevel(window->shsurf);
1331 window->width = window->saved_width;
1332 window->height = window->saved_height;
1334 frame_resize_inside(window->frame,
1337 weston_wm_window_configure(window);
1343 surface_destroy(struct wl_listener *listener, void *data)
1345 struct weston_wm_window *window =
1346 container_of(listener,
1347 struct weston_wm_window, surface_destroy_listener);
1349 wm_log("surface for xid %d destroyed\n", window->id);
1351 /* This should have been freed by the shell.
1352 * Don't try to use it later. */
1353 window->shsurf = NULL;
1354 window->surface = NULL;
1355 window->view = NULL;
1359 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1360 xcb_client_message_event_t *client_message)
1362 struct weston_wm *wm = window->wm;
1363 struct wl_resource *resource;
1365 if (window->surface_id != 0) {
1366 wm_log("already have surface id for window %d\n", window->id);
1370 /* Xwayland will send the wayland requests to create the
1371 * wl_surface before sending this client message. Even so, we
1372 * can end up handling the X event before the wayland requests
1373 * and thus when we try to look up the surface ID, the surface
1374 * hasn't been created yet. In that case put the window on
1375 * the unpaired window list and continue when the surface gets
1377 window->surface_id = client_message->data.data32[0];
1378 resource = wl_client_get_object(wm->server->client,
1379 window->surface_id);
1381 xserver_map_shell_surface(window,
1382 wl_resource_get_user_data(resource));
1384 wl_list_insert(&wm->unpaired_window_list, &window->link);
1388 weston_wm_handle_client_message(struct weston_wm *wm,
1389 xcb_generic_event_t *event)
1391 xcb_client_message_event_t *client_message =
1392 (xcb_client_message_event_t *) event;
1393 struct weston_wm_window *window;
1395 window = hash_table_lookup(wm->window_hash, client_message->window);
1397 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1398 get_atom_name(wm->conn, client_message->type),
1399 client_message->data.data32[0],
1400 client_message->data.data32[1],
1401 client_message->data.data32[2],
1402 client_message->data.data32[3],
1403 client_message->data.data32[4],
1404 client_message->window);
1406 if (client_message->type == wm->atom.net_wm_moveresize)
1407 weston_wm_window_handle_moveresize(window, client_message);
1408 else if (client_message->type == wm->atom.net_wm_state)
1409 weston_wm_window_handle_state(window, client_message);
1410 else if (client_message->type == wm->atom.wl_surface_id)
1411 weston_wm_window_handle_surface_id(window, client_message);
1419 XWM_CURSOR_TOP_LEFT,
1420 XWM_CURSOR_TOP_RIGHT,
1421 XWM_CURSOR_BOTTOM_LEFT,
1422 XWM_CURSOR_BOTTOM_RIGHT,
1423 XWM_CURSOR_LEFT_PTR,
1427 * The following correspondences between file names and cursors was copied
1428 * from: https://bugs.kde.org/attachment.cgi?id=67313
1431 static const char *bottom_left_corners[] = {
1432 "bottom_left_corner",
1437 static const char *bottom_right_corners[] = {
1438 "bottom_right_corner",
1443 static const char *bottom_sides[] = {
1449 static const char *left_ptrs[] = {
1456 static const char *left_sides[] = {
1462 static const char *right_sides[] = {
1468 static const char *top_left_corners[] = {
1474 static const char *top_right_corners[] = {
1480 static const char *top_sides[] = {
1486 struct cursor_alternatives {
1491 static const struct cursor_alternatives cursors[] = {
1492 {top_sides, ARRAY_LENGTH(top_sides)},
1493 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1494 {left_sides, ARRAY_LENGTH(left_sides)},
1495 {right_sides, ARRAY_LENGTH(right_sides)},
1496 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1497 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1498 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1499 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1500 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1504 weston_wm_create_cursors(struct weston_wm *wm)
1507 int i, count = ARRAY_LENGTH(cursors);
1510 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1511 for (i = 0; i < count; i++) {
1512 for (j = 0; j < cursors[i].count; j++) {
1513 name = cursors[i].names[j];
1515 xcb_cursor_library_load_cursor(wm, name);
1516 if (wm->cursors[i] != (xcb_cursor_t)-1)
1521 wm->last_cursor = -1;
1525 weston_wm_destroy_cursors(struct weston_wm *wm)
1529 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1530 xcb_free_cursor(wm->conn, wm->cursors[i]);
1536 get_cursor_for_location(enum theme_location location)
1538 // int location = theme_get_location(t, x, y, width, height, 0);
1541 case THEME_LOCATION_RESIZING_TOP:
1542 return XWM_CURSOR_TOP;
1543 case THEME_LOCATION_RESIZING_BOTTOM:
1544 return XWM_CURSOR_BOTTOM;
1545 case THEME_LOCATION_RESIZING_LEFT:
1546 return XWM_CURSOR_LEFT;
1547 case THEME_LOCATION_RESIZING_RIGHT:
1548 return XWM_CURSOR_RIGHT;
1549 case THEME_LOCATION_RESIZING_TOP_LEFT:
1550 return XWM_CURSOR_TOP_LEFT;
1551 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1552 return XWM_CURSOR_TOP_RIGHT;
1553 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1554 return XWM_CURSOR_BOTTOM_LEFT;
1555 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1556 return XWM_CURSOR_BOTTOM_RIGHT;
1557 case THEME_LOCATION_EXTERIOR:
1558 case THEME_LOCATION_TITLEBAR:
1560 return XWM_CURSOR_LEFT_PTR;
1565 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1568 uint32_t cursor_value_list;
1570 if (wm->last_cursor == cursor)
1573 wm->last_cursor = cursor;
1575 cursor_value_list = wm->cursors[cursor];
1576 xcb_change_window_attributes (wm->conn, window_id,
1577 XCB_CW_CURSOR, &cursor_value_list);
1578 xcb_flush(wm->conn);
1582 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1584 xcb_client_message_event_t client_message;
1586 if (window->delete_window) {
1587 client_message.response_type = XCB_CLIENT_MESSAGE;
1588 client_message.format = 32;
1589 client_message.window = window->id;
1590 client_message.type = window->wm->atom.wm_protocols;
1591 client_message.data.data32[0] =
1592 window->wm->atom.wm_delete_window;
1593 client_message.data.data32[1] = time;
1595 xcb_send_event(window->wm->conn, 0, window->id,
1596 XCB_EVENT_MASK_NO_EVENT,
1597 (char *) &client_message);
1599 xcb_kill_client(window->wm->conn, window->id);
1604 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1606 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1607 struct weston_shell_interface *shell_interface =
1608 &wm->server->compositor->shell_interface;
1609 struct weston_seat *seat;
1610 struct weston_wm_window *window;
1611 enum theme_location location;
1612 enum frame_button_state button_state;
1615 wm_log("XCB_BUTTON_%s (detail %d)\n",
1616 button->response_type == XCB_BUTTON_PRESS ?
1617 "PRESS" : "RELEASE", button->detail);
1619 window = hash_table_lookup(wm->window_hash, button->event);
1620 if (!window || !window->decorate)
1623 if (button->detail != 1 && button->detail != 2)
1626 seat = weston_wm_pick_seat_for_window(window);
1628 button_state = button->response_type == XCB_BUTTON_PRESS ?
1629 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1630 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1632 /* Make sure we're looking at the right location. The frame
1633 * could have received a motion event from a pointer from a
1634 * different wl_seat, but under X it looks like our core
1635 * pointer moved. Move the frame pointer to the button press
1636 * location before deciding what to do. */
1637 location = frame_pointer_motion(window->frame, NULL,
1638 button->event_x, button->event_y);
1639 location = frame_pointer_button(window->frame, NULL,
1640 button_id, button_state);
1641 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1642 weston_wm_window_schedule_repaint(window);
1644 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1645 shell_interface->move(window->shsurf, seat);
1646 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1649 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1650 shell_interface->resize(window->shsurf, seat, location);
1651 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1654 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1655 weston_wm_window_close(window, button->time);
1656 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
1661 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1663 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1664 struct weston_wm_window *window;
1665 enum theme_location location;
1668 window = hash_table_lookup(wm->window_hash, motion->event);
1669 if (!window || !window->decorate)
1672 location = frame_pointer_motion(window->frame, NULL,
1673 motion->event_x, motion->event_y);
1674 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1675 weston_wm_window_schedule_repaint(window);
1677 cursor = get_cursor_for_location(location);
1678 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1682 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1684 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1685 struct weston_wm_window *window;
1686 enum theme_location location;
1689 window = hash_table_lookup(wm->window_hash, enter->event);
1690 if (!window || !window->decorate)
1693 location = frame_pointer_enter(window->frame, NULL,
1694 enter->event_x, enter->event_y);
1695 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1696 weston_wm_window_schedule_repaint(window);
1698 cursor = get_cursor_for_location(location);
1699 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1703 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1705 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1706 struct weston_wm_window *window;
1708 window = hash_table_lookup(wm->window_hash, leave->event);
1709 if (!window || !window->decorate)
1712 frame_pointer_leave(window->frame, NULL);
1713 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1714 weston_wm_window_schedule_repaint(window);
1716 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
1720 weston_wm_handle_event(int fd, uint32_t mask, void *data)
1722 struct weston_wm *wm = data;
1723 xcb_generic_event_t *event;
1726 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1727 if (weston_wm_handle_selection_event(wm, event)) {
1733 if (weston_wm_handle_dnd_event(wm, event)) {
1739 switch (EVENT_TYPE(event)) {
1740 case XCB_BUTTON_PRESS:
1741 case XCB_BUTTON_RELEASE:
1742 weston_wm_handle_button(wm, event);
1744 case XCB_ENTER_NOTIFY:
1745 weston_wm_handle_enter(wm, event);
1747 case XCB_LEAVE_NOTIFY:
1748 weston_wm_handle_leave(wm, event);
1750 case XCB_MOTION_NOTIFY:
1751 weston_wm_handle_motion(wm, event);
1753 case XCB_CREATE_NOTIFY:
1754 weston_wm_handle_create_notify(wm, event);
1756 case XCB_MAP_REQUEST:
1757 weston_wm_handle_map_request(wm, event);
1759 case XCB_MAP_NOTIFY:
1760 weston_wm_handle_map_notify(wm, event);
1762 case XCB_UNMAP_NOTIFY:
1763 weston_wm_handle_unmap_notify(wm, event);
1765 case XCB_REPARENT_NOTIFY:
1766 weston_wm_handle_reparent_notify(wm, event);
1768 case XCB_CONFIGURE_REQUEST:
1769 weston_wm_handle_configure_request(wm, event);
1771 case XCB_CONFIGURE_NOTIFY:
1772 weston_wm_handle_configure_notify(wm, event);
1774 case XCB_DESTROY_NOTIFY:
1775 weston_wm_handle_destroy_notify(wm, event);
1777 case XCB_MAPPING_NOTIFY:
1778 wm_log("XCB_MAPPING_NOTIFY\n");
1780 case XCB_PROPERTY_NOTIFY:
1781 weston_wm_handle_property_notify(wm, event);
1783 case XCB_CLIENT_MESSAGE:
1784 weston_wm_handle_client_message(wm, event);
1792 xcb_flush(wm->conn);
1798 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1800 xcb_depth_iterator_t d_iter;
1801 xcb_visualtype_iterator_t vt_iter;
1802 xcb_visualtype_t *visualtype;
1804 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1806 while (d_iter.rem > 0) {
1807 if (d_iter.data->depth == 32) {
1808 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1809 visualtype = vt_iter.data;
1813 xcb_depth_next(&d_iter);
1816 if (visualtype == NULL) {
1817 weston_log("no 32 bit visualtype\n");
1821 wm->visual_id = visualtype->visual_id;
1822 wm->colormap = xcb_generate_id(wm->conn);
1823 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1824 wm->colormap, wm->screen->root, wm->visual_id);
1828 weston_wm_get_resources(struct weston_wm *wm)
1831 #define F(field) offsetof(struct weston_wm, field)
1833 static const struct { const char *name; int offset; } atoms[] = {
1834 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1835 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1836 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1837 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1838 { "WM_STATE", F(atom.wm_state) },
1839 { "WM_S0", F(atom.wm_s0) },
1840 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
1841 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
1842 { "_NET_WM_NAME", F(atom.net_wm_name) },
1843 { "_NET_WM_PID", F(atom.net_wm_pid) },
1844 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1845 { "_NET_WM_STATE", F(atom.net_wm_state) },
1846 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1847 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1848 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1849 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1851 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1852 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1853 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1854 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1855 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1856 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1857 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
1858 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1859 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
1860 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1861 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1862 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1863 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1864 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1866 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1867 { "_NET_SUPPORTING_WM_CHECK",
1868 F(atom.net_supporting_wm_check) },
1869 { "_NET_SUPPORTED", F(atom.net_supported) },
1870 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1871 { "CLIPBOARD", F(atom.clipboard) },
1872 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
1873 { "TARGETS", F(atom.targets) },
1874 { "UTF8_STRING", F(atom.utf8_string) },
1875 { "_WL_SELECTION", F(atom.wl_selection) },
1876 { "INCR", F(atom.incr) },
1877 { "TIMESTAMP", F(atom.timestamp) },
1878 { "MULTIPLE", F(atom.multiple) },
1879 { "UTF8_STRING" , F(atom.utf8_string) },
1880 { "COMPOUND_TEXT", F(atom.compound_text) },
1881 { "TEXT", F(atom.text) },
1882 { "STRING", F(atom.string) },
1883 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1884 { "text/plain", F(atom.text_plain) },
1885 { "XdndSelection", F(atom.xdnd_selection) },
1886 { "XdndAware", F(atom.xdnd_aware) },
1887 { "XdndEnter", F(atom.xdnd_enter) },
1888 { "XdndLeave", F(atom.xdnd_leave) },
1889 { "XdndDrop", F(atom.xdnd_drop) },
1890 { "XdndStatus", F(atom.xdnd_status) },
1891 { "XdndFinished", F(atom.xdnd_finished) },
1892 { "XdndTypeList", F(atom.xdnd_type_list) },
1893 { "XdndActionCopy", F(atom.xdnd_action_copy) },
1894 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
1898 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1899 xcb_xfixes_query_version_reply_t *xfixes_reply;
1900 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1901 xcb_intern_atom_reply_t *reply;
1902 xcb_render_query_pict_formats_reply_t *formats_reply;
1903 xcb_render_query_pict_formats_cookie_t formats_cookie;
1904 xcb_render_pictforminfo_t *formats;
1907 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1908 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
1910 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1912 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1913 cookies[i] = xcb_intern_atom (wm->conn, 0,
1914 strlen(atoms[i].name),
1917 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1918 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1919 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1923 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1924 if (!wm->xfixes || !wm->xfixes->present)
1925 weston_log("xfixes not available\n");
1927 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1928 XCB_XFIXES_MAJOR_VERSION,
1929 XCB_XFIXES_MINOR_VERSION);
1930 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1931 xfixes_cookie, NULL);
1933 weston_log("xfixes version: %d.%d\n",
1934 xfixes_reply->major_version, xfixes_reply->minor_version);
1938 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1940 if (formats_reply == NULL)
1943 formats = xcb_render_query_pict_formats_formats(formats_reply);
1944 for (i = 0; i < formats_reply->num_formats; i++) {
1945 if (formats[i].direct.red_mask != 0xff &&
1946 formats[i].direct.red_shift != 16)
1948 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1949 formats[i].depth == 24)
1950 wm->format_rgb = formats[i];
1951 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1952 formats[i].depth == 32 &&
1953 formats[i].direct.alpha_mask == 0xff &&
1954 formats[i].direct.alpha_shift == 24)
1955 wm->format_rgba = formats[i];
1958 free(formats_reply);
1962 weston_wm_create_wm_window(struct weston_wm *wm)
1964 static const char name[] = "Weston WM";
1966 wm->wm_window = xcb_generate_id(wm->conn);
1967 xcb_create_window(wm->conn,
1968 XCB_COPY_FROM_PARENT,
1974 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1975 wm->screen->root_visual,
1978 xcb_change_property(wm->conn,
1979 XCB_PROP_MODE_REPLACE,
1981 wm->atom.net_supporting_wm_check,
1986 xcb_change_property(wm->conn,
1987 XCB_PROP_MODE_REPLACE,
1989 wm->atom.net_wm_name,
1990 wm->atom.utf8_string,
1992 strlen(name), name);
1994 xcb_change_property(wm->conn,
1995 XCB_PROP_MODE_REPLACE,
1997 wm->atom.net_supporting_wm_check,
2002 /* Claim the WM_S0 selection even though we don't suport
2003 * the --replace functionality. */
2004 xcb_set_selection_owner(wm->conn,
2007 XCB_TIME_CURRENT_TIME);
2009 xcb_set_selection_owner(wm->conn,
2011 wm->atom.net_wm_cm_s0,
2012 XCB_TIME_CURRENT_TIME);
2016 weston_wm_create(struct weston_xserver *wxs, int fd)
2018 struct weston_wm *wm;
2019 struct wl_event_loop *loop;
2020 xcb_screen_iterator_t s;
2022 xcb_atom_t supported[3];
2024 wm = zalloc(sizeof *wm);
2029 wm->window_hash = hash_table_create();
2030 if (wm->window_hash == NULL) {
2035 /* xcb_connect_to_fd takes ownership of the fd. */
2036 wm->conn = xcb_connect_to_fd(fd, NULL);
2037 if (xcb_connection_has_error(wm->conn)) {
2038 weston_log("xcb_connect_to_fd failed\n");
2040 hash_table_destroy(wm->window_hash);
2045 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2046 wm->screen = s.data;
2048 loop = wl_display_get_event_loop(wxs->wl_display);
2050 wl_event_loop_add_fd(loop, fd,
2052 weston_wm_handle_event, wm);
2053 wl_event_source_check(wm->source);
2055 weston_wm_get_resources(wm);
2056 weston_wm_get_visual_and_colormap(wm);
2059 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2060 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2061 XCB_EVENT_MASK_PROPERTY_CHANGE;
2062 xcb_change_window_attributes(wm->conn, wm->screen->root,
2063 XCB_CW_EVENT_MASK, values);
2065 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2066 XCB_COMPOSITE_REDIRECT_MANUAL);
2068 wm->theme = theme_create();
2070 supported[0] = wm->atom.net_wm_moveresize;
2071 supported[1] = wm->atom.net_wm_state;
2072 supported[2] = wm->atom.net_wm_state_fullscreen;
2073 xcb_change_property(wm->conn,
2074 XCB_PROP_MODE_REPLACE,
2076 wm->atom.net_supported,
2079 ARRAY_LENGTH(supported), supported);
2081 weston_wm_selection_init(wm);
2083 weston_wm_dnd_init(wm);
2085 xcb_flush(wm->conn);
2087 wm->create_surface_listener.notify = weston_wm_create_surface;
2088 wl_signal_add(&wxs->compositor->create_surface_signal,
2089 &wm->create_surface_listener);
2090 wm->activate_listener.notify = weston_wm_window_activate;
2091 wl_signal_add(&wxs->compositor->activate_signal,
2092 &wm->activate_listener);
2093 wm->transform_listener.notify = weston_wm_window_transform;
2094 wl_signal_add(&wxs->compositor->transform_signal,
2095 &wm->transform_listener);
2096 wm->kill_listener.notify = weston_wm_kill_client;
2097 wl_signal_add(&wxs->compositor->kill_signal,
2098 &wm->kill_listener);
2099 wl_list_init(&wm->unpaired_window_list);
2101 weston_wm_create_cursors(wm);
2102 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2104 /* Create wm window and take WM_S0 selection last, which
2105 * signals to Xwayland that we're done with setup. */
2106 weston_wm_create_wm_window(wm);
2108 weston_log("created wm, root %d\n", wm->screen->root);
2114 weston_wm_destroy(struct weston_wm *wm)
2116 /* FIXME: Free windows in hash. */
2117 hash_table_destroy(wm->window_hash);
2118 weston_wm_destroy_cursors(wm);
2119 xcb_disconnect(wm->conn);
2120 wl_event_source_remove(wm->source);
2121 wl_list_remove(&wm->selection_listener.link);
2122 wl_list_remove(&wm->activate_listener.link);
2123 wl_list_remove(&wm->kill_listener.link);
2124 wl_list_remove(&wm->transform_listener.link);
2129 static struct weston_wm_window *
2130 get_wm_window(struct weston_surface *surface)
2132 struct wl_listener *listener;
2134 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2136 return container_of(listener, struct weston_wm_window,
2137 surface_destroy_listener);
2143 weston_wm_window_configure(void *data)
2145 struct weston_wm_window *window = data;
2146 struct weston_wm *wm = window->wm;
2148 int x, y, width, height;
2150 weston_wm_window_get_child_position(window, &x, &y);
2153 values[2] = window->width;
2154 values[3] = window->height;
2155 xcb_configure_window(wm->conn,
2157 XCB_CONFIG_WINDOW_X |
2158 XCB_CONFIG_WINDOW_Y |
2159 XCB_CONFIG_WINDOW_WIDTH |
2160 XCB_CONFIG_WINDOW_HEIGHT,
2163 weston_wm_window_get_frame_size(window, &width, &height);
2166 xcb_configure_window(wm->conn,
2168 XCB_CONFIG_WINDOW_WIDTH |
2169 XCB_CONFIG_WINDOW_HEIGHT,
2172 window->configure_source = NULL;
2174 weston_wm_window_schedule_repaint(window);
2178 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
2180 struct weston_wm_window *window = get_wm_window(surface);
2181 struct weston_wm *wm = window->wm;
2182 struct theme *t = window->wm->theme;
2183 int vborder, hborder;
2185 if (window->fullscreen) {
2188 } else if (window->decorate) {
2189 hborder = 2 * (t->margin + t->width);
2190 vborder = 2 * t->margin + t->titlebar_height + t->width;
2192 hborder = 2 * t->margin;
2193 vborder = 2 * t->margin;
2196 if (width > hborder)
2197 window->width = width - hborder;
2201 if (height > vborder)
2202 window->height = height - vborder;
2207 frame_resize_inside(window->frame, window->width, window->height);
2209 if (window->configure_source)
2212 window->configure_source =
2213 wl_event_loop_add_idle(wm->server->loop,
2214 weston_wm_window_configure, window);
2217 static const struct weston_shell_client shell_client = {
2222 legacy_fullscreen(struct weston_wm *wm,
2223 struct weston_wm_window *window,
2224 struct weston_output **output_ret)
2226 struct weston_compositor *compositor = wm->server->compositor;
2227 struct weston_output *output;
2228 uint32_t minmax = PMinSize | PMaxSize;
2231 /* Heuristics for detecting legacy fullscreen windows... */
2233 wl_list_for_each(output, &compositor->output_list, link) {
2234 if (output->x == window->x &&
2235 output->y == window->y &&
2236 output->width == window->width &&
2237 output->height == window->height &&
2238 window->override_redirect) {
2239 *output_ret = output;
2244 if ((window->size_hints.flags & (USSize |PSize)) &&
2245 window->size_hints.width == output->width &&
2246 window->size_hints.height == output->height)
2248 if ((window->size_hints.flags & minmax) == minmax &&
2249 window->size_hints.min_width == output->width &&
2250 window->size_hints.min_height == output->height &&
2251 window->size_hints.max_width == output->width &&
2252 window->size_hints.max_height == output->height)
2255 if (matching_size && !window->decorate &&
2256 (window->size_hints.flags & (USPosition | PPosition)) &&
2257 window->size_hints.x == output->x &&
2258 window->size_hints.y == output->y) {
2259 *output_ret = output;
2268 xserver_map_shell_surface(struct weston_wm_window *window,
2269 struct weston_surface *surface)
2271 struct weston_wm *wm = window->wm;
2272 struct weston_shell_interface *shell_interface =
2273 &wm->server->compositor->shell_interface;
2274 struct weston_output *output;
2275 struct weston_wm_window *parent;
2277 weston_wm_window_read_properties(window);
2279 /* A weston_wm_window may have many different surfaces assigned
2280 * throughout its life, so we must make sure to remove the listener
2281 * from the old surface signal list. */
2282 if (window->surface)
2283 wl_list_remove(&window->surface_destroy_listener.link);
2285 window->surface = surface;
2286 window->surface_destroy_listener.notify = surface_destroy;
2287 wl_signal_add(&window->surface->destroy_signal,
2288 &window->surface_destroy_listener);
2290 weston_wm_window_schedule_repaint(window);
2292 if (!shell_interface->create_shell_surface)
2295 if (!shell_interface->get_primary_view)
2299 shell_interface->create_shell_surface(shell_interface->shell,
2302 window->view = shell_interface->get_primary_view(shell_interface->shell,
2306 shell_interface->set_title(window->shsurf, window->name);
2308 if (window->fullscreen) {
2309 window->saved_width = window->width;
2310 window->saved_height = window->height;
2311 shell_interface->set_fullscreen(window->shsurf,
2312 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2315 } else if (legacy_fullscreen(wm, window, &output)) {
2316 window->fullscreen = 1;
2317 shell_interface->set_fullscreen(window->shsurf,
2318 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2320 } else if (window->override_redirect) {
2321 shell_interface->set_xwayland(window->shsurf,
2324 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2325 } else if (window->transient_for && window->transient_for->surface) {
2326 parent = window->transient_for;
2327 shell_interface->set_transient(window->shsurf,
2329 window->x - parent->x,
2330 window->y - parent->y, 0);
2332 shell_interface->set_toplevel(window->shsurf);