aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--debian/changelog6
-rw-r--r--src/config.h1
-rw-r--r--src/main.c3
-rw-r--r--src/ui.c67
-rw-r--r--src/ui.h7
-rw-r--r--src/ui_elements.c28
-rw-r--r--src/ui_ipc.c2
-rw-r--r--src/ui_ipc.h3
-rw-r--r--src/ui_txtwindow.h7
9 files changed, 79 insertions, 45 deletions
diff --git a/debian/changelog b/debian/changelog
index c1fb0d1..dd12c47 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+naskpass (1.3-1) unstable; urgency=medium
+
+ * code cleanup
+
+ -- Toni Uhlig <matzeton@googlemail.com> Sun, 24 Jul 2016 00:28:41 +0200
+
naskpass (1.3) unstable; urgency=medium
* fixed coverity issues
diff --git a/src/config.h b/src/config.h
index 797376f..7313669 100644
--- a/src/config.h
+++ b/src/config.h
@@ -8,7 +8,6 @@
#define SEM_GUI "/naskpass-gui"
#define SEM_INP "/naskpass-input"
#define SEM_BSY "/naskpass-busy"
-#define SEM_RDY "/naskpass-initialized"
#define MSQ_PWD "/naskpass-passwd"
#define MSQ_INF "/naskpass-info"
diff --git a/src/main.c b/src/main.c
index d4ba0f9..9d911d7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -191,10 +191,11 @@ main(int argc, char **argv)
}
ret = EXIT_SUCCESS;
+ ui_ipc_free(1);
error:
logs("%s\n", "exiting ..");
if (ffd >= 0) close(ffd);
- ui_ipc_free(1);
+ ui_ipc_free(0);
log_free();
exit(ret);
}
diff --git a/src/ui.c b/src/ui.c
index 4d9fac8..8dab68a 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -48,7 +48,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_busy = PTHREAD_MUTEX_INITIALIZER;
void
@@ -181,16 +180,18 @@ do_ui_update(bool timed_out)
if (timed_out == TRUE && atmout > 0) {
atmout--;
} else if (timed_out == TRUE && atmout == 0) {
- ui_ipc_semwait(SEM_UI);
+ ui_ipc_semtrywait(SEM_UI);
} else {
atmout = APP_TIMEOUT;
}
while (cur != NULL) {
if (cur->cbs.ui_element != NULL) {
- cur->cbs.ui_element(cur->wnd, cur->data, timed_out);
+ if ( (retval = cur->cbs.ui_element(cur->wnd, cur->data, timed_out)) != UICB_OK)
+ break;
doupdate();
} else {
retval = UICB_ERR_CB;
+ break;
}
cur = cur->next;
}
@@ -199,7 +200,7 @@ 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) */
- retval |= (wmove(wnd_main, cur_y, cur_x) << 4);
+ wmove(wnd_main, cur_y, cur_x);
wrefresh(wnd_main);
return (retval);
}
@@ -211,15 +212,13 @@ ui_thrd(void *arg)
struct timespec now;
do_ui_update(true);
- ui_ipc_sempost(SEM_RD);
pthread_mutex_lock(&mtx_update);
clock_gettime(CLOCK_REALTIME, &now);
now.tv_sec += UILOOP_TIMEOUT;
while ( ui_ipc_getvalue(SEM_UI) > 0 ) {
cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &now);
- pthread_mutex_lock(&mtx_busy);
- do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) );
- pthread_mutex_unlock(&mtx_busy);
+ if ( do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ) != UICB_OK )
+ break;
if (cnd_ret == ETIMEDOUT) {
clock_gettime(CLOCK_REALTIME, &now);
now.tv_sec += UILOOP_TIMEOUT;
@@ -230,23 +229,25 @@ ui_thrd(void *arg)
}
void
-ui_thrd_force_update(void)
+ui_thrd_force_update(bool force_all)
{
- pthread_mutex_lock(&mtx_busy);
+ pthread_mutex_lock(&mtx_update);
+ if (force_all)
+ touchwin(wnd_main);
pthread_cond_signal(&cnd_update);
- pthread_mutex_unlock(&mtx_busy);
+ pthread_mutex_unlock(&mtx_update);
}
void
ui_thrd_suspend(void)
{
- pthread_mutex_lock(&mtx_busy);
+ pthread_mutex_lock(&mtx_update);
}
void
ui_thrd_resume(void)
{
- pthread_mutex_unlock(&mtx_busy);
+ pthread_mutex_unlock(&mtx_update);
}
WINDOW *
@@ -293,6 +294,36 @@ stop_ui_thrd(void)
return (pthread_join(thrd, NULL));
}
+int ui_wgetchtest(int timeout, char testchar)
+{
+ int retval = DOUI_OK;
+ usleep(timeout/2);
+ pthread_mutex_lock(&mtx_update);
+ if ( ui_ipc_getvalue(SEM_UI) <= 0 ) {
+ retval = DOUI_KEY;
+ } else if ( wgetch(wnd_main) == testchar ) {
+ retval = DOUI_KEY;
+ }
+ pthread_mutex_unlock(&mtx_update);
+ usleep(timeout/2);
+ return retval;
+}
+
+char ui_wgetch(int timeout)
+{
+ char key;
+ usleep(timeout/2);
+ pthread_mutex_lock(&mtx_update);
+ if ( ui_ipc_getvalue(SEM_UI) <= 0 ) {
+ key = ERR;
+ } else {
+ key = wgetch(wnd_main);
+ }
+ pthread_mutex_unlock(&mtx_update);
+ usleep(timeout/2);
+ return key;
+}
+
int
do_ui(void)
{
@@ -308,17 +339,17 @@ do_ui(void)
pthread_mutex_unlock(&mtx_update);
return ret;
}
- ui_ipc_semwait(SEM_RD);
pthread_mutex_unlock(&mtx_update);
- wtimeout(stdscr, 500);
+ timeout(0);
while ( ui_ipc_getvalue(SEM_UI) > 0 ) {
- if ((key = wgetch(wnd_main)) == ERR) {
+ if ( (key = ui_wgetch(3000)) == ERR )
continue;
- }
+
if ( process_key(key) != true ) {
raise(SIGTERM);
}
- ui_thrd_force_update();
+
+ ui_thrd_force_update(false);
}
stop_ui_thrd();
free_ui_elements();
diff --git a/src/ui.h b/src/ui.h
index 52090ce..d8b1f9b 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -13,6 +13,7 @@
#define DOUI_ERR 4
#define DOUI_TMOUT 5
#define DOUI_NINIT 6
+#define DOUI_KEY 7
#define UILOOP_TIMEOUT 1
@@ -70,7 +71,7 @@ int
deactivate_ui_input(void *data);
void
-ui_thrd_force_update(void);
+ui_thrd_force_update(bool force_all);
void
ui_thrd_suspend(void);
@@ -84,6 +85,10 @@ init_ui(void);
void
free_ui(void);
+char ui_wgetch(int timeout);
+
+int ui_wgetchtest(int timeout, char testchar);
+
int
do_ui(void);
diff --git a/src/ui_elements.c b/src/ui_elements.c
index 78aba85..aae1aff 100644
--- a/src/ui_elements.c
+++ b/src/ui_elements.c
@@ -25,7 +25,6 @@ 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";
@@ -60,17 +59,17 @@ infownd_update(WINDOW *win, struct txtwindow *tw, bool ui_timeout)
}
void
-show_info_wnd(struct txtwindow *wnd, char *title, char *text, chtype fore, chtype back, bool activate, bool blink)
+show_info_wnd(struct txtwindow *wnd, char *_title, char *text, chtype fore, chtype back, bool activate, bool blink)
{
ui_thrd_suspend();
+ deactivate_input(pw_input);
+ set_txtwindow_active(wnd, activate);
set_txtwindow_blink(wnd, blink);
set_txtwindow_color(wnd, fore, back);
- set_txtwindow_title(wnd, title);
+ set_txtwindow_title(wnd, _title);
set_txtwindow_text(wnd, text);
- if (activate)
- set_txtwindow_active(wnd, true);
ui_thrd_resume();
- ui_thrd_force_update();
+ ui_thrd_force_update(false);
}
static int
@@ -88,6 +87,7 @@ passwd_input_cb(WINDOW *wnd, void *data, int key)
clear_input(wnd, a);
deactivate_input(pw_input);
ui_thrd_resume();
+ ui_thrd_force_update(false);
ui_ipc_msgrecv(MQ_IF, ipc_buf);
show_info_wnd(infownd, "BUSY", ipc_buf, COLOR_PAIR(5), COLOR_PAIR(5), true, false);
@@ -96,14 +96,15 @@ passwd_input_cb(WINDOW *wnd, void *data, int key)
if (ui_ipc_msgcount(MQ_IF) > 0) {
ui_ipc_msgrecv(MQ_IF, ipc_buf);
- show_info_wnd(infownd, "ERROR", ipc_buf, COLOR_PAIR(4), COLOR_PAIR(4), false, true);
- while (wgetch(stdscr) != '\n') { };
+ show_info_wnd(infownd, "ERROR", ipc_buf, COLOR_PAIR(4), COLOR_PAIR(4), true, true);
+ while (ui_wgetchtest(1500, '\n') != DOUI_KEY) { };
}
ui_thrd_suspend();
set_txtwindow_active(infownd, false);
activate_input(pw_input);
ui_thrd_resume();
+ ui_thrd_force_update(true);
ui_ipc_sempost(SEM_IN);
break;
@@ -111,15 +112,7 @@ passwd_input_cb(WINDOW *wnd, void *data, int key)
del_input(wnd, a);
break;
case UIKEY_ESC:
- wtimeout(stdscr, 0);
- ui_thrd_suspend();
- deactivate_input(pw_input);
- set_txtwindow_active(infownd, true);
- set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5));
- set_txtwindow_title(infownd, "BUSY");
- set_txtwindow_text(infownd, "bye bye");
- ui_thrd_resume();
- ui_thrd_force_update();
+ show_info_wnd(infownd, "BUSY", "bye bye", COLOR_PAIR(5), COLOR_PAIR(5), true, true);
sleep(2);
return DOUI_ERR;
case UIKEY_DOWN:
@@ -132,7 +125,6 @@ passwd_input_cb(WINDOW *wnd, void *data, int key)
default:
add_input(wnd, a, key);
}
- refresh();
return DOUI_OK;
}
diff --git a/src/ui_ipc.c b/src/ui_ipc.c
index cf137e7..669c1d6 100644
--- a/src/ui_ipc.c
+++ b/src/ui_ipc.c
@@ -48,7 +48,6 @@ ui_ipc_init(int is_master)
JMP_IF( sems[SEM_UI] = sem_open(SEM_GUI, sp_oflags, crt_flags, 0), SEM_FAILED, error );
JMP_IF( sems[SEM_IN] = sem_open(SEM_INP, sp_oflags, crt_flags, 0), SEM_FAILED, error );
JMP_IF( sems[SEM_BS] = sem_open(SEM_BSY, sp_oflags, crt_flags, 0), SEM_FAILED, error );
- JMP_IF( sems[SEM_RD] = sem_open(SEM_RDY, sp_oflags, crt_flags, 0), SEM_FAILED, error );
return 0;
error:
return errno;
@@ -69,7 +68,6 @@ ui_ipc_free(int is_master)
sem_unlink(SEM_BSY);
sem_unlink(SEM_GUI);
sem_unlink(SEM_INP);
- sem_unlink(SEM_RDY);
mq_unlink(MSQ_PWD);
mq_unlink(MSQ_INF);
}
diff --git a/src/ui_ipc.h b/src/ui_ipc.h
index 5ecfaa4..0b71d08 100644
--- a/src/ui_ipc.h
+++ b/src/ui_ipc.h
@@ -8,8 +8,7 @@
enum UI_IPC_SEM {
- SEM_RD = 0, /* UI Init done? */
- SEM_UI, /* TUI active? */
+ SEM_UI = 0, /* TUI active? */
SEM_IN, /* Textfield has input avail */
SEM_BS, /* Master process busy */
SEM_NUM
diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h
index 5ebbd65..260a38a 100644
--- a/src/ui_txtwindow.h
+++ b/src/ui_txtwindow.h
@@ -10,6 +10,10 @@
#define set_txtwindow_active(wnd, activate) wnd->active = activate;
#define set_txtwindow_blink(wnd, blink) wnd->title_blink = blink;
+struct txtwindow;
+
+typedef int (*window_func)(WINDOW *, struct txtwindow *, bool);
+
struct txtwindow {
unsigned int y;
unsigned int x;
@@ -20,12 +24,11 @@ struct txtwindow {
bool title_blink;
size_t title_len;
char **text;
- int (*window_func)(WINDOW *, struct txtwindow *, bool);
+ window_func window_func;
chtype attrs;
chtype text_attrs;
};
-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);