]> git.sur5r.net Git - i3/i3/blobdiff - src/workspace.c
ewmh: implement support for _NET_WORKAREA (rdesktop can use that)
[i3/i3] / src / workspace.c
index c4f1ead199028dc961c9ca3c157d83c36f4dafee..7c29e6f7f8d731fd828f6c03e409acecb7be39e9 100644 (file)
 #include "layout.h"
 #include "workspace.h"
 #include "client.h"
+#include "log.h"
+#include "ewmh.h"
 
+/*
+ * Returns a pointer to the workspace with the given number (starting at 0),
+ * creating the workspace if necessary (by allocating the necessary amount of
+ * memory and initializing the data structures correctly).
+ *
+ */
 Workspace *workspace_get(int number) {
-        if (number > (num_workspaces-1)) {
-                int old_num_workspaces = num_workspaces;
-
-                /* Convert all container->workspace and client->workspace
-                 * pointers to numbers representing their workspace. Necessary
-                 * because the realloc() may make all the pointers invalid, so
-                 * we need to preserve them this way and restore them later.
-                 *
-                 * To distinguish between the first workspace and a NULL
-                 * pointer, we store <workspace number> + 1. */
-                for (int c = 0; c < num_workspaces; c++)
-                        FOR_TABLE(&(workspaces[c])) {
-                                Container *con = workspaces[c].table[cols][rows];
-                                if (con->workspace != NULL) {
-                                        LOG("Handling con %p with pointer %p (num %d)\n", con, con->workspace, con->workspace->num);
-                                        con->workspace = (Workspace*)(con->workspace->num + 1);
-                                }
-                                Client *current;
-                                SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
-                                        if (current->workspace == NULL)
-                                                continue;
-                                        LOG("Handling client %p with pointer %p (num %d)\n", current, current->workspace, current->workspace->num);
-                                        current->workspace = (Workspace*)(current->workspace->num + 1);
-                                }
-                        }
+        Workspace *ws = NULL;
+        TAILQ_FOREACH(ws, workspaces, workspaces)
+                if (ws->num == number)
+                        return ws;
 
-                /* preserve c_ws */
-                c_ws = (Workspace*)(c_ws->num);
-
-                LOG("We need to initialize that one\n");
-                num_workspaces = number+1;
-                workspaces = realloc(workspaces, num_workspaces * sizeof(Workspace));
-                for (int c = old_num_workspaces; c < num_workspaces; c++) {
-                        memset(&workspaces[c], 0, sizeof(Workspace));
-                        workspaces[c].screen = NULL;
-                        workspaces[c].num = c;
-                        TAILQ_INIT(&(workspaces[c].floating_clients));
-                        expand_table_cols(&(workspaces[c]));
-                        expand_table_rows(&(workspaces[c]));
-                        workspace_set_name(&(workspaces[c]), NULL);
-                }
+        /* If we are still there, we could not find the requested workspace. */
+        int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
 
-                c_ws = workspace_get((int)c_ws);
-
-                for (int c = 0; c < old_num_workspaces; c++)
-                        FOR_TABLE(&(workspaces[c])) {
-                                Container *con = workspaces[c].table[cols][rows];
-                                if (con->workspace != NULL) {
-                                        LOG("Handling con %p with (num %d)\n", con, con->workspace);
-                                        con->workspace = workspace_get((int)con->workspace - 1);
-                                }
-                                Client *current;
-                                SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
-                                        if (current->workspace == NULL)
-                                                continue;
-                                        LOG("Handling client %p with (num %d)\n", current, current->workspace);
-                                        current->workspace = workspace_get((int)current->workspace - 1);
-                                }
-                        }
+        DLOG("We need to initialize that one, last ws = %d\n", last_ws);
+
+        for (int c = last_ws; c < number; c++) {
+                DLOG("Creating new ws\n");
 
+                ws = scalloc(sizeof(Workspace));
+                ws->num = c+1;
+                TAILQ_INIT(&(ws->floating_clients));
+                expand_table_cols(ws);
+                expand_table_rows(ws);
+                workspace_set_name(ws, NULL);
 
-                LOG("done\n");
+                TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
         }
+        DLOG("done\n");
 
-        return &(workspaces[number]);
+        ewmh_update_workarea();
+
+        return ws;
 }
 
 /*
@@ -130,7 +100,7 @@ void workspace_set_name(Workspace *ws, const char *name) {
  *
  */
 bool workspace_is_visible(Workspace *ws) {
-        return (ws->screen->current_workspace == ws->num);
+        return (ws->screen->current_workspace == ws);
 }
 
 /*
@@ -143,23 +113,23 @@ void workspace_show(xcb_connection_t *conn, int workspace) {
         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
         Workspace *t_ws = workspace_get(workspace-1);
 
-        LOG("show_workspace(%d)\n", workspace);
+        DLOG("show_workspace(%d)\n", workspace);
 
         /* Store current_row/current_col */
         c_ws->current_row = current_row;
         c_ws->current_col = current_col;
 
         /* Check if the workspace has not been used yet */
-        workspace_initialize(t_ws, c_ws->screen);
+        workspace_initialize(t_ws, c_ws->screen, false);
 
         if (c_ws->screen != t_ws->screen) {
                 /* We need to switch to the other screen first */
-                LOG("moving over to other screen.\n");
+                DLOG("moving over to other screen.\n");
 
                 /* Store the old client */
                 Client *old_client = CUR_CELL->currently_focused;
 
-                c_ws = workspace_get(t_ws->screen->current_workspace);
+                c_ws = t_ws->screen->current_workspace;
                 current_col = c_ws->current_col;
                 current_row = c_ws->current_row;
                 if (CUR_CELL->currently_focused != NULL)
@@ -177,7 +147,7 @@ void workspace_show(xcb_connection_t *conn, int workspace) {
         }
 
         /* Check if we need to change something or if we’re already there */
-        if (c_ws->screen->current_workspace == (workspace-1)) {
+        if (c_ws->screen->current_workspace->num == (workspace-1)) {
                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
                         set_focus(conn, last_focused, true);
@@ -189,16 +159,15 @@ void workspace_show(xcb_connection_t *conn, int workspace) {
                 return;
         }
 
-        t_ws->screen->current_workspace = workspace-1;
         Workspace *old_workspace = c_ws;
-        c_ws = workspace_get(workspace-1);
+        c_ws = t_ws->screen->current_workspace = workspace_get(workspace-1);
 
         /* Unmap all clients of the old workspace */
         workspace_unmap_clients(conn, old_workspace);
 
         current_row = c_ws->current_row;
         current_col = c_ws->current_col;
-        LOG("new current row = %d, current col = %d\n", current_row, current_col);
+        DLOG("new current row = %d, current col = %d\n", current_row, current_col);
 
         workspace_map_clients(conn, c_ws);
 
@@ -241,7 +210,7 @@ static i3Screen *get_screen_from_preference(struct screens_head *slist, char *pr
         char *rest;
         int preferred_screen = strtol(preference, &rest, 10);
 
-        LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
+        DLOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
 
         if ((rest == preference) || (preferred_screen >= num_screens)) {
                 int x = INT_MAX, y = INT_MAX;
@@ -257,16 +226,16 @@ static i3Screen *get_screen_from_preference(struct screens_head *slist, char *pr
                         x = atoi(preference);
                 }
 
-                LOG("Looking for screen at %d x %d\n", x, y);
+                DLOG("Looking for screen at %d x %d\n", x, y);
 
                 TAILQ_FOREACH(screen, slist, screens)
                         if ((x == INT_MAX || screen->rect.x == x) &&
                             (y == INT_MAX || screen->rect.y == y)) {
-                                LOG("found %p\n", screen);
+                                DLOG("found %p\n", screen);
                                 return screen;
                         }
 
-                LOG("none found\n");
+                DLOG("none found\n");
                 return NULL;
         } else {
                 int c = 0;
@@ -278,6 +247,51 @@ static i3Screen *get_screen_from_preference(struct screens_head *slist, char *pr
         return NULL;
 }
 
+/*
+ * Assigns the given workspace to the given screen by correctly updating its
+ * state and reconfiguring all the clients on this workspace.
+ *
+ * This is called when initializing a screen and when re-assigning it to a
+ * different screen which just got available (if you configured it to be on
+ * screen 1 and you just plugged in screen 1).
+ *
+ */
+void workspace_assign_to(Workspace *ws, i3Screen *screen) {
+        Client *client;
+        bool empty = true;
+
+        ws->screen = screen;
+
+        /* Copy the dimensions from the virtual screen */
+        memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
+
+        ewmh_update_workarea();
+
+        /* Force reconfiguration for each client on that workspace */
+        FOR_TABLE(ws)
+                CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients) {
+                        client->force_reconfigure = true;
+                        empty = false;
+                }
+
+        if (empty)
+                return;
+
+        /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
+        render_workspace(global_conn, screen, ws);
+
+        /* …unless we want to see them at the moment, we should hide that workspace */
+        if (workspace_is_visible(ws))
+                return;
+
+        workspace_unmap_clients(global_conn, ws);
+
+        if (c_ws == ws) {
+                DLOG("Need to adjust c_ws...\n");
+                c_ws = screen->current_workspace;
+        }
+}
+
 /*
  * Initializes the given workspace if it is not already initialized. The given
  * screen is to be understood as a fallback, if the workspace itself either
@@ -285,22 +299,29 @@ static i3Screen *get_screen_from_preference(struct screens_head *slist, char *pr
  * the screen is not attached at the moment.
  *
  */
-void workspace_initialize(Workspace *ws, i3Screen *screen) {
-        if (ws->screen != NULL) {
-                LOG("Workspace already initialized\n");
+void workspace_initialize(Workspace *ws, i3Screen *screen, bool recheck) {
+        i3Screen *old_screen;
+
+        if (ws->screen != NULL && !recheck) {
+                DLOG("Workspace already initialized\n");
                 return;
         }
 
+        old_screen = ws->screen;
+
         /* If this workspace has no preferred screen or if the screen it wants
          * to be on is not available at the moment, we initialize it with
          * the screen which was given */
         if (ws->preferred_screen == NULL ||
             (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
                 ws->screen = screen;
-        else { LOG("yay, found assignment\n"); }
 
-        /* Copy the dimensions from the virtual screen */
-        memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
+        DLOG("old_screen = %p, ws->screen = %p\n", old_screen, ws->screen);
+        /* If the assignment did not change, we do not need to update anything */
+        if (old_screen != NULL && ws->screen == old_screen)
+                return;
+
+        workspace_assign_to(ws, ws->screen);
 }
 
 /*
@@ -311,8 +332,8 @@ void workspace_initialize(Workspace *ws, i3Screen *screen) {
 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
         Workspace *result = NULL;
 
-        for (int c = 0; c < num_workspaces; c++) {
-                Workspace *ws = workspace_get(c);
+        Workspace *ws;
+        TAILQ_FOREACH(ws, workspaces, workspaces) {
                 if (ws->preferred_screen == NULL ||
                     !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
                         continue;
@@ -323,22 +344,28 @@ Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *
 
         if (result == NULL) {
                 /* No assignment found, returning first unused workspace */
-                for (int c = 0; c < num_workspaces; c++) {
-                        if (workspaces[c].screen != NULL)
+                Workspace *ws;
+                TAILQ_FOREACH(ws, workspaces, workspaces) {
+                        if (ws->screen != NULL)
                                 continue;
 
-                        result = workspace_get(c);
+                        result = ws;
                         break;
                 }
         }
 
-        if (result != NULL) {
-                workspace_initialize(result, screen);
-                return result;
+        if (result == NULL) {
+                DLOG("No existing free workspace found to assign, creating a new one\n");
+
+                Workspace *ws;
+                int last_ws = 0;
+                TAILQ_FOREACH(ws, workspaces, workspaces)
+                        last_ws = ws->num;
+                result = workspace_get(last_ws + 1);
         }
 
-        LOG("WARNING: No free workspace found to assign!\n");
-        return NULL;
+        workspace_initialize(result, screen, false);
+        return result;
 }
 
 /*
@@ -389,7 +416,7 @@ void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
         int unmapped_clients = 0;
         FOR_TABLE(u_ws)
                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
-                        LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
+                        DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
                         client_unmap(conn, client);
                         unmapped_clients++;
                 }
@@ -399,7 +426,7 @@ void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
                 if (!client_is_floating(client))
                         continue;
 
-                LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
+                DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
 
                 client_unmap(conn, client);
                 unmapped_clients++;
@@ -410,12 +437,12 @@ void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
         if (unmapped_clients == 0 && u_ws != c_ws) {
                 /* Re-assign the workspace of all dock clients which use this workspace */
                 Client *dock;
-                LOG("workspace %p is empty\n", u_ws);
+                DLOG("workspace %p is empty\n", u_ws);
                 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
                         if (dock->workspace != u_ws)
                                 continue;
 
-                        LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
+                        DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
                         dock->workspace = c_ws;
                 }
                 u_ws->screen = NULL;
@@ -447,3 +474,31 @@ void workspace_update_urgent_flag(Workspace *ws) {
 
         ws->urgent = false;
 }
+
+/*
+ * Returns the width of the workspace.
+ *
+ */
+int workspace_width(Workspace *ws) {
+        return ws->rect.width;
+}
+
+/*
+ * Returns the effective height of the workspace (without the internal bar and
+ * without dock clients).
+ *
+ */
+int workspace_height(Workspace *ws) {
+        int height = ws->rect.height;
+        i3Font *font = load_font(global_conn, config.font);
+
+        /* Reserve space for dock clients */
+        Client *client;
+        SLIST_FOREACH(client, &(ws->screen->dock_clients), dock_clients)
+                height -= client->desired_height;
+
+        /* Space for the internal bar */
+        height -= (font->height + 6);
+
+        return height;
+}