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/ui_txtwindow.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/ui_txtwindow.h (limited to 'src/ui_txtwindow.h') 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 From 5dcfb21b85391ddaace3ec7e2161859661894597 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 22:30:32 +0100 Subject: fixed wgetch key loop and wtimeout bug --- src/ui.c | 30 ++++++++++++++++++------------ src/ui.h | 6 ++++++ src/ui_elements.c | 19 +++++++++++++++++-- src/ui_input.c | 5 +---- src/ui_input.h | 2 +- src/ui_txtwindow.h | 2 +- 6 files changed, 44 insertions(+), 20 deletions(-) (limited to 'src/ui_txtwindow.h') diff --git a/src/ui.c b/src/ui.c index fa65e29..f30d10b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -47,7 +47,6 @@ static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t mtx_input = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; @@ -132,7 +131,6 @@ activate_ui_input(void *data) struct nask_ui *cur = nui; if (cur == NULL || data == NULL) return DOUI_NINIT; - pthread_mutex_lock(&mtx_input); while ( cur->data != NULL ) { if ( cur->data == data ) { if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { @@ -143,7 +141,6 @@ activate_ui_input(void *data) } cur = cur->next; } - pthread_mutex_unlock(&mtx_input); return ret; } @@ -152,12 +149,10 @@ deactivate_ui_input(void *data) { int ret = DOUI_ERR; - pthread_mutex_lock(&mtx_input); if (active != NULL && data == active->data) { active = NULL; ret = DOUI_OK; } - pthread_mutex_unlock(&mtx_input); return ret; } @@ -235,6 +230,20 @@ ui_thrd_force_update(void) pthread_mutex_unlock(&mtx_busy); } +void +ui_thrd_suspend(void) +{ + ui_thrd_force_update(); + pthread_mutex_lock(&mtx_busy); +} + +void +ui_thrd_resume(void) +{ + pthread_mutex_unlock(&mtx_busy); + ui_thrd_force_update(); +} + WINDOW * init_ui(void) { @@ -293,18 +302,15 @@ do_ui(void) return ret; } ui_ipc_semwait(SEM_RD); - wtimeout(wnd_main, 1000); pthread_mutex_unlock(&mtx_update); + wtimeout(stdscr, 1000); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - if ((key = wgetch(wnd_main)) == '\0') { - break; - } - if (key == -1) { + if ((key = wgetch(wnd_main)) == ERR) { continue; } if ( process_key(key) != true ) { - ui_ipc_semtrywait(SEM_UI); - ui_thrd_force_update(); + //ui_ipc_semtrywait(SEM_UI); + //ui_thrd_force_update(); } } stop_ui_thrd(); diff --git a/src/ui.h b/src/ui.h index 6f83149..8aa1d55 100644 --- a/src/ui.h +++ b/src/ui.h @@ -72,6 +72,12 @@ deactivate_ui_input(void *data); void ui_thrd_force_update(void); +void +ui_thrd_suspend(void); + +void +ui_thrd_resume(void); + WINDOW * init_ui(void); diff --git a/src/ui_elements.c b/src/ui_elements.c index e57c0de..427415a 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -73,23 +73,36 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) char ipc_buf[IPC_MQSIZ+1]; memset(ipc_buf, '\0', IPC_MQSIZ+1); + wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: - deactivate_input(pw_input); mq_passwd_send(a->input); + ui_thrd_suspend(); clear_input(wnd, a); + deactivate_input(pw_input); 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); + ui_thrd_resume(); sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_thrd_suspend(); set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); + set_txtwindow_title(infownd, "ERROR"); set_txtwindow_text(infownd, ipc_buf); + ui_thrd_resume(); +// while (getch() != '\n') { } +wgetch(stdscr); + ui_thrd_suspend(); + set_txtwindow_active(infownd, false); + activate_input(pw_input); + ui_thrd_resume(); break; case UIKEY_BACKSPACE: del_input(wnd, a); + ui_thrd_force_update(); break; case UIKEY_ESC: return DOUI_ERR; @@ -102,7 +115,9 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) break; default: add_input(wnd, a, key); + ui_thrd_force_update(); } + wtimeout(stdscr, 1000); return DOUI_OK; } @@ -129,7 +144,7 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) register_statusbar(lower); register_anic_default(heartbeat); register_txtwindow(infownd); - activate_input(wnd_main, pw_input); + activate_input(pw_input); set_statusbar_text(higher, title); } diff --git a/src/ui_input.c b/src/ui_input.c index b32570b..9c53330 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -121,7 +121,7 @@ print_input(WINDOW *win, struct input *a) } int -activate_input(WINDOW *win, struct input *a) +activate_input(struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); curs_set(1); @@ -146,7 +146,6 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } @@ -168,7 +167,6 @@ del_input(WINDOW *win, struct input *a) } mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } @@ -181,7 +179,6 @@ clear_input(WINDOW *win, struct input *a) a->input_pos = 0; a->cur_pos = 0; ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } diff --git a/src/ui_input.h b/src/ui_input.h index 8ed7fc3..df7088c 100644 --- a/src/ui_input.h +++ b/src/ui_input.h @@ -26,7 +26,7 @@ void free_input(struct input *a); int -activate_input(WINDOW *win, struct input *a); +activate_input(struct input *a); int deactivate_input(struct input *a); diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h index 913cd99..0ebbd9f 100644 --- a/src/ui_txtwindow.h +++ b/src/ui_txtwindow.h @@ -7,7 +7,7 @@ #define INITIAL_TITLE_LEN 32 -#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() +#define set_txtwindow_active(wnd, activate) wnd->active = activate; struct txtwindow { unsigned int y; -- cgit v1.2.3 From ecd71a9af7f74d1cd73ef015b767aa63dc0605d9 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 09:53:47 +0100 Subject: - atmout/pthread_cond_timedwait bug (still not fixed) - txtwindow update callback added - better update funcs --- Makefile.debug | 5 ++++- src/main.c | 4 ++-- src/ui.c | 16 ++++++++------ src/ui_elements.c | 65 +++++++++++++++++++++++++++--------------------------- src/ui_statusbar.c | 8 +++---- src/ui_statusbar.h | 4 ++-- src/ui_txtwindow.c | 12 ++++++++++ src/ui_txtwindow.h | 8 ++++--- 8 files changed, 70 insertions(+), 52 deletions(-) (limited to 'src/ui_txtwindow.h') diff --git a/Makefile.debug b/Makefile.debug index 2faf3b2..134457c 100644 --- a/Makefile.debug +++ b/Makefile.debug @@ -45,4 +45,7 @@ clean: rm -f $(BIN) $(MAKE) -C tests clean -.PHONY: all debug release strip install uninstall clean +test: + $(MAKE) -C tests run + +.PHONY: all debug release strip install uninstall clean test diff --git a/src/main.c b/src/main.c index 0d0eae3..34e27c9 100644 --- a/src/main.c +++ b/src/main.c @@ -34,8 +34,8 @@ enum msg_index { MSG_CRYPTCMD_ERR, MSG_NUM }; -static const char *msg_arr[] = { "Please wait, got a piped password ..", - "Please wait, busy ..", +static const char *msg_arr[] = { "Please wait, got a piped password", + "Please wait, busy", "check_fifo: %s is not a FIFO\n", "check_fifo: %s error(%d): %s\n", "fifo: cryptcreate busy", diff --git a/src/ui.c b/src/ui.c index f30d10b..acf04f5 100644 --- a/src/ui.c +++ b/src/ui.c @@ -161,7 +161,6 @@ process_key(char key) { bool ret = false; - atmout = APP_TIMEOUT; if ( active != NULL ) { ret = ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } @@ -176,6 +175,9 @@ do_ui_update(bool timed_out) /* call all draw callback's */ erase(); + if (timed_out == TRUE && atmout > 0) { + atmout--; + } else atmout = APP_TIMEOUT; while (cur != NULL) { if (cur->cbs.ui_element != NULL) { cur->cbs.ui_element(cur->wnd, cur->data, timed_out); @@ -199,18 +201,18 @@ static void * ui_thrd(void *arg) { int cnd_ret; - struct timeval now; + struct timespec now; struct timespec wait; - gettimeofday(&now, NULL); + //gettimeofday(&now, NULL); + clock_gettime(CLOCK_REALTIME, &now); wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; - wait.tv_nsec = now.tv_usec * 1000; + wait.tv_nsec = now.tv_nsec * 1000; do_ui_update(true); ui_ipc_sempost(SEM_RD); pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - if (--atmout <= 0) ui_ipc_semtrywait(SEM_UI); pthread_mutex_lock(&mtx_busy); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); pthread_mutex_unlock(&mtx_busy); @@ -309,9 +311,9 @@ do_ui(void) continue; } if ( process_key(key) != true ) { - //ui_ipc_semtrywait(SEM_UI); - //ui_thrd_force_update(); + ui_ipc_semtrywait(SEM_UI); } + ui_thrd_force_update(); } stop_ui_thrd(); free_ui_elements(); diff --git a/src/ui_elements.c b/src/ui_elements.c index 427415a..9f4994d 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -19,51 +19,44 @@ #define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 #define INFOWND_WIDTH 25 #define INFOWND_HEIGHT 1 +#define BSTR_LEN 3 static struct input *pw_input; static struct anic *heartbeat; static struct statusbar *higher, *lower; static struct txtwindow *infownd; +static struct tctwindow *errwnd; static char *title = NULL; +static char busy_str[BSTR_LEN+1] = ".\0\0\0"; static int -lower_statusbar_update(WINDOW *win, struct statusbar *bar) +lower_statusbar_update(WINDOW *win, struct statusbar *bar, bool ui_timeout) { + if (ui_timeout == FALSE) return DOUI_OK; char *tmp = get_system_stat(); set_statusbar_text(bar, tmp); free(tmp); - return 0; -} - -static int -higher_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - return 0; + return DOUI_OK; } static int -infownd_update(WINDOW *win, struct txtwindow *tw) +higher_statusbar_update(WINDOW *win, struct statusbar *bar, bool ui_timeout) { - char *tmp = (char*)(tw->userptr); - size_t len = strlen(tmp); - - if (tw->active) { - if ( len == 3 ) { - memset(tmp+1, '\0', 2); - } else strcat(tmp, "."); - } else (*tmp) = '.'; - return 0; + return DOUI_OK; } static int -mq_passwd_send(char *passwd) +infownd_update(WINDOW *win, struct txtwindow *tw, bool ui_timeout) { - int ret; - - ui_ipc_sempost(SEM_IN); - ret = ui_ipc_msgsend(MQ_PW, passwd); - return ret; + if (ui_timeout == TRUE && tw->active == TRUE) { + size_t len = strlen(busy_str); + if (len > BSTR_LEN) { + memset(busy_str, '\0', BSTR_LEN+1); + busy_str[0] = '.'; + } else strcat(busy_str, "."); + } + return DOUI_OK; } static int @@ -76,33 +69,44 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: - mq_passwd_send(a->input); + ui_ipc_msgsend(MQ_PW, a->input); + ui_thrd_suspend(); clear_input(wnd, a); deactivate_input(pw_input); + ui_thrd_resume(); + ui_ipc_msgrecv(MQ_IF, ipc_buf); + + ui_thrd_suspend(); 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); ui_thrd_resume(); + sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_thrd_suspend(); set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); set_txtwindow_title(infownd, "ERROR"); set_txtwindow_text(infownd, ipc_buf); ui_thrd_resume(); -// while (getch() != '\n') { } -wgetch(stdscr); + + while (wgetch(stdscr) != '\n') { }; + ui_thrd_suspend(); set_txtwindow_active(infownd, false); activate_input(pw_input); ui_thrd_resume(); + + //ui_thrd_force_update(); + ui_ipc_sempost(SEM_IN); break; case UIKEY_BACKSPACE: del_input(wnd, a); - ui_thrd_force_update(); + //ui_thrd_force_update(); break; case UIKEY_ESC: return DOUI_ERR; @@ -115,7 +119,7 @@ wgetch(stdscr); break; default: add_input(wnd, a, key); - ui_thrd_force_update(); + //ui_thrd_force_update(); } wtimeout(stdscr, 1000); return DOUI_OK; @@ -136,8 +140,6 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) lower_statusbar_update); infownd = init_txtwindow_centered(INFOWND_WIDTH, INFOWND_HEIGHT, infownd_update); - infownd->userptr = calloc(4, sizeof(char)); - (*(char*)(infownd->userptr)) = '.'; register_input(NULL, pw_input, passwd_input_cb); register_statusbar(higher); @@ -159,7 +161,6 @@ free_ui_elements(void) free_anic_default(heartbeat); free_statusbar(higher); free_statusbar(lower); - free(infownd->userptr); free_txtwindow(infownd); free_ui(); if (title) { diff --git a/src/ui_statusbar.c b/src/ui_statusbar.c index 8fcfeb4..df88683 100644 --- a/src/ui_statusbar.c +++ b/src/ui_statusbar.c @@ -36,11 +36,6 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out) size_t len; if (a == NULL) return (UICB_ERR_UNDEF); - if (timed_out == true) { - if (a->status_func != NULL) { - a->status_func(win, a); - } - } attron(a->attrs); len = strnlen(a->text, a->width); if (len < a->width) { @@ -50,6 +45,9 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out) memset(tmp, ' ', a->width); tmp[a->width] = '\0'; strncpy((tmp + diff_pos), a->text, len); + if (a->status_func != NULL) { + a->status_func(win, a, timed_out); + } if (win != NULL) { mvwprintw(win, a->y, 0, tmp); } else { diff --git a/src/ui_statusbar.h b/src/ui_statusbar.h index 5139c14..65e9e12 100644 --- a/src/ui_statusbar.h +++ b/src/ui_statusbar.h @@ -8,11 +8,11 @@ struct statusbar { unsigned int y; unsigned int width; char *text; - int (*status_func)(WINDOW *, struct statusbar *); + int (*status_func)(WINDOW *, struct statusbar *, bool); chtype attrs; }; -typedef int (*status_func)(WINDOW *, struct statusbar *); +typedef int (*status_func)(WINDOW *, struct statusbar *, bool); struct statusbar * init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update); diff --git a/src/ui_txtwindow.c b/src/ui_txtwindow.c index 5852c32..90d152d 100644 --- a/src/ui_txtwindow.c +++ b/src/ui_txtwindow.c @@ -96,6 +96,11 @@ txtwindow_cb(WINDOW *win, void *data, bool timedout) if (a->active == true) { print_wnd(a); + if (a->window_func) { + attron(a->text_attrs); + a->window_func(win, a, timedout); + attroff(a->text_attrs); + } } return (UICB_OK); } @@ -173,3 +178,10 @@ set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt) a->text_attrs = txt; } +void +set_txtwindow_dim(struct txtwindow *a, unsigned int w, unsigned int h) +{ + a->width = w; + a->height = h; +} + diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h index 0ebbd9f..1e1bc3f 100644 --- a/src/ui_txtwindow.h +++ b/src/ui_txtwindow.h @@ -18,13 +18,12 @@ struct txtwindow { char *title; size_t title_len; char **text; - int (*window_func)(WINDOW *, struct txtwindow *); + int (*window_func)(WINDOW *, struct txtwindow *, bool); chtype attrs; chtype text_attrs; - void *userptr; }; -typedef int (*window_func)(WINDOW *, struct txtwindow *); +typedef int (*window_func)(WINDOW *, struct txtwindow *, bool); struct txtwindow * init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update); @@ -50,4 +49,7 @@ set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt); void set_txtwindow_pos(struct txtwindow *a, unsigned int x, unsigned int y); +void +set_txtwindow_dim(struct txtwindow *a, unsigned int w, unsigned int h); + #endif -- cgit v1.2.3