Debian Codegraph @

unix/tclXtNotify.c

raw
1 /*
2 * tclXtNotify.c --
3 *
4 * This file contains the notifier driver implementation for the Xt
5 * intrinsics.
6 *
7 * Copyright (c) 1997 by Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #ifndef USE_TCL_STUBS
14 # define USE_TCL_STUBS
15 #endif
16 #include <X11/Intrinsic.h>
17 #include "tclInt.h"
18
19 /*
20 * This structure is used to keep track of the notifier info for a
21 * registered file.
22 */
23
24 typedef struct FileHandler {
25 int fd;
26 int mask; /* Mask of desired events: TCL_READABLE,
27 * etc. */
28 int readyMask; /* Events that have been seen since the last
29 * time FileHandlerEventProc was called for
30 * this file. */
31 XtInputId read; /* Xt read callback handle. */
32 XtInputId write; /* Xt write callback handle. */
33 XtInputId except; /* Xt exception callback handle. */
34 Tcl_FileProc *proc; /* Procedure to call, in the style of
35 * Tcl_CreateFileHandler. */
36 ClientData clientData; /* Argument to pass to proc. */
37 struct FileHandler *nextPtr;/* Next in list of all files we care about. */
38 } FileHandler;
39
40 /*
41 * The following structure is what is added to the Tcl event queue when file
42 * handlers are ready to fire.
43 */
44
45 typedef struct FileHandlerEvent {
46 Tcl_Event header; /* Information that is standard for all
47 * events. */
48 int fd; /* File descriptor that is ready. Used to find
49 * the FileHandler structure for the file
50 * (can't point directly to the FileHandler
51 * structure because it could go away while
52 * the event is queued). */
53 } FileHandlerEvent;
54
55 /*
56 * The following static structure contains the state information for the Xt
57 * based implementation of the Tcl notifier.
58 */
59
60 static struct NotifierState {
61 XtAppContext appContext; /* The context used by the Xt notifier. Can be
62 * set with TclSetAppContext. */
63 int appContextCreated; /* Was it created by us? */
64 XtIntervalId currentTimeout;/* Handle of current timer. */
65 FileHandler *firstFileHandlerPtr;
66 /* Pointer to head of file handler list. */
67 } notifier;
68
69 /*
70 * The following static indicates whether this module has been initialized.
71 */
72
73 static int initialized = 0;
74
75 /*
76 * Static routines defined in this file.
77 */
78
79 static int FileHandlerEventProc(Tcl_Event *evPtr, int flags);
80 static void FileProc(XtPointer clientData, int *source,
81 XtInputId *id);
82 static void NotifierExitHandler(ClientData clientData);
83 static void TimerProc(XtPointer clientData, XtIntervalId *id);
84 static void CreateFileHandler(int fd, int mask,
85 Tcl_FileProc *proc, ClientData clientData);
86 static void DeleteFileHandler(int fd);
87 static void SetTimer(const Tcl_Time * timePtr);
88 static int WaitForEvent(const Tcl_Time * timePtr);
89
90 /*
91 * Functions defined in this file for use by users of the Xt Notifier:
92 */
93
94 MODULE_SCOPE void InitNotifier(void);
95 MODULE_SCOPE XtAppContext TclSetAppContext(XtAppContext ctx);
96
97 /*
98 *----------------------------------------------------------------------
99 *
100 * TclSetAppContext --
101 *
102 * Set the notifier application context.
103 *
104 * Results:
105 * None.
106 *
107 * Side effects:
108 * Sets the application context used by the notifier. Panics if the
109 * context is already set when called.
110 *
111 *----------------------------------------------------------------------
112 */
113
114 XtAppContext
115 TclSetAppContext(
116 XtAppContext appContext)
117 {
118 if (!initialized) {
119 InitNotifier();
120 }
121
122 /*
123 * If we already have a context we check whether we were asked to set a
124 * new context. If so, we panic because we try to prevent switching
125 * contexts by mistake. Otherwise, we return the one we have.
126 */
127
128 if (notifier.appContext != NULL) {
129 if (appContext != NULL) {
130 /*
131 * We already have a context. We do not allow switching contexts
132 * after initialization, so we panic.
133 */
134
135 Tcl_Panic("TclSetAppContext: multiple application contexts");
136 }
137 } else {
138 /*
139 * If we get here we have not yet gotten a context, so either create
140 * one or use the one supplied by our caller.
141 */
142
143 if (appContext == NULL) {
144 /*
145 * We must create a new context and tell our caller what it is, so
146 * she can use it too.
147 */
148
149 notifier.appContext = XtCreateApplicationContext();
150 notifier.appContextCreated = 1;
151 } else {
152 /*
153 * Otherwise we remember the context that our caller gave us and
154 * use it.
155 */
156
157 notifier.appContextCreated = 0;
158 notifier.appContext = appContext;
159 }
160 }
161
162 return notifier.appContext;
163 }
164
165 /*
166 *----------------------------------------------------------------------
167 *
168 * InitNotifier --
169 *
170 * Initializes the notifier state.
171 *
172 * Results:
173 * None.
174 *
175 * Side effects:
176 * Creates a new exit handler.
177 *
178 *----------------------------------------------------------------------
179 */
180
181 void
182 InitNotifier(void)
183 {
184 Tcl_NotifierProcs np;
185
186 /*
187 * Only reinitialize if we are not in exit handling. The notifier can get
188 * reinitialized after its own exit handler has run, because of exit
189 * handlers for the I/O and timer sub-systems (order dependency).
190 */
191
192 if (TclInExit()) {
193 return;
194 }
195
196 memset(&np, 0, sizeof(np));
197 np.createFileHandlerProc = CreateFileHandler;
198 np.deleteFileHandlerProc = DeleteFileHandler;
199 np.setTimerProc = SetTimer;
200 np.waitForEventProc = WaitForEvent;
201 Tcl_SetNotifier(&np);
202
203 /*
204 * DO NOT create the application context yet; doing so would prevent
205 * external applications from setting it for us to their own ones.
206 */
207
208 initialized = 1;
209 Tcl_CreateExitHandler(NotifierExitHandler, NULL);
210 }
211
212 /*
213 *----------------------------------------------------------------------
214 *
215 * NotifierExitHandler --
216 *
217 * This function is called to cleanup the notifier state before Tcl is
218 * unloaded.
219 *
220 * Results:
221 * None.
222 *
223 * Side effects:
224 * Destroys the notifier window.
225 *
226 *----------------------------------------------------------------------
227 */
228
229 static void
230 NotifierExitHandler(
231 ClientData clientData) /* Not used. */
232 {
233 if (notifier.currentTimeout != 0) {
234 XtRemoveTimeOut(notifier.currentTimeout);
235 }
236 for (; notifier.firstFileHandlerPtr != NULL; ) {
237 Tcl_DeleteFileHandler(notifier.firstFileHandlerPtr->fd);
238 }
239 if (notifier.appContextCreated) {
240 XtDestroyApplicationContext(notifier.appContext);
241 notifier.appContextCreated = 0;
242 notifier.appContext = NULL;
243 }
244 initialized = 0;
245 }
246
247 /*
248 *----------------------------------------------------------------------
249 *
250 * SetTimer --
251 *
252 * This procedure sets the current notifier timeout value.
253 *
254 * Results:
255 * None.
256 *
257 * Side effects:
258 * Replaces any previous timer.
259 *
260 *----------------------------------------------------------------------
261 */
262
263 static void
264 SetTimer(
265 const Tcl_Time *timePtr) /* Timeout value, may be NULL. */
266 {
267 long timeout;
268
269 if (!initialized) {
270 InitNotifier();
271 }
272
273 TclSetAppContext(NULL);
274 if (notifier.currentTimeout != 0) {
275 XtRemoveTimeOut(notifier.currentTimeout);
276 }
277 if (timePtr) {
278 timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
279 notifier.currentTimeout = XtAppAddTimeOut(notifier.appContext,
280 (unsigned long) timeout, TimerProc, NULL);
281 } else {
282 notifier.currentTimeout = 0;
283 }
284 }
285
286 /*
287 *----------------------------------------------------------------------
288 *
289 * TimerProc --
290 *
291 * This procedure is the XtTimerCallbackProc used to handle timeouts.
292 *
293 * Results:
294 * None.
295 *
296 * Side effects:
297 * Processes all queued events.
298 *
299 *----------------------------------------------------------------------
300 */
301
302 static void
303 TimerProc(
304 XtPointer clientData, /* Not used. */
305 XtIntervalId *id)
306 {
307 if (*id != notifier.currentTimeout) {
308 return;
309 }
310 notifier.currentTimeout = 0;
311
312 Tcl_ServiceAll();
313 }
314
315 /*
316 *----------------------------------------------------------------------
317 *
318 * CreateFileHandler --
319 *
320 * This procedure registers a file handler with the Xt notifier.
321 *
322 * Results:
323 * None.
324 *
325 * Side effects:
326 * Creates a new file handler structure and registers one or more input
327 * procedures with Xt.
328 *
329 *----------------------------------------------------------------------
330 */
331
332 static void
333 CreateFileHandler(
334 int fd, /* Handle of stream to watch. */
335 int mask, /* OR'ed combination of TCL_READABLE,
336 * TCL_WRITABLE, and TCL_EXCEPTION: indicates
337 * conditions under which proc should be
338 * called. */
339 Tcl_FileProc *proc, /* Procedure to call for each selected
340 * event. */
341 ClientData clientData) /* Arbitrary data to pass to proc. */
342 {
343 FileHandler *filePtr;
344
345 if (!initialized) {
346 InitNotifier();
347 }
348
349 TclSetAppContext(NULL);
350
351 for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL;
352 filePtr = filePtr->nextPtr) {
353 if (filePtr->fd == fd) {
354 break;
355 }
356 }
357 if (filePtr == NULL) {
358 filePtr = ckalloc(sizeof(FileHandler));
359 filePtr->fd = fd;
360 filePtr->read = 0;
361 filePtr->write = 0;
362 filePtr->except = 0;
363 filePtr->readyMask = 0;
364 filePtr->mask = 0;
365 filePtr->nextPtr = notifier.firstFileHandlerPtr;
366 notifier.firstFileHandlerPtr = filePtr;
367 }
368 filePtr->proc = proc;
369 filePtr->clientData = clientData;
370
371 /*
372 * Register the file with the Xt notifier, if it hasn't been done yet.
373 */
374
375 if (mask & TCL_READABLE) {
376 if (!(filePtr->mask & TCL_READABLE)) {
377 filePtr->read = XtAppAddInput(notifier.appContext, fd,
378 INT2PTR(XtInputReadMask), FileProc, filePtr);
379 }
380 } else {
381 if (filePtr->mask & TCL_READABLE) {
382 XtRemoveInput(filePtr->read);
383 }
384 }
385 if (mask & TCL_WRITABLE) {
386 if (!(filePtr->mask & TCL_WRITABLE)) {
387 filePtr->write = XtAppAddInput(notifier.appContext, fd,
388 INT2PTR(XtInputWriteMask), FileProc, filePtr);
389 }
390 } else {
391 if (filePtr->mask & TCL_WRITABLE) {
392 XtRemoveInput(filePtr->write);
393 }
394 }
395 if (mask & TCL_EXCEPTION) {
396 if (!(filePtr->mask & TCL_EXCEPTION)) {
397 filePtr->except = XtAppAddInput(notifier.appContext, fd,
398 INT2PTR(XtInputExceptMask), FileProc, filePtr);
399 }
400 } else {
401 if (filePtr->mask & TCL_EXCEPTION) {
402 XtRemoveInput(filePtr->except);
403 }
404 }
405 filePtr->mask = mask;
406 }
407
408 /*
409 *----------------------------------------------------------------------
410 *
411 * DeleteFileHandler --
412 *
413 * Cancel a previously-arranged callback arrangement for a file.
414 *
415 * Results:
416 * None.
417 *
418 * Side effects:
419 * If a callback was previously registered on file, remove it.
420 *
421 *----------------------------------------------------------------------
422 */
423
424 static void
425 DeleteFileHandler(
426 int fd) /* Stream id for which to remove callback
427 * procedure. */
428 {
429 FileHandler *filePtr, *prevPtr;
430
431 if (!initialized) {
432 InitNotifier();
433 }
434
435 TclSetAppContext(NULL);
436
437 /*
438 * Find the entry for the given file (and return if there isn't one).
439 */
440
441 for (prevPtr = NULL, filePtr = notifier.firstFileHandlerPtr; ;
442 prevPtr = filePtr, filePtr = filePtr->nextPtr) {
443 if (filePtr == NULL) {
444 return;
445 }
446 if (filePtr->fd == fd) {
447 break;
448 }
449 }
450
451 /*
452 * Clean up information in the callback record.
453 */
454
455 if (prevPtr == NULL) {
456 notifier.firstFileHandlerPtr = filePtr->nextPtr;
457 } else {
458 prevPtr->nextPtr = filePtr->nextPtr;
459 }
460 if (filePtr->mask & TCL_READABLE) {
461 XtRemoveInput(filePtr->read);
462 }
463 if (filePtr->mask & TCL_WRITABLE) {
464 XtRemoveInput(filePtr->write);
465 }
466 if (filePtr->mask & TCL_EXCEPTION) {
467 XtRemoveInput(filePtr->except);
468 }
469 ckfree(filePtr);
470 }
471
472 /*
473 *----------------------------------------------------------------------
474 *
475 * FileProc --
476 *
477 * These procedures are called by Xt when a file becomes readable,
478 * writable, or has an exception.
479 *
480 * Results:
481 * None.
482 *
483 * Side effects:
484 * Makes an entry on the Tcl event queue if the event is interesting.
485 *
486 *----------------------------------------------------------------------
487 */
488
489 static void
490 FileProc(
491 XtPointer clientData,
492 int *fd,
493 XtInputId *id)
494 {
495 FileHandler *filePtr = (FileHandler *)clientData;
496 FileHandlerEvent *fileEvPtr;
497 int mask = 0;
498
499 /*
500 * Determine which event happened.
501 */
502
503 if (*id == filePtr->read) {
504 mask = TCL_READABLE;
505 } else if (*id == filePtr->write) {
506 mask = TCL_WRITABLE;
507 } else if (*id == filePtr->except) {
508 mask = TCL_EXCEPTION;
509 }
510
511 /*
512 * Ignore unwanted or duplicate events.
513 */
514
515 if (!(filePtr->mask & mask) || (filePtr->readyMask & mask)) {
516 return;
517 }
518
519 /*
520 * This is an interesting event, so put it onto the event queue.
521 */
522
523 filePtr->readyMask |= mask;
524 fileEvPtr = ckalloc(sizeof(FileHandlerEvent));
525 fileEvPtr->header.proc = FileHandlerEventProc;
526 fileEvPtr->fd = filePtr->fd;
527 Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
528
529 /*
530 * Process events on the Tcl event queue before returning to Xt.
531 */
532
533 Tcl_ServiceAll();
534 }
535
536 /*
537 *----------------------------------------------------------------------
538 *
539 * FileHandlerEventProc --
540 *
541 * This procedure is called by Tcl_ServiceEvent when a file event reaches
542 * the front of the event queue. This procedure is responsible for
543 * actually handling the event by invoking the callback for the file
544 * handler.
545 *
546 * Results:
547 * Returns 1 if the event was handled, meaning it should be removed from
548 * the queue. Returns 0 if the event was not handled, meaning it should
549 * stay on the queue. The only time the event isn't handled is if the
550 * TCL_FILE_EVENTS flag bit isn't set.
551 *
552 * Side effects:
553 * Whatever the file handler's callback procedure does.
554 *
555 *----------------------------------------------------------------------
556 */
557
558 static int
559 FileHandlerEventProc(
560 Tcl_Event *evPtr, /* Event to service. */
561 int flags) /* Flags that indicate what events to handle,
562 * such as TCL_FILE_EVENTS. */
563 {
564 FileHandler *filePtr;
565 FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr;
566 int mask;
567
568 if (!(flags & TCL_FILE_EVENTS)) {
569 return 0;
570 }
571
572 /*
573 * Search through the file handlers to find the one whose handle matches
574 * the event. We do this rather than keeping a pointer to the file handler
575 * directly in the event, so that the handler can be deleted while the
576 * event is queued without leaving a dangling pointer.
577 */
578
579 for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL;
580 filePtr = filePtr->nextPtr) {
581 if (filePtr->fd != fileEvPtr->fd) {
582 continue;
583 }
584
585 /*
586 * The code is tricky for two reasons:
587 * 1. The file handler's desired events could have changed since the
588 * time when the event was queued, so AND the ready mask with the
589 * desired mask.
590 * 2. The file could have been closed and re-opened since the time
591 * when the event was queued. This is why the ready mask is stored
592 * in the file handler rather than the queued event: it will be
593 * zeroed when a new file handler is created for the newly opened
594 * file.
595 */
596
597 mask = filePtr->readyMask & filePtr->mask;
598 filePtr->readyMask = 0;
599 if (mask != 0) {
600 filePtr->proc(filePtr->clientData, mask);
601 }
602 break;
603 }
604 return 1;
605 }
606
607 /*
608 *----------------------------------------------------------------------
609 *
610 * WaitForEvent --
611 *
612 * This function is called by Tcl_DoOneEvent to wait for new events on
613 * the message queue. If the block time is 0, then Tcl_WaitForEvent just
614 * polls without blocking.
615 *
616 * Results:
617 * Returns 1 if an event was found, else 0. This ensures that
618 * Tcl_DoOneEvent will return 1, even if the event is handled by non-Tcl
619 * code.
620 *
621 * Side effects:
622 * Queues file events that are detected by the select.
623 *
624 *----------------------------------------------------------------------
625 */
626
627 static int
628 WaitForEvent(
629 const Tcl_Time *timePtr) /* Maximum block time, or NULL. */
630 {
631 int timeout;
632
633 if (!initialized) {
634 InitNotifier();
635 }
636
637 TclSetAppContext(NULL);
638
639 if (timePtr) {
640 timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
641 if (timeout == 0) {
642 if (XtAppPending(notifier.appContext)) {
643 goto process;
644 } else {
645 return 0;
646 }
647 } else {
648 Tcl_SetTimer(timePtr);
649 }
650 }
651
652 process:
653 XtAppProcessEvent(notifier.appContext, XtIMAll);
654 return 1;
655 }
656
657 /*
658 * Local Variables:
659 * mode: c
660 * c-basic-offset: 4
661 * fill-column: 78
662 * End:
663 */