Debian Codegraph @

xpa/tclloop.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 #if HAVE_TCL
8
9 #include <tcl.h>
10
11 /* We build XPA on Windows using Cygwin and, for convenience, we sense the
12 presence of Windows by the presence of Cygwin. This is important for Tcl
13 because Windows does not support Tcl_CreateFileHandler */
14 #ifndef HAVE_TCLFILEHANDLER
15 #if HAVE_CYGWIN
16 #define HAVE_TCLFILEHANDLER 0
17 #else
18 #define HAVE_TCLFILEHANDLER 1
19 #endif
20 #endif
21
22 /*
23 *----------------------------------------------------------------------------
24 *
25 *
26 * Private Routines and Data
27 *
28 *
29 *----------------------------------------------------------------------------
30 */
31
32 #define TCL_SOCKET TCL_UNIX_FD
33
34 /*
35 *
36 * record struct for maintining Tcl info in Tcl select loop
37 *
38 */
39 typedef struct xpatclrec{
40 Tcl_Event header;
41 int fd;
42 void *client_data;
43 } *XPATcl, XPATclRec;
44
45
46 #if HAVE_TCLFILEHANDLER
47 /*
48 *----------------------------------------------------------------------------
49 *
50 * Routine: XPATclHandler
51 *
52 * Purpose: handle one request for an xpaset or xpaget
53 *
54 * Return: none
55 *
56 *----------------------------------------------------------------------------
57 */
58 #ifdef ANSI_FUNC
59 static void
60 XPATclHandler (ClientData client_data, int mask)
61 #else
62 static void XPATclHandler(client_data, mask)
63 ClientData client_data;
64 int mask;
65 #endif
66 {
67 XPATcl xptr = (XPATcl)client_data;
68 if( (xptr == NULL) || (xptr->client_data == NULL) )
69 return;
70 XPAHandler((XPA)xptr->client_data, xptr->fd);
71 }
72
73 #else
74
75 #define TCL_BLOCKTIME_SEC 0
76 #define TCL_BLOCKTIME_USEC 1000
77
78 /*
79 *----------------------------------------------------------------------
80 *
81 * XPAEventProc --
82 *
83 * This procedure is called by Tcl_ServiceEvent when an XPA event
84 * reaches the front of the event queue. This procedure is
85 * responsible for notifying the generic channel code.
86 *
87 * Results:
88 * Returns 1 if the event was handled, meaning it should be removed
89 * from the queue. Returns 0 if the event was not handled, meaning
90 * it should stay on the queue. The only time the event isn't
91 * handled is if the TCL_FILE_EVENTS flag bit isn't set.
92 *
93 * Side effects:
94 * Whatever the channel callback procedures do.
95 *
96 *----------------------------------------------------------------------
97 */
98 #ifdef ANSI_FUNC
99 static int
100 XPAEventProc(Tcl_Event *evPtr, int flags)
101 #else
102 static int
103 XPAEventProc(evPtr, flags)
104 Tcl_Event *evPtr; /* Event to service. */
105 int flags; /* Flags that indicate what events to
106 * handle, such as TCL_FILE_EVENTS. */
107 #endif
108 {
109 XPATcl xptr = (XPATcl)evPtr;
110 if( xptr && xptr->client_data )
111 XPAHandler((XPA)xptr->client_data, xptr->fd);
112 return 1;
113 }
114
115 /*
116 *----------------------------------------------------------------------
117 *
118 * XPASetupProc --
119 *
120 * This procedure is invoked before Tcl_DoOneEvent blocks waiting
121 * for an event.
122 *
123 * Results:
124 * None.
125 *
126 * Side effects:
127 * Adjusts the block time if needed.
128 *
129 *----------------------------------------------------------------------
130 */
131 #ifdef ANSI_FUNC
132 static void
133 XPASetupProc(ClientData data, int flags)
134 #else
135 static void
136 XPASetupProc(data, flags)
137 ClientData data;
138 int flags;
139 #endif
140 {
141 Tcl_Time blockTime = { TCL_BLOCKTIME_SEC, TCL_BLOCKTIME_USEC };
142
143 Tcl_SetMaxBlockTime(&blockTime);
144 return;
145 }
146
147 /*
148 *----------------------------------------------------------------------
149 *
150 * XPACheckProc --
151 *
152 * This procedure is called by Tcl_DoOneEvent to check the XPA
153 * event source for events.
154 *
155 * Results:
156 * None.
157 *
158 * Side effects:
159 * May queue an event.
160 *
161 *----------------------------------------------------------------------
162 */
163 #ifdef ANSI_FUNC
164 static void
165 XPACheckProc(ClientData data, int flags)
166 #else
167 static void
168 XPACheckProc(data, flags)
169 ClientData data;
170 int flags;
171 #endif
172 {
173 XPATcl xptr = (XPATcl)data;
174 XPATcl evPtr;
175 fd_set readfds;
176 struct timeval tv;
177 int got;
178
179 again:
180 FD_ZERO(&readfds);
181 if( XPAActiveFd(xptr->fd) ){
182 FD_SET(xptr->fd, &readfds);
183 tv.tv_sec = 0;
184 tv.tv_usec = 0;
185 got = xselect(xptr->fd+1, &readfds, NULL, NULL, &tv);
186 if( got < 0 ){
187 if( errno == EINTR )
188 goto again;
189 perror("xpa select event");
190 Tcl_DeleteEventSource(XPASetupProc, XPACheckProc, xptr);
191 }
192 else if( got ){
193 evPtr = (XPATcl)Tcl_Alloc(sizeof(XPATclRec));
194 memcpy(evPtr, xptr, sizeof(XPATclRec));
195 evPtr->header.proc = XPAEventProc;
196 Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
197 }
198 }
199 }
200 #endif
201
202 /*
203 *----------------------------------------------------------------------------
204 *
205 * Routine: XPATclEnableOneInput
206 *
207 * Purpose: Enable 1 XPA entry from the Tcl event loop
208 *
209 * Results: none
210 *
211 *----------------------------------------------------------------------------
212 */
213 #ifdef ANSI_FUNC
214 static void
215 XPATclEnableOneInput (void *client_data)
216 #else
217 static void XPATclEnableOneInput(client_data)
218 void *client_data;
219 #endif
220 {
221 XPATcl xptr = (XPATcl)client_data;
222 if( xptr == NULL )
223 return;
224 #if HAVE_TCLFILEHANDLER
225 Tcl_CreateFileHandler(xptr->fd, TCL_READABLE, XPATclHandler, xptr);
226 #else
227 Tcl_CreateEventSource(XPASetupProc, XPACheckProc, xptr);
228 #endif
229 }
230
231 /*
232 *----------------------------------------------------------------------------
233 *
234 * Routine: XPATclDisableOneInput
235 *
236 * Purpose: Disable 1 XPA entry from the Tcl event loop
237 *
238 * Results: none
239 *
240 *----------------------------------------------------------------------------
241 */
242 #ifdef ANSI_FUNC
243 static void
244 XPATclDisableOneInput (void *client_data)
245 #else
246 static void XPATclDisableOneInput(client_data)
247 void *client_data;
248 #endif
249 {
250 XPATcl xptr = (XPATcl)client_data;
251 if( xptr == NULL )
252 return;
253 #if HAVE_TCLFILEHANDLER
254 Tcl_CreateFileHandler(xptr->fd, 0, XPATclHandler, xptr->client_data);
255 #else
256 Tcl_DeleteEventSource(XPASetupProc, XPACheckProc, xptr);
257 #endif
258 }
259
260 /*
261 *----------------------------------------------------------------------------
262 *
263 * Routine: XPATclAddOneInput
264 *
265 * Purpose: Add 1 XPA entry to the Tcl event loop
266 *
267 * Results: none
268 *
269 *----------------------------------------------------------------------------
270 */
271 #ifdef ANSI_FUNC
272 static void *
273 XPATclAddOneInput (void *client_data, int fd)
274 #else
275 static void *XPATclAddOneInput(client_data, fd)
276 void *client_data;
277 int fd;
278 #endif
279 {
280 XPATcl xptr;
281 if( fd < 0 )
282 return(NULL);
283 xptr = (XPATcl)xcalloc(1, sizeof(XPATclRec));
284 xptr->fd = fd;
285 xptr->client_data = (XPA)client_data;
286 XPATclEnableOneInput(xptr);
287 return(xptr);
288 }
289
290 /*
291 *----------------------------------------------------------------------------
292 *
293 * Routine: XPATclDelOneInput
294 *
295 * Purpose: Delete 1 XPA entry from the Tcl event loop (called by XPAFree)
296 *
297 * Results: none
298 *
299 *----------------------------------------------------------------------------
300 */
301 #ifdef ANSI_FUNC
302 static void
303 XPATclDelOneInput (void *client_data)
304 #else
305 static void XPATclDelOneInput(client_data)
306 void *client_data;
307 #endif
308 {
309 XPATcl xptr = (XPATcl)client_data;
310 if( xptr == NULL )
311 return;
312 XPATclDisableOneInput(xptr);
313 xfree(xptr);
314 }
315
316 /*
317 *----------------------------------------------------------------------------
318 *
319 *
320 * Public Routines and Data
321 *
322 *
323 *----------------------------------------------------------------------------
324 */
325
326 /*
327 *----------------------------------------------------------------------------
328 *
329 * Routine: XPATclAddInput
330 *
331 * Purpose: Add XPA entries to the Tcl event loop
332 *
333 * Results: number of xpa entried added
334 *
335 *----------------------------------------------------------------------------
336 */
337 #ifdef ANSI_FUNC
338 int
339 XPATclAddInput (XPA xpa)
340 #else
341 int XPATclAddInput(xpa)
342 XPA xpa;
343 #endif
344 {
345 XPA cur;
346 int got=0;
347
348 /* if a specific xpa was specified, just add it */
349 if( xpa != NULL ){
350 /* remove old one */
351 if( xpa->seldel && xpa->selptr ){
352 (xpa->seldel)(xpa->selptr);
353 }
354 /* add new one */
355 xpa->seladd = XPATclAddOneInput;
356 xpa->seldel = XPATclDelOneInput;
357 #if HAVE_TCLFILEHANDLER
358 xpa->selon = XPATclEnableOneInput;
359 xpa->seloff = XPATclDisableOneInput;
360 #endif
361 xpa->selptr = XPATclAddOneInput((void *)xpa, xpa->fd);
362 got = 1;
363 }
364 /* otherwise set up all xpa's */
365 else{
366 for(cur=(XPA)XPAListHead(); cur!=NULL; cur=cur->next){
367 /* remove old one */
368 if( cur->seldel && cur->selptr ){
369 (cur->seldel)(cur->selptr);
370 }
371 /* add new one */
372 cur->seladd = XPATclAddOneInput;
373 cur->seldel = XPATclDelOneInput;
374 #if HAVE_TCLFILEHANDLER
375 cur->selon = XPATclEnableOneInput;
376 cur->seloff = XPATclDisableOneInput;
377 #endif
378 cur->selptr = XPATclAddOneInput((void *)cur, cur->fd);
379 got++;
380 }
381 }
382 return(got);
383 }
384
385 int xpa_tcl = 1;
386
387 #else
388
389 int xpa_tcl = 0;
390
391 #endif