4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * client.c: holds all client-specific functions
17 #include <xcb/xcb_icccm.h>
26 * Removes the given client from the container, either because it will be inserted into another
27 * one or because it was unmapped
30 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container) {
31 CIRCLEQ_REMOVE(&(container->clients), client, clients);
33 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
35 /* If the container will be empty now and is in stacking mode, we need to
36 unmap the stack_win */
37 if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
38 struct Stack_Window *stack_win = &(container->stack_win);
39 stack_win->rect.height = 0;
40 xcb_unmap_window(conn, stack_win->window);
45 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
49 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
50 int mid_x = client->rect.width / 2,
51 mid_y = client->rect.height / 2;
52 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
56 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
59 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
60 xcb_get_property_cookie_t cookie;
61 xcb_get_wm_protocols_reply_t protocols;
64 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
65 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
68 /* Check if the client’s protocols have the requested atom set */
69 for (uint32_t i = 0; i < protocols.atoms_len; i++)
70 if (protocols.atoms[i] == atom)
73 xcb_get_wm_protocols_reply_wipe(&protocols);
79 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
82 void client_kill(xcb_connection_t *conn, Client *window) {
83 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
84 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
85 LOG("Killing window the hard way\n");
86 xcb_kill_client(conn, window->child);
90 xcb_client_message_event_t ev;
92 memset(&ev, 0, sizeof(xcb_client_message_event_t));
94 ev.response_type = XCB_CLIENT_MESSAGE;
95 ev.window = window->child;
96 ev.type = atoms[WM_PROTOCOLS];
98 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
99 ev.data.data32[1] = XCB_CURRENT_TIME;
101 LOG("Sending WM_DELETE to the client\n");
102 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
107 * Checks if the given window class and title match the given client
108 * Window title is passed as "normal" string and as UCS-2 converted string for
109 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
112 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
113 char *to_title_ucs, int to_title_ucs_len) {
114 /* Check if the given class is part of the window class */
115 if (strcasestr(client->window_class, to_class) == NULL)
118 /* If no title was given, we’re done */
119 if (to_title == NULL)
122 if (client->name_len > -1) {
123 /* UCS-2 converted window titles */
124 if (memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
128 if (strcasestr(client->name, to_title) == NULL)