]> git.sur5r.net Git - i3/i3/blob - src/client.c
Implement putting clients onto specific workspaces ("assign" in the configfile)
[i3/i3] / src / client.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * client.c: holds all client-specific functions
11  *
12  */
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <xcb/xcb.h>
17 #include <xcb/xcb_icccm.h>
18
19 #include "data.h"
20 #include "i3.h"
21 #include "xcb.h"
22 #include "util.h"
23 #include "queue.h"
24
25 /*
26  * Removes the given client from the container, either because it will be inserted into another
27  * one or because it was unmapped
28  *
29  */
30 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container) {
31         CIRCLEQ_REMOVE(&(container->clients), client, clients);
32
33         SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
34
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);
41         }
42 }
43
44 /*
45  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
46  * selecting it
47  *
48  */
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);
53 }
54
55 /*
56  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
57  *
58  */
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;
62         bool result = false;
63
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)
66                 return false;
67
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)
71                         result = true;
72
73         xcb_get_wm_protocols_reply_wipe(&protocols);
74
75         return result;
76 }
77
78 /*
79  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
80  *
81  */
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);
87                 return;
88         }
89
90         xcb_client_message_event_t ev;
91
92         memset(&ev, 0, sizeof(xcb_client_message_event_t));
93
94         ev.response_type = XCB_CLIENT_MESSAGE;
95         ev.window = window->child;
96         ev.type = atoms[WM_PROTOCOLS];
97         ev.format = 32;
98         ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
99         ev.data.data32[1] = XCB_CURRENT_TIME;
100
101         LOG("Sending WM_DELETE to the client\n");
102         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
103         xcb_flush(conn);
104 }
105
106 /*
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.
110  *
111  */
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)
116                 return false;
117
118         /* If no title was given, we’re done */
119         if (to_title == NULL)
120                 return true;
121
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)
125                         return false;
126         } else {
127                 /* Legacy hints */
128                 if (strcasestr(client->name, to_title) == NULL)
129                         return false;
130         }
131
132         return true;
133 }