This repository was archived by the owner on Apr 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtkEvent.c
More file actions
1897 lines (1728 loc) · 52.2 KB
/
tkEvent.c
File metadata and controls
1897 lines (1728 loc) · 52.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tkEvent.c --
*
* This file provides basic event-managing facilities, whereby
* procedure callbacks may be attached to certain events. It
* also contains the command procedures for the commands "after"
* and "fileevent", plus abridged versions of "tkwait" and
* "update", for use with Tk_EventInit.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
/*
* For each timer callback that's pending, there is one record
* of the following type, chained together in a list sorted by
* time (earliest event first).
*/
typedef struct TimerEvent {
struct timeval time; /* When timer is to fire. */
void (*proc) _ANSI_ARGS_((ClientData clientData));
/* Procedure to call. */
ClientData clientData; /* Argument to pass to proc. */
Tk_TimerToken token; /* Identifies event so it can be
* deleted. */
struct TimerEvent *nextPtr; /* Next event in queue, or NULL for
* end of queue. */
} TimerEvent;
static TimerEvent *firstTimerHandlerPtr;
/* First event in queue. */
/*
* The information below is used to provide read, write, and
* exception masks to select during calls to Tk_DoOneEvent.
*/
static fd_mask ready[3*MASK_SIZE];
/* Masks passed to select and modified
* by kernel to indicate which files are
* actually ready. */
static fd_mask check[3*MASK_SIZE];
/* Temporary set of masks, built up during
* Tk_DoOneEvent, that reflects what files
* we should wait for in the next select
* (doesn't include things that we've been
* asked to ignore in this call). */
static int numFds = 0; /* Number of valid bits in mask
* arrays (this value is passed
* to select). */
/*
* For each file registered in a call to Tk_CreateFileHandler,
* and for each display that's currently active, there is one
* record of the following type. All of these records are
* chained together into a single list.
*/
typedef struct FileHandler {
int fd; /* POSIX file descriptor for file. */
fd_mask *readPtr; /* Pointer to word in ready array
* for this file's read mask bit. */
fd_mask *writePtr; /* Same for write mask bit. */
fd_mask *exceptPtr; /* Same for except mask bit. */
fd_mask *checkReadPtr; /* Pointer to word in check array for
* this file's read mask bit. */
fd_mask *checkWritePtr; /* Same for write mask bit. */
fd_mask *checkExceptPtr; /* Same for except mask bit. */
fd_mask bitSelect; /* Value to AND with *readPtr etc. to
* select just this file's bit. */
int mask; /* Mask of desired events: TK_READABLE, etc. */
Tk_FileProc *proc; /* Procedure to call, in the style of
* Tk_CreateFileHandler. This is NULL
* if the handler was created by
* Tk_CreateFileHandler2. */
Tk_FileProc2 *proc2; /* Procedure to call, in the style of
* Tk_CreateFileHandler2. NULL means that
* the handler was created by
* Tk_CreateFileHandler. */
ClientData clientData; /* Argument to pass to proc. */
struct FileHandler *nextPtr;/* Next in list of all files we
* care about (NULL for end of
* list). */
} FileHandler;
static FileHandler *firstFileHandlerPtr;
/* List of all file events. */
/*
* There is one of the following structures for each of the
* handlers declared in a call to Tk_DoWhenIdle. All of the
* currently-active handlers are linked together into a list.
*/
typedef struct IdleHandler {
void (*proc) _ANSI_ARGS_((ClientData clientData));
/* Procedure to call. */
ClientData clientData; /* Value to pass to proc. */
int generation; /* Used to distinguish older handlers from
* recently-created ones. */
struct IdleHandler *nextPtr;/* Next in list of active handlers. */
} IdleHandler;
static IdleHandler *idleList = NULL;
/* First in list of all idle handlers. */
static IdleHandler *lastIdlePtr = NULL;
/* Last in list (or NULL for empty list). */
static int idleGeneration = 0; /* Used to fill in the "generation" fields
* of IdleHandler structures. Increments
* each time Tk_DoOneEvent starts calling
* idle handlers, so that all old handlers
* can be called without calling any of the
* new ones created by old ones. */
static int oldGeneration = 0; /* "generation" currently being handled. */
/*
* The following procedure provides a secret hook for tkXEvent.c so that
* it can handle delayed mouse motion events at the right time.
*/
void (*tkDelayedEventProc) _ANSI_ARGS_((void)) = NULL;
/*
* One of the following structures exists for each file with a handler
* created by the "fileevent" command. Several of the fields are
* two-element arrays, in which the first element is used for read
* events and the second for write events.
*/
typedef struct FileEvent {
FILE *f; /* Stdio handle for file. */
Tcl_Interp *interps[2]; /* Interpreters in which to execute
* scripts. NULL means no handler
* for event. */
char *scripts[2]; /* Scripts to evaluate in response to
* events (malloc'ed). NULL means no
* handler for event. */
struct FileEvent *nextPtr; /* Next in list of all file events
* currently defined. */
} FileEvent;
static FileEvent *firstFileEventPtr = NULL;
/* First in list of all existing
* file events. */
/*
* The data structure below is used by the "after" command to remember
* the command to be executed later.
*/
typedef struct AfterInfo {
Tcl_Interp *interp; /* Interpreter in which to execute command. */
char *command; /* Command to execute. Malloc'ed, so must
* be freed when structure is deallocated. */
int id; /* Integer identifier for command; used to
* cancel it. */
Tk_TimerToken token; /* Used to cancel the "after" command. NULL
* means that the command is run as an
* idle handler rather than as a timer
* handler. */
struct AfterInfo *nextPtr; /* Next in list of all "after" commands for
* the application. */
} AfterInfo;
static AfterInfo *firstAfterPtr = NULL;
/* First in list of all pending "after"
* commands. */
/*
* The data structure below is used to report background errors. One
* such structure is allocated for each error; it holds information
* about the interpreter and the error until tkerror can be invoked
* later as an idle handler.
*/
typedef struct BgError {
Tcl_Interp *interp; /* Interpreter in which error occurred. NULL
* means this error report has been cancelled
* (a previous report generated a break). */
char *errorMsg; /* The error message (interp->result when
* the error occurred). Malloc-ed. */
char *errorInfo; /* Value of the errorInfo variable
* (malloc-ed). */
char *errorCode; /* Value of the errorCode variable
* (malloc-ed). */
struct BgError *nextPtr; /* Next in list of all pending error
* reports. */
} BgError;
static BgError *firstBgPtr = NULL;
/* First in list of all background errors
* waiting to be processed (NULL if none). */
static BgError *lastBgPtr = NULL;
/* First in list of all background errors
* waiting to be processed (NULL if none). */
/*
* Prototypes for procedures referenced only in this file:
*/
static void AfterProc _ANSI_ARGS_((ClientData clientData));
static void DeleteFileEvent _ANSI_ARGS_((FILE *f));
static int FileEventProc _ANSI_ARGS_((ClientData clientData,
int mask, int flags));
static void FreeAfterPtr _ANSI_ARGS_((AfterInfo *afterPtr));
static void HandleBgErrors _ANSI_ARGS_((ClientData clientData));
static int TkwaitCmd2 _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int UpdateCmd2 _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static char * WaitVariableProc2 _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, char *name1, char *name2,
int flags));
/*
*--------------------------------------------------------------
*
* Tk_CreateFileHandler --
*
* Arrange for a given procedure to be invoked whenever
* a given file becomes readable or writable.
*
* Results:
* None.
*
* Side effects:
* From now on, whenever the I/O channel given by fd becomes
* ready in the way indicated by mask, proc will be invoked.
* See the manual entry for details on the calling sequence
* to proc. If fd is already registered then the old mask
* and proc and clientData values will be replaced with
* new ones.
*
*--------------------------------------------------------------
*/
void
Tk_CreateFileHandler(fd, mask, proc, clientData)
int fd; /* Integer identifier for stream. */
int mask; /* OR'ed combination of TK_READABLE,
* TK_WRITABLE, and TK_EXCEPTION:
* indicates conditions under which
* proc should be called. TK_IS_DISPLAY
* indicates that this is a display and that
* clientData is the (Display *) for it,
* and that events should be handled
* automatically.*/
Tk_FileProc *proc; /* Procedure to call for each
* selected event. */
ClientData clientData; /* Arbitrary data to pass to proc. */
{
register FileHandler *filePtr;
int index;
if (fd >= FD_SETSIZE) {
panic("Tk_CreatefileHandler can't handle file id %d", fd);
}
/*
* Make sure the file isn't already registered. Create a
* new record in the normal case where there's no existing
* record.
*/
for (filePtr = firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd == fd) {
break;
}
}
index = fd/(NBBY*sizeof(fd_mask));
if (filePtr == NULL) {
filePtr = (FileHandler *) ckalloc(sizeof(FileHandler));
filePtr->fd = fd;
filePtr->readPtr = &ready[index];
filePtr->writePtr = &ready[index+MASK_SIZE];
filePtr->exceptPtr = &ready[index+2*MASK_SIZE];
filePtr->checkReadPtr = &check[index];
filePtr->checkWritePtr = &check[index+MASK_SIZE];
filePtr->checkExceptPtr = &check[index+2*MASK_SIZE];
filePtr->bitSelect = 1 << (fd%(NBBY*sizeof(fd_mask)));
filePtr->nextPtr = firstFileHandlerPtr;
firstFileHandlerPtr = filePtr;
}
/*
* The remainder of the initialization below is done
* regardless of whether or not this is a new record
* or a modification of an old one.
*/
filePtr->mask = mask;
filePtr->proc = proc;
filePtr->proc2 = NULL;
filePtr->clientData = clientData;
if (numFds <= fd) {
numFds = fd+1;
}
}
/*
*--------------------------------------------------------------
*
* Tk_CreateFileHandler2 --
*
* Arrange for a given procedure to be invoked during the
* event loop to handle a particular file.
*
* Results:
* None.
*
* Side effects:
* In each pass through Tk_DoOneEvent, proc will be invoked to
* decide whether fd is "ready" and take appropriate action if
* it is. See the manual entry for details on the calling
* sequence to proc. If a handler for fd has already been
* registered then it is superseded by the new one.
*
*--------------------------------------------------------------
*/
void
Tk_CreateFileHandler2(fd, proc, clientData)
int fd; /* Integer identifier for stream. */
Tk_FileProc2 *proc; /* Procedure to call from the event
* dispatcher. */
ClientData clientData; /* Arbitrary data to pass to proc. */
{
register FileHandler *filePtr;
/*
* Let Tk_CreateFileHandler do all of the work of setting up
* the handler, then just modify things a bit after it returns.
*/
Tk_CreateFileHandler(fd, 0, (Tk_FileProc *) NULL, clientData);
for (filePtr = firstFileHandlerPtr; filePtr->fd != fd;
filePtr = filePtr->nextPtr) {
/* Empty loop body. */
}
filePtr->proc = NULL;
filePtr->proc2 = proc;
}
/*
*--------------------------------------------------------------
*
* Tk_DeleteFileHandler --
*
* Cancel a previously-arranged callback arrangement for
* a file.
*
* Results:
* None.
*
* Side effects:
* If a callback was previously registered on fd, remove it.
*
*--------------------------------------------------------------
*/
void
Tk_DeleteFileHandler(fd)
int fd; /* Stream id for which to remove
* callback procedure. */
{
register FileHandler *filePtr;
FileHandler *prevPtr;
/*
* Find the entry for the given file (and return if there
* isn't one).
*/
for (prevPtr = NULL, filePtr = firstFileHandlerPtr; ;
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
if (filePtr == NULL) {
return;
}
if (filePtr->fd == fd) {
break;
}
}
/*
* Clean up information in the callback record.
*/
if (prevPtr == NULL) {
firstFileHandlerPtr = filePtr->nextPtr;
} else {
prevPtr->nextPtr = filePtr->nextPtr;
}
ckfree((char *) filePtr);
/*
* Recompute numFds.
*/
numFds = 0;
for (filePtr = firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (numFds <= filePtr->fd) {
numFds = filePtr->fd+1;
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_CreateTimerHandler --
*
* Arrange for a given procedure to be invoked at a particular
* time in the future.
*
* Results:
* The return value is a token for the timer event, which
* may be used to delete the event before it fires.
*
* Side effects:
* When milliseconds have elapsed, proc will be invoked
* exactly once.
*
*--------------------------------------------------------------
*/
Tk_TimerToken
Tk_CreateTimerHandler(milliseconds, proc, clientData)
int milliseconds; /* How many milliseconds to wait
* before invoking proc. */
Tk_TimerProc *proc; /* Procedure to invoke. */
ClientData clientData; /* Arbitrary data to pass to proc. */
{
register TimerEvent *timerPtr, *tPtr2, *prevPtr;
static int id = 0;
timerPtr = (TimerEvent *) ckalloc(sizeof(TimerEvent));
/*
* Compute when the event should fire.
*/
(void) gettimeofday(&timerPtr->time, (struct timezone *) NULL);
timerPtr->time.tv_sec += milliseconds/1000;
timerPtr->time.tv_usec += (milliseconds%1000)*1000;
if (timerPtr->time.tv_usec >= 1000000) {
timerPtr->time.tv_usec -= 1000000;
timerPtr->time.tv_sec += 1;
}
/*
* Fill in other fields for the event.
*/
timerPtr->proc = proc;
timerPtr->clientData = clientData;
id++;
timerPtr->token = (Tk_TimerToken) id;
/*
* Add the event to the queue in the correct position
* (ordered by event firing time).
*/
for (tPtr2 = firstTimerHandlerPtr, prevPtr = NULL; tPtr2 != NULL;
prevPtr = tPtr2, tPtr2 = tPtr2->nextPtr) {
if ((tPtr2->time.tv_sec > timerPtr->time.tv_sec)
|| ((tPtr2->time.tv_sec == timerPtr->time.tv_sec)
&& (tPtr2->time.tv_usec > timerPtr->time.tv_usec))) {
break;
}
}
if (prevPtr == NULL) {
timerPtr->nextPtr = firstTimerHandlerPtr;
firstTimerHandlerPtr = timerPtr;
} else {
timerPtr->nextPtr = prevPtr->nextPtr;
prevPtr->nextPtr = timerPtr;
}
return timerPtr->token;
}
/*
*--------------------------------------------------------------
*
* Tk_DeleteTimerHandler --
*
* Delete a previously-registered timer handler.
*
* Results:
* None.
*
* Side effects:
* Destroy the timer callback identified by TimerToken,
* so that its associated procedure will not be called.
* If the callback has already fired, or if the given
* token doesn't exist, then nothing happens.
*
*--------------------------------------------------------------
*/
void
Tk_DeleteTimerHandler(token)
Tk_TimerToken token; /* Result previously returned by
* Tk_DeleteTimerHandler. */
{
register TimerEvent *timerPtr, *prevPtr;
for (timerPtr = firstTimerHandlerPtr, prevPtr = NULL; timerPtr != NULL;
prevPtr = timerPtr, timerPtr = timerPtr->nextPtr) {
if (timerPtr->token != token) {
continue;
}
if (prevPtr == NULL) {
firstTimerHandlerPtr = timerPtr->nextPtr;
} else {
prevPtr->nextPtr = timerPtr->nextPtr;
}
ckfree((char *) timerPtr);
return;
}
}
/*
*--------------------------------------------------------------
*
* Tk_DoWhenIdle --
*
* Arrange for proc to be invoked the next time the
* system is idle (i.e., just before the next time
* that Tk_DoOneEvent would have to wait for something
* to happen).
*
* Results:
* None.
*
* Side effects:
* Proc will eventually be called, with clientData
* as argument. See the manual entry for details.
*
*--------------------------------------------------------------
*/
void
Tk_DoWhenIdle(proc, clientData)
Tk_IdleProc *proc; /* Procedure to invoke. */
ClientData clientData; /* Arbitrary value to pass to proc. */
{
register IdleHandler *idlePtr;
idlePtr = (IdleHandler *) ckalloc(sizeof(IdleHandler));
idlePtr->proc = proc;
idlePtr->clientData = clientData;
idlePtr->generation = idleGeneration;
idlePtr->nextPtr = NULL;
if (lastIdlePtr == NULL) {
idleList = idlePtr;
} else {
lastIdlePtr->nextPtr = idlePtr;
}
lastIdlePtr = idlePtr;
}
/*
*--------------------------------------------------------------
*
* Tk_DoWhenIdle2 --
*
* Arrange for proc to be invoked when the system is idle
* (i.e., if currently idle or just before the next time
* that Tk_DoOneEvent would have to wait for something
* to happen).
*
* Results:
* None.
*
* Side effects:
* Proc will eventually be called, with clientData
* as argument. See the manual entry for details.
*
*--------------------------------------------------------------
*/
void
Tk_DoWhenIdle2(proc, clientData)
Tk_IdleProc *proc; /* Procedure to invoke. */
ClientData clientData; /* Arbitrary value to pass to proc. */
{
register IdleHandler *idlePtr;
idlePtr = (IdleHandler *) ckalloc(sizeof(IdleHandler));
idlePtr->proc = proc;
idlePtr->clientData = clientData;
idlePtr->generation = idleList == NULL ? oldGeneration :
idleList->generation;
idlePtr->nextPtr = NULL;
if (lastIdlePtr == NULL) {
idleList = idlePtr;
} else {
lastIdlePtr->nextPtr = idlePtr;
}
lastIdlePtr = idlePtr;
}
/*
*----------------------------------------------------------------------
*
* Tk_CancelIdleCall --
*
* If there are any when-idle calls requested to a given procedure
* with given clientData, cancel all of them.
*
* Results:
* None.
*
* Side effects:
* If the proc/clientData combination were on the when-idle list,
* they are removed so that they will never be called.
*
*----------------------------------------------------------------------
*/
void
Tk_CancelIdleCall(proc, clientData)
Tk_IdleProc *proc; /* Procedure that was previously registered. */
ClientData clientData; /* Arbitrary value to pass to proc. */
{
register IdleHandler *idlePtr, *prevPtr;
IdleHandler *nextPtr;
for (prevPtr = NULL, idlePtr = idleList; idlePtr != NULL;
prevPtr = idlePtr, idlePtr = idlePtr->nextPtr) {
while ((idlePtr->proc == proc)
&& (idlePtr->clientData == clientData)) {
nextPtr = idlePtr->nextPtr;
ckfree((char *) idlePtr);
idlePtr = nextPtr;
if (prevPtr == NULL) {
idleList = idlePtr;
} else {
prevPtr->nextPtr = idlePtr;
}
if (idlePtr == NULL) {
lastIdlePtr = prevPtr;
return;
}
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_DoOneEvent --
*
* Process a single event of some sort. If there's no
* work to do, wait for an event to occur, then process
* it.
*
* Results:
* The return value is 1 if the procedure actually found
* an event to process. If no event was found then 0 is
* returned.
*
* Side effects:
* May delay execution of process while waiting for an
* X event, X error, file-ready event, or timer event.
* The handling of the event could cause additional
* side effects. Collapses sequences of mouse-motion
* events for the same window into a single event by
* delaying motion event processing.
*
*--------------------------------------------------------------
*/
int
Tk_DoOneEvent(flags)
int flags; /* Miscellaneous flag values: may be any
* combination of TK_DONT_WAIT, TK_X_EVENTS,
* TK_FILE_EVENTS, TK_TIMER_EVENTS, and
* TK_IDLE_EVENTS. */
{
register FileHandler *filePtr;
struct timeval curTime, timeoutVal, *timeoutPtr;
int numFound, mask, anyFilesToWaitFor;
if ((flags & TK_ALL_EVENTS) == 0) {
flags |= TK_ALL_EVENTS;
}
/*
* Phase One: see if there's a ready file that was left over
* from before (i.e don't do a select, just check the bits from
* the last select).
*/
checkFiles:
if (tcl_AsyncReady) {
(void) Tcl_AsyncInvoke((Tcl_Interp *) NULL, 0);
return 1;
}
memset((VOID *) check, 0, 3*MASK_SIZE*sizeof(fd_mask));
anyFilesToWaitFor = 0;
for (filePtr = firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
mask = 0;
if (*filePtr->readPtr & filePtr->bitSelect) {
mask |= TK_READABLE;
*filePtr->readPtr &= ~filePtr->bitSelect;
}
if (*filePtr->writePtr & filePtr->bitSelect) {
mask |= TK_WRITABLE;
*filePtr->writePtr &= ~filePtr->bitSelect;
}
if (*filePtr->exceptPtr & filePtr->bitSelect) {
mask |= TK_EXCEPTION;
*filePtr->exceptPtr &= ~filePtr->bitSelect;
}
if (filePtr->proc2 != NULL) {
/*
* Handler created by Tk_CreateFileHandler2.
*/
mask = (*filePtr->proc2)(filePtr->clientData, mask, flags);
if (mask == TK_FILE_HANDLED) {
return 1;
}
} else {
/*
* Handler created by Tk_CreateFileHandler.
*/
if (!(flags & TK_FILE_EVENTS)) {
continue;
}
if (mask != 0) {
(*filePtr->proc)(filePtr->clientData, mask);
return 1;
}
mask = filePtr->mask;
}
if (mask != 0) {
anyFilesToWaitFor = 1;
if (mask & TK_READABLE) {
*filePtr->checkReadPtr |= filePtr->bitSelect;
}
if (mask & TK_WRITABLE) {
*filePtr->checkWritePtr |= filePtr->bitSelect;
}
if (mask & TK_EXCEPTION) {
*filePtr->checkExceptPtr |= filePtr->bitSelect;
}
}
}
/*
* Phase Two: get the current time and see if any timer
* events are ready to fire. If so, fire one and return.
*/
checkTime:
if ((firstTimerHandlerPtr != NULL) && (flags & TK_TIMER_EVENTS)) {
register TimerEvent *timerPtr = firstTimerHandlerPtr;
(void) gettimeofday(&curTime, (struct timezone *) NULL);
if ((timerPtr->time.tv_sec < curTime.tv_sec)
|| ((timerPtr->time.tv_sec == curTime.tv_sec)
&& (timerPtr->time.tv_usec < curTime.tv_usec))) {
firstTimerHandlerPtr = timerPtr->nextPtr;
(*timerPtr->proc)(timerPtr->clientData);
ckfree((char *) timerPtr);
return 1;
}
}
/*
* Phase Three: if there are DoWhenIdle requests pending (or
* if we're not allowed to block), then do a select with an
* instantaneous timeout. If a ready file is found, then go
* back to process it.
*/
if (((idleList != NULL) && (flags & TK_IDLE_EVENTS))
|| (flags & TK_DONT_WAIT)) {
if (flags & (TK_X_EVENTS|TK_FILE_EVENTS)) {
memcpy((VOID *) ready, (VOID *) check,
3*MASK_SIZE*sizeof(fd_mask));
timeoutVal.tv_sec = timeoutVal.tv_usec = 0;
numFound = select(numFds, (SELECT_MASK *) &ready[0],
(SELECT_MASK *) &ready[MASK_SIZE],
(SELECT_MASK *) &ready[2*MASK_SIZE], &timeoutVal);
if (numFound <= 0) {
/*
* Some systems don't clear the masks after an error, so
* we have to do it here.
*/
memset((VOID *) ready, 0, 3*MASK_SIZE*sizeof(fd_mask));
}
if ((numFound > 0) || ((numFound == -1) && (errno == EINTR))) {
goto checkFiles;
}
}
}
/*
* Phase Four: if there is a delayed motion event then call a procedure
* to handle it. Do it now, before calling any DoWhenIdle handlers,
* since the goal of idle handlers is to delay until after all pending
* events have been processed.
*
* The particular implementation of this (a procedure variable shared
* with tkXEvent.c) is a bit kludgy, but it allows this file to be used
* separately without any of the rest of Tk.
*/
if ((tkDelayedEventProc != NULL) && (flags & TK_X_EVENTS)) {
(*tkDelayedEventProc)();
return 1;
}
/*
* Phase Five: process all pending DoWhenIdle requests.
*/
if ((idleList != NULL) && (flags & TK_IDLE_EVENTS)) {
register IdleHandler *idlePtr;
int myGeneration;
oldGeneration = myGeneration = idleList->generation;
idleGeneration++;
/*
* The code below is trickier than it may look, for the following
* reasons:
*
* 1. New handlers can get added to the list while the current
* one is being processed. If new ones get added, we don't
* want to process them during this pass through the list (want
* to check for other work to do first). This is implemented
* using the generation number in the handler: new handlers
* will have a different generation than any of the ones currently
* on the list.
* 2. The handler can call Tk_DoOneEvent, so we have to remove
* the hander from the list before calling it. Otherwise an
* infinite loop could result.
* 3. Tk_CancelIdleCall can be called to remove an element from
* the list while a handler is executing, so the list could
* change structure during the call.
*/
for (idlePtr = idleList;
((idlePtr != NULL) && (idlePtr->generation == myGeneration));
idlePtr = idleList) {
idleList = idlePtr->nextPtr;
if (idleList == NULL) {
lastIdlePtr = NULL;
}
(*idlePtr->proc)(idlePtr->clientData);
ckfree((char *) idlePtr);
}
return 1;
}
/*
* Phase Six: do a select to wait for either one of the
* files to become ready or for the first timer event to
* fire. Then go back to process the event.
*/
if ((flags & TK_DONT_WAIT)
|| !(flags & (TK_TIMER_EVENTS|TK_FILE_EVENTS|TK_X_EVENTS))) {
return 0;
}
if ((firstTimerHandlerPtr == NULL) || !(flags & TK_TIMER_EVENTS)) {
timeoutPtr = NULL;
} else {
timeoutPtr = &timeoutVal;
timeoutVal.tv_sec = firstTimerHandlerPtr->time.tv_sec
- curTime.tv_sec;
timeoutVal.tv_usec = firstTimerHandlerPtr->time.tv_usec
- curTime.tv_usec;
if (timeoutVal.tv_usec < 0) {
timeoutVal.tv_sec -= 1;
timeoutVal.tv_usec += 1000000;
}
}
if ((timeoutPtr == NULL) && !anyFilesToWaitFor) {
return 0;
}
memcpy((VOID *) ready, (VOID *) check, 3*MASK_SIZE*sizeof(fd_mask));
numFound = select(numFds, (SELECT_MASK *) &ready[0],
(SELECT_MASK *) &ready[MASK_SIZE],
(SELECT_MASK *) &ready[2*MASK_SIZE], timeoutPtr);
if (numFound == -1) {
/*
* Some systems don't clear the masks after an error, so
* we have to do it here.
*/
memset((VOID *) ready, 0, 3*MASK_SIZE*sizeof(fd_mask));
}
if (numFound == 0) {
goto checkTime;
}
goto checkFiles;
}
/*
*----------------------------------------------------------------------
*
* Tk_Sleep --
*
* Delay execution for the specified number of milliseconds.
*
* Results:
* None.
*
* Side effects:
* Time passes.
*
*----------------------------------------------------------------------
*/
void
Tk_Sleep(ms)
int ms; /* Number of milliseconds to sleep. */
{
static struct timeval delay;
delay.tv_sec = ms/1000;
delay.tv_usec = (ms%1000)*1000;
(void) select(0, (SELECT_MASK *) 0, (SELECT_MASK *) 0,
(SELECT_MASK *) 0, &delay);
}
/*
*----------------------------------------------------------------------
*
* Tk_BackgroundError --
*
* This procedure is invoked to handle errors that occur in Tcl
* commands that are invoked in "background" (e.g. from event or
* timer bindings).
*
* Results:
* None.
*
* Side effects:
* The command "tkerror" is invoked later as an idle handler to
* process the error, passing it the error message. If that fails,
* then an error message is output on stderr.
*
*----------------------------------------------------------------------
*/
void
Tk_BackgroundError(interp)
Tcl_Interp *interp; /* Interpreter in which an error has
* occurred. */
{
BgError *errPtr;
char *varValue;
/*
* The Tcl_AddErrorInfo call below (with an empty string) ensures that
* errorInfo gets properly set. It's needed in cases where the error
* came from a utility procedure like Tcl_GetVar instead of Tcl_Eval;
* in these cases errorInfo still won't have been set when this
* procedure is called.
*/
Tcl_AddErrorInfo(interp, "");
errPtr = (BgError *) ckalloc(sizeof(BgError));
errPtr->interp = interp;
errPtr->errorMsg = (char *) ckalloc((unsigned) (strlen(interp->result)
+ 1));
strcpy(errPtr->errorMsg, interp->result);
varValue = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
if (varValue == NULL) {
varValue = errPtr->errorMsg;
}
errPtr->errorInfo = (char *) ckalloc((unsigned) (strlen(varValue) + 1));
strcpy(errPtr->errorInfo, varValue);
varValue = Tcl_GetVar(interp, "errorCode", TCL_GLOBAL_ONLY);
if (varValue == NULL) {
varValue = "";
}
errPtr->errorCode = (char *) ckalloc((unsigned) (strlen(varValue) + 1));