]> git.sur5r.net Git - i3/i3/blob - src/table.c
Move stuff to include/ and src/
[i3/i3] / src / table.c
1 /*
2  * This file provides functions for easier accessing of _the_ table
3  *
4  */
5 #include <stdio.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include <stdbool.h>
12 #include <assert.h>
13
14 #include "data.h"
15 #include "table.h"
16
17 int current_workspace = 0;
18 Workspace workspaces[10];
19 /* Convenience pointer to the current workspace */
20 Workspace *c_ws = &workspaces[0];
21 int current_col = 0;
22 int current_row = 0;
23
24 /*
25  * Initialize table
26  *
27  */
28 void init_table() {
29         int i;
30         memset(workspaces, 0, sizeof(workspaces));
31
32         for (i = 0; i < 10; i++) {
33                 expand_table_cols(&(workspaces[i]));
34                 expand_table_rows(&(workspaces[i]));
35         }
36 }
37
38 static void new_container(Container **container) {
39         Container *new;
40         new = *container = calloc(sizeof(Container), 1);
41         CIRCLEQ_INIT(&(new->clients));
42         new->colspan = 1;
43         new->rowspan = 1;
44 }
45
46 /*
47  * Add one row to the table
48  *
49  */
50 void expand_table_rows(Workspace *workspace) {
51         int c;
52
53         workspace->rows++;
54
55         for (c = 0; c < workspace->cols; c++) {
56                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
57                 new_container(&(workspace->table[c][workspace->rows-1]));
58         }
59 }
60
61 /*
62  * Add one column to the table
63  *
64  */
65 void expand_table_cols(Workspace *workspace) {
66         int c;
67
68         workspace->cols++;
69
70         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
71         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
72         for (c = 0; c < workspace->rows; c++)
73                 new_container(&(workspace->table[workspace->cols-1][c]));
74 }
75
76 /*
77  * Performs simple bounds checking for the given column/row
78  *
79  */
80 bool cell_exists(int col, int row) {
81         return (col >= 0 && col < c_ws->rows) &&
82                 (row >= 0 && row < c_ws->cols);
83 }