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