]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
implement 'move' command in the new parser
[i3/i3] / src / commands.c
index 7cb0bdca3f328b43f0ed50536c79e962fb62caad..2d8155f276a162494db56b6ce416566875b2861a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * i3 - an improved dynamic tiling window manager
  *
- * © 2009 Michael Stapelberg and contributors
+ * © 2009-2010 Michael Stapelberg and contributors
  *
  * See file LICENSE for license information.
  *
@@ -22,7 +22,7 @@
 #include "table.h"
 #include "layout.h"
 #include "i3.h"
-#include "xinerama.h"
+#include "randr.h"
 #include "client.h"
 #include "floating.h"
 #include "xcb.h"
@@ -31,6 +31,9 @@
 #include "commands.h"
 #include "resize.h"
 #include "log.h"
+#include "sighandler.h"
+#include "manage.h"
+#include "ipc.h"
 
 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
         /* If this container is empty, we’re done */
@@ -87,19 +90,20 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
         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);
-        }
+#define CHECK_COLROW_BOUNDARIES \
+        do { \
+                if (new_col >= t_ws->cols) \
+                        new_col = (t_ws->cols - 1); \
+                if (new_row >= t_ws->rows) \
+                        new_row = (t_ws->rows - 1); \
+        } while (0)
 
         /* There always is a container. If not, current_col or current_row is wrong */
         assert(container != NULL);
 
         if (container->workspace->fullscreen_client != NULL) {
-                LOG("You're in fullscreen mode. Won't switch focus\n");
-                return;
+                LOG("You're in fullscreen mode. Forcing focus to operate on whole screens\n");
+                thing = THING_SCREEN;
         }
 
         /* For focusing screens, situation is different: we get the rect
@@ -107,7 +111,7 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
          * right/left/bottom/top and just switch to the workspace on
          * the target screen. */
         if (thing == THING_SCREEN) {
-                i3Screen *cs = c_ws->screen;
+                Output *cs = c_ws->output;
                 assert(cs != NULL);
                 Rect bounds = cs->rect;
 
@@ -119,17 +123,17 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                         bounds.y -= bounds.height;
                 else bounds.y += bounds.height;
 
-                i3Screen *target = get_screen_containing(bounds.x, bounds.y);
+                Output *target = get_output_containing(bounds.x, bounds.y);
                 if (target == NULL) {
-                        DLOG("Target screen NULL\n");
+                        DLOG("Target output NULL\n");
                         /* Wrap around if the target screen is out of bounds */
                         if (direction == D_RIGHT)
-                                target = get_screen_most(D_LEFT, cs);
+                                target = get_output_most(D_LEFT, cs);
                         else if (direction == D_LEFT)
-                                target = get_screen_most(D_RIGHT, cs);
+                                target = get_output_most(D_RIGHT, cs);
                         else if (direction == D_UP)
-                                target = get_screen_most(D_DOWN, cs);
-                        else target = get_screen_most(D_UP, cs);
+                                target = get_output_most(D_DOWN, cs);
+                        else target = get_output_most(D_UP, cs);
                 }
 
                 DLOG("Switching to ws %d\n", target->current_workspace + 1);
@@ -144,9 +148,9 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                         if (focus_window_in_container(conn, container, direction))
                                 return;
 
-                if (direction == D_DOWN && cell_exists(current_col, current_row+1))
+                if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
                         new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
-                else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
+                else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
                         /* Set new_row as a sane default, but it may get overwritten in a second */
                         new_row--;
 
@@ -161,18 +165,18 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                 } else {
                         /* Let’s see if there is a screen down/up there to which we can switch */
                         DLOG("container is at %d with height %d\n", container->y, container->height);
-                        i3Screen *screen;
+                        Output *output;
                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
-                        if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
+                        if ((output = get_output_containing(container->x, destination_y)) == NULL) {
                                 DLOG("Wrapping screen around vertically\n");
                                 /* No screen found? Then wrap */
-                                screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
+                                output = get_output_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->output);
                         }
-                        t_ws = screen->current_workspace;
+                        t_ws = output->current_workspace;
                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
                 }
 
-                check_colrow_boundaries();
+                CHECK_COLROW_BOUNDARIES;
 
                 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
@@ -182,14 +186,31 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                                         continue;
 
                                 new_col = cols;
+                                DLOG("Fixed it to new col %d\n", new_col);
+                                break;
+                        }
+                }
+
+                if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
+                        DLOG("Cell still empty, checking for full cols above spanned width...\n");
+                        DLOG("new_col = %d\n", new_col);
+                        DLOG("colspan = %d\n", container->colspan);
+                        for (int cols = new_col;
+                             cols < container->col + container->colspan;
+                             cols += t_ws->table[cols][new_row]->colspan) {
+                                DLOG("candidate: new_row = %d, cols = %d\n", new_row, cols);
+                                if (t_ws->table[cols][new_row]->currently_focused == NULL)
+                                        continue;
+
+                                new_col = cols;
+                                DLOG("Fixed it to new col %d\n", new_col);
                                 break;
                         }
-                        DLOG("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))
+                if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
-                else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
+                else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
                         /* Set new_col as a sane default, but it may get overwritten in a second */
                         new_col--;
 
@@ -204,17 +225,17 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                 } else {
                         /* Let’s see if there is a screen left/right here to which we can switch */
                         DLOG("container is at %d with width %d\n", container->x, container->width);
-                        i3Screen *screen;
+                        Output *output;
                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
-                        if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
+                        if ((output = get_output_containing(destination_x, container->y)) == NULL) {
                                 DLOG("Wrapping screen around horizontally\n");
-                                screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
+                                output = get_output_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->output);
                         }
-                        t_ws = screen->current_workspace;
+                        t_ws = output->current_workspace;
                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
                 }
 
-                check_colrow_boundaries();
+                CHECK_COLROW_BOUNDARIES;
 
                 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
@@ -224,16 +245,34 @@ static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t t
                                         continue;
 
                                 new_row = rows;
+                                DLOG("Fixed it to new row %d\n", new_row);
                                 break;
                         }
-                        DLOG("Fixed it to new row %d\n", new_row);
                 }
+
+                if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
+                        DLOG("Cell still empty, checking for full cols near full spanned height...\n");
+                        DLOG("new_row = %d\n", new_row);
+                        DLOG("rowspan = %d\n", container->rowspan);
+                        for (int rows = new_row;
+                             rows < container->row + container->rowspan;
+                             rows += t_ws->table[new_col][rows]->rowspan) {
+                                DLOG("candidate: new_col = %d, rows = %d\n", new_col, rows);
+                                if (t_ws->table[new_col][rows]->currently_focused == NULL)
+                                        continue;
+
+                                new_row = rows;
+                                DLOG("Fixed it to new col %d\n", new_row);
+                                break;
+                        }
+                }
+
         } else {
                 ELOG("direction unhandled\n");
                 return;
         }
 
-        check_colrow_boundaries();
+        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);
@@ -358,7 +397,7 @@ static void move_current_window(xcb_connection_t *conn, direction_t direction) {
         /* Fix colspan/rowspan if it’d overlap */
         fix_colrowspan(conn, workspace);
 
-        render_workspace(conn, workspace->screen, workspace);
+        render_workspace(conn, workspace->output, workspace);
         xcb_flush(conn);
 
         set_focus(conn, current_client, true);
@@ -452,7 +491,7 @@ static void snap_current_container(xcb_connection_t *conn, direction_t direction
         switch (direction) {
                 case D_LEFT:
                         /* Snap to the left is actually a move to the left and then a snap right */
-                        if (!cell_exists(container->col - 1, container->row) ||
+                        if (!cell_exists(container->workspace, container->col - 1, container->row) ||
                             CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
                                 ELOG("cannot snap to left - the cell is already used\n");
                                 return;
@@ -465,7 +504,7 @@ static void snap_current_container(xcb_connection_t *conn, direction_t direction
                         /* Check if the cell is used */
                         int new_col = container->col + container->colspan;
                         for (int i = 0; i < container->rowspan; i++)
-                                if (!cell_exists(new_col, container->row + i) ||
+                                if (!cell_exists(container->workspace, new_col, container->row + i) ||
                                     CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
                                         ELOG("cannot snap to right - the cell is already used\n");
                                         return;
@@ -485,7 +524,7 @@ static void snap_current_container(xcb_connection_t *conn, direction_t direction
                         break;
                 }
                 case D_UP:
-                        if (!cell_exists(container->col, container->row - 1) ||
+                        if (!cell_exists(container->workspace, container->col, container->row - 1) ||
                             CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
                                 ELOG("cannot snap to top - the cell is already used\n");
                                 return;
@@ -498,7 +537,7 @@ static void snap_current_container(xcb_connection_t *conn, direction_t direction
                         DLOG("snapping down\n");
                         int new_row = container->row + container->rowspan;
                         for (int i = 0; i < container->colspan; i++)
-                                if (!cell_exists(container->col + i, new_row) ||
+                                if (!cell_exists(container->workspace, container->col + i, new_row) ||
                                     CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
                                         ELOG("cannot snap down - the cell is already used\n");
                                         return;
@@ -531,7 +570,7 @@ static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *cl
 
         LOG("moving floating\n");
 
-        workspace_initialize(t_ws, c_ws->screen);
+        workspace_initialize(t_ws, c_ws->output, false);
 
         /* Check if there is already a fullscreen client on the destination workspace and
          * stop moving if so. */
@@ -561,12 +600,30 @@ static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *cl
                 xcb_flush(conn);
         }
 
+        /* Configure the window above all tiling windows (or below a fullscreen
+         * window, if any) */
+        if (t_ws->fullscreen_client != NULL) {
+                uint32_t values[] = { t_ws->fullscreen_client->frame, XCB_STACK_MODE_BELOW };
+                xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
+        } else {
+                Client *last_tiling;
+                SLIST_FOREACH(last_tiling, &(t_ws->focus_stack), focus_clients)
+                        if (!client_is_floating(last_tiling))
+                                break;
+                if (last_tiling != SLIST_END(&(t_ws->focus_stack))) {
+                        uint32_t values[] = { last_tiling->frame, XCB_STACK_MODE_ABOVE };
+                        xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
+                }
+        }
+
         DLOG("done\n");
 
         render_layout(conn);
 
-        if (workspace_is_visible(t_ws))
+        if (workspace_is_visible(t_ws)) {
+                client_warp_pointer_into(conn, client);
                 set_focus(conn, client, true);
+        }
 }
 
 /*
@@ -592,7 +649,7 @@ static void move_current_window_to_workspace(xcb_connection_t *conn, int workspa
         if (to_focus == NULL)
                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
 
-        workspace_initialize(t_ws, container->workspace->screen);
+        workspace_initialize(t_ws, container->workspace->output, false);
         /* Check if there is already a fullscreen client on the destination workspace and
          * stop moving if so. */
         if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
@@ -626,7 +683,7 @@ static void move_current_window_to_workspace(xcb_connection_t *conn, int workspa
         } else {
                 if (current_client->fullscreen) {
                         DLOG("Calling client_enter_fullscreen again\n");
-                        client_enter_fullscreen(conn, current_client);
+                        client_enter_fullscreen(conn, current_client, false);
                 }
         }
 
@@ -635,8 +692,10 @@ static void move_current_window_to_workspace(xcb_connection_t *conn, int workspa
 
         render_layout(conn);
 
-        if (workspace_is_visible(to_container->workspace))
+        if (workspace_is_visible(to_container->workspace)) {
+                client_warp_pointer_into(conn, current_client);
                 set_focus(conn, current_client, true);
+        }
 }
 
 /*
@@ -662,6 +721,7 @@ static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
         }
 
         free(classtitle);
+        workspace_show(conn, client->workspace->num + 1);
         set_focus(conn, client, true);
 }
 
@@ -759,29 +819,6 @@ static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
                 }
 }
 
-/*
- * Goes through the list of arguments (for exec()) and checks if the given argument
- * is present. If not, it copies the arguments (because we cannot realloc it) and
- * appends the given argument.
- *
- */
-static char **append_argument(char **original, char *argument) {
-        int num_args;
-        for (num_args = 0; original[num_args] != NULL; num_args++) {
-                DLOG("original argument: \"%s\"\n", original[num_args]);
-                /* If the argument is already present we return the original pointer */
-                if (strcmp(original[num_args], argument) == 0)
-                        return original;
-        }
-        /* Copy the original array */
-        char **result = smalloc((num_args+2) * sizeof(char*));
-        memcpy(result, original, num_args * sizeof(char*));
-        result[num_args] = argument;
-        result[num_args+1] = NULL;
-
-        return result;
-}
-
 /* 
  * Switch to next or previous existing workspace
  *
@@ -790,16 +827,32 @@ static void next_previous_workspace(xcb_connection_t *conn, int direction) {
         Workspace *ws = c_ws;
 
         if (direction == 'n') {
-                while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
-                        if (ws->screen == NULL)
+                while (1) {
+                        ws = TAILQ_NEXT(ws, workspaces);
+
+                        if (ws == TAILQ_END(workspaces))
+                                ws = TAILQ_FIRST(workspaces);
+
+                        if (ws == c_ws)
+                                return;
+
+                        if (ws->output == NULL)
                                 continue;
 
                         workspace_show(conn, ws->num + 1);
                         return;
                 }
         } else if (direction == 'p') {
-                while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
-                        if (ws->screen == NULL)
+                while (1) {
+                        ws = TAILQ_PREV(ws, workspaces_head, workspaces);
+
+                        if (ws == TAILQ_END(workspaces))
+                                ws = TAILQ_LAST(workspaces, workspaces_head);
+
+                        if (ws == c_ws)
+                                return;
+
+                        if (ws->output == NULL)
                                 continue;
 
                         workspace_show(conn, ws->num + 1);
@@ -812,7 +865,34 @@ static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, c
         int first, second;
         resize_orientation_t orientation = O_VERTICAL;
         Container *con = last_focused->container;
-        Workspace *ws = con->workspace;
+        Workspace *ws = last_focused->workspace;
+
+        if (client_is_floating(last_focused)) {
+                DLOG("Resizing a floating client\n");
+                if (STARTS_WITH(command, "left")) {
+                        command += strlen("left");
+                        last_focused->rect.width += atoi(command);
+                        last_focused->rect.x -= atoi(command);
+                } else if (STARTS_WITH(command, "right")) {
+                        command += strlen("right");
+                        last_focused->rect.width += atoi(command);
+                } else if (STARTS_WITH(command, "top")) {
+                        command += strlen("top");
+                        last_focused->rect.height += atoi(command);
+                        last_focused->rect.y -= atoi(command);
+                } else if (STARTS_WITH(command, "bottom")) {
+                        command += strlen("bottom");
+                        last_focused->rect.height += atoi(command);
+                } else {
+                        ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
+                        return;
+                }
+
+                /* resize_client flushes */
+                resize_client(conn, last_focused);
+
+                return;
+        }
 
         if (STARTS_WITH(command, "left")) {
                 if (con->col == 0)
@@ -824,7 +904,7 @@ static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, c
                 first = con->col + (con->colspan - 1);
                 DLOG("column %d\n", first);
 
-                if (!cell_exists(first, con->row) ||
+                if (!cell_exists(ws, first, con->row) ||
                     (first == (ws->cols-1)))
                         return;
 
@@ -839,7 +919,7 @@ static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, c
                 command += strlen("top");
         } else if (STARTS_WITH(command, "bottom")) {
                 first = con->row + (con->rowspan - 1);
-                if (!cell_exists(con->col, first) ||
+                if (!cell_exists(ws, con->col, first) ||
                     (first == (ws->rows-1)))
                         return;
 
@@ -954,23 +1034,23 @@ void parse_command(xcb_connection_t *conn, const char *command) {
         /* Is it an <exit>? */
         if (STARTS_WITH(command, "exit")) {
                 LOG("User issued exit-command, exiting without error.\n");
+                restore_geometry(global_conn);
+                ipc_shutdown();
                 exit(EXIT_SUCCESS);
         }
 
         /* Is it a <reload>? */
         if (STARTS_WITH(command, "reload")) {
                 load_configuration(conn, NULL, true);
+                render_layout(conn);
+                /* Send an IPC event just in case the ws names have changed */
+                ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
                 return;
         }
 
         /* Is it <restart>? Then restart in place. */
         if (STARTS_WITH(command, "restart")) {
-                LOG("restarting \"%s\"...\n", start_argv[0]);
-                /* make sure -a is in the argument list or append it */
-                start_argv = append_argument(start_argv, "-a");
-
-                execvp(start_argv[0], start_argv);
-                /* not reached */
+                i3_restart();
         }
 
         if (STARTS_WITH(command, "kill")) {
@@ -1000,11 +1080,14 @@ void parse_command(xcb_connection_t *conn, const char *command) {
                 return;
         }
 
-        /* Is it 'f' for fullscreen? */
+        /* Is it 'f' for fullscreen, or 'fg' for fullscreen_global? */
         if (command[0] == 'f') {
                 if (last_focused == NULL)
                         return;
-                client_toggle_fullscreen(conn, last_focused);
+                if (command[1] == 'g')
+                        client_toggle_fullscreen_global(conn, last_focused);
+                else
+                        client_toggle_fullscreen(conn, last_focused);
                 return;
         }
 
@@ -1016,9 +1099,9 @@ void parse_command(xcb_connection_t *conn, const char *command) {
                 }
                 LOG("Switching mode for current container\n");
                 int new_mode = MODE_DEFAULT;
-                if (command[0] == 's')
+                if (command[0] == 's' && CUR_CELL->mode != MODE_STACK)
                         new_mode = MODE_STACK;
-                if (command[0] == 'T')
+                if (command[0] == 'T' && CUR_CELL->mode != MODE_TABBED)
                         new_mode = MODE_TABBED;
                 switch_layout_mode(conn, CUR_CELL, new_mode);
                 return;
@@ -1088,6 +1171,9 @@ void parse_command(xcb_connection_t *conn, const char *command) {
 
                 Workspace *ws = last_focused->workspace;
 
+                if (last_focused->fullscreen)
+                        client_leave_fullscreen(conn, last_focused);
+
                 toggle_floating_mode(conn, last_focused, false);
                 /* delete all empty columns/rows */
                 cleanup_table(conn, ws);
@@ -1095,7 +1181,7 @@ void parse_command(xcb_connection_t *conn, const char *command) {
                 /* Fix colspan/rowspan if it’d overlap */
                 fix_colrowspan(conn, ws);
 
-                render_workspace(conn, ws->screen, ws);
+                render_workspace(conn, ws->output, ws);
 
                 /* Re-focus the client because cleanup_table sets the focus to the last
                  * focused client inside a container only. */