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-15 03:07:29 +01:00
|
|
|
* table.c: Functions/macros for easy modifying/accessing of _the_ table (defining our
|
|
|
|
* layout).
|
2009-02-08 04:04:35 +01: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-13 05:16:47 +01:00
|
|
|
int current_workspace = 0;
|
|
|
|
Workspace workspaces[10];
|
|
|
|
/* Convenience pointer to the current workspace */
|
|
|
|
Workspace *c_ws = &workspaces[0];
|
2009-02-13 19:04:45 +01:00
|
|
|
int current_col = 0;
|
|
|
|
int current_row = 0;
|
2009-02-08 04:04:35 +01:00
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
/*
|
|
|
|
* Initialize table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-08 04:04:35 +01:00
|
|
|
void init_table() {
|
2009-02-14 02:33:31 +01:00
|
|
|
memset(workspaces, 0, sizeof(workspaces));
|
2009-02-13 05:16:47 +01:00
|
|
|
|
2009-02-15 02:30:18 +01:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2009-02-15 01:58:09 +01:00
|
|
|
workspaces[i].screen = NULL;
|
2009-02-23 00:18:13 +01:00
|
|
|
SLIST_INIT(&(workspaces[i].dock_clients));
|
2009-02-14 02:33:31 +01:00
|
|
|
expand_table_cols(&(workspaces[i]));
|
|
|
|
expand_table_rows(&(workspaces[i]));
|
|
|
|
}
|
2009-02-08 04:04:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-14 08:38:07 +01:00
|
|
|
static void new_container(Workspace *workspace, Container **container) {
|
2009-02-14 02:33:31 +01:00
|
|
|
Container *new;
|
|
|
|
new = *container = calloc(sizeof(Container), 1);
|
|
|
|
CIRCLEQ_INIT(&(new->clients));
|
|
|
|
new->colspan = 1;
|
|
|
|
new->rowspan = 1;
|
2009-02-14 08:38:07 +01:00
|
|
|
new->workspace = workspace;
|
2009-02-10 05:50:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
/*
|
|
|
|
* Add one row to the table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-13 05:16:47 +01:00
|
|
|
void expand_table_rows(Workspace *workspace) {
|
2009-02-14 02:33:31 +01:00
|
|
|
workspace->rows++;
|
2009-02-08 04:04:35 +01:00
|
|
|
|
2009-02-15 02:30:18 +01:00
|
|
|
for (int c = 0; c < workspace->cols; c++) {
|
2009-02-14 02:33:31 +01:00
|
|
|
workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
|
2009-02-14 08:38:07 +01:00
|
|
|
new_container(workspace, &(workspace->table[c][workspace->rows-1]));
|
2009-02-14 02:33:31 +01:00
|
|
|
}
|
2009-02-08 04:04:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
/*
|
|
|
|
* Add one column to the table
|
|
|
|
*
|
|
|
|
*/
|
2009-02-13 05:16:47 +01:00
|
|
|
void expand_table_cols(Workspace *workspace) {
|
2009-02-14 02:33:31 +01:00
|
|
|
workspace->cols++;
|
2009-02-13 05:16:47 +01:00
|
|
|
|
2009-02-14 02:33:31 +01:00
|
|
|
workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
|
|
|
|
workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
|
2009-02-15 02:30:18 +01:00
|
|
|
for (int c = 0; c < workspace->rows; c++)
|
2009-02-14 08:38:07 +01:00
|
|
|
new_container(workspace, &(workspace->table[workspace->cols-1][c]));
|
2009-02-08 04:04:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-24 20:29:30 +01:00
|
|
|
static void shrink_table_cols(Workspace *workspace) {
|
|
|
|
workspace->cols--;
|
|
|
|
free(workspace->table[workspace->cols]);
|
|
|
|
workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shrink_table_rows(Workspace *workspace) {
|
|
|
|
workspace->rows--;
|
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
|
|
|
workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-08 04:04:35 +01:00
|
|
|
/*
|
|
|
|
* Performs simple bounds checking for the given column/row
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool cell_exists(int col, int row) {
|
2009-02-23 01:41:26 +01:00
|
|
|
return (col >= 0 && col < c_ws->cols) &&
|
2009-02-24 20:29:30 +01:00
|
|
|
(row >= 0 && row < c_ws->rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_columns_from(Workspace *workspace, int cols) {
|
|
|
|
for (; cols < workspace->cols; cols++)
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++) {
|
|
|
|
free(workspace->table[cols-1][rows]);
|
|
|
|
|
|
|
|
printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
|
|
|
|
workspace->table[cols-1][rows] = workspace->table[cols][rows];
|
|
|
|
workspace->table[cols][rows] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_rows_from(Workspace *workspace, int rows) {
|
|
|
|
for (; rows < workspace->rows; rows++)
|
|
|
|
for (int cols = 0; cols < workspace->cols; cols++) {
|
|
|
|
free(workspace->table[cols][rows-1]);
|
|
|
|
|
|
|
|
printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
|
|
|
|
workspace->table[cols][rows-1] = workspace->table[cols][rows];
|
|
|
|
workspace->table[cols][rows] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void cleanup_table(Workspace *workspace) {
|
|
|
|
/* Check for empty columns */
|
|
|
|
for (int cols = 0; cols < workspace->cols;) {
|
|
|
|
bool completely_empty = true;
|
|
|
|
for (int rows = 0; rows < workspace->rows; rows++)
|
|
|
|
if (workspace->table[cols][rows]->currently_focused != NULL) {
|
|
|
|
completely_empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (completely_empty && cols > 0) {
|
|
|
|
printf("Removing completely empty column %d\n", cols);
|
|
|
|
if (cols < (workspace->cols - 1))
|
|
|
|
move_columns_from(workspace, cols+1);
|
|
|
|
shrink_table_cols(workspace);
|
|
|
|
} else cols++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for empty rows */
|
|
|
|
for (int rows = 0; rows < workspace->rows;) {
|
|
|
|
bool completely_empty = true;
|
|
|
|
for (int cols = 0; cols < workspace->cols; cols++)
|
|
|
|
if (workspace->table[cols][rows]->currently_focused != NULL) {
|
|
|
|
completely_empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (completely_empty && rows > 0) {
|
|
|
|
printf("Removing completely empty row %d\n", rows);
|
|
|
|
if (rows < (workspace->rows - 1))
|
|
|
|
move_rows_from(workspace, rows+1);
|
|
|
|
shrink_table_rows(workspace);
|
|
|
|
} else rows++;
|
|
|
|
}
|
2009-02-26 22:23:41 +01: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-08 04:04:35 +01:00
|
|
|
}
|