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);
124 t_ws->screen->current_workspace = workspace-1;
125 Workspace *old_workspace = c_ws;
126 c_ws = &workspaces[workspace-1];
128 /* Unmap all clients of the old workspace */
129 workspace_unmap_clients(conn, old_workspace);
131 current_row = c_ws->current_row;
132 current_col = c_ws->current_col;
133 LOG("new current row = %d, current col = %d\n", current_row, current_col);
135 workspace_map_clients(conn, c_ws);
137 /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
138 * render_layout afterwards, there is a short flickering on the source
139 * workspace (assign ws 3 to screen 0, ws 4 to screen 1, create single
140 * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
143 /* Restore focus on the new workspace */
144 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
145 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
146 set_focus(conn, last_focused, true);
147 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
151 /* We can warp the pointer only after the window has been
152 * reconfigured in render_layout, otherwise the pointer will
153 * be warped to the old position, which will not work when we
154 * moved it to another screen. */
155 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
156 client_warp_pointer_into(conn, last_focused);
163 * Parses the preferred_screen property of a workspace. You can either specify
164 * the screen number (it is not given that the screen numbering always stays
165 * the same) or the screen coordinates (exact coordinates, e.g. 1280 will match
166 * the screen starting at x=1280, but 1281 will not). For coordinates, you can
167 * either specify an x coordinate ("1280") or an y coordinate ("x800") or both
171 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
174 int preferred_screen = strtol(preference, &rest, 10);
176 LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
178 if ((rest == preference) || (preferred_screen >= num_screens)) {
179 int x = INT_MAX, y = INT_MAX;
180 if (strchr(preference, 'x') != NULL) {
181 /* Check if only the y coordinate was specified */
182 if (*preference == 'x')
183 y = atoi(preference+1);
185 x = atoi(preference);
186 y = atoi(strchr(preference, 'x') + 1);
189 x = atoi(preference);
192 LOG("Looking for screen at %d x %d\n", x, y);
194 TAILQ_FOREACH(screen, slist, screens)
195 if ((x == INT_MAX || screen->rect.x == x) &&
196 (y == INT_MAX || screen->rect.y == y)) {
197 LOG("found %p\n", screen);
205 TAILQ_FOREACH(screen, slist, screens)
206 if (c++ == preferred_screen)
214 * Initializes the given workspace if it is not already initialized. The given
215 * screen is to be understood as a fallback, if the workspace itself either
216 * was not assigned to a particular screen or cannot be placed there because
217 * the screen is not attached at the moment.
220 void workspace_initialize(Workspace *ws, i3Screen *screen) {
221 if (ws->screen != NULL) {
222 LOG("Workspace already initialized\n");
226 /* If this workspace has no preferred screen or if the screen it wants
227 * to be on is not available at the moment, we initialize it with
228 * the screen which was given */
229 if (ws->preferred_screen == NULL ||
230 (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
232 else { LOG("yay, found assignment\n"); }
234 /* Copy the dimensions from the virtual screen */
235 memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
239 * Gets the first unused workspace for the given screen, taking into account
240 * the preferred_screen setting of every workspace (workspace assignments).
243 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
244 Workspace *result = NULL;
246 for (int c = 0; c < 10; c++) {
247 Workspace *ws = &(workspaces[c]);
248 if (ws->preferred_screen == NULL ||
249 !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
256 if (result == NULL) {
257 /* No assignment found, returning first unused workspace */
258 for (int c = 0; c < 10; c++) {
259 if (workspaces[c].screen != NULL)
262 result = &(workspaces[c]);
267 if (result != NULL) {
268 workspace_initialize(result, screen);
272 LOG("WARNING: No free workspace found to assign!\n");
277 * Maps all clients (and stack windows) of the given workspace.
280 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
283 ignore_enter_notify_forall(conn, ws, true);
285 /* Map all clients on the new workspace */
287 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
288 client_map(conn, client);
290 /* Map all floating clients */
291 if (!ws->floating_hidden)
292 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
293 client_map(conn, client);
295 /* Map all stack windows, if any */
296 struct Stack_Window *stack_win;
297 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
298 if (stack_win->container->workspace == ws)
299 xcb_map_window(conn, stack_win->window);
301 ignore_enter_notify_forall(conn, ws, false);
305 * Unmaps all clients (and stack windows) of the given workspace.
307 * This needs to be called separately when temporarily rendering
308 * a workspace which is not the active workspace to force
309 * reconfiguration of all clients, like in src/xinerama.c when
310 * re-assigning a workspace to another screen.
313 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
315 struct Stack_Window *stack_win;
317 /* Ignore notify events because they would cause focus to be changed */
318 ignore_enter_notify_forall(conn, u_ws, true);
320 /* Unmap all clients of the given workspace */
321 int unmapped_clients = 0;
323 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
324 LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
325 client_unmap(conn, client);
329 /* To find floating clients, we traverse the focus stack */
330 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
331 if (!client_is_floating(client))
334 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
336 client_unmap(conn, client);
340 /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
341 * if it is not the current workspace. */
342 if (unmapped_clients == 0 && u_ws != c_ws) {
343 /* Re-assign the workspace of all dock clients which use this workspace */
345 LOG("workspace %p is empty\n", u_ws);
346 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
347 if (dock->workspace != u_ws)
350 LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
351 dock->workspace = c_ws;
356 /* Unmap the stack windows on the given workspace, if any */
357 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
358 if (stack_win->container->workspace == u_ws)
359 xcb_unmap_window(conn, stack_win->window);
361 ignore_enter_notify_forall(conn, u_ws, false);