]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
Bugfix: Repeatedly try to find screens if none are available (Thanks mxf)
[i3/i3] / src / commands.c
index 8b088793129b49c3568c7308fc36b1084739f30c..275f8f96bb08c931d09842656a91b6e46f848dbe 100644 (file)
@@ -25,6 +25,7 @@
 #include "xinerama.h"
 #include "client.h"
 #include "floating.h"
+#include "xcb.h"
 
 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
         /* If this container is empty, we’re done */
@@ -59,10 +60,17 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
 
         int new_row = current_row,
             new_col = current_col;
-
         Container *container = CUR_CELL;
         Workspace *t_ws = c_ws;
 
+        /* Makes sure new_col and new_row are within bounds of the new workspace */
+        void check_colrow_boundaries() {
+                if (new_col >= t_ws->cols)
+                        new_col = (t_ws->cols - 1);
+                if (new_row >= t_ws->rows)
+                        new_row = (t_ws->rows - 1);
+        }
+
         /* There always is a container. If not, current_col or current_row is wrong */
         assert(container != NULL);
 
@@ -105,6 +113,21 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                         t_ws = &(workspaces[screen->current_workspace]);
                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
                 }
+
+                check_colrow_boundaries();
+
+                LOG("new_col = %d, new_row = %d\n", new_col, new_row);
+                if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
+                        LOG("Cell empty, checking for colspanned client above...\n");
+                        for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
+                                if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
+                                        continue;
+
+                                new_col = cols;
+                                break;
+                        }
+                        LOG("Fixed it to new col %d\n", new_col);
+                }
         } else if (direction == D_LEFT || direction == D_RIGHT) {
                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
@@ -132,16 +155,27 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                         t_ws = &(workspaces[screen->current_workspace]);
                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
                 }
+
+                check_colrow_boundaries();
+
+                LOG("new_col = %d, new_row = %d\n", new_col, new_row);
+                if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
+                        LOG("Cell empty, checking for rowspanned client above...\n");
+                        for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
+                                if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
+                                        continue;
+
+                                new_row = rows;
+                                break;
+                        }
+                        LOG("Fixed it to new row %d\n", new_row);
+                }
         } else {
                 LOG("direction unhandled\n");
                 return;
         }
 
-        /* Bounds checking */
-        if (new_col >= t_ws->cols)
-                new_col = (t_ws->cols - 1);
-        if (new_row >= t_ws->rows)
-                new_row = (t_ws->rows - 1);
+        check_colrow_boundaries();
 
         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
@@ -434,7 +468,8 @@ static void snap_current_container(xcb_connection_t *conn, direction_t direction
 
 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
-        Workspace *t_ws = &(workspaces[workspace-1]);
+        Workspace *t_ws = &(workspaces[workspace-1]),
+                  *old_ws = client->workspace;
 
         LOG("moving floating\n");
 
@@ -452,29 +487,35 @@ static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *cl
                 }
         }
 
-        /* Remove from focus stack and list of floating clients */
-        SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
-        TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
+        floating_assign_to_workspace(client, t_ws);
 
-        if (client->workspace->fullscreen_client == client)
-                client->workspace->fullscreen_client = NULL;
-
-        /* Insert into destination focus stack and list of floating clients */
-        client->workspace = t_ws;
-        SLIST_INSERT_HEAD(&(t_ws->focus_stack), client, focus_clients);
-        TAILQ_INSERT_TAIL(&(t_ws->floating_clients), client, floating_clients);
-        if (client->fullscreen)
-                t_ws->fullscreen_client = client;
+        bool target_invisible = t_ws->screen->current_workspace != t_ws->num;
 
         /* If we’re moving it to an invisible screen, we need to unmap it */
-        if (t_ws->screen->current_workspace != t_ws->num) {
+        if (target_invisible) {
                 LOG("This workspace is not visible, unmapping\n");
                 xcb_unmap_window(conn, client->frame);
+        } else {
+                /* If this is not the case, we move the window to a workspace
+                 * which is on another screen, so we also need to adjust its
+                 * coordinates. */
+                LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
+                uint32_t relative_x = client->rect.x - old_ws->rect.x,
+                         relative_y = client->rect.y - old_ws->rect.y;
+                LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
+                client->rect.x = t_ws->rect.x + relative_x;
+                client->rect.y = t_ws->rect.y + relative_y;
+                LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
+                reposition_client(conn, client);
+                xcb_flush(conn);
         }
 
         LOG("done\n");
 
         render_layout(conn);
+
+        if (!target_invisible)
+                set_focus(conn, client, true);
 }
 
 /*
@@ -526,8 +567,6 @@ static void move_current_window_to_workspace(xcb_connection_t *conn, int workspa
         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
 
         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
-        if (current_client->fullscreen)
-                t_ws->fullscreen_client = current_client;
         LOG("Moved.\n");
 
         current_client->container = to_container;
@@ -535,16 +574,26 @@ static void move_current_window_to_workspace(xcb_connection_t *conn, int workspa
         container->currently_focused = to_focus;
         to_container->currently_focused = current_client;
 
+        bool target_invisible = (to_container->workspace->screen->current_workspace != to_container->workspace->num);
+
         /* If we’re moving it to an invisible screen, we need to unmap it */
-        if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
+        if (target_invisible) {
                 LOG("This workspace is not visible, unmapping\n");
                 xcb_unmap_window(conn, current_client->frame);
+        } else {
+                if (current_client->fullscreen) {
+                        LOG("Calling client_enter_fullscreen again\n");
+                        client_enter_fullscreen(conn, current_client);
+                }
         }
 
         /* delete all empty columns/rows */
         cleanup_table(conn, container->workspace);
 
         render_layout(conn);
+
+        if (!target_invisible)
+                set_focus(conn, current_client, true);
 }
 
 /*
@@ -926,7 +975,11 @@ void parse_command(xcb_connection_t *conn, const char *command) {
                 fix_colrowspan(conn, last_focused->workspace);
 
                 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
-                xcb_flush(conn);
+
+                /* Re-focus the client because cleanup_table sets the focus to the last
+                 * focused client inside a container only. */
+                set_focus(conn, last_focused, true);
+
                 return;
         }