]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
. Allow python scripts to watch file handles
authorsopwith <sopwith>
Mon, 2 Apr 2001 15:59:14 +0000 (15:59 +0000)
committersopwith <sopwith>
Mon, 2 Apr 2001 15:59:14 +0000 (15:59 +0000)
. Fix 64-bit warnings in snackmodule
. Misc snack.py cleanups
. Add NEWT_FD_EXCEPT to allow watching for fd exceptions
. In newtExitStruct, return the first file descriptor that an event occurred on for NEWT_EXIT_FDREADY

form.c
newt.h
snack.py
snackmodule.c

diff --git a/form.c b/form.c
index 825fd46bf7de79a30cb18a224fe02a998951d752..6f68886b19d469c2a17134978351d0bbda8cc85d 100644 (file)
--- a/form.c
+++ b/form.c
@@ -884,7 +884,7 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
     struct eventResult er;
     int key, i, max;
     int done = 0;
-    fd_set readSet, writeSet;
+    fd_set readSet, writeSet, exceptSet;
     struct timeval nextTimeout, now, timeout;
 #ifdef USE_GPM
     int x, y;
@@ -914,6 +914,7 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
 
        FD_ZERO(&readSet);
        FD_ZERO(&writeSet);
+       FD_ZERO(&exceptSet);
        FD_SET(0, &readSet);
 #ifdef USE_GPM
        if (gpm_fd > 0) {
@@ -929,6 +930,8 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
                FD_SET(form->fds[i].fd, &readSet);
            if (form->fds[i].flags & NEWT_FD_WRITE)
                FD_SET(form->fds[i].fd, &writeSet);
+           if (form->fds[i].flags & NEWT_FD_EXCEPT)
+               FD_SET(form->fds[i].fd, &exceptSet);
        }
 
        if (form->timer) {
@@ -965,7 +968,7 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
            timeout.tv_sec = timeout.tv_usec = 0;
        }
 
-       i = select(max + 1, &readSet, &writeSet, NULL
+       i = select(max + 1, &readSet, &writeSet, &exceptSet
                        form->timer ? &timeout : NULL);
        if (i < 0) continue;    /* ?? What should we do here? */
 
@@ -1034,6 +1037,19 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
                    }
                }
            } else {
+               for (i = 0; i < form->numFds; i++) {
+                   if (((form->fds[i].flags & NEWT_FD_READ)
+                       && FD_ISSET(form->fds[i].fd, &readSet))
+                       || ((form->fds[i].flags & NEWT_FD_WRITE)
+                       && FD_ISSET(form->fds[i].fd, &writeSet))
+                       || ((form->fds[i].flags & NEWT_FD_EXCEPT)
+                       && FD_ISSET(form->fds[i].fd, &exceptSet))) break;
+               }
+               if(i < form->numFds)
+                   es->u.watch = form->fds[i].fd;
+               else
+                   es->u.watch = -1;
+
                es->reason = NEWT_EXIT_FDREADY;
                done = 1;
            }
diff --git a/newt.h b/newt.h
index afa972bd273279561b195e73dab171b4a866c339..3854240114e0d906c96a0e71be03acef54d06038 100644 (file)
--- a/newt.h
+++ b/newt.h
@@ -75,6 +75,7 @@ enum newtFlagsSense { NEWT_FLAGS_SET, NEWT_FLAGS_RESET, NEWT_FLAGS_TOGGLE };
 #define NEWT_FLAG_PASSWORD      (1 << 11)  /* draw '*'  of chars in entrybox */
 #define NEWT_FD_READ           (1 << 0)
 #define NEWT_FD_WRITE          (1 << 1)
+#define NEWT_FD_EXCEPT         (1 << 2)
 
 #define NEWT_CHECKBOXTREE_COLLAPSED    '\0'
 #define NEWT_CHECKBOXTREE_EXPANDED     '\1'
@@ -207,6 +208,7 @@ struct newtExitStruct {
     enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY,
           NEWT_EXIT_TIMER } reason;
     union {
+       int watch;
        int key;
        newtComponent co;
     } u;
index 9c6efac4b782f5d7866432909461d650bd339024..b8927c839d4ef72298d061d6de34093e3472ed4d 100644 (file)
--- a/snack.py
+++ b/snack.py
@@ -8,13 +8,9 @@ import _snack
 import types
 import string
 
-snackArgs = {}
-snackArgs['append'] = -1
+from _snack import FLAG_DISABLED, FLAGS_SET, FLAGS_RESET, FLAGS_TOGGLE, FD_READ, FD_WRITE, FD_EXCEPT
 
-FLAG_DISABLED = _snack.FLAG_DISABLED
-FLAGS_SET = _snack.FLAGS_SET
-FLAGS_RESET = _snack.FLAGS_RESET
-FLAGS_TOGGLE = _snack.FLAGS_TOGGLE
+snackArgs = {"append":-1}
 
 class Widget:
     def setCallback(self, obj, data = None):
@@ -179,6 +175,8 @@ class Form:
            return self.trans[which]
        elif (what == _snack.FORM_EXIT_TIMER):
            return "TIMER"
+       elif (what == _snack.FORM_EXIT_FDREADY):
+           return self.filemap[which]
 
        return hotkeys[which]
 
@@ -188,6 +186,7 @@ class Form:
 
     def __init__(self, helpArg = None):
        self.trans = {}
+       self.filemap = {}
        self.w = _snack.form(helpArg)
        # we do the reference count for the helpArg in python! gross
        self.helpArg = helpArg
@@ -198,6 +197,10 @@ class Form:
     def setTimer (self, timer):
         self.w.settimer (timer)
 
+    def watchFile (self, file, flags):
+       self.filemap[file.fileno()] = file
+       self.w.watchfd (file.fileno(), flags)
+
 class Grid:
 
     def place(self, x, y):
index 6749119cdbcc3b82bd834973a9d5d1c9c5ed372e..6f84a1af958f7712b16421be779b17b232c7f3a9 100644 (file)
@@ -15,6 +15,9 @@ struct callbackStruct {
     PyObject * cb, * data;
 };
 
+/* Integer to pointer, 64-bit-sane */
+#define I2P(x) ((void *)(long)(x))
+
 static struct callbackStruct suspend;
 static struct callbackStruct helpCallback;
 
@@ -131,6 +134,7 @@ static PyObject * formRun(snackForm * s, PyObject * args);
 static PyObject * formHotKey(snackForm * s, PyObject * args);
 static PyObject * formSetCurrent(snackForm * form, PyObject * args);
 static PyObject * formSetTimer(snackForm * form, PyObject * args);
+static PyObject * formWatchFD(snackForm * form, PyObject * args);
 
 static PyMethodDef formMethods[] = {
     { "add", (PyCFunction) formAdd, METH_VARARGS, NULL },
@@ -139,6 +143,7 @@ static PyMethodDef formMethods[] = {
     { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL },
     { "setcurrent", (PyCFunction) formSetCurrent, METH_VARARGS, NULL },
     { "settimer", (PyCFunction) formSetTimer, METH_VARARGS, NULL },
+    { "watchfd", (PyCFunction) formWatchFD, METH_VARARGS, NULL },
     { NULL }
 };
 
@@ -812,6 +817,8 @@ static PyObject * formRun(snackForm * s, PyObject * args) {
        return Py_BuildValue("(si)", "hotkey", result.u.key);
     else if (result.reason == NEWT_EXIT_TIMER)
        return Py_BuildValue("(si)", "timer", 0);
+    else if (result.reason == NEWT_EXIT_FDREADY)
+       return Py_BuildValue("(si)", "fdready", result.u.watch);
     else
        return Py_BuildValue("(si)", "widget", result.u.co);
 }
@@ -840,6 +847,18 @@ static PyObject * formSetTimer(snackForm * form, PyObject * args) {
     return Py_None;
 }
 
+static PyObject * formWatchFD(snackForm * form, PyObject * args) {
+    int fd, fdflags;
+
+    if (!PyArg_ParseTuple(args, "ii", &fd, &fdflags))
+       return NULL;
+
+    newtFormWatchFd(form->fo, fd, fdflags);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
 static PyObject * formSetCurrent(snackForm * form, PyObject * args) {
     snackWidget * w;
 
@@ -923,7 +942,7 @@ static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) {
     if (!PyArg_ParseTuple(args, "s", &text))
        return NULL;
 
-    newtListboxAddEntry(s->co, text, (void *) s->anint);
+    newtListboxAddEntry(s->co, text, I2P(s->anint));
 
     return PyInt_FromLong(s->anint++);
 }
@@ -935,7 +954,7 @@ static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) {
     if (!PyArg_ParseTuple(args, "si", &text, &key))
        return NULL;
 
-    newtListboxInsertEntry(s->co, text, (void *) s->anint, (void *) key);
+    newtListboxInsertEntry(s->co, text, I2P(s->anint), I2P(key));
 
     return PyInt_FromLong(s->anint++);
 }
@@ -946,7 +965,7 @@ static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) {
     if (!PyArg_ParseTuple(args, "i", &key))
        return NULL;
 
-    newtListboxDeleteEntry(s->co, (void *) key);
+    newtListboxDeleteEntry(s->co, I2P(key));
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -965,7 +984,7 @@ static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) {
     if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
 
-    newtListboxSetCurrentByKey(s->co, (void *) index);
+    newtListboxSetCurrentByKey(s->co, I2P(index));
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -1022,7 +1041,7 @@ static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) {
     }
     path[len] = NEWT_ARG_LAST;
 
-    newtCheckboxTreeAddArray(s->co, text, (void *) s->anint,
+    newtCheckboxTreeAddArray(s->co, text, I2P(s->anint),
                             selected ? NEWT_FLAG_SELECTED : 0, path);
 
     return PyInt_FromLong(s->anint++);
@@ -1032,7 +1051,7 @@ static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args) {
     if (!PyArg_ParseTuple(args, ""))
        return NULL;
 
-    return PyInt_FromLong((int) newtCheckboxTreeGetCurrent(s->co));
+    return PyInt_FromLong((long)newtCheckboxTreeGetCurrent(s->co));
 }
 
 static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) {
@@ -1041,7 +1060,7 @@ static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) {
 
     if (!PyArg_ParseTuple(args, "is", &data, &text)) return NULL;
 
-    newtCheckboxTreeSetEntry(s->co, (void *)data, text);
+    newtCheckboxTreeSetEntry(s->co, I2P(data), text);
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -1052,7 +1071,7 @@ static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args)
 
     if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
 
-    newtCheckboxTreeSetCurrent(s->co, (void *)data);
+    newtCheckboxTreeSetCurrent(s->co, I2P(data));
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -1064,7 +1083,7 @@ static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * ar
 
     if (!PyArg_ParseTuple(args, "i|i", &data, &isOn)) return NULL;
 
-    newtCheckboxTreeSetEntryValue(s->co, (void *)data,
+    newtCheckboxTreeSetEntryValue(s->co, I2P(data),
                                  isOn ? NEWT_CHECKBOXTREE_SELECTED :
                                         NEWT_CHECKBOXTREE_UNSELECTED);
 
@@ -1080,7 +1099,7 @@ static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * ar
 
     if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
 
-    selection = newtCheckboxTreeGetEntryValue(s->co, (void *)data);
+    selection = newtCheckboxTreeGetEntryValue(s->co, I2P(data));
 
     if (selection == -1) return NULL;
 
@@ -1119,7 +1138,7 @@ static PyObject * widgetCheckboxTreeGetSel(snackWidget * s,
 
     sel = PyList_New(0);
     for (i = 0; i < numselected; i++) {
-       PyList_Append(sel, PyInt_FromLong((int) selection[i]));
+       PyList_Append(sel, PyInt_FromLong((long) selection[i]));
     }
     free(selection);
 
@@ -1140,9 +1159,14 @@ void init_snack(void) {
     PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX));
     PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY));
 
+    PyDict_SetItemString(d, "FD_READ", PyInt_FromLong(NEWT_FD_READ));
+    PyDict_SetItemString(d, "FD_WRITE", PyInt_FromLong(NEWT_FD_WRITE));
+    PyDict_SetItemString(d, "FD_EXCEPT", PyInt_FromLong(NEWT_FD_EXCEPT));
+
     PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey"));
     PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget"));
     PyDict_SetItemString(d, "FORM_EXIT_TIMER", PyString_FromString("timer"));
+    PyDict_SetItemString(d, "FORM_EXIT_FDREADY", PyString_FromString("fdready"));
 
     PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1));
     PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2));