diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 27 | ||||
-rw-r--r-- | ui.c | 75 | ||||
-rw-r--r-- | ui.h | 12 | ||||
-rw-r--r-- | ui_elements.c | 83 | ||||
-rw-r--r-- | ui_elements.h | 20 | ||||
-rw-r--r-- | ui_nwindow.c | 11 | ||||
-rw-r--r-- | ui_nwindow.h | 12 |
8 files changed, 168 insertions, 73 deletions
@@ -5,3 +5,4 @@ /tests/mqtest /tests/strsep *.swp +*.o @@ -1,18 +1,28 @@ -CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 +CFLAGS = $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 -fPIC DBGFLAGS = -g -LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -lrt -CC ?= gcc -INSTALL ?= install -VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) +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 = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c main.c +SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c ui_elements.c main.c +OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) -all: $(BIN) +all: $(OBJECTS) $(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ $(BIN): $(SOURCES) - $(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all +strip: + $(STRIP) $(BIN) + +release: all strip + debug: $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' $(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' @@ -31,6 +41,7 @@ uninstall: rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass clean: + rm -f $(OBJECTS) rm -f $(BIN) $(MAKE) -C tests clean @@ -15,6 +15,7 @@ #include <signal.h> #include "ui.h" +#include "ui_elements.h" #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" @@ -40,11 +41,11 @@ static unsigned int max_x, max_y; static WINDOW *wnd_main; static struct nask_ui *nui = NULL; +static struct nask_input *nin = 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; static sem_t sem_rdy; static sem_t /* TUI active? */ *sp_ui, /* Textfield input available? */ *sp_input; static mqd_t mq_passwd, mq_info; @@ -75,6 +76,12 @@ register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd) } void +register_input(ui_input_callback ipcb, void *data, WINDOW *wnd) +{ + +} + +void unregister_ui_elt(void *data) { struct nask_ui *cur = nui, *next, *before = NULL; @@ -130,23 +137,21 @@ ui_thrd(void *arg) struct timeval now; struct timespec wait; - pthread_mutex_lock(&mtx_update); gettimeofday(&now, NULL); wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; wait.tv_nsec = now.tv_usec * 1000; do_ui_update(true); sem_post(&sem_rdy); while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { - pthread_mutex_unlock(&mtx_busy); + pthread_mutex_lock(&mtx_update); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + if (--atmout == 0) sem_trywait(sp_ui); + do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; } - pthread_mutex_lock(&mtx_busy); - if (--atmout == 0) sem_trywait(sp_ui); - do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); + pthread_mutex_unlock(&mtx_update); } - pthread_mutex_unlock(&mtx_busy); pthread_mutex_unlock(&mtx_update); return (NULL); } @@ -154,7 +159,9 @@ ui_thrd(void *arg) void ui_thrd_force_update(void) { + pthread_mutex_lock(&mtx_update); pthread_cond_signal(&cnd_update); + pthread_mutex_unlock(&mtx_update); } WINDOW * @@ -225,7 +232,6 @@ process_key(char key, struct input *a, WINDOW *win) break; case UIKEY_ESC: retval = false; - ui_thrd_force_update(); break; case UIKEY_DOWN: case UIKEY_UP: @@ -238,28 +244,10 @@ process_key(char key, struct input *a, WINDOW *win) return (retval); } -static int -lower_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - char *tmp = get_system_stat(); - set_statusbar_text(bar, tmp); - free(tmp); - return (0); -} - -static int -infownd_update(WINDOW *win, struct txtwindow *tw) -{ - return (0); -} - int do_ui(void) { - struct input *pw_input; struct anic *heartbeat; - struct statusbar *higher, *lower; - struct txtwindow *infownd; char key = '\0'; char *title = NULL, mq_msg[IPC_MQSIZ+1]; int i_sval = -1, ret = DOUI_ERR; @@ -273,22 +261,11 @@ do_ui(void) perror("init semaphore/messageq"); goto error; } + + /* init TUI and UI Elements (input field, status bar, etc) */ init_ui(); - 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(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL); - 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); + init_ui_elements(wnd_main, max_x, max_y); - register_input(NULL, pw_input); - register_statusbar(higher); - register_statusbar(lower); - register_anic(heartbeat); - register_txtwindow(infownd); - set_txtwindow_title(infownd, "WARNING"); - set_txtwindow_text(infownd, "String0---------------#\nString1--------------------#\nString2-----#"); - activate_input(wnd_main, pw_input); - set_statusbar_text(higher, title); if (run_ui_thrd() != 0) { goto error; } @@ -301,28 +278,22 @@ do_ui(void) if (key == -1) { continue; } - pthread_mutex_lock(&mtx_busy); 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); do_ui_update(false); - pthread_mutex_unlock(&mtx_busy); } ui_thrd_force_update(); stop_ui_thrd(); - unregister_ui_elt(lower); - unregister_ui_elt(higher); - unregister_ui_elt(heartbeat); - unregister_ui_elt(pw_input); - free_input(pw_input); - free_anic(heartbeat); - free_statusbar(higher); - free_statusbar(lower); - free_txtwindow(infownd); - free_ui(); + free_ui_elements(); + ret = DOUI_OK; mq_close(mq_passwd); mq_close(mq_info); @@ -26,9 +26,16 @@ typedef int (*ui_callback)(WINDOW *, void *, bool); +typedef int (*ui_input_callback)(WINDOW *, void *, int); + + +union ui_type { + ui_callback ui_element; + ui_input_callback ui_input; +}; struct nask_ui { - ui_callback ui_elt_cb; + union ui_type type; WINDOW *wnd; void *data; struct nask_ui *next; @@ -38,6 +45,9 @@ void register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd); void +register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd); + +void unregister_ui_elt(void *data); void diff --git a/ui_elements.c b/ui_elements.c new file mode 100644 index 0000000..b5b56e2 --- /dev/null +++ b/ui_elements.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "ui_elements.h" + + +#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 + +static struct input *pw_input; +static struct anic *heartbeat; +static struct statusbar *higher, *lower; +static struct txtwindow *infownd; +static char *title = NULL; + + +static int +lower_statusbar_update(WINDOW *win, struct statusbar *bar) +{ + char *tmp = get_system_stat(); + set_statusbar_text(bar, tmp); + free(tmp); + return (0); +} + +static int +infownd_update(WINDOW *win, struct txtwindow *tw) +{ + 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); +} + +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)); + heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL); + 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); + infownd->userptr = calloc(4, sizeof(char)); + (*(char*)(infownd->userptr)) = '.'; + + register_input(NULL, pw_input); + register_statusbar(higher); + register_statusbar(lower); + register_anic(heartbeat); + register_txtwindow(infownd); + set_txtwindow_title(infownd, "WARNING"); + activate_input(wnd_main, pw_input); + set_statusbar_text(higher, title); +} + +void +free_ui_elements(void) +{ + unregister_ui_elt(lower); + unregister_ui_elt(higher); + unregister_ui_elt(heartbeat); + unregister_ui_elt(pw_input); + free_input(pw_input); + free_anic(heartbeat); + free_statusbar(higher); + free_statusbar(lower); + free(infownd->userptr); + free_txtwindow(infownd); + free_ui(); +} diff --git a/ui_elements.h b/ui_elements.h new file mode 100644 index 0000000..591e18b --- /dev/null +++ b/ui_elements.h @@ -0,0 +1,20 @@ +#ifndef UI_ELEMENTS_H +#define UI_ELEMENTS_H 1 + +#include "ui.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.h" + +#include "status.h" +#include "config.h" + + +void +init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y); + +void +free_ui_elements(void); + +#endif diff --git a/ui_nwindow.c b/ui_nwindow.c index b21bb29..1b88319 100644 --- a/ui_nwindow.c +++ b/ui_nwindow.c @@ -1,7 +1,6 @@ #include <stdlib.h> #include <string.h> -#include "ui.h" #include "ui_nwindow.h" @@ -14,7 +13,7 @@ init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int a->y = y; a->width = width; a->height = height; - a->scrollable = false; + a->active = false; a->title_len = INITIAL_TITLE_LEN; a->title = calloc(a->title_len+1, sizeof(char)); a->text = NULL; @@ -74,19 +73,21 @@ print_wnd(struct txtwindow *a) 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[i] ) { + while ( a->text && a->text[i] ) { mvprintw(y+i, x, a->text[i]); i++; } attroff(a->text_attrs); } -int +static int txtwindow_cb(WINDOW *win, void *data, bool timedout) { struct txtwindow *a = (struct txtwindow *) data; - print_wnd(a); + if (a->active == true) { + print_wnd(a); + } return (UICB_OK); } diff --git a/ui_nwindow.h b/ui_nwindow.h index ee2b810..198481b 100644 --- a/ui_nwindow.h +++ b/ui_nwindow.h @@ -3,21 +3,25 @@ #include <ncurses.h> +#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 scrollable; + 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 *); @@ -28,9 +32,6 @@ init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int he void free_txtwindow(struct txtwindow *a); -int -txtwindow_cb(WINDOW *win, void *data, bool timedout); - void register_txtwindow(struct txtwindow *a); @@ -38,9 +39,6 @@ void set_txtwindow_text(struct txtwindow *a, char *text); void -set_txtwindow_scrollable(struct txtwindow *a, bool scrollable); - -void set_txtwindow_title(struct txtwindow *a, const char *title); #endif |