2009-02-14 02:33:31 +01:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=8:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
|
*
|
2009-02-15 03:07:29 +01:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors
|
2009-02-14 02:33:31 +01:00
|
|
|
|
*
|
|
|
|
|
* See file LICENSE for license information.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-13 19:04:45 +01:00
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "data.h"
|
|
|
|
|
#include "table.h"
|
|
|
|
|
#include "layout.h"
|
|
|
|
|
#include "i3.h"
|
2009-03-04 13:06:14 +01:00
|
|
|
|
#include "xinerama.h"
|
2009-02-13 19:04:45 +01:00
|
|
|
|
|
2009-04-01 12:31:13 +02:00
|
|
|
|
bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* If this container is empty, we’re done */
|
|
|
|
|
if (container->currently_focused == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-03-04 23:45:44 +01:00
|
|
|
|
/* Get the previous/next client or wrap around */
|
2009-03-03 23:51:02 +01:00
|
|
|
|
Client *candidate = NULL;
|
2009-03-04 23:45:44 +01:00
|
|
|
|
if (direction == D_UP) {
|
|
|
|
|
if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
|
|
|
|
|
candidate = CIRCLEQ_LAST(&(container->clients));
|
|
|
|
|
}
|
|
|
|
|
else if (direction == D_DOWN) {
|
|
|
|
|
if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
|
|
|
|
|
candidate = CIRCLEQ_FIRST(&(container->clients));
|
2009-03-06 06:46:43 +01:00
|
|
|
|
} else LOG("Direction not implemented!\n");
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
2009-03-05 19:47:16 +01:00
|
|
|
|
/* If we could not switch, the container contains exactly one client. We return false */
|
|
|
|
|
if (candidate == container->currently_focused)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-03-04 23:45:44 +01:00
|
|
|
|
/* Set focus */
|
2009-04-01 12:50:42 +02:00
|
|
|
|
set_focus(conn, candidate, true);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 23:45:44 +01:00
|
|
|
|
typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
|
|
|
|
|
|
|
|
|
|
static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("focusing direction %d\n", direction);
|
2009-03-03 02:28:26 +01:00
|
|
|
|
|
|
|
|
|
int new_row = current_row,
|
|
|
|
|
new_col = current_col;
|
|
|
|
|
|
2009-03-04 13:06:14 +01:00
|
|
|
|
Container *container = CUR_CELL;
|
|
|
|
|
Workspace *t_ws = c_ws;
|
|
|
|
|
|
|
|
|
|
/* There always is a container. If not, current_col or current_row is wrong */
|
|
|
|
|
assert(container != NULL);
|
|
|
|
|
|
2009-03-06 16:53:47 +01:00
|
|
|
|
if (container->workspace->fullscreen_client != NULL) {
|
|
|
|
|
LOG("You're in fullscreen mode. Won't switch focus\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
|
|
|
|
|
if (direction == D_UP || direction == D_DOWN) {
|
2009-03-04 23:45:44 +01:00
|
|
|
|
if (thing == THING_WINDOW)
|
|
|
|
|
/* Let’s see if we can perform up/down focus in the current container */
|
|
|
|
|
if (focus_window_in_container(conn, container, direction))
|
|
|
|
|
return;
|
2009-02-23 01:41:26 +01:00
|
|
|
|
|
|
|
|
|
if (direction == D_DOWN && cell_exists(current_col, current_row+1))
|
2009-03-03 02:28:26 +01:00
|
|
|
|
new_row++;
|
2009-02-23 01:41:26 +01:00
|
|
|
|
else if (direction == D_UP && cell_exists(current_col, current_row-1))
|
2009-03-03 02:28:26 +01:00
|
|
|
|
new_row--;
|
2009-03-04 13:06:14 +01:00
|
|
|
|
else {
|
|
|
|
|
/* Let’s see if there is a screen down/up there to which we can switch */
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("container is at %d with height %d\n", container->y, container->height);
|
2009-03-04 13:06:14 +01:00
|
|
|
|
i3Screen *screen;
|
|
|
|
|
int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
|
|
|
|
|
if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
|
2009-03-09 07:39:19 +01:00
|
|
|
|
LOG("Wrapping screen around vertically\n");
|
|
|
|
|
/* No screen found? Then wrap */
|
|
|
|
|
screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
|
2009-03-04 13:06:14 +01:00
|
|
|
|
}
|
|
|
|
|
t_ws = &(workspaces[screen->current_workspace]);
|
|
|
|
|
new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
|
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
} else if (direction == D_LEFT || direction == D_RIGHT) {
|
|
|
|
|
if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
|
2009-03-03 02:28:26 +01:00
|
|
|
|
new_col++;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
|
2009-03-03 02:28:26 +01:00
|
|
|
|
new_col--;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
else {
|
2009-03-04 13:06:14 +01:00
|
|
|
|
/* Let’s see if there is a screen left/right here to which we can switch */
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("container is at %d with width %d\n", container->x, container->width);
|
2009-03-04 13:06:14 +01:00
|
|
|
|
i3Screen *screen;
|
|
|
|
|
int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
|
|
|
|
|
if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
|
2009-03-09 07:39:19 +01:00
|
|
|
|
LOG("Wrapping screen around horizontally\n");
|
|
|
|
|
screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
|
2009-03-04 13:06:14 +01:00
|
|
|
|
}
|
|
|
|
|
t_ws = &(workspaces[screen->current_workspace]);
|
|
|
|
|
new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("direction unhandled\n");
|
2009-02-23 01:41:26 +01:00
|
|
|
|
return;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
}
|
2009-02-23 01:41:26 +01:00
|
|
|
|
|
2009-03-09 07:39:19 +01:00
|
|
|
|
/* 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);
|
|
|
|
|
|
2009-03-04 13:06:14 +01:00
|
|
|
|
if (t_ws->table[new_col][new_row]->currently_focused != NULL)
|
2009-04-01 12:50:42 +02:00
|
|
|
|
set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Tries to move the window inside its current container.
|
|
|
|
|
*
|
|
|
|
|
* Returns true if the window could be moved, false otherwise.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 15:45:12 +01:00
|
|
|
|
static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
|
2009-02-14 02:33:31 +01:00
|
|
|
|
direction_t direction) {
|
2009-02-23 03:44:10 +01:00
|
|
|
|
assert(client->container != NULL);
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
|
2009-03-03 23:51:02 +01:00
|
|
|
|
CIRCLEQ_NEXT(client, clients));
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
if (other == CIRCLEQ_END(&(client->container->clients)))
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("i can do that\n");
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* We can move the client inside its current container */
|
|
|
|
|
CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
|
|
|
|
|
if (direction == D_UP)
|
|
|
|
|
CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
|
|
|
|
|
else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
|
2009-03-04 15:45:12 +01:00
|
|
|
|
render_layout(conn);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return true;
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-03-11 18:56:31 +01:00
|
|
|
|
* Moves the current window or whole container to the given direction, creating a column/row if
|
|
|
|
|
* necessary.
|
2009-02-13 19:04:45 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 15:45:12 +01:00
|
|
|
|
static void move_current_window(xcb_connection_t *conn, direction_t direction) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
|
2009-03-11 18:56:31 +01:00
|
|
|
|
(direction == D_LEFT ? "left" : "right"))));
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Get current window */
|
|
|
|
|
Container *container = CUR_CELL,
|
2009-02-15 01:58:09 +01:00
|
|
|
|
*new = NULL;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
/* There has to be a container, see focus_window() */
|
|
|
|
|
assert(container != NULL);
|
|
|
|
|
|
2009-02-23 01:41:26 +01:00
|
|
|
|
/* If there is no window or the dock window is focused, we’re done */
|
|
|
|
|
if (container->currently_focused == NULL ||
|
|
|
|
|
container->currently_focused->dock)
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2009-04-07 16:48:42 +02:00
|
|
|
|
/* As soon as the client is moved away, the last focused client in the old
|
2009-02-14 02:33:31 +01:00
|
|
|
|
* container needs to get focus, if any. Therefore, we save it here. */
|
|
|
|
|
Client *current_client = container->currently_focused;
|
2009-04-07 16:48:42 +02:00
|
|
|
|
Client *to_focus = NULL, *client_loop;
|
|
|
|
|
|
|
|
|
|
SLIST_FOREACH(client_loop, &(container->workspace->focus_stack), focus_clients)
|
|
|
|
|
if ((client_loop->container == container) && (client_loop != current_client)) {
|
|
|
|
|
to_focus = client_loop;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (to_focus == NULL) {
|
|
|
|
|
to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
|
|
|
|
|
if (to_focus == NULL)
|
|
|
|
|
to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
|
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case D_LEFT:
|
2009-03-11 01:55:10 +01:00
|
|
|
|
/* If we’re at the left-most position, move the rest of the table right */
|
|
|
|
|
if (current_col == 0) {
|
|
|
|
|
expand_table_cols_at_head(c_ws);
|
2009-03-11 18:56:31 +01:00
|
|
|
|
new = CUR_CELL;
|
2009-03-11 01:55:10 +01:00
|
|
|
|
} else
|
|
|
|
|
new = CUR_TABLE[--current_col][current_row];
|
2009-02-14 02:33:31 +01:00
|
|
|
|
break;
|
|
|
|
|
case D_RIGHT:
|
|
|
|
|
if (current_col == (c_ws->cols-1))
|
|
|
|
|
expand_table_cols(c_ws);
|
|
|
|
|
|
|
|
|
|
new = CUR_TABLE[++current_col][current_row];
|
|
|
|
|
break;
|
|
|
|
|
case D_UP:
|
2009-03-11 18:56:31 +01:00
|
|
|
|
if (move_current_window_in_container(conn, current_client, D_UP))
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2009-03-11 18:56:31 +01:00
|
|
|
|
/* if we’re at the up-most position, move the rest of the table down */
|
|
|
|
|
if (current_row == 0) {
|
|
|
|
|
expand_table_rows_at_head(c_ws);
|
|
|
|
|
new = CUR_CELL;
|
|
|
|
|
} else
|
|
|
|
|
new = CUR_TABLE[current_col][--current_row];
|
2009-02-14 02:33:31 +01:00
|
|
|
|
break;
|
|
|
|
|
case D_DOWN:
|
2009-03-04 15:45:12 +01:00
|
|
|
|
if (move_current_window_in_container(conn, current_client, D_DOWN))
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (current_row == (c_ws->rows-1))
|
|
|
|
|
expand_table_rows(c_ws);
|
|
|
|
|
|
|
|
|
|
new = CUR_TABLE[current_col][++current_row];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Remove it from the old container and put it into the new one */
|
2009-03-06 19:08:59 +01:00
|
|
|
|
remove_client_from_container(conn, current_client, container);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
|
2009-04-01 12:02:22 +02:00
|
|
|
|
SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
/* Update data structures */
|
|
|
|
|
current_client->container = new;
|
|
|
|
|
container->currently_focused = to_focus;
|
|
|
|
|
new->currently_focused = current_client;
|
|
|
|
|
|
2009-03-09 08:14:00 +01:00
|
|
|
|
Workspace *workspace = container->workspace;
|
|
|
|
|
|
2009-02-24 20:29:30 +01:00
|
|
|
|
/* delete all empty columns/rows */
|
2009-03-09 08:14:00 +01:00
|
|
|
|
cleanup_table(conn, workspace);
|
2009-03-05 17:17:37 +01:00
|
|
|
|
|
|
|
|
|
/* Fix colspan/rowspan if it’d overlap */
|
2009-03-09 08:14:00 +01:00
|
|
|
|
fix_colrowspan(conn, workspace);
|
2009-03-05 17:17:37 +01:00
|
|
|
|
|
2009-03-04 15:45:12 +01:00
|
|
|
|
render_layout(conn);
|
2009-02-23 01:41:26 +01:00
|
|
|
|
|
2009-04-01 12:50:42 +02:00
|
|
|
|
set_focus(conn, current_client, true);
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-11 18:56:31 +01:00
|
|
|
|
static void move_current_container(xcb_connection_t *conn, direction_t direction) {
|
|
|
|
|
LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
|
|
|
|
|
(direction == D_LEFT ? "left" : "right"))));
|
|
|
|
|
/* Get current window */
|
|
|
|
|
Container *container = CUR_CELL,
|
|
|
|
|
*new = NULL;
|
|
|
|
|
|
|
|
|
|
Container **old = &CUR_CELL;
|
|
|
|
|
|
|
|
|
|
/* There has to be a container, see focus_window() */
|
|
|
|
|
assert(container != NULL);
|
|
|
|
|
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case D_LEFT:
|
|
|
|
|
/* If we’re at the left-most position, move the rest of the table right */
|
|
|
|
|
if (current_col == 0) {
|
|
|
|
|
expand_table_cols_at_head(c_ws);
|
|
|
|
|
new = CUR_CELL;
|
|
|
|
|
old = &CUR_TABLE[current_col+1][current_row];
|
|
|
|
|
} else
|
|
|
|
|
new = CUR_TABLE[--current_col][current_row];
|
|
|
|
|
break;
|
|
|
|
|
case D_RIGHT:
|
|
|
|
|
if (current_col == (c_ws->cols-1))
|
|
|
|
|
expand_table_cols(c_ws);
|
|
|
|
|
|
|
|
|
|
new = CUR_TABLE[++current_col][current_row];
|
|
|
|
|
break;
|
|
|
|
|
case D_UP:
|
|
|
|
|
/* if we’re at the up-most position, move the rest of the table down */
|
|
|
|
|
if (current_row == 0) {
|
|
|
|
|
expand_table_rows_at_head(c_ws);
|
|
|
|
|
new = CUR_CELL;
|
|
|
|
|
old = &CUR_TABLE[current_col][current_row+1];
|
|
|
|
|
} else
|
|
|
|
|
new = CUR_TABLE[current_col][--current_row];
|
|
|
|
|
break;
|
|
|
|
|
case D_DOWN:
|
|
|
|
|
if (current_row == (c_ws->rows-1))
|
|
|
|
|
expand_table_rows(c_ws);
|
|
|
|
|
|
|
|
|
|
new = CUR_TABLE[current_col][++current_row];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
|
|
|
|
|
|
|
|
|
|
/* Swap the containers */
|
|
|
|
|
int col = new->col;
|
|
|
|
|
int row = new->row;
|
|
|
|
|
|
|
|
|
|
*old = new;
|
|
|
|
|
new->col = container->col;
|
|
|
|
|
new->row = container->row;
|
|
|
|
|
|
|
|
|
|
CUR_CELL = container;
|
|
|
|
|
container->col = col;
|
|
|
|
|
container->row = row;
|
|
|
|
|
|
|
|
|
|
Workspace *workspace = container->workspace;
|
|
|
|
|
|
|
|
|
|
/* delete all empty columns/rows */
|
|
|
|
|
cleanup_table(conn, workspace);
|
|
|
|
|
|
|
|
|
|
/* Fix colspan/rowspan if it’d overlap */
|
|
|
|
|
fix_colrowspan(conn, workspace);
|
|
|
|
|
|
|
|
|
|
render_layout(conn);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-13 19:04:45 +01:00
|
|
|
|
/*
|
|
|
|
|
* "Snaps" the current container (not possible for windows, because it works at table base)
|
|
|
|
|
* to the given direction, that is, adjusts cellspan/rowspan
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 15:45:12 +01:00
|
|
|
|
static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("snapping container to direction %d\n", direction);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
Container *container = CUR_CELL;
|
|
|
|
|
|
|
|
|
|
assert(container != NULL);
|
|
|
|
|
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case D_LEFT:
|
|
|
|
|
/* Snap to the left is actually a move to the left and then a snap right */
|
2009-03-05 17:17:37 +01:00
|
|
|
|
if (!cell_exists(container->col - 1, container->row) ||
|
|
|
|
|
CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("cannot snap to left - the cell is already used\n");
|
2009-03-05 17:17:37 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 15:45:12 +01:00
|
|
|
|
move_current_window(conn, D_LEFT);
|
|
|
|
|
snap_current_container(conn, D_RIGHT);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
2009-03-05 17:23:13 +01:00
|
|
|
|
case D_RIGHT: {
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Check if the cell is used */
|
2009-03-05 17:23:13 +01:00
|
|
|
|
int new_col = container->col + container->colspan;
|
|
|
|
|
if (!cell_exists(new_col, container->row) ||
|
|
|
|
|
CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("cannot snap to right - the cell is already used\n");
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if there are other cells with rowspan, which are in our way.
|
|
|
|
|
* If so, reduce their rowspan. */
|
2009-02-15 02:30:18 +01:00
|
|
|
|
for (int i = container->row-1; i >= 0; i--) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("we got cell %d, %d with rowspan %d\n",
|
2009-03-05 17:23:13 +01:00
|
|
|
|
new_col, i, CUR_TABLE[new_col][i]->rowspan);
|
|
|
|
|
while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
|
|
|
|
|
CUR_TABLE[new_col][i]->rowspan--;
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container->colspan++;
|
|
|
|
|
break;
|
2009-03-05 17:23:13 +01:00
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
case D_UP:
|
2009-03-05 17:23:13 +01:00
|
|
|
|
if (!cell_exists(container->col, container->row - 1) ||
|
|
|
|
|
CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("cannot snap to top - the cell is already used\n");
|
2009-03-05 17:23:13 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 15:45:12 +01:00
|
|
|
|
move_current_window(conn, D_UP);
|
|
|
|
|
snap_current_container(conn, D_DOWN);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
2009-03-05 17:23:13 +01:00
|
|
|
|
case D_DOWN: {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("snapping down\n");
|
2009-03-05 17:23:13 +01:00
|
|
|
|
int new_row = container->row + container->rowspan;
|
|
|
|
|
if (!cell_exists(container->col, new_row) ||
|
|
|
|
|
CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("cannot snap down - the cell is already used\n");
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 02:30:18 +01:00
|
|
|
|
for (int i = container->col-1; i >= 0; i--) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("we got cell %d, %d with colspan %d\n",
|
2009-03-05 17:23:13 +01:00
|
|
|
|
i, new_row, CUR_TABLE[i][new_row]->colspan);
|
|
|
|
|
while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
|
|
|
|
|
CUR_TABLE[i][new_row]->colspan--;
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container->rowspan++;
|
|
|
|
|
break;
|
2009-03-05 17:23:13 +01:00
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 15:45:12 +01:00
|
|
|
|
render_layout(conn);
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 08:59:03 +01:00
|
|
|
|
/*
|
|
|
|
|
* Moves the currently selected window to the given workspace
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 15:45:12 +01:00
|
|
|
|
static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("Moving current window to workspace %d\n", workspace);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
|
|
|
|
|
Container *container = CUR_CELL;
|
|
|
|
|
|
|
|
|
|
assert(container != NULL);
|
|
|
|
|
|
|
|
|
|
/* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
|
|
|
|
|
Workspace *t_ws = &(workspaces[workspace-1]);
|
|
|
|
|
|
|
|
|
|
Client *current_client = container->currently_focused;
|
|
|
|
|
if (current_client == NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("No currently focused client in current container.\n");
|
2009-03-04 08:59:03 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
|
|
|
|
|
if (to_focus == NULL)
|
|
|
|
|
to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
|
|
|
|
|
|
|
|
|
|
if (t_ws->screen == NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("initializing new workspace, setting num to %d\n", workspace-1);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
t_ws->screen = container->workspace->screen;
|
|
|
|
|
/* Copy the dimensions from the virtual screen */
|
|
|
|
|
memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
|
|
|
|
|
|
|
|
|
|
assert(to_container != NULL);
|
|
|
|
|
|
2009-04-01 12:02:22 +02:00
|
|
|
|
remove_client_from_container(conn, current_client, container);
|
2009-03-05 01:48:30 +01:00
|
|
|
|
|
2009-03-04 08:59:03 +01:00
|
|
|
|
CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
|
2009-03-05 01:48:30 +01:00
|
|
|
|
SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("Moved.\n");
|
2009-03-04 08:59:03 +01:00
|
|
|
|
|
|
|
|
|
current_client->container = to_container;
|
|
|
|
|
container->currently_focused = to_focus;
|
|
|
|
|
to_container->currently_focused = current_client;
|
|
|
|
|
|
|
|
|
|
/* 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) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("This workspace is not visible, unmapping\n");
|
2009-03-04 15:45:12 +01:00
|
|
|
|
xcb_unmap_window(conn, current_client->frame);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* delete all empty columns/rows */
|
2009-03-04 15:45:12 +01:00
|
|
|
|
cleanup_table(conn, container->workspace);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
|
2009-03-04 15:45:12 +01:00
|
|
|
|
render_layout(conn);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-15 17:49:25 +01:00
|
|
|
|
/*
|
|
|
|
|
* Switches to the given workspace
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void show_workspace(xcb_connection_t *conn, int workspace) {
|
2009-02-14 02:33:31 +01:00
|
|
|
|
Client *client;
|
|
|
|
|
xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
|
2009-02-15 01:58:09 +01:00
|
|
|
|
/* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
|
|
|
|
|
Workspace *t_ws = &(workspaces[workspace-1]);
|
|
|
|
|
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("show_workspace(%d)\n", workspace);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
/* Store current_row/current_col */
|
|
|
|
|
c_ws->current_row = current_row;
|
|
|
|
|
c_ws->current_col = current_col;
|
|
|
|
|
|
2009-02-15 01:58:09 +01:00
|
|
|
|
/* Check if the workspace has not been used yet */
|
|
|
|
|
if (t_ws->screen == NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("initializing new workspace, setting num to %d\n", workspace);
|
2009-02-15 01:58:09 +01:00
|
|
|
|
t_ws->screen = c_ws->screen;
|
|
|
|
|
/* Copy the dimensions from the virtual screen */
|
|
|
|
|
memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c_ws->screen != t_ws->screen) {
|
|
|
|
|
/* We need to switch to the other screen first */
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("moving over to other screen.\n");
|
2009-03-15 21:13:15 +01:00
|
|
|
|
|
|
|
|
|
/* Store the old client */
|
|
|
|
|
Client *old_client = CUR_CELL->currently_focused;
|
|
|
|
|
|
2009-02-15 01:58:09 +01:00
|
|
|
|
c_ws = &(workspaces[t_ws->screen->current_workspace]);
|
|
|
|
|
current_col = c_ws->current_col;
|
|
|
|
|
current_row = c_ws->current_row;
|
|
|
|
|
if (CUR_CELL->currently_focused != NULL)
|
|
|
|
|
warp_pointer_into(conn, CUR_CELL->currently_focused);
|
|
|
|
|
else {
|
|
|
|
|
Rect *dims = &(c_ws->screen->rect);
|
|
|
|
|
xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
|
|
|
|
|
dims->x + (dims->width / 2), dims->y + (dims->height / 2));
|
|
|
|
|
}
|
2009-03-15 21:13:15 +01:00
|
|
|
|
|
|
|
|
|
/* Re-decorate the old client, it’s not focused anymore */
|
|
|
|
|
if ((old_client != NULL) && !old_client->dock)
|
|
|
|
|
redecorate_window(conn, old_client);
|
|
|
|
|
else xcb_flush(conn);
|
2009-02-15 01:58:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if we need to change something or if we’re already there */
|
2009-04-08 13:48:37 +02:00
|
|
|
|
if (c_ws->screen->current_workspace == (workspace-1)) {
|
|
|
|
|
if (CUR_CELL->currently_focused != NULL)
|
|
|
|
|
set_focus(conn, CUR_CELL->currently_focused, true);
|
2009-02-15 01:58:09 +01:00
|
|
|
|
return;
|
2009-04-08 13:48:37 +02:00
|
|
|
|
}
|
2009-02-15 01:58:09 +01:00
|
|
|
|
|
|
|
|
|
t_ws->screen->current_workspace = workspace-1;
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* TODO: does grabbing the server actually bring us any (speed)advantages? */
|
|
|
|
|
//xcb_grab_server(conn);
|
|
|
|
|
|
2009-03-11 00:20:56 +01:00
|
|
|
|
ignore_enter_notify_forall(conn, c_ws, true);
|
|
|
|
|
|
2009-02-15 01:58:09 +01:00
|
|
|
|
/* Unmap all clients of the current workspace */
|
2009-03-04 22:52:36 +01:00
|
|
|
|
int unmapped_clients = 0;
|
2009-03-11 00:20:56 +01:00
|
|
|
|
FOR_TABLE(c_ws)
|
|
|
|
|
CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
|
|
|
|
|
xcb_unmap_window(conn, client->frame);
|
|
|
|
|
unmapped_clients++;
|
|
|
|
|
}
|
2009-03-04 22:52:36 +01:00
|
|
|
|
|
|
|
|
|
/* If we did not unmap any clients, the workspace is empty and we can destroy it */
|
|
|
|
|
if (unmapped_clients == 0)
|
|
|
|
|
c_ws->screen = NULL;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
2009-02-25 02:05:08 +01:00
|
|
|
|
/* Unmap the stack windows on the current workspace, if any */
|
|
|
|
|
struct Stack_Window *stack_win;
|
|
|
|
|
SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
|
|
|
|
|
if (stack_win->container->workspace == c_ws)
|
|
|
|
|
xcb_unmap_window(conn, stack_win->window);
|
|
|
|
|
|
2009-03-11 00:20:56 +01:00
|
|
|
|
ignore_enter_notify_forall(conn, c_ws, false);
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
c_ws = &workspaces[workspace-1];
|
|
|
|
|
current_row = c_ws->current_row;
|
|
|
|
|
current_col = c_ws->current_col;
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("new current row = %d, current col = %d\n", current_row, current_col);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
2009-03-11 00:20:56 +01:00
|
|
|
|
ignore_enter_notify_forall(conn, c_ws, true);
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Map all clients on the new workspace */
|
2009-03-11 00:20:56 +01:00
|
|
|
|
FOR_TABLE(c_ws)
|
|
|
|
|
CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
|
|
|
|
|
xcb_map_window(conn, client->frame);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
2009-02-25 02:05:08 +01:00
|
|
|
|
/* Map all stack windows, if any */
|
|
|
|
|
SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
|
|
|
|
|
if (stack_win->container->workspace == c_ws)
|
|
|
|
|
xcb_map_window(conn, stack_win->window);
|
|
|
|
|
|
2009-03-11 00:20:56 +01:00
|
|
|
|
ignore_enter_notify_forall(conn, c_ws, false);
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Restore focus on the new workspace */
|
|
|
|
|
if (CUR_CELL->currently_focused != NULL)
|
2009-04-01 12:50:42 +02:00
|
|
|
|
set_focus(conn, CUR_CELL->currently_focused, true);
|
2009-02-15 01:58:09 +01:00
|
|
|
|
else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
|
|
|
|
|
//xcb_ungrab_server(conn);
|
|
|
|
|
|
|
|
|
|
render_layout(conn);
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Parses a command, see file CMDMODE for more information
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void parse_command(xcb_connection_t *conn, const char *command) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("--- parsing command \"%s\" ---\n", command);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Hmm, just to be sure */
|
|
|
|
|
if (command[0] == '\0')
|
|
|
|
|
return;
|
|
|
|
|
|
2009-03-14 21:31:22 +01:00
|
|
|
|
/* Is it an <exec>? Then execute the given command. */
|
|
|
|
|
if (STARTS_WITH(command, "exec ")) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("starting \"%s\"\n", command + strlen("exec "));
|
2009-02-14 02:33:31 +01:00
|
|
|
|
start_application(command+strlen("exec "));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-14 21:31:22 +01:00
|
|
|
|
/* Is it an <exit>? */
|
2009-03-14 22:09:36 +01:00
|
|
|
|
if (STARTS_WITH(command, "exit")) {
|
|
|
|
|
LOG("User issued exit-command, exiting without error.\n");
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
2009-03-14 21:31:22 +01:00
|
|
|
|
|
|
|
|
|
/* Is it <restart>? Then restart in place. */
|
|
|
|
|
if (STARTS_WITH(command, "restart")) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("restarting \"%s\"...\n", application_path);
|
2009-02-27 22:40:48 +01:00
|
|
|
|
execl(application_path, application_path, NULL);
|
|
|
|
|
/* not reached */
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-14 22:09:36 +01:00
|
|
|
|
if (STARTS_WITH(command, "kill")) {
|
|
|
|
|
if (CUR_CELL->currently_focused == NULL) {
|
|
|
|
|
LOG("There is no window to kill\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG("Killing current window\n");
|
|
|
|
|
kill_window(conn, CUR_CELL->currently_focused);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 20:12:50 +01:00
|
|
|
|
/* Is it 'f' for fullscreen? */
|
|
|
|
|
if (command[0] == 'f') {
|
|
|
|
|
if (CUR_CELL->currently_focused == NULL)
|
|
|
|
|
return;
|
|
|
|
|
toggle_fullscreen(conn, CUR_CELL->currently_focused);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-24 00:30:04 +01:00
|
|
|
|
/* Is it just 's' for stacking or 'd' for default? */
|
|
|
|
|
if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("Switching mode for current container\n");
|
2009-02-24 00:30:04 +01:00
|
|
|
|
switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 23:45:44 +01:00
|
|
|
|
enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Is it a <with>? */
|
|
|
|
|
if (command[0] == 'w') {
|
2009-03-04 23:45:44 +01:00
|
|
|
|
command++;
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* TODO: implement */
|
2009-03-04 23:45:44 +01:00
|
|
|
|
if (command[0] == 'c') {
|
|
|
|
|
with = WITH_CONTAINER;
|
|
|
|
|
command++;
|
|
|
|
|
} else {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("not yet implemented.\n");
|
2009-03-04 23:45:44 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* It's a normal <cmd> */
|
|
|
|
|
char *rest = NULL;
|
|
|
|
|
enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
|
|
|
|
|
direction_t direction;
|
2009-03-04 08:59:03 +01:00
|
|
|
|
int times = strtol(command, &rest, 10);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
if (rest == NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("Invalid command (\"%s\")\n", command);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*rest == '\0') {
|
|
|
|
|
/* No rest? This was a tag number, not a times specification */
|
|
|
|
|
show_workspace(conn, times);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-28 02:46:36 +01:00
|
|
|
|
if (*rest == 'm' || *rest == 's') {
|
|
|
|
|
action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
|
|
|
|
|
rest++;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 08:59:03 +01:00
|
|
|
|
int workspace = strtol(rest, &rest, 10);
|
|
|
|
|
|
|
|
|
|
if (rest == NULL) {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("Invalid command (\"%s\")\n", command);
|
2009-03-04 08:59:03 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*rest == '\0') {
|
|
|
|
|
move_current_window_to_workspace(conn, workspace);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
|
/* Now perform action to <where> */
|
|
|
|
|
while (*rest != '\0') {
|
|
|
|
|
if (*rest == 'h')
|
|
|
|
|
direction = D_LEFT;
|
|
|
|
|
else if (*rest == 'j')
|
|
|
|
|
direction = D_DOWN;
|
|
|
|
|
else if (*rest == 'k')
|
|
|
|
|
direction = D_UP;
|
|
|
|
|
else if (*rest == 'l')
|
|
|
|
|
direction = D_RIGHT;
|
|
|
|
|
else {
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("unknown direction: %c\n", *rest);
|
2009-02-14 02:33:31 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-11 18:56:31 +01:00
|
|
|
|
if (action == ACTION_FOCUS)
|
2009-03-04 23:45:44 +01:00
|
|
|
|
focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
|
2009-03-11 18:56:31 +01:00
|
|
|
|
else if (action == ACTION_MOVE) {
|
|
|
|
|
if (with == WITH_WINDOW)
|
|
|
|
|
move_current_window(conn, direction);
|
|
|
|
|
else move_current_container(conn, direction);
|
2009-03-04 23:45:44 +01:00
|
|
|
|
}
|
2009-02-14 02:33:31 +01:00
|
|
|
|
else if (action == ACTION_SNAP)
|
|
|
|
|
snap_current_container(conn, direction);
|
|
|
|
|
|
|
|
|
|
rest++;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 06:46:43 +01:00
|
|
|
|
LOG("--- done ---\n");
|
2009-02-13 19:04:45 +01:00
|
|
|
|
}
|