]> 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 8f0b42d7576a72d71a31bb6e2eec7889217c1e39..7c29e6f7f8d731fd828f6c03e409acecb7be39e9 100644 (file)
@@ -26,6 +26,8 @@
 #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),
@@ -34,7 +36,7 @@
  *
  */
 Workspace *workspace_get(int number) {
-        Workspace *ws;
+        Workspace *ws = NULL;
         TAILQ_FOREACH(ws, workspaces, workspaces)
                 if (ws->num == number)
                         return ws;
@@ -42,13 +44,13 @@ Workspace *workspace_get(int number) {
         /* If we are still there, we could not find the requested workspace. */
         int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
 
-        LOG("We need to initialize that one, last ws = %d\n", last_ws);
+        DLOG("We need to initialize that one, last ws = %d\n", last_ws);
 
         for (int c = last_ws; c < number; c++) {
-                LOG("Creating new ws\n");
+                DLOG("Creating new ws\n");
 
                 ws = scalloc(sizeof(Workspace));
-                ws->num = number;
+                ws->num = c+1;
                 TAILQ_INIT(&(ws->floating_clients));
                 expand_table_cols(ws);
                 expand_table_rows(ws);
@@ -56,7 +58,9 @@ Workspace *workspace_get(int number) {
 
                 TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
         }
-        LOG("done\n");
+        DLOG("done\n");
+
+        ewmh_update_workarea();
 
         return ws;
 }
@@ -109,18 +113,18 @@ 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;
@@ -163,7 +167,7 @@ void workspace_show(xcb_connection_t *conn, int 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);
 
@@ -206,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;
@@ -222,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;
@@ -243,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
@@ -250,12 +299,16 @@ 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 */
@@ -263,8 +316,12 @@ void workspace_initialize(Workspace *ws, i3Screen *screen) {
             (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
                 ws->screen = screen;
 
-        /* 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);
 }
 
 /*
@@ -298,7 +355,7 @@ Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *
         }
 
         if (result == NULL) {
-                LOG("No existing free workspace found to assign, creating a new one\n");
+                DLOG("No existing free workspace found to assign, creating a new one\n");
 
                 Workspace *ws;
                 int last_ws = 0;
@@ -307,7 +364,7 @@ Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *
                 result = workspace_get(last_ws + 1);
         }
 
-        workspace_initialize(result, screen);
+        workspace_initialize(result, screen, false);
         return result;
 }
 
@@ -359,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++;
                 }
@@ -369,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++;
@@ -380,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;
@@ -417,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;
+}