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"
41 #include "xserver-server-protocol.h"
44 struct wm_size_hints {
47 int32_t width, height; /* should set so old wm's don't mess up */
48 int32_t min_width, min_height;
49 int32_t max_width, max_height;
50 int32_t width_inc, height_inc;
54 } min_aspect, max_aspect;
55 int32_t base_width, base_height;
59 #define USPosition (1L << 0)
60 #define USSize (1L << 1)
61 #define PPosition (1L << 2)
62 #define PSize (1L << 3)
63 #define PMinSize (1L << 4)
64 #define PMaxSize (1L << 5)
65 #define PResizeInc (1L << 6)
66 #define PAspect (1L << 7)
67 #define PBaseSize (1L << 8)
68 #define PWinGravity (1L << 9)
70 struct motif_wm_hints {
78 #define MWM_HINTS_FUNCTIONS (1L << 0)
79 #define MWM_HINTS_DECORATIONS (1L << 1)
80 #define MWM_HINTS_INPUT_MODE (1L << 2)
81 #define MWM_HINTS_STATUS (1L << 3)
83 #define MWM_FUNC_ALL (1L << 0)
84 #define MWM_FUNC_RESIZE (1L << 1)
85 #define MWM_FUNC_MOVE (1L << 2)
86 #define MWM_FUNC_MINIMIZE (1L << 3)
87 #define MWM_FUNC_MAXIMIZE (1L << 4)
88 #define MWM_FUNC_CLOSE (1L << 5)
90 #define MWM_DECOR_ALL (1L << 0)
91 #define MWM_DECOR_BORDER (1L << 1)
92 #define MWM_DECOR_RESIZEH (1L << 2)
93 #define MWM_DECOR_TITLE (1L << 3)
94 #define MWM_DECOR_MENU (1L << 4)
95 #define MWM_DECOR_MINIMIZE (1L << 5)
96 #define MWM_DECOR_MAXIMIZE (1L << 6)
98 #define MWM_INPUT_MODELESS 0
99 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
100 #define MWM_INPUT_SYSTEM_MODAL 2
101 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
102 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
104 #define MWM_TEAROFF_WINDOW (1L<<0)
106 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
107 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
108 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
109 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
110 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
111 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
112 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
113 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
114 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
115 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
116 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
117 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
119 struct weston_wm_window {
120 struct weston_wm *wm;
122 xcb_window_t frame_id;
124 cairo_surface_t *cairo_surface;
126 struct weston_surface *surface;
127 struct shell_surface *shsurf;
128 struct weston_view *view;
129 struct wl_listener surface_destroy_listener;
130 struct wl_event_source *repaint_source;
131 struct wl_event_source *configure_source;
132 int properties_dirty;
137 struct weston_wm_window *transient_for;
142 int saved_width, saved_height;
144 int override_redirect;
148 struct wm_size_hints size_hints;
149 struct motif_wm_hints motif_hints;
153 static struct weston_wm_window *
154 get_wm_window(struct weston_surface *surface);
157 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
160 xserver_map_shell_surface(struct weston_wm_window *window,
161 struct weston_surface *surface);
163 static int __attribute__ ((format (printf, 1, 2)))
164 wm_log(const char *fmt, ...)
171 l = weston_vlog(fmt, argp);
180 static int __attribute__ ((format (printf, 1, 2)))
181 wm_log_continue(const char *fmt, ...)
188 l = weston_vlog_continue(fmt, argp);
199 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
201 xcb_get_atom_name_cookie_t cookie;
202 xcb_get_atom_name_reply_t *reply;
203 xcb_generic_error_t *e;
204 static char buffer[64];
206 if (atom == XCB_ATOM_NONE)
209 cookie = xcb_get_atom_name (c, atom);
210 reply = xcb_get_atom_name_reply (c, cookie, &e);
213 snprintf(buffer, sizeof buffer, "%.*s",
214 xcb_get_atom_name_name_length (reply),
215 xcb_get_atom_name_name (reply));
217 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
226 xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
228 xcb_connection_t *c = wm->conn;
229 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
230 xcb_screen_t *screen = s.data;
233 xcb_render_picture_t pic;
235 int stride = img->width * 4;
237 pix = xcb_generate_id(c);
238 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
240 pic = xcb_generate_id(c);
241 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
243 gc = xcb_generate_id(c);
244 xcb_create_gc(c, gc, pix, 0, 0);
246 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
247 img->width, img->height, 0, 0, 0, 32,
248 stride * img->height, (uint8_t *) img->pixels);
251 cursor = xcb_generate_id(c);
252 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
254 xcb_render_free_picture(c, pic);
255 xcb_free_pixmap(c, pix);
261 xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
263 /* TODO: treat animated cursors as well */
264 if (images->nimage != 1)
267 return xcb_cursor_image_load_cursor(wm, images->images[0]);
271 xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
274 XcursorImages *images;
281 v = getenv ("XCURSOR_SIZE");
288 images = XcursorLibraryLoadImages (file, NULL, size);
292 cursor = xcb_cursor_images_load_cursor (wm, images);
293 XcursorImagesDestroy (images);
299 dump_property(struct weston_wm *wm,
300 xcb_atom_t property, xcb_get_property_reply_t *reply)
303 const char *text_value, *name;
304 xcb_atom_t *atom_value;
308 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
310 wm_log_continue("(no reply)\n");
314 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
315 get_atom_name(wm->conn, reply->type),
317 xcb_get_property_value_length(reply),
320 if (reply->type == wm->atom.incr) {
321 incr_value = xcb_get_property_value(reply);
322 wm_log_continue("%d\n", *incr_value);
323 } else if (reply->type == wm->atom.utf8_string ||
324 reply->type == wm->atom.string) {
325 text_value = xcb_get_property_value(reply);
326 if (reply->value_len > 40)
329 len = reply->value_len;
330 wm_log_continue("\"%.*s\"\n", len, text_value);
331 } else if (reply->type == XCB_ATOM_ATOM) {
332 atom_value = xcb_get_property_value(reply);
333 for (i = 0; i < reply->value_len; i++) {
334 name = get_atom_name(wm->conn, atom_value[i]);
335 if (width + strlen(name) + 2 > 78) {
336 wm_log_continue("\n ");
339 width += wm_log_continue(", ");
342 width += wm_log_continue("%s", name);
344 wm_log_continue("\n");
346 wm_log_continue("huh?\n");
351 read_and_dump_property(struct weston_wm *wm,
352 xcb_window_t window, xcb_atom_t property)
354 xcb_get_property_reply_t *reply;
355 xcb_get_property_cookie_t cookie;
357 cookie = xcb_get_property(wm->conn, 0, window,
358 property, XCB_ATOM_ANY, 0, 2048);
359 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
361 dump_property(wm, property, reply);
366 /* We reuse some predefined, but otherwise useles atoms */
367 #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
368 #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
369 #define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
370 #define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
373 weston_wm_window_read_properties(struct weston_wm_window *window)
375 struct weston_wm *wm = window->wm;
376 struct weston_shell_interface *shell_interface =
377 &wm->server->compositor->shell_interface;
379 #define F(field) offsetof(struct weston_wm_window, field)
385 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
386 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
387 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
388 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
389 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
390 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
391 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
392 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
393 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
394 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
395 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
399 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
400 xcb_get_property_reply_t *reply;
406 if (!window->properties_dirty)
408 window->properties_dirty = 0;
410 for (i = 0; i < ARRAY_LENGTH(props); i++)
411 cookie[i] = xcb_get_property(wm->conn,
415 XCB_ATOM_ANY, 0, 2048);
417 window->decorate = !window->override_redirect;
418 window->size_hints.flags = 0;
419 window->motif_hints.flags = 0;
420 window->delete_window = 0;
422 for (i = 0; i < ARRAY_LENGTH(props); i++) {
423 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
425 /* Bad window, typically */
427 if (reply->type == XCB_ATOM_NONE) {
428 /* No such property */
433 p = ((char *) window + props[i].offset);
435 switch (props[i].type) {
436 case XCB_ATOM_WM_CLIENT_MACHINE:
437 case XCB_ATOM_STRING:
438 /* FIXME: We're using this for both string and
444 strndup(xcb_get_property_value(reply),
445 xcb_get_property_value_length(reply));
447 case XCB_ATOM_WINDOW:
448 xid = xcb_get_property_value(reply);
449 *(struct weston_wm_window **) p =
450 hash_table_lookup(wm->window_hash, *xid);
452 case XCB_ATOM_CARDINAL:
454 atom = xcb_get_property_value(reply);
455 *(xcb_atom_t *) p = *atom;
457 case TYPE_WM_PROTOCOLS:
458 atom = xcb_get_property_value(reply);
459 for (i = 0; i < reply->value_len; i++)
460 if (atom[i] == wm->atom.wm_delete_window)
461 window->delete_window = 1;
465 case TYPE_WM_NORMAL_HINTS:
466 memcpy(&window->size_hints,
467 xcb_get_property_value(reply),
468 sizeof window->size_hints);
470 case TYPE_NET_WM_STATE:
471 window->fullscreen = 0;
472 atom = xcb_get_property_value(reply);
473 for (i = 0; i < reply->value_len; i++)
474 if (atom[i] == wm->atom.net_wm_state_fullscreen)
475 window->fullscreen = 1;
477 case TYPE_MOTIF_WM_HINTS:
478 memcpy(&window->motif_hints,
479 xcb_get_property_value(reply),
480 sizeof window->motif_hints);
481 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
483 window->motif_hints.decorations > 0;
491 if (window->shsurf && window->name)
492 shell_interface->set_title(window->shsurf, window->name);
493 if (window->frame && window->name)
494 frame_set_title(window->frame, window->name);
498 weston_wm_window_get_frame_size(struct weston_wm_window *window,
499 int *width, int *height)
501 struct theme *t = window->wm->theme;
503 if (window->fullscreen) {
504 *width = window->width;
505 *height = window->height;
506 } else if (window->decorate && window->frame) {
507 *width = frame_width(window->frame);
508 *height = frame_height(window->frame);
510 *width = window->width + t->margin * 2;
511 *height = window->height + t->margin * 2;
516 weston_wm_window_get_child_position(struct weston_wm_window *window,
519 struct theme *t = window->wm->theme;
521 if (window->fullscreen) {
524 } else if (window->decorate && window->frame) {
525 frame_interior(window->frame, x, y, NULL, NULL);
533 weston_wm_window_send_configure_notify(struct weston_wm_window *window)
535 xcb_configure_notify_event_t configure_notify;
536 struct weston_wm *wm = window->wm;
539 weston_wm_window_get_child_position(window, &x, &y);
540 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
541 configure_notify.pad0 = 0;
542 configure_notify.event = window->id;
543 configure_notify.window = window->id;
544 configure_notify.above_sibling = XCB_WINDOW_NONE;
545 configure_notify.x = x;
546 configure_notify.y = y;
547 configure_notify.width = window->width;
548 configure_notify.height = window->height;
549 configure_notify.border_width = 0;
550 configure_notify.override_redirect = 0;
551 configure_notify.pad1 = 0;
553 xcb_send_event(wm->conn, 0, window->id,
554 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
555 (char *) &configure_notify);
559 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
561 xcb_configure_request_event_t *configure_request =
562 (xcb_configure_request_event_t *) event;
563 struct weston_wm_window *window;
564 uint32_t mask, values[16];
565 int x, y, width, height, i = 0;
567 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
568 configure_request->window,
569 configure_request->x, configure_request->y,
570 configure_request->width, configure_request->height);
572 window = hash_table_lookup(wm->window_hash, configure_request->window);
574 if (window->fullscreen) {
575 weston_wm_window_send_configure_notify(window);
579 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
580 window->width = configure_request->width;
581 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
582 window->height = configure_request->height;
585 frame_resize_inside(window->frame, window->width, window->height);
587 weston_wm_window_get_child_position(window, &x, &y);
590 values[i++] = window->width;
591 values[i++] = window->height;
593 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
594 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
595 XCB_CONFIG_WINDOW_BORDER_WIDTH;
596 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
597 values[i++] = configure_request->sibling;
598 mask |= XCB_CONFIG_WINDOW_SIBLING;
600 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
601 values[i++] = configure_request->stack_mode;
602 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
605 xcb_configure_window(wm->conn, window->id, mask, values);
607 weston_wm_window_get_frame_size(window, &width, &height);
610 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
611 xcb_configure_window(wm->conn, window->frame_id, mask, values);
613 weston_wm_window_schedule_repaint(window);
617 our_resource(struct weston_wm *wm, uint32_t id)
619 const xcb_setup_t *setup;
621 setup = xcb_get_setup(wm->conn);
623 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
627 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
629 xcb_configure_notify_event_t *configure_notify =
630 (xcb_configure_notify_event_t *) event;
631 struct weston_wm_window *window;
633 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
634 configure_notify->window,
635 configure_notify->x, configure_notify->y,
636 configure_notify->width, configure_notify->height);
638 window = hash_table_lookup(wm->window_hash, configure_notify->window);
639 window->x = configure_notify->x;
640 window->y = configure_notify->y;
641 if (window->override_redirect) {
642 window->width = configure_notify->width;
643 window->height = configure_notify->height;
645 frame_resize_inside(window->frame,
646 window->width, window->height);
651 weston_wm_kill_client(struct wl_listener *listener, void *data)
653 struct weston_surface *surface = data;
654 struct weston_wm_window *window = get_wm_window(surface);
660 gethostname(name, 1024);
662 /* this is only one heuristic to guess the PID of a client is valid,
663 * assuming it's compliant with icccm and ewmh. Non-compliants and
664 * remote applications of course fail. */
665 if (!strcmp(window->machine, name) && window->pid != 0)
666 kill(window->pid, SIGKILL);
670 weston_wm_create_surface(struct wl_listener *listener, void *data)
672 struct weston_surface *surface = data;
673 struct weston_wm *wm =
674 container_of(listener,
675 struct weston_wm, create_surface_listener);
676 struct weston_wm_window *window;
678 if (wl_resource_get_client(surface->resource) != wm->server->client)
681 wl_list_for_each(window, &wm->unpaired_window_list, link)
682 if (window->surface_id ==
683 wl_resource_get_id(surface->resource)) {
684 xserver_map_shell_surface(window, surface);
690 weston_wm_window_activate(struct wl_listener *listener, void *data)
692 struct weston_surface *surface = data;
693 struct weston_wm_window *window = NULL;
694 struct weston_wm *wm =
695 container_of(listener, struct weston_wm, activate_listener);
696 xcb_client_message_event_t client_message;
699 window = get_wm_window(surface);
703 client_message.response_type = XCB_CLIENT_MESSAGE;
704 client_message.format = 32;
705 client_message.window = window->id;
706 client_message.type = wm->atom.wm_protocols;
707 client_message.data.data32[0] = wm->atom.wm_take_focus;
708 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
710 xcb_send_event(wm->conn, 0, window->id,
711 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
712 (char *) &client_message);
714 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
715 window->id, XCB_TIME_CURRENT_TIME);
717 xcb_set_input_focus (wm->conn,
718 XCB_INPUT_FOCUS_POINTER_ROOT,
720 XCB_TIME_CURRENT_TIME);
723 if (wm->focus_window) {
724 if (wm->focus_window->frame)
725 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
726 weston_wm_window_schedule_repaint(wm->focus_window);
728 wm->focus_window = window;
729 if (wm->focus_window) {
730 if (wm->focus_window->frame)
731 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
732 weston_wm_window_schedule_repaint(wm->focus_window);
737 weston_wm_window_transform(struct wl_listener *listener, void *data)
739 struct weston_surface *surface = data;
740 struct weston_wm_window *window = get_wm_window(surface);
741 struct weston_wm *wm =
742 container_of(listener, struct weston_wm, transform_listener);
743 uint32_t mask, values[2];
748 if (!window->view || !weston_view_is_mapped(window->view))
751 if (window->x != window->view->geometry.x ||
752 window->y != window->view->geometry.y) {
753 values[0] = window->view->geometry.x;
754 values[1] = window->view->geometry.y;
755 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
757 xcb_configure_window(wm->conn, window->frame_id, mask, values);
762 #define ICCCM_WITHDRAWN_STATE 0
763 #define ICCCM_NORMAL_STATE 1
764 #define ICCCM_ICONIC_STATE 3
767 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
769 struct weston_wm *wm = window->wm;
770 uint32_t property[2];
773 property[1] = XCB_WINDOW_NONE;
775 xcb_change_property(wm->conn,
776 XCB_PROP_MODE_REPLACE,
785 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
787 struct weston_wm *wm = window->wm;
788 uint32_t property[1];
792 if (window->fullscreen)
793 property[i++] = wm->atom.net_wm_state_fullscreen;
795 xcb_change_property(wm->conn,
796 XCB_PROP_MODE_REPLACE,
798 wm->atom.net_wm_state,
805 weston_wm_window_create_frame(struct weston_wm_window *window)
807 struct weston_wm *wm = window->wm;
809 int x, y, width, height;
811 window->frame = frame_create(window->wm->theme,
812 window->width, window->height,
813 FRAME_BUTTON_CLOSE, window->name);
814 frame_resize_inside(window->frame, window->width, window->height);
816 weston_wm_window_get_frame_size(window, &width, &height);
817 weston_wm_window_get_child_position(window, &x, &y);
819 values[0] = wm->screen->black_pixel;
821 XCB_EVENT_MASK_KEY_PRESS |
822 XCB_EVENT_MASK_KEY_RELEASE |
823 XCB_EVENT_MASK_BUTTON_PRESS |
824 XCB_EVENT_MASK_BUTTON_RELEASE |
825 XCB_EVENT_MASK_POINTER_MOTION |
826 XCB_EVENT_MASK_ENTER_WINDOW |
827 XCB_EVENT_MASK_LEAVE_WINDOW |
828 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
829 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
830 values[2] = wm->colormap;
832 window->frame_id = xcb_generate_id(wm->conn);
833 xcb_create_window(wm->conn,
840 XCB_WINDOW_CLASS_INPUT_OUTPUT,
842 XCB_CW_BORDER_PIXEL |
844 XCB_CW_COLORMAP, values);
846 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
849 xcb_configure_window(wm->conn, window->id,
850 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
852 window->cairo_surface =
853 cairo_xcb_surface_create_with_xrender_format(wm->conn,
859 hash_table_insert(wm->window_hash, window->frame_id, window);
863 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
865 xcb_map_request_event_t *map_request =
866 (xcb_map_request_event_t *) event;
867 struct weston_wm_window *window;
869 if (our_resource(wm, map_request->window)) {
870 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
871 map_request->window);
875 window = hash_table_lookup(wm->window_hash, map_request->window);
877 weston_wm_window_read_properties(window);
879 if (window->frame_id == XCB_WINDOW_NONE)
880 weston_wm_window_create_frame(window);
882 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
883 window->id, window, window->frame_id);
885 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
886 weston_wm_window_set_net_wm_state(window);
888 xcb_map_window(wm->conn, map_request->window);
889 xcb_map_window(wm->conn, window->frame_id);
893 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
895 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
897 if (our_resource(wm, map_notify->window)) {
898 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
903 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
907 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
909 xcb_unmap_notify_event_t *unmap_notify =
910 (xcb_unmap_notify_event_t *) event;
911 struct weston_wm_window *window;
913 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
914 unmap_notify->window,
916 our_resource(wm, unmap_notify->window) ? ", ours" : "");
918 if (our_resource(wm, unmap_notify->window))
921 if (unmap_notify->response_type & SEND_EVENT_MASK)
922 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
923 * as it may come in after we've destroyed the window. */
926 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
927 if (wm->focus_window == window)
928 wm->focus_window = NULL;
930 wl_list_remove(&window->surface_destroy_listener.link);
931 window->surface = NULL;
932 window->shsurf = NULL;
934 xcb_unmap_window(wm->conn, window->frame_id);
938 weston_wm_window_draw_decoration(void *data)
940 struct weston_wm_window *window = data;
941 struct weston_wm *wm = window->wm;
942 struct theme *t = wm->theme;
944 int x, y, width, height;
945 int32_t input_x, input_y, input_w, input_h;
949 weston_wm_window_read_properties(window);
951 window->repaint_source = NULL;
953 weston_wm_window_get_frame_size(window, &width, &height);
954 weston_wm_window_get_child_position(window, &x, &y);
956 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
957 cr = cairo_create(window->cairo_surface);
959 if (window->fullscreen) {
961 } else if (window->decorate) {
962 if (wm->focus_window == window)
963 flags |= THEME_FRAME_ACTIVE;
965 frame_repaint(window->frame, cr);
967 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
968 cairo_set_source_rgba(cr, 0, 0, 0, 0);
971 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
972 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
973 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
978 if (window->surface) {
979 pixman_region32_fini(&window->surface->pending.opaque);
980 if(window->has_alpha) {
981 pixman_region32_init(&window->surface->pending.opaque);
983 /* We leave an extra pixel around the X window area to
984 * make sure we don't sample from the undefined alpha
985 * channel when filtering. */
986 pixman_region32_init_rect(&window->surface->pending.opaque,
992 weston_view_geometry_dirty(window->view);
994 pixman_region32_fini(&window->surface->pending.input);
996 if (window->fullscreen) {
999 input_w = window->width;
1000 input_h = window->height;
1001 } else if (window->decorate) {
1002 frame_input_rect(window->frame, &input_x, &input_y,
1003 &input_w, &input_h);
1006 pixman_region32_init_rect(&window->surface->pending.input,
1007 input_x, input_y, input_w, input_h);
1012 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1014 struct weston_wm *wm = window->wm;
1017 if (window->frame_id == XCB_WINDOW_NONE) {
1018 if (window->surface != NULL) {
1019 weston_wm_window_get_frame_size(window, &width, &height);
1020 pixman_region32_fini(&window->surface->pending.opaque);
1021 if(window->has_alpha) {
1022 pixman_region32_init(&window->surface->pending.opaque);
1024 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1028 weston_view_geometry_dirty(window->view);
1033 if (window->repaint_source)
1036 window->repaint_source =
1037 wl_event_loop_add_idle(wm->server->loop,
1038 weston_wm_window_draw_decoration,
1043 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1045 xcb_property_notify_event_t *property_notify =
1046 (xcb_property_notify_event_t *) event;
1047 struct weston_wm_window *window;
1049 window = hash_table_lookup(wm->window_hash, property_notify->window);
1053 window->properties_dirty = 1;
1055 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
1056 if (property_notify->state == XCB_PROPERTY_DELETE)
1057 wm_log("deleted\n");
1059 read_and_dump_property(wm, property_notify->window,
1060 property_notify->atom);
1062 if (property_notify->atom == wm->atom.net_wm_name ||
1063 property_notify->atom == XCB_ATOM_WM_NAME)
1064 weston_wm_window_schedule_repaint(window);
1068 weston_wm_window_create(struct weston_wm *wm,
1069 xcb_window_t id, int width, int height, int x, int y, int override)
1071 struct weston_wm_window *window;
1073 xcb_get_geometry_cookie_t geometry_cookie;
1074 xcb_get_geometry_reply_t *geometry_reply;
1076 window = zalloc(sizeof *window);
1077 if (window == NULL) {
1078 wm_log("failed to allocate window\n");
1082 geometry_cookie = xcb_get_geometry(wm->conn, id);
1084 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1085 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1089 window->properties_dirty = 1;
1090 window->override_redirect = override;
1091 window->width = width;
1092 window->height = height;
1096 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1097 /* technically we should use XRender and check the visual format's
1098 alpha_mask, but checking depth is simpler and works in all known cases */
1099 if(geometry_reply != NULL)
1100 window->has_alpha = geometry_reply->depth == 32;
1101 free(geometry_reply);
1103 hash_table_insert(wm->window_hash, id, window);
1107 weston_wm_window_destroy(struct weston_wm_window *window)
1109 struct weston_wm *wm = window->wm;
1111 if (window->repaint_source)
1112 wl_event_source_remove(window->repaint_source);
1113 if (window->cairo_surface)
1114 cairo_surface_destroy(window->cairo_surface);
1116 if (window->frame_id) {
1117 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1118 xcb_destroy_window(wm->conn, window->frame_id);
1119 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1120 hash_table_remove(wm->window_hash, window->frame_id);
1121 window->frame_id = XCB_WINDOW_NONE;
1124 hash_table_remove(window->wm->window_hash, window->id);
1129 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1131 xcb_create_notify_event_t *create_notify =
1132 (xcb_create_notify_event_t *) event;
1134 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1135 create_notify->window,
1136 create_notify->width, create_notify->height,
1137 create_notify->override_redirect ? ", override" : "",
1138 our_resource(wm, create_notify->window) ? ", ours" : "");
1140 if (our_resource(wm, create_notify->window))
1143 weston_wm_window_create(wm, create_notify->window,
1144 create_notify->width, create_notify->height,
1145 create_notify->x, create_notify->y,
1146 create_notify->override_redirect);
1150 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1152 xcb_destroy_notify_event_t *destroy_notify =
1153 (xcb_destroy_notify_event_t *) event;
1154 struct weston_wm_window *window;
1156 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1157 destroy_notify->window,
1158 destroy_notify->event,
1159 our_resource(wm, destroy_notify->window) ? ", ours" : "");
1161 if (our_resource(wm, destroy_notify->window))
1164 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
1165 weston_wm_window_destroy(window);
1169 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1171 xcb_reparent_notify_event_t *reparent_notify =
1172 (xcb_reparent_notify_event_t *) event;
1173 struct weston_wm_window *window;
1175 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1176 reparent_notify->window,
1177 reparent_notify->parent,
1178 reparent_notify->event);
1180 if (reparent_notify->parent == wm->screen->root) {
1181 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1182 reparent_notify->x, reparent_notify->y,
1183 reparent_notify->override_redirect);
1184 } else if (!our_resource(wm, reparent_notify->parent)) {
1185 window = hash_table_lookup(wm->window_hash,
1186 reparent_notify->window);
1187 weston_wm_window_destroy(window);
1191 struct weston_seat *
1192 weston_wm_pick_seat(struct weston_wm *wm)
1194 return container_of(wm->server->compositor->seat_list.next,
1195 struct weston_seat, link);
1199 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1200 xcb_client_message_event_t *client_message)
1202 static const int map[] = {
1203 THEME_LOCATION_RESIZING_TOP_LEFT,
1204 THEME_LOCATION_RESIZING_TOP,
1205 THEME_LOCATION_RESIZING_TOP_RIGHT,
1206 THEME_LOCATION_RESIZING_RIGHT,
1207 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1208 THEME_LOCATION_RESIZING_BOTTOM,
1209 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1210 THEME_LOCATION_RESIZING_LEFT
1213 struct weston_wm *wm = window->wm;
1214 struct weston_seat *seat = weston_wm_pick_seat(wm);
1216 struct weston_shell_interface *shell_interface =
1217 &wm->server->compositor->shell_interface;
1219 if (seat->pointer->button_count != 1 || !window->view
1220 || seat->pointer->focus != window->view)
1223 detail = client_message->data.data32[2];
1225 case _NET_WM_MOVERESIZE_MOVE:
1226 shell_interface->move(window->shsurf, seat);
1228 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1229 case _NET_WM_MOVERESIZE_SIZE_TOP:
1230 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1231 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1232 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1233 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1234 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1235 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1236 shell_interface->resize(window->shsurf, seat, map[detail]);
1238 case _NET_WM_MOVERESIZE_CANCEL:
1243 #define _NET_WM_STATE_REMOVE 0
1244 #define _NET_WM_STATE_ADD 1
1245 #define _NET_WM_STATE_TOGGLE 2
1248 update_state(int action, int *state)
1250 int new_state, changed;
1253 case _NET_WM_STATE_REMOVE:
1256 case _NET_WM_STATE_ADD:
1259 case _NET_WM_STATE_TOGGLE:
1260 new_state = !*state;
1266 changed = (*state != new_state);
1273 weston_wm_window_configure(void *data);
1276 weston_wm_window_handle_state(struct weston_wm_window *window,
1277 xcb_client_message_event_t *client_message)
1279 struct weston_wm *wm = window->wm;
1280 struct weston_shell_interface *shell_interface =
1281 &wm->server->compositor->shell_interface;
1282 uint32_t action, property;
1284 action = client_message->data.data32[0];
1285 property = client_message->data.data32[1];
1287 if (property == wm->atom.net_wm_state_fullscreen &&
1288 update_state(action, &window->fullscreen)) {
1289 weston_wm_window_set_net_wm_state(window);
1290 if (window->fullscreen) {
1291 window->saved_width = window->width;
1292 window->saved_height = window->height;
1295 shell_interface->set_fullscreen(window->shsurf,
1296 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1299 shell_interface->set_toplevel(window->shsurf);
1300 window->width = window->saved_width;
1301 window->height = window->saved_height;
1303 frame_resize_inside(window->frame,
1306 weston_wm_window_configure(window);
1312 surface_destroy(struct wl_listener *listener, void *data)
1314 struct weston_wm_window *window =
1315 container_of(listener,
1316 struct weston_wm_window, surface_destroy_listener);
1318 wm_log("surface for xid %d destroyed\n", window->id);
1320 /* This should have been freed by the shell.
1321 * Don't try to use it later. */
1322 window->shsurf = NULL;
1323 window->surface = NULL;
1324 window->view = NULL;
1328 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1329 xcb_client_message_event_t *client_message)
1331 struct weston_wm *wm = window->wm;
1332 struct wl_resource *resource;
1334 if (window->surface_id != 0) {
1335 wm_log("already have surface id for window %d\n", window->id);
1339 /* Xwayland will send the wayland requests to create the
1340 * wl_surface before sending this client message. Even so, we
1341 * can end up handling the X event before the wayland requests
1342 * and thus when we try to look up the surface ID, the surface
1343 * hasn't been created yet. In that case put the window on
1344 * the unpaired window list and continue when the surface gets
1346 window->surface_id = client_message->data.data32[0];
1347 resource = wl_client_get_object(wm->server->client,
1348 window->surface_id);
1350 xserver_map_shell_surface(window,
1351 wl_resource_get_user_data(resource));
1353 wl_list_insert(&wm->unpaired_window_list, &window->link);
1357 weston_wm_handle_client_message(struct weston_wm *wm,
1358 xcb_generic_event_t *event)
1360 xcb_client_message_event_t *client_message =
1361 (xcb_client_message_event_t *) event;
1362 struct weston_wm_window *window;
1364 window = hash_table_lookup(wm->window_hash, client_message->window);
1366 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1367 get_atom_name(wm->conn, client_message->type),
1368 client_message->data.data32[0],
1369 client_message->data.data32[1],
1370 client_message->data.data32[2],
1371 client_message->data.data32[3],
1372 client_message->data.data32[4],
1373 client_message->window);
1375 if (client_message->type == wm->atom.net_wm_moveresize)
1376 weston_wm_window_handle_moveresize(window, client_message);
1377 else if (client_message->type == wm->atom.net_wm_state)
1378 weston_wm_window_handle_state(window, client_message);
1379 else if (client_message->type == wm->atom.wl_surface_id)
1380 weston_wm_window_handle_surface_id(window, client_message);
1388 XWM_CURSOR_TOP_LEFT,
1389 XWM_CURSOR_TOP_RIGHT,
1390 XWM_CURSOR_BOTTOM_LEFT,
1391 XWM_CURSOR_BOTTOM_RIGHT,
1392 XWM_CURSOR_LEFT_PTR,
1396 * The following correspondences between file names and cursors was copied
1397 * from: https://bugs.kde.org/attachment.cgi?id=67313
1400 static const char *bottom_left_corners[] = {
1401 "bottom_left_corner",
1406 static const char *bottom_right_corners[] = {
1407 "bottom_right_corner",
1412 static const char *bottom_sides[] = {
1418 static const char *left_ptrs[] = {
1425 static const char *left_sides[] = {
1431 static const char *right_sides[] = {
1437 static const char *top_left_corners[] = {
1443 static const char *top_right_corners[] = {
1449 static const char *top_sides[] = {
1455 struct cursor_alternatives {
1460 static const struct cursor_alternatives cursors[] = {
1461 {top_sides, ARRAY_LENGTH(top_sides)},
1462 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1463 {left_sides, ARRAY_LENGTH(left_sides)},
1464 {right_sides, ARRAY_LENGTH(right_sides)},
1465 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1466 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1467 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1468 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1469 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1473 weston_wm_create_cursors(struct weston_wm *wm)
1476 int i, count = ARRAY_LENGTH(cursors);
1479 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1480 for (i = 0; i < count; i++) {
1481 for (j = 0; j < cursors[i].count; j++) {
1482 name = cursors[i].names[j];
1484 xcb_cursor_library_load_cursor(wm, name);
1485 if (wm->cursors[i] != (xcb_cursor_t)-1)
1490 wm->last_cursor = -1;
1494 weston_wm_destroy_cursors(struct weston_wm *wm)
1498 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1499 xcb_free_cursor(wm->conn, wm->cursors[i]);
1505 get_cursor_for_location(enum theme_location location)
1507 // int location = theme_get_location(t, x, y, width, height, 0);
1510 case THEME_LOCATION_RESIZING_TOP:
1511 return XWM_CURSOR_TOP;
1512 case THEME_LOCATION_RESIZING_BOTTOM:
1513 return XWM_CURSOR_BOTTOM;
1514 case THEME_LOCATION_RESIZING_LEFT:
1515 return XWM_CURSOR_LEFT;
1516 case THEME_LOCATION_RESIZING_RIGHT:
1517 return XWM_CURSOR_RIGHT;
1518 case THEME_LOCATION_RESIZING_TOP_LEFT:
1519 return XWM_CURSOR_TOP_LEFT;
1520 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1521 return XWM_CURSOR_TOP_RIGHT;
1522 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1523 return XWM_CURSOR_BOTTOM_LEFT;
1524 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1525 return XWM_CURSOR_BOTTOM_RIGHT;
1526 case THEME_LOCATION_EXTERIOR:
1527 case THEME_LOCATION_TITLEBAR:
1529 return XWM_CURSOR_LEFT_PTR;
1534 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1537 uint32_t cursor_value_list;
1539 if (wm->last_cursor == cursor)
1542 wm->last_cursor = cursor;
1544 cursor_value_list = wm->cursors[cursor];
1545 xcb_change_window_attributes (wm->conn, window_id,
1546 XCB_CW_CURSOR, &cursor_value_list);
1547 xcb_flush(wm->conn);
1551 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1553 xcb_client_message_event_t client_message;
1555 if (window->delete_window) {
1556 client_message.response_type = XCB_CLIENT_MESSAGE;
1557 client_message.format = 32;
1558 client_message.window = window->id;
1559 client_message.type = window->wm->atom.wm_protocols;
1560 client_message.data.data32[0] =
1561 window->wm->atom.wm_delete_window;
1562 client_message.data.data32[1] = time;
1564 xcb_send_event(window->wm->conn, 0, window->id,
1565 XCB_EVENT_MASK_NO_EVENT,
1566 (char *) &client_message);
1568 xcb_kill_client(window->wm->conn, window->id);
1573 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1575 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1576 struct weston_shell_interface *shell_interface =
1577 &wm->server->compositor->shell_interface;
1578 struct weston_seat *seat = weston_wm_pick_seat(wm);
1579 struct weston_wm_window *window;
1580 enum theme_location location;
1581 enum frame_button_state button_state;
1584 wm_log("XCB_BUTTON_%s (detail %d)\n",
1585 button->response_type == XCB_BUTTON_PRESS ?
1586 "PRESS" : "RELEASE", button->detail);
1588 window = hash_table_lookup(wm->window_hash, button->event);
1589 if (!window || !window->decorate)
1592 if (button->detail != 1 && button->detail != 2)
1595 button_state = button->response_type == XCB_BUTTON_PRESS ?
1596 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1597 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1599 location = frame_pointer_button(window->frame, NULL,
1600 button_id, button_state);
1601 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1602 weston_wm_window_schedule_repaint(window);
1604 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1605 shell_interface->move(window->shsurf, seat);
1606 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1609 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1610 shell_interface->resize(window->shsurf, seat, location);
1611 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1614 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1615 weston_wm_window_close(window, button->time);
1616 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
1621 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1623 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1624 struct weston_wm_window *window;
1625 enum theme_location location;
1628 window = hash_table_lookup(wm->window_hash, motion->event);
1629 if (!window || !window->decorate)
1632 location = frame_pointer_motion(window->frame, NULL,
1633 motion->event_x, motion->event_y);
1634 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1635 weston_wm_window_schedule_repaint(window);
1637 cursor = get_cursor_for_location(location);
1638 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1642 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1644 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1645 struct weston_wm_window *window;
1646 enum theme_location location;
1649 window = hash_table_lookup(wm->window_hash, enter->event);
1650 if (!window || !window->decorate)
1653 location = frame_pointer_enter(window->frame, NULL,
1654 enter->event_x, enter->event_y);
1655 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1656 weston_wm_window_schedule_repaint(window);
1658 cursor = get_cursor_for_location(location);
1659 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1663 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1665 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1666 struct weston_wm_window *window;
1668 window = hash_table_lookup(wm->window_hash, leave->event);
1669 if (!window || !window->decorate)
1672 frame_pointer_leave(window->frame, NULL);
1673 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1674 weston_wm_window_schedule_repaint(window);
1676 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
1680 weston_wm_handle_event(int fd, uint32_t mask, void *data)
1682 struct weston_wm *wm = data;
1683 xcb_generic_event_t *event;
1686 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1687 if (weston_wm_handle_selection_event(wm, event)) {
1693 if (weston_wm_handle_dnd_event(wm, event)) {
1699 switch (EVENT_TYPE(event)) {
1700 case XCB_BUTTON_PRESS:
1701 case XCB_BUTTON_RELEASE:
1702 weston_wm_handle_button(wm, event);
1704 case XCB_ENTER_NOTIFY:
1705 weston_wm_handle_enter(wm, event);
1707 case XCB_LEAVE_NOTIFY:
1708 weston_wm_handle_leave(wm, event);
1710 case XCB_MOTION_NOTIFY:
1711 weston_wm_handle_motion(wm, event);
1713 case XCB_CREATE_NOTIFY:
1714 weston_wm_handle_create_notify(wm, event);
1716 case XCB_MAP_REQUEST:
1717 weston_wm_handle_map_request(wm, event);
1719 case XCB_MAP_NOTIFY:
1720 weston_wm_handle_map_notify(wm, event);
1722 case XCB_UNMAP_NOTIFY:
1723 weston_wm_handle_unmap_notify(wm, event);
1725 case XCB_REPARENT_NOTIFY:
1726 weston_wm_handle_reparent_notify(wm, event);
1728 case XCB_CONFIGURE_REQUEST:
1729 weston_wm_handle_configure_request(wm, event);
1731 case XCB_CONFIGURE_NOTIFY:
1732 weston_wm_handle_configure_notify(wm, event);
1734 case XCB_DESTROY_NOTIFY:
1735 weston_wm_handle_destroy_notify(wm, event);
1737 case XCB_MAPPING_NOTIFY:
1738 wm_log("XCB_MAPPING_NOTIFY\n");
1740 case XCB_PROPERTY_NOTIFY:
1741 weston_wm_handle_property_notify(wm, event);
1743 case XCB_CLIENT_MESSAGE:
1744 weston_wm_handle_client_message(wm, event);
1752 xcb_flush(wm->conn);
1758 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1760 xcb_depth_iterator_t d_iter;
1761 xcb_visualtype_iterator_t vt_iter;
1762 xcb_visualtype_t *visualtype;
1764 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1766 while (d_iter.rem > 0) {
1767 if (d_iter.data->depth == 32) {
1768 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1769 visualtype = vt_iter.data;
1773 xcb_depth_next(&d_iter);
1776 if (visualtype == NULL) {
1777 weston_log("no 32 bit visualtype\n");
1781 wm->visual_id = visualtype->visual_id;
1782 wm->colormap = xcb_generate_id(wm->conn);
1783 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1784 wm->colormap, wm->screen->root, wm->visual_id);
1788 weston_wm_get_resources(struct weston_wm *wm)
1791 #define F(field) offsetof(struct weston_wm, field)
1793 static const struct { const char *name; int offset; } atoms[] = {
1794 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1795 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1796 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1797 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1798 { "WM_STATE", F(atom.wm_state) },
1799 { "WM_S0", F(atom.wm_s0) },
1800 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
1801 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
1802 { "_NET_WM_NAME", F(atom.net_wm_name) },
1803 { "_NET_WM_PID", F(atom.net_wm_pid) },
1804 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1805 { "_NET_WM_STATE", F(atom.net_wm_state) },
1806 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1807 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1808 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1809 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1811 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1812 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1813 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1814 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1815 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1816 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1817 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
1818 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1819 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
1820 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1821 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1822 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1823 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1824 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1826 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1827 { "_NET_SUPPORTING_WM_CHECK",
1828 F(atom.net_supporting_wm_check) },
1829 { "_NET_SUPPORTED", F(atom.net_supported) },
1830 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1831 { "CLIPBOARD", F(atom.clipboard) },
1832 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
1833 { "TARGETS", F(atom.targets) },
1834 { "UTF8_STRING", F(atom.utf8_string) },
1835 { "_WL_SELECTION", F(atom.wl_selection) },
1836 { "INCR", F(atom.incr) },
1837 { "TIMESTAMP", F(atom.timestamp) },
1838 { "MULTIPLE", F(atom.multiple) },
1839 { "UTF8_STRING" , F(atom.utf8_string) },
1840 { "COMPOUND_TEXT", F(atom.compound_text) },
1841 { "TEXT", F(atom.text) },
1842 { "STRING", F(atom.string) },
1843 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1844 { "text/plain", F(atom.text_plain) },
1845 { "XdndSelection", F(atom.xdnd_selection) },
1846 { "XdndAware", F(atom.xdnd_aware) },
1847 { "XdndEnter", F(atom.xdnd_enter) },
1848 { "XdndLeave", F(atom.xdnd_leave) },
1849 { "XdndDrop", F(atom.xdnd_drop) },
1850 { "XdndStatus", F(atom.xdnd_status) },
1851 { "XdndFinished", F(atom.xdnd_finished) },
1852 { "XdndTypeList", F(atom.xdnd_type_list) },
1853 { "XdndActionCopy", F(atom.xdnd_action_copy) },
1854 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
1858 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1859 xcb_xfixes_query_version_reply_t *xfixes_reply;
1860 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1861 xcb_intern_atom_reply_t *reply;
1862 xcb_render_query_pict_formats_reply_t *formats_reply;
1863 xcb_render_query_pict_formats_cookie_t formats_cookie;
1864 xcb_render_pictforminfo_t *formats;
1867 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1868 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
1870 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1872 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1873 cookies[i] = xcb_intern_atom (wm->conn, 0,
1874 strlen(atoms[i].name),
1877 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1878 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1879 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1883 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1884 if (!wm->xfixes || !wm->xfixes->present)
1885 weston_log("xfixes not available\n");
1887 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1888 XCB_XFIXES_MAJOR_VERSION,
1889 XCB_XFIXES_MINOR_VERSION);
1890 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1891 xfixes_cookie, NULL);
1893 weston_log("xfixes version: %d.%d\n",
1894 xfixes_reply->major_version, xfixes_reply->minor_version);
1898 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1900 if (formats_reply == NULL)
1903 formats = xcb_render_query_pict_formats_formats(formats_reply);
1904 for (i = 0; i < formats_reply->num_formats; i++) {
1905 if (formats[i].direct.red_mask != 0xff &&
1906 formats[i].direct.red_shift != 16)
1908 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1909 formats[i].depth == 24)
1910 wm->format_rgb = formats[i];
1911 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1912 formats[i].depth == 32 &&
1913 formats[i].direct.alpha_mask == 0xff &&
1914 formats[i].direct.alpha_shift == 24)
1915 wm->format_rgba = formats[i];
1918 free(formats_reply);
1922 weston_wm_create_wm_window(struct weston_wm *wm)
1924 static const char name[] = "Weston WM";
1926 wm->wm_window = xcb_generate_id(wm->conn);
1927 xcb_create_window(wm->conn,
1928 XCB_COPY_FROM_PARENT,
1934 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1935 wm->screen->root_visual,
1938 xcb_change_property(wm->conn,
1939 XCB_PROP_MODE_REPLACE,
1941 wm->atom.net_supporting_wm_check,
1946 xcb_change_property(wm->conn,
1947 XCB_PROP_MODE_REPLACE,
1949 wm->atom.net_wm_name,
1950 wm->atom.utf8_string,
1952 strlen(name), name);
1954 xcb_change_property(wm->conn,
1955 XCB_PROP_MODE_REPLACE,
1957 wm->atom.net_supporting_wm_check,
1962 /* Claim the WM_S0 selection even though we don't suport
1963 * the --replace functionality. */
1964 xcb_set_selection_owner(wm->conn,
1967 XCB_TIME_CURRENT_TIME);
1969 xcb_set_selection_owner(wm->conn,
1971 wm->atom.net_wm_cm_s0,
1972 XCB_TIME_CURRENT_TIME);
1976 weston_wm_create(struct weston_xserver *wxs, int fd)
1978 struct weston_wm *wm;
1979 struct wl_event_loop *loop;
1980 xcb_screen_iterator_t s;
1982 xcb_atom_t supported[3];
1984 wm = zalloc(sizeof *wm);
1989 wm->window_hash = hash_table_create();
1990 if (wm->window_hash == NULL) {
1995 /* xcb_connect_to_fd takes ownership of the fd. */
1996 wm->conn = xcb_connect_to_fd(fd, NULL);
1997 if (xcb_connection_has_error(wm->conn)) {
1998 weston_log("xcb_connect_to_fd failed\n");
2000 hash_table_destroy(wm->window_hash);
2005 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2006 wm->screen = s.data;
2008 loop = wl_display_get_event_loop(wxs->wl_display);
2010 wl_event_loop_add_fd(loop, fd,
2012 weston_wm_handle_event, wm);
2013 wl_event_source_check(wm->source);
2015 weston_wm_get_resources(wm);
2016 weston_wm_get_visual_and_colormap(wm);
2019 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2020 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2021 XCB_EVENT_MASK_PROPERTY_CHANGE;
2022 xcb_change_window_attributes(wm->conn, wm->screen->root,
2023 XCB_CW_EVENT_MASK, values);
2025 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2026 XCB_COMPOSITE_REDIRECT_MANUAL);
2028 wm->theme = theme_create();
2030 supported[0] = wm->atom.net_wm_moveresize;
2031 supported[1] = wm->atom.net_wm_state;
2032 supported[2] = wm->atom.net_wm_state_fullscreen;
2033 xcb_change_property(wm->conn,
2034 XCB_PROP_MODE_REPLACE,
2036 wm->atom.net_supported,
2039 ARRAY_LENGTH(supported), supported);
2041 weston_wm_selection_init(wm);
2043 weston_wm_dnd_init(wm);
2045 xcb_flush(wm->conn);
2047 wm->create_surface_listener.notify = weston_wm_create_surface;
2048 wl_signal_add(&wxs->compositor->create_surface_signal,
2049 &wm->create_surface_listener);
2050 wm->activate_listener.notify = weston_wm_window_activate;
2051 wl_signal_add(&wxs->compositor->activate_signal,
2052 &wm->activate_listener);
2053 wm->transform_listener.notify = weston_wm_window_transform;
2054 wl_signal_add(&wxs->compositor->transform_signal,
2055 &wm->transform_listener);
2056 wm->kill_listener.notify = weston_wm_kill_client;
2057 wl_signal_add(&wxs->compositor->kill_signal,
2058 &wm->kill_listener);
2059 wl_list_init(&wm->unpaired_window_list);
2061 weston_wm_create_cursors(wm);
2062 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2064 /* Create wm window and take WM_S0 selection last, which
2065 * signals to Xwayland that we're done with setup. */
2066 weston_wm_create_wm_window(wm);
2068 weston_log("created wm, root %d\n", wm->screen->root);
2074 weston_wm_destroy(struct weston_wm *wm)
2076 /* FIXME: Free windows in hash. */
2077 hash_table_destroy(wm->window_hash);
2078 weston_wm_destroy_cursors(wm);
2079 xcb_disconnect(wm->conn);
2080 wl_event_source_remove(wm->source);
2081 wl_list_remove(&wm->selection_listener.link);
2082 wl_list_remove(&wm->activate_listener.link);
2083 wl_list_remove(&wm->kill_listener.link);
2084 wl_list_remove(&wm->transform_listener.link);
2089 static struct weston_wm_window *
2090 get_wm_window(struct weston_surface *surface)
2092 struct wl_listener *listener;
2094 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2096 return container_of(listener, struct weston_wm_window,
2097 surface_destroy_listener);
2103 weston_wm_window_configure(void *data)
2105 struct weston_wm_window *window = data;
2106 struct weston_wm *wm = window->wm;
2108 int x, y, width, height;
2110 weston_wm_window_get_child_position(window, &x, &y);
2113 values[2] = window->width;
2114 values[3] = window->height;
2115 xcb_configure_window(wm->conn,
2117 XCB_CONFIG_WINDOW_X |
2118 XCB_CONFIG_WINDOW_Y |
2119 XCB_CONFIG_WINDOW_WIDTH |
2120 XCB_CONFIG_WINDOW_HEIGHT,
2123 weston_wm_window_get_frame_size(window, &width, &height);
2126 xcb_configure_window(wm->conn,
2128 XCB_CONFIG_WINDOW_WIDTH |
2129 XCB_CONFIG_WINDOW_HEIGHT,
2132 window->configure_source = NULL;
2134 weston_wm_window_schedule_repaint(window);
2138 send_configure(struct weston_surface *surface,
2139 uint32_t edges, int32_t width, int32_t height)
2141 struct weston_wm_window *window = get_wm_window(surface);
2142 struct weston_wm *wm = window->wm;
2143 struct theme *t = window->wm->theme;
2144 int vborder, hborder;
2146 if (window->fullscreen) {
2149 } else if (window->decorate) {
2150 hborder = 2 * (t->margin + t->width);
2151 vborder = 2 * t->margin + t->titlebar_height + t->width;
2153 hborder = 2 * t->margin;
2154 vborder = 2 * t->margin;
2157 if (width > hborder)
2158 window->width = width - hborder;
2162 if (height > vborder)
2163 window->height = height - vborder;
2168 frame_resize_inside(window->frame, window->width, window->height);
2170 if (window->configure_source)
2173 window->configure_source =
2174 wl_event_loop_add_idle(wm->server->loop,
2175 weston_wm_window_configure, window);
2178 static const struct weston_shell_client shell_client = {
2183 legacy_fullscreen(struct weston_wm *wm,
2184 struct weston_wm_window *window,
2185 struct weston_output **output_ret)
2187 struct weston_compositor *compositor = wm->server->compositor;
2188 struct weston_output *output;
2189 uint32_t minmax = PMinSize | PMaxSize;
2192 /* Heuristics for detecting legacy fullscreen windows... */
2194 wl_list_for_each(output, &compositor->output_list, link) {
2195 if (output->x == window->x &&
2196 output->y == window->y &&
2197 output->width == window->width &&
2198 output->height == window->height &&
2199 window->override_redirect) {
2200 *output_ret = output;
2205 if ((window->size_hints.flags & (USSize |PSize)) &&
2206 window->size_hints.width == output->width &&
2207 window->size_hints.height == output->height)
2209 if ((window->size_hints.flags & minmax) == minmax &&
2210 window->size_hints.min_width == output->width &&
2211 window->size_hints.min_height == output->height &&
2212 window->size_hints.max_width == output->width &&
2213 window->size_hints.max_height == output->height)
2216 if (matching_size && !window->decorate &&
2217 (window->size_hints.flags & (USPosition | PPosition)) &&
2218 window->size_hints.x == output->x &&
2219 window->size_hints.y == output->y) {
2220 *output_ret = output;
2229 xserver_map_shell_surface(struct weston_wm_window *window,
2230 struct weston_surface *surface)
2232 struct weston_wm *wm = window->wm;
2233 struct weston_shell_interface *shell_interface =
2234 &wm->server->compositor->shell_interface;
2235 struct weston_output *output;
2236 struct weston_wm_window *parent;
2238 weston_wm_window_read_properties(window);
2240 /* A weston_wm_window may have many different surfaces assigned
2241 * throughout its life, so we must make sure to remove the listener
2242 * from the old surface signal list. */
2243 if (window->surface)
2244 wl_list_remove(&window->surface_destroy_listener.link);
2246 window->surface = surface;
2247 window->surface_destroy_listener.notify = surface_destroy;
2248 wl_signal_add(&window->surface->destroy_signal,
2249 &window->surface_destroy_listener);
2251 weston_wm_window_schedule_repaint(window);
2253 if (!shell_interface->create_shell_surface)
2256 if (!shell_interface->get_primary_view)
2260 shell_interface->create_shell_surface(shell_interface->shell,
2263 window->view = shell_interface->get_primary_view(shell_interface->shell,
2267 shell_interface->set_title(window->shsurf, window->name);
2269 if (window->fullscreen) {
2270 window->saved_width = window->width;
2271 window->saved_height = window->height;
2272 shell_interface->set_fullscreen(window->shsurf,
2273 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2276 } else if (legacy_fullscreen(wm, window, &output)) {
2277 window->fullscreen = 1;
2278 shell_interface->set_fullscreen(window->shsurf,
2279 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2281 } else if (window->override_redirect) {
2282 shell_interface->set_xwayland(window->shsurf,
2285 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2286 } else if (window->transient_for && window->transient_for->surface) {
2287 parent = window->transient_for;
2288 shell_interface->set_transient(window->shsurf,
2290 window->x - parent->x,
2291 window->y - parent->y, 0);
2293 shell_interface->set_toplevel(window->shsurf);