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
31 #include <sys/socket.h>
37 #include <X11/Xcursor/Xcursor.h>
38 #include <linux/input.h>
42 #include "cairo-util.h"
43 #include "compositor.h"
45 #include "shared/helpers.h"
47 struct wm_size_hints {
50 int32_t width, height; /* should set so old wm's don't mess up */
51 int32_t min_width, min_height;
52 int32_t max_width, max_height;
53 int32_t width_inc, height_inc;
57 } min_aspect, max_aspect;
58 int32_t base_width, base_height;
62 #define USPosition (1L << 0)
63 #define USSize (1L << 1)
64 #define PPosition (1L << 2)
65 #define PSize (1L << 3)
66 #define PMinSize (1L << 4)
67 #define PMaxSize (1L << 5)
68 #define PResizeInc (1L << 6)
69 #define PAspect (1L << 7)
70 #define PBaseSize (1L << 8)
71 #define PWinGravity (1L << 9)
73 struct motif_wm_hints {
81 #define MWM_HINTS_FUNCTIONS (1L << 0)
82 #define MWM_HINTS_DECORATIONS (1L << 1)
83 #define MWM_HINTS_INPUT_MODE (1L << 2)
84 #define MWM_HINTS_STATUS (1L << 3)
86 #define MWM_FUNC_ALL (1L << 0)
87 #define MWM_FUNC_RESIZE (1L << 1)
88 #define MWM_FUNC_MOVE (1L << 2)
89 #define MWM_FUNC_MINIMIZE (1L << 3)
90 #define MWM_FUNC_MAXIMIZE (1L << 4)
91 #define MWM_FUNC_CLOSE (1L << 5)
93 #define MWM_DECOR_ALL (1L << 0)
94 #define MWM_DECOR_BORDER (1L << 1)
95 #define MWM_DECOR_RESIZEH (1L << 2)
96 #define MWM_DECOR_TITLE (1L << 3)
97 #define MWM_DECOR_MENU (1L << 4)
98 #define MWM_DECOR_MINIMIZE (1L << 5)
99 #define MWM_DECOR_MAXIMIZE (1L << 6)
101 #define MWM_DECOR_EVERYTHING \
102 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
103 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
105 #define MWM_INPUT_MODELESS 0
106 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
107 #define MWM_INPUT_SYSTEM_MODAL 2
108 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
109 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
111 #define MWM_TEAROFF_WINDOW (1L<<0)
113 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
114 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
115 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
116 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
117 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
118 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
119 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
120 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
121 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
122 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
123 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
124 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
126 struct weston_wm_window {
127 struct weston_wm *wm;
129 xcb_window_t frame_id;
131 cairo_surface_t *cairo_surface;
133 struct weston_surface *surface;
134 struct shell_surface *shsurf;
135 struct weston_view *view;
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;
149 int saved_width, saved_height;
151 int override_redirect;
157 struct wm_size_hints size_hints;
158 struct motif_wm_hints motif_hints;
162 static struct weston_wm_window *
163 get_wm_window(struct weston_surface *surface);
166 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
169 xserver_map_shell_surface(struct weston_wm_window *window,
170 struct weston_surface *surface);
172 static int __attribute__ ((format (printf, 1, 2)))
173 wm_log(const char *fmt, ...)
180 l = weston_vlog(fmt, argp);
189 static int __attribute__ ((format (printf, 1, 2)))
190 wm_log_continue(const char *fmt, ...)
197 l = weston_vlog_continue(fmt, argp);
206 static bool __attribute__ ((warn_unused_result))
207 wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
208 struct weston_wm_window **window)
210 *window = hash_table_lookup(wm->window_hash, hash);
217 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
219 xcb_get_atom_name_cookie_t cookie;
220 xcb_get_atom_name_reply_t *reply;
221 xcb_generic_error_t *e;
222 static char buffer[64];
224 if (atom == XCB_ATOM_NONE)
227 cookie = xcb_get_atom_name (c, atom);
228 reply = xcb_get_atom_name_reply (c, cookie, &e);
231 snprintf(buffer, sizeof buffer, "%.*s",
232 xcb_get_atom_name_name_length (reply),
233 xcb_get_atom_name_name (reply));
235 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
244 xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
246 xcb_connection_t *c = wm->conn;
247 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
248 xcb_screen_t *screen = s.data;
251 xcb_render_picture_t pic;
253 int stride = img->width * 4;
255 pix = xcb_generate_id(c);
256 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
258 pic = xcb_generate_id(c);
259 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
261 gc = xcb_generate_id(c);
262 xcb_create_gc(c, gc, pix, 0, 0);
264 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
265 img->width, img->height, 0, 0, 0, 32,
266 stride * img->height, (uint8_t *) img->pixels);
269 cursor = xcb_generate_id(c);
270 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
272 xcb_render_free_picture(c, pic);
273 xcb_free_pixmap(c, pix);
279 xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
281 /* TODO: treat animated cursors as well */
282 if (images->nimage != 1)
285 return xcb_cursor_image_load_cursor(wm, images->images[0]);
289 xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
292 XcursorImages *images;
299 v = getenv ("XCURSOR_SIZE");
306 images = XcursorLibraryLoadImages (file, NULL, size);
310 cursor = xcb_cursor_images_load_cursor (wm, images);
311 XcursorImagesDestroy (images);
317 dump_property(struct weston_wm *wm,
318 xcb_atom_t property, xcb_get_property_reply_t *reply)
321 const char *text_value, *name;
322 xcb_atom_t *atom_value;
326 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
328 wm_log_continue("(no reply)\n");
332 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
333 get_atom_name(wm->conn, reply->type),
335 xcb_get_property_value_length(reply),
338 if (reply->type == wm->atom.incr) {
339 incr_value = xcb_get_property_value(reply);
340 wm_log_continue("%d\n", *incr_value);
341 } else if (reply->type == wm->atom.utf8_string ||
342 reply->type == wm->atom.string) {
343 text_value = xcb_get_property_value(reply);
344 if (reply->value_len > 40)
347 len = reply->value_len;
348 wm_log_continue("\"%.*s\"\n", len, text_value);
349 } else if (reply->type == XCB_ATOM_ATOM) {
350 atom_value = xcb_get_property_value(reply);
351 for (i = 0; i < reply->value_len; i++) {
352 name = get_atom_name(wm->conn, atom_value[i]);
353 if (width + strlen(name) + 2 > 78) {
354 wm_log_continue("\n ");
357 width += wm_log_continue(", ");
360 width += wm_log_continue("%s", name);
362 wm_log_continue("\n");
364 wm_log_continue("huh?\n");
369 read_and_dump_property(struct weston_wm *wm,
370 xcb_window_t window, xcb_atom_t property)
372 xcb_get_property_reply_t *reply;
373 xcb_get_property_cookie_t cookie;
375 cookie = xcb_get_property(wm->conn, 0, window,
376 property, XCB_ATOM_ANY, 0, 2048);
377 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
379 dump_property(wm, property, reply);
384 /* We reuse some predefined, but otherwise useles atoms */
385 #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
386 #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
387 #define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
388 #define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
391 weston_wm_window_read_properties(struct weston_wm_window *window)
393 struct weston_wm *wm = window->wm;
394 struct weston_shell_interface *shell_interface =
395 &wm->server->compositor->shell_interface;
397 #define F(field) offsetof(struct weston_wm_window, field)
403 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
404 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
405 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
406 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
407 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
408 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
409 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
410 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
411 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
412 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
413 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
417 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
418 xcb_get_property_reply_t *reply;
425 if (!window->properties_dirty)
427 window->properties_dirty = 0;
429 for (i = 0; i < ARRAY_LENGTH(props); i++)
430 cookie[i] = xcb_get_property(wm->conn,
434 XCB_ATOM_ANY, 0, 2048);
436 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
437 window->size_hints.flags = 0;
438 window->motif_hints.flags = 0;
439 window->delete_window = 0;
441 for (i = 0; i < ARRAY_LENGTH(props); i++) {
442 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
444 /* Bad window, typically */
446 if (reply->type == XCB_ATOM_NONE) {
447 /* No such property */
452 p = ((char *) window + props[i].offset);
454 switch (props[i].type) {
455 case XCB_ATOM_WM_CLIENT_MACHINE:
456 case XCB_ATOM_STRING:
457 /* FIXME: We're using this for both string and
463 strndup(xcb_get_property_value(reply),
464 xcb_get_property_value_length(reply));
466 case XCB_ATOM_WINDOW:
467 xid = xcb_get_property_value(reply);
468 if (!wm_lookup_window(wm, *xid, p))
469 weston_log("XCB_ATOM_WINDOW contains window"
470 " id not found in hash table.\n");
472 case XCB_ATOM_CARDINAL:
474 atom = xcb_get_property_value(reply);
475 *(xcb_atom_t *) p = *atom;
477 case TYPE_WM_PROTOCOLS:
478 atom = xcb_get_property_value(reply);
479 for (i = 0; i < reply->value_len; i++)
480 if (atom[i] == wm->atom.wm_delete_window) {
481 window->delete_window = 1;
485 case TYPE_WM_NORMAL_HINTS:
486 memcpy(&window->size_hints,
487 xcb_get_property_value(reply),
488 sizeof window->size_hints);
490 case TYPE_NET_WM_STATE:
491 window->fullscreen = 0;
492 atom = xcb_get_property_value(reply);
493 for (i = 0; i < reply->value_len; i++) {
494 if (atom[i] == wm->atom.net_wm_state_fullscreen)
495 window->fullscreen = 1;
496 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
497 window->maximized_vert = 1;
498 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
499 window->maximized_horz = 1;
502 case TYPE_MOTIF_WM_HINTS:
503 memcpy(&window->motif_hints,
504 xcb_get_property_value(reply),
505 sizeof window->motif_hints);
506 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
507 if (window->motif_hints.decorations & MWM_DECOR_ALL)
508 /* MWM_DECOR_ALL means all except the other values listed. */
510 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
513 window->motif_hints.decorations;
522 if (window->pid > 0) {
523 gethostname(name, sizeof(name));
524 for (i = 0; i < sizeof(name); i++) {
528 if (i == sizeof(name))
529 name[0] = '\0'; /* ignore stupid hostnames */
531 /* this is only one heuristic to guess the PID of a client is
532 * valid, assuming it's compliant with icccm and ewmh.
533 * Non-compliants and remote applications of course fail. */
534 if (!window->machine || strcmp(window->machine, name))
538 if (window->shsurf && window->name)
539 shell_interface->set_title(window->shsurf, window->name);
540 if (window->frame && window->name)
541 frame_set_title(window->frame, window->name);
542 if (window->shsurf && window->pid > 0)
543 shell_interface->set_pid(window->shsurf, window->pid);
547 weston_wm_window_get_frame_size(struct weston_wm_window *window,
548 int *width, int *height)
550 struct theme *t = window->wm->theme;
552 if (window->fullscreen) {
553 *width = window->width;
554 *height = window->height;
555 } else if (window->decorate && window->frame) {
556 *width = frame_width(window->frame);
557 *height = frame_height(window->frame);
559 *width = window->width + t->margin * 2;
560 *height = window->height + t->margin * 2;
565 weston_wm_window_get_child_position(struct weston_wm_window *window,
568 struct theme *t = window->wm->theme;
570 if (window->fullscreen) {
573 } else if (window->decorate && window->frame) {
574 frame_interior(window->frame, x, y, NULL, NULL);
582 weston_wm_window_send_configure_notify(struct weston_wm_window *window)
584 xcb_configure_notify_event_t configure_notify;
585 struct weston_wm *wm = window->wm;
588 weston_wm_window_get_child_position(window, &x, &y);
589 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
590 configure_notify.pad0 = 0;
591 configure_notify.event = window->id;
592 configure_notify.window = window->id;
593 configure_notify.above_sibling = XCB_WINDOW_NONE;
594 configure_notify.x = x;
595 configure_notify.y = y;
596 configure_notify.width = window->width;
597 configure_notify.height = window->height;
598 configure_notify.border_width = 0;
599 configure_notify.override_redirect = 0;
600 configure_notify.pad1 = 0;
602 xcb_send_event(wm->conn, 0, window->id,
603 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
604 (char *) &configure_notify);
608 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
610 xcb_configure_request_event_t *configure_request =
611 (xcb_configure_request_event_t *) event;
612 struct weston_wm_window *window;
613 uint32_t mask, values[16];
614 int x, y, width, height, i = 0;
616 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
617 configure_request->window,
618 configure_request->x, configure_request->y,
619 configure_request->width, configure_request->height);
621 if (!wm_lookup_window(wm, configure_request->window, &window))
624 if (window->fullscreen) {
625 weston_wm_window_send_configure_notify(window);
629 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
630 window->width = configure_request->width;
631 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
632 window->height = configure_request->height;
635 frame_resize_inside(window->frame, window->width, window->height);
637 weston_wm_window_get_child_position(window, &x, &y);
640 values[i++] = window->width;
641 values[i++] = window->height;
643 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
644 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
645 XCB_CONFIG_WINDOW_BORDER_WIDTH;
646 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
647 values[i++] = configure_request->sibling;
648 mask |= XCB_CONFIG_WINDOW_SIBLING;
650 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
651 values[i++] = configure_request->stack_mode;
652 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
655 xcb_configure_window(wm->conn, window->id, mask, values);
657 weston_wm_window_get_frame_size(window, &width, &height);
660 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
661 xcb_configure_window(wm->conn, window->frame_id, mask, values);
663 weston_wm_window_schedule_repaint(window);
667 our_resource(struct weston_wm *wm, uint32_t id)
669 const xcb_setup_t *setup;
671 setup = xcb_get_setup(wm->conn);
673 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
677 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
679 xcb_configure_notify_event_t *configure_notify =
680 (xcb_configure_notify_event_t *) event;
681 struct weston_wm_window *window;
683 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
684 configure_notify->window,
685 configure_notify->x, configure_notify->y,
686 configure_notify->width, configure_notify->height);
688 if (!wm_lookup_window(wm, configure_notify->window, &window))
691 window->x = configure_notify->x;
692 window->y = configure_notify->y;
693 if (window->override_redirect) {
694 window->width = configure_notify->width;
695 window->height = configure_notify->height;
697 frame_resize_inside(window->frame,
698 window->width, window->height);
703 weston_wm_kill_client(struct wl_listener *listener, void *data)
705 struct weston_surface *surface = data;
706 struct weston_wm_window *window = get_wm_window(surface);
711 kill(window->pid, SIGKILL);
715 weston_wm_create_surface(struct wl_listener *listener, void *data)
717 struct weston_surface *surface = data;
718 struct weston_wm *wm =
719 container_of(listener,
720 struct weston_wm, create_surface_listener);
721 struct weston_wm_window *window;
723 if (wl_resource_get_client(surface->resource) != wm->server->client)
726 wl_list_for_each(window, &wm->unpaired_window_list, link)
727 if (window->surface_id ==
728 wl_resource_get_id(surface->resource)) {
729 xserver_map_shell_surface(window, surface);
730 window->surface_id = 0;
731 wl_list_remove(&window->link);
737 weston_wm_send_focus_window(struct weston_wm *wm,
738 struct weston_wm_window *window)
740 xcb_client_message_event_t client_message;
745 if (window->override_redirect)
748 client_message.response_type = XCB_CLIENT_MESSAGE;
749 client_message.format = 32;
750 client_message.window = window->id;
751 client_message.type = wm->atom.wm_protocols;
752 client_message.data.data32[0] = wm->atom.wm_take_focus;
753 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
755 xcb_send_event(wm->conn, 0, window->id,
756 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
757 (char *) &client_message);
759 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
760 window->id, XCB_TIME_CURRENT_TIME);
762 values[0] = XCB_STACK_MODE_ABOVE;
763 xcb_configure_window (wm->conn, window->frame_id,
764 XCB_CONFIG_WINDOW_STACK_MODE, values);
766 xcb_set_input_focus (wm->conn,
767 XCB_INPUT_FOCUS_POINTER_ROOT,
769 XCB_TIME_CURRENT_TIME);
774 weston_wm_window_activate(struct wl_listener *listener, void *data)
776 struct weston_surface *surface = data;
777 struct weston_wm_window *window = NULL;
778 struct weston_wm *wm =
779 container_of(listener, struct weston_wm, activate_listener);
782 window = get_wm_window(surface);
785 weston_wm_send_focus_window(wm, window);
787 if (wm->focus_window) {
788 if (wm->focus_window->frame)
789 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
790 weston_wm_window_schedule_repaint(wm->focus_window);
792 wm->focus_window = window;
793 if (wm->focus_window) {
794 if (wm->focus_window->frame)
795 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
796 weston_wm_window_schedule_repaint(wm->focus_window);
801 weston_wm_window_transform(struct wl_listener *listener, void *data)
803 struct weston_surface *surface = data;
804 struct weston_wm_window *window = get_wm_window(surface);
805 struct weston_wm *wm =
806 container_of(listener, struct weston_wm, transform_listener);
807 uint32_t mask, values[2];
812 if (!window->view || !weston_view_is_mapped(window->view))
815 if (window->x != window->view->geometry.x ||
816 window->y != window->view->geometry.y) {
817 values[0] = window->view->geometry.x;
818 values[1] = window->view->geometry.y;
819 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
821 xcb_configure_window(wm->conn, window->frame_id, mask, values);
826 #define ICCCM_WITHDRAWN_STATE 0
827 #define ICCCM_NORMAL_STATE 1
828 #define ICCCM_ICONIC_STATE 3
831 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
833 struct weston_wm *wm = window->wm;
834 uint32_t property[2];
837 property[1] = XCB_WINDOW_NONE;
839 xcb_change_property(wm->conn,
840 XCB_PROP_MODE_REPLACE,
849 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
851 struct weston_wm *wm = window->wm;
852 uint32_t property[3];
856 if (window->fullscreen)
857 property[i++] = wm->atom.net_wm_state_fullscreen;
858 if (window->maximized_vert)
859 property[i++] = wm->atom.net_wm_state_maximized_vert;
860 if (window->maximized_horz)
861 property[i++] = wm->atom.net_wm_state_maximized_horz;
863 xcb_change_property(wm->conn,
864 XCB_PROP_MODE_REPLACE,
866 wm->atom.net_wm_state,
873 weston_wm_window_create_frame(struct weston_wm_window *window)
875 struct weston_wm *wm = window->wm;
877 int x, y, width, height;
878 int buttons = FRAME_BUTTON_CLOSE;
880 if (window->decorate & MWM_DECOR_MAXIMIZE)
881 buttons |= FRAME_BUTTON_MAXIMIZE;
883 window->frame = frame_create(window->wm->theme,
884 window->width, window->height,
885 buttons, window->name);
886 frame_resize_inside(window->frame, window->width, window->height);
888 weston_wm_window_get_frame_size(window, &width, &height);
889 weston_wm_window_get_child_position(window, &x, &y);
891 values[0] = wm->screen->black_pixel;
893 XCB_EVENT_MASK_KEY_PRESS |
894 XCB_EVENT_MASK_KEY_RELEASE |
895 XCB_EVENT_MASK_BUTTON_PRESS |
896 XCB_EVENT_MASK_BUTTON_RELEASE |
897 XCB_EVENT_MASK_POINTER_MOTION |
898 XCB_EVENT_MASK_ENTER_WINDOW |
899 XCB_EVENT_MASK_LEAVE_WINDOW |
900 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
901 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
902 values[2] = wm->colormap;
904 window->frame_id = xcb_generate_id(wm->conn);
905 xcb_create_window(wm->conn,
912 XCB_WINDOW_CLASS_INPUT_OUTPUT,
914 XCB_CW_BORDER_PIXEL |
916 XCB_CW_COLORMAP, values);
918 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
921 xcb_configure_window(wm->conn, window->id,
922 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
924 window->cairo_surface =
925 cairo_xcb_surface_create_with_xrender_format(wm->conn,
931 hash_table_insert(wm->window_hash, window->frame_id, window);
935 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
936 * Passing a <0 desktop value deletes the property.
939 weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
943 xcb_change_property(window->wm->conn,
944 XCB_PROP_MODE_REPLACE,
946 window->wm->atom.net_wm_desktop,
951 xcb_delete_property(window->wm->conn,
953 window->wm->atom.net_wm_desktop);
958 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
960 xcb_map_request_event_t *map_request =
961 (xcb_map_request_event_t *) event;
962 struct weston_wm_window *window;
964 if (our_resource(wm, map_request->window)) {
965 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
966 map_request->window);
970 if (!wm_lookup_window(wm, map_request->window, &window))
973 weston_wm_window_read_properties(window);
975 if (window->frame_id == XCB_WINDOW_NONE)
976 weston_wm_window_create_frame(window);
978 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
979 window->id, window, window->frame_id);
981 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
982 weston_wm_window_set_net_wm_state(window);
983 weston_wm_window_set_virtual_desktop(window, 0);
985 xcb_map_window(wm->conn, map_request->window);
986 xcb_map_window(wm->conn, window->frame_id);
990 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
992 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
994 if (our_resource(wm, map_notify->window)) {
995 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1000 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
1004 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1006 xcb_unmap_notify_event_t *unmap_notify =
1007 (xcb_unmap_notify_event_t *) event;
1008 struct weston_wm_window *window;
1010 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1011 unmap_notify->window,
1012 unmap_notify->event,
1013 our_resource(wm, unmap_notify->window) ? ", ours" : "");
1015 if (our_resource(wm, unmap_notify->window))
1018 if (unmap_notify->response_type & SEND_EVENT_MASK)
1019 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1020 * as it may come in after we've destroyed the window. */
1023 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1026 if (window->surface_id) {
1027 /* Make sure we're not on the unpaired surface list or we
1028 * could be assigned a surface during surface creation that
1029 * was mapped before this unmap request.
1031 wl_list_remove(&window->link);
1032 window->surface_id = 0;
1034 if (wm->focus_window == window)
1035 wm->focus_window = NULL;
1036 if (window->surface)
1037 wl_list_remove(&window->surface_destroy_listener.link);
1038 window->surface = NULL;
1039 window->shsurf = NULL;
1040 window->view = NULL;
1042 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1043 weston_wm_window_set_virtual_desktop(window, -1);
1045 xcb_unmap_window(wm->conn, window->frame_id);
1049 weston_wm_window_draw_decoration(void *data)
1051 struct weston_wm_window *window = data;
1052 struct weston_wm *wm = window->wm;
1053 struct theme *t = wm->theme;
1055 int x, y, width, height;
1056 int32_t input_x, input_y, input_w, input_h;
1057 struct weston_shell_interface *shell_interface =
1058 &wm->server->compositor->shell_interface;
1061 weston_wm_window_read_properties(window);
1063 window->repaint_source = NULL;
1065 weston_wm_window_get_frame_size(window, &width, &height);
1066 weston_wm_window_get_child_position(window, &x, &y);
1068 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1069 cr = cairo_create(window->cairo_surface);
1071 if (window->fullscreen) {
1073 } else if (window->decorate) {
1074 if (wm->focus_window == window)
1075 flags |= THEME_FRAME_ACTIVE;
1077 frame_repaint(window->frame, cr);
1079 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1080 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1083 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
1088 if (window->surface) {
1089 pixman_region32_fini(&window->surface->pending.opaque);
1090 if(window->has_alpha) {
1091 pixman_region32_init(&window->surface->pending.opaque);
1093 /* We leave an extra pixel around the X window area to
1094 * make sure we don't sample from the undefined alpha
1095 * channel when filtering. */
1096 pixman_region32_init_rect(&window->surface->pending.opaque,
1099 window->height + 2);
1102 weston_view_geometry_dirty(window->view);
1104 pixman_region32_fini(&window->surface->pending.input);
1106 if (window->decorate && !window->fullscreen) {
1107 frame_input_rect(window->frame, &input_x, &input_y,
1108 &input_w, &input_h);
1116 pixman_region32_init_rect(&window->surface->pending.input,
1117 input_x, input_y, input_w, input_h);
1119 shell_interface->set_window_geometry(window->shsurf,
1120 input_x, input_y, input_w, input_h);
1125 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1127 struct weston_wm *wm = window->wm;
1130 if (window->frame_id == XCB_WINDOW_NONE) {
1131 if (window->surface != NULL) {
1132 weston_wm_window_get_frame_size(window, &width, &height);
1133 pixman_region32_fini(&window->surface->pending.opaque);
1134 if(window->has_alpha) {
1135 pixman_region32_init(&window->surface->pending.opaque);
1137 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1141 weston_view_geometry_dirty(window->view);
1146 if (window->repaint_source)
1149 window->repaint_source =
1150 wl_event_loop_add_idle(wm->server->loop,
1151 weston_wm_window_draw_decoration,
1156 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1158 xcb_property_notify_event_t *property_notify =
1159 (xcb_property_notify_event_t *) event;
1160 struct weston_wm_window *window;
1162 if (!wm_lookup_window(wm, property_notify->window, &window))
1165 window->properties_dirty = 1;
1167 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
1168 if (property_notify->state == XCB_PROPERTY_DELETE)
1169 wm_log("deleted\n");
1171 read_and_dump_property(wm, property_notify->window,
1172 property_notify->atom);
1174 if (property_notify->atom == wm->atom.net_wm_name ||
1175 property_notify->atom == XCB_ATOM_WM_NAME)
1176 weston_wm_window_schedule_repaint(window);
1180 weston_wm_window_create(struct weston_wm *wm,
1181 xcb_window_t id, int width, int height, int x, int y, int override)
1183 struct weston_wm_window *window;
1185 xcb_get_geometry_cookie_t geometry_cookie;
1186 xcb_get_geometry_reply_t *geometry_reply;
1188 window = zalloc(sizeof *window);
1189 if (window == NULL) {
1190 wm_log("failed to allocate window\n");
1194 geometry_cookie = xcb_get_geometry(wm->conn, id);
1196 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1197 XCB_EVENT_MASK_FOCUS_CHANGE;
1198 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1202 window->properties_dirty = 1;
1203 window->override_redirect = override;
1204 window->width = width;
1205 window->height = height;
1209 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1210 /* technically we should use XRender and check the visual format's
1211 alpha_mask, but checking depth is simpler and works in all known cases */
1212 if(geometry_reply != NULL)
1213 window->has_alpha = geometry_reply->depth == 32;
1214 free(geometry_reply);
1216 hash_table_insert(wm->window_hash, id, window);
1220 weston_wm_window_destroy(struct weston_wm_window *window)
1222 struct weston_wm *wm = window->wm;
1224 if (window->repaint_source)
1225 wl_event_source_remove(window->repaint_source);
1226 if (window->cairo_surface)
1227 cairo_surface_destroy(window->cairo_surface);
1229 if (window->frame_id) {
1230 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1231 xcb_destroy_window(wm->conn, window->frame_id);
1232 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1233 weston_wm_window_set_virtual_desktop(window, -1);
1234 hash_table_remove(wm->window_hash, window->frame_id);
1235 window->frame_id = XCB_WINDOW_NONE;
1238 if (window->surface_id)
1239 wl_list_remove(&window->link);
1241 if (window->surface)
1242 wl_list_remove(&window->surface_destroy_listener.link);
1244 hash_table_remove(window->wm->window_hash, window->id);
1249 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1251 xcb_create_notify_event_t *create_notify =
1252 (xcb_create_notify_event_t *) event;
1254 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1255 create_notify->window,
1256 create_notify->width, create_notify->height,
1257 create_notify->override_redirect ? ", override" : "",
1258 our_resource(wm, create_notify->window) ? ", ours" : "");
1260 if (our_resource(wm, create_notify->window))
1263 weston_wm_window_create(wm, create_notify->window,
1264 create_notify->width, create_notify->height,
1265 create_notify->x, create_notify->y,
1266 create_notify->override_redirect);
1270 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1272 xcb_destroy_notify_event_t *destroy_notify =
1273 (xcb_destroy_notify_event_t *) event;
1274 struct weston_wm_window *window;
1276 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1277 destroy_notify->window,
1278 destroy_notify->event,
1279 our_resource(wm, destroy_notify->window) ? ", ours" : "");
1281 if (our_resource(wm, destroy_notify->window))
1284 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1287 weston_wm_window_destroy(window);
1291 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1293 xcb_reparent_notify_event_t *reparent_notify =
1294 (xcb_reparent_notify_event_t *) event;
1295 struct weston_wm_window *window;
1297 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1298 reparent_notify->window,
1299 reparent_notify->parent,
1300 reparent_notify->event);
1302 if (reparent_notify->parent == wm->screen->root) {
1303 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1304 reparent_notify->x, reparent_notify->y,
1305 reparent_notify->override_redirect);
1306 } else if (!our_resource(wm, reparent_notify->parent)) {
1307 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1309 weston_wm_window_destroy(window);
1313 struct weston_seat *
1314 weston_wm_pick_seat(struct weston_wm *wm)
1316 return container_of(wm->server->compositor->seat_list.next,
1317 struct weston_seat, link);
1320 static struct weston_seat *
1321 weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1323 struct weston_wm *wm = window->wm;
1324 struct weston_seat *seat, *s;
1327 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
1328 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1329 struct weston_pointer *old_pointer =
1330 weston_seat_get_pointer(seat);
1332 if (pointer && pointer->focus &&
1333 pointer->focus->surface == window->surface &&
1334 pointer->button_count > 0 &&
1336 pointer->grab_serial -
1337 old_pointer->grab_serial < (1 << 30)))
1345 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1346 xcb_client_message_event_t *client_message)
1348 static const int map[] = {
1349 THEME_LOCATION_RESIZING_TOP_LEFT,
1350 THEME_LOCATION_RESIZING_TOP,
1351 THEME_LOCATION_RESIZING_TOP_RIGHT,
1352 THEME_LOCATION_RESIZING_RIGHT,
1353 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1354 THEME_LOCATION_RESIZING_BOTTOM,
1355 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1356 THEME_LOCATION_RESIZING_LEFT
1359 struct weston_wm *wm = window->wm;
1360 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
1361 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1363 struct weston_shell_interface *shell_interface =
1364 &wm->server->compositor->shell_interface;
1366 if (!pointer || pointer->button_count != 1
1368 || pointer->focus->surface != window->surface)
1371 detail = client_message->data.data32[2];
1373 case _NET_WM_MOVERESIZE_MOVE:
1374 shell_interface->move(window->shsurf, pointer);
1376 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1377 case _NET_WM_MOVERESIZE_SIZE_TOP:
1378 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1379 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1380 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1381 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1382 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1383 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1384 shell_interface->resize(window->shsurf, pointer, map[detail]);
1386 case _NET_WM_MOVERESIZE_CANCEL:
1391 #define _NET_WM_STATE_REMOVE 0
1392 #define _NET_WM_STATE_ADD 1
1393 #define _NET_WM_STATE_TOGGLE 2
1396 update_state(int action, int *state)
1398 int new_state, changed;
1401 case _NET_WM_STATE_REMOVE:
1404 case _NET_WM_STATE_ADD:
1407 case _NET_WM_STATE_TOGGLE:
1408 new_state = !*state;
1414 changed = (*state != new_state);
1421 weston_wm_window_configure(void *data);
1424 weston_wm_window_set_toplevel(struct weston_wm_window *window)
1426 struct weston_shell_interface *shell_interface =
1427 &window->wm->server->compositor->shell_interface;
1429 shell_interface->set_toplevel(window->shsurf);
1430 window->width = window->saved_width;
1431 window->height = window->saved_height;
1433 frame_resize_inside(window->frame,
1436 weston_wm_window_configure(window);
1440 weston_wm_window_is_maximized(struct weston_wm_window *window)
1442 return window->maximized_horz && window->maximized_vert;
1446 weston_wm_window_handle_state(struct weston_wm_window *window,
1447 xcb_client_message_event_t *client_message)
1449 struct weston_wm *wm = window->wm;
1450 struct weston_shell_interface *shell_interface =
1451 &wm->server->compositor->shell_interface;
1452 uint32_t action, property;
1453 int maximized = weston_wm_window_is_maximized(window);
1455 action = client_message->data.data32[0];
1456 property = client_message->data.data32[1];
1458 if (property == wm->atom.net_wm_state_fullscreen &&
1459 update_state(action, &window->fullscreen)) {
1460 weston_wm_window_set_net_wm_state(window);
1461 if (window->fullscreen) {
1462 window->saved_width = window->width;
1463 window->saved_height = window->height;
1466 shell_interface->set_fullscreen(window->shsurf,
1467 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1471 weston_wm_window_set_toplevel(window);
1474 if (property == wm->atom.net_wm_state_maximized_vert &&
1475 update_state(action, &window->maximized_vert))
1476 weston_wm_window_set_net_wm_state(window);
1477 if (property == wm->atom.net_wm_state_maximized_horz &&
1478 update_state(action, &window->maximized_horz))
1479 weston_wm_window_set_net_wm_state(window);
1481 if (maximized != weston_wm_window_is_maximized(window)) {
1482 if (weston_wm_window_is_maximized(window)) {
1483 window->saved_width = window->width;
1484 window->saved_height = window->height;
1487 shell_interface->set_maximized(window->shsurf);
1488 } else if (window->shsurf) {
1489 weston_wm_window_set_toplevel(window);
1496 surface_destroy(struct wl_listener *listener, void *data)
1498 struct weston_wm_window *window =
1499 container_of(listener,
1500 struct weston_wm_window, surface_destroy_listener);
1502 wm_log("surface for xid %d destroyed\n", window->id);
1504 /* This should have been freed by the shell.
1505 * Don't try to use it later. */
1506 window->shsurf = NULL;
1507 window->surface = NULL;
1508 window->view = NULL;
1512 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1513 xcb_client_message_event_t *client_message)
1515 struct weston_wm *wm = window->wm;
1516 struct wl_resource *resource;
1518 if (window->surface_id != 0) {
1519 wm_log("already have surface id for window %d\n", window->id);
1523 /* Xwayland will send the wayland requests to create the
1524 * wl_surface before sending this client message. Even so, we
1525 * can end up handling the X event before the wayland requests
1526 * and thus when we try to look up the surface ID, the surface
1527 * hasn't been created yet. In that case put the window on
1528 * the unpaired window list and continue when the surface gets
1530 uint32_t id = client_message->data.data32[0];
1531 resource = wl_client_get_object(wm->server->client, id);
1533 window->surface_id = 0;
1534 xserver_map_shell_surface(window,
1535 wl_resource_get_user_data(resource));
1538 window->surface_id = id;
1539 wl_list_insert(&wm->unpaired_window_list, &window->link);
1544 weston_wm_handle_client_message(struct weston_wm *wm,
1545 xcb_generic_event_t *event)
1547 xcb_client_message_event_t *client_message =
1548 (xcb_client_message_event_t *) event;
1549 struct weston_wm_window *window;
1551 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1552 get_atom_name(wm->conn, client_message->type),
1553 client_message->data.data32[0],
1554 client_message->data.data32[1],
1555 client_message->data.data32[2],
1556 client_message->data.data32[3],
1557 client_message->data.data32[4],
1558 client_message->window);
1560 /* The window may get created and destroyed before we actually
1561 * handle the message. If it doesn't exist, bail.
1563 if (!wm_lookup_window(wm, client_message->window, &window))
1566 if (client_message->type == wm->atom.net_wm_moveresize)
1567 weston_wm_window_handle_moveresize(window, client_message);
1568 else if (client_message->type == wm->atom.net_wm_state)
1569 weston_wm_window_handle_state(window, client_message);
1570 else if (client_message->type == wm->atom.wl_surface_id)
1571 weston_wm_window_handle_surface_id(window, client_message);
1579 XWM_CURSOR_TOP_LEFT,
1580 XWM_CURSOR_TOP_RIGHT,
1581 XWM_CURSOR_BOTTOM_LEFT,
1582 XWM_CURSOR_BOTTOM_RIGHT,
1583 XWM_CURSOR_LEFT_PTR,
1587 * The following correspondences between file names and cursors was copied
1588 * from: https://bugs.kde.org/attachment.cgi?id=67313
1591 static const char *bottom_left_corners[] = {
1592 "bottom_left_corner",
1597 static const char *bottom_right_corners[] = {
1598 "bottom_right_corner",
1603 static const char *bottom_sides[] = {
1609 static const char *left_ptrs[] = {
1616 static const char *left_sides[] = {
1622 static const char *right_sides[] = {
1628 static const char *top_left_corners[] = {
1634 static const char *top_right_corners[] = {
1640 static const char *top_sides[] = {
1646 struct cursor_alternatives {
1651 static const struct cursor_alternatives cursors[] = {
1652 {top_sides, ARRAY_LENGTH(top_sides)},
1653 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1654 {left_sides, ARRAY_LENGTH(left_sides)},
1655 {right_sides, ARRAY_LENGTH(right_sides)},
1656 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1657 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1658 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1659 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1660 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1664 weston_wm_create_cursors(struct weston_wm *wm)
1667 int i, count = ARRAY_LENGTH(cursors);
1670 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1671 for (i = 0; i < count; i++) {
1672 for (j = 0; j < cursors[i].count; j++) {
1673 name = cursors[i].names[j];
1675 xcb_cursor_library_load_cursor(wm, name);
1676 if (wm->cursors[i] != (xcb_cursor_t)-1)
1681 wm->last_cursor = -1;
1685 weston_wm_destroy_cursors(struct weston_wm *wm)
1689 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1690 xcb_free_cursor(wm->conn, wm->cursors[i]);
1696 get_cursor_for_location(enum theme_location location)
1698 // int location = theme_get_location(t, x, y, width, height, 0);
1701 case THEME_LOCATION_RESIZING_TOP:
1702 return XWM_CURSOR_TOP;
1703 case THEME_LOCATION_RESIZING_BOTTOM:
1704 return XWM_CURSOR_BOTTOM;
1705 case THEME_LOCATION_RESIZING_LEFT:
1706 return XWM_CURSOR_LEFT;
1707 case THEME_LOCATION_RESIZING_RIGHT:
1708 return XWM_CURSOR_RIGHT;
1709 case THEME_LOCATION_RESIZING_TOP_LEFT:
1710 return XWM_CURSOR_TOP_LEFT;
1711 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1712 return XWM_CURSOR_TOP_RIGHT;
1713 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1714 return XWM_CURSOR_BOTTOM_LEFT;
1715 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1716 return XWM_CURSOR_BOTTOM_RIGHT;
1717 case THEME_LOCATION_EXTERIOR:
1718 case THEME_LOCATION_TITLEBAR:
1720 return XWM_CURSOR_LEFT_PTR;
1725 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1728 uint32_t cursor_value_list;
1730 if (wm->last_cursor == cursor)
1733 wm->last_cursor = cursor;
1735 cursor_value_list = wm->cursors[cursor];
1736 xcb_change_window_attributes (wm->conn, window_id,
1737 XCB_CW_CURSOR, &cursor_value_list);
1738 xcb_flush(wm->conn);
1742 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1744 xcb_client_message_event_t client_message;
1746 if (window->delete_window) {
1747 client_message.response_type = XCB_CLIENT_MESSAGE;
1748 client_message.format = 32;
1749 client_message.window = window->id;
1750 client_message.type = window->wm->atom.wm_protocols;
1751 client_message.data.data32[0] =
1752 window->wm->atom.wm_delete_window;
1753 client_message.data.data32[1] = time;
1755 xcb_send_event(window->wm->conn, 0, window->id,
1756 XCB_EVENT_MASK_NO_EVENT,
1757 (char *) &client_message);
1759 xcb_kill_client(window->wm->conn, window->id);
1764 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1766 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1767 struct weston_shell_interface *shell_interface =
1768 &wm->server->compositor->shell_interface;
1769 struct weston_seat *seat;
1770 struct weston_pointer *pointer;
1771 struct weston_wm_window *window;
1772 enum theme_location location;
1773 enum frame_button_state button_state;
1776 wm_log("XCB_BUTTON_%s (detail %d)\n",
1777 button->response_type == XCB_BUTTON_PRESS ?
1778 "PRESS" : "RELEASE", button->detail);
1780 if (!wm_lookup_window(wm, button->event, &window) ||
1784 if (button->detail != 1 && button->detail != 2)
1787 seat = weston_wm_pick_seat_for_window(window);
1788 pointer = weston_seat_get_pointer(seat);
1790 button_state = button->response_type == XCB_BUTTON_PRESS ?
1791 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1792 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1794 /* Make sure we're looking at the right location. The frame
1795 * could have received a motion event from a pointer from a
1796 * different wl_seat, but under X it looks like our core
1797 * pointer moved. Move the frame pointer to the button press
1798 * location before deciding what to do. */
1799 location = frame_pointer_motion(window->frame, NULL,
1800 button->event_x, button->event_y);
1801 location = frame_pointer_button(window->frame, NULL,
1802 button_id, button_state);
1803 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1804 weston_wm_window_schedule_repaint(window);
1806 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1808 shell_interface->move(window->shsurf, pointer);
1809 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1812 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1814 shell_interface->resize(window->shsurf, pointer, location);
1815 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1818 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1819 weston_wm_window_close(window, button->time);
1820 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
1823 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1824 window->maximized_horz = !window->maximized_horz;
1825 window->maximized_vert = !window->maximized_vert;
1826 if (weston_wm_window_is_maximized(window)) {
1827 window->saved_width = window->width;
1828 window->saved_height = window->height;
1829 shell_interface->set_maximized(window->shsurf);
1831 weston_wm_window_set_toplevel(window);
1833 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1838 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1840 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1841 struct weston_wm_window *window;
1842 enum theme_location location;
1845 if (!wm_lookup_window(wm, motion->event, &window) ||
1849 location = frame_pointer_motion(window->frame, NULL,
1850 motion->event_x, motion->event_y);
1851 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1852 weston_wm_window_schedule_repaint(window);
1854 cursor = get_cursor_for_location(location);
1855 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1859 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1861 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1862 struct weston_wm_window *window;
1863 enum theme_location location;
1866 if (!wm_lookup_window(wm, enter->event, &window) ||
1870 location = frame_pointer_enter(window->frame, NULL,
1871 enter->event_x, enter->event_y);
1872 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1873 weston_wm_window_schedule_repaint(window);
1875 cursor = get_cursor_for_location(location);
1876 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1880 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1882 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1883 struct weston_wm_window *window;
1885 if (!wm_lookup_window(wm, leave->event, &window) ||
1889 frame_pointer_leave(window->frame, NULL);
1890 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1891 weston_wm_window_schedule_repaint(window);
1893 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
1897 weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1899 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1900 /* Do not let X clients change the focus behind the compositor's
1901 * back. Reset the focus to the old one if it changed. */
1902 if (!wm->focus_window || focus->event != wm->focus_window->id)
1903 weston_wm_send_focus_window(wm, wm->focus_window);
1907 weston_wm_handle_event(int fd, uint32_t mask, void *data)
1909 struct weston_wm *wm = data;
1910 xcb_generic_event_t *event;
1913 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1914 if (weston_wm_handle_selection_event(wm, event)) {
1920 if (weston_wm_handle_dnd_event(wm, event)) {
1926 switch (EVENT_TYPE(event)) {
1927 case XCB_BUTTON_PRESS:
1928 case XCB_BUTTON_RELEASE:
1929 weston_wm_handle_button(wm, event);
1931 case XCB_ENTER_NOTIFY:
1932 weston_wm_handle_enter(wm, event);
1934 case XCB_LEAVE_NOTIFY:
1935 weston_wm_handle_leave(wm, event);
1937 case XCB_MOTION_NOTIFY:
1938 weston_wm_handle_motion(wm, event);
1940 case XCB_CREATE_NOTIFY:
1941 weston_wm_handle_create_notify(wm, event);
1943 case XCB_MAP_REQUEST:
1944 weston_wm_handle_map_request(wm, event);
1946 case XCB_MAP_NOTIFY:
1947 weston_wm_handle_map_notify(wm, event);
1949 case XCB_UNMAP_NOTIFY:
1950 weston_wm_handle_unmap_notify(wm, event);
1952 case XCB_REPARENT_NOTIFY:
1953 weston_wm_handle_reparent_notify(wm, event);
1955 case XCB_CONFIGURE_REQUEST:
1956 weston_wm_handle_configure_request(wm, event);
1958 case XCB_CONFIGURE_NOTIFY:
1959 weston_wm_handle_configure_notify(wm, event);
1961 case XCB_DESTROY_NOTIFY:
1962 weston_wm_handle_destroy_notify(wm, event);
1964 case XCB_MAPPING_NOTIFY:
1965 wm_log("XCB_MAPPING_NOTIFY\n");
1967 case XCB_PROPERTY_NOTIFY:
1968 weston_wm_handle_property_notify(wm, event);
1970 case XCB_CLIENT_MESSAGE:
1971 weston_wm_handle_client_message(wm, event);
1974 weston_wm_handle_focus_in(wm, event);
1982 xcb_flush(wm->conn);
1988 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1990 xcb_depth_iterator_t d_iter;
1991 xcb_visualtype_iterator_t vt_iter;
1992 xcb_visualtype_t *visualtype;
1994 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1996 while (d_iter.rem > 0) {
1997 if (d_iter.data->depth == 32) {
1998 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1999 visualtype = vt_iter.data;
2003 xcb_depth_next(&d_iter);
2006 if (visualtype == NULL) {
2007 weston_log("no 32 bit visualtype\n");
2011 wm->visual_id = visualtype->visual_id;
2012 wm->colormap = xcb_generate_id(wm->conn);
2013 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2014 wm->colormap, wm->screen->root, wm->visual_id);
2018 weston_wm_get_resources(struct weston_wm *wm)
2021 #define F(field) offsetof(struct weston_wm, field)
2023 static const struct { const char *name; int offset; } atoms[] = {
2024 { "WM_PROTOCOLS", F(atom.wm_protocols) },
2025 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
2026 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2027 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
2028 { "WM_STATE", F(atom.wm_state) },
2029 { "WM_S0", F(atom.wm_s0) },
2030 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
2031 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
2032 { "_NET_WM_NAME", F(atom.net_wm_name) },
2033 { "_NET_WM_PID", F(atom.net_wm_pid) },
2034 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2035 { "_NET_WM_STATE", F(atom.net_wm_state) },
2036 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2037 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
2038 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2039 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2040 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
2041 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
2042 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2044 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2045 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2046 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2047 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2048 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2049 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2050 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
2051 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2052 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
2053 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2054 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2055 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2056 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2057 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2059 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2060 { "_NET_SUPPORTING_WM_CHECK",
2061 F(atom.net_supporting_wm_check) },
2062 { "_NET_SUPPORTED", F(atom.net_supported) },
2063 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2064 { "CLIPBOARD", F(atom.clipboard) },
2065 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
2066 { "TARGETS", F(atom.targets) },
2067 { "UTF8_STRING", F(atom.utf8_string) },
2068 { "_WL_SELECTION", F(atom.wl_selection) },
2069 { "INCR", F(atom.incr) },
2070 { "TIMESTAMP", F(atom.timestamp) },
2071 { "MULTIPLE", F(atom.multiple) },
2072 { "UTF8_STRING" , F(atom.utf8_string) },
2073 { "COMPOUND_TEXT", F(atom.compound_text) },
2074 { "TEXT", F(atom.text) },
2075 { "STRING", F(atom.string) },
2076 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2077 { "text/plain", F(atom.text_plain) },
2078 { "XdndSelection", F(atom.xdnd_selection) },
2079 { "XdndAware", F(atom.xdnd_aware) },
2080 { "XdndEnter", F(atom.xdnd_enter) },
2081 { "XdndLeave", F(atom.xdnd_leave) },
2082 { "XdndDrop", F(atom.xdnd_drop) },
2083 { "XdndStatus", F(atom.xdnd_status) },
2084 { "XdndFinished", F(atom.xdnd_finished) },
2085 { "XdndTypeList", F(atom.xdnd_type_list) },
2086 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2087 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
2091 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2092 xcb_xfixes_query_version_reply_t *xfixes_reply;
2093 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2094 xcb_intern_atom_reply_t *reply;
2095 xcb_render_query_pict_formats_reply_t *formats_reply;
2096 xcb_render_query_pict_formats_cookie_t formats_cookie;
2097 xcb_render_pictforminfo_t *formats;
2100 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
2101 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
2103 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2105 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2106 cookies[i] = xcb_intern_atom (wm->conn, 0,
2107 strlen(atoms[i].name),
2110 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2111 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2112 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2116 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2117 if (!wm->xfixes || !wm->xfixes->present)
2118 weston_log("xfixes not available\n");
2120 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2121 XCB_XFIXES_MAJOR_VERSION,
2122 XCB_XFIXES_MINOR_VERSION);
2123 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2124 xfixes_cookie, NULL);
2126 weston_log("xfixes version: %d.%d\n",
2127 xfixes_reply->major_version, xfixes_reply->minor_version);
2131 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2133 if (formats_reply == NULL)
2136 formats = xcb_render_query_pict_formats_formats(formats_reply);
2137 for (i = 0; i < formats_reply->num_formats; i++) {
2138 if (formats[i].direct.red_mask != 0xff &&
2139 formats[i].direct.red_shift != 16)
2141 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2142 formats[i].depth == 24)
2143 wm->format_rgb = formats[i];
2144 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2145 formats[i].depth == 32 &&
2146 formats[i].direct.alpha_mask == 0xff &&
2147 formats[i].direct.alpha_shift == 24)
2148 wm->format_rgba = formats[i];
2151 free(formats_reply);
2155 weston_wm_create_wm_window(struct weston_wm *wm)
2157 static const char name[] = "Weston WM";
2159 wm->wm_window = xcb_generate_id(wm->conn);
2160 xcb_create_window(wm->conn,
2161 XCB_COPY_FROM_PARENT,
2167 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2168 wm->screen->root_visual,
2171 xcb_change_property(wm->conn,
2172 XCB_PROP_MODE_REPLACE,
2174 wm->atom.net_supporting_wm_check,
2179 xcb_change_property(wm->conn,
2180 XCB_PROP_MODE_REPLACE,
2182 wm->atom.net_wm_name,
2183 wm->atom.utf8_string,
2185 strlen(name), name);
2187 xcb_change_property(wm->conn,
2188 XCB_PROP_MODE_REPLACE,
2190 wm->atom.net_supporting_wm_check,
2195 /* Claim the WM_S0 selection even though we don't suport
2196 * the --replace functionality. */
2197 xcb_set_selection_owner(wm->conn,
2200 XCB_TIME_CURRENT_TIME);
2202 xcb_set_selection_owner(wm->conn,
2204 wm->atom.net_wm_cm_s0,
2205 XCB_TIME_CURRENT_TIME);
2209 weston_wm_create(struct weston_xserver *wxs, int fd)
2211 struct weston_wm *wm;
2212 struct wl_event_loop *loop;
2213 xcb_screen_iterator_t s;
2215 xcb_atom_t supported[5];
2217 wm = zalloc(sizeof *wm);
2222 wm->window_hash = hash_table_create();
2223 if (wm->window_hash == NULL) {
2228 /* xcb_connect_to_fd takes ownership of the fd. */
2229 wm->conn = xcb_connect_to_fd(fd, NULL);
2230 if (xcb_connection_has_error(wm->conn)) {
2231 weston_log("xcb_connect_to_fd failed\n");
2233 hash_table_destroy(wm->window_hash);
2238 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2239 wm->screen = s.data;
2241 loop = wl_display_get_event_loop(wxs->wl_display);
2243 wl_event_loop_add_fd(loop, fd,
2245 weston_wm_handle_event, wm);
2246 wl_event_source_check(wm->source);
2248 weston_wm_get_resources(wm);
2249 weston_wm_get_visual_and_colormap(wm);
2252 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2253 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2254 XCB_EVENT_MASK_PROPERTY_CHANGE;
2255 xcb_change_window_attributes(wm->conn, wm->screen->root,
2256 XCB_CW_EVENT_MASK, values);
2258 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2259 XCB_COMPOSITE_REDIRECT_MANUAL);
2261 wm->theme = theme_create();
2263 supported[0] = wm->atom.net_wm_moveresize;
2264 supported[1] = wm->atom.net_wm_state;
2265 supported[2] = wm->atom.net_wm_state_fullscreen;
2266 supported[3] = wm->atom.net_wm_state_maximized_vert;
2267 supported[4] = wm->atom.net_wm_state_maximized_horz;
2268 xcb_change_property(wm->conn,
2269 XCB_PROP_MODE_REPLACE,
2271 wm->atom.net_supported,
2274 ARRAY_LENGTH(supported), supported);
2276 weston_wm_selection_init(wm);
2278 weston_wm_dnd_init(wm);
2280 xcb_flush(wm->conn);
2282 wm->create_surface_listener.notify = weston_wm_create_surface;
2283 wl_signal_add(&wxs->compositor->create_surface_signal,
2284 &wm->create_surface_listener);
2285 wm->activate_listener.notify = weston_wm_window_activate;
2286 wl_signal_add(&wxs->compositor->activate_signal,
2287 &wm->activate_listener);
2288 wm->transform_listener.notify = weston_wm_window_transform;
2289 wl_signal_add(&wxs->compositor->transform_signal,
2290 &wm->transform_listener);
2291 wm->kill_listener.notify = weston_wm_kill_client;
2292 wl_signal_add(&wxs->compositor->kill_signal,
2293 &wm->kill_listener);
2294 wl_list_init(&wm->unpaired_window_list);
2296 weston_wm_create_cursors(wm);
2297 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2299 /* Create wm window and take WM_S0 selection last, which
2300 * signals to Xwayland that we're done with setup. */
2301 weston_wm_create_wm_window(wm);
2303 weston_log("created wm, root %d\n", wm->screen->root);
2309 weston_wm_destroy(struct weston_wm *wm)
2311 /* FIXME: Free windows in hash. */
2312 hash_table_destroy(wm->window_hash);
2313 weston_wm_destroy_cursors(wm);
2314 xcb_disconnect(wm->conn);
2315 wl_event_source_remove(wm->source);
2316 wl_list_remove(&wm->selection_listener.link);
2317 wl_list_remove(&wm->activate_listener.link);
2318 wl_list_remove(&wm->kill_listener.link);
2319 wl_list_remove(&wm->transform_listener.link);
2320 wl_list_remove(&wm->create_surface_listener.link);
2325 static struct weston_wm_window *
2326 get_wm_window(struct weston_surface *surface)
2328 struct wl_listener *listener;
2330 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2332 return container_of(listener, struct weston_wm_window,
2333 surface_destroy_listener);
2339 weston_wm_window_configure(void *data)
2341 struct weston_wm_window *window = data;
2342 struct weston_wm *wm = window->wm;
2344 int x, y, width, height;
2346 weston_wm_window_get_child_position(window, &x, &y);
2349 values[2] = window->width;
2350 values[3] = window->height;
2351 xcb_configure_window(wm->conn,
2353 XCB_CONFIG_WINDOW_X |
2354 XCB_CONFIG_WINDOW_Y |
2355 XCB_CONFIG_WINDOW_WIDTH |
2356 XCB_CONFIG_WINDOW_HEIGHT,
2359 weston_wm_window_get_frame_size(window, &width, &height);
2362 xcb_configure_window(wm->conn,
2364 XCB_CONFIG_WINDOW_WIDTH |
2365 XCB_CONFIG_WINDOW_HEIGHT,
2368 window->configure_source = NULL;
2370 weston_wm_window_schedule_repaint(window);
2374 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
2376 struct weston_wm_window *window = get_wm_window(surface);
2377 struct weston_wm *wm = window->wm;
2378 struct theme *t = window->wm->theme;
2379 int vborder, hborder;
2381 if (window->decorate && !window->fullscreen) {
2382 hborder = 2 * t->width;
2383 vborder = t->titlebar_height + t->width;
2389 if (width > hborder)
2390 window->width = width - hborder;
2394 if (height > vborder)
2395 window->height = height - vborder;
2400 frame_resize_inside(window->frame, window->width, window->height);
2402 if (window->configure_source)
2405 window->configure_source =
2406 wl_event_loop_add_idle(wm->server->loop,
2407 weston_wm_window_configure, window);
2410 static const struct weston_shell_client shell_client = {
2415 legacy_fullscreen(struct weston_wm *wm,
2416 struct weston_wm_window *window,
2417 struct weston_output **output_ret)
2419 struct weston_compositor *compositor = wm->server->compositor;
2420 struct weston_output *output;
2421 uint32_t minmax = PMinSize | PMaxSize;
2424 /* Heuristics for detecting legacy fullscreen windows... */
2426 wl_list_for_each(output, &compositor->output_list, link) {
2427 if (output->x == window->x &&
2428 output->y == window->y &&
2429 output->width == window->width &&
2430 output->height == window->height &&
2431 window->override_redirect) {
2432 *output_ret = output;
2437 if ((window->size_hints.flags & (USSize |PSize)) &&
2438 window->size_hints.width == output->width &&
2439 window->size_hints.height == output->height)
2441 if ((window->size_hints.flags & minmax) == minmax &&
2442 window->size_hints.min_width == output->width &&
2443 window->size_hints.min_height == output->height &&
2444 window->size_hints.max_width == output->width &&
2445 window->size_hints.max_height == output->height)
2448 if (matching_size && !window->decorate &&
2449 (window->size_hints.flags & (USPosition | PPosition)) &&
2450 window->size_hints.x == output->x &&
2451 window->size_hints.y == output->y) {
2452 *output_ret = output;
2461 weston_wm_window_type_inactive(struct weston_wm_window *window)
2463 struct weston_wm *wm = window->wm;
2465 return window->type == wm->atom.net_wm_window_type_tooltip ||
2466 window->type == wm->atom.net_wm_window_type_dropdown ||
2467 window->type == wm->atom.net_wm_window_type_dnd ||
2468 window->type == wm->atom.net_wm_window_type_combo ||
2469 window->type == wm->atom.net_wm_window_type_popup ||
2470 window->type == wm->atom.net_wm_window_type_utility;
2474 xserver_map_shell_surface(struct weston_wm_window *window,
2475 struct weston_surface *surface)
2477 struct weston_wm *wm = window->wm;
2478 struct weston_shell_interface *shell_interface =
2479 &wm->server->compositor->shell_interface;
2480 struct weston_output *output;
2481 struct weston_wm_window *parent;
2484 weston_wm_window_read_properties(window);
2486 /* A weston_wm_window may have many different surfaces assigned
2487 * throughout its life, so we must make sure to remove the listener
2488 * from the old surface signal list. */
2489 if (window->surface)
2490 wl_list_remove(&window->surface_destroy_listener.link);
2492 window->surface = surface;
2493 window->surface_destroy_listener.notify = surface_destroy;
2494 wl_signal_add(&window->surface->destroy_signal,
2495 &window->surface_destroy_listener);
2497 weston_wm_window_schedule_repaint(window);
2499 if (!shell_interface->create_shell_surface)
2502 if (!shell_interface->get_primary_view)
2505 if (window->surface->configure) {
2506 weston_log("warning, unexpected in %s: "
2507 "surface's configure hook is already set.\n",
2513 shell_interface->create_shell_surface(shell_interface->shell,
2516 window->view = shell_interface->get_primary_view(shell_interface->shell,
2520 shell_interface->set_title(window->shsurf, window->name);
2521 if (window->pid > 0)
2522 shell_interface->set_pid(window->shsurf, window->pid);
2524 if (window->fullscreen) {
2525 window->saved_width = window->width;
2526 window->saved_height = window->height;
2527 shell_interface->set_fullscreen(window->shsurf,
2528 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2531 } else if (legacy_fullscreen(wm, window, &output)) {
2532 window->fullscreen = 1;
2533 shell_interface->set_fullscreen(window->shsurf,
2534 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2536 } else if (window->override_redirect) {
2537 shell_interface->set_xwayland(window->shsurf,
2540 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2541 } else if (window->transient_for && window->transient_for->surface) {
2542 parent = window->transient_for;
2543 if (weston_wm_window_type_inactive(window))
2544 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
2545 shell_interface->set_transient(window->shsurf,
2547 window->x - parent->x,
2548 window->y - parent->y, flags);
2549 } else if (weston_wm_window_is_maximized(window)) {
2550 shell_interface->set_maximized(window->shsurf);
2552 if (weston_wm_window_type_inactive(window)) {
2553 shell_interface->set_xwayland(window->shsurf,
2556 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2558 shell_interface->set_toplevel(window->shsurf);