• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

src/workspace.c

Go to the documentation of this file.
00001 /*
00002  * vim:ts=8:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  *
00006  * © 2009-2010 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  * workspace.c: Functions for modifying workspaces
00011  *
00012  */
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <limits.h>
00017 #include <err.h>
00018 
00019 #include "util.h"
00020 #include "data.h"
00021 #include "i3.h"
00022 #include "config.h"
00023 #include "xcb.h"
00024 #include "table.h"
00025 #include "randr.h"
00026 #include "layout.h"
00027 #include "workspace.h"
00028 #include "client.h"
00029 #include "log.h"
00030 #include "ewmh.h"
00031 #include "ipc.h"
00032 
00033 /*
00034  * Returns a pointer to the workspace with the given number (starting at 0),
00035  * creating the workspace if necessary (by allocating the necessary amount of
00036  * memory and initializing the data structures correctly).
00037  *
00038  */
00039 Workspace *workspace_get(int number) {
00040         Workspace *ws = NULL;
00041         TAILQ_FOREACH(ws, workspaces, workspaces)
00042                 if (ws->num == number)
00043                         return ws;
00044 
00045         /* If we are still there, we could not find the requested workspace. */
00046         int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
00047 
00048         DLOG("We need to initialize that one, last ws = %d\n", last_ws);
00049 
00050         for (int c = last_ws; c < number; c++) {
00051                 DLOG("Creating new ws\n");
00052 
00053                 ws = scalloc(sizeof(Workspace));
00054                 ws->num = c+1;
00055                 TAILQ_INIT(&(ws->floating_clients));
00056                 expand_table_cols(ws);
00057                 expand_table_rows(ws);
00058                 workspace_set_name(ws, NULL);
00059 
00060                 TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
00061 
00062                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
00063         }
00064         DLOG("done\n");
00065 
00066         ewmh_update_workarea();
00067 
00068         return ws;
00069 }
00070 
00071 /*
00072  * Sets the name (or just its number) for the given workspace. This has to
00073  * be called for every workspace as the rendering function
00074  * (render_internal_bar) relies on workspace->name and workspace->name_len
00075  * being ready-to-use.
00076  *
00077  */
00078 void workspace_set_name(Workspace *ws, const char *name) {
00079         char *label;
00080         int ret;
00081 
00082         if (name != NULL)
00083                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
00084         else ret = asprintf(&label, "%d", ws->num + 1);
00085 
00086         if (ret == -1)
00087                 errx(1, "asprintf() failed");
00088 
00089         FREE(ws->name);
00090         FREE(ws->utf8_name);
00091 
00092         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
00093         if (config.font != NULL)
00094                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
00095         else ws->text_width = 0;
00096         ws->utf8_name = label;
00097 }
00098 
00099 /*
00100  * Returns true if the workspace is currently visible. Especially important for
00101  * multi-monitor environments, as they can have multiple currenlty active
00102  * workspaces.
00103  *
00104  */
00105 bool workspace_is_visible(Workspace *ws) {
00106         return (ws->output != NULL && ws->output->current_workspace == ws);
00107 }
00108 
00109 /*
00110  * Switches to the given workspace
00111  *
00112  */
00113 void workspace_show(xcb_connection_t *conn, int workspace) {
00114         bool need_warp = false;
00115         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
00116         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
00117         Workspace *t_ws = workspace_get(workspace-1);
00118 
00119         DLOG("show_workspace(%d)\n", workspace);
00120 
00121         /* Store current_row/current_col */
00122         c_ws->current_row = current_row;
00123         c_ws->current_col = current_col;
00124 
00125         /* Check if the workspace has not been used yet */
00126         workspace_initialize(t_ws, c_ws->output, false);
00127 
00128         if (c_ws->output != t_ws->output) {
00129                 /* We need to switch to the other output first */
00130                 DLOG("moving over to other output.\n");
00131 
00132                 /* Store the old client */
00133                 Client *old_client = CUR_CELL->currently_focused;
00134 
00135                 c_ws = t_ws->output->current_workspace;
00136                 current_col = c_ws->current_col;
00137                 current_row = c_ws->current_row;
00138                 if (CUR_CELL->currently_focused != NULL)
00139                         need_warp = true;
00140                 else {
00141                         Rect *dims = &(c_ws->output->rect);
00142                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
00143                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
00144                 }
00145 
00146                 /* Re-decorate the old client, it’s not focused anymore */
00147                 if ((old_client != NULL) && !old_client->dock)
00148                         redecorate_window(conn, old_client);
00149                 else xcb_flush(conn);
00150 
00151                 /* We need to check if a global fullscreen-client is blocking
00152                  * the t_ws and if necessary switch that to local fullscreen */
00153                 Client* client = c_ws->fullscreen_client;
00154                 if (client != NULL && client->workspace != c_ws) {
00155                         if (c_ws->fullscreen_client->workspace != c_ws)
00156                                 c_ws->fullscreen_client = NULL;
00157                         client_enter_fullscreen(conn, client, false);
00158                 }
00159         }
00160 
00161         /* Check if we need to change something or if we’re already there */
00162         if (c_ws->output->current_workspace->num == (workspace-1)) {
00163                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
00164                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
00165                         set_focus(conn, last_focused, true);
00166                 if (need_warp) {
00167                         client_warp_pointer_into(conn, last_focused);
00168                         xcb_flush(conn);
00169                 }
00170 
00171                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00172 
00173                 return;
00174         }
00175 
00176         Workspace *old_workspace = c_ws;
00177         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
00178 
00179         /* Unmap all clients of the old workspace */
00180         workspace_unmap_clients(conn, old_workspace);
00181 
00182         current_row = c_ws->current_row;
00183         current_col = c_ws->current_col;
00184         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
00185 
00186         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00187 
00188         workspace_map_clients(conn, c_ws);
00189 
00190         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
00191          * render_layout afterwards, there is a short flickering on the source
00192          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
00193          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
00194          * flickering). */
00195 
00196         /* Restore focus on the new workspace */
00197         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
00198         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
00199                 set_focus(conn, last_focused, true);
00200         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
00201 
00202         render_layout(conn);
00203 
00204         /* We can warp the pointer only after the window has been
00205          * reconfigured in render_layout, otherwise the pointer will
00206          * be warped to the old position, which will not work when we
00207          * moved it to another output. */
00208         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
00209                 client_warp_pointer_into(conn, last_focused);
00210                 xcb_flush(conn);
00211         }
00212 }
00213 
00214 /*
00215  * Assigns the given workspace to the given output by correctly updating its
00216  * state and reconfiguring all the clients on this workspace.
00217  *
00218  * This is called when initializing a output and when re-assigning it to a
00219  * different output which just got available (if you configured it to be on
00220  * output 1 and you just plugged in output 1).
00221  *
00222  */
00223 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
00224         Client *client;
00225         bool empty = true;
00226         bool visible = workspace_is_visible(ws);
00227 
00228         ws->output = output;
00229 
00230         /* Copy the dimensions from the virtual output */
00231         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
00232 
00233         ewmh_update_workarea();
00234 
00235         /* Force reconfiguration for each client on that workspace */
00236         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
00237                 client->force_reconfigure = true;
00238                 empty = false;
00239         }
00240 
00241         if (empty)
00242                 return;
00243 
00244         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
00245         render_workspace(global_conn, output, ws);
00246 
00247         /* …unless we want to see them at the moment, we should hide that workspace */
00248         if (visible && !hide_it)
00249                 return;
00250 
00251         /* however, if this is the current workspace, we only need to adjust
00252          * the output’s current_workspace pointer (and must not unmap the
00253          * windows) */
00254         if (c_ws == ws) {
00255                 DLOG("Need to adjust output->current_workspace...\n");
00256                 output->current_workspace = c_ws;
00257                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00258                 return;
00259         }
00260 
00261         workspace_unmap_clients(global_conn, ws);
00262 }
00263 
00264 /*
00265  * Initializes the given workspace if it is not already initialized. The given
00266  * screen is to be understood as a fallback, if the workspace itself either
00267  * was not assigned to a particular screen or cannot be placed there because
00268  * the screen is not attached at the moment.
00269  *
00270  */
00271 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
00272         Output *old_output;
00273 
00274         if (ws->output != NULL && !recheck) {
00275                 DLOG("Workspace already initialized\n");
00276                 return;
00277         }
00278 
00279         old_output = ws->output;
00280 
00281         /* If this workspace has no preferred output or if the output it wants
00282          * to be on is not available at the moment, we initialize it with
00283          * the output which was given */
00284         if (ws->preferred_output == NULL ||
00285             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
00286                 ws->output = output;
00287 
00288         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
00289         /* If the assignment did not change, we do not need to update anything */
00290         if (old_output != NULL && ws->output == old_output)
00291                 return;
00292 
00293         workspace_assign_to(ws, ws->output, false);
00294 }
00295 
00296 /*
00297  * Gets the first unused workspace for the given screen, taking into account
00298  * the preferred_output setting of every workspace (workspace assignments).
00299  *
00300  */
00301 Workspace *get_first_workspace_for_output(Output *output) {
00302         Workspace *result = NULL;
00303 
00304         Workspace *ws;
00305         TAILQ_FOREACH(ws, workspaces, workspaces) {
00306                 if (ws->preferred_output == NULL ||
00307                     get_output_by_name(ws->preferred_output) != output)
00308                         continue;
00309 
00310                 result = ws;
00311                 break;
00312         }
00313 
00314         if (result == NULL) {
00315                 /* No assignment found, returning first unused workspace */
00316                 TAILQ_FOREACH(ws, workspaces, workspaces) {
00317                         if (ws->output != NULL)
00318                                 continue;
00319 
00320                         result = ws;
00321                         break;
00322                 }
00323         }
00324 
00325         if (result == NULL) {
00326                 DLOG("No existing free workspace found to assign, creating a new one\n");
00327 
00328                 int last_ws = 0;
00329                 TAILQ_FOREACH(ws, workspaces, workspaces)
00330                         last_ws = ws->num;
00331                 result = workspace_get(last_ws + 1);
00332         }
00333 
00334         workspace_initialize(result, output, false);
00335         return result;
00336 }
00337 
00338 /*
00339  * Maps all clients (and stack windows) of the given workspace.
00340  *
00341  */
00342 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
00343         Client *client;
00344 
00345         ignore_enter_notify_forall(conn, ws, true);
00346 
00347         /* Map all clients on the new workspace */
00348         FOR_TABLE(ws)
00349                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
00350                         client_map(conn, client);
00351 
00352         /* Map all floating clients */
00353         if (!ws->floating_hidden)
00354                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
00355                         client_map(conn, client);
00356 
00357         /* Map all stack windows, if any */
00358         struct Stack_Window *stack_win;
00359         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00360                 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
00361                         xcb_map_window(conn, stack_win->window);
00362 
00363         ignore_enter_notify_forall(conn, ws, false);
00364 }
00365 
00366 /*
00367  * Unmaps all clients (and stack windows) of the given workspace.
00368  *
00369  * This needs to be called separately when temporarily rendering
00370  * a workspace which is not the active workspace to force
00371  * reconfiguration of all clients, like in src/xinerama.c when
00372  * re-assigning a workspace to another screen.
00373  *
00374  */
00375 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
00376         Client *client;
00377         struct Stack_Window *stack_win;
00378 
00379         /* Ignore notify events because they would cause focus to be changed */
00380         ignore_enter_notify_forall(conn, u_ws, true);
00381 
00382         /* Unmap all clients of the given workspace */
00383         int unmapped_clients = 0;
00384         FOR_TABLE(u_ws)
00385                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
00386                         DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
00387                         client_unmap(conn, client);
00388                         unmapped_clients++;
00389                 }
00390 
00391         /* To find floating clients, we traverse the focus stack */
00392         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
00393                 if (!client_is_floating(client))
00394                         continue;
00395 
00396                 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
00397 
00398                 client_unmap(conn, client);
00399                 unmapped_clients++;
00400         }
00401 
00402         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
00403          * if it is not the current workspace. */
00404         if (unmapped_clients == 0 && u_ws != c_ws) {
00405                 /* Re-assign the workspace of all dock clients which use this workspace */
00406                 Client *dock;
00407                 DLOG("workspace %p is empty\n", u_ws);
00408                 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
00409                         if (dock->workspace != u_ws)
00410                                 continue;
00411 
00412                         DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
00413                         dock->workspace = c_ws;
00414                 }
00415                 u_ws->output = NULL;
00416         }
00417 
00418         /* Unmap the stack windows on the given workspace, if any */
00419         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00420                 if (stack_win->container->workspace == u_ws)
00421                         xcb_unmap_window(conn, stack_win->window);
00422 
00423         ignore_enter_notify_forall(conn, u_ws, false);
00424 }
00425 
00426 /*
00427  * Goes through all clients on the given workspace and updates the workspace’s
00428  * urgent flag accordingly.
00429  *
00430  */
00431 void workspace_update_urgent_flag(Workspace *ws) {
00432         Client *current;
00433         bool old_flag = ws->urgent;
00434         bool urgent = false;
00435 
00436         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
00437                 if (!current->urgent)
00438                         continue;
00439 
00440                 urgent = true;
00441                 break;
00442         }
00443 
00444         ws->urgent = urgent;
00445 
00446         if (old_flag != urgent)
00447                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
00448 }
00449 
00450 /*
00451  * Returns the width of the workspace.
00452  *
00453  */
00454 int workspace_width(Workspace *ws) {
00455         return ws->rect.width;
00456 }
00457 
00458 /*
00459  * Returns the effective height of the workspace (without the internal bar and
00460  * without dock clients).
00461  *
00462  */
00463 int workspace_height(Workspace *ws) {
00464         int height = ws->rect.height;
00465         i3Font *font = load_font(global_conn, config.font);
00466 
00467         /* Reserve space for dock clients */
00468         Client *client;
00469         SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
00470                 height -= client->desired_height;
00471 
00472         /* Space for the internal bar */
00473         if (!config.disable_workspace_bar)
00474                 height -= (font->height + 6);
00475 
00476         return height;
00477 }

Generated on Mon Aug 22 2011 for i3 by  doxygen 1.7.1