diff options
author | toni <matzeton@googlemail.com> | 2016-07-31 04:12:44 +0200 |
---|---|---|
committer | toni <matzeton@googlemail.com> | 2016-07-31 04:12:44 +0200 |
commit | f47610184e074eb2cdfae9ceaee5f8811007faff (patch) | |
tree | 636b732188f4698c61972d101d2abac385ee8717 | |
parent | c0eb5798acaea17314de1a18cd0f4ce3d82c7e73 (diff) |
exported atmout to nask_ui, fixed weird memory error (busy_str in
infownd_update had a buffer overflow, but valgrind wasn't able to catch
this, cz the overwrite happened in same memory segment (special case),
gdb ftw
-rw-r--r-- | src/ui.c | 42 | ||||
-rw-r--r-- | src/ui.h | 2 | ||||
-rw-r--r-- | src/ui_nask.c | 34 | ||||
-rw-r--r-- | src/ui_txtwindow.c | 17 | ||||
-rw-r--r-- | src/ui_txtwindow.h | 6 |
5 files changed, 66 insertions, 35 deletions
@@ -15,20 +15,6 @@ #include "ui_nask.h" #include "status.h" -#include "config.h" - -#define APP_TIMEOUT 60 -#define APP_TIMEOUT_FMT "%02d" -#define PASSWD_WIDTH 35 -#define PASSWD_HEIGHT 5 -#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 STRLEN(s) (sizeof(s)/sizeof(s[0])) static unsigned int max_x, max_y; @@ -37,8 +23,8 @@ static WINDOW *wnd_main = NULL; static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, /* current active input */ *active = NULL; static uicb_update update_callback = NULL; +static uicb_update postupdate_callback = NULL; static pthread_t thrd; -static int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; @@ -170,16 +156,10 @@ do_ui_update(bool timed_out) /* call all draw callback's */ erase(); - if (!timed_out) { - atmout = APP_TIMEOUT; - } else if (atmout > 0) { - atmout--; - } else if (atmout == 0) { - ui_ipc_semtrywait(SEM_UI); - } if (update_callback) - update_callback(timed_out); + if (update_callback(timed_out) != UICB_OK) + return UICB_ERR_CB; while (cur != NULL) { if (cur->cbs.ui_element != NULL) { @@ -192,11 +172,11 @@ do_ui_update(bool timed_out) } cur = cur->next; } - /* TODO: Maybe export to an extra module? */ - attron(COLOR_PAIR(1)); - mvprintw(0, max_x - STRLEN(APP_TIMEOUT_FMT), "[" APP_TIMEOUT_FMT "]", atmout); - attroff(COLOR_PAIR(1)); - /* EoT (End of Todo) */ + + if (postupdate_callback) + if (postupdate_callback(timed_out) != UICB_OK) + return UICB_ERR_CB; + move(cur_y, cur_x); refresh(); return (retval); @@ -228,7 +208,7 @@ ui_thrd(void *arg) while ( ui_ipc_getvalue(SEM_UI) > 0 ) { cnd_ret = ui_cond_timedwait(&cnd_update, &mtx_update, UILOOP_TIMEOUT); if (cnd_ret == 0) { - do_ui_update(true); + do_ui_update(false); } else if (cnd_ret == ETIMEDOUT) { do_ui_update(true); } @@ -304,10 +284,12 @@ char ui_wgetch(int timeout) } WINDOW * -init_ui(uicb_update on_update_callback) +init_ui(uicb_update on_update_callback, uicb_update on_postupdate_callback) { if (on_update_callback) update_callback = on_update_callback; + if (on_postupdate_callback) + postupdate_callback = on_postupdate_callback; wnd_main = initscr(); max_x = getmaxx(wnd_main); max_y = getmaxy(wnd_main); @@ -94,7 +94,7 @@ char ui_wgetch(int timeout); int ui_wgetchtest(int timeout, char testchar); WINDOW * -init_ui(uicb_update on_update_callback); +init_ui(uicb_update on_update_callback, uicb_update on_postupdate_callback); void free_ui(void); diff --git a/src/ui_nask.c b/src/ui_nask.c index 597e42a..6e017c9 100644 --- a/src/ui_nask.c +++ b/src/ui_nask.c @@ -13,19 +13,26 @@ #include "status.h" +#define APP_TIMEOUT 60 +#define APP_TIMEOUT_FMT "%02d" +#define BSTR_LEN 3 #define PASSWD_WIDTH 35 #define PASSWD_HEIGHT 5 #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 1 -#define BSTR_LEN 3 +#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) +#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 + +#define STRLEN(s) (sizeof(s)/sizeof(s[0])) static struct input *pw_input; static struct anic *heartbeat; static struct statusbar *higher, *lower; static struct txtwindow *infownd; +static int atmout = APP_TIMEOUT; static char *title = NULL; static char busy_str[BSTR_LEN+1] = ".\0\0\0"; @@ -51,10 +58,12 @@ infownd_update(WINDOW *win, struct txtwindow *tw, bool ui_timeout) { if (ui_timeout == TRUE && tw->active == TRUE) { size_t len = strlen(busy_str); - if (len > BSTR_LEN) { + if (len == 0) { + strcat(busy_str, "."); + } else if (len >= BSTR_LEN) { memset(busy_str, '\0', BSTR_LEN+1); - busy_str[0] = '.'; } else strcat(busy_str, "."); + mvprintw(tw->y, tw->x + get_txtwindow_textlen(0, tw) + 1, busy_str); } return DOUI_OK; } @@ -175,6 +184,14 @@ free_ui_elements(void) static int on_update_cb(bool timeout) { + if (!timeout) { + atmout = APP_TIMEOUT; + } else if (atmout > 0) { + atmout--; + } else if (atmout == 0) { + ui_ipc_semtrywait(SEM_UI); + } + if ( ui_ipc_getvalue(SEM_IN) <= 0 ) { attron(COLOR_PAIR(4)); const char msg[] = "Got a piped password .."; @@ -186,6 +203,15 @@ on_update_cb(bool timeout) return UICB_OK; } +static int +on_postupdate_cb(bool timeout) +{ + attron(COLOR_PAIR(1)); + mvprintw(0, (unsigned int)(ui_get_maxx() - STRLEN(APP_TIMEOUT_FMT)), "[" APP_TIMEOUT_FMT "]", atmout); + attroff(COLOR_PAIR(1)); + return UICB_OK; +} + int do_ui(void) { @@ -193,7 +219,7 @@ do_ui(void) int ret = DOUI_ERR; /* init TUI and UI Elements (input field, status bar, etc) */ - if (init_ui(on_update_cb)) + if (init_ui(on_update_cb, on_postupdate_cb)) init_ui_elements(ui_get_maxx(), ui_get_maxy()); else return DOUI_ERR; diff --git a/src/ui_txtwindow.c b/src/ui_txtwindow.c index 0acfb72..5776f02 100644 --- a/src/ui_txtwindow.c +++ b/src/ui_txtwindow.c @@ -170,6 +170,23 @@ set_txtwindow_text(struct txtwindow *a, char *text) } } +size_t +get_txtwindow_rows(struct txtwindow *a) +{ + size_t ret = 0; + + while ( a->text[ret] != NULL ) + ret++; + + return ret++; +} + +size_t +get_txtwindow_textlen(size_t row_index, struct txtwindow *a) +{ + return strlen(a->text[row_index]); +} + void set_txtwindow_title(struct txtwindow *a, const char *title) { diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h index 260a38a..5b91350 100644 --- a/src/ui_txtwindow.h +++ b/src/ui_txtwindow.h @@ -45,6 +45,12 @@ register_txtwindow(struct txtwindow *a); void set_txtwindow_text(struct txtwindow *a, char *text); +size_t +get_txtwindow_rows(struct txtwindow *a); + +size_t +get_txtwindow_textlen(size_t row_index, struct txtwindow *a); + void set_txtwindow_title(struct txtwindow *a, const char *title); |