aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2014-12-05 01:12:04 +0100
committertoni <matzeton@googlemail.com>2014-12-05 01:12:04 +0100
commit39b290a1a184060111719885b88fce4bcd068be5 (patch)
tree31377da45afe48099dcf6473e20c7105878c740b
parent26f91f28f8d14951df16d1d1a3a17f2d9b0e6a83 (diff)
- preparing major naskpass (UI) upgrade ..
-rw-r--r--ui.c194
-rw-r--r--ui.h14
-rw-r--r--ui_ani.c26
-rw-r--r--ui_ani.h9
4 files changed, 136 insertions, 107 deletions
diff --git a/ui.c b/ui.c
index c8a6e90..935f8dc 100644
--- a/ui.c
+++ b/ui.c
@@ -14,6 +14,7 @@
#include <sys/sysinfo.h>
#include "ui.h"
+#include "ui_ani.h"
#define PKGNAME "nask"
#define LOGLEN 128
@@ -45,123 +46,95 @@ pthread_mutex_t tmretmtx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ncbsy = PTHREAD_MUTEX_INITIALIZER;
-static void getwmaxyx(WINDOW *wnd, int x, int y, size_t len, int *startyp, int *startxp)
+void
+register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd, chtype attrs)
{
- int maxy, maxx;
+ struct nask_ui *tmp, *new;
- getmaxyx(wnd, maxy, maxx);
- *startxp = ( ((int)(maxx/2)) - x - ((int)(len/2)));
- *startyp = ( ((int)(maxy/2)) - y);
-}
-
-static void print_rel_to_wnd(WINDOW *wnd, int x, int y, char *text, size_t addwidth)
-{
- int startx, starty;
- size_t len = strlen(text);
-
- getwmaxyx(wnd, x, y, len+addwidth, &starty, &startx);
-
- pthread_mutex_lock(&ncbsy);
- mvhline(starty-2, startx-2, 0, len+addwidth+3);
- mvhline(starty+2, startx-2, 0, len+addwidth+3);
- mvvline(starty-1, startx-3, 0, 3);
- mvvline(starty-1, startx+len+addwidth+1, 0, 3);
- mvaddch(starty-2, startx-3, ACS_ULCORNER);
- mvaddch(starty+2, startx-3, ACS_LLCORNER);
- mvaddch(starty-2, startx+len+addwidth+1, ACS_URCORNER);
- mvaddch(starty+2, startx+len+addwidth+1, ACS_LRCORNER);
- mvwprintw(wnd, starty, startx, text);
- pthread_mutex_unlock(&ncbsy);
-}
-
-static size_t print_pw_status(WINDOW *wnd, char *text)
-{
- int startx, starty;
- size_t len = strlen(text);
-
- curs_set(0);
- getwmaxyx(wnd, 0, SRELPOSY, len+4, &starty, &startx);
- pthread_mutex_lock(&ncbsy);
- attron(A_BLINK);
- mvwprintw(wnd, starty, startx-2, "< ");
- mvwprintw(wnd, starty, startx+len, " >");
- attroff(A_BLINK);
- attron(A_BOLD | COLOR_PAIR(1));
- mvwprintw(wnd, starty, startx, "%s", text);
- attroff(A_BOLD | COLOR_PAIR(1));
- pthread_mutex_unlock(&ncbsy);
- return (strlen(text));
+ if (nui != NULL) {
+ tmp = nui;
+ while (tmp->next != NULL) {
+ tmp = tmp->next;
+ }
+ }
+ new = calloc(1, sizeof(struct nask_ui));
+ new->ui_elt_cb = uicb;
+ new->do_update = true;
+ new->wnd = wnd;
+ new->attrs = attrs;
+ new->data = data;
+ new->next = NULL;
+ if (nui == NULL) {
+ nui = new;
+ nui->next = NULL;
+ } else {
+ tmp->next = new;
+ }
}
-static void clear_pw_status(WINDOW *wnd, size_t len)
+void
+unregister_ui_elt(void *data)
{
- int startx, starty, curx, cury;
- char buf[len+5];
- if (len <= 0) return;
-
- attroff(A_BLINK | A_BOLD | COLOR_PAIR(1));
- memset(buf, ' ', len+4);
- buf[len+4] = '\0';
-
- pthread_mutex_lock(&ncbsy);
- getyx(wnd, cury, curx);
- getwmaxyx(wnd, 0, SRELPOSY, len+4, &starty, &startx);
- mvwprintw(wnd, starty, startx-2, "%s", buf);
- wmove(wnd, cury, curx);
- curs_set(1);
- pthread_mutex_unlock(&ncbsy);
+ struct nask_ui *cur = nui, *next, *before = NULL;
+
+ 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;
+ }
}
-void register_ui_elt(ui_callback uicb, void *data)
+static int
+do_ui_update(void)
{
- struct nask_ui *tmp;
-
- if (nui == NULL) {
- nui = calloc(1, sizeof(struct nask_ui));
- nui->next = NULL;
- }
- tmp = nui;
- while (tmp->next != NULL) {
- tmp = tmp->next;
+ int retval = UICB_OK;
+ struct nask_ui *cur = nui;
+
+ while (cur != NULL) {
+ if (cur->ui_elt_cb != NULL) {
+ attron(cur->attrs);
+ cur->ui_elt_cb(cur->wnd, cur->data, cur->do_update);
+ attroff(cur->attrs);
+ } else {
+ retval = UICB_ERR_CB;
+ }
+ cur = cur->next;
}
- tmp->next = calloc(1, sizeof(struct nask_ui));
- tmp->next->ui_elt_cb = uicb;
- tmp->next->do_update = true;
- tmp->next->data = data;
- tmp->next->next = NULL;
+ refresh();
+ return (retval);
}
-static void *ui_thrd(void *arg) {
+static void *
+ui_thrd(void *arg)
+{
struct timeval now;
struct timespec wait;
pthread_mutex_lock(&mtx_update);
gettimeofday(&now, NULL);
wait.tv_sec = now.tv_sec + 1;
- wait.tv_nsec = now.tv_usec;
+ wait.tv_nsec = now.tv_usec * 1000;
+ do_ui_update();
while (active == true) {
pthread_cond_timedwait(&cnd_update, &mtx_update, &wait);
+ wait.tv_sec += 1;
if (active == false) break;
+ do_ui_update();
}
pthread_mutex_unlock(&mtx_update);
return (NULL);
}
-int do_update(void) {
-
-}
-
-int run_ui_thrd(void) {
- active = true;
- return (pthread_create(&thrd, NULL, &ui_thrd, NULL));
-}
-
-int stop_ui_thrd(void) {
- active = false;
- return (pthread_join(thrd, NULL));
-}
-
-void init_ui(void)
+static void
+init_ui(void)
{
wnd_main = initscr();
start_color();
@@ -174,21 +147,48 @@ void init_ui(void)
cbreak();
}
-void free_ui(void)
+static void
+free_ui(void)
{
+ delwin(wnd_main);
endwin();
clear();
}
-int main(int argc, char **argv)
-{
+int
+run_ui_thrd(void) {
init_ui();
+ active = true;
+ return (pthread_create(&thrd, NULL, &ui_thrd, NULL));
+}
+int
+stop_ui_thrd(void) {
+ active = false;
+ free_ui();
+ return (pthread_join(thrd, NULL));
+}
+
+int
+main(int argc, char **argv)
+{
+ struct anic *heartbeat = init_anic(2,2);
+ struct anic *a = init_anic(4,4);
+ struct anic *b = init_anic(6,6);
+ a->state = '-';
+ b->state = '\\';
+
+ register_anic(heartbeat, A_BOLD | COLOR_PAIR(3));
+ register_anic(a,0); register_anic(b,COLOR_PAIR(1));
if (run_ui_thrd() != 0) {
exit(EXIT_FAILURE);
}
+sleep(5);
stop_ui_thrd();
-
- free_ui();
+ unregister_ui_elt(a);
+ unregister_ui_elt(heartbeat);
+ unregister_ui_elt(b);
+ free_anic(heartbeat);
+ free_anic(a); free_anic(b);
return (0);
}
diff --git a/ui.h b/ui.h
index ff8e144..90b10cd 100644
--- a/ui.h
+++ b/ui.h
@@ -6,20 +6,30 @@
#define UICB_OK 0
#define UICB_ERR_UNDEF 1
+#define UICB_ERR_NOP 2
+#define UICB_ERR_CB 3
-typedef int (*ui_callback)(WINDOW *, void *);
+typedef int (*ui_callback)(WINDOW *, void *, bool);
struct nask_ui {
ui_callback ui_elt_cb;
bool do_update;
+ WINDOW *wnd;
+ chtype attrs;
void *data;
struct nask_ui *next;
};
void
-register_ui_elt(ui_callback uicb, void *data);
+register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd, chtype attrs);
void
unregister_ui_elt(void *data);
+int
+run_ui_thrd(void);
+
+int
+stop_ui_thrd(void);
+
#endif
diff --git a/ui_ani.c b/ui_ani.c
index 40a07e9..cdb8713 100644
--- a/ui_ani.c
+++ b/ui_ani.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
+#include "ui.h"
#include "ui_ani.h"
#define ANIC_INITSTATE '\0'
@@ -23,15 +24,30 @@ free_anic(struct anic *a)
}
int
-anic_cb(WINDOW *win, void *data)
+anic_cb(WINDOW *win, void *data, bool needs_update)
{
struct anic *a = (struct anic *) data;
+ if (a == NULL) return (UICB_ERR_UNDEF);
switch (a->state) {
default:
- case '|': return ('/');
- case '/': return ('-');
- case '-': return ('\\');
- case '\\': return ('|');
+ case '|': a->state = '/'; break;
+ case '/': a->state = '-'; break;
+ case '-': a->state = '\\'; break;
+ case '\\': a->state = '|'; break;
}
+ if (needs_update == true) {
+ if (win != NULL) {
+ mvwaddch(win, a->y, a->x, a->state);
+ } else {
+ mvaddch(a->y, a->x, a->state);
+ }
+ } else return (UICB_ERR_NOP);
+ return (UICB_OK);
+}
+
+void
+register_anic(struct anic *a, chtype attr)
+{
+ register_ui_elt(anic_cb, (void *) a, NULL, attr);
}
diff --git a/ui_ani.h b/ui_ani.h
index 41b48eb..20a4b5a 100644
--- a/ui_ani.h
+++ b/ui_ani.h
@@ -1,5 +1,5 @@
-#ifndef UI_H
-#define UI_H 1
+#ifndef UI_ANIC_H
+#define UI_ANIC_H 1
#include <ncurses.h>
@@ -16,6 +16,9 @@ void
free_anic(struct anic *a);
int
-anic_cb(WINDOW *win, void *data);
+anic_cb(WINDOW *win, void *data, bool needs_update);
+
+void
+register_anic(struct anic *a, chtype attr);
#endif