From: ewt Date: Tue, 28 Apr 1998 06:28:08 +0000 (+0000) Subject: added watchfd stuff X-Git-Tag: r0-24~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d02ffb497cffa3478689bd5f6ac1f67e0f87e411;p=thirdparty%2Fnewt.git added watchfd stuff --- diff --git a/form.c b/form.c index 9a62fe8..c9b1aa0 100644 --- a/form.c +++ b/form.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "newt.h" #include "newt_pr.h" @@ -19,6 +20,11 @@ struct element { newtComponent co; /* into actual through vertOffset */ }; +struct fdInfo { + int fd; + int flags; +}; + struct form { int numCompsAlloced; struct element * elements; @@ -34,6 +40,9 @@ struct form { int numHotKeys; int background; int beenSet; + int numFds; + struct fdInfo * fds; + int maxFd; }; static void gotoComponent(struct form * form, int newComp); @@ -85,6 +94,9 @@ newtComponent newtForm(newtComponent vertBar, const char * help, int flags) { form->vertOffset = 0; form->fixedHeight = 0; form->numRows = 0; + form->numFds = 0; + form->maxFd = 0; + form->fds = NULL; form->beenSet = 0; form->elements = malloc(sizeof(*(form->elements)) * form->numCompsAlloced); @@ -474,6 +486,7 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) { struct eventResult er; int key, i; int done = 0; + fd_set readSet, writeSet; newtFormSetSize(co); /* draw all of the components */ @@ -486,33 +499,52 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) { while (!done) { newtRefresh(); - key = newtGetKey(); - if (key == NEWT_KEY_RESIZE) { - /* newtResizeScreen(1); */ - continue; + FD_ZERO(&readSet); + FD_ZERO(&writeSet); + FD_SET(0, &readSet); + for (i = 0; i < form->numFds; i++) { + if (form->fds[i].flags & NEWT_FD_READ) + FD_SET(form->fds[i].fd, &readSet); + if (form->fds[i].flags & NEWT_FD_WRITE) + FD_SET(form->fds[i].fd, &writeSet); } - for (i = 0; i < form->numHotKeys; i++) { - if (form->hotKeys[i] == key) { - es->reason = NEWT_EXIT_HOTKEY; - es->u.key = key; - done = 1; - break; + i = select(form->maxFd + 1, &readSet, &writeSet, NULL, NULL); + if (i < 0) continue; /* ?? What should we do here? */ + + if (FD_ISSET(0, &readSet)) { + key = newtGetKey(); + + if (key == NEWT_KEY_RESIZE) { + /* newtResizeScreen(1); */ + continue; } - } - if (!done) { - ev.event = EV_KEYPRESS; - ev.u.key = key; - - er = sendEvent(co, ev); - - if (er.result == ER_EXITFORM) { - done = 1; - es->reason = NEWT_EXIT_COMPONENT; - es->u.co = form->exitComp; - } + for (i = 0; i < form->numHotKeys; i++) { + if (form->hotKeys[i] == key) { + es->reason = NEWT_EXIT_HOTKEY; + es->u.key = key; + done = 1; + break; + } + } + + if (!done) { + ev.event = EV_KEYPRESS; + ev.u.key = key; + + er = sendEvent(co, ev); + + if (er.result == ER_EXITFORM) { + done = 1; + es->reason = NEWT_EXIT_COMPONENT; + es->u.co = form->exitComp; + } + } + } else { + es->reason = NEWT_EXIT_FDREADY; + done = 1; } } @@ -569,3 +601,12 @@ void newtFormSetBackground(newtComponent co, int color) { form->background = color; } + +void newtFormWatchFd(newtComponent co, int fd, int fdFlags) { + struct form * form = co->data; + + form->fds = realloc(form->fds, (form->numFds + 1) * sizeof(*form->fds)); + form->fds[form->numFds].fd = fd; + form->fds[form->numFds++].flags = fdFlags; + if (form->maxFd < fd) form->maxFd = fd; +} diff --git a/newt.h b/newt.h index 12dbb2f..8c5ae46 100644 --- a/newt.h +++ b/newt.h @@ -80,6 +80,9 @@ enum newtFlagsSense { NEWT_FLAGS_SET, NEWT_FLAGS_RESET, NEWT_FLAGS_TOGGLE }; #define NEWT_TEXTBOX_SCROLL NEWT_FLAG_SCROLL #define NEWT_FORM_NOF12 NEWT_FLAG_NOF12 +#define NEWT_FD_READ (1 << 0) +#define NEWT_FD_WRITE (1 << 1) + typedef struct newtComponent_struct * newtComponent; extern const struct newtColors newtDefaultColorPalette; @@ -116,6 +119,7 @@ newtComponent newtCompactButton(int left, int top, const char * text); newtComponent newtButton(int left, int top, const char * text); newtComponent newtCheckbox(int left, int top, const char * text, char defValue, const char * seq, char * result); +char newtCheckboxGetValue(newtComponent co); newtComponent newtRadiobutton(int left, int top, const char * text, int isDefault, newtComponent prevButton); newtComponent newtRadioGetCurrent(newtComponent setMember); @@ -163,7 +167,7 @@ char * newtReflowText(char * text, int width, int flexDown, int flexUp, int * actualWidth, int * actualHeight); struct newtExitStruct { - enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT } reason; + enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY } reason; union { int key; newtComponent co; @@ -171,6 +175,7 @@ struct newtExitStruct { } ; newtComponent newtForm(newtComponent vertBar, const char * help, int flags); +void newtFormWatchFd(newtComponent form, int fd, int fdFlags); void newtFormSetSize(newtComponent co); newtComponent newtFormGetCurrent(newtComponent co); void newtFormSetBackground(newtComponent co, int color); @@ -184,10 +189,13 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es); void newtDrawForm(newtComponent form); void newtFormAddHotKey(newtComponent co, int key); +typedef int (*newtEntryFilter)(newtComponent entry, void * data, int ch, + int cursor); newtComponent newtEntry(int left, int top, const char * initialValue, int width, char ** resultPtr, int flags); void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd); -void newtEntrySetFlags(newtComponent co, int flags, enum newtFlagsSense sense); +void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data); +char * newtEntryGetValue(newtComponent co); newtComponent newtScale(int left, int top, int width, long long fullValue); void newtScaleSet(newtComponent co, unsigned long long amount);