From 2ba6401e39bcfaaccaa45e6b7ef780a7a15b0c48 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 18 Jan 2023 14:07:38 +0200 Subject: [PATCH] curses --- src/ftp.c | 10 ++++-- src/main.c | 18 +++------- src/tgfx.h | 16 +++++++++ src/tui.c | 4 ++- src/tui_list.c | 4 +-- src/tuipriv.h | 2 +- src/unix/input.c | 5 ++- src/unix/tgfx.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 141 insertions(+), 21 deletions(-) diff --git a/src/ftp.c b/src/ftp.c index 46fad28..262140d 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -8,6 +9,11 @@ #include "ftp.h" #include "util.h" +#ifdef __unix__ +#include +#define closesocket(s) close(s) +#endif + struct ftp *ftp_alloc(void) { struct ftp *ftp; @@ -44,8 +50,9 @@ int ftp_connect(struct ftp *ftp, const char *hostname, int port) return -1; } + memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(host->h_addr); + addr.sin_addr = *((struct in_addr*)host->h_addr); addr.sin_port = htons(port); if(connect(ftp->ctl, (struct sockaddr*)&addr, sizeof addr) == -1) { @@ -54,7 +61,6 @@ int ftp_connect(struct ftp *ftp, const char *hostname, int port) ftp->ctl = -1; return -1; } - return 0; } diff --git a/src/main.c b/src/main.c index 721cd9a..d6ec65b 100644 --- a/src/main.c +++ b/src/main.c @@ -20,6 +20,8 @@ int main(void) init_input(); + tg_init(); + tg_bgchar(' '); tg_clear(); @@ -29,16 +31,7 @@ int main(void) tui_add_list_item(uilist, "another item"); tui_add_list_item(uilist, "foo"); - /* - tg_bgcolor(1); - tg_rect("Remote", 0, 0, 40, 23, TGFX_FRAME); - tg_rect("Local", 40, 0, 40, 23, TGFX_FRAME); - - tg_bgcolor(0); - tg_fgcolor(7); - tg_text(0, 23, ">"); - tg_setcursor(2, 23); - */ + tg_setcursor(0, 24); tui_draw(uilist); @@ -54,10 +47,7 @@ int main(void) } done: - tg_bgchar(' '); - tg_bgcolor(0); - tg_fgcolor(7); - tg_clear(); + tg_cleanup(); cleanup_input(); diff --git a/src/tgfx.h b/src/tgfx.h index 49f93b0..e26b7a0 100644 --- a/src/tgfx.h +++ b/src/tgfx.h @@ -4,10 +4,26 @@ #include enum { + TGFX_BLACK, + TGFX_BLUE, + TGFX_GREEN, + TGFX_CYAN, + TGFX_RED, + TGFX_MAGENTA, + TGFX_YELLOW, + TGFX_WHITE +}; + +enum { TGFX_FRAME = 1, TGFX_SHADOW = 2 }; +void tg_init(void); +void tg_cleanup(void); + +void tg_redraw(void); + void tg_clear(void); void tg_fgcolor(int col); diff --git a/src/tui.c b/src/tui.c index 99bdf2a..106b28f 100644 --- a/src/tui.c +++ b/src/tui.c @@ -54,7 +54,7 @@ void tui_remove_widget(struct tui_widget *par, struct tui_widget *w) struct tui_widget *iter, dummy; if(w->par != par) { - fprintf(stderr, "failed to remove widget %p from %p\n", w, par); + fprintf(stderr, "failed to remove widget %p from %p\n", (void*)w, (void*)par); return; } @@ -91,6 +91,8 @@ void tui_draw(struct tui_widget *w) tui_draw(iter); iter = iter->next; } + + tg_redraw(); } void tui_set_callback(struct tui_widget *w, int type, tui_callback func, void *cls) diff --git a/src/tui_list.c b/src/tui_list.c index 9d491df..4111d84 100644 --- a/src/tui_list.c +++ b/src/tui_list.c @@ -58,9 +58,9 @@ static void draw_list(struct tui_widget *w, void *cls) int i, x, y, num; struct tui_list *wl = (struct tui_list*)w; - tui_wtoscr(w, 0, 0, &x, &y); + tui_wtoscr(w, 0, 0, &x, &y); - tg_bgcolor(1); + tg_bgcolor(TGFX_BLUE); tg_rect(wl->title, x, y, wl->width, wl->height, TGFX_FRAME); num = darr_size(wl->entries); diff --git a/src/tuipriv.h b/src/tuipriv.h index db444cf..27a18b5 100644 --- a/src/tuipriv.h +++ b/src/tuipriv.h @@ -29,6 +29,6 @@ struct tui_list { WCOMMON; char **entries; /* darr */ }; - + #endif /* TUIPRIV_H_ */ diff --git a/src/unix/input.c b/src/unix/input.c index e1d689d..50eb23e 100644 --- a/src/unix/input.c +++ b/src/unix/input.c @@ -1,3 +1,4 @@ +#include #include "input.h" int init_input(void) @@ -11,5 +12,7 @@ void cleanup_input(void) int wait_input(union event *ev) { - return 0; + ev->type = EV_KEY; + ev->key.key = getch(); + return 1; } diff --git a/src/unix/tgfx.c b/src/unix/tgfx.c index 4c8955d..f88fd15 100644 --- a/src/unix/tgfx.c +++ b/src/unix/tgfx.c @@ -1,6 +1,38 @@ #include #include "tgfx.h" +static int fgcol, bgcol; +static int bgchar; +static int cur_x, cur_y; + +static int curses_color(int col); + +void tg_init(void) +{ + initscr(); + raw(); + keypad(stdscr, TRUE); + noecho(); + start_color(); + + fgcol = curses_color(TGFX_WHITE); + bgcol = curses_color(TGFX_BLACK); + bgchar = ' '; + + tg_color(fgcol | (bgcol << 4)); +} + +void tg_cleanup(void) +{ + endwin(); +} + +void tg_redraw(void) +{ + move(cur_y, cur_x); + refresh(); +} + void tg_clear(void) { } @@ -8,31 +40,102 @@ void tg_clear(void) void tg_fgcolor(int col) { + fgcol = curses_color(col); + init_pair(1, fgcol, bgcol); } void tg_bgcolor(int col) { + bgcol = curses_color(col); + init_pair(1, fgcol, bgcol); } void tg_color(int col) { + fgcol = curses_color(col & 0xf); + bgcol = curses_color((col >> 4) & 0xf); + init_pair(1, fgcol, bgcol); } void tg_bgchar(int c) { + bgchar = c; } void tg_setcursor(int x, int y) { + move(y, x); + cur_x = x; + cur_y = y; } void tg_text(int x, int y, const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + tg_vtext(x, y, fmt, ap); + va_end(ap); +} + +void tg_vtext(int x, int y, const char *fmt, va_list ap) +{ + attron(COLOR_PAIR(1)); + move(y, x); + vw_printw(stdscr, fmt, ap); + attroff(COLOR_PAIR(1)); } void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags) { + int i; + + attron(COLOR_PAIR(1)); + + for(i=0; i