diff --git a/cli/curse.c b/cli/curse.c index 3c465fe..0549c9a 100644 --- a/cli/curse.c +++ b/cli/curse.c @@ -43,6 +43,7 @@ void init_ncurses(void) { /* NCurses Init */ initscr(); + /* WARNING: no you shouldn't delwin(stdscr) it breaks everything... */ __conf_window(stdscr); start_color(); @@ -81,34 +82,3 @@ int resizemv_window(const int x, const int y, const int width, const int height, WINDOW *const win) { return wresize(win, height, width) || mvwin(win, y, x); } - -/* -int main(const int argc, const char *const argv[]) { - init_ncurses(); - WINDOW *stdscr = new_window(0, 0, 0, 0); - - // Set background - wbkgd(stdscr, COLOR_PAIR(1)); - - attron(COLOR_PAIR(1)); - attron(A_BOLD); - int counter = 0; - while (1) { - mvprintw(0, 0, "COUNTER %d", counter++); - refresh(); - - switch (getch()) { - case 'q': - goto end; - default: - break; - } - - usleep(BASE_DELAY); - } - -end: - endwin(); - return 0; -} -*/ diff --git a/cli/main.c b/cli/main.c index 6c79397..32811e3 100644 --- a/cli/main.c +++ b/cli/main.c @@ -4,20 +4,8 @@ #include -// #include "child.h" -#include "curse.h" #include "tree.h" -// struct d_window { -// int ptmx; // fd -// }; - -// struct d_window new_window() { -// struct d_window w = { -// .ptmx = posix_openpt(O_RDWR | O_NOCTTY), -// }; -// } - int main(int argc, char **argv) { // struct d_child child; // spawnchild(&child); @@ -34,10 +22,12 @@ int main(int argc, char **argv) { wrefresh(tree->root->child[0]->win); wrefresh(tree->root->child[1]->win); + // wbkgd(tree->root->win, COLOR_PAIR(1)); + // wrefresh(tree->root->win); + while (1) { ct_update(tree); - // refresh(); - // usleep(100000); + usleep(100000); } destroy_tree(tree); diff --git a/cli/tree.c b/cli/tree.c index 177338f..aadc024 100644 --- a/cli/tree.c +++ b/cli/tree.c @@ -3,6 +3,7 @@ #include #include "curse.h" +#include "ncurses.h" #include "tree.h" /* Internal allocator method for crs_node structures. @@ -152,14 +153,19 @@ static int bifurcate_dims(const struct crs_nodedims *const parent_dims, return 0; } +/* NOTE: resize_node calls wnoutrefresh(3x), which expects + * NOTE: a call doupdate(3x) call afterwards to flush ncurses + * NOTE: virtual screen to the physical screen. + */ 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); + + wnoutrefresh(node->win); } else { /* Abstract Node */ assert(node->type == NODE_ABSTRACT); @@ -256,17 +262,26 @@ void resize_tree(struct crs_tree *const tree, struct crs_nodedims *const dims) { } void ct_update(struct crs_tree *const tree) { - struct crs_nodedims *term_dims; + struct crs_nodedims * term_dims; + int term_width, term_height; + const int key = wgetch(curscr); - 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) { + /* ncurses binds a SIGWINCH handler if SIGWINCH has SIG_DFL disposition + * when initscr(3x) is called. This handler emits KEY_RESIZE (decimal 410) to stdin. + * REF: manpages -> resizeterm(3x) initscr(3x) wgetch(3x) + */ + switch (key) { + case -1: + return; + case KEY_RESIZE: + termsize(term_width, term_height); term_dims = __alloc_dims(0, 0, term_width, term_height); resize_tree(tree, term_dims); + // flush ncurses virtual screen -> physical screen + doupdate(); /* TODO: why the fuck does this line make everything work?? */ - wrefresh(tree->root->child[0]->win); + // printf("hola\n"); + default: + break; } }