Debian Codegraph @

pTk/mTk/additions/exWinHandle.c

raw
1 /*
2 Copyright (c) 1998 Nick Ing-Simmons. All rights reserved.
3 This program is free software; you can redistribute it and/or
4 modify it under the same terms as FileHandle itself.
5 */
6 #ifdef TCL_EVENT_IMPLEMENT
7
8 #include "tkPort.h"
9 #include "Lang.h"
10
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13 #include <winsock.h>
14 #include "tkWinInt.h"
15
16 typedef struct FileHandler {
17 int fd;
18 int mask; /* Mask of desired events: TCL_READABLE,
19 * etc. */
20 int readyMask; /* Mask of events that have been seen since the
21 * last time file handlers were invoked for
22 * this file. */
23 int pending;
24 HANDLE auxEvent;
25 Tcl_FileProc *proc; /* Procedure to call, in the style of
26 * Tcl_CreateFileHandler. */
27 ClientData clientData; /* Argument to pass to proc. */
28 struct FileHandler *nextPtr;/* Next in list of all files we care about. */
29 } FileHandler;
30
31 typedef struct FileHandlerEvent {
32 Tcl_Event header; /* Information that is standard for
33 * all events. */
34 int fd; /* File descriptor that is ready. Used
35 * to find the FileHandler structure for
36 * the file (can't point directly to the
37 * FileHandler structure because it could
38 * go away while the event is queued). */
39 } FileHandlerEvent;
40
41
42 static int initialized;
43
44 static FileHandler *firstFileHandlerPtr;
45
46 static void FileHandleEventInit(void);
47
48 static void
49 FileReadyProc(ClientData clientData, HANDLE handle)
50 {
51 FileHandler *filePtr = (FileHandler *) clientData;
52 filePtr->readyMask |= TCL_READABLE;
53 }
54
55
56 static void
57 FileHandleSetupProc(ClientData data, int flags)
58 {
59 static Tcl_Time blockTime = { 0, 0 };
60 FileHandler *filePtr;
61
62 if (!(flags & TCL_FILE_EVENTS)) {
63 return;
64 }
65
66 for (filePtr = firstFileHandlerPtr; filePtr != NULL; filePtr = filePtr->nextPtr) {
67 if (!filePtr->readyMask && filePtr->mask & TCL_READABLE) {
68 HANDLE fh = (HANDLE) Lang_OSHandle(filePtr->fd);
69 #if 0
70 /* Now see if the handle is a socket */
71 char sockbuf[256];
72 int optlen = sizeof(sockbuf);
73 int retval = getsockopt((SOCKET)fh, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
74 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
75 /* Not a socket - use the raw handle */
76 #endif
77 Tcl_WatchHandle(fh, FileReadyProc, (ClientData) filePtr);
78 #if 0
79 } else {
80 /* Socket handle */
81 if (!filePtr->auxEvent) {
82 filePtr->auxEvent = WSACreateEvent();
83 }
84 WSAEventSelect((SOCKET) fh, filePtr->auxEvent, FD_READ|FD_ACCEPT);
85 Tcl_WatchHandle(filePtr->auxEvent, FileReadyProc, (ClientData) filePtr);
86 }
87 #endif
88 break;
89 }
90 }
91
92 }
93
94 static int
95 FileHandlerEventProc(evPtr, flags)
96 Tcl_Event *evPtr; /* Event to service. */
97 int flags; /* Flags that indicate what events to
98 * handle, such as TCL_FILE_EVENTS. */
99 {
100 FileHandler *filePtr;
101 FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr;
102 int mask;
103
104 if (!(flags & TCL_FILE_EVENTS)) {
105 return 0;
106 }
107
108 /*
109 * Search through the file handlers to find the one whose handle matches
110 * the event. We do this rather than keeping a pointer to the file
111 * handler directly in the event, so that the handler can be deleted
112 * while the event is queued without leaving a dangling pointer.
113 */
114
115 for (filePtr = firstFileHandlerPtr; filePtr != NULL;
116 filePtr = filePtr->nextPtr) {
117 if (filePtr->fd != fileEvPtr->fd) {
118 continue;
119 }
120
121 /*
122 * The code is tricky for two reasons:
123 * 1. The file handler's desired events could have changed
124 * since the time when the event was queued, so AND the
125 * ready mask with the desired mask.
126 * 2. The file could have been closed and re-opened since
127 * the time when the event was queued. This is why the
128 * ready mask is stored in the file handler rather than
129 * the queued event: it will be zeroed when a new
130 * file handler is created for the newly opened file.
131 */
132
133 mask = filePtr->readyMask & filePtr->mask;
134 filePtr->readyMask = 0;
135 filePtr->pending = 0;
136 if (mask != 0) {
137 (*filePtr->proc)(filePtr->clientData, mask);
138 }
139 break;
140 }
141 return 1;
142 }
143
144 static void
145 FileHandleCheckProc(data, flags)
146 ClientData data; /* Not used. */
147 int flags; /* Event flags as passed to Tcl_DoOneEvent. */
148 {
149
150 FileHandlerEvent *fileEvPtr;
151 FileHandler *filePtr = firstFileHandlerPtr;
152
153 if (!(flags & TCL_FILE_EVENTS)) {
154 return;
155 }
156
157 while (filePtr)
158 {
159 if (filePtr->readyMask && !filePtr->pending)
160 {
161 fileEvPtr = (FileHandlerEvent *) ckalloc(sizeof(FileHandlerEvent));
162 fileEvPtr->fd = filePtr->fd;
163 Tcl_QueueProcEvent(FileHandlerEventProc, (Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
164 filePtr->pending = 1;
165 }
166 filePtr = filePtr->nextPtr;
167 }
168
169 }
170
171 static void
172 FileHandleExitHandler(ClientData clientData)
173 {
174 Tcl_DeleteEventSource(FileHandleSetupProc, FileHandleCheckProc, NULL);
175 initialized = 0;
176 }
177
178 static void
179 FileHandleEventInit(void)
180 {
181 initialized = 1;
182 firstFileHandlerPtr = NULL;
183 Tcl_CreateEventSource(FileHandleSetupProc, FileHandleCheckProc, NULL);
184 Tcl_CreateExitHandler(FileHandleExitHandler, NULL);
185 }
186
187
188 void
189 Tcl_CreateFileHandler(fd, mask, proc, clientData)
190 int fd; /* UNIX-like "FileHandle" of stream to watch. */
191 int mask; /* OR'ed combination of TCL_READABLE,
192 * TCL_WRITABLE, and TCL_EXCEPTION:
193 * indicates conditions under which
194 * proc should be called. */
195 Tcl_FileProc *proc; /* Procedure to call for each
196 * selected event. */
197 ClientData clientData; /* Arbitrary data to pass to proc. */
198 {
199 FileHandler *filePtr;
200
201 if (!initialized) {
202 FileHandleEventInit();
203 }
204
205 for (filePtr = firstFileHandlerPtr; filePtr != NULL;
206 filePtr = filePtr->nextPtr) {
207 if (filePtr->fd == fd) {
208 break;
209 }
210 }
211
212 if (filePtr == NULL) {
213 filePtr = (FileHandler*) ckalloc(sizeof(FileHandler)); /* MLK */
214 filePtr->fd = fd;
215 filePtr->readyMask = 0;
216 filePtr->pending = 0;
217 filePtr->auxEvent = 0;
218 filePtr->nextPtr = firstFileHandlerPtr;
219 firstFileHandlerPtr = filePtr;
220 }
221 filePtr->proc = proc;
222 filePtr->clientData = clientData;
223 filePtr->mask = mask;
224 }
225
226 void
227 Tcl_DeleteFileHandler(fd)
228 int fd; /* Stream id for which to remove callback procedure. */
229 {
230 FileHandler *filePtr, *prevPtr;
231 int index, bit, i;
232 unsigned long flags;
233
234 if (!initialized) {
235 FileHandleEventInit();
236 }
237
238 /*
239 * Find the entry for the given file (and return if there
240 * isn't one).
241 */
242
243 for (prevPtr = NULL, filePtr = firstFileHandlerPtr; ;
244 prevPtr = filePtr, filePtr = filePtr->nextPtr) {
245 if (filePtr == NULL) {
246 return;
247 }
248 if (filePtr->fd == fd) {
249 break;
250 }
251 }
252 /*
253 * Clean up information in the callback record.
254 */
255
256 if (prevPtr == NULL) {
257 firstFileHandlerPtr = filePtr->nextPtr;
258 } else {
259 prevPtr->nextPtr = filePtr->nextPtr;
260 }
261 if (filePtr->auxEvent) {
262 CloseHandle(filePtr->auxEvent);
263 }
264 ckfree((char *) filePtr);
265 }
266
267 #endif