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