4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * util.c: Utility functions, which can be useful everywhere.
28 int min(int a, int b) {
29 return (a < b ? a : b);
32 int max(int a, int b) {
33 return (a > b ? a : b);
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
41 void slog(char *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);
58 * Prints the message (see printf()) to stderr, then exits the program.
61 void die(char *fmt, ...) {
65 vfprintf(stderr, fmt, args);
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
76 void *smalloc(size_t size) {
77 void *result = malloc(size);
78 exit_if_null(result, "Too less memory for malloc(%d)\n", size);
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);
88 char *sstrdup(const char *str) {
89 char *result = strdup(str);
90 exit_if_null(result, "Too less memory for strdup()\n");
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 :-).
100 * The shell is determined by looking for the SHELL environment variable. If it
101 * does not exist, /bin/sh is used.
104 void start_application(const char *command) {
108 /* Stores the path of the shell */
109 static const char *shell = NULL;
112 if ((shell = getenv("SHELL")) == NULL)
115 /* This is the child */
116 execl(shell, shell, "-c", command, NULL);
125 * Checks a generic cookie for errors and quits with the given message if there
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);
132 fprintf(stderr, "ERROR: %s : %d\n", err_message , error->error_code);
133 xcb_disconnect(conn);
139 * Sets the given client as focused by updating the data structures correctly,
140 * updating the X input focus and finally re-decorating both windows (to signalize
141 * the user the new focus situation)
144 void set_focus(xcb_connection_t *conn, Client *client) {
145 /* The dock window cannot be focused */
146 /* TODO: does this play well with dzen2’s popup menus? or do we just need to set the input
147 focus but not update our internal structures? */
151 /* Store the old client */
152 Client *old_client = CUR_CELL->currently_focused;
154 /* TODO: check if the focus needs to be changed at all */
155 /* Store current_row/current_col */
156 c_ws->current_row = current_row;
157 c_ws->current_col = current_col;
158 c_ws = client->container->workspace;
160 /* Update container */
161 client->container->currently_focused = client;
163 current_col = client->container->col;
164 current_row = client->container->row;
166 LOG("set_focus(frame %08x, child %08x, name %s)\n", client->frame, client->child, client->name);
167 /* Set focus to the entered window, and flush xcb buffer immediately */
168 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, client->child, XCB_CURRENT_TIME);
169 //xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, 10, 10);
171 /* If we’re in stacking mode, this renders the container to update changes in the title
172 bars and to raise the focused client */
173 if ((old_client != NULL) && (old_client != client))
174 redecorate_window(conn, old_client);
176 SLIST_REMOVE(&(client->container->workspace->focus_stack), client, Client, focus_clients);
177 SLIST_INSERT_HEAD(&(client->container->workspace->focus_stack), client, focus_clients);
179 /* redecorate_window flushes, so we don’t need to */
180 redecorate_window(conn, client);
184 * Called when the user switches to another mode or when the container is
185 * destroyed and thus needs to be cleaned up.
188 void leave_stack_mode(xcb_connection_t *conn, Container *container) {
189 /* When going out of stacking mode, we need to close the window */
190 struct Stack_Window *stack_win = &(container->stack_win);
192 SLIST_REMOVE(&stack_wins, stack_win, Stack_Window, stack_windows);
194 xcb_free_gc(conn, stack_win->gc);
195 xcb_destroy_window(conn, stack_win->window);
197 stack_win->rect.width = -1;
198 stack_win->rect.height = -1;
202 * Switches the layout of the given container taking care of the necessary house-keeping
205 void switch_layout_mode(xcb_connection_t *conn, Container *container, int mode) {
206 if (mode == MODE_STACK) {
207 /* When we’re already in stacking mode, nothing has to be done */
208 if (container->mode == MODE_STACK)
211 /* When entering stacking mode, we need to open a window on which we can draw the
212 title bars of the clients, it has height 1 because we don’t bother here with
213 calculating the correct height - it will be adjusted when rendering anyways. */
214 Rect rect = {container->x, container->y, container->width, 1 };
219 /* Don’t generate events for our new window, it should *not* be managed */
220 mask |= XCB_CW_OVERRIDE_REDIRECT;
223 /* We want to know when… */
224 mask |= XCB_CW_EVENT_MASK;
225 values[1] = XCB_EVENT_MASK_BUTTON_PRESS | /* …mouse is pressed */
226 XCB_EVENT_MASK_EXPOSURE; /* …our window needs to be redrawn */
228 struct Stack_Window *stack_win = &(container->stack_win);
229 stack_win->window = create_window(conn, rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, mask, values);
231 /* Generate a graphics context for the titlebar */
232 stack_win->gc = xcb_generate_id(conn);
233 xcb_create_gc(conn, stack_win->gc, stack_win->window, 0, 0);
235 stack_win->container = container;
237 SLIST_INSERT_HEAD(&stack_wins, stack_win, stack_windows);
239 if (container->mode == MODE_STACK)
240 leave_stack_mode(conn, container);
242 container->mode = mode;
244 /* Force reconfiguration of each client */
247 CIRCLEQ_FOREACH(client, &(container->clients), clients)
248 client->force_reconfigure = true;
254 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
258 void warp_pointer_into(xcb_connection_t *conn, Client *client) {
259 int mid_x = client->rect.width / 2,
260 mid_y = client->rect.height / 2;
261 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
265 * Toggles fullscreen mode for the given client. It updates the data structures and
266 * reconfigures (= resizes/moves) the client and its frame to the full size of the
267 * screen. When leaving fullscreen, re-rendering the layout is forced.
270 void toggle_fullscreen(xcb_connection_t *conn, Client *client) {
271 /* clients without a container (docks) cannot be focused */
272 assert(client->container != NULL);
274 Workspace *workspace = client->container->workspace;
276 workspace->fullscreen_client = (client->fullscreen ? NULL : client);
278 client->fullscreen = !client->fullscreen;
280 if (client->fullscreen) {
281 LOG("Entering fullscreen mode...\n");
282 /* We just entered fullscreen mode, let’s configure the window */
283 uint32_t mask = XCB_CONFIG_WINDOW_X |
284 XCB_CONFIG_WINDOW_Y |
285 XCB_CONFIG_WINDOW_WIDTH |
286 XCB_CONFIG_WINDOW_HEIGHT;
287 uint32_t values[4] = {workspace->rect.x,
289 workspace->rect.width,
290 workspace->rect.height};
292 LOG("child itself will be at %dx%d with size %dx%d\n",
293 values[0], values[1], values[2], values[3]);
295 xcb_configure_window(conn, client->frame, mask, values);
297 /* Child’s coordinates are relative to the parent (=frame) */
300 xcb_configure_window(conn, client->child, mask, values);
302 /* Raise the window */
303 values[0] = XCB_STACK_MODE_ABOVE;
304 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
307 LOG("leaving fullscreen mode\n");
308 /* Because the coordinates of the window haven’t changed, it would not be
309 re-configured if we don’t set the following flag */
310 client->force_reconfigure = true;
311 /* We left fullscreen mode, redraw the container */
312 render_container(conn, client->container);