From 0adbc2a776c36b736bb5acb7184ba559d9428bc4 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 26 Jul 2019 22:13:33 +0100 Subject: towards dynamic screen management --- gramscii.c | 67 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'gramscii.c') diff --git a/gramscii.c b/gramscii.c index 3bed08b..8a89fc9 100644 --- a/gramscii.c +++ b/gramscii.c @@ -33,6 +33,13 @@ #include "arg.h" +typedef struct{ + int sz; + int lst; + char *s; +} line_t; + + #define MOVE 0x00 #define BOX 0x01 #define ARROW 0x02 @@ -71,9 +78,10 @@ #define MIN(x,y) (x) < (y) ? (x) : (y) #define MAX(x,y) (x) > (y) ? (x) : (y) -/** #define DEBUG 1 **/ +#define DEBUG 1 -char **screen; +line_t *screen; +int num_lines; int WIDTH, HEIGHT; int state; @@ -113,7 +121,7 @@ struct termios t1, t2, t3; void dump_lines(){ int i; for (i=0; i Date: Fri, 26 Jul 2019 22:55:56 +0100 Subject: change screen management -- dynamic array of lines --- gramscii.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'gramscii.c') diff --git a/gramscii.c b/gramscii.c index 8a89fc9..4342da9 100644 --- a/gramscii.c +++ b/gramscii.c @@ -136,6 +136,11 @@ void cleanup(int s){ exit(0); } +void exit_cleanup(void){ + cleanup(0); +} + + /*** Status bar ***/ char* state_str(){ @@ -201,6 +206,7 @@ char get_key(FILE *fc, char *msg){ printf("%*s", WIDTH, ""); printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%s", msg); + fflush(stdout); printf("\033[0m"); fflush(stdout); return fgetc(fc); @@ -242,14 +248,33 @@ void show_cursor(){ void set_xy(int _x, int _y, char c){ - /* FIXME: check if x and y are valid!!!! */ + line_t *tmp; + if (_y >= num_lines){ + tmp = realloc(screen, (_y + LONG_STEP)* sizeof(line_t)); + if (tmp == NULL){ + fprintf(stderr, "Unable to allocate memory for more lines"); + exit(1); + } + else while ( num_lines < _y + LONG_STEP){ + screen[num_lines].sz = WIDTH+1; + screen[num_lines].s = malloc((screen[num_lines].sz) * sizeof(char)); + if (screen[num_lines].s == NULL){ + perror("allocating screen[num_lines].s"); + exit(1); + } + memset(screen[num_lines].s, BG, screen[num_lines].sz); + screen[num_lines].lst = 0; + screen[num_lines].s[screen[num_lines].lst+1]='\0'; + num_lines ++; + } + } if (screen[_y].sz < _x + 2){ screen[_y].sz = (_x +2) * 2; screen[_y].s = realloc(screen[_y].s, screen[_y].sz * sizeof(char)); } while (screen[_y].lst<_x){ + screen[_y].lst ++; screen[_y].s[screen[_y].lst] = BG; - screen[_y].lst += 1; } screen[_y].s[_x] = c; if (_x == screen[_y].lst) @@ -806,7 +831,7 @@ void check_modified(FILE *fc){ if (!is_yes(get_key(fc, "Unsaved changes. Write to file [y/n]?")) ){ return; } - write_file(0); + write_file(fc); } } @@ -905,7 +930,7 @@ void init_screen(){ exit(1); } memset(screen[i].s, BG, screen[i].sz); - screen[i].lst = WIDTH; + screen[i].lst = 0; screen[i].s[screen[i].lst+1]='\0'; } reset_styles(); @@ -917,6 +942,7 @@ void init(){ signal(SIGINT, cleanup); signal(SIGTERM, cleanup); signal(SIGQUIT, cleanup); + atexit(exit_cleanup); tcgetattr(0, &t1); t2 = t1; @@ -981,7 +1007,6 @@ void commands(FILE *fc){ case 'q': check_modified(fc);/** FALLTHROUGH **/ case 'Q': - cleanup(0); exit(0); break; } @@ -1028,6 +1053,5 @@ int main(int argc, char *argv[]){ argc--; } commands(stdin); - cleanup(0); return 0; } -- cgit v1.2.3