2009-02-13 20:33:31 -05:00
|
|
|
/*
|
|
|
|
* vim:ts=8:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
*
|
2009-02-14 21:07:29 -05:00
|
|
|
* © 2009 Michael Stapelberg and contributors
|
2009-02-13 20:33:31 -05:00
|
|
|
*
|
|
|
|
* See file LICENSE for license information.
|
|
|
|
*
|
2009-02-14 21:07:29 -05:00
|
|
|
* table.c: Functions/macros for easy modifying/accessing of _the_ table (defining our
|
|
|
|
* layout).
|
2009-02-07 22:04:35 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "data.h"
|
|
|
|
#include "table.h"
|
2009-02-27 19:12:05 -05:00
|
|
|
#include "util.h"
|
2009-03-04 15:30:02 -05:00
|
|
|
#include "i3.h"
|
2009-02-07 22:04:35 -05:00
|
|
|
|
2009-02-12 23:16:47 -05:00
|
|
|
int current_workspace = 0;
|
|
|
|
Workspace workspaces[10];
|
|
|
|
/* Convenience pointer to the current workspace */
|
|
|
|
Workspace *c_ws = &workspaces[0];
|
2009-02-13 13:04:45 -05:00
|
|
|
int current_col = 0;
|
|
|
|
int current_row = 0;
|
2009-02-07 22:04:35 -05:00
|
|
|
|
2009-02-08 05:25:32 -05:00
|
|
|
/*
|
|
|
|
* Initialize table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-07 22:04:35 -05:00
|
|
|
void init_table() {
|
2009-02-13 20:33:31 -05:00
|
|
|
memset(workspaces, 0, sizeof(workspaces));
|
2009-02-12 23:16:47 -05:00
|
|
|
|
2009-02-14 20:30:18 -05:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2009-02-14 19:58:09 -05:00
|
|
|
workspaces[i].screen = NULL;
|
2009-03-04 02:59:03 -05:00
|
|
|
workspaces[i].num = i;
|
2009-02-22 18:18:13 -05:00
|
|
|
SLIST_INIT(&(workspaces[i].dock_clients));
|
2009-02-13 20:33:31 -05:00
|
|
|
expand_table_cols(&(workspaces[i]));
|
|
|
|
expand_table_rows(&(workspaces[i]));
|
|
|
|
}
|
2009-02-07 22:04:35 -05:00
|
|
|
}
|
|
|
|
|
2009-03-04 15:00:32 -05:00
|
|
|
static void new_container(Workspace *workspace, Container **container, int col, int row) {
|
2009-02-13 20:33:31 -05:00
|
|
|
Container *new;
|
|
|
|
new = *container = calloc(sizeof(Container), 1);
|
|
|
|
CIRCLEQ_INIT(&(new->clients));
|
|
|
|
new->colspan = 1;
|
|
|
|
new->rowspan = 1;
|
2009-03-04 15:00:32 -05:00
|
|
|
new->col = col;
|
|
|
|
new->row = row;
|
2009-02-14 02:38:07 -05:00
|
|
|
new->workspace = workspace;
|
2009-02-09 23:50:35 -05:00
|
|
|
}
|
|
|
|
|
2009-02-08 05:25:32 -05:00
|
|
|
/*
|
|
|
|
* Add one row to the table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-12 23:16:47 -05:00
|
|
|
void expand_table_rows(Workspace *workspace) {
|
2009-02-13 20:33:31 -05:00
|
|
|
workspace->rows++;
|
2009-02-07 22:04:35 -05:00
|
|
|
|
2009-02-14 20:30:18 -05:00
|
|
|
for (int c = 0; c < workspace->cols; c++) {
|
2009-02-13 20:33:31 -05:00
|
|
|
workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
|
2009-03-04 15:00:32 -05:00
|
|
|
new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
|
2009-02-13 20:33:31 -05:00
|
|
|
}
|
2009-02-07 22:04:35 -05:00
|
|
|
}
|
|
|
|
|
2009-02-08 05:25:32 -05:00
|
|
|
/*
|
|
|
|
* Add one column to the table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-12 23:16:47 -05:00
|
|
|
void expand_table_cols(Workspace *workspace) {
|
2009-02-13 20:33:31 -05:00
|
|
|
workspace->cols++;
|
2009-02-12 23:16:47 -05:00
|
|
|
|
2009-02-13 20:33:31 -05:00
|
|
|
workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
|
|
|
|
workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
|
2009-02-14 20:30:18 -05:00
|
|
|
for (int c = 0; c < workspace->rows; c++)
|
2009-03-04 15:00:32 -05:00
|
|
|
new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
|
2009-02-07 22:04:35 -05:00
|
|
|
}
|
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
/*
|
|
|
|
* Shrinks the table by one column.
|
|
|
|
*
|
|
|
|
* The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
|
|
|
|
* function may only be called from move_*() or after making sure that the containers are freed
|
|
|
|
* properly.
|
|
|
|
*
|
|
|
|
*/
|
2009-02-24 14:29:30 -05:00
|
|
|
static void shrink_table_cols(Workspace *workspace) {
|
|
|
|
workspace->cols--;
|
2009-03-09 01:49:11 -04:00
|
|
|
|
|
|
|
/* Free the container-pointers */
|
2009-02-24 14:29:30 -05:00
|
|
|
free(workspace->table[workspace->cols]);
|
2009-03-09 01:49:11 -04:00
|
|
|
|
|
|
|
/* Re-allocate the table */
|
2009-02-24 14:29:30 -05:00
|
|
|
workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
|
|
|
|
}
|
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
/*
|
|
|
|
* See shrink_table_cols()
|
|
|
|
*
|
|
|
|
*/
|
2009-02-24 14:29:30 -05:00
|
|
|
static void shrink_table_rows(Workspace *workspace) {
|
|
|
|
workspace->rows--;
|
2009-03-09 03:14:00 -04:00
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
2009-02-24 14:29:30 -05:00
|
|
|
workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-07 22:04:35 -05:00
|
|
|
/*
|
|
|
|
* Performs simple bounds checking for the given column/row
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool cell_exists(int col, int row) {
|
2009-02-22 19:41:26 -05:00
|
|
|
return (col >= 0 && col < c_ws->cols) &&
|
2009-02-24 14:29:30 -05:00
|
|
|
(row >= 0 && row < c_ws->rows);
|
|
|
|
}
|
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
|
|
|
|
Container *old_container = workspace->table[col][row];
|
2009-03-04 14:41:54 -05:00
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
if (old_container->mode == MODE_STACK)
|
|
|
|
leave_stack_mode(conn, old_container);
|
2009-03-04 14:41:54 -05:00
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
free(old_container);
|
|
|
|
}
|
2009-03-04 15:30:02 -05:00
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
|
|
|
|
LOG("firstly freeing \n");
|
|
|
|
|
|
|
|
/* Free the columns which are cleaned up */
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++)
|
|
|
|
free_container(conn, workspace, cols-1, rows);
|
2009-03-04 19:20:13 -05:00
|
|
|
|
|
|
|
for (; cols < workspace->cols; cols++)
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("at col = %d, row = %d\n", cols, rows);
|
2009-03-04 19:20:13 -05:00
|
|
|
Container *new_container = workspace->table[cols][rows];
|
2009-02-24 14:29:30 -05:00
|
|
|
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
|
2009-03-04 14:41:54 -05:00
|
|
|
workspace->table[cols-1][rows] = new_container;
|
|
|
|
|
|
|
|
new_container->row = rows;
|
|
|
|
new_container->col = cols-1;
|
2009-02-24 14:29:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 15:30:02 -05:00
|
|
|
static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
|
2009-03-09 03:14:00 -04:00
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
|
|
|
free_container(conn, workspace, cols, rows-1);
|
2009-03-04 14:41:54 -05:00
|
|
|
|
2009-03-04 19:20:13 -05:00
|
|
|
for (; rows < workspace->rows; rows++)
|
|
|
|
for (int cols = 0; cols < workspace->cols; cols++) {
|
|
|
|
Container *new_container = workspace->table[cols][rows];
|
2009-02-24 14:29:30 -05:00
|
|
|
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
|
2009-03-04 14:41:54 -05:00
|
|
|
workspace->table[cols][rows-1] = new_container;
|
2009-03-04 19:20:13 -05:00
|
|
|
|
2009-03-04 14:41:54 -05:00
|
|
|
new_container->row = rows-1;
|
|
|
|
new_container->col = cols;
|
2009-02-24 14:29:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 11:17:37 -05:00
|
|
|
void dump_table(xcb_connection_t *conn, Workspace *workspace) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("dump_table()\n");
|
2009-03-10 19:20:56 -04:00
|
|
|
FOR_TABLE(workspace) {
|
|
|
|
Container *con = workspace->table[cols][rows];
|
|
|
|
LOG("----\n");
|
|
|
|
LOG("at col=%d, row=%d\n", cols, rows);
|
|
|
|
LOG("currently_focused = %p\n", con->currently_focused);
|
|
|
|
Client *loop;
|
|
|
|
CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
|
|
|
|
LOG("got client %08x / %s\n", loop->child, loop->name);
|
2009-03-05 11:17:37 -05:00
|
|
|
}
|
2009-03-10 19:20:56 -04:00
|
|
|
LOG("----\n");
|
2009-03-05 11:17:37 -05:00
|
|
|
}
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("done\n");
|
2009-03-05 11:17:37 -05:00
|
|
|
}
|
|
|
|
|
2009-02-24 14:29:30 -05:00
|
|
|
/*
|
|
|
|
* Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
|
|
|
|
*
|
|
|
|
*/
|
2009-02-27 19:12:05 -05:00
|
|
|
void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("cleanup_table()\n");
|
2009-03-02 21:40:57 -05:00
|
|
|
|
|
|
|
/* Check for empty columns if we got more than one column */
|
|
|
|
for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
|
2009-02-24 14:29:30 -05:00
|
|
|
bool completely_empty = true;
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++)
|
|
|
|
if (workspace->table[cols][rows]->currently_focused != NULL) {
|
|
|
|
completely_empty = false;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-02 21:40:57 -05:00
|
|
|
if (completely_empty) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("Removing completely empty column %d\n", cols);
|
2009-02-24 14:29:30 -05:00
|
|
|
if (cols < (workspace->cols - 1))
|
2009-03-04 15:30:02 -05:00
|
|
|
move_columns_from(conn, workspace, cols+1);
|
2009-03-09 03:14:00 -04:00
|
|
|
else {
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++)
|
|
|
|
free_container(conn, workspace, cols, rows);
|
|
|
|
}
|
2009-02-24 14:29:30 -05:00
|
|
|
shrink_table_cols(workspace);
|
2009-03-02 21:40:57 -05:00
|
|
|
|
|
|
|
if (workspace->current_col >= workspace->cols)
|
|
|
|
workspace->current_col = workspace->cols - 1;
|
2009-02-24 14:29:30 -05:00
|
|
|
} else cols++;
|
|
|
|
}
|
|
|
|
|
2009-03-09 03:14:00 -04:00
|
|
|
/* Check for empty rows if we got more than one row */
|
2009-03-02 21:40:57 -05:00
|
|
|
for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
|
2009-02-24 14:29:30 -05:00
|
|
|
bool completely_empty = true;
|
2009-03-09 03:14:00 -04:00
|
|
|
LOG("Checking row %d\n", rows);
|
2009-02-24 14:29:30 -05:00
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
|
|
|
if (workspace->table[cols][rows]->currently_focused != NULL) {
|
|
|
|
completely_empty = false;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-02 21:40:57 -05:00
|
|
|
if (completely_empty) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("Removing completely empty row %d\n", rows);
|
2009-02-24 14:29:30 -05:00
|
|
|
if (rows < (workspace->rows - 1))
|
2009-03-04 15:30:02 -05:00
|
|
|
move_rows_from(conn, workspace, rows+1);
|
2009-03-09 03:14:00 -04:00
|
|
|
else {
|
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
|
|
|
free_container(conn, workspace, cols, rows);
|
|
|
|
}
|
2009-02-24 14:29:30 -05:00
|
|
|
shrink_table_rows(workspace);
|
2009-03-02 21:40:57 -05:00
|
|
|
|
|
|
|
if (workspace->current_row >= workspace->rows)
|
|
|
|
workspace->current_row = workspace->rows - 1;
|
2009-02-24 14:29:30 -05:00
|
|
|
} else rows++;
|
|
|
|
}
|
2009-02-26 16:23:41 -05:00
|
|
|
|
|
|
|
/* Boundary checking for current_col and current_row */
|
|
|
|
if (current_col >= c_ws->cols)
|
|
|
|
current_col = c_ws->cols-1;
|
|
|
|
|
|
|
|
if (current_row >= c_ws->rows)
|
|
|
|
current_row = c_ws->rows-1;
|
2009-02-27 19:12:05 -05:00
|
|
|
|
|
|
|
if (CUR_CELL->currently_focused != NULL)
|
|
|
|
set_focus(conn, CUR_CELL->currently_focused);
|
2009-02-07 22:04:35 -05:00
|
|
|
}
|
2009-03-05 11:17:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fixes col/rowspan (makes sure there are no overlapping windows)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
|
2009-03-06 00:46:43 -05:00
|
|
|
LOG("Fixing col/rowspan\n");
|
2009-03-05 11:17:37 -05:00
|
|
|
|
2009-03-10 19:20:56 -04:00
|
|
|
FOR_TABLE(workspace) {
|
|
|
|
Container *con = workspace->table[cols][rows];
|
|
|
|
if (con->colspan > 1) {
|
|
|
|
LOG("gots one with colspan %d\n", con->colspan);
|
|
|
|
while (con->colspan > 1 &&
|
|
|
|
workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
|
|
|
|
con->colspan--;
|
|
|
|
LOG("fixed it to %d\n", con->colspan);
|
2009-03-05 11:17:37 -05:00
|
|
|
}
|
2009-03-10 19:20:56 -04:00
|
|
|
if (con->rowspan > 1) {
|
|
|
|
LOG("gots one with rowspan %d\n", con->rowspan);
|
|
|
|
while (con->rowspan > 1 &&
|
|
|
|
workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
|
|
|
|
con->rowspan--;
|
|
|
|
LOG("fixed it to %d\n", con->rowspan);
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 11:17:37 -05:00
|
|
|
}
|