From f2f6ea5029c6c43dc43d714978daca38c03a8a83 Mon Sep 17 00:00:00 2001 From: toni Date: Sun, 8 Nov 2015 20:28:30 +0100 Subject: - changed dir structure - fixed ipc semaphore/mq stuff --- src/ui.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 src/ui.c (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..2fb5a1f --- /dev/null +++ b/src/ui.c @@ -0,0 +1,270 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ui.h" +#include "ui_ipc.h" +#include "ui_elements.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.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; +static WINDOW *wnd_main; +static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, + /* current active input */ *active = NULL; +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 +register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd) +{ + struct nask_ui *tmp, *new; + + new = calloc(1, sizeof(struct nask_ui)); + new->cbs = *cbs; + new->wnd = wnd; + new->data = data; + new->next = NULL; + if (nui == NULL) { + nui = new; + nui->next = NULL; + } else { + tmp = nui; + while (tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = new; + } +} + +void +unregister_ui_elt(void *data) +{ + struct nask_ui *cur, *next, *before = NULL; + + cur = nui; + while (cur != NULL) { + next = cur->next; + if (cur->data != NULL && cur->data == data) { + free(cur); + if (before != NULL) { + before->next = next; + } else { + nui = next; + } + } + before = cur; + cur = next; + } +} + +int +activate_ui_input(void *data) +{ + struct nask_ui *cur = nui; + + if (cur == NULL || data == NULL) return DOUI_NINIT; + 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 ) { + active = cur; + return DOUI_OK; + } + } + cur = cur->next; + } + return DOUI_ERR; +} + +int +reactivate_ui_input(void) +{ + if (active) { + if (active->cbs.ui_element) { + } + return ( active->cbs.ui_input(active->wnd, active->data, UIKEY_ACTIVATE) ); + } else return DOUI_ERR; +} + +static bool +process_key(char key) +{ + atmout = APP_TIMEOUT; + if ( active != NULL ) { + return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + } + return false; +} + +static int +do_ui_update(bool timed_out) +{ + int retval = UICB_OK; + int curx = getcurx(wnd_main); + int cury = getcury(wnd_main); + struct nask_ui *cur = nui; + + /* call all draw callback's */ + erase(); + while (cur != NULL) { + if (cur->cbs.ui_element != NULL) { + cur->cbs.ui_element(cur->wnd, cur->data, timed_out); + doupdate(); + } else { + retval = UICB_ERR_CB; + } + 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) */ + wmove(wnd_main, cury, curx); + wrefresh(wnd_main); + return (retval); +} + +static void * +ui_thrd(void *arg) +{ + int cnd_ret; + struct timeval now; + struct timespec wait; + + gettimeofday(&now, NULL); + wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; + wait.tv_nsec = now.tv_usec * 1000; + do_ui_update(true); + ui_ipc_sempost(SEM_RD); + pthread_mutex_lock(&mtx_update); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + pthread_mutex_unlock(&mtx_busy); + cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + pthread_mutex_lock(&mtx_busy); + if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); + do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); + if (cnd_ret == ETIMEDOUT) { + wait.tv_sec += UILOOP_TIMEOUT; + } + } + pthread_mutex_unlock(&mtx_update); + return (NULL); +} + +void +ui_thrd_force_update(void) +{ + pthread_mutex_lock(&mtx_busy); + pthread_cond_signal(&cnd_update); + pthread_mutex_unlock(&mtx_busy); +} + +WINDOW * +init_ui(void) +{ + wnd_main = initscr(); + max_x = getmaxx(wnd_main); + max_y = getmaxy(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); + init_pair(4, COLOR_YELLOW, COLOR_RED); + raw(); + keypad(stdscr, TRUE); + noecho(); + cbreak(); + return (wnd_main); +} + +void +free_ui(void) +{ + delwin(wnd_main); + endwin(); + clear(); + printf(" \033[2J\n"); +} + +static int +run_ui_thrd(void) { + return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); +} + +static int +stop_ui_thrd(void) +{ + return (pthread_join(thrd, NULL)); +} + +int +do_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) { + return ret; + } + ui_ipc_semwait(SEM_RD); + wtimeout(wnd_main, 1000); + pthread_mutex_unlock(&mtx_update); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + if ((key = wgetch(wnd_main)) == '\0') { + break; + } + if (key == -1) { + continue; + } + if ( process_key(key) != true ) { + ui_ipc_semtrywait(SEM_UI); + ui_thrd_force_update(); + } + } + stop_ui_thrd(); + free_ui_elements(); + + return DOUI_OK; +} + -- cgit v1.2.3 From 6f7ce363ee56f96fe797433ed48b18a0787ed110 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 15:28:48 +0100 Subject: trying to fix ncurses problerms .. fixed --- src/ui.c | 35 +++++++++++++++++++++++------------ src/ui.h | 3 +++ src/ui_elements.c | 16 +++------------- src/ui_input.c | 11 ++++++++++- src/ui_input.h | 3 +++ 5 files changed, 42 insertions(+), 26 deletions(-) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index 2fb5a1f..780452d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -47,6 +47,7 @@ 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; @@ -96,39 +97,49 @@ unregister_ui_elt(void *data) int activate_ui_input(void *data) { + int ret = DOUI_ERR; 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 ) { active = cur; - return DOUI_OK; + ret = DOUI_OK; + break; } } cur = cur->next; } - return DOUI_ERR; + pthread_mutex_unlock(&mtx_input); + return ret; } int -reactivate_ui_input(void) +deactivate_ui_input(void *data) { - if (active) { - if (active->cbs.ui_element) { - } - return ( active->cbs.ui_input(active->wnd, active->data, UIKEY_ACTIVATE) ); - } else return DOUI_ERR; + 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; } static bool process_key(char key) { + bool ret = false; + atmout = APP_TIMEOUT; if ( active != NULL ) { - return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + ret = ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } - return false; + return ret; } static int @@ -174,11 +185,11 @@ ui_thrd(void *arg) ui_ipc_sempost(SEM_RD); pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - pthread_mutex_unlock(&mtx_busy); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - pthread_mutex_lock(&mtx_busy); 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); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; } diff --git a/src/ui.h b/src/ui.h index 91d5403..fa895ca 100644 --- a/src/ui.h +++ b/src/ui.h @@ -51,6 +51,9 @@ unregister_ui_elt(void *data); int activate_ui_input(void *data); +int +deactivate_ui_input(void *data); + void ui_thrd_force_update(void); diff --git a/src/ui_elements.c b/src/ui_elements.c index 29c2e32..27ccea0 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -71,22 +71,13 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; -/* - * if ( process_key(key, pw_input, wnd_main) == false ) { - * curs_set(0); - * memset(mq_msg, '\0', IPC_MQSIZ+1); - * mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); - * set_txtwindow_text(infownd, mq_msg); - * set_txtwindow_active(infownd, true); - * sleep(3); - * sem_trywait(sp_ui); - * } - * activate_input(wnd_main, pw_input); - */ + switch (key) { case UIKEY_ENTER: + deactivate_input(pw_input); mq_passwd_send(a->input, a->input_len); clear_input(wnd, a); + set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, "BLA"); set_txtwindow_active(infownd, true); break; @@ -125,7 +116,6 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) register_statusbar(lower); register_anic_default(heartbeat); register_txtwindow(infownd); - set_txtwindow_title(infownd, "WARNING"); activate_input(wnd_main, pw_input); set_statusbar_text(higher, title); } diff --git a/src/ui_input.c b/src/ui_input.c index 5f9a679..3065f1d 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -125,6 +125,7 @@ activate_input(WINDOW *win, struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); size_t p_len = strlen(a->prompt); + curs_set(1); if (win == NULL) { move(a->y, a->x + p_len + a->cur_pos); } else { @@ -133,6 +134,13 @@ activate_input(WINDOW *win, struct input *a) return (activate_ui_input( (void *) a )); } +int +deactivate_input(struct input *a) +{ + curs_set(0); + return (deactivate_ui_input(a)); +} + int add_input(WINDOW *win, struct input *a, int key) { @@ -142,7 +150,8 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_pos; ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); - print_input(win, a); + //print_input(win, a); + ui_thrd_force_update(); return (UICB_OK); } diff --git a/src/ui_input.h b/src/ui_input.h index 657c926..8ed7fc3 100644 --- a/src/ui_input.h +++ b/src/ui_input.h @@ -28,6 +28,9 @@ free_input(struct input *a); int activate_input(WINDOW *win, struct input *a); +int +deactivate_input(struct input *a); + int add_input(WINDOW *win, struct input *a, int key); -- cgit v1.2.3 From 7bceed7feae9e9abd58cc653596f6a3678b0ce37 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 22:09:53 +0100 Subject: ui thread does now the cursor mgmt --- src/ui.c | 26 +++++++++++++++++++++++--- src/ui.h | 9 +++++++++ src/ui_input.c | 15 ++++++--------- 3 files changed, 38 insertions(+), 12 deletions(-) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index 780452d..bb12936 100644 --- a/src/ui.c +++ b/src/ui.c @@ -40,6 +40,7 @@ static unsigned int max_x, max_y; +static unsigned int cur_x, cur_y; static WINDOW *wnd_main; static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, /* current active input */ *active = NULL; @@ -94,6 +95,25 @@ unregister_ui_elt(void *data) } } +void +ui_set_cur(unsigned int x, unsigned int y) +{ + cur_x = x; + cur_y = y; +} + +int +ui_get_curx(void) +{ + return (cur_x); +} + +int +ui_get_cury(void) +{ + return (cur_y); +} + int activate_ui_input(void *data) { @@ -146,8 +166,6 @@ static int do_ui_update(bool timed_out) { int retval = UICB_OK; - int curx = getcurx(wnd_main); - int cury = getcury(wnd_main); struct nask_ui *cur = nui; /* call all draw callback's */ @@ -166,7 +184,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) */ - wmove(wnd_main, cury, curx); + wmove(wnd_main, cur_y, cur_x); wrefresh(wnd_main); return (retval); } @@ -212,6 +230,8 @@ 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); diff --git a/src/ui.h b/src/ui.h index fa895ca..68f9fcd 100644 --- a/src/ui.h +++ b/src/ui.h @@ -48,6 +48,15 @@ register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); +void +ui_set_cur(unsigned int x, unsigned int y); + +int +ui_get_curx(void); + +int +ui_get_cury(void); + int activate_ui_input(void *data); diff --git a/src/ui_input.c b/src/ui_input.c index 3065f1d..b32570b 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -124,13 +124,8 @@ int activate_input(WINDOW *win, struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); - size_t p_len = strlen(a->prompt); curs_set(1); - if (win == NULL) { - move(a->y, a->x + p_len + a->cur_pos); - } else { - wmove(win, a->y, a->x + p_len + a->cur_pos); - } + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); return (activate_ui_input( (void *) a )); } @@ -150,7 +145,7 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_pos; ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); - //print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); ui_thrd_force_update(); return (UICB_OK); } @@ -172,7 +167,8 @@ del_input(WINDOW *win, struct input *a) --a->cur_pos; } mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); - print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); + ui_thrd_force_update(); return (UICB_OK); } @@ -184,7 +180,8 @@ clear_input(WINDOW *win, struct input *a) a->input_len = 0; a->input_pos = 0; a->cur_pos = 0; - print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); + ui_thrd_force_update(); return (UICB_OK); } -- cgit v1.2.3 From 7c3c6b99bec920ea850739e62b9fec8a29dc0dc0 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 22:53:40 +0100 Subject: ~auto(conf|make)~ --- Makefile | 53 ----------------------------------- Makefile.am | 11 ++++++++ Makefile.debug | 53 +++++++++++++++++++++++++++++++++++ autogen.sh | 15 ++++++++++ configure.ac | 68 ++++++++++++++++++++++++++++++++++++++++++++ m4/ax_check_typedef.m4 | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 3 ++ src/ui.c | 1 - 8 files changed, 226 insertions(+), 54 deletions(-) delete mode 100644 Makefile create mode 100644 Makefile.am create mode 100644 Makefile.debug create mode 100755 autogen.sh create mode 100644 configure.ac create mode 100644 m4/ax_check_typedef.m4 create mode 100644 src/Makefile.am (limited to 'src/ui.c') diff --git a/Makefile b/Makefile deleted file mode 100644 index 9efaec9..0000000 --- a/Makefile +++ /dev/null @@ -1,53 +0,0 @@ -CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP -DBGFLAGS = -g -LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt -CC = gcc -INSTALL = install -STRIP = strip -VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) -BIN = naskpass -SOURCES = $(wildcard src/*.c) -OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) -DEPS = $(patsubst %.c,%.d,$(SOURCES)) - -all: $(OBJECTS) $(BIN) - -%.o: %.c - $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ - -$(BIN): $(SOURCES) - $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) - $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all - -strip: $(OBJECTS) $(BIN) - $(STRIP) $(BIN) - -release: all strip - -debug: - @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' - @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' - -install: - $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass - $(INSTALL) -D -m 0755 scripts/naskpass.inithook $(DESTDIR)/usr/share/naskpass/naskpass.hook.initramfs - $(INSTALL) -D -m 0755 scripts/naskpass.initscript $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs - $(INSTALL) -D -m 0755 scripts/naskconf $(DESTDIR)/usr/share/naskpass/naskconf - -uninstall: - rm -f $(DESTDIR)/lib/cryptsetup/naskpass - rm -f $(DESTDIR)/usr/share/initramfs-tools/hooks/naskpass - rm -f $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs - rm -f $(DESTDIR)/usr/share/naskpass/naskconf - rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass - -clean: - rm -f $(DEPS) - rm -f $(OBJECTS) - rm -f $(BIN) - $(MAKE) -C tests clean - -source: - -dh_make --createorig -p naskpass_$(VERSION) -s -y - -.PHONY: all install clean diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..d682608 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,11 @@ +SUBDIRS = src + + +clean-local: + rm -f naskpass + +distclean-local: clean-local + rm -f aclocal.m4 config.log config.status configure + rm -f Makefile.in src/Makefile.in src/aconfig.h.in src/aconfig.h.in~ src/*.d + rm -rf build autom4te.cache + diff --git a/Makefile.debug b/Makefile.debug new file mode 100644 index 0000000..9efaec9 --- /dev/null +++ b/Makefile.debug @@ -0,0 +1,53 @@ +CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP +DBGFLAGS = -g +LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt +CC = gcc +INSTALL = install +STRIP = strip +VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) +BIN = naskpass +SOURCES = $(wildcard src/*.c) +OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) +DEPS = $(patsubst %.c,%.d,$(SOURCES)) + +all: $(OBJECTS) $(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ + +$(BIN): $(SOURCES) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) + $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all + +strip: $(OBJECTS) $(BIN) + $(STRIP) $(BIN) + +release: all strip + +debug: + @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' + @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' + +install: + $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass + $(INSTALL) -D -m 0755 scripts/naskpass.inithook $(DESTDIR)/usr/share/naskpass/naskpass.hook.initramfs + $(INSTALL) -D -m 0755 scripts/naskpass.initscript $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs + $(INSTALL) -D -m 0755 scripts/naskconf $(DESTDIR)/usr/share/naskpass/naskconf + +uninstall: + rm -f $(DESTDIR)/lib/cryptsetup/naskpass + rm -f $(DESTDIR)/usr/share/initramfs-tools/hooks/naskpass + rm -f $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs + rm -f $(DESTDIR)/usr/share/naskpass/naskconf + rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass + +clean: + rm -f $(DEPS) + rm -f $(OBJECTS) + rm -f $(BIN) + $(MAKE) -C tests clean + +source: + -dh_make --createorig -p naskpass_$(VERSION) -s -y + +.PHONY: all install clean diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..96fb4e9 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +rm -f config.cache +mkdir -p build + +echo "Looking in current directory for macros." +aclocal -I . +echo "Adding missing files." +automake --add-missing --force-missing +echo "Autoconf, Autoheader, Automake" +autoconf +autoheader +automake --foreign --add-missing --force-missing --copy +exit $? + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..3786c0e --- /dev/null +++ b/configure.ac @@ -0,0 +1,68 @@ +# -*- Autoconf -*- +# configure script generation for ncblog +# + +AC_PREREQ([2.67]) +AC_INIT([naskpass], [0.01], [matzeton@googlemail.com]) +AC_CONFIG_AUX_DIR([build]) +AM_INIT_AUTOMAKE([1.11 foreign no-define -Wall -Werror]) +AM_WITH_DMALLOC +AC_CANONICAL_BUILD +AC_CANONICAL_HOST +AC_CONFIG_SRCDIR([src/aconfig.h.in]) +AC_CONFIG_HEADER([src/aconfig.h]) +AC_CONFIG_MACRO_DIR([m4]) +AC_USE_SYSTEM_EXTENSIONS +CFLAGS="-Os" +LDFLAGS="" + +# Checks for programs. +AM_PROG_AR +AM_PROG_INSTALL_STRIP +AC_PROG_CC +AC_PROG_INSTALL +AC_PROG_RANLIB +AC_C_INLINE +AC_PREFIX_DEFAULT([/usr]) +AC_CHECK_TOOL([STRIP],[strip]) + +# Checks for header files. +AC_HEADER_SYS_WAIT +AC_HEADER_TIME +AC_HEADER_STDBOOL +AC_HEADER_STDC +AC_HEADER_STAT +AC_HEADER_DIRENT +AC_HEADER_ASSERT +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_COMPUTE_INT +AC_TYPE_UID_T +AC_TYPE_MODE_T +AC_TYPE_PID_T +AC_TYPE_SIZE_T +AC_TYPE_SSIZE_T +AC_TYPE_UINT16_T +AC_TYPE_UINT8_T +AX_CHECK_TYPEDEF([size_t], [stdio.h],,[AC_MSG_ERROR([*** Missing size_t typedef in stdio.h])]) +AX_CHECK_TYPEDEF([ssize_t], [stdio.h],,[AC_MSG_ERROR([*** Missing size_t typedef in stdio.h])]) + +# Checks for library functions. +AC_FUNC_FORK +AC_FUNC_MALLOC +AC_FUNC_MMAP +AC_FUNC_REALLOC +AC_FUNC_STRNLEN +AC_FUNC_STAT +AC_FUNC_MKTIME +AC_FUNC_VPRINTF +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork gettimeofday memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) + +AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) +LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" +AC_SUBST([AM_CFLAGS]) +AC_SUBST([AM_LDFLAGS]) +AC_CONFIG_FILES([Makefile src/Makefile]) +AC_OUTPUT + diff --git a/m4/ax_check_typedef.m4 b/m4/ax_check_typedef.m4 new file mode 100644 index 0000000..13b9d5e --- /dev/null +++ b/m4/ax_check_typedef.m4 @@ -0,0 +1,76 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_check_typedef.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_CHECK_TYPEDEF(TYPEDEF, HEADER [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]) +# +# DESCRIPTION +# +# Check if the given typedef-name is recognized as a type. The trick is to +# use a sizeof(TYPEDEF) and see if the compiler is happy with that. +# +# This can be thought of as a mixture of AC_CHECK_TYPE(TYPEDEF,DEFAULT) +# and AC_CHECK_LIB(LIBRARY,FUNCTION,ACTION-IF-FOUND,ACTION-IF-NOT-FOUND). +# +# A convenience macro AX_CHECK_TYPEDEF_ is provided that will not emit any +# message to the user - it just executes one of the actions. +# +# LICENSE +# +# Copyright (c) 2008 Guido U. Draheim +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 5 + +AU_ALIAS([AC_CHECK_TYPEDEF], [AX_CHECK_TYPEDEF]) +AC_DEFUN([AX_CHECK_TYPEDEF_], +[dnl +ac_lib_var=`echo $1['_']$2 | sed 'y%./+-%__p_%'` +AC_CACHE_VAL(ac_cv_lib_$ac_lib_var, +[ eval "ac_cv_type_$ac_lib_var='not-found'" + ac_cv_check_typedef_header=`echo ifelse([$2], , stddef.h, $2)` + AC_TRY_COMPILE( [#include <$ac_cv_check_typedef_header>], + [int x = sizeof($1); x = x;], + eval "ac_cv_type_$ac_lib_var=yes" , + eval "ac_cv_type_$ac_lib_var=no" ) + if test `eval echo '$ac_cv_type_'$ac_lib_var` = "no" ; then + ifelse([$4], , :, $4) + else + ifelse([$3], , :, $3) + fi +])]) + +dnl AX_CHECK_TYPEDEF(TYPEDEF, HEADER [, ACTION-IF-FOUND, +dnl [, ACTION-IF-NOT-FOUND ]]) +AC_DEFUN([AX_CHECK_TYPEDEF], +[dnl + AC_MSG_CHECKING([for $1 in $2]) + AX_CHECK_TYPEDEF_($1,$2,AC_MSG_RESULT(yes),AC_MSG_RESULT(no))dnl +]) diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..07b4cfa --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,3 @@ +bin_PROGRAMS=naskpass +naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_nwindow.c ui_statusbar.c + diff --git a/src/ui.c b/src/ui.c index bb12936..8b3d49f 100644 --- a/src/ui.c +++ b/src/ui.c @@ -12,7 +12,6 @@ #include #include #include -#include #include "ui.h" #include "ui_ipc.h" -- cgit v1.2.3 From dcf8b9b6981d0d21db6edb3265b56c55b39e4ff4 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 01:18:11 +0100 Subject: waitpid(...) checks now for WIFEXITED instead of WIFSIGNALED --- Makefile.am | 2 +- configure.ac | 2 +- src/aconfig.h.in | 299 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 2 - src/main.c | 13 +-- src/ui.c | 2 +- src/ui_elements.c | 11 +- src/ui_ipc.c | 30 ++++-- src/ui_ipc.h | 9 +- tests/mqtest.c | 23 ++++- 10 files changed, 363 insertions(+), 30 deletions(-) create mode 100644 src/aconfig.h.in (limited to 'src/ui.c') diff --git a/Makefile.am b/Makefile.am index d682608..1d76541 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,6 +6,6 @@ clean-local: distclean-local: clean-local rm -f aclocal.m4 config.log config.status configure - rm -f Makefile.in src/Makefile.in src/aconfig.h.in src/aconfig.h.in~ src/*.d + rm -f Makefile.in src/Makefile.in src/aconfig.h.in~ src/*.d rm -rf build autom4te.cache diff --git a/configure.ac b/configure.ac index 3786c0e..943f5cf 100644 --- a/configure.ac +++ b/configure.ac @@ -34,7 +34,7 @@ AC_HEADER_STDC AC_HEADER_STAT AC_HEADER_DIRENT AC_HEADER_ASSERT -AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h]) +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h alloca.h]) # Checks for typedefs, structures, and compiler characteristics. AC_COMPUTE_INT diff --git a/src/aconfig.h.in b/src/aconfig.h.in new file mode 100644 index 0000000..f690396 --- /dev/null +++ b/src/aconfig.h.in @@ -0,0 +1,299 @@ +/* src/aconfig.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the `alarm' function. */ +#undef HAVE_ALARM + +/* Define to 1 if you have the header file. */ +#undef HAVE_ALLOCA_H + +/* Define to 1 if you have the `asprintf' function. */ +#undef HAVE_ASPRINTF + +/* Define to 1 if you have the `clock_gettime' function. */ +#undef HAVE_CLOCK_GETTIME + +/* Define to 1 if you have the `close' function. */ +#undef HAVE_CLOSE + +/* Do NOT change THIS! */ +#undef HAVE_CONFIG + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_DIRENT_H + +/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ +#undef HAVE_DOPRNT + +/* Define to 1 if you have the header file. */ +#undef HAVE_ERRNO_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_FCNTL_H + +/* Define to 1 if you have the `fork' function. */ +#undef HAVE_FORK + +/* Define to 1 if you have the `fprintf' function. */ +#undef HAVE_FPRINTF + +/* Define to 1 if you have the `getpagesize' function. */ +#undef HAVE_GETPAGESIZE + +/* Define to 1 if you have the `gettimeofday' function. */ +#undef HAVE_GETTIMEOFDAY + +/* Define to 1 if you have the header file. */ +#undef HAVE_INTTYPES_H + +/* Define to 1 if your system has a GNU libc compatible `malloc' function, and + to 0 otherwise. */ +#undef HAVE_MALLOC + +/* Define to 1 if you have the `memcpy' function. */ +#undef HAVE_MEMCPY + +/* Define to 1 if you have the `memmove' function. */ +#undef HAVE_MEMMOVE + +/* Define to 1 if you have the header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `memset' function. */ +#undef HAVE_MEMSET + +/* Define to 1 if you have the `mkfifo' function. */ +#undef HAVE_MKFIFO + +/* Define to 1 if you have a working `mmap' system call. */ +#undef HAVE_MMAP + +/* Define to 1 if you have the header file. */ +#undef HAVE_MQUEUE_H + +/* Define to 1 if you have the header file, and it defines `DIR'. */ +#undef HAVE_NDIR_H + +/* Define to 1 if you have the `open' function. */ +#undef HAVE_OPEN + +/* Define to 1 if you have the `printf' function. */ +#undef HAVE_PRINTF + +/* Define to 1 if your system has a GNU libc compatible `realloc' function, + and to 0 otherwise. */ +#undef HAVE_REALLOC + +/* Define to 1 if you have the header file. */ +#undef HAVE_SEMAPHORE_H + +/* Define to 1 if you have the `stat' function. */ +#undef HAVE_STAT + +/* Define to 1 if `stat' has the bug that it succeeds when given the + zero-length file name argument. */ +#undef HAVE_STAT_EMPTY_STRING_BUG + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDBOOL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDIO_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the `strdup' function. */ +#undef HAVE_STRDUP + +/* Define to 1 if you have the `strerror' function. */ +#undef HAVE_STRERROR + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if you have the `strlen' function. */ +#undef HAVE_STRLEN + +/* Define to 1 if you have the `strndup' function. */ +#undef HAVE_STRNDUP + +/* Define to 1 if you have the `strnlen' function. */ +#undef HAVE_STRNLEN + +/* Define to 1 if you have the `strstr' function. */ +#undef HAVE_STRSTR + +/* Define to 1 if you have the `strtol' function. */ +#undef HAVE_STRTOL + +/* Define to 1 if you have the `system' function. */ +#undef HAVE_SYSTEM + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_DIR_H + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_NDIR_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_PARAM_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TIME_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_WAIT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_TIME_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_UNISTD_H + +/* Define to 1 if you have the `vfork' function. */ +#undef HAVE_VFORK + +/* Define to 1 if you have the header file. */ +#undef HAVE_VFORK_H + +/* Define to 1 if you have the `vprintf' function. */ +#undef HAVE_VPRINTF + +/* Define to 1 if `fork' works. */ +#undef HAVE_WORKING_FORK + +/* Define to 1 if `vfork' works. */ +#undef HAVE_WORKING_VFORK + +/* Define to 1 if the system has the type `_Bool'. */ +#undef HAVE__BOOL + +/* Define to 1 if `lstat' dereferences a symlink specified with a trailing + slash. */ +#undef LSTAT_FOLLOWS_SLASHED_SYMLINK + +/* Define to 1 if assertions should be disabled. */ +#undef NDEBUG + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the home page for this package. */ +#undef PACKAGE_URL + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* Define to 1 if the `S_IS*' macros in do not work properly. */ +#undef STAT_MACROS_BROKEN + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS + +/* Define to 1 if you can safely include both and . */ +#undef TIME_WITH_SYS_TIME + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# undef _ALL_SOURCE +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# undef _GNU_SOURCE +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# undef _POSIX_PTHREAD_SEMANTICS +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# undef _TANDEM_SOURCE +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# undef __EXTENSIONS__ +#endif + + +/* Define if using the dmalloc debugging malloc package */ +#undef WITH_DMALLOC + +/* Define to 1 if on MINIX. */ +#undef _MINIX + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +#undef _POSIX_1_SOURCE + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +#undef _POSIX_SOURCE + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +#undef _UINT8_T + +/* Define to `int' if doesn't define. */ +#undef gid_t + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +#undef inline +#endif + +/* Define to rpl_malloc if the replacement function should be used. */ +#undef malloc + +/* Define to `int' if does not define. */ +#undef mode_t + +/* Define to `int' if does not define. */ +#undef pid_t + +/* Define to rpl_realloc if the replacement function should be used. */ +#undef realloc + +/* Define to `unsigned int' if does not define. */ +#undef size_t + +/* Define to `int' if does not define. */ +#undef ssize_t + +/* Define to `int' if doesn't define. */ +#undef uid_t + +/* Define to the type of an unsigned integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +#undef uint16_t + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +#undef uint8_t + +/* Define as `fork' if `vfork' does not work. */ +#undef vfork diff --git a/src/config.h b/src/config.h index a7fddc0..9d72ef3 100644 --- a/src/config.h +++ b/src/config.h @@ -12,8 +12,6 @@ #define MSQ_PWD "/naskpass-passwd" #define MSQ_INF "/naskpass-info" -#define IPC_MQSIZ 128 - #ifdef _VERSION #define VERSION _VERSION #else diff --git a/src/main.c b/src/main.c index 5f0d6e9..8dbe953 100644 --- a/src/main.c +++ b/src/main.c @@ -142,21 +142,22 @@ main(int argc, char **argv) while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { ui_ipc_sempost(SEM_BS); if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } - } else if ( ui_ipc_msgrecv(MQ_PW, pbuf, IPC_MQSIZ) != (int)-1 ) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY))); + } else if ( ui_ipc_msgcount(MQ_PW) > 0 ) { + ui_ipc_msgrecv(MQ_PW, pbuf); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } ui_ipc_semwait(SEM_IN); } ui_ipc_semwait(SEM_BS); usleep(100000); waitpid(child, &c_status, WNOHANG); - if ( WIFSIGNALED(c_status) != 0 ) { + if ( WIFEXITED(c_status) != 0 ) { break; } } diff --git a/src/ui.c b/src/ui.c index 8b3d49f..5581bdc 100644 --- a/src/ui.c +++ b/src/ui.c @@ -203,7 +203,7 @@ ui_thrd(void *arg) 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); + 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); diff --git a/src/ui_elements.c b/src/ui_elements.c index 27ccea0..b274529 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -58,12 +58,12 @@ infownd_update(WINDOW *win, struct txtwindow *tw) } static int -mq_passwd_send(char *passwd, size_t len) +mq_passwd_send(char *passwd) { int ret; ui_ipc_sempost(SEM_IN); - ret = ui_ipc_msgsend(MQ_PW, passwd, len); + ret = ui_ipc_msgsend(MQ_PW, passwd); return ret; } @@ -71,14 +71,17 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; + char ipc_buf[IPC_MQSIZ+1]; + memset(ipc_buf, '\0', IPC_MQSIZ+1); switch (key) { case UIKEY_ENTER: deactivate_input(pw_input); - mq_passwd_send(a->input, a->input_len); + mq_passwd_send(a->input); clear_input(wnd, a); + ui_ipc_msgrecv(MQ_IF, ipc_buf); set_txtwindow_title(infownd, "BUSY"); - set_txtwindow_text(infownd, "BLA"); + set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); break; case UIKEY_BACKSPACE: diff --git a/src/ui_ipc.c b/src/ui_ipc.c index 9eac79e..cf137e7 100644 --- a/src/ui_ipc.c +++ b/src/ui_ipc.c @@ -1,12 +1,13 @@ #include #include -#include +#include #ifdef SEM_TIMEDWAIT #include #endif #include #include #include +#include #include #include "ui_ipc.h" @@ -35,14 +36,14 @@ ui_ipc_init(int is_master) sp_oflags = O_CREAT | O_EXCL; mq_oflags = O_NONBLOCK | O_CREAT | O_EXCL; crt_flags = S_IRUSR | S_IWUSR; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); } else { sp_oflags = 0; mq_oflags = 0; crt_flags = 0; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); } 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 ); @@ -117,14 +118,25 @@ ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) #endif int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len) +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr) { - return ( mq_send(msqs[e_mq], msg_ptr, msg_len, 0) ); + char *tmp = alloca(IPC_MQSIZ); + memset(tmp, '\0', IPC_MQSIZ); + strncpy(tmp, msg_ptr, IPC_MQSIZ); + return ( mq_send(msqs[e_mq], tmp, IPC_MQSIZ, 0) ); } ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len) +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr) { - return ( mq_receive(msqs[e_mq], msg_ptr, msg_len, NULL) ); + return mq_receive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL); } +long +ui_ipc_msgcount(enum UI_IPC_MSQ e_mq) +{ + struct mq_attr m_attr; + bzero(&m_attr, sizeof(struct mq_attr)); + if (mq_getattr(msqs[e_mq], &m_attr) != 0) return -1; + return m_attr.mq_curmsgs; +} diff --git a/src/ui_ipc.h b/src/ui_ipc.h index 2c5bcb5..ec36f35 100644 --- a/src/ui_ipc.h +++ b/src/ui_ipc.h @@ -4,6 +4,8 @@ #include "status.h" #include "config.h" +#define IPC_MQSIZ 128 + enum UI_IPC_SEM { SEM_RD = 0, /* UI Init done? */ @@ -44,9 +46,12 @@ ui_ipc_semtimedwait(enum UI_IPC_MSQ e_sp, int timeout); #endif int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len); +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr); ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len); +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr); + +long +ui_ipc_msgcount(enum UI_IPC_MSQ e_mq); #endif diff --git a/tests/mqtest.c b/tests/mqtest.c index e9bc8e7..f2808c8 100644 --- a/tests/mqtest.c +++ b/tests/mqtest.c @@ -1,18 +1,22 @@ #include #include +#include #include #include #include +#include #include #include static mqd_t mq_test; +static mqd_t mq_recv; static const size_t bufsiz = 256; int main(int argc, char **argv) { + int c_stat; struct mq_attr m_attr; char buf[bufsiz], recv[bufsiz]; unsigned int prio; @@ -31,10 +35,10 @@ int main(int argc, char **argv) mq_unlink("/testmq"); assert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); assert( mq_getattr(mq_test, &m_attr) == 0 ); - printf("flags.........: %ld\n" - "maxmsg........: %ld\n" - "msgsize.......: %ld\n" - "curmsg........: %ld\n", + printf("flags..........: %ld\n" + "maxmsg.........: %ld\n" + "msgsize........: %ld\n" + "curmsg.........: %ld\n", m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs); strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ"); @@ -44,6 +48,17 @@ int main(int argc, char **argv) printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + memset(recv, '\0', bufsiz); + if (fork() > 0) { + assert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + assert ( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 ); + printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + return 0; + } + printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); + assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + wait(&c_stat); + return 0; } -- cgit v1.2.3 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 (limited to 'src/ui.c') 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 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.c') 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.c') 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 From a3d1a3011a74cb9dca2a15edf47f766b69ff4331 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 18:45:43 +0100 Subject: replaced obsolete gettimeofday with clock_gettime (pthread_conf_timedwait) --- configure.ac | 2 +- src/aconfig.h.in | 3 --- src/ui.c | 20 +++++++++----------- src/ui_elements.c | 19 ++++++++++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) (limited to 'src/ui.c') diff --git a/configure.ac b/configure.ac index dc50072..bafaddc 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork gettimeofday memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" diff --git a/src/aconfig.h.in b/src/aconfig.h.in index f690396..a24ff8d 100644 --- a/src/aconfig.h.in +++ b/src/aconfig.h.in @@ -40,9 +40,6 @@ /* Define to 1 if you have the `getpagesize' function. */ #undef HAVE_GETPAGESIZE -/* Define to 1 if you have the `gettimeofday' function. */ -#undef HAVE_GETTIMEOFDAY - /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H diff --git a/src/ui.c b/src/ui.c index acf04f5..a3e43c2 100644 --- a/src/ui.c +++ b/src/ui.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -202,22 +203,20 @@ ui_thrd(void *arg) { int cnd_ret; struct timespec now; - struct timespec wait; - //gettimeofday(&now, NULL); - clock_gettime(CLOCK_REALTIME, &now); - wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; - wait.tv_nsec = now.tv_nsec * 1000; 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, &wait); + 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 (cnd_ret == ETIMEDOUT) { - wait.tv_sec += UILOOP_TIMEOUT; + clock_gettime(CLOCK_REALTIME, &now); + now.tv_sec += UILOOP_TIMEOUT; } } pthread_mutex_unlock(&mtx_update); @@ -235,7 +234,6 @@ ui_thrd_force_update(void) void ui_thrd_suspend(void) { - ui_thrd_force_update(); pthread_mutex_lock(&mtx_busy); } @@ -243,7 +241,6 @@ void ui_thrd_resume(void) { pthread_mutex_unlock(&mtx_busy); - ui_thrd_force_update(); } WINDOW * @@ -265,6 +262,7 @@ init_ui(void) raw(); keypad(stdscr, TRUE); noecho(); + nodelay(stdscr, TRUE); cbreak(); return (wnd_main); } @@ -305,13 +303,13 @@ do_ui(void) } ui_ipc_semwait(SEM_RD); pthread_mutex_unlock(&mtx_update); - wtimeout(stdscr, 1000); + wtimeout(stdscr, 500); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if ((key = wgetch(wnd_main)) == ERR) { continue; } if ( process_key(key) != true ) { - ui_ipc_semtrywait(SEM_UI); + raise(SIGTERM); } ui_thrd_force_update(); } diff --git a/src/ui_elements.c b/src/ui_elements.c index 9f4994d..535bc0c 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -66,7 +66,7 @@ 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); +// wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: ui_ipc_msgsend(MQ_PW, a->input); @@ -84,6 +84,7 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); ui_thrd_resume(); + ui_thrd_force_update(); sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); @@ -101,14 +102,22 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) 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(); 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(); + sleep(2); return DOUI_ERR; case UIKEY_DOWN: case UIKEY_UP: @@ -119,9 +128,9 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) break; default: add_input(wnd, a, key); - //ui_thrd_force_update(); } - wtimeout(stdscr, 1000); +// wtimeout(stdscr, 1000); + refresh(); return DOUI_OK; } -- cgit v1.2.3