-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkEvent.c
More file actions
2103 lines (1873 loc) · 56.2 KB
/
Copy pathtkEvent.c
File metadata and controls
2103 lines (1873 loc) · 56.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 low-level facilities for managing X events in
* Tk.
*
* Copyright © 1990-1994 The Regents of the University of California.
* Copyright © 1994-1995 Sun Microsystems, Inc.
* Copyright © 1998-2000 Ajuba Solutions.
* Copyright © 2004 George Peter Staplin
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#ifdef _WIN32
#include "tkWinInt.h"
#elif defined(MAC_OSX_TK)
#include "tkMacOSXInt.h"
#else
#include "tkUnixInt.h"
#endif
/*
* There's a potential problem if a handler is deleted while it's current
* (i.e. its function is executing), since Tk_HandleEvent will need to read
* the handler's "nextPtr" field when the function returns. To handle this
* problem, structures of the type below indicate the next handler to be
* processed for any (recursively nested) dispatches in progress. The
* nextHandler fields get updated if the handlers pointed to are deleted.
* Tk_HandleEvent also needs to know if the entire window gets deleted; the
* winPtr field is set to zero if that particular window gets deleted.
*/
typedef struct InProgress {
XEvent *eventPtr; /* Event currently being handled. */
TkWindow *winPtr; /* Window for event. Gets set to None if
* window is deleted while event is being
* handled. */
TkEventHandler *nextHandler;/* Next handler in search. */
struct InProgress *nextPtr; /* Next higher nested search. */
} InProgress;
/*
* For each call to Tk_CreateGenericHandler or Tk_CreateClientMessageHandler,
* an instance of the following structure will be created. All of the active
* handlers are linked into a list.
*/
typedef struct GenericHandler {
Tk_GenericProc *proc; /* Function to dispatch on all X events. */
void *clientData; /* Client data to pass to function. */
int deleteFlag; /* Flag to set when this handler is
* deleted. */
struct GenericHandler *nextPtr;
/* Next handler in list of all generic
* handlers, or NULL for end of list. */
} GenericHandler;
/*
* There's a potential problem if Tk_HandleEvent is entered recursively. A
* handler cannot be deleted physically until we have returned from calling
* it. Otherwise, we're looking at unallocated memory in advancing to its
* `next' entry. We deal with the problem by using the `delete flag' and
* deleting handlers only when it's known that there's no handler active.
*/
/*
* The following structure is used for queueing X-style events on the Tcl
* event queue.
*/
typedef struct TkWindowEvent {
Tcl_Event header; /* Standard information for all events. */
XEvent event; /* The X event. */
} TkWindowEvent;
/*
* Array of event masks corresponding to each X event:
*/
static const unsigned long eventMasks[TK_LASTEVENT] = {
0,
0,
KeyPressMask, /* KeyPress */
KeyReleaseMask, /* KeyRelease */
ButtonPressMask, /* ButtonPress */
ButtonReleaseMask, /* ButtonRelease */
PointerMotionMask|PointerMotionHintMask|ButtonMotionMask
|Button1MotionMask|Button2MotionMask|Button3MotionMask
|Button4MotionMask|Button5MotionMask,
/* MotionNotify */
EnterWindowMask, /* EnterNotify */
LeaveWindowMask, /* LeaveNotify */
FocusChangeMask, /* FocusIn */
FocusChangeMask, /* FocusOut */
KeymapStateMask, /* KeymapNotify */
ExposureMask, /* Expose */
ExposureMask, /* GraphicsExpose */
ExposureMask, /* NoExpose */
VisibilityChangeMask, /* VisibilityNotify */
SubstructureNotifyMask, /* CreateNotify */
StructureNotifyMask, /* DestroyNotify */
StructureNotifyMask, /* UnmapNotify */
StructureNotifyMask, /* MapNotify */
SubstructureRedirectMask, /* MapRequest */
StructureNotifyMask, /* ReparentNotify */
StructureNotifyMask, /* ConfigureNotify */
SubstructureRedirectMask, /* ConfigureRequest */
StructureNotifyMask, /* GravityNotify */
ResizeRedirectMask, /* ResizeRequest */
StructureNotifyMask, /* CirculateNotify */
SubstructureRedirectMask, /* CirculateRequest */
PropertyChangeMask, /* PropertyNotify */
0, /* SelectionClear */
0, /* SelectionRequest */
0, /* SelectionNotify */
ColormapChangeMask, /* ColormapNotify */
0, /* ClientMessage */
0, /* Mapping Notify */
VirtualEventMask, /* VirtualEvents */
ActivateMask, /* ActivateNotify */
ActivateMask, /* DeactivateNotify */
MouseWheelMask, /* MouseWheelEvent */
TouchpadScrollMask /* TouchpadScroll */
};
/*
* For each exit handler created with a call to TkCreateExitHandler or
* TkCreateThreadExitHandler there is a structure of the following type:
*/
typedef struct ExitHandler {
Tcl_ExitProc *proc; /* Function to call when process exits. */
void *clientData; /* One word of information to pass to proc. */
struct ExitHandler *nextPtr;/* Next in list of all exit handlers for this
* application, or NULL for end of list. */
} ExitHandler;
/*
* The structure below is used to store Data for the Event module that must be
* kept thread-local. The "dataKey" is used to fetch the thread-specific
* storage for the current thread.
*/
typedef struct {
int handlersActive; /* The following variable has a non-zero value
* when a handler is active. */
InProgress *pendingPtr; /* Topmost search in progress, or NULL if
* none. */
/*
* List of generic handler records.
*/
GenericHandler *genericList;/* First handler in the list, or NULL. */
GenericHandler *lastGenericPtr;
/* Last handler in list. */
/*
* List of client message handler records.
*/
GenericHandler *cmList; /* First handler in the list, or NULL. */
GenericHandler *lastCmPtr; /* Last handler in list. */
/*
* If someone has called Tk_RestrictEvents, the information below keeps
* track of it.
*/
Tk_RestrictProc *restrictProc;
/* Function to call. NULL means no
* restrictProc is currently in effect. */
void *restrictArg; /* Argument to pass to restrictProc. */
ExitHandler *firstExitPtr; /* First in list of all exit handlers for this
* thread. */
int inExit; /* True when this thread is exiting. This is
* used as a hack to decide to close the
* standard channels. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* There are both per-process and per-thread exit handlers. The first list is
* controlled by a mutex. The other is in thread local storage.
*/
static ExitHandler *firstExitPtr = NULL;
/* First in list of all exit handlers for
* application. */
TCL_DECLARE_MUTEX(exitMutex)
/*
* Prototypes for functions that are only referenced locally within this file.
*/
static void CleanUpTkEvent(XEvent *eventPtr);
static void DelayedMotionProc(void *clientData);
static unsigned long GetEventMaskFromXEvent(XEvent *eventPtr);
static TkWindow * GetTkWindowFromXEvent(XEvent *eventPtr);
static void InvokeClientMessageHandlers(ThreadSpecificData *tsdPtr,
Tk_Window tkwin, XEvent *eventPtr);
static int InvokeFocusHandlers(TkWindow **winPtrPtr,
unsigned long mask, XEvent *eventPtr);
static int InvokeGenericHandlers(ThreadSpecificData *tsdPtr,
XEvent *eventPtr);
static int InvokeMouseHandlers(TkWindow *winPtr,
unsigned long mask, XEvent *eventPtr);
static Window ParentXId(Display *display, Window w);
static int RefreshKeyboardMappingIfNeeded(XEvent *eventPtr);
static int TkXErrorHandler(void *clientData,
XErrorEvent *errEventPtr);
static int WindowEventProc(Tcl_Event *evPtr, int flags);
static void CreateXIC(TkWindow *winPtr);
/*
*----------------------------------------------------------------------
*
* InvokeFocusHandlers --
*
* Call focus-related code to look at FocusIn, FocusOut, Enter, and Leave
* events; depending on its return value, ignore the event.
*
* Results:
* 0 further processing can be done on the event.
* 1 we are done with the event passed.
*
* Side effects:
* The *winPtrPtr in the caller may be changed to the TkWindow for the
* window with focus.
*
*----------------------------------------------------------------------
*/
static int
InvokeFocusHandlers(
TkWindow **winPtrPtr,
unsigned long mask,
XEvent *eventPtr)
{
if ((mask & (FocusChangeMask|EnterWindowMask|LeaveWindowMask))
&& (TkFocusFilterEvent(*winPtrPtr, eventPtr) == 0)) {
return 1;
}
/*
* Only key-related events are directed according to the focus.
*/
if (mask & (KeyPressMask|KeyReleaseMask)) {
(*winPtrPtr)->dispPtr->lastEventTime = eventPtr->xkey.time;
*winPtrPtr = TkFocusKeyEvent(*winPtrPtr, eventPtr);
if (*winPtrPtr == NULL) {
return 1;
}
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* InvokeMouseHandlers --
*
* Call a grab-related function to do special processing on pointer
* events.
*
* Results:
* 0 further processing can be done on the event.
* 1 we are done with the event passed.
*
* Side effects:
* New events may be queued from TkPointerEvent and grabs may be added
* and/or removed. The eventPtr may be changed by TkPointerEvent in some
* cases.
*
*----------------------------------------------------------------------
*/
static int
InvokeMouseHandlers(
TkWindow *winPtr,
unsigned long mask,
XEvent *eventPtr)
{
if (mask & (ButtonPressMask|ButtonReleaseMask|PointerMotionMask
|EnterWindowMask|LeaveWindowMask)) {
if (mask & (ButtonPressMask|ButtonReleaseMask)) {
winPtr->dispPtr->lastEventTime = eventPtr->xbutton.time;
} else if (mask & PointerMotionMask) {
winPtr->dispPtr->lastEventTime = eventPtr->xmotion.time;
} else {
winPtr->dispPtr->lastEventTime = eventPtr->xcrossing.time;
}
if (TkPointerEvent(eventPtr, winPtr) == 0) {
/*
* The event should be ignored to make grab work correctly (as the
* comment for TkPointerEvent states).
*/
return 1;
}
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* CreateXIC --
*
* Create the X input context for our winPtr.
* XIM is only ever enabled on Unix.
*
*----------------------------------------------------------------------
*/
static void
CreateXIC(
TkWindow *winPtr)
{
TkDisplay *dispPtr = winPtr->dispPtr;
long im_event_mask = 0L;
const char *preedit_attname = NULL;
XVaNestedList preedit_attlist = NULL;
if (dispPtr->inputStyle & XIMPreeditPosition) {
XPoint spot = {0, 0};
preedit_attname = XNPreeditAttributes;
preedit_attlist = XVaCreateNestedList(0,
XNSpotLocation, &spot,
XNFontSet, dispPtr->inputXfs,
(char *)NULL);
}
winPtr->inputContext = XCreateIC(dispPtr->inputMethod,
XNInputStyle, dispPtr->inputStyle,
XNClientWindow, winPtr->window,
XNFocusWindow, winPtr->window,
preedit_attname, preedit_attlist,
(char *)NULL);
if (preedit_attlist) {
XFree(preedit_attlist);
}
if (winPtr->inputContext == NULL) {
/* XCreateIC failed. */
return;
}
winPtr->ximGeneration = dispPtr->ximGeneration;
/*
* Adjust the window's event mask if the IM requires it.
*/
XGetICValues(winPtr->inputContext, XNFilterEvents, &im_event_mask, (char *)NULL);
if ((winPtr->atts.event_mask & im_event_mask) != im_event_mask) {
winPtr->atts.event_mask |= im_event_mask;
XSelectInput(winPtr->display, winPtr->window, winPtr->atts.event_mask);
}
}
/*
*----------------------------------------------------------------------
*
* GetTkWindowFromXEvent --
*
* Attempt to find which TkWindow is associated with an event. If it
* fails we attempt to get the TkWindow from the parent for a property
* notification.
*
* Results:
* The TkWindow associated with the event or NULL.
*
* Side effects:
* TkSelPropProc may influence selection on windows not known to Tk.
*
*----------------------------------------------------------------------
*/
static TkWindow *
GetTkWindowFromXEvent(
XEvent *eventPtr)
{
TkWindow *winPtr;
Window parentXId, handlerWindow = eventPtr->xany.window;
if ((eventPtr->xany.type == StructureNotifyMask)
&& (eventPtr->xmap.event != eventPtr->xmap.window)) {
handlerWindow = eventPtr->xmap.event;
}
winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, handlerWindow);
if (winPtr == NULL) {
/*
* There isn't a TkWindow structure for this window. However, if the
* event is a PropertyNotify event then call the selection manager (it
* deals beneath-the-table with certain properties). Also, if the
* window's parent is a Tk window that has the TK_PROP_PROPCHANGE flag
* set, then we must propagate the PropertyNotify event up to the
* parent.
*/
if (eventPtr->type != PropertyNotify) {
return NULL;
}
TkSelPropProc(eventPtr);
parentXId = ParentXId(eventPtr->xany.display, handlerWindow);
if (parentXId == None) {
return NULL;
}
winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, parentXId);
if (winPtr == NULL) {
return NULL;
}
if (!(winPtr->flags & TK_PROP_PROPCHANGE)) {
return NULL;
}
}
return winPtr;
}
/*
*----------------------------------------------------------------------
*
* GetEventMaskFromXEvent --
*
* The event type is looked up in our eventMasks tables, and may be
* changed to a different mask depending on the state of the event and
* window members.
*
* Results:
* The mask for the event.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static unsigned long
GetEventMaskFromXEvent(
XEvent *eventPtr)
{
unsigned long mask;
if (eventPtr->xany.type <TK_LASTEVENT) {
mask = eventMasks[eventPtr->xany.type];
} else {
mask = 0;
}
/*
* Events selected by StructureNotify require special handling. They look
* the same as those selected by SubstructureNotify. The only difference
* is whether the "event" and "window" fields are the same. Compare the
* two fields and convert StructureNotify to SubstructureNotify if
* necessary.
*/
if (mask == StructureNotifyMask) {
if (eventPtr->xmap.event != eventPtr->xmap.window) {
mask = SubstructureNotifyMask;
}
}
return mask;
}
/*
*----------------------------------------------------------------------
*
* RefreshKeyboardMappingIfNeeded --
*
* If the event is a MappingNotify event, find its display and refresh
* the keyboard mapping information for the display.
*
* Results:
* 0 if the event was not a MappingNotify event
* 1 if the event was a MappingNotify event
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
RefreshKeyboardMappingIfNeeded(
XEvent *eventPtr)
{
TkDisplay *dispPtr;
if (eventPtr->type == MappingNotify) {
dispPtr = TkGetDisplay(eventPtr->xmapping.display);
if (dispPtr != NULL) {
XRefreshKeyboardMapping(&eventPtr->xmapping);
dispPtr->bindInfoStale = 1;
}
return 1;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* Tk_GetButtonMask --
*
* Return the proper Button${n}Mask for the button. Don't care about
* Button4 - Button7, because those are not actually buttons: Those
* are used for the horizontal or vertical mouse wheels. Button4Mask
* and higher is actually used for Button 8 and higher.
*
* Results:
* A button mask.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static const unsigned buttonMasks[] = {
0, Button1Mask, Button2Mask, Button3Mask, 0, 0, 0, 0, Button4Mask, \
Button5Mask, Button6Mask, Button7Mask, Button8Mask, Button9Mask
};
unsigned
Tk_GetButtonMask(
unsigned button)
{
return (button > Button9) ? 0 : buttonMasks[button];
}
/*
*----------------------------------------------------------------------
*
* InvokeClientMessageHandlers --
*
* Iterate the list of handlers and invoke the function pointer for each.
*
* Results:
* None.
*
* Side effects:
* Handlers may be deleted and events may be sent to handlers.
*
*----------------------------------------------------------------------
*/
static void
InvokeClientMessageHandlers(
ThreadSpecificData *tsdPtr,
Tk_Window tkwin,
XEvent *eventPtr)
{
GenericHandler *prevPtr, *tmpPtr, *curPtr = tsdPtr->cmList;
for (prevPtr = NULL; curPtr != NULL; ) {
if (curPtr->deleteFlag) {
if (!tsdPtr->handlersActive) {
/*
* This handler needs to be deleted and there are no calls
* pending through any handlers, so now is a safe time to
* delete it.
*/
tmpPtr = curPtr->nextPtr;
if (prevPtr == NULL) {
tsdPtr->cmList = tmpPtr;
} else {
prevPtr->nextPtr = tmpPtr;
}
if (tmpPtr == NULL) {
tsdPtr->lastCmPtr = prevPtr;
}
ckfree(curPtr);
curPtr = tmpPtr;
continue;
}
} else {
int done;
tsdPtr->handlersActive++;
done = (*(Tk_ClientMessageProc *)curPtr->proc)(tkwin, eventPtr);
tsdPtr->handlersActive--;
if (done) {
break;
}
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
}
/*
*----------------------------------------------------------------------
*
* InvokeGenericHandlers --
*
* Iterate the list of handlers and invoke the function pointer for each.
* If the handler invoked returns a non-zero value then we are done.
*
* Results:
* 0 when the event wasn't handled by a handler. Non-zero when it was
* processed and handled by a handler.
*
* Side effects:
* Handlers may be deleted and events may be sent to handlers.
*
*----------------------------------------------------------------------
*/
static int
InvokeGenericHandlers(
ThreadSpecificData *tsdPtr,
XEvent *eventPtr)
{
GenericHandler *prevPtr, *tmpPtr, *curPtr = tsdPtr->genericList;
for (prevPtr = NULL; curPtr != NULL; ) {
if (curPtr->deleteFlag) {
if (!tsdPtr->handlersActive) {
/*
* This handler needs to be deleted and there are no calls
* pending through the handler, so now is a safe time to
* delete it.
*/
tmpPtr = curPtr->nextPtr;
if (prevPtr == NULL) {
tsdPtr->genericList = tmpPtr;
} else {
prevPtr->nextPtr = tmpPtr;
}
if (tmpPtr == NULL) {
tsdPtr->lastGenericPtr = prevPtr;
}
ckfree(curPtr);
curPtr = tmpPtr;
continue;
}
} else {
int done;
tsdPtr->handlersActive++;
done = curPtr->proc(curPtr->clientData, eventPtr);
tsdPtr->handlersActive--;
if (done) {
return done;
}
}
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* Tk_CreateEventHandler --
*
* Arrange for a given function to be invoked whenever events from a
* given class occur in a given window.
*
* Results:
* None.
*
* Side effects:
* From now on, whenever an event of the type given by mask occurs for
* token and is processed by Tk_HandleEvent, proc will be called. See the
* manual entry for details of the calling sequence and return value for
* proc.
*
*----------------------------------------------------------------------
*/
void
Tk_CreateEventHandler(
Tk_Window token, /* Token for window in which to create
* handler. */
unsigned long mask, /* Events for which proc should be called. */
Tk_EventProc *proc, /* Function to call for each selected event */
void *clientData) /* Arbitrary data to pass to proc. */
{
TkEventHandler *handlerPtr;
TkWindow *winPtr = (TkWindow *)token;
/*
* Skim through the list of existing handlers to (a) compute the overall
* event mask for the window (so we can pass this new value to the X
* system) and (b) see if there's already a handler declared with the same
* callback and clientData (if so, just change the mask). If no existing
* handler matches, then create a new handler.
*/
if (winPtr->handlerList == NULL) {
/*
* No event handlers defined at all, so must create.
*/
handlerPtr = (TkEventHandler *)ckalloc(sizeof(TkEventHandler));
winPtr->handlerList = handlerPtr;
} else {
int found = 0;
for (handlerPtr = winPtr->handlerList; ;
handlerPtr = handlerPtr->nextPtr) {
if ((handlerPtr->proc == proc)
&& (handlerPtr->clientData == clientData)) {
handlerPtr->mask = mask;
found = 1;
}
if (handlerPtr->nextPtr == NULL) {
break;
}
}
/*
* If we found anything, we're done because we do not need to use
* XSelectInput; Tk always selects on all events anyway in order to
* support binding on classes, 'all' and other bind-tags.
*/
if (found) {
return;
}
/*
* No event handler matched, so create a new one.
*/
handlerPtr->nextPtr = (TkEventHandler *)ckalloc(sizeof(TkEventHandler));
handlerPtr = handlerPtr->nextPtr;
}
/*
* Initialize the new event handler.
*/
handlerPtr->mask = mask;
handlerPtr->proc = proc;
handlerPtr->clientData = clientData;
handlerPtr->nextPtr = NULL;
/*
* No need to call XSelectInput: Tk always selects on all events for all
* windows (needed to support bindings on classes and "all").
*/
}
/*
*----------------------------------------------------------------------
*
* Tk_DeleteEventHandler --
*
* Delete a previously-created handler.
*
* Results:
* None.
*
* Side effects:
* If there existed a handler as described by the parameters, the handler
* is deleted so that proc will not be invoked again.
*
*----------------------------------------------------------------------
*/
void
Tk_DeleteEventHandler(
Tk_Window token, /* Same as corresponding arguments passed */
unsigned long mask, /* previously to Tk_CreateEventHandler. */
Tk_EventProc *proc,
void *clientData)
{
TkEventHandler *handlerPtr;
InProgress *ipPtr;
TkEventHandler *prevPtr;
TkWindow *winPtr = (TkWindow *) token;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* Find the event handler to be deleted, or return immediately if it
* doesn't exist.
*/
for (handlerPtr = winPtr->handlerList, prevPtr = NULL; ;
prevPtr = handlerPtr, handlerPtr = handlerPtr->nextPtr) {
if (handlerPtr == NULL) {
return;
}
if ((handlerPtr->mask == mask) && (handlerPtr->proc == proc)
&& (handlerPtr->clientData == clientData)) {
break;
}
}
/*
* If Tk_HandleEvent is about to process this handler, tell it to process
* the next one instead.
*/
for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) {
if (ipPtr->nextHandler == handlerPtr) {
ipPtr->nextHandler = handlerPtr->nextPtr;
}
}
/*
* Free resources associated with the handler.
*/
if (prevPtr == NULL) {
winPtr->handlerList = handlerPtr->nextPtr;
} else {
prevPtr->nextPtr = handlerPtr->nextPtr;
}
ckfree(handlerPtr);
/*
* No need to call XSelectInput: Tk always selects on all events for all
* windows (needed to support bindings on classes and "all").
*/
}
/*----------------------------------------------------------------------
*
* Tk_CreateGenericHandler --
*
* Register a function to be called on each X event, regardless of
* display or window. Generic handlers are useful for capturing events
* that aren't associated with windows, or events for windows not managed
* by Tk.
*
* Results:
* None.
*
* Side Effects:
* From now on, whenever an X event is given to Tk_HandleEvent, invoke
* proc, giving it clientData and the event as arguments.
*
*----------------------------------------------------------------------
*/
void
Tk_CreateGenericHandler(
Tk_GenericProc *proc, /* Function to call on every event. */
void *clientData) /* One-word value to pass to proc. */
{
GenericHandler *handlerPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
handlerPtr = (GenericHandler *)ckalloc(sizeof(GenericHandler));
handlerPtr->proc = proc;
handlerPtr->clientData = clientData;
handlerPtr->deleteFlag = 0;
handlerPtr->nextPtr = NULL;
if (tsdPtr->genericList == NULL) {
tsdPtr->genericList = handlerPtr;
} else {
tsdPtr->lastGenericPtr->nextPtr = handlerPtr;
}
tsdPtr->lastGenericPtr = handlerPtr;
}
/*
*----------------------------------------------------------------------
*
* Tk_DeleteGenericHandler --
*
* Delete a previously-created generic handler.
*
* Results:
* None.
*
* Side Effects:
* If there existed a handler as described by the parameters, that
* handler is logically deleted so that proc will not be invoked again.
* The physical deletion happens in the event loop in Tk_HandleEvent.
*
*----------------------------------------------------------------------
*/
void
Tk_DeleteGenericHandler(
Tk_GenericProc *proc,
void *clientData)
{
GenericHandler * handler;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
for (handler=tsdPtr->genericList ; handler ; handler=handler->nextPtr) {
if ((handler->proc == proc) && (handler->clientData == clientData)) {
handler->deleteFlag = 1;
}
}
}
/*----------------------------------------------------------------------
*
* Tk_CreateClientMessageHandler --
*
* Register a function to be called on each ClientMessage event.
* ClientMessage handlers are useful for Drag&Drop extensions.
*
* Results:
* None.
*
* Side Effects:
* From now on, whenever a ClientMessage event is received that isn't a
* WM_PROTOCOL event or SelectionEvent, invoke proc, giving it tkwin and
* the event as arguments.
*
*----------------------------------------------------------------------
*/
void
Tk_CreateClientMessageHandler(
Tk_ClientMessageProc *proc) /* Function to call on event. */
{
GenericHandler *handlerPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* We use a GenericHandler struct, because it's basically the same, except
* with an extra clientData field we'll never use.
*/
handlerPtr = (GenericHandler *)ckalloc(sizeof(GenericHandler));
handlerPtr->proc = (Tk_GenericProc *) proc;
handlerPtr->clientData = NULL; /* never used */
handlerPtr->deleteFlag = 0;
handlerPtr->nextPtr = NULL;
if (tsdPtr->cmList == NULL) {
tsdPtr->cmList = handlerPtr;
} else {
tsdPtr->lastCmPtr->nextPtr = handlerPtr;
}
tsdPtr->lastCmPtr = handlerPtr;
}
/*
*----------------------------------------------------------------------
*
* Tk_DeleteClientMessageHandler --
*
* Delete a previously-created ClientMessage handler.
*
* Results:
* None.
*
* Side Effects:
* If there existed a handler as described by the parameters, that
* handler is logically deleted so that proc will not be invoked again.
* The physical deletion happens in the event loop in
* TkClientMessageEventProc.
*
*----------------------------------------------------------------------
*/
void
Tk_DeleteClientMessageHandler(
Tk_ClientMessageProc *proc)
{
GenericHandler * handler;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
for (handler=tsdPtr->cmList ; handler!=NULL ; handler=handler->nextPtr) {
if (handler->proc == (Tk_GenericProc *) proc) {
handler->deleteFlag = 1;
}
}
}
/*
*----------------------------------------------------------------------
*
* TkXErrorHandler --
*
* TkXErrorHandler is an error handler, to be installed via
* Tk_CreateErrorHandler, that will set a flag if an X error occurred.
*