2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <sys/socket.h>
38 #include <X11/Xcursor/Xcursor.h>
39 #include <linux/input.h>
43 #include "cairo-util.h"
44 #include "compositor.h"
46 #include "shared/helpers.h"
48 struct wm_size_hints {
51 int32_t width, height; /* should set so old wm's don't mess up */
52 int32_t min_width, min_height;
53 int32_t max_width, max_height;
54 int32_t width_inc, height_inc;
58 } min_aspect, max_aspect;
59 int32_t base_width, base_height;
63 #define USPosition (1L << 0)
64 #define USSize (1L << 1)
65 #define PPosition (1L << 2)
66 #define PSize (1L << 3)
67 #define PMinSize (1L << 4)
68 #define PMaxSize (1L << 5)
69 #define PResizeInc (1L << 6)
70 #define PAspect (1L << 7)
71 #define PBaseSize (1L << 8)
72 #define PWinGravity (1L << 9)
74 struct motif_wm_hints {
82 #define MWM_HINTS_FUNCTIONS (1L << 0)
83 #define MWM_HINTS_DECORATIONS (1L << 1)
84 #define MWM_HINTS_INPUT_MODE (1L << 2)
85 #define MWM_HINTS_STATUS (1L << 3)
87 #define MWM_FUNC_ALL (1L << 0)
88 #define MWM_FUNC_RESIZE (1L << 1)
89 #define MWM_FUNC_MOVE (1L << 2)
90 #define MWM_FUNC_MINIMIZE (1L << 3)
91 #define MWM_FUNC_MAXIMIZE (1L << 4)
92 #define MWM_FUNC_CLOSE (1L << 5)
94 #define MWM_DECOR_ALL (1L << 0)
95 #define MWM_DECOR_BORDER (1L << 1)
96 #define MWM_DECOR_RESIZEH (1L << 2)
97 #define MWM_DECOR_TITLE (1L << 3)
98 #define MWM_DECOR_MENU (1L << 4)
99 #define MWM_DECOR_MINIMIZE (1L << 5)
100 #define MWM_DECOR_MAXIMIZE (1L << 6)
102 #define MWM_DECOR_EVERYTHING \
103 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
104 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
106 #define MWM_INPUT_MODELESS 0
107 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
108 #define MWM_INPUT_SYSTEM_MODAL 2
109 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
110 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
112 #define MWM_TEAROFF_WINDOW (1L<<0)
114 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
115 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
116 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
117 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
118 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
119 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
120 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
121 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
122 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
123 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
124 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
125 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
127 struct weston_wm_window {
128 struct weston_wm *wm;
130 xcb_window_t frame_id;
132 cairo_surface_t *cairo_surface;
134 struct weston_surface *surface;
135 struct shell_surface *shsurf;
136 struct wl_listener surface_destroy_listener;
137 struct wl_event_source *repaint_source;
138 struct wl_event_source *configure_source;
139 int properties_dirty;
144 struct weston_wm_window *transient_for;
150 int saved_width, saved_height;
152 int override_redirect;
158 struct wm_size_hints size_hints;
159 struct motif_wm_hints motif_hints;
163 static struct weston_wm_window *
164 get_wm_window(struct weston_surface *surface);
167 weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
170 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
173 xserver_map_shell_surface(struct weston_wm_window *window,
174 struct weston_surface *surface);
176 static int __attribute__ ((format (printf, 1, 2)))
177 wm_log(const char *fmt, ...)
184 l = weston_vlog(fmt, argp);
193 static int __attribute__ ((format (printf, 1, 2)))
194 wm_log_continue(const char *fmt, ...)
201 l = weston_vlog_continue(fmt, argp);
210 static bool __attribute__ ((warn_unused_result))
211 wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
212 struct weston_wm_window **window)
214 *window = hash_table_lookup(wm->window_hash, hash);
221 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
223 xcb_get_atom_name_cookie_t cookie;
224 xcb_get_atom_name_reply_t *reply;
225 xcb_generic_error_t *e;
226 static char buffer[64];
228 if (atom == XCB_ATOM_NONE)
231 cookie = xcb_get_atom_name (c, atom);
232 reply = xcb_get_atom_name_reply (c, cookie, &e);
235 snprintf(buffer, sizeof buffer, "%.*s",
236 xcb_get_atom_name_name_length (reply),
237 xcb_get_atom_name_name (reply));
239 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
248 xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
250 xcb_connection_t *c = wm->conn;
251 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
252 xcb_screen_t *screen = s.data;
255 xcb_render_picture_t pic;
257 int stride = img->width * 4;
259 pix = xcb_generate_id(c);
260 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
262 pic = xcb_generate_id(c);
263 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
265 gc = xcb_generate_id(c);
266 xcb_create_gc(c, gc, pix, 0, 0);
268 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
269 img->width, img->height, 0, 0, 0, 32,
270 stride * img->height, (uint8_t *) img->pixels);
273 cursor = xcb_generate_id(c);
274 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
276 xcb_render_free_picture(c, pic);
277 xcb_free_pixmap(c, pix);
283 xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
285 /* TODO: treat animated cursors as well */
286 if (images->nimage != 1)
289 return xcb_cursor_image_load_cursor(wm, images->images[0]);
293 xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
296 XcursorImages *images;
303 v = getenv ("XCURSOR_SIZE");
310 images = XcursorLibraryLoadImages (file, NULL, size);
314 cursor = xcb_cursor_images_load_cursor (wm, images);
315 XcursorImagesDestroy (images);
321 dump_property(struct weston_wm *wm,
322 xcb_atom_t property, xcb_get_property_reply_t *reply)
325 const char *text_value, *name;
326 xcb_atom_t *atom_value;
330 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
332 wm_log_continue("(no reply)\n");
336 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
337 get_atom_name(wm->conn, reply->type),
339 xcb_get_property_value_length(reply),
342 if (reply->type == wm->atom.incr) {
343 incr_value = xcb_get_property_value(reply);
344 wm_log_continue("%d\n", *incr_value);
345 } else if (reply->type == wm->atom.utf8_string ||
346 reply->type == wm->atom.string) {
347 text_value = xcb_get_property_value(reply);
348 if (reply->value_len > 40)
351 len = reply->value_len;
352 wm_log_continue("\"%.*s\"\n", len, text_value);
353 } else if (reply->type == XCB_ATOM_ATOM) {
354 atom_value = xcb_get_property_value(reply);
355 for (i = 0; i < reply->value_len; i++) {
356 name = get_atom_name(wm->conn, atom_value[i]);
357 if (width + strlen(name) + 2 > 78) {
358 wm_log_continue("\n ");
361 width += wm_log_continue(", ");
364 width += wm_log_continue("%s", name);
366 wm_log_continue("\n");
368 wm_log_continue("huh?\n");
373 read_and_dump_property(struct weston_wm *wm,
374 xcb_window_t window, xcb_atom_t property)
376 xcb_get_property_reply_t *reply;
377 xcb_get_property_cookie_t cookie;
379 cookie = xcb_get_property(wm->conn, 0, window,
380 property, XCB_ATOM_ANY, 0, 2048);
381 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
383 dump_property(wm, property, reply);
388 /* We reuse some predefined, but otherwise useles atoms */
389 #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
390 #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
391 #define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
392 #define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
395 weston_wm_window_read_properties(struct weston_wm_window *window)
397 struct weston_wm *wm = window->wm;
398 struct weston_shell_interface *shell_interface =
399 &wm->server->compositor->shell_interface;
401 #define F(field) offsetof(struct weston_wm_window, field)
407 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
408 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
409 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
410 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
411 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
412 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
413 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
414 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
415 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
416 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
417 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
421 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
422 xcb_get_property_reply_t *reply;
429 if (!window->properties_dirty)
431 window->properties_dirty = 0;
433 for (i = 0; i < ARRAY_LENGTH(props); i++)
434 cookie[i] = xcb_get_property(wm->conn,
438 XCB_ATOM_ANY, 0, 2048);
440 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
441 window->size_hints.flags = 0;
442 window->motif_hints.flags = 0;
443 window->delete_window = 0;
445 for (i = 0; i < ARRAY_LENGTH(props); i++) {
446 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
448 /* Bad window, typically */
450 if (reply->type == XCB_ATOM_NONE) {
451 /* No such property */
456 p = ((char *) window + props[i].offset);
458 switch (props[i].type) {
459 case XCB_ATOM_WM_CLIENT_MACHINE:
460 case XCB_ATOM_STRING:
461 /* FIXME: We're using this for both string and
467 strndup(xcb_get_property_value(reply),
468 xcb_get_property_value_length(reply));
470 case XCB_ATOM_WINDOW:
471 xid = xcb_get_property_value(reply);
472 if (!wm_lookup_window(wm, *xid, p))
473 weston_log("XCB_ATOM_WINDOW contains window"
474 " id not found in hash table.\n");
476 case XCB_ATOM_CARDINAL:
478 atom = xcb_get_property_value(reply);
479 *(xcb_atom_t *) p = *atom;
481 case TYPE_WM_PROTOCOLS:
482 atom = xcb_get_property_value(reply);
483 for (i = 0; i < reply->value_len; i++)
484 if (atom[i] == wm->atom.wm_delete_window) {
485 window->delete_window = 1;
489 case TYPE_WM_NORMAL_HINTS:
490 memcpy(&window->size_hints,
491 xcb_get_property_value(reply),
492 sizeof window->size_hints);
494 case TYPE_NET_WM_STATE:
495 window->fullscreen = 0;
496 atom = xcb_get_property_value(reply);
497 for (i = 0; i < reply->value_len; i++) {
498 if (atom[i] == wm->atom.net_wm_state_fullscreen)
499 window->fullscreen = 1;
500 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
501 window->maximized_vert = 1;
502 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
503 window->maximized_horz = 1;
506 case TYPE_MOTIF_WM_HINTS:
507 memcpy(&window->motif_hints,
508 xcb_get_property_value(reply),
509 sizeof window->motif_hints);
510 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
511 if (window->motif_hints.decorations & MWM_DECOR_ALL)
512 /* MWM_DECOR_ALL means all except the other values listed. */
514 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
517 window->motif_hints.decorations;
526 if (window->pid > 0) {
527 gethostname(name, sizeof(name));
528 for (i = 0; i < sizeof(name); i++) {
532 if (i == sizeof(name))
533 name[0] = '\0'; /* ignore stupid hostnames */
535 /* this is only one heuristic to guess the PID of a client is
536 * valid, assuming it's compliant with icccm and ewmh.
537 * Non-compliants and remote applications of course fail. */
538 if (!window->machine || strcmp(window->machine, name))
542 if (window->shsurf && window->name)
543 shell_interface->set_title(window->shsurf, window->name);
544 if (window->frame && window->name)
545 frame_set_title(window->frame, window->name);
546 if (window->shsurf && window->pid > 0)
547 shell_interface->set_pid(window->shsurf, window->pid);
551 weston_wm_window_get_frame_size(struct weston_wm_window *window,
552 int *width, int *height)
554 struct theme *t = window->wm->theme;
556 if (window->fullscreen) {
557 *width = window->width;
558 *height = window->height;
559 } else if (window->decorate && window->frame) {
560 *width = frame_width(window->frame);
561 *height = frame_height(window->frame);
563 *width = window->width + t->margin * 2;
564 *height = window->height + t->margin * 2;
569 weston_wm_window_get_child_position(struct weston_wm_window *window,
572 struct theme *t = window->wm->theme;
574 if (window->fullscreen) {
577 } else if (window->decorate && window->frame) {
578 frame_interior(window->frame, x, y, NULL, NULL);
586 weston_wm_window_send_configure_notify(struct weston_wm_window *window)
588 xcb_configure_notify_event_t configure_notify;
589 struct weston_wm *wm = window->wm;
592 weston_wm_window_get_child_position(window, &x, &y);
593 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
594 configure_notify.pad0 = 0;
595 configure_notify.event = window->id;
596 configure_notify.window = window->id;
597 configure_notify.above_sibling = XCB_WINDOW_NONE;
598 configure_notify.x = x;
599 configure_notify.y = y;
600 configure_notify.width = window->width;
601 configure_notify.height = window->height;
602 configure_notify.border_width = 0;
603 configure_notify.override_redirect = 0;
604 configure_notify.pad1 = 0;
606 xcb_send_event(wm->conn, 0, window->id,
607 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
608 (char *) &configure_notify);
612 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
614 xcb_configure_request_event_t *configure_request =
615 (xcb_configure_request_event_t *) event;
616 struct weston_wm_window *window;
617 uint32_t mask, values[16];
618 int x, y, width, height, i = 0;
620 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
621 configure_request->window,
622 configure_request->x, configure_request->y,
623 configure_request->width, configure_request->height);
625 if (!wm_lookup_window(wm, configure_request->window, &window))
628 if (window->fullscreen) {
629 weston_wm_window_send_configure_notify(window);
633 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
634 window->width = configure_request->width;
635 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
636 window->height = configure_request->height;
639 frame_resize_inside(window->frame, window->width, window->height);
641 weston_wm_window_get_child_position(window, &x, &y);
644 values[i++] = window->width;
645 values[i++] = window->height;
647 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
648 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
649 XCB_CONFIG_WINDOW_BORDER_WIDTH;
650 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
651 values[i++] = configure_request->sibling;
652 mask |= XCB_CONFIG_WINDOW_SIBLING;
654 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
655 values[i++] = configure_request->stack_mode;
656 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
659 xcb_configure_window(wm->conn, window->id, mask, values);
661 weston_wm_window_get_frame_size(window, &width, &height);
664 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
665 xcb_configure_window(wm->conn, window->frame_id, mask, values);
667 weston_wm_window_schedule_repaint(window);
671 our_resource(struct weston_wm *wm, uint32_t id)
673 const xcb_setup_t *setup;
675 setup = xcb_get_setup(wm->conn);
677 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
681 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
683 xcb_configure_notify_event_t *configure_notify =
684 (xcb_configure_notify_event_t *) event;
685 struct weston_wm_window *window;
687 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
688 configure_notify->window,
689 configure_notify->x, configure_notify->y,
690 configure_notify->width, configure_notify->height);
692 if (!wm_lookup_window(wm, configure_notify->window, &window))
695 window->x = configure_notify->x;
696 window->y = configure_notify->y;
697 window->pos_dirty = false;
699 if (window->override_redirect) {
700 window->width = configure_notify->width;
701 window->height = configure_notify->height;
703 frame_resize_inside(window->frame,
704 window->width, window->height);
709 weston_wm_kill_client(struct wl_listener *listener, void *data)
711 struct weston_surface *surface = data;
712 struct weston_wm_window *window = get_wm_window(surface);
717 kill(window->pid, SIGKILL);
721 weston_wm_create_surface(struct wl_listener *listener, void *data)
723 struct weston_surface *surface = data;
724 struct weston_wm *wm =
725 container_of(listener,
726 struct weston_wm, create_surface_listener);
727 struct weston_wm_window *window;
729 if (wl_resource_get_client(surface->resource) != wm->server->client)
732 wl_list_for_each(window, &wm->unpaired_window_list, link)
733 if (window->surface_id ==
734 wl_resource_get_id(surface->resource)) {
735 xserver_map_shell_surface(window, surface);
736 window->surface_id = 0;
737 wl_list_remove(&window->link);
743 weston_wm_send_focus_window(struct weston_wm *wm,
744 struct weston_wm_window *window)
746 xcb_client_message_event_t client_message;
751 if (window->override_redirect)
754 client_message.response_type = XCB_CLIENT_MESSAGE;
755 client_message.format = 32;
756 client_message.window = window->id;
757 client_message.type = wm->atom.wm_protocols;
758 client_message.data.data32[0] = wm->atom.wm_take_focus;
759 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
761 xcb_send_event(wm->conn, 0, window->id,
762 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
763 (char *) &client_message);
765 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
766 window->id, XCB_TIME_CURRENT_TIME);
768 values[0] = XCB_STACK_MODE_ABOVE;
769 xcb_configure_window (wm->conn, window->frame_id,
770 XCB_CONFIG_WINDOW_STACK_MODE, values);
772 xcb_set_input_focus (wm->conn,
773 XCB_INPUT_FOCUS_POINTER_ROOT,
775 XCB_TIME_CURRENT_TIME);
780 weston_wm_window_activate(struct wl_listener *listener, void *data)
782 struct weston_surface_activation_data *activation_data = data;
783 struct weston_surface *surface = activation_data->surface;
784 struct weston_wm_window *window = NULL;
785 struct weston_wm *wm =
786 container_of(listener, struct weston_wm, activate_listener);
789 window = get_wm_window(surface);
793 weston_wm_set_net_active_window(wm, window->id);
795 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
798 weston_wm_send_focus_window(wm, window);
800 if (wm->focus_window) {
801 if (wm->focus_window->frame)
802 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
803 weston_wm_window_schedule_repaint(wm->focus_window);
805 wm->focus_window = window;
806 if (wm->focus_window) {
807 if (wm->focus_window->frame)
808 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
809 weston_wm_window_schedule_repaint(wm->focus_window);
816 #define ICCCM_WITHDRAWN_STATE 0
817 #define ICCCM_NORMAL_STATE 1
818 #define ICCCM_ICONIC_STATE 3
821 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
823 struct weston_wm *wm = window->wm;
824 uint32_t property[2];
827 property[1] = XCB_WINDOW_NONE;
829 xcb_change_property(wm->conn,
830 XCB_PROP_MODE_REPLACE,
839 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
841 struct weston_wm *wm = window->wm;
842 uint32_t property[3];
846 if (window->fullscreen)
847 property[i++] = wm->atom.net_wm_state_fullscreen;
848 if (window->maximized_vert)
849 property[i++] = wm->atom.net_wm_state_maximized_vert;
850 if (window->maximized_horz)
851 property[i++] = wm->atom.net_wm_state_maximized_horz;
853 xcb_change_property(wm->conn,
854 XCB_PROP_MODE_REPLACE,
856 wm->atom.net_wm_state,
863 weston_wm_window_create_frame(struct weston_wm_window *window)
865 struct weston_wm *wm = window->wm;
867 int x, y, width, height;
868 int buttons = FRAME_BUTTON_CLOSE;
870 if (window->decorate & MWM_DECOR_MAXIMIZE)
871 buttons |= FRAME_BUTTON_MAXIMIZE;
873 window->frame = frame_create(window->wm->theme,
874 window->width, window->height,
875 buttons, window->name);
876 frame_resize_inside(window->frame, window->width, window->height);
878 weston_wm_window_get_frame_size(window, &width, &height);
879 weston_wm_window_get_child_position(window, &x, &y);
881 values[0] = wm->screen->black_pixel;
883 XCB_EVENT_MASK_KEY_PRESS |
884 XCB_EVENT_MASK_KEY_RELEASE |
885 XCB_EVENT_MASK_BUTTON_PRESS |
886 XCB_EVENT_MASK_BUTTON_RELEASE |
887 XCB_EVENT_MASK_POINTER_MOTION |
888 XCB_EVENT_MASK_ENTER_WINDOW |
889 XCB_EVENT_MASK_LEAVE_WINDOW |
890 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
891 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
892 values[2] = wm->colormap;
894 window->frame_id = xcb_generate_id(wm->conn);
895 xcb_create_window(wm->conn,
902 XCB_WINDOW_CLASS_INPUT_OUTPUT,
904 XCB_CW_BORDER_PIXEL |
906 XCB_CW_COLORMAP, values);
908 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
911 xcb_configure_window(wm->conn, window->id,
912 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
914 window->cairo_surface =
915 cairo_xcb_surface_create_with_xrender_format(wm->conn,
921 hash_table_insert(wm->window_hash, window->frame_id, window);
925 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
926 * Passing a <0 desktop value deletes the property.
929 weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
933 xcb_change_property(window->wm->conn,
934 XCB_PROP_MODE_REPLACE,
936 window->wm->atom.net_wm_desktop,
941 xcb_delete_property(window->wm->conn,
943 window->wm->atom.net_wm_desktop);
948 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
950 xcb_map_request_event_t *map_request =
951 (xcb_map_request_event_t *) event;
952 struct weston_wm_window *window;
954 if (our_resource(wm, map_request->window)) {
955 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
956 map_request->window);
960 if (!wm_lookup_window(wm, map_request->window, &window))
963 weston_wm_window_read_properties(window);
965 if (window->frame_id == XCB_WINDOW_NONE)
966 weston_wm_window_create_frame(window);
968 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
969 window->id, window, window->frame_id);
971 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
972 weston_wm_window_set_net_wm_state(window);
973 weston_wm_window_set_virtual_desktop(window, 0);
975 xcb_map_window(wm->conn, map_request->window);
976 xcb_map_window(wm->conn, window->frame_id);
980 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
982 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
984 if (our_resource(wm, map_notify->window)) {
985 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
990 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
994 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
996 xcb_unmap_notify_event_t *unmap_notify =
997 (xcb_unmap_notify_event_t *) event;
998 struct weston_wm_window *window;
1000 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1001 unmap_notify->window,
1002 unmap_notify->event,
1003 our_resource(wm, unmap_notify->window) ? ", ours" : "");
1005 if (our_resource(wm, unmap_notify->window))
1008 if (unmap_notify->response_type & SEND_EVENT_MASK)
1009 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1010 * as it may come in after we've destroyed the window. */
1013 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1016 if (window->surface_id) {
1017 /* Make sure we're not on the unpaired surface list or we
1018 * could be assigned a surface during surface creation that
1019 * was mapped before this unmap request.
1021 wl_list_remove(&window->link);
1022 window->surface_id = 0;
1024 if (wm->focus_window == window)
1025 wm->focus_window = NULL;
1026 if (window->surface)
1027 wl_list_remove(&window->surface_destroy_listener.link);
1028 window->surface = NULL;
1029 window->shsurf = NULL;
1031 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1032 weston_wm_window_set_virtual_desktop(window, -1);
1034 xcb_unmap_window(wm->conn, window->frame_id);
1038 weston_wm_window_draw_decoration(void *data)
1040 struct weston_wm_window *window = data;
1041 struct weston_wm *wm = window->wm;
1042 struct theme *t = wm->theme;
1044 int x, y, width, height;
1045 int32_t input_x, input_y, input_w, input_h;
1046 struct weston_shell_interface *shell_interface =
1047 &wm->server->compositor->shell_interface;
1049 struct weston_view *view;
1051 weston_wm_window_read_properties(window);
1053 window->repaint_source = NULL;
1055 weston_wm_window_get_frame_size(window, &width, &height);
1056 weston_wm_window_get_child_position(window, &x, &y);
1058 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1059 cr = cairo_create(window->cairo_surface);
1061 if (window->fullscreen) {
1063 } else if (window->decorate) {
1064 if (wm->focus_window == window)
1065 flags |= THEME_FRAME_ACTIVE;
1067 frame_repaint(window->frame, cr);
1069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1070 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1073 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
1078 if (window->surface) {
1079 pixman_region32_fini(&window->surface->pending.opaque);
1080 if (window->has_alpha) {
1081 pixman_region32_init(&window->surface->pending.opaque);
1083 /* We leave an extra pixel around the X window area to
1084 * make sure we don't sample from the undefined alpha
1085 * channel when filtering. */
1086 pixman_region32_init_rect(&window->surface->pending.opaque,
1089 window->height + 2);
1091 wl_list_for_each(view, &window->surface->views, surface_link)
1092 weston_view_geometry_dirty(view);
1094 pixman_region32_fini(&window->surface->pending.input);
1096 if (window->decorate && !window->fullscreen) {
1097 frame_input_rect(window->frame, &input_x, &input_y,
1098 &input_w, &input_h);
1106 pixman_region32_init_rect(&window->surface->pending.input,
1107 input_x, input_y, input_w, input_h);
1109 shell_interface->set_window_geometry(window->shsurf,
1110 input_x, input_y, input_w, input_h);
1115 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1117 struct weston_wm *wm = window->wm;
1118 struct weston_view *view;
1121 if (window->frame_id == XCB_WINDOW_NONE) {
1122 if (window->surface != NULL) {
1123 weston_wm_window_get_frame_size(window, &width, &height);
1124 pixman_region32_fini(&window->surface->pending.opaque);
1125 if (window->has_alpha) {
1126 pixman_region32_init(&window->surface->pending.opaque);
1128 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1131 wl_list_for_each(view, &window->surface->views, surface_link)
1132 weston_view_geometry_dirty(view);
1137 if (window->repaint_source)
1140 window->repaint_source =
1141 wl_event_loop_add_idle(wm->server->loop,
1142 weston_wm_window_draw_decoration,
1147 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1149 xcb_property_notify_event_t *property_notify =
1150 (xcb_property_notify_event_t *) event;
1151 struct weston_wm_window *window;
1153 if (!wm_lookup_window(wm, property_notify->window, &window))
1156 window->properties_dirty = 1;
1158 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
1159 if (property_notify->state == XCB_PROPERTY_DELETE)
1160 wm_log("deleted\n");
1162 read_and_dump_property(wm, property_notify->window,
1163 property_notify->atom);
1165 if (property_notify->atom == wm->atom.net_wm_name ||
1166 property_notify->atom == XCB_ATOM_WM_NAME)
1167 weston_wm_window_schedule_repaint(window);
1171 weston_wm_window_create(struct weston_wm *wm,
1172 xcb_window_t id, int width, int height, int x, int y, int override)
1174 struct weston_wm_window *window;
1176 xcb_get_geometry_cookie_t geometry_cookie;
1177 xcb_get_geometry_reply_t *geometry_reply;
1179 window = zalloc(sizeof *window);
1180 if (window == NULL) {
1181 wm_log("failed to allocate window\n");
1185 geometry_cookie = xcb_get_geometry(wm->conn, id);
1187 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1188 XCB_EVENT_MASK_FOCUS_CHANGE;
1189 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1193 window->properties_dirty = 1;
1194 window->override_redirect = override;
1195 window->width = width;
1196 window->height = height;
1199 window->pos_dirty = false;
1201 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1202 /* technically we should use XRender and check the visual format's
1203 alpha_mask, but checking depth is simpler and works in all known cases */
1204 if (geometry_reply != NULL)
1205 window->has_alpha = geometry_reply->depth == 32;
1206 free(geometry_reply);
1208 hash_table_insert(wm->window_hash, id, window);
1212 weston_wm_window_destroy(struct weston_wm_window *window)
1214 struct weston_wm *wm = window->wm;
1216 if (window->repaint_source)
1217 wl_event_source_remove(window->repaint_source);
1218 if (window->cairo_surface)
1219 cairo_surface_destroy(window->cairo_surface);
1221 if (window->frame_id) {
1222 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1223 xcb_destroy_window(wm->conn, window->frame_id);
1224 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1225 weston_wm_window_set_virtual_desktop(window, -1);
1226 hash_table_remove(wm->window_hash, window->frame_id);
1227 window->frame_id = XCB_WINDOW_NONE;
1230 if (window->surface_id)
1231 wl_list_remove(&window->link);
1233 if (window->surface)
1234 wl_list_remove(&window->surface_destroy_listener.link);
1236 hash_table_remove(window->wm->window_hash, window->id);
1241 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1243 xcb_create_notify_event_t *create_notify =
1244 (xcb_create_notify_event_t *) event;
1246 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1247 create_notify->window,
1248 create_notify->width, create_notify->height,
1249 create_notify->override_redirect ? ", override" : "",
1250 our_resource(wm, create_notify->window) ? ", ours" : "");
1252 if (our_resource(wm, create_notify->window))
1255 weston_wm_window_create(wm, create_notify->window,
1256 create_notify->width, create_notify->height,
1257 create_notify->x, create_notify->y,
1258 create_notify->override_redirect);
1262 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1264 xcb_destroy_notify_event_t *destroy_notify =
1265 (xcb_destroy_notify_event_t *) event;
1266 struct weston_wm_window *window;
1268 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1269 destroy_notify->window,
1270 destroy_notify->event,
1271 our_resource(wm, destroy_notify->window) ? ", ours" : "");
1273 if (our_resource(wm, destroy_notify->window))
1276 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1279 weston_wm_window_destroy(window);
1283 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1285 xcb_reparent_notify_event_t *reparent_notify =
1286 (xcb_reparent_notify_event_t *) event;
1287 struct weston_wm_window *window;
1289 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1290 reparent_notify->window,
1291 reparent_notify->parent,
1292 reparent_notify->event);
1294 if (reparent_notify->parent == wm->screen->root) {
1295 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1296 reparent_notify->x, reparent_notify->y,
1297 reparent_notify->override_redirect);
1298 } else if (!our_resource(wm, reparent_notify->parent)) {
1299 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1301 weston_wm_window_destroy(window);
1305 struct weston_seat *
1306 weston_wm_pick_seat(struct weston_wm *wm)
1308 return container_of(wm->server->compositor->seat_list.next,
1309 struct weston_seat, link);
1312 static struct weston_seat *
1313 weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1315 struct weston_wm *wm = window->wm;
1316 struct weston_seat *seat, *s;
1319 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
1320 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1321 struct weston_pointer *old_pointer =
1322 weston_seat_get_pointer(seat);
1324 if (pointer && pointer->focus &&
1325 pointer->focus->surface == window->surface &&
1326 pointer->button_count > 0 &&
1328 pointer->grab_serial -
1329 old_pointer->grab_serial < (1 << 30)))
1337 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1338 xcb_client_message_event_t *client_message)
1340 static const int map[] = {
1341 THEME_LOCATION_RESIZING_TOP_LEFT,
1342 THEME_LOCATION_RESIZING_TOP,
1343 THEME_LOCATION_RESIZING_TOP_RIGHT,
1344 THEME_LOCATION_RESIZING_RIGHT,
1345 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1346 THEME_LOCATION_RESIZING_BOTTOM,
1347 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1348 THEME_LOCATION_RESIZING_LEFT
1351 struct weston_wm *wm = window->wm;
1352 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
1353 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1355 struct weston_shell_interface *shell_interface =
1356 &wm->server->compositor->shell_interface;
1358 if (!pointer || pointer->button_count != 1
1360 || pointer->focus->surface != window->surface)
1363 detail = client_message->data.data32[2];
1365 case _NET_WM_MOVERESIZE_MOVE:
1366 shell_interface->move(window->shsurf, pointer);
1368 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1369 case _NET_WM_MOVERESIZE_SIZE_TOP:
1370 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1371 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1372 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1373 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1374 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1375 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1376 shell_interface->resize(window->shsurf, pointer, map[detail]);
1378 case _NET_WM_MOVERESIZE_CANCEL:
1383 #define _NET_WM_STATE_REMOVE 0
1384 #define _NET_WM_STATE_ADD 1
1385 #define _NET_WM_STATE_TOGGLE 2
1388 update_state(int action, int *state)
1390 int new_state, changed;
1393 case _NET_WM_STATE_REMOVE:
1396 case _NET_WM_STATE_ADD:
1399 case _NET_WM_STATE_TOGGLE:
1400 new_state = !*state;
1406 changed = (*state != new_state);
1413 weston_wm_window_configure(void *data);
1416 weston_wm_window_set_toplevel(struct weston_wm_window *window)
1418 struct weston_shell_interface *shell_interface =
1419 &window->wm->server->compositor->shell_interface;
1421 shell_interface->set_toplevel(window->shsurf);
1422 window->width = window->saved_width;
1423 window->height = window->saved_height;
1425 frame_resize_inside(window->frame,
1428 weston_wm_window_configure(window);
1432 weston_wm_window_is_maximized(struct weston_wm_window *window)
1434 return window->maximized_horz && window->maximized_vert;
1438 weston_wm_window_handle_state(struct weston_wm_window *window,
1439 xcb_client_message_event_t *client_message)
1441 struct weston_wm *wm = window->wm;
1442 struct weston_shell_interface *shell_interface =
1443 &wm->server->compositor->shell_interface;
1444 uint32_t action, property;
1445 int maximized = weston_wm_window_is_maximized(window);
1447 action = client_message->data.data32[0];
1448 property = client_message->data.data32[1];
1450 if (property == wm->atom.net_wm_state_fullscreen &&
1451 update_state(action, &window->fullscreen)) {
1452 weston_wm_window_set_net_wm_state(window);
1453 if (window->fullscreen) {
1454 window->saved_width = window->width;
1455 window->saved_height = window->height;
1458 shell_interface->set_fullscreen(window->shsurf,
1459 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1463 weston_wm_window_set_toplevel(window);
1466 if (property == wm->atom.net_wm_state_maximized_vert &&
1467 update_state(action, &window->maximized_vert))
1468 weston_wm_window_set_net_wm_state(window);
1469 if (property == wm->atom.net_wm_state_maximized_horz &&
1470 update_state(action, &window->maximized_horz))
1471 weston_wm_window_set_net_wm_state(window);
1473 if (maximized != weston_wm_window_is_maximized(window)) {
1474 if (weston_wm_window_is_maximized(window)) {
1475 window->saved_width = window->width;
1476 window->saved_height = window->height;
1479 shell_interface->set_maximized(window->shsurf);
1480 } else if (window->shsurf) {
1481 weston_wm_window_set_toplevel(window);
1488 surface_destroy(struct wl_listener *listener, void *data)
1490 struct weston_wm_window *window =
1491 container_of(listener,
1492 struct weston_wm_window, surface_destroy_listener);
1494 wm_log("surface for xid %d destroyed\n", window->id);
1496 /* This should have been freed by the shell.
1497 * Don't try to use it later. */
1498 window->shsurf = NULL;
1499 window->surface = NULL;
1503 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1504 xcb_client_message_event_t *client_message)
1506 struct weston_wm *wm = window->wm;
1507 struct wl_resource *resource;
1509 if (window->surface_id != 0) {
1510 wm_log("already have surface id for window %d\n", window->id);
1514 /* Xwayland will send the wayland requests to create the
1515 * wl_surface before sending this client message. Even so, we
1516 * can end up handling the X event before the wayland requests
1517 * and thus when we try to look up the surface ID, the surface
1518 * hasn't been created yet. In that case put the window on
1519 * the unpaired window list and continue when the surface gets
1521 uint32_t id = client_message->data.data32[0];
1522 resource = wl_client_get_object(wm->server->client, id);
1524 window->surface_id = 0;
1525 xserver_map_shell_surface(window,
1526 wl_resource_get_user_data(resource));
1529 window->surface_id = id;
1530 wl_list_insert(&wm->unpaired_window_list, &window->link);
1535 weston_wm_handle_client_message(struct weston_wm *wm,
1536 xcb_generic_event_t *event)
1538 xcb_client_message_event_t *client_message =
1539 (xcb_client_message_event_t *) event;
1540 struct weston_wm_window *window;
1542 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1543 get_atom_name(wm->conn, client_message->type),
1544 client_message->data.data32[0],
1545 client_message->data.data32[1],
1546 client_message->data.data32[2],
1547 client_message->data.data32[3],
1548 client_message->data.data32[4],
1549 client_message->window);
1551 /* The window may get created and destroyed before we actually
1552 * handle the message. If it doesn't exist, bail.
1554 if (!wm_lookup_window(wm, client_message->window, &window))
1557 if (client_message->type == wm->atom.net_wm_moveresize)
1558 weston_wm_window_handle_moveresize(window, client_message);
1559 else if (client_message->type == wm->atom.net_wm_state)
1560 weston_wm_window_handle_state(window, client_message);
1561 else if (client_message->type == wm->atom.wl_surface_id)
1562 weston_wm_window_handle_surface_id(window, client_message);
1570 XWM_CURSOR_TOP_LEFT,
1571 XWM_CURSOR_TOP_RIGHT,
1572 XWM_CURSOR_BOTTOM_LEFT,
1573 XWM_CURSOR_BOTTOM_RIGHT,
1574 XWM_CURSOR_LEFT_PTR,
1578 * The following correspondences between file names and cursors was copied
1579 * from: https://bugs.kde.org/attachment.cgi?id=67313
1582 static const char *bottom_left_corners[] = {
1583 "bottom_left_corner",
1588 static const char *bottom_right_corners[] = {
1589 "bottom_right_corner",
1594 static const char *bottom_sides[] = {
1600 static const char *left_ptrs[] = {
1607 static const char *left_sides[] = {
1613 static const char *right_sides[] = {
1619 static const char *top_left_corners[] = {
1625 static const char *top_right_corners[] = {
1631 static const char *top_sides[] = {
1637 struct cursor_alternatives {
1642 static const struct cursor_alternatives cursors[] = {
1643 {top_sides, ARRAY_LENGTH(top_sides)},
1644 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1645 {left_sides, ARRAY_LENGTH(left_sides)},
1646 {right_sides, ARRAY_LENGTH(right_sides)},
1647 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1648 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1649 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1650 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1651 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1655 weston_wm_create_cursors(struct weston_wm *wm)
1658 int i, count = ARRAY_LENGTH(cursors);
1661 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1662 for (i = 0; i < count; i++) {
1663 for (j = 0; j < cursors[i].count; j++) {
1664 name = cursors[i].names[j];
1666 xcb_cursor_library_load_cursor(wm, name);
1667 if (wm->cursors[i] != (xcb_cursor_t)-1)
1672 wm->last_cursor = -1;
1676 weston_wm_destroy_cursors(struct weston_wm *wm)
1680 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1681 xcb_free_cursor(wm->conn, wm->cursors[i]);
1687 get_cursor_for_location(enum theme_location location)
1690 case THEME_LOCATION_RESIZING_TOP:
1691 return XWM_CURSOR_TOP;
1692 case THEME_LOCATION_RESIZING_BOTTOM:
1693 return XWM_CURSOR_BOTTOM;
1694 case THEME_LOCATION_RESIZING_LEFT:
1695 return XWM_CURSOR_LEFT;
1696 case THEME_LOCATION_RESIZING_RIGHT:
1697 return XWM_CURSOR_RIGHT;
1698 case THEME_LOCATION_RESIZING_TOP_LEFT:
1699 return XWM_CURSOR_TOP_LEFT;
1700 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1701 return XWM_CURSOR_TOP_RIGHT;
1702 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1703 return XWM_CURSOR_BOTTOM_LEFT;
1704 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1705 return XWM_CURSOR_BOTTOM_RIGHT;
1706 case THEME_LOCATION_EXTERIOR:
1707 case THEME_LOCATION_TITLEBAR:
1709 return XWM_CURSOR_LEFT_PTR;
1714 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1717 uint32_t cursor_value_list;
1719 if (wm->last_cursor == cursor)
1722 wm->last_cursor = cursor;
1724 cursor_value_list = wm->cursors[cursor];
1725 xcb_change_window_attributes (wm->conn, window_id,
1726 XCB_CW_CURSOR, &cursor_value_list);
1727 xcb_flush(wm->conn);
1731 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1733 xcb_client_message_event_t client_message;
1735 if (window->delete_window) {
1736 client_message.response_type = XCB_CLIENT_MESSAGE;
1737 client_message.format = 32;
1738 client_message.window = window->id;
1739 client_message.type = window->wm->atom.wm_protocols;
1740 client_message.data.data32[0] =
1741 window->wm->atom.wm_delete_window;
1742 client_message.data.data32[1] = time;
1744 xcb_send_event(window->wm->conn, 0, window->id,
1745 XCB_EVENT_MASK_NO_EVENT,
1746 (char *) &client_message);
1748 xcb_kill_client(window->wm->conn, window->id);
1753 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1755 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1756 struct weston_shell_interface *shell_interface =
1757 &wm->server->compositor->shell_interface;
1758 struct weston_seat *seat;
1759 struct weston_pointer *pointer;
1760 struct weston_wm_window *window;
1761 enum theme_location location;
1762 enum frame_button_state button_state;
1765 wm_log("XCB_BUTTON_%s (detail %d)\n",
1766 button->response_type == XCB_BUTTON_PRESS ?
1767 "PRESS" : "RELEASE", button->detail);
1769 if (!wm_lookup_window(wm, button->event, &window) ||
1773 if (button->detail != 1 && button->detail != 2)
1776 seat = weston_wm_pick_seat_for_window(window);
1777 pointer = weston_seat_get_pointer(seat);
1779 button_state = button->response_type == XCB_BUTTON_PRESS ?
1780 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1781 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1783 /* Make sure we're looking at the right location. The frame
1784 * could have received a motion event from a pointer from a
1785 * different wl_seat, but under X it looks like our core
1786 * pointer moved. Move the frame pointer to the button press
1787 * location before deciding what to do. */
1788 location = frame_pointer_motion(window->frame, NULL,
1789 button->event_x, button->event_y);
1790 location = frame_pointer_button(window->frame, NULL,
1791 button_id, button_state);
1792 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1793 weston_wm_window_schedule_repaint(window);
1795 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1797 shell_interface->move(window->shsurf, pointer);
1798 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1801 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1803 shell_interface->resize(window->shsurf, pointer, location);
1804 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1807 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1808 weston_wm_window_close(window, button->time);
1809 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
1812 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1813 window->maximized_horz = !window->maximized_horz;
1814 window->maximized_vert = !window->maximized_vert;
1815 if (weston_wm_window_is_maximized(window)) {
1816 window->saved_width = window->width;
1817 window->saved_height = window->height;
1818 shell_interface->set_maximized(window->shsurf);
1820 weston_wm_window_set_toplevel(window);
1822 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1827 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1829 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1830 struct weston_wm_window *window;
1831 enum theme_location location;
1834 if (!wm_lookup_window(wm, motion->event, &window) ||
1838 location = frame_pointer_motion(window->frame, NULL,
1839 motion->event_x, motion->event_y);
1840 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1841 weston_wm_window_schedule_repaint(window);
1843 cursor = get_cursor_for_location(location);
1844 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1848 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1850 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1851 struct weston_wm_window *window;
1852 enum theme_location location;
1855 if (!wm_lookup_window(wm, enter->event, &window) ||
1859 location = frame_pointer_enter(window->frame, NULL,
1860 enter->event_x, enter->event_y);
1861 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1862 weston_wm_window_schedule_repaint(window);
1864 cursor = get_cursor_for_location(location);
1865 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1869 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1871 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1872 struct weston_wm_window *window;
1874 if (!wm_lookup_window(wm, leave->event, &window) ||
1878 frame_pointer_leave(window->frame, NULL);
1879 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1880 weston_wm_window_schedule_repaint(window);
1882 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
1886 weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1888 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1889 /* Do not let X clients change the focus behind the compositor's
1890 * back. Reset the focus to the old one if it changed. */
1891 if (!wm->focus_window || focus->event != wm->focus_window->id)
1892 weston_wm_send_focus_window(wm, wm->focus_window);
1896 weston_wm_handle_event(int fd, uint32_t mask, void *data)
1898 struct weston_wm *wm = data;
1899 xcb_generic_event_t *event;
1902 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1903 if (weston_wm_handle_selection_event(wm, event)) {
1909 if (weston_wm_handle_dnd_event(wm, event)) {
1915 switch (EVENT_TYPE(event)) {
1916 case XCB_BUTTON_PRESS:
1917 case XCB_BUTTON_RELEASE:
1918 weston_wm_handle_button(wm, event);
1920 case XCB_ENTER_NOTIFY:
1921 weston_wm_handle_enter(wm, event);
1923 case XCB_LEAVE_NOTIFY:
1924 weston_wm_handle_leave(wm, event);
1926 case XCB_MOTION_NOTIFY:
1927 weston_wm_handle_motion(wm, event);
1929 case XCB_CREATE_NOTIFY:
1930 weston_wm_handle_create_notify(wm, event);
1932 case XCB_MAP_REQUEST:
1933 weston_wm_handle_map_request(wm, event);
1935 case XCB_MAP_NOTIFY:
1936 weston_wm_handle_map_notify(wm, event);
1938 case XCB_UNMAP_NOTIFY:
1939 weston_wm_handle_unmap_notify(wm, event);
1941 case XCB_REPARENT_NOTIFY:
1942 weston_wm_handle_reparent_notify(wm, event);
1944 case XCB_CONFIGURE_REQUEST:
1945 weston_wm_handle_configure_request(wm, event);
1947 case XCB_CONFIGURE_NOTIFY:
1948 weston_wm_handle_configure_notify(wm, event);
1950 case XCB_DESTROY_NOTIFY:
1951 weston_wm_handle_destroy_notify(wm, event);
1953 case XCB_MAPPING_NOTIFY:
1954 wm_log("XCB_MAPPING_NOTIFY\n");
1956 case XCB_PROPERTY_NOTIFY:
1957 weston_wm_handle_property_notify(wm, event);
1959 case XCB_CLIENT_MESSAGE:
1960 weston_wm_handle_client_message(wm, event);
1963 weston_wm_handle_focus_in(wm, event);
1972 xcb_flush(wm->conn);
1978 weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
1979 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
1980 wm->screen->root, wm->atom.net_active_window,
1981 wm->atom.window, 32, 1, &window);
1985 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1987 xcb_depth_iterator_t d_iter;
1988 xcb_visualtype_iterator_t vt_iter;
1989 xcb_visualtype_t *visualtype;
1991 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1993 while (d_iter.rem > 0) {
1994 if (d_iter.data->depth == 32) {
1995 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1996 visualtype = vt_iter.data;
2000 xcb_depth_next(&d_iter);
2003 if (visualtype == NULL) {
2004 weston_log("no 32 bit visualtype\n");
2008 wm->visual_id = visualtype->visual_id;
2009 wm->colormap = xcb_generate_id(wm->conn);
2010 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2011 wm->colormap, wm->screen->root, wm->visual_id);
2015 weston_wm_get_resources(struct weston_wm *wm)
2018 #define F(field) offsetof(struct weston_wm, field)
2020 static const struct { const char *name; int offset; } atoms[] = {
2021 { "WM_PROTOCOLS", F(atom.wm_protocols) },
2022 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
2023 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2024 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
2025 { "WM_STATE", F(atom.wm_state) },
2026 { "WM_S0", F(atom.wm_s0) },
2027 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
2028 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
2029 { "_NET_WM_NAME", F(atom.net_wm_name) },
2030 { "_NET_WM_PID", F(atom.net_wm_pid) },
2031 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2032 { "_NET_WM_STATE", F(atom.net_wm_state) },
2033 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2034 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
2035 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2036 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2037 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
2038 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
2039 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2041 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2042 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2043 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2044 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2045 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2046 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2047 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
2048 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2049 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
2050 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2051 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2052 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2053 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2054 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2056 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2057 { "_NET_SUPPORTING_WM_CHECK",
2058 F(atom.net_supporting_wm_check) },
2059 { "_NET_SUPPORTED", F(atom.net_supported) },
2060 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
2061 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2062 { "CLIPBOARD", F(atom.clipboard) },
2063 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
2064 { "TARGETS", F(atom.targets) },
2065 { "UTF8_STRING", F(atom.utf8_string) },
2066 { "_WL_SELECTION", F(atom.wl_selection) },
2067 { "INCR", F(atom.incr) },
2068 { "TIMESTAMP", F(atom.timestamp) },
2069 { "MULTIPLE", F(atom.multiple) },
2070 { "UTF8_STRING" , F(atom.utf8_string) },
2071 { "COMPOUND_TEXT", F(atom.compound_text) },
2072 { "TEXT", F(atom.text) },
2073 { "STRING", F(atom.string) },
2074 { "WINDOW", F(atom.window) },
2075 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2076 { "text/plain", F(atom.text_plain) },
2077 { "XdndSelection", F(atom.xdnd_selection) },
2078 { "XdndAware", F(atom.xdnd_aware) },
2079 { "XdndEnter", F(atom.xdnd_enter) },
2080 { "XdndLeave", F(atom.xdnd_leave) },
2081 { "XdndDrop", F(atom.xdnd_drop) },
2082 { "XdndStatus", F(atom.xdnd_status) },
2083 { "XdndFinished", F(atom.xdnd_finished) },
2084 { "XdndTypeList", F(atom.xdnd_type_list) },
2085 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2086 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
2090 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2091 xcb_xfixes_query_version_reply_t *xfixes_reply;
2092 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2093 xcb_intern_atom_reply_t *reply;
2094 xcb_render_query_pict_formats_reply_t *formats_reply;
2095 xcb_render_query_pict_formats_cookie_t formats_cookie;
2096 xcb_render_pictforminfo_t *formats;
2099 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
2100 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
2102 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2104 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2105 cookies[i] = xcb_intern_atom (wm->conn, 0,
2106 strlen(atoms[i].name),
2109 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2110 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2111 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2115 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2116 if (!wm->xfixes || !wm->xfixes->present)
2117 weston_log("xfixes not available\n");
2119 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2120 XCB_XFIXES_MAJOR_VERSION,
2121 XCB_XFIXES_MINOR_VERSION);
2122 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2123 xfixes_cookie, NULL);
2125 weston_log("xfixes version: %d.%d\n",
2126 xfixes_reply->major_version, xfixes_reply->minor_version);
2130 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2132 if (formats_reply == NULL)
2135 formats = xcb_render_query_pict_formats_formats(formats_reply);
2136 for (i = 0; i < formats_reply->num_formats; i++) {
2137 if (formats[i].direct.red_mask != 0xff &&
2138 formats[i].direct.red_shift != 16)
2140 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2141 formats[i].depth == 24)
2142 wm->format_rgb = formats[i];
2143 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2144 formats[i].depth == 32 &&
2145 formats[i].direct.alpha_mask == 0xff &&
2146 formats[i].direct.alpha_shift == 24)
2147 wm->format_rgba = formats[i];
2150 free(formats_reply);
2154 weston_wm_create_wm_window(struct weston_wm *wm)
2156 static const char name[] = "Weston WM";
2158 wm->wm_window = xcb_generate_id(wm->conn);
2159 xcb_create_window(wm->conn,
2160 XCB_COPY_FROM_PARENT,
2166 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2167 wm->screen->root_visual,
2170 xcb_change_property(wm->conn,
2171 XCB_PROP_MODE_REPLACE,
2173 wm->atom.net_supporting_wm_check,
2178 xcb_change_property(wm->conn,
2179 XCB_PROP_MODE_REPLACE,
2181 wm->atom.net_wm_name,
2182 wm->atom.utf8_string,
2184 strlen(name), name);
2186 xcb_change_property(wm->conn,
2187 XCB_PROP_MODE_REPLACE,
2189 wm->atom.net_supporting_wm_check,
2194 /* Claim the WM_S0 selection even though we don't suport
2195 * the --replace functionality. */
2196 xcb_set_selection_owner(wm->conn,
2199 XCB_TIME_CURRENT_TIME);
2201 xcb_set_selection_owner(wm->conn,
2203 wm->atom.net_wm_cm_s0,
2204 XCB_TIME_CURRENT_TIME);
2208 weston_wm_create(struct weston_xserver *wxs, int fd)
2210 struct weston_wm *wm;
2211 struct wl_event_loop *loop;
2212 xcb_screen_iterator_t s;
2214 xcb_atom_t supported[6];
2216 wm = zalloc(sizeof *wm);
2221 wm->window_hash = hash_table_create();
2222 if (wm->window_hash == NULL) {
2227 /* xcb_connect_to_fd takes ownership of the fd. */
2228 wm->conn = xcb_connect_to_fd(fd, NULL);
2229 if (xcb_connection_has_error(wm->conn)) {
2230 weston_log("xcb_connect_to_fd failed\n");
2232 hash_table_destroy(wm->window_hash);
2237 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2238 wm->screen = s.data;
2240 loop = wl_display_get_event_loop(wxs->wl_display);
2242 wl_event_loop_add_fd(loop, fd,
2244 weston_wm_handle_event, wm);
2245 wl_event_source_check(wm->source);
2247 weston_wm_get_resources(wm);
2248 weston_wm_get_visual_and_colormap(wm);
2251 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2252 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2253 XCB_EVENT_MASK_PROPERTY_CHANGE;
2254 xcb_change_window_attributes(wm->conn, wm->screen->root,
2255 XCB_CW_EVENT_MASK, values);
2257 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2258 XCB_COMPOSITE_REDIRECT_MANUAL);
2260 wm->theme = theme_create();
2262 supported[0] = wm->atom.net_wm_moveresize;
2263 supported[1] = wm->atom.net_wm_state;
2264 supported[2] = wm->atom.net_wm_state_fullscreen;
2265 supported[3] = wm->atom.net_wm_state_maximized_vert;
2266 supported[4] = wm->atom.net_wm_state_maximized_horz;
2267 supported[5] = wm->atom.net_active_window;
2268 xcb_change_property(wm->conn,
2269 XCB_PROP_MODE_REPLACE,
2271 wm->atom.net_supported,
2274 ARRAY_LENGTH(supported), supported);
2276 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2278 weston_wm_selection_init(wm);
2280 weston_wm_dnd_init(wm);
2282 xcb_flush(wm->conn);
2284 wm->create_surface_listener.notify = weston_wm_create_surface;
2285 wl_signal_add(&wxs->compositor->create_surface_signal,
2286 &wm->create_surface_listener);
2287 wm->activate_listener.notify = weston_wm_window_activate;
2288 wl_signal_add(&wxs->compositor->activate_signal,
2289 &wm->activate_listener);
2290 wm->kill_listener.notify = weston_wm_kill_client;
2291 wl_signal_add(&wxs->compositor->kill_signal,
2292 &wm->kill_listener);
2293 wl_list_init(&wm->unpaired_window_list);
2295 weston_wm_create_cursors(wm);
2296 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2298 /* Create wm window and take WM_S0 selection last, which
2299 * signals to Xwayland that we're done with setup. */
2300 weston_wm_create_wm_window(wm);
2302 weston_log("created wm, root %d\n", wm->screen->root);
2308 weston_wm_destroy(struct weston_wm *wm)
2310 /* FIXME: Free windows in hash. */
2311 hash_table_destroy(wm->window_hash);
2312 weston_wm_destroy_cursors(wm);
2313 xcb_disconnect(wm->conn);
2314 wl_event_source_remove(wm->source);
2315 wl_list_remove(&wm->selection_listener.link);
2316 wl_list_remove(&wm->activate_listener.link);
2317 wl_list_remove(&wm->kill_listener.link);
2318 wl_list_remove(&wm->create_surface_listener.link);
2323 static struct weston_wm_window *
2324 get_wm_window(struct weston_surface *surface)
2326 struct wl_listener *listener;
2328 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2330 return container_of(listener, struct weston_wm_window,
2331 surface_destroy_listener);
2337 weston_wm_window_configure(void *data)
2339 struct weston_wm_window *window = data;
2340 struct weston_wm *wm = window->wm;
2342 int x, y, width, height;
2344 weston_wm_window_get_child_position(window, &x, &y);
2347 values[2] = window->width;
2348 values[3] = window->height;
2349 xcb_configure_window(wm->conn,
2351 XCB_CONFIG_WINDOW_X |
2352 XCB_CONFIG_WINDOW_Y |
2353 XCB_CONFIG_WINDOW_WIDTH |
2354 XCB_CONFIG_WINDOW_HEIGHT,
2357 weston_wm_window_get_frame_size(window, &width, &height);
2360 xcb_configure_window(wm->conn,
2362 XCB_CONFIG_WINDOW_WIDTH |
2363 XCB_CONFIG_WINDOW_HEIGHT,
2366 window->configure_source = NULL;
2368 weston_wm_window_schedule_repaint(window);
2372 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
2374 struct weston_wm_window *window = get_wm_window(surface);
2375 struct weston_wm *wm = window->wm;
2376 struct theme *t = window->wm->theme;
2377 int vborder, hborder;
2379 if (window->decorate && !window->fullscreen) {
2380 hborder = 2 * t->width;
2381 vborder = t->titlebar_height + t->width;
2387 if (width > hborder)
2388 window->width = width - hborder;
2392 if (height > vborder)
2393 window->height = height - vborder;
2398 frame_resize_inside(window->frame, window->width, window->height);
2400 if (window->configure_source)
2403 window->configure_source =
2404 wl_event_loop_add_idle(wm->server->loop,
2405 weston_wm_window_configure, window);
2409 send_position(struct weston_surface *surface, int32_t x, int32_t y)
2411 struct weston_wm_window *window = get_wm_window(surface);
2412 struct weston_wm *wm;
2413 uint32_t mask, values[2];
2415 if (!window || !window->wm)
2419 /* We use pos_dirty to tell whether a configure message is in flight.
2420 * This is needed in case we send two configure events in a very
2421 * short time, since window->x/y is set in after a roundtrip, hence
2422 * we cannot just check if the current x and y are different. */
2423 if (window->x != x || window->y != y || window->pos_dirty) {
2424 window->pos_dirty = true;
2427 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2429 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2430 xcb_flush(wm->conn);
2434 static const struct weston_shell_client shell_client = {
2440 legacy_fullscreen(struct weston_wm *wm,
2441 struct weston_wm_window *window,
2442 struct weston_output **output_ret)
2444 struct weston_compositor *compositor = wm->server->compositor;
2445 struct weston_output *output;
2446 uint32_t minmax = PMinSize | PMaxSize;
2449 /* Heuristics for detecting legacy fullscreen windows... */
2451 wl_list_for_each(output, &compositor->output_list, link) {
2452 if (output->x == window->x &&
2453 output->y == window->y &&
2454 output->width == window->width &&
2455 output->height == window->height &&
2456 window->override_redirect) {
2457 *output_ret = output;
2462 if ((window->size_hints.flags & (USSize |PSize)) &&
2463 window->size_hints.width == output->width &&
2464 window->size_hints.height == output->height)
2466 if ((window->size_hints.flags & minmax) == minmax &&
2467 window->size_hints.min_width == output->width &&
2468 window->size_hints.min_height == output->height &&
2469 window->size_hints.max_width == output->width &&
2470 window->size_hints.max_height == output->height)
2473 if (matching_size && !window->decorate &&
2474 (window->size_hints.flags & (USPosition | PPosition)) &&
2475 window->size_hints.x == output->x &&
2476 window->size_hints.y == output->y) {
2477 *output_ret = output;
2486 weston_wm_window_type_inactive(struct weston_wm_window *window)
2488 struct weston_wm *wm = window->wm;
2490 return window->type == wm->atom.net_wm_window_type_tooltip ||
2491 window->type == wm->atom.net_wm_window_type_dropdown ||
2492 window->type == wm->atom.net_wm_window_type_dnd ||
2493 window->type == wm->atom.net_wm_window_type_combo ||
2494 window->type == wm->atom.net_wm_window_type_popup ||
2495 window->type == wm->atom.net_wm_window_type_utility;
2499 xserver_map_shell_surface(struct weston_wm_window *window,
2500 struct weston_surface *surface)
2502 struct weston_wm *wm = window->wm;
2503 struct weston_shell_interface *shell_interface =
2504 &wm->server->compositor->shell_interface;
2505 struct weston_output *output;
2506 struct weston_wm_window *parent;
2509 weston_wm_window_read_properties(window);
2511 /* A weston_wm_window may have many different surfaces assigned
2512 * throughout its life, so we must make sure to remove the listener
2513 * from the old surface signal list. */
2514 if (window->surface)
2515 wl_list_remove(&window->surface_destroy_listener.link);
2517 window->surface = surface;
2518 window->surface_destroy_listener.notify = surface_destroy;
2519 wl_signal_add(&window->surface->destroy_signal,
2520 &window->surface_destroy_listener);
2522 weston_wm_window_schedule_repaint(window);
2524 if (!shell_interface->create_shell_surface)
2527 if (window->surface->committed) {
2528 weston_log("warning, unexpected in %s: "
2529 "surface's configure hook is already set.\n",
2535 shell_interface->create_shell_surface(shell_interface->shell,
2540 shell_interface->set_title(window->shsurf, window->name);
2541 if (window->pid > 0)
2542 shell_interface->set_pid(window->shsurf, window->pid);
2544 if (window->fullscreen) {
2545 window->saved_width = window->width;
2546 window->saved_height = window->height;
2547 shell_interface->set_fullscreen(window->shsurf,
2548 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2551 } else if (legacy_fullscreen(wm, window, &output)) {
2552 window->fullscreen = 1;
2553 shell_interface->set_fullscreen(window->shsurf,
2554 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2556 } else if (window->override_redirect) {
2557 shell_interface->set_xwayland(window->shsurf,
2560 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2561 } else if (window->transient_for && window->transient_for->surface) {
2562 parent = window->transient_for;
2563 if (weston_wm_window_type_inactive(window))
2564 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
2565 shell_interface->set_transient(window->shsurf,
2567 window->x - parent->x,
2568 window->y - parent->y, flags);
2569 } else if (weston_wm_window_is_maximized(window)) {
2570 shell_interface->set_maximized(window->shsurf);
2572 if (weston_wm_window_type_inactive(window)) {
2573 shell_interface->set_xwayland(window->shsurf,
2576 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2578 shell_interface->set_toplevel(window->shsurf);