aboutsummaryrefslogtreecommitdiff
path: root/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'ui.c')
-rw-r--r--ui.c209
1 files changed, 109 insertions, 100 deletions
diff --git a/ui.c b/ui.c
index e5f4454..dc16b96 100644
--- a/ui.c
+++ b/ui.c
@@ -15,6 +15,7 @@
#include <signal.h>
#include "ui.h"
+#include "ui_ipc.h"
#include "ui_elements.h"
#include "ui_ani.h"
#include "ui_input.h"
@@ -40,52 +41,83 @@
static unsigned int max_x, max_y;
static WINDOW *wnd_main;
-static struct nask_ui *nui = NULL;
-static struct nask_input *nin = NULL;
+static struct nask_ui /* simple linked list to all GUI objects */ *nui = NULL,
+ /* simple linked list to all INPUT objects */ *nin = 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 sem_t sem_rdy;
-static sem_t /* TUI active? */ *sp_ui, /* Textfield input available? */ *sp_input;
-static mqd_t mq_passwd, mq_info;
-void
-register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd)
+static void
+register_basic(enum ui_type utype, union ui_data uicb, void *data, WINDOW *wnd)
{
- struct nask_ui *tmp, *new;
+ struct nask_ui *tmp, *new, *ui = NULL;
- if (nui != NULL) {
- tmp = nui;
- while (tmp->next != NULL) {
- tmp = tmp->next;
- }
+ switch (utype) {
+ case UI_ELEMENT:
+ ui = nui;
+ break;
+ case UI_INPUT:
+ ui = nin;
+ break;
}
new = calloc(1, sizeof(struct nask_ui));
- new->ui_elt_cb = uicb;
+ new->type = utype;
+ new->callback = uicb;
new->wnd = wnd;
new->data = data;
new->next = NULL;
- if (nui == NULL) {
- nui = new;
- nui->next = NULL;
+ if (ui == NULL) {
+ ui = new;
+ ui->next = NULL;
+ switch (utype) {
+ case UI_ELEMENT:
+ nui = ui;
+ break;
+ case UI_INPUT:
+ nin = ui;
+ break;
+ }
} else {
+ tmp = ui;
+ while (tmp->next != NULL) {
+ tmp = tmp->next;
+ }
tmp->next = new;
}
}
void
-register_input(ui_input_callback ipcb, void *data, WINDOW *wnd)
+register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd)
{
-
+ union ui_data cb;
+ cb.ui_element = uicb;
+ register_basic(UI_ELEMENT, cb, data, wnd);
}
void
-unregister_ui_elt(void *data)
+register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd)
+{
+ union ui_data cb;
+ cb.ui_input = ipcb;
+ register_basic(UI_INPUT, cb, data, wnd);
+}
+
+static void
+unregister_basic(enum ui_type utype, void *data)
{
- struct nask_ui *cur = nui, *next, *before = NULL;
+ struct nask_ui *cur, *next, *before = NULL, **ui = NULL;
+ switch (utype) {
+ case UI_ELEMENT:
+ ui = &nui;
+ break;
+ case UI_INPUT:
+ ui = &nin;
+ break;
+ }
+ cur = *ui;
while (cur != NULL) {
next = cur->next;
if (cur->data != NULL && cur->data == data) {
@@ -93,7 +125,7 @@ unregister_ui_elt(void *data)
if (before != NULL) {
before->next = next;
} else {
- nui = next;
+ *ui = next;
}
}
before = cur;
@@ -101,6 +133,46 @@ unregister_ui_elt(void *data)
}
}
+void
+unregister_ui_elt(void *data)
+{
+ unregister_basic(UI_ELEMENT, data);
+}
+
+void
+unregister_ui_input(void *data)
+{
+ unregister_basic(UI_INPUT, data);
+}
+
+int
+activate_ui_input(void *data)
+{
+ struct nask_ui *cur = nin;
+
+ if (cur == NULL || data == NULL) return DOUI_NINIT;
+ while ( cur != NULL ) {
+ if ( cur == data && cur->type == UI_INPUT ) {
+ if ( cur->callback.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) {
+ active = cur;
+ return DOUI_OK;
+ }
+ }
+ cur = cur->next;
+ }
+ return DOUI_ERR;
+}
+
+static bool
+process_key(char key)
+{
+ atmout = APP_TIMEOUT;
+ if ( active != NULL ) {
+ return ( active->callback.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false );
+ }
+ return false;
+}
+
static int
do_ui_update(bool timed_out)
{
@@ -112,8 +184,8 @@ do_ui_update(bool timed_out)
/* call all draw callback's */
erase();
while (cur != NULL) {
- if (cur->ui_elt_cb != NULL) {
- cur->ui_elt_cb(cur->wnd, cur->data, timed_out);
+ if (cur->type == UI_ELEMENT && cur->callback.ui_element != NULL) {
+ cur->callback.ui_element(cur->wnd, cur->data, timed_out);
doupdate();
} else {
retval = UICB_ERR_CB;
@@ -133,7 +205,7 @@ do_ui_update(bool timed_out)
static void *
ui_thrd(void *arg)
{
- int cnd_ret, i_sval;
+ int cnd_ret;
struct timeval now;
struct timespec wait;
@@ -141,11 +213,11 @@ ui_thrd(void *arg)
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 ) {
+ ui_ipc_sempost(SEM_RD);
+ while ( ui_ipc_getvalue(SEM_UI) > 0 ) {
pthread_mutex_lock(&mtx_update);
cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait);
- if (--atmout == 0) sem_trywait(sp_ui);
+ 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;
@@ -202,66 +274,14 @@ stop_ui_thrd(void)
return (pthread_join(thrd, NULL));
}
-static int
-mq_passwd_send(char *passwd, size_t len)
-{
- struct mq_attr m_attr;
-
- sem_post(sp_input);
- if (mq_send(mq_passwd, passwd, len, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) {
- return m_attr.mq_curmsgs;
- }
- memset(passwd, '\0', len);
- return -1;
-}
-
-static bool
-process_key(char key, struct input *a, WINDOW *win)
-{
- bool retval = true;
-
- atmout = APP_TIMEOUT;
- switch (key) {
- case UIKEY_ENTER:
- if ( mq_passwd_send(a->input, a->input_len) > 0 ) {
- retval = false;
- } else retval = true;
- break;
- case UIKEY_BACKSPACE:
- del_input(win, a);
- break;
- case UIKEY_ESC:
- retval = false;
- break;
- case UIKEY_DOWN:
- case UIKEY_UP:
- case UIKEY_LEFT:
- case UIKEY_RIGHT:
- break;
- default:
- add_input(win, a, key);
- }
- return (retval);
-}
-
int
do_ui(void)
{
- struct anic *heartbeat;
char key = '\0';
- char *title = NULL, mq_msg[IPC_MQSIZ+1];
- int i_sval = -1, ret = DOUI_ERR;
+ char *title = NULL;
+ int ret = DOUI_ERR;
asprintf(&title, "/* %s-%s */", PKGNAME, VERSION);
- sp_ui = sem_open(SEM_GUI, 0, 0, 0);
- sp_input = sem_open(SEM_INP, 0, 0, 0);
- mq_passwd = mq_open(MSQ_PWD, O_WRONLY, 0, NULL);
- mq_info = mq_open(MSQ_INF, O_RDONLY, 0, NULL);
- if ( sem_init(&sem_rdy, 0, 0) == -1 || !sp_ui || !sp_input || mq_passwd == (mqd_t)-1 || mq_info == (mqd_t)-1 ) {
- perror("init semaphore/messageq");
- goto error;
- }
-
/* init TUI and UI Elements (input field, status bar, etc) */
init_ui();
init_ui_elements(wnd_main, max_x, max_y);
@@ -269,25 +289,20 @@ do_ui(void)
if (run_ui_thrd() != 0) {
goto error;
}
- sem_wait(&sem_rdy);
- wtimeout(wnd_main, 1000);
- while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) {
+ ui_ipc_semwait(SEM_RD);
+ wtimeout(wnd_main, 10);
+ while ( ui_ipc_getvalue(SEM_UI) > 0 ) {
if ((key = wgetch(wnd_main)) == '\0') {
break;
}
if (key == -1) {
continue;
}
- 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);
+ if ( process_key(key) != true ) {
+printf("BLABL\n");
+ break;
}
- activate_input(wnd_main, pw_input);
+printf("BLUBB\n");
do_ui_update(false);
}
ui_thrd_force_update();
@@ -295,15 +310,9 @@ do_ui(void)
free_ui_elements();
ret = DOUI_OK;
- mq_close(mq_passwd);
- mq_close(mq_info);
error:
if (title) free(title);
- if (sp_ui) sem_close(sp_ui);
- if (sp_input) sem_close(sp_input);
title = NULL;
- sp_ui = NULL;
- sp_input = NULL;
return ret;
}