aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ui.c33
-rw-r--r--ui.h9
-rw-r--r--ui_ani.c2
-rw-r--r--ui_ani.h1
-rw-r--r--ui_input.c64
-rw-r--r--ui_input.h13
6 files changed, 97 insertions, 25 deletions
diff --git a/ui.c b/ui.c
index ba5b9d5..e321689 100644
--- a/ui.c
+++ b/ui.c
@@ -24,7 +24,7 @@ pthread_mutex_t ncbsy = PTHREAD_MUTEX_INITIALIZER;
void
-register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd, chtype attrs)
+register_ui_elt(ui_callback uicb, ui_callback post_uicb, void *data, WINDOW *wnd, chtype attrs)
{
struct nask_ui *tmp, *new;
@@ -36,6 +36,7 @@ register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd, chtype attrs)
}
new = calloc(1, sizeof(struct nask_ui));
new->ui_elt_cb = uicb;
+ new->postui_elt_cb = post_uicb;
new->do_update = true;
new->wnd = wnd;
new->attrs = attrs;
@@ -89,6 +90,7 @@ do_ui_update(void)
int retval = UICB_OK;
struct nask_ui *cur = nui;
+ /* call all draw callback's */
while (cur != NULL) {
if (cur->ui_elt_cb != NULL) {
attron(cur->attrs);
@@ -100,6 +102,15 @@ do_ui_update(void)
cur = cur->next;
}
refresh();
+ /* call all post draw callback's */
+ while (cur != NULL) {
+ if (cur->postui_elt_cb != NULL) {
+ if (cur->postui_elt_cb(cur->wnd, cur->data, cur->do_update) == UICB_CURSOR) {
+ break;
+ }
+ }
+ cur = cur->next;
+ }
return (retval);
}
@@ -110,6 +121,7 @@ ui_thrd(void *arg)
struct timespec wait;
pthread_mutex_lock(&mtx_update);
+ do_ui_update();
gettimeofday(&now, NULL);
wait.tv_sec = now.tv_sec + 1;
wait.tv_nsec = now.tv_usec * 1000;
@@ -125,6 +137,12 @@ ui_thrd(void *arg)
}
void
+ui_thrd_force_update(void)
+{
+ pthread_cond_signal(&cnd_update);
+}
+
+void
init_ui(void)
{
wnd_main = initscr();
@@ -144,6 +162,7 @@ free_ui(void)
delwin(wnd_main);
endwin();
clear();
+ printf(" \033[2J");
}
int
@@ -161,7 +180,8 @@ stop_ui_thrd(void) {
int
main(int argc, char **argv)
{
- struct input *pw_input = init_input(true, true, "PASSWORD", 128);
+ struct input *pw_input = init_input(1,7,20,"PASSWORD", 128);
+ struct input *c = init_input(3,8,25,"BLABLUBB", 128);
struct anic *heartbeat = init_anic(2,2);
struct anic *a = init_anic(4,4);
struct anic *b = init_anic(6,6);
@@ -171,19 +191,22 @@ main(int argc, char **argv)
init_ui();
register_anic(heartbeat, A_BOLD | COLOR_PAIR(3));
register_anic(a,0); register_anic(b,COLOR_PAIR(1));
- register_input(1, 1, 10, 5, pw_input, COLOR_PAIR(2));
+ register_input(NULL, pw_input, COLOR_PAIR(3));
+ register_input(NULL, c, COLOR_PAIR(3));
if (run_ui_thrd() != 0) {
exit(EXIT_FAILURE);
}
-sleep(5);
+ wgetch(wnd_main);
+ ui_thrd_force_update();
stop_ui_thrd();
unregister_ui_elt(a);
unregister_ui_elt(heartbeat);
unregister_ui_elt(b);
unregister_ui_elt(pw_input);
+ unregister_ui_elt(c);
free_input(pw_input);
free_anic(heartbeat);
- free_anic(a); free_anic(b);
+ free_anic(a); free_anic(b); free_input(c);
free_ui();
return (0);
}
diff --git a/ui.h b/ui.h
index 8202f14..5fac530 100644
--- a/ui.h
+++ b/ui.h
@@ -3,16 +3,18 @@
#include <ncurses.h>
-
#define UICB_OK 0
#define UICB_ERR_UNDEF 1
#define UICB_ERR_NOP 2
#define UICB_ERR_CB 3
+#define UICB_CURSOR 4
+
typedef int (*ui_callback)(WINDOW *, void *, bool);
struct nask_ui {
ui_callback ui_elt_cb;
+ ui_callback postui_elt_cb;
bool do_update;
WINDOW *wnd;
chtype attrs;
@@ -21,7 +23,7 @@ struct nask_ui {
};
void
-register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd, chtype attrs);
+register_ui_elt(ui_callback uicb, ui_callback post_uicb, void *data, WINDOW *wnd, chtype attrs);
void
unregister_ui_elt(void *data);
@@ -30,6 +32,9 @@ void
set_update(void *ptr_data, bool do_update);
void
+ui_thrd_force_update(void);
+
+void
init_ui(void);
void
diff --git a/ui_ani.c b/ui_ani.c
index cdb8713..c7aa61c 100644
--- a/ui_ani.c
+++ b/ui_ani.c
@@ -49,5 +49,5 @@ anic_cb(WINDOW *win, void *data, bool needs_update)
void
register_anic(struct anic *a, chtype attr)
{
- register_ui_elt(anic_cb, (void *) a, NULL, attr);
+ register_ui_elt(anic_cb, NULL, (void *) a, NULL, attr);
}
diff --git a/ui_ani.h b/ui_ani.h
index 20a4b5a..43a98b3 100644
--- a/ui_ani.h
+++ b/ui_ani.h
@@ -3,6 +3,7 @@
#include <ncurses.h>
+
struct anic {
unsigned int x;
unsigned int y;
diff --git a/ui_input.c b/ui_input.c
index 44ce318..af88b3a 100644
--- a/ui_input.c
+++ b/ui_input.c
@@ -1,19 +1,24 @@
#include <stdlib.h>
+#include <string.h>
#include "ui.h"
#include "ui_input.h"
struct input *
-init_input(bool box, bool shadow, char *prompt, size_t input_len)
+init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len)
{
struct input *a = calloc(1, sizeof(struct input));
- a->box = box;
- a->shadow = shadow;
+ a->x = x;
+ a->y = y;
+ a->width = width;
a->input = calloc(input_len+1, sizeof(char));
- a->input_len = input_len;
- a->prompt = prompt;
+ a->input_max = input_len;
+ a->input_len = 0;
+ a->input_pos = 0;
+ a->prompt = strdup(prompt);
+ a->active = false;
return (a);
}
@@ -23,26 +28,59 @@ free_input(struct input *a)
if (a->input != NULL) {
free(a->input);
}
+ free(a->prompt);
free(a);
}
+static void
+print_input(WINDOW *win, struct input *a)
+{
+ char *tmp;
+ int i;
+ size_t p_len = strlen(a->prompt);
+
+ if (win == NULL) {
+ mvprintw(a->y, a->x, a->prompt);
+ tmp = calloc(a->width+1, sizeof(char));
+ for (i = 0; i < a->width; i++) {
+ *(tmp + i) = '_';
+ }
+ mvprintw(a->y, a->x + p_len, tmp);
+ free(tmp);
+ mvprintw(a->y, a->x + p_len, a->input);
+ }
+}
+
+int
+post_input_cb(WINDOW *win, void *data, bool needs_update)
+{
+ struct input *a = (struct input *) data;
+
+ if (a == NULL) return (UICB_ERR_UNDEF);
+ if (a->active == true) {
+ if (win == NULL) {
+ move(a->y, a->x + a->input_pos);
+ } else {
+ wmove(win, a->y, a->x + a->input_pos);
+ }
+ return (UICB_CURSOR);
+ }
+ return (UICB_OK);
+}
+
+
int
input_cb(WINDOW *win, void *data, bool needs_update)
{
struct input *a = (struct input *) data;
if (a == NULL) return (UICB_ERR_UNDEF);
-mvaddch(7,1,'A');
-wrefresh(win);
-refresh();
+ print_input(win, a);
return (UICB_OK);
}
void
-register_input(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct input *a, chtype attr)
+register_input(WINDOW *win, struct input *a, chtype attr)
{
- WINDOW *wnd = newwin(height, width, y, x);
- box(wnd, 0, 0);
-wrefresh(wnd);
- register_ui_elt(input_cb, (void *) a, wnd, attr);
+ register_ui_elt(input_cb, post_input_cb, (void *) a, win, attr);
}
diff --git a/ui_input.h b/ui_input.h
index 2a1699c..b9e816e 100644
--- a/ui_input.h
+++ b/ui_input.h
@@ -3,16 +3,21 @@
#include <ncurses.h>
+
struct input {
- bool box;
- bool shadow;
+ unsigned int x;
+ unsigned int y;
+ unsigned int width;
char *input;
+ size_t input_max;
size_t input_len;
+ size_t input_pos;
char *prompt;
+ bool active;
};
struct input *
-init_input(bool box, bool shadow, char *prompt, size_t input_len);
+init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len);
void
free_input(struct input *a);
@@ -21,6 +26,6 @@ int
input_cb(WINDOW *win, void *data, bool needs_update);
void
-register_input(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct input *a, chtype attr);
+register_input(WINDOW *win, struct input *a, chtype attr);
#endif