diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/ui.c | 107 | ||||
-rw-r--r-- | src/ui.h | 21 | ||||
-rw-r--r-- | src/ui_nask.c | 55 | ||||
-rw-r--r-- | src/ui_nask.h | 10 |
5 files changed, 101 insertions, 94 deletions
@@ -1,4 +1,4 @@ -<img src=https://travis-ci.org/lnslbrty/naskpass.svg?branch=jessie> +<img src=https://travis-ci.org/lnslbrty/naskpass.svg?branch=master> <img src=https://scan.coverity.com/projects/9389/badge.svg?flat=1> naskpass @@ -33,7 +33,7 @@ static unsigned int max_x, max_y; static unsigned int cur_x, cur_y; -static WINDOW *wnd_main; +static WINDOW *wnd_main = NULL; static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, /* current active input */ *active = NULL; static pthread_t thrd; @@ -150,7 +150,7 @@ deactivate_ui_input(void *data) return ret; } -static bool +bool process_key(char key) { bool ret = false; @@ -161,7 +161,7 @@ process_key(char key) return ret; } -static int +int do_ui_update(bool timed_out) { int retval = UICB_OK; @@ -192,8 +192,8 @@ do_ui_update(bool timed_out) mvprintw(0, max_x - STRLEN(APP_TIMEOUT_FMT), "[" APP_TIMEOUT_FMT "]", atmout); attroff(COLOR_PAIR(1)); /* EoT (End of Todo) */ - wmove(wnd_main, cur_y, cur_x); - wrefresh(wnd_main); + move(cur_y, cur_x); + refresh(); return (retval); } @@ -257,45 +257,12 @@ ui_thrd_resume(void) pthread_mutex_unlock(&mtx_update); } -WINDOW * -init_ui(void) -{ - wnd_main = initscr(); - max_x = getmaxx(wnd_main); - max_y = getmaxy(wnd_main); - cur_x = getcurx(wnd_main); - cur_y = getcury(wnd_main); - start_color(); - 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(); - nodelay(stdscr, TRUE); - cbreak(); - return (wnd_main); -} - -void -free_ui(void) -{ - delwin(wnd_main); - endwin(); - clear(); - printf(" \033[2J\n"); -} - -static int +int run_ui_thrd(void) { return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); } -static int +int stop_ui_thrd(void) { return (pthread_join(thrd, NULL)); @@ -308,7 +275,7 @@ int ui_wgetchtest(int timeout, char testchar) pthread_mutex_lock(&mtx_update); if ( ui_ipc_getvalue(SEM_UI) <= 0 ) { retval = DOUI_KEY; - } else if ( wgetch(wnd_main) == testchar ) { + } else if ( wgetch(stdscr) == testchar ) { retval = DOUI_KEY; } pthread_mutex_unlock(&mtx_update); @@ -324,46 +291,36 @@ char ui_wgetch(int timeout) if ( ui_ipc_getvalue(SEM_UI) <= 0 ) { key = ERR; } else { - key = wgetch(wnd_main); + key = wgetch(stdscr); } pthread_mutex_unlock(&mtx_update); usleep(timeout/2); return key; } -int -do_ui(void) +WINDOW * +init_ui(void) { - char key = '\0'; - int ret = DOUI_ERR; - - /* init TUI and UI Elements (input field, status bar, etc) */ - init_ui(); - init_ui_elements(wnd_main, max_x, max_y); - - pthread_mutex_lock(&mtx_update); - if (run_ui_thrd() != 0) { - pthread_mutex_unlock(&mtx_update); - return ret; - } - timeout(0); - pthread_mutex_unlock(&mtx_update); - - while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - if ( (key = ui_wgetch(10000)) == ERR ) - continue; - - if ( process_key(key) != true ) { - ui_ipc_semtrywait(SEM_UI); - } - - pthread_mutex_lock(&mtx_update); - do_ui_update(false); - pthread_mutex_unlock(&mtx_update); - } - stop_ui_thrd(); - free_ui_elements(); - - return DOUI_OK; + wnd_main = initscr(); + max_x = getmaxx(wnd_main); + max_y = getmaxy(wnd_main); + cur_x = getcurx(wnd_main); + cur_y = getcury(wnd_main); + start_color(); + raw(); + keypad(stdscr, TRUE); + noecho(); + nodelay(stdscr, TRUE); + cbreak(); + set_escdelay(25); + return wnd_main; } +void +free_ui(void) +{ + delwin(wnd_main); + endwin(); + clear(); + printf(" \033[2J\n"); +} @@ -70,6 +70,12 @@ activate_ui_input(void *data); int deactivate_ui_input(void *data); +bool +process_key(char key); + +int +do_ui_update(bool timed_out); + void ui_thrd_force_update(bool force_all, bool timedout); @@ -79,17 +85,20 @@ ui_thrd_suspend(void); void ui_thrd_resume(void); -WINDOW * -init_ui(void); +int +run_ui_thrd(void); -void -free_ui(void); +int +stop_ui_thrd(void); char ui_wgetch(int timeout); int ui_wgetchtest(int timeout, char testchar); -int -do_ui(void); +WINDOW * +init_ui(void); + +void +free_ui(void); #endif diff --git a/src/ui_nask.c b/src/ui_nask.c index 5eaf49f..896c834 100644 --- a/src/ui_nask.c +++ b/src/ui_nask.c @@ -21,6 +21,7 @@ #define INFOWND_HEIGHT 1 #define BSTR_LEN 3 + static struct input *pw_input; static struct anic *heartbeat; static struct statusbar *higher, *lower; @@ -58,7 +59,7 @@ infownd_update(WINDOW *win, struct txtwindow *tw, bool ui_timeout) return DOUI_OK; } -void +static void show_info_wnd(struct txtwindow *wnd, char *_title, char *text, chtype fore, chtype back, bool activate, bool blink) { ui_thrd_suspend(); @@ -125,8 +126,8 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) return DOUI_OK; } -void -init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) +static void +init_ui_elements(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, @@ -150,7 +151,7 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) set_statusbar_text(higher, title); } -void +static void free_ui_elements(void) { unregister_ui_elt(lower); @@ -168,3 +169,49 @@ free_ui_elements(void) title = NULL; } } + +int +do_ui(void) +{ + char key = '\0'; + int ret = DOUI_ERR; + + /* init TUI and UI Elements (input field, status bar, etc) */ + if (init_ui()) + init_ui_elements(ui_get_maxx(), ui_get_maxy()); + else + return DOUI_ERR; + + /* some color definitions */ + 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); + + ui_thrd_suspend(); + if (run_ui_thrd() != 0) { + ui_thrd_resume(); + return ret; + } + timeout(0); + ui_thrd_resume(); + + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + if ( (key = ui_wgetch(10000)) == ERR ) + continue; + + if ( process_key(key) != true ) { + ui_ipc_semtrywait(SEM_UI); + } + + ui_thrd_suspend(); + do_ui_update(false); + ui_thrd_resume(); + } + stop_ui_thrd(); + free_ui_elements(); + + return DOUI_OK; +} diff --git a/src/ui_nask.h b/src/ui_nask.h index 3d99987..ec8fbaf 100644 --- a/src/ui_nask.h +++ b/src/ui_nask.h @@ -5,13 +5,7 @@ #include "ui_txtwindow.h" -void -show_info_wnd(struct txtwindow *wnd, char *title, char *text, chtype fore, chtype back, bool activate, bool blink); - -void -init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y); - -void -free_ui_elements(void); +int +do_ui(void); #endif |