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 * Prints the message (see printf()) to stderr, then exits the program.
40 void die(char *fmt, ...) {
44 vfprintf(stderr, fmt, args);
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
55 void *smalloc(size_t size) {
56 void *result = malloc(size);
57 exit_if_null(result, "Too less memory for malloc(%d)\n", size);
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);
67 char *sstrdup(const char *str) {
68 char *result = strdup(str);
69 exit_if_null(result, "Too less memory for strdup()\n");
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 :-).
79 * The shell is determined by looking for the SHELL environment variable. If it
80 * does not exist, /bin/sh is used.
83 void start_application(const char *command) {
87 /* Stores the path of the shell */
88 static const char *shell = NULL;
91 if ((shell = getenv("SHELL")) == NULL)
94 /* This is the child */
95 execl(shell, shell, "-c", command, NULL);
104 * Checks a generic cookie for errors and quits with the given message if there
108 void check_error(xcb_connection_t *connection, xcb_void_cookie_t cookie, char *err_message) {
109 xcb_generic_error_t *error = xcb_request_check(connection, cookie);
111 fprintf(stderr, "ERROR: %s : %d\n", err_message , error->error_code);
112 xcb_disconnect(connection);
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)
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? */
130 /* Store the old client */
131 Client *old_client = CUR_CELL->currently_focused;
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;
139 /* Update container */
140 client->container->currently_focused = client;
142 current_col = client->container->col;
143 current_row = client->container->row;
145 printf("set_focus(frame %08x, child %08x)\n", client->frame, client->child);
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);
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);
155 /* redecorate_window flushes, so we don’t need to */
156 redecorate_window(conn, client);
160 * Switches the layout of the given container taking care of the necessary house-keeping
163 void switch_layout_mode(xcb_connection_t *conn, Container *container, int mode) {
164 if (mode == MODE_STACK) {
165 /* When we’re already in stacking mode, nothing has to be done */
166 if (container->mode == MODE_STACK)
169 /* When entering stacking mode, we need to open a window on which we can draw the
170 title bars of the clients, it has height 1 because we don’t bother here with
171 calculating the correct height - it will be adjusted when rendering anyways. */
172 Rect rect = {container->x, container->y, container->width, 1 };
177 /* Don’t generate events for our new window, it should *not* be managed */
178 mask |= XCB_CW_OVERRIDE_REDIRECT;
181 /* We want to know when… */
182 mask |= XCB_CW_EVENT_MASK;
183 values[1] = XCB_EVENT_MASK_BUTTON_PRESS | /* …mouse is pressed */
184 XCB_EVENT_MASK_EXPOSURE; /* …our window needs to be redrawn */
186 struct Stack_Window *stack_win = &(container->stack_win);
187 stack_win->window = create_window(conn, rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, mask, values);
189 /* Generate a graphics context for the titlebar */
190 stack_win->gc = xcb_generate_id(conn);
191 xcb_create_gc(conn, stack_win->gc, stack_win->window, 0, 0);
193 stack_win->container = container;
195 SLIST_INSERT_HEAD(&stack_wins, stack_win, stack_windows);
197 if (container->mode == MODE_STACK) {
198 /* When going out of stacking mode, we need to close the window */
199 struct Stack_Window *stack_win = &(container->stack_win);
201 SLIST_REMOVE(&stack_wins, stack_win, Stack_Window, stack_windows);
203 xcb_free_gc(conn, stack_win->gc);
204 xcb_destroy_window(conn, stack_win->window);
206 stack_win->width = -1;
207 stack_win->height = -1;
210 container->mode = mode;
212 /* Force reconfiguration of each client */
215 CIRCLEQ_FOREACH(client, &(container->clients), clients)
216 client->force_reconfigure = true;
222 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
226 void warp_pointer_into(xcb_connection_t *connection, Client *client) {
227 int mid_x = client->rect.width / 2,
228 mid_y = client->rect.height / 2;
229 xcb_warp_pointer(connection, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
233 * Toggles fullscreen mode for the given client. It updates the data structures and
234 * reconfigures (= resizes/moves) the client and its frame to the full size of the
235 * screen. When leaving fullscreen, re-rendering the layout is forced.
238 void toggle_fullscreen(xcb_connection_t *conn, Client *client) {
239 /* clients without a container (docks) cannot be focused */
240 assert(client->container != NULL);
242 Workspace *workspace = client->container->workspace;
244 workspace->fullscreen_client = (client->fullscreen ? NULL : client);
246 client->fullscreen = !client->fullscreen;
248 if (client->fullscreen) {
249 printf("Entering fullscreen mode...\n");
250 /* We just entered fullscreen mode, let’s configure the window */
251 uint32_t mask = XCB_CONFIG_WINDOW_X |
252 XCB_CONFIG_WINDOW_Y |
253 XCB_CONFIG_WINDOW_WIDTH |
254 XCB_CONFIG_WINDOW_HEIGHT;
255 uint32_t values[4] = {workspace->rect.x,
257 workspace->rect.width,
258 workspace->rect.height};
260 printf("child itself will be at %dx%d with size %dx%d\n",
261 values[0], values[1], values[2], values[3]);
263 xcb_configure_window(conn, client->frame, mask, values);
265 /* Child’s coordinates are relative to the parent (=frame) */
268 xcb_configure_window(conn, client->child, mask, values);
270 /* Raise the window */
271 values[0] = XCB_STACK_MODE_ABOVE;
272 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
275 printf("leaving fullscreen mode\n");
276 /* Because the coordinates of the window haven’t changed, it would not be
277 re-configured if we don’t set the following flag */
278 client->force_reconfigure = true;
279 /* We left fullscreen mode, redraw the container */
280 render_container(conn, client->container);