]> git.sur5r.net Git - i3/i3/blob - src/util.c
Bugfix: Correctly check for empty containers and unmap the stack_win
[i3/i3] / src / util.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  * util.c: Utility functions, which can be useful everywhere.
11  *
12  */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/wait.h>
18 #include <stdarg.h>
19 #include <assert.h>
20
21 #include "i3.h"
22 #include "data.h"
23 #include "table.h"
24 #include "layout.h"
25 #include "util.h"
26 #include "xcb.h"
27
28 int min(int a, int b) {
29         return (a < b ? a : b);
30 }
31
32 int max(int a, int b) {
33         return (a > b ? a : b);
34 }
35
36 /*
37  * Logs the given message to stdout while prefixing the current time to it.
38  * This is to be called by LOG() which includes filename/linenumber
39  *
40  */
41 void slog(char *fmt, ...) {
42         va_list args;
43         char timebuf[64];
44
45         va_start(args, fmt);
46         /* Get current time */
47         time_t t = time(NULL);
48         /* Convert time to local time (determined by the locale) */
49         struct tm *tmp = localtime(&t);
50         /* Generate time prefix */
51         strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
52         printf("%s", timebuf);
53         vprintf(fmt, args);
54         va_end(args);
55 }
56
57 /*
58  * Prints the message (see printf()) to stderr, then exits the program.
59  *
60  */
61 void die(char *fmt, ...) {
62         va_list args;
63
64         va_start(args, fmt);
65         vfprintf(stderr, fmt, args);
66         va_end(args);
67
68         exit(EXIT_FAILURE);
69 }
70
71 /*
72  * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of
73  * the called functions returns NULL, meaning that there is no more memory available
74  *
75  */
76 void *smalloc(size_t size) {
77         void *result = malloc(size);
78         exit_if_null(result, "Too less memory for malloc(%d)\n", size);
79         return result;
80 }
81
82 void *scalloc(size_t size) {
83         void *result = calloc(size, 1);
84         exit_if_null(result, "Too less memory for calloc(%d)\n", size);
85         return result;
86 }
87
88 char *sstrdup(const char *str) {
89         char *result = strdup(str);
90         exit_if_null(result, "Too less memory for strdup()\n");
91         return result;
92 }
93
94 /*
95  * Starts the given application by passing it through a shell. We use double fork
96  * to avoid zombie processes. As the started application’s parent exits (immediately),
97  * the application is reparented to init (process-id 1), which correctly handles
98  * childs, so we don’t have to do it :-).
99  *
100  * The shell is determined by looking for the SHELL environment variable. If it
101  * does not exist, /bin/sh is used.
102  *
103  */
104 void start_application(const char *command) {
105         if (fork() == 0) {
106                 /* Child process */
107                 if (fork() == 0) {
108                         /* Stores the path of the shell */
109                         static const char *shell = NULL;
110
111                         if (shell == NULL)
112                                 if ((shell = getenv("SHELL")) == NULL)
113                                         shell = "/bin/sh";
114
115                         /* This is the child */
116                         execl(shell, shell, "-c", command, NULL);
117                         /* not reached */
118                 }
119                 exit(0);
120         }
121         wait(0);
122 }
123
124 /*
125  * Checks a generic cookie for errors and quits with the given message if there
126  * was an error.
127  *
128  */
129 void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, char *err_message) {
130         xcb_generic_error_t *error = xcb_request_check(conn, cookie);
131         if (error != NULL) {
132                 fprintf(stderr, "ERROR: %s : %d\n", err_message , error->error_code);
133                 xcb_disconnect(conn);
134                 exit(-1);
135         }
136 }
137
138 /*
139  * Removes the given client from the container, either because it will be inserted into another
140  * one or because it was unmapped
141  *
142  */
143 void remove_client_from_container(xcb_connection_t *conn, Client *client, Container *container) {
144         CIRCLEQ_REMOVE(&(container->clients), client, clients);
145
146         /* If the container will be empty now and is in stacking mode, we need to
147            unmap the stack_win */
148         if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
149                 struct Stack_Window *stack_win = &(container->stack_win);
150                 stack_win->rect.height = 0;
151                 xcb_unmap_window(conn, stack_win->window);
152         }
153 }
154
155 /*
156  * Sets the given client as focused by updating the data structures correctly,
157  * updating the X input focus and finally re-decorating both windows (to signalize
158  * the user the new focus situation)
159  *
160  */
161 void set_focus(xcb_connection_t *conn, Client *client) {
162         /* The dock window cannot be focused */
163         /* TODO: does this play well with dzen2’s popup menus? or do we just need to set the input
164            focus but not update our internal structures? */
165         if (client->dock)
166                 return;
167
168         /* Store the old client */
169         Client *old_client = CUR_CELL->currently_focused;
170
171         /* TODO: check if the focus needs to be changed at all */
172         /* Store current_row/current_col */
173         c_ws->current_row = current_row;
174         c_ws->current_col = current_col;
175         c_ws = client->container->workspace;
176
177         /* Update container */
178         client->container->currently_focused = client;
179
180         current_col = client->container->col;
181         current_row = client->container->row;
182
183         LOG("set_focus(frame %08x, child %08x, name %s)\n", client->frame, client->child, client->name);
184         /* Set focus to the entered window, and flush xcb buffer immediately */
185         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, client->child, XCB_CURRENT_TIME);
186         //xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, 10, 10);
187
188         /* If we’re in stacking mode, this renders the container to update changes in the title
189            bars and to raise the focused client */
190         if ((old_client != NULL) && (old_client != client))
191                 redecorate_window(conn, old_client);
192
193         SLIST_REMOVE(&(client->container->workspace->focus_stack), client, Client, focus_clients);
194         SLIST_INSERT_HEAD(&(client->container->workspace->focus_stack), client, focus_clients);
195
196         /* redecorate_window flushes, so we don’t need to */
197         redecorate_window(conn, client);
198 }
199
200 /*
201  * Called when the user switches to another mode or when the container is
202  * destroyed and thus needs to be cleaned up.
203  *
204  */
205 void leave_stack_mode(xcb_connection_t *conn, Container *container) {
206         /* When going out of stacking mode, we need to close the window */
207         struct Stack_Window *stack_win = &(container->stack_win);
208
209         SLIST_REMOVE(&stack_wins, stack_win, Stack_Window, stack_windows);
210
211         xcb_free_gc(conn, stack_win->gc);
212         xcb_destroy_window(conn, stack_win->window);
213
214         stack_win->rect.width = -1;
215         stack_win->rect.height = -1;
216 }
217
218 /*
219  * Switches the layout of the given container taking care of the necessary house-keeping
220  *
221  */
222 void switch_layout_mode(xcb_connection_t *conn, Container *container, int mode) {
223         if (mode == MODE_STACK) {
224                 /* When we’re already in stacking mode, nothing has to be done */
225                 if (container->mode == MODE_STACK)
226                         return;
227
228                 /* When entering stacking mode, we need to open a window on which we can draw the
229                    title bars of the clients, it has height 1 because we don’t bother here with
230                    calculating the correct height - it will be adjusted when rendering anyways. */
231                 Rect rect = {container->x, container->y, container->width, 1 };
232
233                 uint32_t mask = 0;
234                 uint32_t values[2];
235
236                 /* Don’t generate events for our new window, it should *not* be managed */
237                 mask |= XCB_CW_OVERRIDE_REDIRECT;
238                 values[0] = 1;
239
240                 /* We want to know when… */
241                 mask |= XCB_CW_EVENT_MASK;
242                 values[1] =     XCB_EVENT_MASK_BUTTON_PRESS |   /* …mouse is pressed */
243                                 XCB_EVENT_MASK_EXPOSURE;        /* …our window needs to be redrawn */
244
245                 struct Stack_Window *stack_win = &(container->stack_win);
246                 stack_win->window = create_window(conn, rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, mask, values);
247
248                 /* Generate a graphics context for the titlebar */
249                 stack_win->gc = xcb_generate_id(conn);
250                 xcb_create_gc(conn, stack_win->gc, stack_win->window, 0, 0);
251
252                 stack_win->container = container;
253
254                 SLIST_INSERT_HEAD(&stack_wins, stack_win, stack_windows);
255         } else {
256                 if (container->mode == MODE_STACK)
257                         leave_stack_mode(conn, container);
258         }
259         container->mode = mode;
260
261         /* Force reconfiguration of each client */
262         Client *client;
263
264         CIRCLEQ_FOREACH(client, &(container->clients), clients)
265                 client->force_reconfigure = true;
266
267         render_layout(conn);
268 }
269
270 /*
271  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
272  * selecting it
273  *
274  */
275 void warp_pointer_into(xcb_connection_t *conn, Client *client) {
276         int mid_x = client->rect.width / 2,
277             mid_y = client->rect.height / 2;
278         xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
279 }
280
281 /*
282  * Toggles fullscreen mode for the given client. It updates the data structures and
283  * reconfigures (= resizes/moves) the client and its frame to the full size of the
284  * screen. When leaving fullscreen, re-rendering the layout is forced.
285  *
286  */
287 void toggle_fullscreen(xcb_connection_t *conn, Client *client) {
288         /* clients without a container (docks) cannot be focused */
289         assert(client->container != NULL);
290
291         Workspace *workspace = client->container->workspace;
292
293         workspace->fullscreen_client = (client->fullscreen ? NULL : client);
294
295         client->fullscreen = !client->fullscreen;
296
297         if (client->fullscreen) {
298                 LOG("Entering fullscreen mode...\n");
299                 /* We just entered fullscreen mode, let’s configure the window */
300                  uint32_t mask = XCB_CONFIG_WINDOW_X |
301                                  XCB_CONFIG_WINDOW_Y |
302                                  XCB_CONFIG_WINDOW_WIDTH |
303                                  XCB_CONFIG_WINDOW_HEIGHT;
304                 uint32_t values[4] = {workspace->rect.x,
305                                       workspace->rect.y,
306                                       workspace->rect.width,
307                                       workspace->rect.height};
308
309                 LOG("child itself will be at %dx%d with size %dx%d\n",
310                                 values[0], values[1], values[2], values[3]);
311
312                 xcb_configure_window(conn, client->frame, mask, values);
313
314                 /* Child’s coordinates are relative to the parent (=frame) */
315                 values[0] = 0;
316                 values[1] = 0;
317                 xcb_configure_window(conn, client->child, mask, values);
318
319                 /* Raise the window */
320                 values[0] = XCB_STACK_MODE_ABOVE;
321                 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
322
323         } else {
324                 LOG("leaving fullscreen mode\n");
325                 /* Because the coordinates of the window haven’t changed, it would not be
326                    re-configured if we don’t set the following flag */
327                 client->force_reconfigure = true;
328                 /* We left fullscreen mode, redraw the container */
329                 render_container(conn, client->container);
330         }
331
332         xcb_flush(conn);
333 }