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