]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Implement the urgency hint for windows/workspaces
[i3/i3] / src / workspace.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  * workspace.c: Functions for modifying workspaces
11  *
12  */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <err.h>
18
19 #include "util.h"
20 #include "data.h"
21 #include "i3.h"
22 #include "config.h"
23 #include "xcb.h"
24 #include "table.h"
25 #include "xinerama.h"
26 #include "layout.h"
27 #include "workspace.h"
28 #include "client.h"
29
30 /*
31  * Sets the name (or just its number) for the given workspace. This has to
32  * be called for every workspace as the rendering function
33  * (render_internal_bar) relies on workspace->name and workspace->name_len
34  * being ready-to-use.
35  *
36  */
37 void workspace_set_name(Workspace *ws, const char *name) {
38         char *label;
39         int ret;
40
41         if (name != NULL)
42                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
43         else ret = asprintf(&label, "%d", ws->num + 1);
44
45         if (ret == -1)
46                 errx(1, "asprintf() failed");
47
48         FREE(ws->name);
49
50         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
51         if (config.font != NULL)
52                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
53         else ws->text_width = 0;
54
55         free(label);
56 }
57
58 /*
59  * Returns true if the workspace is currently visible. Especially important for
60  * multi-monitor environments, as they can have multiple currenlty active
61  * workspaces.
62  *
63  */
64 bool workspace_is_visible(Workspace *ws) {
65         return (ws->screen->current_workspace == ws->num);
66 }
67
68 /*
69  * Switches to the given workspace
70  *
71  */
72 void workspace_show(xcb_connection_t *conn, int workspace) {
73         bool need_warp = false;
74         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
75         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
76         Workspace *t_ws = &(workspaces[workspace-1]);
77
78         LOG("show_workspace(%d)\n", workspace);
79
80         /* Store current_row/current_col */
81         c_ws->current_row = current_row;
82         c_ws->current_col = current_col;
83
84         /* Check if the workspace has not been used yet */
85         workspace_initialize(t_ws, c_ws->screen);
86
87         if (c_ws->screen != t_ws->screen) {
88                 /* We need to switch to the other screen first */
89                 LOG("moving over to other screen.\n");
90
91                 /* Store the old client */
92                 Client *old_client = CUR_CELL->currently_focused;
93
94                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
95                 current_col = c_ws->current_col;
96                 current_row = c_ws->current_row;
97                 if (CUR_CELL->currently_focused != NULL)
98                         need_warp = true;
99                 else {
100                         Rect *dims = &(c_ws->screen->rect);
101                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
102                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
103                 }
104
105                 /* Re-decorate the old client, it’s not focused anymore */
106                 if ((old_client != NULL) && !old_client->dock)
107                         redecorate_window(conn, old_client);
108                 else xcb_flush(conn);
109         }
110
111         /* Check if we need to change something or if we’re already there */
112         if (c_ws->screen->current_workspace == (workspace-1)) {
113                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
114                 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
115                         set_focus(conn, last_focused, true);
116                         if (need_warp) {
117                                 client_warp_pointer_into(conn, last_focused);
118                                 xcb_flush(conn);
119                         }
120                 }
121
122                 return;
123         }
124
125         t_ws->screen->current_workspace = workspace-1;
126         Workspace *old_workspace = c_ws;
127         c_ws = &workspaces[workspace-1];
128
129         /* Unmap all clients of the old workspace */
130         workspace_unmap_clients(conn, old_workspace);
131
132         current_row = c_ws->current_row;
133         current_col = c_ws->current_col;
134         LOG("new current row = %d, current col = %d\n", current_row, current_col);
135
136         workspace_map_clients(conn, c_ws);
137
138         /* Restore focus on the new workspace */
139         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
140         if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
141                 set_focus(conn, last_focused, true);
142                 if (need_warp) {
143                         client_warp_pointer_into(conn, last_focused);
144                         xcb_flush(conn);
145                 }
146         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
147
148         render_layout(conn);
149 }
150
151
152 /*
153  * Parses the preferred_screen property of a workspace. You can either specify
154  * the screen number (it is not given that the screen numbering always stays
155  * the same) or the screen coordinates (exact coordinates, e.g. 1280 will match
156  * the screen starting at x=1280, but 1281 will not). For coordinates, you can
157  * either specify an x coordinate ("1280") or an y coordinate ("x800") or both
158  * ("1280x800").
159  *
160  */
161 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
162         i3Screen *screen;
163         char *rest;
164         int preferred_screen = strtol(preference, &rest, 10);
165
166         LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
167
168         if ((rest == preference) || (preferred_screen >= num_screens)) {
169                 int x = INT_MAX, y = INT_MAX;
170                 if (strchr(preference, 'x') != NULL) {
171                         /* Check if only the y coordinate was specified */
172                         if (*preference == 'x')
173                                 y = atoi(preference+1);
174                         else {
175                                 x = atoi(preference);
176                                 y = atoi(strchr(preference, 'x') + 1);
177                         }
178                 } else {
179                         x = atoi(preference);
180                 }
181
182                 LOG("Looking for screen at %d x %d\n", x, y);
183
184                 TAILQ_FOREACH(screen, slist, screens)
185                         if ((x == INT_MAX || screen->rect.x == x) &&
186                             (y == INT_MAX || screen->rect.y == y)) {
187                                 LOG("found %p\n", screen);
188                                 return screen;
189                         }
190
191                 LOG("none found\n");
192                 return NULL;
193         } else {
194                 int c = 0;
195                 TAILQ_FOREACH(screen, slist, screens)
196                         if (c++ == preferred_screen)
197                                 return screen;
198         }
199
200         return NULL;
201 }
202
203 /*
204  * Initializes the given workspace if it is not already initialized. The given
205  * screen is to be understood as a fallback, if the workspace itself either
206  * was not assigned to a particular screen or cannot be placed there because
207  * the screen is not attached at the moment.
208  *
209  */
210 void workspace_initialize(Workspace *ws, i3Screen *screen) {
211         if (ws->screen != NULL) {
212                 LOG("Workspace already initialized\n");
213                 return;
214         }
215
216         /* If this workspace has no preferred screen or if the screen it wants
217          * to be on is not available at the moment, we initialize it with
218          * the screen which was given */
219         if (ws->preferred_screen == NULL ||
220             (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
221                 ws->screen = screen;
222         else { LOG("yay, found assignment\n"); }
223
224         /* Copy the dimensions from the virtual screen */
225         memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
226 }
227
228 /*
229  * Gets the first unused workspace for the given screen, taking into account
230  * the preferred_screen setting of every workspace (workspace assignments).
231  *
232  */
233 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
234         Workspace *result = NULL;
235
236         for (int c = 0; c < 10; c++) {
237                 Workspace *ws = &(workspaces[c]);
238                 if (ws->preferred_screen == NULL ||
239                     !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
240                         continue;
241
242                 result = ws;
243                 break;
244         }
245
246         if (result == NULL) {
247                 /* No assignment found, returning first unused workspace */
248                 for (int c = 0; c < 10; c++) {
249                         if (workspaces[c].screen != NULL)
250                                 continue;
251
252                         result = &(workspaces[c]);
253                         break;
254                 }
255         }
256
257         if (result != NULL) {
258                 workspace_initialize(result, screen);
259                 return result;
260         }
261
262         LOG("WARNING: No free workspace found to assign!\n");
263         return NULL;
264 }
265
266 /*
267  * Maps all clients (and stack windows) of the given workspace.
268  *
269  */
270 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
271         Client *client;
272
273         ignore_enter_notify_forall(conn, ws, true);
274
275         /* Map all clients on the new workspace */
276         FOR_TABLE(ws)
277                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
278                         client_map(conn, client);
279
280         /* Map all floating clients */
281         if (!ws->floating_hidden)
282                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
283                         client_map(conn, client);
284
285         /* Map all stack windows, if any */
286         struct Stack_Window *stack_win;
287         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
288                 if (stack_win->container->workspace == ws)
289                         xcb_map_window(conn, stack_win->window);
290
291         ignore_enter_notify_forall(conn, ws, false);
292 }
293
294 /*
295  * Unmaps all clients (and stack windows) of the given workspace.
296  *
297  * This needs to be called separately when temporarily rendering
298  * a workspace which is not the active workspace to force
299  * reconfiguration of all clients, like in src/xinerama.c when
300  * re-assigning a workspace to another screen.
301  *
302  */
303 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
304         Client *client;
305         struct Stack_Window *stack_win;
306
307         /* Ignore notify events because they would cause focus to be changed */
308         ignore_enter_notify_forall(conn, u_ws, true);
309
310         /* Unmap all clients of the given workspace */
311         int unmapped_clients = 0;
312         FOR_TABLE(u_ws)
313                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
314                         LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
315                         client_unmap(conn, client);
316                         unmapped_clients++;
317                 }
318
319         /* To find floating clients, we traverse the focus stack */
320         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
321                 if (!client_is_floating(client))
322                         continue;
323
324                 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
325
326                 client_unmap(conn, client);
327                 unmapped_clients++;
328         }
329
330         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
331          * if it is not the current workspace. */
332         if (unmapped_clients == 0 && u_ws != c_ws) {
333                 /* Re-assign the workspace of all dock clients which use this workspace */
334                 Client *dock;
335                 LOG("workspace %p is empty\n", u_ws);
336                 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
337                         if (dock->workspace != u_ws)
338                                 continue;
339
340                         LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
341                         dock->workspace = c_ws;
342                 }
343                 u_ws->screen = NULL;
344         }
345
346         /* Unmap the stack windows on the given workspace, if any */
347         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
348                 if (stack_win->container->workspace == u_ws)
349                         xcb_unmap_window(conn, stack_win->window);
350
351         ignore_enter_notify_forall(conn, u_ws, false);
352 }
353
354 /*
355  * Goes through all clients on the given workspace and updates the workspace’s
356  * urgent flag accordingly.
357  *
358  */
359 void workspace_update_urgent_flag(Workspace *ws) {
360         Client *current;
361
362         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
363                 if (!current->urgent)
364                         continue;
365
366                 ws->urgent = true;
367                 return;
368         }
369
370         ws->urgent = false;
371 }