add (terrible) support for auto window resizing
This commit is contained in:
parent
a90b99baa3
commit
373081a1b3
4 changed files with 94 additions and 90 deletions
|
|
@ -15,6 +15,8 @@ enum crs_termmode {
|
|||
TMODE_NORAW,
|
||||
};
|
||||
|
||||
#define termsize(width, height) (width=COLS); (height=LINES)
|
||||
|
||||
int termmode(const enum crs_termmode mode);
|
||||
|
||||
void init_ncurses(void);
|
||||
|
|
|
|||
50
cli/main.c
50
cli/main.c
|
|
@ -23,60 +23,24 @@ int main(int argc, char **argv) {
|
|||
// spawnchild(&child);
|
||||
|
||||
struct crs_tree *tree;
|
||||
init_tree(&tree);
|
||||
ct_init(&tree);
|
||||
|
||||
init_pair(1, COLOR_CYAN, COLOR_CYAN);
|
||||
init_pair(2, COLOR_RED, COLOR_RED);
|
||||
init_pair(3, COLOR_MAGENTA, COLOR_MAGENTA);
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW);
|
||||
init_pair(5, COLOR_GREEN, COLOR_GREEN);
|
||||
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
/* BUG TODO : PROGRAM SHITS ITSELF IF RATIO MAKES WIDTH/HEIGHT 0, MAYBE CLAMP? : TODO BUG*/
|
||||
bifurcate_window_node(&tree->root, AXIS_X, FALSE, 0.5);
|
||||
bifurcate_window_node(&tree->root->child[0], AXIS_X, FALSE, 0.05);
|
||||
bifurcate_window_node(&tree->root->child[1], AXIS_Y, FALSE, 0.7);
|
||||
bifurcate_window_node(&tree->root->child[1]->child[1], AXIS_X, FALSE, 0.9);
|
||||
|
||||
wbkgd(tree->root->child[0]->child[0]->win, COLOR_PAIR(5));
|
||||
wbkgd(tree->root->child[0]->child[1]->win, COLOR_PAIR(3));
|
||||
wbkgd(tree->root->child[1]->child[0]->win, COLOR_PAIR(2));
|
||||
wbkgd(tree->root->child[1]->child[1]->child[0]->win, COLOR_PAIR(1));
|
||||
wbkgd(tree->root->child[1]->child[1]->child[1]->win, COLOR_PAIR(4));
|
||||
wrefresh(tree->root->child[0]->child[0]->win);
|
||||
wrefresh(tree->root->child[0]->child[1]->win);
|
||||
wrefresh(tree->root->child[1]->child[0]->win);
|
||||
wrefresh(tree->root->child[1]->child[1]->child[0]->win);
|
||||
wrefresh(tree->root->child[1]->child[1]->child[1]->win);
|
||||
|
||||
/* NOTE : THIS IS VALID : NOTE */
|
||||
// refresh();
|
||||
// WINDOW *my_win = newwin(0, 0, 0, 0);
|
||||
// wbkgd(my_win, COLOR_PAIR(1));
|
||||
// wrefresh(my_win);
|
||||
|
||||
/* NOTE : THIS IS VALID : NOTE */
|
||||
// wbkgd(tree->root->win, COLOR_PAIR(2));
|
||||
// wrefresh(tree->root->win);
|
||||
wbkgd(tree->root->child[0]->win, COLOR_PAIR(1));
|
||||
wbkgd(tree->root->child[1]->win, COLOR_PAIR(2));
|
||||
wrefresh(tree->root->child[0]->win);
|
||||
wrefresh(tree->root->child[1]->win);
|
||||
|
||||
while (1) {
|
||||
ct_update(tree);
|
||||
// refresh();
|
||||
usleep(100000);
|
||||
// usleep(100000);
|
||||
}
|
||||
|
||||
destroy_tree(tree);
|
||||
// endwin();
|
||||
// free(tree);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
124
cli/tree.c
124
cli/tree.c
|
|
@ -22,16 +22,17 @@ static struct crs_node *init_window_node(WINDOW *const win) {
|
|||
return node;
|
||||
}
|
||||
|
||||
static struct crs_node *auto_window_node(const struct crs_nodedims *dims) {
|
||||
static struct crs_node *
|
||||
auto_window_node(const struct crs_nodedims *const dims) {
|
||||
WINDOW *win = newwin(dims->height, dims->width, dims->y, dims->x);
|
||||
return init_window_node(win);
|
||||
}
|
||||
|
||||
static struct crs_node *init_abstract_node(struct crs_node *node0,
|
||||
struct crs_node *node1,
|
||||
static struct crs_node *init_abstract_node(struct crs_node *const node0,
|
||||
struct crs_node *const node1,
|
||||
const enum crs_axis axis,
|
||||
const float ratio,
|
||||
struct crs_nodedims *dims) {
|
||||
struct crs_nodedims *const dims) {
|
||||
struct crs_node *node = __alloc_node(NODE_ABSTRACT);
|
||||
node->axis = axis;
|
||||
node->ratio = ratio;
|
||||
|
|
@ -42,7 +43,7 @@ static struct crs_node *init_abstract_node(struct crs_node *node0,
|
|||
return node;
|
||||
}
|
||||
|
||||
static void destroy_node(struct crs_node *node) {
|
||||
static void destroy_node(struct crs_node *const node) {
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
delwin(node->win);
|
||||
|
|
@ -58,7 +59,22 @@ end:
|
|||
free(node);
|
||||
}
|
||||
|
||||
static inline struct crs_nodedims *__dup_dims(const struct crs_nodedims *dims) {
|
||||
static inline struct crs_nodedims *__alloc_dims(int x, int y, int width,
|
||||
int height) {
|
||||
struct crs_nodedims *dims;
|
||||
|
||||
dims = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
|
||||
*dims = (struct crs_nodedims){
|
||||
.x = x,
|
||||
.y = y,
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
return dims;
|
||||
}
|
||||
|
||||
static inline struct crs_nodedims *
|
||||
__dup_dims(const struct crs_nodedims *const dims) {
|
||||
struct crs_nodedims *dup;
|
||||
dup = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
|
||||
memcpy(dup, dims, sizeof(struct crs_nodedims));
|
||||
|
|
@ -66,18 +82,33 @@ static inline struct crs_nodedims *__dup_dims(const struct crs_nodedims *dims) {
|
|||
return dup;
|
||||
}
|
||||
|
||||
static struct crs_nodedims *get_dims(const struct crs_node *node) {
|
||||
static inline struct crs_nodedims *termdims(void) {
|
||||
struct crs_nodedims *dims = __alloc_dims(0, 0, 0, 0);
|
||||
termsize(dims->width, dims->height);
|
||||
|
||||
return dims;
|
||||
}
|
||||
|
||||
static inline void nodesize(const struct crs_node *const node, int *const width,
|
||||
int *const height) {
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
getmaxyx(node->win, *height, *width);
|
||||
} else {
|
||||
/* Abstract Node */
|
||||
assert(node->type == NODE_ABSTRACT);
|
||||
*width = node->dims->width;
|
||||
*height = node->dims->height;
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct crs_nodedims *nodedims(const struct crs_node *const node) {
|
||||
struct crs_nodedims *dims;
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
dims = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
|
||||
// GET_WNODEDIMS();
|
||||
getbegyx(node->win, dims->y, dims->x);
|
||||
getmaxyx(node->win, dims->height, dims->width);
|
||||
printf("getx: %d\n", dims->x);
|
||||
printf("gety: %d\n", dims->y);
|
||||
printf("getwidth: %d\n", dims->width);
|
||||
printf("getheight: %d\n", dims->height);
|
||||
} else {
|
||||
/* Abstract Node */
|
||||
assert(node->type == NODE_ABSTRACT);
|
||||
|
|
@ -87,30 +118,20 @@ static struct crs_nodedims *get_dims(const struct crs_node *node) {
|
|||
return dims;
|
||||
}
|
||||
|
||||
struct crs_nodedims term_dims(void) {
|
||||
struct crs_nodedims dims;
|
||||
getbegyx(stdscr, dims.y, dims.x);
|
||||
getmaxyx(stdscr, dims.height, dims.width);
|
||||
return dims;
|
||||
}
|
||||
|
||||
/* Calculate the dimensions for nodes resulting from a bifurcation.
|
||||
* Returns 0 on success, and 1 on failure if any width/height are 0 characters.
|
||||
* WARNING: This function does not guarantee the x,y positions returned
|
||||
* WARNING: are valid screen coordinates.
|
||||
*/
|
||||
static int bifurcate_dims(const struct crs_node *parent,
|
||||
static int bifurcate_dims(const struct crs_nodedims *const parent_dims,
|
||||
const enum crs_axis axis, const float ratio,
|
||||
struct crs_nodedims **dims0,
|
||||
struct crs_nodedims **dims1) {
|
||||
struct crs_nodedims **const dims0,
|
||||
struct crs_nodedims **const dims1) {
|
||||
assert(0 < ratio && ratio < 1);
|
||||
struct crs_nodedims *_dims0, *_dims1;
|
||||
|
||||
_dims0 = get_dims(parent);
|
||||
_dims1 = __dup_dims(_dims0);
|
||||
|
||||
printf("widthP: %d\n", _dims0->width);
|
||||
printf("heightP: %d\n", _dims0->height);
|
||||
_dims0 = __dup_dims(parent_dims);
|
||||
_dims1 = __dup_dims(parent_dims);
|
||||
|
||||
if (axis == AXIS_X) {
|
||||
_dims0->width *= ratio;
|
||||
|
|
@ -122,13 +143,8 @@ static int bifurcate_dims(const struct crs_node *parent,
|
|||
_dims1->y += _dims0->height;
|
||||
}
|
||||
|
||||
if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height) {
|
||||
printf("width0: %d\n", _dims0->width);
|
||||
printf("height0: %d\n", _dims0->height);
|
||||
printf("width1: %d\n", _dims1->width);
|
||||
printf("height1: %d\n", _dims1->height);
|
||||
if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// propagate bifurcated dimensions
|
||||
*dims0 = _dims0;
|
||||
|
|
@ -136,11 +152,13 @@ static int bifurcate_dims(const struct crs_node *parent,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void resize_node(struct crs_node *node, struct crs_nodedims *new_dims) {
|
||||
static void resize_node(struct crs_node *const node,
|
||||
struct crs_nodedims *const new_dims) {
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
resizemv_window(new_dims->x, new_dims->y, new_dims->width, new_dims->height,
|
||||
node->win);
|
||||
wrefresh(node->win);
|
||||
free(new_dims);
|
||||
} else {
|
||||
/* Abstract Node */
|
||||
|
|
@ -149,7 +167,7 @@ static void resize_node(struct crs_node *node, struct crs_nodedims *new_dims) {
|
|||
|
||||
free(node->dims);
|
||||
node->dims = new_dims;
|
||||
bifurcate_dims(node, node->axis, node->ratio, &dims0, &dims1);
|
||||
bifurcate_dims(new_dims, node->axis, node->ratio, &dims0, &dims1);
|
||||
|
||||
resize_node(node->child[0], dims0);
|
||||
resize_node(node->child[1], dims1);
|
||||
|
|
@ -162,17 +180,21 @@ static void resize_node(struct crs_node *node, struct crs_nodedims *new_dims) {
|
|||
* axis - controls which direction the subdivision occurs
|
||||
* invert_axis - invert index of the original node in the new abstract node
|
||||
*/
|
||||
void bifurcate_window_node(struct crs_node **node, const enum crs_axis axis,
|
||||
const int invert_axis, const float ratio) {
|
||||
void bifurcate_window_node(struct crs_node **const node,
|
||||
const enum crs_axis axis, const int invert_axis,
|
||||
const float ratio) {
|
||||
assert((*node)->type == NODE_WINDOW);
|
||||
struct crs_nodedims *dims0, *dims1;
|
||||
struct crs_node *node0, *node1;
|
||||
|
||||
if (bifurcate_dims(*node, axis, ratio, &dims0, &dims1)) {
|
||||
printf("FAILED TERRIBLY THE FUCK\n");
|
||||
struct crs_nodedims *original_dims = nodedims(*node);
|
||||
if (bifurcate_dims(original_dims, axis, ratio, &dims0, &dims1)) {
|
||||
/* TODO: handle this error properly */
|
||||
free(original_dims);
|
||||
exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (invert_axis) {
|
||||
/* Inverted Bifurcation */
|
||||
node0 = auto_window_node(dims0);
|
||||
|
|
@ -185,13 +207,13 @@ void bifurcate_window_node(struct crs_node **node, const enum crs_axis axis,
|
|||
resize_node(node0, dims0);
|
||||
}
|
||||
|
||||
*node = init_abstract_node(node0, node1, axis, ratio, (*node)->dims);
|
||||
*node = init_abstract_node(node0, node1, axis, ratio, original_dims);
|
||||
}
|
||||
|
||||
/* Collapse an abstract node, killing one child node and resizing
|
||||
* the other to take its place.
|
||||
*/
|
||||
static void collapse_abstract_node(struct crs_node **node,
|
||||
static void collapse_abstract_node(struct crs_node **const node,
|
||||
const int collapse_i) {
|
||||
assert((*node)->type == NODE_ABSTRACT);
|
||||
assert(0 <= collapse_i && collapse_i < NODE_CHILD_N);
|
||||
|
|
@ -213,7 +235,7 @@ static struct crs_node *init_root_node(void) {
|
|||
return init_window_node(rootwin);
|
||||
}
|
||||
|
||||
int init_cursetree(struct crs_tree **const tree) {
|
||||
int ct_init(struct crs_tree **const tree) {
|
||||
*tree = (struct crs_tree *)malloc(sizeof(struct crs_tree));
|
||||
|
||||
/* Initialise NCurses Library & Root Node */
|
||||
|
|
@ -224,9 +246,7 @@ int init_cursetree(struct crs_tree **const tree) {
|
|||
}
|
||||
|
||||
void destroy_tree(struct crs_tree *const tree) {
|
||||
/* WARNING: is it ok to delwin(stdscr) ?? */
|
||||
destroy_node(tree->root);
|
||||
|
||||
endwin();
|
||||
free(tree);
|
||||
}
|
||||
|
|
@ -234,3 +254,19 @@ void destroy_tree(struct crs_tree *const tree) {
|
|||
void resize_tree(struct crs_tree *const tree, struct crs_nodedims *const dims) {
|
||||
resize_node(tree->root, dims);
|
||||
}
|
||||
|
||||
void ct_update(struct crs_tree *const tree) {
|
||||
struct crs_nodedims *term_dims;
|
||||
|
||||
int term_width, term_height, win_width, win_height;
|
||||
termsize(term_width, term_height);
|
||||
nodesize(tree->root, &win_width, &win_height);
|
||||
/* Check if window size has changed, I assume ncurses already binds
|
||||
* a SIGWINCH handler so we can do this instead (TODO: sighandler?) */
|
||||
if (win_width != term_width || win_height != term_height) {
|
||||
term_dims = __alloc_dims(0, 0, term_width, term_height);
|
||||
resize_tree(tree, term_dims);
|
||||
/* TODO: why the fuck does this line make everything work?? */
|
||||
wrefresh(tree->root->child[0]->win);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,11 +51,13 @@ struct crs_tree {
|
|||
};
|
||||
|
||||
/* === External Interface === */
|
||||
int init_cursetree(struct crs_tree **const tree);
|
||||
int ct_init(struct crs_tree **const tree);
|
||||
void destroy_tree(struct crs_tree *const tree);
|
||||
void resize_tree(struct crs_tree *const tree, struct crs_nodedims *const dims);
|
||||
void ct_update(struct crs_tree *const tree);
|
||||
|
||||
void bifurcate_window_node(struct crs_node **node, const enum crs_axis axis,
|
||||
const int invert_axis, const float ratio);
|
||||
void bifurcate_window_node(struct crs_node **const node,
|
||||
const enum crs_axis axis, const int invert_axis,
|
||||
const float ratio);
|
||||
|
||||
#endif /* DORNE_TREE_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue