| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a50ba8 commit 7bf1564
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,24 @@ PERFORMANCE OF THIS SOFTWARE. | |||
| 83 | 83 | #define HAVE_CREATEFILEHANDLER | |
| 84 | 84 | #endif | |
| 85 | 85 | ||
| 86 | + #ifdef MS_WINDOWS | ||
| 87 | + #define FHANDLETYPE TCL_WIN_SOCKET | ||
| 88 | + #else | ||
| 89 | + #define FHANDLETYPE TCL_UNIX_FD | ||
| 90 | + #endif | ||
| 91 | + | ||
| 92 | + #if TKMAJORMINOR < 8000 | ||
| 93 | + #define FHANDLE Tcl_File | ||
| 94 | + #define MAKEFHANDLE(fd) Tcl_GetFile((ClientData)(fd), FHANDLETYPE) | ||
| 95 | + #else | ||
| 96 | + #define FHANDLE int | ||
| 97 | + #define MAKEFHANDLE(fd) (fd) | ||
| 98 | + #endif | ||
| 99 | + | ||
| 100 | + #if defined(HAVE_CREATEFILEHANDLER) && !defined(MS_WINDOWS) | ||
| 101 | + #define WAIT_FOR_STDIN | ||
| 102 | + #endif | ||
| 103 | + | ||
| 86 | 104 | extern int Tk_GetNumMainWindows(); | |
| 87 | 105 | ||
| 88 | 106 | #ifdef macintosh | |
@@ -342,6 +360,10 @@ Tcl_AppInit(interp) | |||
| 342 | 360 | /* Initialize the Tk application; see the `main' function in | |
| 343 | 361 | * `tkMain.c'. | |
| 344 | 362 | */ | |
| 363 | + | ||
| 364 | + static void EnableEventHook(); /* Forward */ | ||
| 365 | + static void DisableEventHook(); /* Forward */ | ||
| 366 | + | ||
| 345 | 367 | static TkappObject * | |
| 346 | 368 | Tkapp_New(screenName, baseName, className, interactive) | |
| 347 | 369 | char *screenName; | |
@@ -392,6 +414,8 @@ Tkapp_New(screenName, baseName, className, interactive) | |||
| 392 | 414 | if (Tcl_AppInit(v->interp) != TCL_OK) | |
| 393 | 415 | return (TkappObject *)Tkinter_Error(v); | |
| 394 | 416 | ||
| 417 | + EnableEventHook(); | ||
| 418 | + | ||
| 395 | 419 | return v; | |
| 396 | 420 | } | |
| 397 | 421 | ||
@@ -1128,9 +1152,7 @@ Tkapp_CreateFileHandler(self, args) | |||
| 1128 | 1152 | PyObject *file, *func, *data; | |
| 1129 | 1153 | PyObject *idkey; | |
| 1130 | 1154 | int mask, id; | |
| 1131 | - #if TKMAJORMINOR < 8000 | ||
| 1132 | - Tcl_File tfile; | ||
| 1133 | - #endif | ||
| 1155 | + FHANDLE tfile; | ||
| 1134 | 1156 | ||
| 1135 | 1157 | if (!Tkapp_ClientDataDict) { | |
| 1136 | 1158 | if (!(Tkapp_ClientDataDict = PyDict_New())) | |
@@ -1159,18 +1181,9 @@ Tkapp_CreateFileHandler(self, args) | |||
| 1159 | 1181 | } | |
| 1160 | 1182 | Py_DECREF(idkey); | |
| 1161 | 1183 | ||
| 1162 | - #if TKMAJORMINOR < 8000 | ||
| 1163 | - #ifdef MS_WINDOWS | ||
| 1164 | - /* We assume this is a socket... */ | ||
| 1165 | - tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); | ||
| 1166 | - #else /* !MS_WINDOWS */ | ||
| 1167 | - tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); | ||
| 1168 | - #endif /* !MS_WINDOWS */ | ||
| 1184 | + tfile = MAKEFHANDLE(id); | ||
| 1169 | 1185 | /* Ought to check for null Tcl_File object... */ | |
| 1170 | 1186 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); | |
| 1171 | - #else /* >= 8000 */ | ||
| 1172 | - Tcl_CreateFileHandler(id, mask, FileHandler, (ClientData) data); | ||
| 1173 | - #endif /* >= 8000 */ | ||
| 1174 | 1187 | /* XXX fileHandlerDict */ | |
| 1175 | 1188 | Py_INCREF(Py_None); | |
| 1176 | 1189 | return Py_None; | |
@@ -1186,9 +1199,7 @@ Tkapp_DeleteFileHandler(self, args) | |||
| 1186 | 1199 | PyObject *idkey; | |
| 1187 | 1200 | PyObject *data; | |
| 1188 | 1201 | int id; | |
| 1189 | - #if TKMAJORMINOR < 8000 | ||
| 1190 | - Tcl_File tfile; | ||
| 1191 | - #endif | ||
| 1202 | + FHANDLE tfile; | ||
| 1192 | 1203 | ||
| 1193 | 1204 | if (!PyArg_ParseTuple(args, "O", &file)) | |
| 1194 | 1205 | return NULL; | |
@@ -1207,18 +1218,9 @@ Tkapp_DeleteFileHandler(self, args) | |||
| 1207 | 1218 | PyDict_DelItem(Tkapp_ClientDataDict, idkey); | |
| 1208 | 1219 | Py_DECREF(idkey); | |
| 1209 | 1220 | ||
| 1210 | - #if TKMAJORMINOR < 8000 | ||
| 1211 | - #ifdef MS_WINDOWS | ||
| 1212 | - /* We assume this is a socket... */ | ||
| 1213 | - tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET); | ||
| 1214 | - #else | ||
| 1215 | - tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD); | ||
| 1216 | - #endif | ||
| 1221 | + tfile = MAKEFHANDLE(id); | ||
| 1217 | 1222 | /* Ought to check for null Tcl_File object... */ | |
| 1218 | 1223 | Tcl_DeleteFileHandler(tfile); | |
| 1219 | - #else /* >= 8000 */ | ||
| 1220 | - Tcl_DeleteFileHandler(id); | ||
| 1221 | - #endif /* >= 8000 */ | ||
| 1222 | 1224 | /* XXX fileHandlerDict */ | |
| 1223 | 1225 | Py_INCREF(Py_None); | |
| 1224 | 1226 | return Py_None; | |
@@ -1511,6 +1513,7 @@ Tkapp_Dealloc(self) | |||
| 1511 | 1513 | { | |
| 1512 | 1514 | Tcl_DeleteInterp(Tkapp_Interp(self)); | |
| 1513 | 1515 | PyMem_DEL(self); | |
| 1516 | + DisableEventHook(); | ||
| 1514 | 1517 | } | |
| 1515 | 1518 | ||
| 1516 | 1519 | static PyObject * | |
@@ -1584,22 +1587,41 @@ static PyMethodDef moduleMethods[] = | |||
| 1584 | 1587 | {NULL, NULL} | |
| 1585 | 1588 | }; | |
| 1586 | 1589 | ||
| 1590 | + #ifdef WAIT_FOR_STDIN | ||
| 1591 | + #define WAITFLAG 0 | ||
| 1592 | + | ||
| 1593 | + static int stdin_ready = 0; | ||
| 1594 | + | ||
| 1595 | + static void | ||
| 1596 | + MyFileProc(clientData, mask) | ||
| 1597 | + void *clientData; | ||
| 1598 | + int mask; | ||
| 1599 | + { | ||
| 1600 | + stdin_ready = 1; | ||
| 1601 | + } | ||
| 1602 | + #else | ||
| 1603 | + #define WAITFLAG TCL_DONT_WAIT | ||
| 1604 | + #endif | ||
| 1605 | + | ||
| 1587 | 1606 | static PyInterpreterState *event_interp = NULL; | |
| 1588 | 1607 | ||
| 1589 | 1608 | static int | |
| 1590 | 1609 | EventHook() | |
| 1591 | 1610 | { | |
| 1592 | 1611 | PyThreadState *tstate, *save_tstate; | |
| 1612 | + #ifdef WAIT_FOR_STDIN | ||
| 1613 | + FHANDLE tfile = MAKEFHANDLE(((int)fileno(stdin))); | ||
| 1593 | 1614 | ||
| 1594 | - if (Tk_GetNumMainWindows() == 0) | ||
| 1595 | - return 0; | ||
| 1596 | - if (event_interp == NULL) | ||
| 1597 | - return 0; | ||
| 1615 | + stdin_ready = 0; | ||
| 1616 | + Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); | ||
| 1617 | + #endif | ||
| 1598 | 1618 | tstate = PyThreadState_New(event_interp); | |
| 1599 | 1619 | save_tstate = PyThreadState_Swap(NULL); | |
| 1600 | 1620 | PyEval_RestoreThread(tstate); | |
| 1601 | - if (!errorInCmd) | ||
| 1602 | - Tcl_DoOneEvent(TCL_DONT_WAIT); | ||
| 1621 | + #ifdef WAIT_FOR_STDIN | ||
| 1622 | + while (!errorInCmd && !stdin_ready) | ||
| 1623 | + #endif | ||
| 1624 | + Tcl_DoOneEvent(WAITFLAG); | ||
| 1603 | 1625 | if (errorInCmd) { | |
| 1604 | 1626 | errorInCmd = 0; | |
| 1605 | 1627 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); | |
@@ -1610,9 +1632,29 @@ EventHook() | |||
| 1610 | 1632 | PyEval_SaveThread(); | |
| 1611 | 1633 | PyThreadState_Swap(save_tstate); | |
| 1612 | 1634 | PyThreadState_Delete(tstate); | |
| 1635 | + #ifdef WAIT_FOR_STDIN | ||
| 1636 | + Tcl_DeleteFileHandler(tfile); | ||
| 1637 | + #endif | ||
| 1613 | 1638 | return 0; | |
| 1614 | 1639 | } | |
| 1615 | 1640 | ||
| 1641 | + static void | ||
| 1642 | + EnableEventHook() | ||
| 1643 | + { | ||
| 1644 | + if (PyOS_InputHook == NULL) { | ||
| 1645 | + event_interp = PyThreadState_Get()->interp; | ||
| 1646 | + PyOS_InputHook = EventHook; | ||
| 1647 | + } | ||
| 1648 | + } | ||
| 1649 | + | ||
| 1650 | + static void | ||
| 1651 | + DisableEventHook() | ||
| 1652 | + { | ||
| 1653 | + if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { | ||
| 1654 | + PyOS_InputHook = NULL; | ||
| 1655 | + } | ||
| 1656 | + } | ||
| 1657 | + | ||
| 1616 | 1658 | ||
| 1617 | 1659 | /* all errors will be checked in one fell swoop in init_tkinter() */ | |
| 1618 | 1660 | static void | |
@@ -1670,11 +1712,6 @@ init_tkinter() | |||
| 1670 | 1712 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); | |
| 1671 | 1713 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); | |
| 1672 | 1714 | ||
| 1673 | - if (PyOS_InputHook == NULL) { | ||
| 1674 | - event_interp = PyThreadState_Get()->interp; | ||
| 1675 | - PyOS_InputHook = EventHook; | ||
| 1676 | - } | ||
| 1677 | - | ||
| 1678 | 1715 | if (PyErr_Occurred()) | |
| 1679 | 1716 | return; | |
| 1680 | 1717 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments