From 99dfc48c542ec59c9541faa33c8895bfa471089b Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 20:07:03 +0100 Subject: - ui_nwindow.c renamed to better ui_txtwindow.c - better ui_txtwindow.c --- src/main.c | 1 + src/ui.c | 21 ++++++- src/ui.h | 10 ++- src/ui_elements.c | 26 +++++--- src/ui_ipc.h | 2 +- src/ui_nwindow.c | 157 ----------------------------------------------- src/ui_nwindow.h | 44 -------------- src/ui_txtwindow.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui_txtwindow.h | 53 ++++++++++++++++ 9 files changed, 274 insertions(+), 215 deletions(-) delete mode 100644 src/ui_nwindow.c delete mode 100644 src/ui_nwindow.h create mode 100644 src/ui_txtwindow.c create mode 100644 src/ui_txtwindow.h diff --git a/src/main.c b/src/main.c index 8dbe953..0d0eae3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/src/ui.c b/src/ui.c index 5581bdc..fa65e29 100644 --- a/src/ui.c +++ b/src/ui.c @@ -19,7 +19,7 @@ #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" -#include "ui_nwindow.h" +#include "ui_txtwindow.h" #include "status.h" #include "config.h" @@ -94,6 +94,18 @@ unregister_ui_elt(void *data) } } +unsigned int +ui_get_maxx(void) +{ + return max_x; +} + +unsigned int +ui_get_maxy(void) +{ + return max_y; +} + void ui_set_cur(unsigned int x, unsigned int y) { @@ -101,13 +113,13 @@ ui_set_cur(unsigned int x, unsigned int y) cur_y = y; } -int +unsigned int ui_get_curx(void) { return (cur_x); } -int +unsigned int ui_get_cury(void) { return (cur_y); @@ -235,7 +247,10 @@ init_ui(void) init_pair(1, COLOR_RED, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_BLACK); init_pair(3, COLOR_BLACK, COLOR_WHITE); + /* TXTwindow */ init_pair(4, COLOR_YELLOW, COLOR_RED); + init_pair(5, COLOR_WHITE, COLOR_CYAN); + /* EoF TXTwindow */ raw(); keypad(stdscr, TRUE); noecho(); diff --git a/src/ui.h b/src/ui.h index 68f9fcd..6f83149 100644 --- a/src/ui.h +++ b/src/ui.h @@ -48,13 +48,19 @@ register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); +unsigned int +ui_get_maxx(void); + +unsigned int +ui_get_maxy(void); + void ui_set_cur(unsigned int x, unsigned int y); -int +unsigned int ui_get_curx(void); -int +unsigned int ui_get_cury(void); int diff --git a/src/ui_elements.c b/src/ui_elements.c index b274529..e57c0de 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "ui.h" @@ -7,7 +8,7 @@ #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" -#include "ui_nwindow.h" +#include "ui_txtwindow.h" #include "ui_elements.h" #include "status.h" @@ -17,9 +18,7 @@ #define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) #define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 #define INFOWND_WIDTH 25 -#define INFOWND_HEIGHT 3 -#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) -#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 +#define INFOWND_HEIGHT 1 static struct input *pw_input; static struct anic *heartbeat; @@ -80,9 +79,14 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) mq_passwd_send(a->input); clear_input(wnd, a); ui_ipc_msgrecv(MQ_IF, ipc_buf); + set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5)); set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); + sleep(2); + ui_ipc_msgrecv(MQ_IF, ipc_buf); + set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); + set_txtwindow_text(infownd, ipc_buf); break; case UIKEY_BACKSPACE: del_input(wnd, a); @@ -106,11 +110,17 @@ void init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) { asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); + pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, + (unsigned int)(max_y / 2)-PASSWD_YRELPOS, + PASSWD_WIDTH, "PASSWORD: ", + IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); heartbeat = init_anic_default(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), higher_statusbar_update); - lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); - infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), + higher_statusbar_update); + lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), + lower_statusbar_update); + infownd = init_txtwindow_centered(INFOWND_WIDTH, INFOWND_HEIGHT, + infownd_update); infownd->userptr = calloc(4, sizeof(char)); (*(char*)(infownd->userptr)) = '.'; diff --git a/src/ui_ipc.h b/src/ui_ipc.h index ec36f35..5ecfaa4 100644 --- a/src/ui_ipc.h +++ b/src/ui_ipc.h @@ -8,7 +8,7 @@ enum UI_IPC_SEM { - SEM_RD = 0, /* UI Init done? */ + SEM_RD = 0, /* UI Init done? */ SEM_UI, /* TUI active? */ SEM_IN, /* Textfield has input avail */ SEM_BS, /* Master process busy */ diff --git a/src/ui_nwindow.c b/src/ui_nwindow.c deleted file mode 100644 index aa1812d..0000000 --- a/src/ui_nwindow.c +++ /dev/null @@ -1,157 +0,0 @@ -#include -#include - -#include "ui_nwindow.h" - - -struct txtwindow * -init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update) -{ - struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); - - a->x = x; - a->y = y; - a->width = width; - a->height = height; - a->active = false; - a->title_len = INITIAL_TITLE_LEN; - a->title = calloc(a->title_len+1, sizeof(char)); - a->text = NULL; - a->attrs = attrs; - a->text_attrs = text_attrs; - a->window_func = cb_update; - return (a); -} - -static void -__free_text(struct txtwindow *a) -{ - if (a->text) { - if (a->text[0]) { - free(a->text[0]); - } - free(a->text); - a->text = NULL; - } -} - -void -free_txtwindow(struct txtwindow *a) -{ - __free_text(a); - if (a->title) { - free(a->title); - } - free(a); -} - -static void -print_wnd(struct txtwindow *a) -{ - int i, x = a->x, y = a->y, w = a->width, h = a->height; - char tmp[a->width+1]; - - attron(a->attrs); - /* print window surface */ - memset(tmp, ' ', a->width); - tmp[a->width] = '\0'; - for (i = y-1; i < y+h+2; i++) - mvprintw(i, x, tmp); - /* print window border */ - mvhline(y-2, x, 0, w); - mvhline(y+h+2, x, 0, w); - mvvline(y-1, x-1, 0, h+3); - mvvline(y-1, x+w, 0, h+3); - /* print window border edges */ - mvaddch(y-2, x-1, ACS_ULCORNER); - mvaddch(y+2+h, x-1, ACS_LLCORNER); - mvaddch(y-2, x+w, ACS_URCORNER); - mvaddch(y+2+h, x+w, ACS_LRCORNER); - /* print window title */ - attroff(a->attrs); - attron(a->text_attrs); - mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title); - /* print windows text */ - i = 0; - while ( a->text && a->text[i] ) { - mvprintw(y+i, x, a->text[i]); - i++; - } - attroff(a->text_attrs); -} - -static int -txtwindow_cb(WINDOW *win, void *data, bool timedout) -{ - struct txtwindow *a = (struct txtwindow *) data; - - if (a->active == true) { - print_wnd(a); - } - return (UICB_OK); -} - -void inline -register_txtwindow(struct txtwindow *a) -{ - struct ui_callbacks cbs; - cbs.ui_element = txtwindow_cb; - cbs.ui_input = NULL; - register_ui_elt(&cbs, (void *) a, NULL); -} - -static size_t -__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) -{ - size_t cursiz = sz_dest; - - if (sz_src > sz_dest) { - *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); - cursiz = sz_src; - } - memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); - memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); - return sz_src; -} - -/* seperate a String with NEWLINES into an array */ -static char ** -__do_textadjust(struct txtwindow *a, char *text) -{ - int i = 0, rows = (int)(strlen(text) / a->width)+1; - char **adj_text = calloc(rows+1, sizeof(char *)); - char *p_strtok, *tok; - const char sep[] = "\n"; - - if (rows > a->height) goto error; - p_strtok = strdup(text); - while ( (tok = strsep(&p_strtok, sep)) && rows-- >= 0 ) { - if (strlen(tok) > a->width) { - strcpy(tok+a->width-3, "..."); - *(tok+a->width) = '\0'; - } - adj_text[i] = tok; - i++; - } - return adj_text; -error: - free(adj_text); - return NULL; -} - -void -set_txtwindow_text(struct txtwindow *a, char *text) -{ - char **fmt_text = __do_textadjust(a, text); - - if (fmt_text) { - __free_text(a); - a->text = fmt_text; - } -} - -void -set_txtwindow_title(struct txtwindow *a, const char *title) -{ - a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); -} diff --git a/src/ui_nwindow.h b/src/ui_nwindow.h deleted file mode 100644 index 198481b..0000000 --- a/src/ui_nwindow.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef UI_TXTWINDOW_H -#define UI_TXTWINDOW_H 1 - -#include - -#include "ui.h" - -#define INITIAL_TITLE_LEN 32 - -#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() - -struct txtwindow { - unsigned int y; - unsigned int x; - unsigned int width; - unsigned int height; - bool active; - char *title; - size_t title_len; - char **text; - int (*window_func)(WINDOW *, struct txtwindow *); - chtype attrs; - chtype text_attrs; - void *userptr; -}; - -typedef int (*window_func)(WINDOW *, struct txtwindow *); - -struct txtwindow * -init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update); - -void -free_txtwindow(struct txtwindow *a); - -void -register_txtwindow(struct txtwindow *a); - -void -set_txtwindow_text(struct txtwindow *a, char *text); - -void -set_txtwindow_title(struct txtwindow *a, const char *title); - -#endif diff --git a/src/ui_txtwindow.c b/src/ui_txtwindow.c new file mode 100644 index 0000000..5852c32 --- /dev/null +++ b/src/ui_txtwindow.c @@ -0,0 +1,175 @@ +#include +#include + +#include "ui_txtwindow.h" + + +struct txtwindow * +init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update) +{ + struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); + + a->x = x; + a->y = y; + a->width = width; + a->height = height; + a->active = false; + a->title_len = INITIAL_TITLE_LEN; + a->title = calloc(a->title_len+1, sizeof(char)); + a->text = NULL; + a->attrs = 0; + a->text_attrs = 0; + a->window_func = cb_update; + return (a); +} + +struct txtwindow * +init_txtwindow_centered(unsigned int width, unsigned int height, window_func cb_update) +{ + unsigned int x = (ui_get_maxx()/2)-(width/2); + unsigned int y = (ui_get_maxy()/2)-(height/2); + return init_txtwindow(x, y, width, height, cb_update); +} + +static void +__free_text(struct txtwindow *a) +{ + if (a->text) { + if (a->text[0]) { + free(a->text[0]); + } + free(a->text); + a->text = NULL; + } +} + +void +free_txtwindow(struct txtwindow *a) +{ + __free_text(a); + if (a->title) { + free(a->title); + } + free(a); +} + +static void +print_wnd(struct txtwindow *a) +{ + int i, x = a->x, y = a->y, w = a->width, h = a->height; + char tmp[a->width+1]; + + attron(a->attrs); + /* print window surface */ + memset(tmp, ' ', a->width); + tmp[a->width] = '\0'; + for (i = y-1; i < y+h+1; i++) + mvprintw(i, x, tmp); + /* print window border */ + mvhline(y-2, x, 0, w); + mvhline(y+h+1, x, 0, w); + mvvline(y-1, x-1, 0, h+3); + mvvline(y-1, x+w, 0, h+3); + /* print window border edges */ + mvaddch(y-2, x-1, ACS_ULCORNER); + mvaddch(y+1+h, x-1, ACS_LLCORNER); + mvaddch(y-2, x+w, ACS_URCORNER); + mvaddch(y+1+h, x+w, ACS_LRCORNER); + /* print window title */ + attroff(a->attrs); + attron(a->text_attrs); + mvprintw(y-2, x+(w/2)-((a->title_len+4)/2), "[ %s ]", a->title); + /* print windows text */ + i = -1; + if (a->text) { + while ( a->text[++i] ) { + mvprintw(y+i, x+1, a->text[i]); + } + } + attroff(a->text_attrs); +} + +static int +txtwindow_cb(WINDOW *win, void *data, bool timedout) +{ + struct txtwindow *a = (struct txtwindow *) data; + + if (a->active == true) { + print_wnd(a); + } + return (UICB_OK); +} + +void inline +register_txtwindow(struct txtwindow *a) +{ + struct ui_callbacks cbs; + cbs.ui_element = txtwindow_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); +} + +static size_t +__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) +{ + size_t cursiz = sz_dest; + + if (sz_src > sz_dest) { + *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); + cursiz = sz_src; + } + memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); + memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); + return sz_src; +} + +/* seperate a String with NEWLINES into an array */ +static char ** +__do_textadjust(struct txtwindow *a, char *text) +{ + int i = 0, rows = (int)(strlen(text) / a->width); + char **adj_text = calloc(rows+2, sizeof(char *)); + char *p_strtok, *tok; + const char sep[] = "\n"; + + if (rows > a->height) goto error; + p_strtok = strdup(text); + do { + tok = strsep(&p_strtok, sep); + if (strlen(tok) > a->width) { + strcpy(tok+a->width-3, "..."); + *(tok+a->width) = '\0'; + } + adj_text[i] = tok; + i++; + } while (rows > 0); + return adj_text; +error: + free(adj_text); + return NULL; +} + +void +set_txtwindow_text(struct txtwindow *a, char *text) +{ + char **fmt_text = __do_textadjust(a, text); + + if (fmt_text) { + __free_text(a); + a->text = fmt_text; + } +} + +void +set_txtwindow_title(struct txtwindow *a, const char *title) +{ + a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); +} + +void +set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt) +{ + a->attrs = wnd; + a->text_attrs = txt; +} + diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h new file mode 100644 index 0000000..913cd99 --- /dev/null +++ b/src/ui_txtwindow.h @@ -0,0 +1,53 @@ +#ifndef UI_TXTWINDOW_H +#define UI_TXTWINDOW_H 1 + +#include + +#include "ui.h" + +#define INITIAL_TITLE_LEN 32 + +#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() + +struct txtwindow { + unsigned int y; + unsigned int x; + unsigned int width; + unsigned int height; + bool active; + char *title; + size_t title_len; + char **text; + int (*window_func)(WINDOW *, struct txtwindow *); + chtype attrs; + chtype text_attrs; + void *userptr; +}; + +typedef int (*window_func)(WINDOW *, struct txtwindow *); + +struct txtwindow * +init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update); + +struct txtwindow * +init_txtwindow_centered(unsigned int width, unsigned int height, window_func cb_update); + +void +free_txtwindow(struct txtwindow *a); + +void +register_txtwindow(struct txtwindow *a); + +void +set_txtwindow_text(struct txtwindow *a, char *text); + +void +set_txtwindow_title(struct txtwindow *a, const char *title); + +void +set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt); + +void +set_txtwindow_pos(struct txtwindow *a, unsigned int x, unsigned int y); + +#endif -- cgit v1.2.3