4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * workspace.c: Functions for modifying workspaces
27 #include "workspace.h"
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
37 void workspace_set_name(Workspace *ws, const char *name) {
42 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
43 else ret = asprintf(&label, "%d", ws->num + 1);
46 errx(1, "asprintf() failed");
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;
59 * Returns true if the workspace is currently visible. Especially important for
60 * multi-monitor environments, as they can have multiple currenlty active
64 bool workspace_is_visible(Workspace *ws) {
65 return (ws->screen->current_workspace == ws->num);
69 * Switches to the given workspace
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]);
78 LOG("show_workspace(%d)\n", workspace);
80 /* Store current_row/current_col */
81 c_ws->current_row = current_row;
82 c_ws->current_col = current_col;
84 /* Check if the workspace has not been used yet */
85 workspace_initialize(t_ws, c_ws->screen);
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");
91 /* Store the old client */
92 Client *old_client = CUR_CELL->currently_focused;
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)
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));
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);
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);
117 client_warp_pointer_into(conn, last_focused);
125 t_ws->screen->current_workspace = workspace-1;
126 Workspace *old_workspace = c_ws;
127 c_ws = &workspaces[workspace-1];
129 /* Unmap all clients of the old workspace */
130 workspace_unmap_clients(conn, old_workspace);
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);
136 workspace_map_clients(conn, c_ws);
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);
143 client_warp_pointer_into(conn, last_focused);
146 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
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
161 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
164 int preferred_screen = strtol(preference, &rest, 10);
166 LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
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);
175 x = atoi(preference);
176 y = atoi(strchr(preference, 'x') + 1);
179 x = atoi(preference);
182 LOG("Looking for screen at %d x %d\n", x, y);
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);
195 TAILQ_FOREACH(screen, slist, screens)
196 if (c++ == preferred_screen)
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.
210 void workspace_initialize(Workspace *ws, i3Screen *screen) {
211 if (ws->screen != NULL) {
212 LOG("Workspace already initialized\n");
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)
222 else { LOG("yay, found assignment\n"); }
224 /* Copy the dimensions from the virtual screen */
225 memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
229 * Gets the first unused workspace for the given screen, taking into account
230 * the preferred_screen setting of every workspace (workspace assignments).
233 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
234 Workspace *result = NULL;
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))
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)
252 result = &(workspaces[c]);
257 if (result != NULL) {
258 workspace_initialize(result, screen);
262 LOG("WARNING: No free workspace found to assign!\n");
267 * Maps all clients (and stack windows) of the given workspace.
270 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
273 ignore_enter_notify_forall(conn, ws, true);
275 /* Map all clients on the new workspace */
277 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
278 xcb_map_window(conn, client->frame);
280 /* Map all floating clients */
281 if (!ws->floating_hidden)
282 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
283 xcb_map_window(conn, client->frame);
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);
291 ignore_enter_notify_forall(conn, ws, false);
295 * Unmaps all clients (and stack windows) of the given workspace.
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.
303 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
305 struct Stack_Window *stack_win;
307 /* Ignore notify events because they would cause focus to be changed */
308 ignore_enter_notify_forall(conn, u_ws, true);
310 /* Unmap all clients of the given workspace */
311 int unmapped_clients = 0;
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 xcb_unmap_window(conn, client->frame);
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))
324 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
326 xcb_unmap_window(conn, client->frame);
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 */
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)
340 LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
341 dock->workspace = c_ws;
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);
351 ignore_enter_notify_forall(conn, u_ws, false);