]> git.sur5r.net Git - i3/i3/blob - table.c
d689ce72c0c4d92d8301ee0313d3d586e99f18c4
[i3/i3] / 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 /* This is a two-dimensional dynamic array of Container-pointers. I’ve always wanted
18  * to be a three-star programmer :) */
19 Container ***table = NULL;
20
21 struct table_dimensions_t table_dims = {0, 0};
22
23 /*
24  * Initialize table
25  *
26  */
27 void init_table() {
28         expand_table_cols();
29         expand_table_rows();
30 }
31
32 static void new_container(Container **container) {
33         Container *new;
34         new = *container = calloc(sizeof(Container), 1);
35         CIRCLEQ_INIT(&(new->clients));
36         new->colspan = 1;
37         new->rowspan = 1;
38 }
39
40 /*
41  * Add one row to the table
42  *
43  */
44 void expand_table_rows() {
45         int c;
46
47         table_dims.y++;
48
49         for (c = 0; c < table_dims.x; c++) {
50                 table[c] = realloc(table[c], sizeof(Container*) * table_dims.y);
51                 new_container(&(table[c][table_dims.y-1]));
52         }
53 }
54
55 /*
56  * Add one column to the table
57  *
58  */
59 void expand_table_cols() {
60         int c;
61
62         table_dims.x++;
63         table = realloc(table, sizeof(Container**) * table_dims.x);
64         table[table_dims.x-1] = calloc(sizeof(Container*) * table_dims.y, 1);
65         for (c = 0; c < table_dims.y; c++)
66                 new_container(&(table[table_dims.x-1][c]));
67 }
68
69 /*
70  * Performs simple bounds checking for the given column/row
71  *
72  */
73 bool cell_exists(int col, int row) {
74         return (col >= 0 && col < table_dims.x) &&
75                 (row >= 0 && row < table_dims.y);
76 }