]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Re-implement support for the urgency hint, extend t/13-urgent.t
[i3/i3] / src / workspace.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * workspace.c: Functions for modifying workspaces
8  *
9  */
10 #include <limits.h>
11
12 #include "all.h"
13
14 /*
15  * Returns a pointer to the workspace with the given number (starting at 0),
16  * creating the workspace if necessary (by allocating the necessary amount of
17  * memory and initializing the data structures correctly).
18  *
19  */
20 Con *workspace_get(const char *num) {
21     Con *output, *workspace = NULL, *current;
22
23     /* TODO: could that look like this in the future?
24     GET_MATCHING_NODE(workspace, croot, strcasecmp(current->name, num) != 0);
25     */
26     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
27         TAILQ_FOREACH(current, &(output->nodes_head), nodes) {
28             if (strcasecmp(current->name, num) != 0)
29                     continue;
30
31             workspace = current;
32             break;
33         }
34     }
35
36     LOG("getting ws %s\n", num);
37     if (workspace == NULL) {
38         LOG("need to create this one\n");
39         output = con_get_output(focused);
40         LOG("got output %p\n", output);
41         workspace = con_new(output);
42         workspace->type = CT_WORKSPACE;
43         workspace->name = strdup(num);
44         workspace->orientation = HORIZ;
45
46         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
47     }
48
49     //ewmh_update_workarea();
50
51     return workspace;
52 }
53
54 #if 0
55
56 /*
57  * Sets the name (or just its number) for the given workspace. This has to
58  * be called for every workspace as the rendering function
59  * (render_internal_bar) relies on workspace->name and workspace->name_len
60  * being ready-to-use.
61  *
62  */
63 void workspace_set_name(Workspace *ws, const char *name) {
64         char *label;
65         int ret;
66
67         if (name != NULL)
68                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
69         else ret = asprintf(&label, "%d", ws->num + 1);
70
71         if (ret == -1)
72                 errx(1, "asprintf() failed");
73
74         FREE(ws->name);
75         FREE(ws->utf8_name);
76
77         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
78         if (config.font != NULL)
79                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
80         else ws->text_width = 0;
81         ws->utf8_name = label;
82 }
83
84 /*
85  * Returns true if the workspace is currently visible. Especially important for
86  * multi-monitor environments, as they can have multiple currenlty active
87  * workspaces.
88  *
89  */
90 bool workspace_is_visible(Workspace *ws) {
91         return (ws->output != NULL && ws->output->current_workspace == ws);
92 }
93 #endif
94
95
96 /*
97  * Switches to the given workspace
98  *
99  */
100 void workspace_show(const char *num) {
101     Con *workspace, *current, *old;
102
103     old = con_get_workspace(focused);
104
105     workspace = workspace_get(num);
106     if (workspace == old)
107         return;
108     workspace->fullscreen_mode = CF_OUTPUT;
109     /* disable fullscreen */
110     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes)
111         current->fullscreen_mode = CF_NONE;
112
113     LOG("switching to %p\n", workspace);
114     Con *next = workspace;
115
116     while (!TAILQ_EMPTY(&(next->focus_head)))
117         next = TAILQ_FIRST(&(next->focus_head));
118
119
120     if (TAILQ_EMPTY(&(old->nodes_head))) {
121         LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
122         tree_close(old, false);
123     }
124
125     con_focus(next);
126     workspace->fullscreen_mode = CF_OUTPUT;
127     LOG("focused now = %p / %s\n", focused, focused->name);
128 #if 0
129
130         /* Check if the workspace has not been used yet */
131         workspace_initialize(t_ws, c_ws->output, false);
132
133         if (c_ws->output != t_ws->output) {
134                 /* We need to switch to the other output first */
135                 DLOG("moving over to other output.\n");
136
137                 /* Store the old client */
138                 Client *old_client = CUR_CELL->currently_focused;
139
140                 c_ws = t_ws->output->current_workspace;
141                 current_col = c_ws->current_col;
142                 current_row = c_ws->current_row;
143                 if (CUR_CELL->currently_focused != NULL)
144                         need_warp = true;
145                 else {
146                         Rect *dims = &(c_ws->output->rect);
147                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
148                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
149                 }
150
151                 /* Re-decorate the old client, it’s not focused anymore */
152                 if ((old_client != NULL) && !old_client->dock)
153                         redecorate_window(conn, old_client);
154                 else xcb_flush(conn);
155
156                 /* We need to check if a global fullscreen-client is blocking
157                  * the t_ws and if necessary switch that to local fullscreen */
158                 Client* client = c_ws->fullscreen_client;
159                 if (client != NULL && client->workspace != c_ws) {
160                         if (c_ws->fullscreen_client->workspace != c_ws)
161                                 c_ws->fullscreen_client = NULL;
162                         client_enter_fullscreen(conn, client, false);
163                 }
164         }
165
166         /* Check if we need to change something or if we’re already there */
167         if (c_ws->output->current_workspace->num == (workspace-1)) {
168                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
169                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
170                         set_focus(conn, last_focused, true);
171                 if (need_warp) {
172                         client_warp_pointer_into(conn, last_focused);
173                         xcb_flush(conn);
174                 }
175
176                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
177
178                 return;
179         }
180
181         Workspace *old_workspace = c_ws;
182         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
183
184         /* Unmap all clients of the old workspace */
185         workspace_unmap_clients(conn, old_workspace);
186
187         current_row = c_ws->current_row;
188         current_col = c_ws->current_col;
189         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
190
191         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
192
193         workspace_map_clients(conn, c_ws);
194
195         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
196          * render_layout afterwards, there is a short flickering on the source
197          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
198          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
199          * flickering). */
200
201         /* Restore focus on the new workspace */
202         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
203         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
204                 set_focus(conn, last_focused, true);
205         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
206
207         render_layout(conn);
208
209         /* We can warp the pointer only after the window has been
210          * reconfigured in render_layout, otherwise the pointer will
211          * be warped to the old position, which will not work when we
212          * moved it to another output. */
213         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
214                 client_warp_pointer_into(conn, last_focused);
215                 xcb_flush(conn);
216         }
217 #endif
218 }
219
220 #if 0
221 /*
222  * Assigns the given workspace to the given output by correctly updating its
223  * state and reconfiguring all the clients on this workspace.
224  *
225  * This is called when initializing a output and when re-assigning it to a
226  * different output which just got available (if you configured it to be on
227  * output 1 and you just plugged in output 1).
228  *
229  */
230 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
231         Client *client;
232         bool empty = true;
233         bool visible = workspace_is_visible(ws);
234
235         ws->output = output;
236
237         /* Copy the dimensions from the virtual output */
238         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
239
240         ewmh_update_workarea();
241
242         /* Force reconfiguration for each client on that workspace */
243         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
244                 client->force_reconfigure = true;
245                 empty = false;
246         }
247
248         if (empty)
249                 return;
250
251         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
252         render_workspace(global_conn, output, ws);
253
254         /* …unless we want to see them at the moment, we should hide that workspace */
255         if (visible && !hide_it)
256                 return;
257
258         /* however, if this is the current workspace, we only need to adjust
259          * the output’s current_workspace pointer (and must not unmap the
260          * windows) */
261         if (c_ws == ws) {
262                 DLOG("Need to adjust output->current_workspace...\n");
263                 output->current_workspace = c_ws;
264                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
265                 return;
266         }
267
268         workspace_unmap_clients(global_conn, ws);
269 }
270
271 /*
272  * Initializes the given workspace if it is not already initialized. The given
273  * screen is to be understood as a fallback, if the workspace itself either
274  * was not assigned to a particular screen or cannot be placed there because
275  * the screen is not attached at the moment.
276  *
277  */
278 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
279         Output *old_output;
280
281         if (ws->output != NULL && !recheck) {
282                 DLOG("Workspace already initialized\n");
283                 return;
284         }
285
286         old_output = ws->output;
287
288         /* If this workspace has no preferred output or if the output it wants
289          * to be on is not available at the moment, we initialize it with
290          * the output which was given */
291         if (ws->preferred_output == NULL ||
292             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
293                 ws->output = output;
294
295         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
296         /* If the assignment did not change, we do not need to update anything */
297         if (old_output != NULL && ws->output == old_output)
298                 return;
299
300         workspace_assign_to(ws, ws->output, false);
301 }
302
303 /*
304  * Gets the first unused workspace for the given screen, taking into account
305  * the preferred_output setting of every workspace (workspace assignments).
306  *
307  */
308 Workspace *get_first_workspace_for_output(Output *output) {
309         Workspace *result = NULL;
310
311         Workspace *ws;
312         TAILQ_FOREACH(ws, workspaces, workspaces) {
313                 if (ws->preferred_output == NULL ||
314                     get_output_by_name(ws->preferred_output) != output)
315                         continue;
316
317                 result = ws;
318                 break;
319         }
320
321         if (result == NULL) {
322                 /* No assignment found, returning first unused workspace */
323                 TAILQ_FOREACH(ws, workspaces, workspaces) {
324                         if (ws->output != NULL)
325                                 continue;
326
327                         result = ws;
328                         break;
329                 }
330         }
331
332         if (result == NULL) {
333                 DLOG("No existing free workspace found to assign, creating a new one\n");
334
335                 int last_ws = 0;
336                 TAILQ_FOREACH(ws, workspaces, workspaces)
337                         last_ws = ws->num;
338                 result = workspace_get(last_ws + 1);
339         }
340
341         workspace_initialize(result, output, false);
342         return result;
343 }
344
345 /*
346  * Maps all clients (and stack windows) of the given workspace.
347  *
348  */
349 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
350         Client *client;
351
352         ignore_enter_notify_forall(conn, ws, true);
353
354         /* Map all clients on the new workspace */
355         FOR_TABLE(ws)
356                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
357                         client_map(conn, client);
358
359         /* Map all floating clients */
360         if (!ws->floating_hidden)
361                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
362                         client_map(conn, client);
363
364         /* Map all stack windows, if any */
365         struct Stack_Window *stack_win;
366         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
367                 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
368                         xcb_map_window(conn, stack_win->window);
369
370         ignore_enter_notify_forall(conn, ws, false);
371 }
372
373 /*
374  * Unmaps all clients (and stack windows) of the given workspace.
375  *
376  * This needs to be called separately when temporarily rendering
377  * a workspace which is not the active workspace to force
378  * reconfiguration of all clients, like in src/xinerama.c when
379  * re-assigning a workspace to another screen.
380  *
381  */
382 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
383         Client *client;
384         struct Stack_Window *stack_win;
385
386         /* Ignore notify events because they would cause focus to be changed */
387         ignore_enter_notify_forall(conn, u_ws, true);
388
389         /* Unmap all clients of the given workspace */
390         int unmapped_clients = 0;
391         FOR_TABLE(u_ws)
392                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
393                         DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
394                         client_unmap(conn, client);
395                         unmapped_clients++;
396                 }
397
398         /* To find floating clients, we traverse the focus stack */
399         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
400                 if (!client_is_floating(client))
401                         continue;
402
403                 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
404
405                 client_unmap(conn, client);
406                 unmapped_clients++;
407         }
408
409         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
410          * if it is not the current workspace. */
411         if (unmapped_clients == 0 && u_ws != c_ws) {
412                 /* Re-assign the workspace of all dock clients which use this workspace */
413                 Client *dock;
414                 DLOG("workspace %p is empty\n", u_ws);
415                 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
416                         if (dock->workspace != u_ws)
417                                 continue;
418
419                         DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
420                         dock->workspace = c_ws;
421                 }
422                 u_ws->output = NULL;
423         }
424
425         /* Unmap the stack windows on the given workspace, if any */
426         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
427                 if (stack_win->container->workspace == u_ws)
428                         xcb_unmap_window(conn, stack_win->window);
429
430         ignore_enter_notify_forall(conn, u_ws, false);
431 }
432 #endif
433
434 static bool get_urgency_flag(Con *con) {
435     Con *child;
436     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
437         if (child->urgent || get_urgency_flag(child))
438             return true;
439
440     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
441         if (child->urgent || get_urgency_flag(child))
442             return true;
443
444     return false;
445 }
446
447 /*
448  * Goes through all clients on the given workspace and updates the workspace’s
449  * urgent flag accordingly.
450  *
451  */
452 void workspace_update_urgent_flag(Con *ws) {
453     bool old_flag = ws->urgent;
454     ws->urgent = get_urgency_flag(ws);
455     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
456
457     if (old_flag != ws->urgent)
458         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
459 }
460
461 #if 0
462
463 /*
464  * Returns the width of the workspace.
465  *
466  */
467 int workspace_width(Workspace *ws) {
468         return ws->rect.width;
469 }
470
471 /*
472  * Returns the effective height of the workspace (without the internal bar and
473  * without dock clients).
474  *
475  */
476 int workspace_height(Workspace *ws) {
477         int height = ws->rect.height;
478         i3Font *font = load_font(global_conn, config.font);
479
480         /* Reserve space for dock clients */
481         Client *client;
482         SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
483                 height -= client->desired_height;
484
485         /* Space for the internal bar */
486         if (!config.disable_workspace_bar)
487                 height -= (font->height + 6);
488
489         return height;
490 }
491 #endif