]> git.sur5r.net Git - i3/i3/blob - src/client.c
53cacbf4d69f713cb939c01540c054d28a80eacf
[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 #include <assert.h>
16
17 #include <xcb/xcb.h>
18 #include <xcb/xcb_icccm.h>
19
20 #include "data.h"
21 #include "i3.h"
22 #include "xcb.h"
23 #include "util.h"
24 #include "queue.h"
25 #include "layout.h"
26
27 /*
28  * Removes the given client from the container, either because it will be inserted into another
29  * one or because it was unmapped
30  *
31  */
32 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
33         CIRCLEQ_REMOVE(&(container->clients), client, clients);
34
35         if (remove_from_focusstack)
36                 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
37
38         /* If the container will be empty now and is in stacking mode, we need to
39            unmap the stack_win */
40         if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
41                 struct Stack_Window *stack_win = &(container->stack_win);
42                 stack_win->rect.height = 0;
43                 xcb_unmap_window(conn, stack_win->window);
44         }
45 }
46
47 /*
48  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
49  * selecting it
50  *
51  */
52 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
53         int mid_x = client->rect.width / 2,
54             mid_y = client->rect.height / 2;
55         xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
56 }
57
58 /*
59  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
60  *
61  */
62 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
63         xcb_get_property_cookie_t cookie;
64         xcb_get_wm_protocols_reply_t protocols;
65         bool result = false;
66
67         cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
68         if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
69                 return false;
70
71         /* Check if the client’s protocols have the requested atom set */
72         for (uint32_t i = 0; i < protocols.atoms_len; i++)
73                 if (protocols.atoms[i] == atom)
74                         result = true;
75
76         xcb_get_wm_protocols_reply_wipe(&protocols);
77
78         return result;
79 }
80
81 /*
82  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
83  *
84  */
85 void client_kill(xcb_connection_t *conn, Client *window) {
86         /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
87         if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
88                 LOG("Killing window the hard way\n");
89                 xcb_kill_client(conn, window->child);
90                 return;
91         }
92
93         xcb_client_message_event_t ev;
94
95         memset(&ev, 0, sizeof(xcb_client_message_event_t));
96
97         ev.response_type = XCB_CLIENT_MESSAGE;
98         ev.window = window->child;
99         ev.type = atoms[WM_PROTOCOLS];
100         ev.format = 32;
101         ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
102         ev.data.data32[1] = XCB_CURRENT_TIME;
103
104         LOG("Sending WM_DELETE to the client\n");
105         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
106         xcb_flush(conn);
107 }
108
109 /*
110  * Checks if the given window class and title match the given client
111  * Window title is passed as "normal" string and as UCS-2 converted string for
112  * matching _NET_WM_NAME capable clients as well as those using legacy hints.
113  *
114  */
115 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
116                                char *to_title_ucs, int to_title_ucs_len) {
117         /* Check if the given class is part of the window class */
118         if (strcasestr(client->window_class, to_class) == NULL)
119                 return false;
120
121         /* If no title was given, we’re done */
122         if (to_title == NULL)
123                 return true;
124
125         if (client->name_len > -1) {
126                 /* UCS-2 converted window titles */
127                 if (memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
128                         return false;
129         } else {
130                 /* Legacy hints */
131                 if (strcasestr(client->name, to_title) == NULL)
132                         return false;
133         }
134
135         return true;
136 }
137
138 /*
139  * Toggles fullscreen mode for the given client. It updates the data structures and
140  * reconfigures (= resizes/moves) the client and its frame to the full size of the
141  * screen. When leaving fullscreen, re-rendering the layout is forced.
142  *
143  */
144 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
145         /* dock clients cannot enter fullscreen mode */
146         assert(!client->dock);
147
148         Workspace *workspace = client->workspace;
149
150         if (!client->fullscreen) {
151                 if (workspace->fullscreen_client != NULL) {
152                         LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
153                         return;
154                 }
155                 client->fullscreen = true;
156                 workspace->fullscreen_client = client;
157                 LOG("Entering fullscreen mode...\n");
158                 /* We just entered fullscreen mode, let’s configure the window */
159                 uint32_t mask = XCB_CONFIG_WINDOW_X |
160                                 XCB_CONFIG_WINDOW_Y |
161                                 XCB_CONFIG_WINDOW_WIDTH |
162                                 XCB_CONFIG_WINDOW_HEIGHT;
163                 uint32_t values[4] = {workspace->rect.x,
164                                       workspace->rect.y,
165                                       workspace->rect.width,
166                                       workspace->rect.height};
167
168                 LOG("child itself will be at %dx%d with size %dx%d\n",
169                                 values[0], values[1], values[2], values[3]);
170
171                 xcb_configure_window(conn, client->frame, mask, values);
172
173                 /* Child’s coordinates are relative to the parent (=frame) */
174                 values[0] = 0;
175                 values[1] = 0;
176                 xcb_configure_window(conn, client->child, mask, values);
177
178                 /* Raise the window */
179                 values[0] = XCB_STACK_MODE_ABOVE;
180                 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
181
182                 Rect child_rect = workspace->rect;
183                 child_rect.x = child_rect.y = 0;
184                 fake_configure_notify(conn, child_rect, client->child);
185         } else {
186                 LOG("leaving fullscreen mode\n");
187                 client->fullscreen = false;
188                 workspace->fullscreen_client = NULL;
189                 if (client->floating >= FLOATING_AUTO_ON) {
190                         /* For floating clients it’s enough if we just reconfigure that window (in fact,
191                          * re-rendering the layout will not update the client.) */
192                         reposition_client(conn, client);
193                         resize_client(conn, client);
194                         /* redecorate_window flushes */
195                         redecorate_window(conn, client);
196                 } else {
197                         /* Because the coordinates of the window haven’t changed, it would not be
198                            re-configured if we don’t set the following flag */
199                         client->force_reconfigure = true;
200                         /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
201                         render_layout(conn);
202                 }
203         }
204
205         xcb_flush(conn);
206 }