aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <toni@devlap.local>2015-05-07 12:36:31 +0200
committertoni <toni@devlap.local>2015-05-07 12:36:31 +0200
commit282927c4582d34b9608d04df20d5059e6a63cfe4 (patch)
treea7e9d395f272afa3170d59bec80d123b6f23f900
parent590d34f183c9abc60068a767426edbfec30cda3e (diff)
status window code stub
-rw-r--r--Makefile6
-rw-r--r--main.c9
-rw-r--r--ui.c13
-rw-r--r--ui_nwindow.c70
-rw-r--r--ui_nwindow.h44
-rw-r--r--ui_statusbar.c8
-rw-r--r--ui_statusbar.h8
7 files changed, 148 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index e2ffc96..dac9980 100644
--- a/Makefile
+++ b/Makefile
@@ -1,16 +1,20 @@
CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1
+DBGFLAGS = -g
LDFLAGS ?= $(shell ncurses5-config --libs) -pthread
CC := gcc
INSTALL ?= install
VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.1a"; fi)
BIN = naskpass
-SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui.c main.c
+SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c main.c
all: $(BIN)
$(BIN): $(SOURCES)
$(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN)
+debug:
+ $(MAKE) 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
diff --git a/main.c b/main.c
index 0bcaf39..1eff534 100644
--- a/main.c
+++ b/main.c
@@ -16,6 +16,8 @@
#include "config.h"
+static bool ui_active = true;
+
static void
usage(char *arg0)
{
@@ -113,6 +115,7 @@ main(int argc, char **argv)
if (fifo_path == NULL) fifo_path = strdup(DEFAULT_FIFO);
if (check_fifo(fifo_path) == false) {
+ usage(argv[0]);
exit(EXIT_FAILURE);
}
if ((ffd = open(fifo_path, O_NONBLOCK | O_RDWR)) < 0) {
@@ -123,12 +126,17 @@ main(int argc, char **argv)
if ((child = fork()) == 0) {
/* child */
+ ui_active = true;
do_ui(ffd);
+ ui_active = false;
} else if (child > 0) {
/* parent */
fclose(stdin);
while (input_timeout(ffd, 1) == 0) {
usleep(100000);
+ if (ui_active == true) {
+
+ }
}
stop_ui();
wait(&c_status);
@@ -144,6 +152,7 @@ main(int argc, char **argv)
exit(EXIT_FAILURE);
}
+printf("BLA\n");
close(ffd);
if (crypt_cmd != NULL) free(crypt_cmd);
free(fifo_path);
diff --git a/ui.c b/ui.c
index 1eaf0c0..2f772fc 100644
--- a/ui.c
+++ b/ui.c
@@ -15,6 +15,7 @@
#include "ui_ani.h"
#include "ui_input.h"
#include "ui_statusbar.h"
+#include "ui_nwindow.h"
#include "status.h"
#include "config.h"
@@ -182,6 +183,7 @@ static int
run_ui_thrd(void) {
pthread_mutex_lock(&mtx_busy);
active = true;
+ passwd_from_ui = false;
pthread_cond_signal(&cnd_update);
pthread_mutex_unlock(&mtx_busy);
return (pthread_create(&thrd, NULL, &ui_thrd, NULL));
@@ -223,6 +225,7 @@ process_key(wchar_t key, struct input *a, WINDOW *win)
switch (key) {
case UIKEY_ENTER:
send_passwd(ffd, a->input, a->input_len);
+ passwd_from_ui = true;
retval = false;
break;
case UIKEY_BACKSPACE:
@@ -252,12 +255,19 @@ lower_statusbar_update(WINDOW *win, struct statusbar *bar)
return (0);
}
+static int
+infownd_update(WINDOW *win, struct txtwindow *tw)
+{
+ return (0);
+}
+
int
do_ui(int fifo_fd)
{
struct input *pw_input;
struct anic *heartbeat;
struct statusbar *higher, *lower;
+ struct txtwindow *infownd;
char key = '\0';
char *title;
@@ -272,6 +282,8 @@ do_ui(int fifo_fd)
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(10, 10, 25, 8, COLOR_PAIR(3), infownd_update);
+
register_input(NULL, pw_input);
register_statusbar(higher);
register_statusbar(lower);
@@ -305,6 +317,7 @@ do_ui(int fifo_fd)
free_anic(heartbeat);
free_statusbar(higher);
free_statusbar(lower);
+ free_txtwindow(infownd);
free_ui();
return (DOUI_OK);
error:
diff --git a/ui_nwindow.c b/ui_nwindow.c
new file mode 100644
index 0000000..7098097
--- /dev/null
+++ b/ui_nwindow.c
@@ -0,0 +1,70 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "ui.h"
+#include "ui_nwindow.h"
+
+
+struct txtwindow *
+init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype 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->scrollable = false;
+ a->title_len = INITIAL_TITLE_LEN;
+ a->text_len = INITIAL_TEXT_LEN;
+ a->title = calloc(a->title_len, sizeof(char));
+ a->text = calloc(a->text_len, sizeof(char));
+ a->attrs = attrs;
+ a->window_func = cb_update;
+ return (a);
+}
+
+void
+free_txtwindow(struct txtwindow *a)
+{
+ if (a->text) {
+ free(a->text);
+ }
+ if (a->title) {
+ free(a->title);
+ }
+ free(a);
+}
+
+int
+txtwindow_cb(WINDOW *win, void *data, bool timedout)
+{
+ struct txtwindow *a = (struct txtwindow *) data;
+ return (UICB_OK);
+}
+
+void inline
+register_txtwindow(struct txtwindow *a)
+{
+ register_ui_elt(txtwindow_cb, (void *) a, NULL);
+}
+
+static inline size_t
+__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src)
+{
+ if (sz_src > sz_dest) {
+ *p_dest = (char *) realloc(*p_dest, sz_dest * sizeof(char));
+ }
+ memset(*p_dest, '\0', sz_dest);
+ return sz_dest;
+}
+
+void
+set_txtwindow_text(struct txtwindow *a, const char *text)
+{
+ size_t len = strlen(text);
+
+ if (len > a->text_len) {
+ a->text_len = __do_textcpy(&a->text, a->text_len, text, len);
+ }
+}
diff --git a/ui_nwindow.h b/ui_nwindow.h
new file mode 100644
index 0000000..5a983f7
--- /dev/null
+++ b/ui_nwindow.h
@@ -0,0 +1,44 @@
+#ifndef UI_TXTWINDOW_H
+#define UI_TXTWINDOW_H 1
+
+#include <ncurses.h>
+
+
+#define INITIAL_TITLE_LEN 32
+#define INITIAL_TEXT_LEN 128
+
+struct txtwindow {
+ unsigned int y;
+ unsigned int x;
+ unsigned int width;
+ unsigned int height;
+ bool scrollable;
+ char *title;
+ size_t title_len;
+ char *text;
+ size_t text_len;
+ int (*window_func)(WINDOW *, struct txtwindow *);
+ chtype attrs;
+};
+
+typedef int (*window_func)(WINDOW *, struct txtwindow *);
+
+struct txtwindow *
+init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, window_func cb_update);
+
+void
+free_txtwindow(struct txtwindow *a);
+
+int
+txtwindow_cb(WINDOW *win, void *data, bool timedout);
+
+void
+register_txtwindow(struct txtwindow *a);
+
+void
+set_txtwindow_text(struct txtwindow *a, const char *text);
+
+void
+set_txtwindow_scrollable(struct txtwindow *a, bool scrollable);
+
+#endif
diff --git a/ui_statusbar.c b/ui_statusbar.c
index 792a0ae..8172433 100644
--- a/ui_statusbar.c
+++ b/ui_statusbar.c
@@ -6,7 +6,7 @@
struct statusbar *
-init_statusbar(unsigned int y, unsigned int width, chtype attrs, update_func cb_update)
+init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update)
{
struct statusbar *a = calloc(1, sizeof(struct statusbar));
@@ -14,7 +14,7 @@ init_statusbar(unsigned int y, unsigned int width, chtype attrs, update_func cb_
a->width = width;
a->text = calloc(a->width, sizeof(char));
a->attrs = attrs;
- a->update_func = cb_update;
+ a->status_func = cb_update;
return (a);
}
@@ -37,8 +37,8 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out)
if (a == NULL) return (UICB_ERR_UNDEF);
if (timed_out == true) {
- if (a->update_func != NULL) {
- a->update_func(win, a);
+ if (a->status_func != NULL) {
+ a->status_func(win, a);
}
}
attron(a->attrs);
diff --git a/ui_statusbar.h b/ui_statusbar.h
index 1f7e6b1..5139c14 100644
--- a/ui_statusbar.h
+++ b/ui_statusbar.h
@@ -4,20 +4,18 @@
#include <ncurses.h>
-#define UPDATE_CBDEF int (*update_func)(WINDOW *, struct statusbar *)
-
struct statusbar {
unsigned int y;
unsigned int width;
char *text;
- UPDATE_CBDEF;
+ int (*status_func)(WINDOW *, struct statusbar *);
chtype attrs;
};
-typedef UPDATE_CBDEF;
+typedef int (*status_func)(WINDOW *, struct statusbar *);
struct statusbar *
-init_statusbar(unsigned int y, unsigned int width, chtype attrs, update_func cb_update);
+init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update);
void
free_statusbar(struct statusbar *a);