]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
cmdparse: don’t allow empty operations
[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, bool *created) {
21     Con *output, *workspace = NULL, *child;
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(child, &(output_get_content(output)->nodes_head), nodes) {
28             if (strcasecmp(child->name, num) != 0)
29                 continue;
30
31             workspace = child;
32             break;
33         }
34
35     LOG("getting ws %s\n", num);
36     if (workspace == NULL) {
37         LOG("need to create this one\n");
38         output = con_get_output(focused);
39         Con *content = output_get_content(output);
40         LOG("got output %p with content %p\n", output, content);
41         /* We need to attach this container after setting its type. con_attach
42          * will handle CT_WORKSPACEs differently */
43         workspace = con_new(NULL);
44         char *name;
45         asprintf(&name, "[i3 con] workspace %s", num);
46         x_set_name(workspace, name);
47         free(name);
48         workspace->type = CT_WORKSPACE;
49         FREE(workspace->name);
50         workspace->name = sstrdup(num);
51         /* We set ->num to the number if this workspace’s name consists only of
52          * a positive number. Otherwise it’s a named ws and num will be -1. */
53         char *end;
54         long parsed_num = strtol(num, &end, 10);
55         if (parsed_num == LONG_MIN ||
56             parsed_num == LONG_MAX ||
57             parsed_num < 0 ||
58             (end && *end != '\0'))
59             workspace->num = -1;
60         else workspace->num = parsed_num;
61         LOG("num = %d\n", workspace->num);
62         workspace->orientation = HORIZ;
63         con_attach(workspace, content, false);
64
65         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
66         if (created != NULL)
67             *created = true;
68     }
69     else if (created != NULL) {
70         *created = false;
71     }
72
73     return workspace;
74 }
75
76 #if 0
77
78 /*
79  * Sets the name (or just its number) for the given workspace. This has to
80  * be called for every workspace as the rendering function
81  * (render_internal_bar) relies on workspace->name and workspace->name_len
82  * being ready-to-use.
83  *
84  */
85 void workspace_set_name(Workspace *ws, const char *name) {
86         char *label;
87         int ret;
88
89         if (name != NULL)
90                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
91         else ret = asprintf(&label, "%d", ws->num + 1);
92
93         if (ret == -1)
94                 errx(1, "asprintf() failed");
95
96         FREE(ws->name);
97         FREE(ws->utf8_name);
98
99         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
100         if (config.font != NULL)
101                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
102         else ws->text_width = 0;
103         ws->utf8_name = label;
104 }
105 #endif
106
107 /*
108  * Returns true if the workspace is currently visible. Especially important for
109  * multi-monitor environments, as they can have multiple currenlty active
110  * workspaces.
111  *
112  */
113 bool workspace_is_visible(Con *ws) {
114     Con *output = con_get_output(ws);
115     if (output == NULL)
116         return false;
117     Con *fs = con_get_fullscreen_con(output);
118     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
119     return (fs == ws);
120 }
121
122 /*
123  * XXX: we need to clean up all this recursive walking code.
124  *
125  */
126 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
127     Con *current;
128
129     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
130         if (current != exclude &&
131             current->sticky_group != NULL &&
132             current->window != NULL &&
133             strcmp(current->sticky_group, sticky_group) == 0)
134             return current;
135
136         Con *recurse = _get_sticky(current, sticky_group, exclude);
137         if (recurse != NULL)
138             return recurse;
139     }
140
141     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
142         if (current != exclude &&
143             current->sticky_group != NULL &&
144             current->window != NULL &&
145             strcmp(current->sticky_group, sticky_group) == 0)
146             return current;
147
148         Con *recurse = _get_sticky(current, sticky_group, exclude);
149         if (recurse != NULL)
150             return recurse;
151     }
152
153     return NULL;
154 }
155
156 /*
157  * Reassigns all child windows in sticky containers. Called when the user
158  * changes workspaces.
159  *
160  * XXX: what about sticky containers which contain containers?
161  *
162  */
163 static void workspace_reassign_sticky(Con *con) {
164     Con *current;
165     /* 1: go through all containers */
166
167     /* handle all children and floating windows of this node */
168     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
169         if (current->sticky_group == NULL) {
170             workspace_reassign_sticky(current);
171             continue;
172         }
173
174         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
175         /* 2: find a window which we can re-assign */
176         Con *output = con_get_output(current);
177         Con *src = _get_sticky(output, current->sticky_group, current);
178
179         if (src == NULL) {
180             LOG("No window found for this sticky group\n");
181             workspace_reassign_sticky(current);
182             continue;
183         }
184
185         x_move_win(src, current);
186         current->window = src->window;
187         current->mapped = true;
188         src->window = NULL;
189         src->mapped = false;
190
191         x_reparent_child(current, src);
192
193         LOG("re-assigned window from src %p to dest %p\n", src, current);
194     }
195
196     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
197         workspace_reassign_sticky(current);
198 }
199
200 /*
201  * Switches to the given workspace
202  *
203  */
204 void workspace_show(const char *num) {
205     Con *workspace, *current, *old = NULL;
206
207     bool changed_num_workspaces;
208     workspace = workspace_get(num, &changed_num_workspaces);
209
210     /* disable fullscreen for the other workspaces and get the workspace we are
211      * currently on. */
212     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
213         if (current->fullscreen_mode == CF_OUTPUT)
214             old = current;
215         current->fullscreen_mode = CF_NONE;
216     }
217     assert(old != NULL);
218
219     /* Check if the the currently focused con is on the same Output as the
220      * workspace we chose as 'old'. If not, use the workspace of the currently
221      * focused con */
222     Con *ws = con_get_workspace(focused);
223     if (ws && ws->parent != old->parent)
224         old = ws;
225
226     /* enable fullscreen for the target workspace. If it happens to be the
227      * same one we are currently on anyways, we can stop here. */
228     workspace->fullscreen_mode = CF_OUTPUT;
229     if (workspace == old)
230         return;
231
232     workspace_reassign_sticky(workspace);
233
234     LOG("switching to %p\n", workspace);
235     Con *next = con_descend_focused(workspace);
236
237     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
238         /* check if this workspace is currently visible */
239         if (!workspace_is_visible(old)) {
240             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
241             tree_close(old, false, false);
242             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
243             changed_num_workspaces = true;
244         }
245     }
246
247     con_focus(next);
248     workspace->fullscreen_mode = CF_OUTPUT;
249     LOG("focused now = %p / %s\n", focused, focused->name);
250
251     /* Update the EWMH hints */
252     if (changed_num_workspaces)
253         ewmh_update_workarea();
254     ewmh_update_current_desktop();
255
256     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
257 #if 0
258
259         /* Check if the workspace has not been used yet */
260         workspace_initialize(t_ws, c_ws->output, false);
261
262         if (c_ws->output != t_ws->output) {
263                 /* We need to switch to the other output first */
264                 DLOG("moving over to other output.\n");
265
266                 /* Store the old client */
267                 Client *old_client = CUR_CELL->currently_focused;
268
269                 c_ws = t_ws->output->current_workspace;
270                 current_col = c_ws->current_col;
271                 current_row = c_ws->current_row;
272                 if (CUR_CELL->currently_focused != NULL)
273                         need_warp = true;
274                 else {
275                         Rect *dims = &(c_ws->output->rect);
276                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
277                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
278                 }
279
280                 /* Re-decorate the old client, it’s not focused anymore */
281                 if ((old_client != NULL) && !old_client->dock)
282                         redecorate_window(conn, old_client);
283                 else xcb_flush(conn);
284
285                 /* We need to check if a global fullscreen-client is blocking
286                  * the t_ws and if necessary switch that to local fullscreen */
287                 Client* client = c_ws->fullscreen_client;
288                 if (client != NULL && client->workspace != c_ws) {
289                         if (c_ws->fullscreen_client->workspace != c_ws)
290                                 c_ws->fullscreen_client = NULL;
291                         client_enter_fullscreen(conn, client, false);
292                 }
293         }
294
295         /* Check if we need to change something or if we’re already there */
296         if (c_ws->output->current_workspace->num == (workspace-1)) {
297                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
298                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
299                         set_focus(conn, last_focused, true);
300                 if (need_warp) {
301                         client_warp_pointer_into(conn, last_focused);
302                         xcb_flush(conn);
303                 }
304
305                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
306
307                 return;
308         }
309
310         Workspace *old_workspace = c_ws;
311         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
312
313         /* Unmap all clients of the old workspace */
314         workspace_unmap_clients(conn, old_workspace);
315
316         current_row = c_ws->current_row;
317         current_col = c_ws->current_col;
318         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
319
320         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
321
322         workspace_map_clients(conn, c_ws);
323
324         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
325          * render_layout afterwards, there is a short flickering on the source
326          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
327          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
328          * flickering). */
329
330         /* Restore focus on the new workspace */
331         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
332         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
333                 set_focus(conn, last_focused, true);
334         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
335
336         render_layout(conn);
337
338         /* We can warp the pointer only after the window has been
339          * reconfigured in render_layout, otherwise the pointer will
340          * be warped to the old position, which will not work when we
341          * moved it to another output. */
342         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
343                 client_warp_pointer_into(conn, last_focused);
344                 xcb_flush(conn);
345         }
346 #endif
347 }
348
349 #if 0
350 /*
351  * Assigns the given workspace to the given output by correctly updating its
352  * state and reconfiguring all the clients on this workspace.
353  *
354  * This is called when initializing a output and when re-assigning it to a
355  * different output which just got available (if you configured it to be on
356  * output 1 and you just plugged in output 1).
357  *
358  */
359 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
360         Client *client;
361         bool empty = true;
362         bool visible = workspace_is_visible(ws);
363
364         ws->output = output;
365
366         /* Copy the dimensions from the virtual output */
367         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
368
369         ewmh_update_workarea();
370
371         /* Force reconfiguration for each client on that workspace */
372         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
373                 client->force_reconfigure = true;
374                 empty = false;
375         }
376
377         if (empty)
378                 return;
379
380         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
381         render_workspace(global_conn, output, ws);
382
383         /* …unless we want to see them at the moment, we should hide that workspace */
384         if (visible && !hide_it)
385                 return;
386
387         /* however, if this is the current workspace, we only need to adjust
388          * the output’s current_workspace pointer (and must not unmap the
389          * windows) */
390         if (c_ws == ws) {
391                 DLOG("Need to adjust output->current_workspace...\n");
392                 output->current_workspace = c_ws;
393                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
394                 return;
395         }
396
397         workspace_unmap_clients(global_conn, ws);
398 }
399
400 /*
401  * Initializes the given workspace if it is not already initialized. The given
402  * screen is to be understood as a fallback, if the workspace itself either
403  * was not assigned to a particular screen or cannot be placed there because
404  * the screen is not attached at the moment.
405  *
406  */
407 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
408         Output *old_output;
409
410         if (ws->output != NULL && !recheck) {
411                 DLOG("Workspace already initialized\n");
412                 return;
413         }
414
415         old_output = ws->output;
416
417         /* If this workspace has no preferred output or if the output it wants
418          * to be on is not available at the moment, we initialize it with
419          * the output which was given */
420         if (ws->preferred_output == NULL ||
421             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
422                 ws->output = output;
423
424         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
425         /* If the assignment did not change, we do not need to update anything */
426         if (old_output != NULL && ws->output == old_output)
427                 return;
428
429         workspace_assign_to(ws, ws->output, false);
430 }
431
432 /*
433  * Gets the first unused workspace for the given screen, taking into account
434  * the preferred_output setting of every workspace (workspace assignments).
435  *
436  */
437 Workspace *get_first_workspace_for_output(Output *output) {
438         Workspace *result = NULL;
439
440         Workspace *ws;
441         TAILQ_FOREACH(ws, workspaces, workspaces) {
442                 if (ws->preferred_output == NULL ||
443                     get_output_by_name(ws->preferred_output) != output)
444                         continue;
445
446                 result = ws;
447                 break;
448         }
449
450         if (result == NULL) {
451                 /* No assignment found, returning first unused workspace */
452                 TAILQ_FOREACH(ws, workspaces, workspaces) {
453                         if (ws->output != NULL)
454                                 continue;
455
456                         result = ws;
457                         break;
458                 }
459         }
460
461         if (result == NULL) {
462                 DLOG("No existing free workspace found to assign, creating a new one\n");
463
464                 int last_ws = 0;
465                 TAILQ_FOREACH(ws, workspaces, workspaces)
466                         last_ws = ws->num;
467                 result = workspace_get(last_ws + 1, NULL);
468         }
469
470         workspace_initialize(result, output, false);
471         return result;
472 }
473
474 #endif
475
476 static bool get_urgency_flag(Con *con) {
477     Con *child;
478     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
479         if (child->urgent || get_urgency_flag(child))
480             return true;
481
482     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
483         if (child->urgent || get_urgency_flag(child))
484             return true;
485
486     return false;
487 }
488
489 /*
490  * Goes through all clients on the given workspace and updates the workspace’s
491  * urgent flag accordingly.
492  *
493  */
494 void workspace_update_urgent_flag(Con *ws) {
495     bool old_flag = ws->urgent;
496     ws->urgent = get_urgency_flag(ws);
497     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
498
499     if (old_flag != ws->urgent)
500         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
501 }
502
503 /*
504  * 'Forces' workspace orientation by moving all cons into a new split-con with
505  * the same orientation as the workspace and then changing the workspace
506  * orientation.
507  *
508  */
509 void ws_force_orientation(Con *ws, orientation_t orientation) {
510     /* 1: create a new split container */
511     Con *split = con_new(NULL);
512     split->parent = ws;
513
514     /* 2: copy layout and orientation from workspace */
515     split->layout = ws->layout;
516     split->orientation = ws->orientation;
517
518     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
519
520     /* 3: move the existing cons of this workspace below the new con */
521     DLOG("Moving cons\n");
522     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
523         Con *child = TAILQ_FIRST(&(ws->nodes_head));
524         con_detach(child);
525         con_attach(child, split, true);
526     }
527
528     /* 4: switch workspace orientation */
529     ws->orientation = orientation;
530
531     /* 5: attach the new split container to the workspace */
532     DLOG("Attaching new split to ws\n");
533     con_attach(split, ws, false);
534
535     /* 6: fix the percentages */
536     con_fix_percent(ws);
537
538     if (old_focused)
539         con_focus(old_focused);
540 }