-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkBind.c
More file actions
5472 lines (4895 loc) · 156 KB
/
Copy pathtkBind.c
File metadata and controls
5472 lines (4895 loc) · 156 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
/*
* tkBind.c --
*
* This file provides functions that associate Tcl commands with X events
* or sequences of X events.
*
* Copyright © 1989-1994 The Regents of the University of California.
* Copyright © 1994-1997 Sun Microsystems, Inc.
* Copyright © 1998 Scriptics Corporation.
* Copyright © 2018-2019 Gregor Cramer.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkDList.h"
#include "tkArray.h"
#if defined(_WIN32)
#include "tkWinInt.h"
#elif defined(MAC_OSX_TK)
#include "tkMacOSXInt.h"
#else /* if defined(__unix__) */
#include "tkUnixInt.h"
#endif
#ifdef NDEBUG
# define DEBUG(expr)
#else
# define DEBUG(expr) expr
#endif
#define SIZE_OF_ARRAY(arr) (sizeof(arr)/sizeof(arr[0]))
/*
* File structure:
*
* Structure definitions and static variables.
*
* Init/Free this package.
*
* Tcl "bind" command (actually located in tkCmds.c) core implementation, plus helpers.
*
* Tcl "event" command implementation, plus helpers.
*
* Package-specific common helpers.
*
* Non-package-specific helpers.
*/
/*
* The output for motion events is of the type <B1-Motion>. This can be changed to become
* <Motion-1> instead by setting PRINT_SHORT_MOTION_SYNTAX to 1, however this would be a
* backwards incompatibility.
*/
#ifndef PRINT_SHORT_MOTION_SYNTAX
# define PRINT_SHORT_MOTION_SYNTAX 0 /* set to 1 if wanted */
#endif
/*
* For debugging only, normally set to zero.
*/
#ifdef SUPPORT_DEBUGGING
# undef SUPPORT_DEBUGGING
#endif
#define SUPPORT_DEBUGGING 0
/*
* Test validity of PSEntry items.
*/
# define TEST_PSENTRY(psPtr) psPtr->number != 0xdeadbeef
# define MARK_PSENTRY(psPtr) psPtr->number = 0xdeadbeef
/*
* The following union is used to hold the detail information from an XEvent
* (including Tk's XVirtualEvent extension).
*/
typedef KeySym Info;
typedef union {
Info info; /* This either corresponds to xkey.keycode, or to xbutton.button,
* or is meaningless, depending on event type. */
Tk_Uid name; /* Tk_Uid of virtual event. */
} Detail;
/*
* We need an extended event definition.
*/
typedef struct {
XEvent xev; /* The original event from server. */
Detail detail; /* Additional information (for hashing). */
unsigned countAny; /* Count of multi-events, like multi-clicks, or repeated key pressing,
* this count does not depend on detail (keySym or button). */
unsigned countDetailed;
/* Count of multi-events, like multi-clicks, or repeated key pressing,
* this count considers the detail (keySym or button). */
} Event;
struct PatSeq; /* forward declaration */
/* We need this array for bookkeeping the last matching modifier mask per pattern. */
TK_ARRAY_DEFINE(PSModMaskArr, unsigned);
typedef struct PSEntry {
TK_DLIST_LINKS(PSEntry); /* Makes this struct a doubly linked list; must be first entry. */
Window window; /* Window of last match. */
struct PatSeq* psPtr; /* Pointer to pattern sequence. */
PSModMaskArr *lastModMaskArr;
/* Last matching modifier mask per pattern (except last pattern).
* Only needed if pattern sequence is not single (more than one
* pattern), and if one of these patterns contains a non-zero
* modifier mask. */
unsigned count; /* Only promote to next level if this count has reached count of
* pattern. */
unsigned expired:1; /* Whether this entry is expired, this means it has to be removed
* from promotion list. */
unsigned keepIt:1; /* Whether to keep this entry, even if expired. */
} PSEntry;
/* Defining the whole PSList_* stuff (list of PSEntry items). */
TK_DLIST_DEFINE(PSList, PSEntry);
/* Don't keep larger arrays of modifier masks inside PSEntry. */
#define MAX_MOD_MASK_ARR_SIZE 8
/*
* Maps and lookup tables from an event to a list of patterns that match that event.
*/
typedef struct {
Tcl_HashTable patternTable; /* Keys are PatternTableKey structs, values are (PatSeq *). */
Tcl_HashTable listTable; /* Keys are PatternTableKey structs, values are (PSList *). */
PSList entryPool; /* Contains free (unused) list items. */
unsigned number; /* Needed for enumeration of pattern sequences. */
} LookupTables;
/*
* The structure below represents a binding table. A binding table represents
* a domain in which event bindings may occur. It includes a space of objects
* relative to which events occur (usually windows, but not always), a history
* of recent events in the domain, and a set of mappings that associate
* particular Tcl commands with sequences of events in the domain. Multiple
* binding tables may exist at once, either because there are multiple
* applications open, or because there are multiple domains within an
* application with separate event bindings for each (for example, each canvas
* widget has a separate binding table for associating events with the items
* in the canvas).
*/
/* Defining the whole PromArr_* stuff (array of PSList entries) */
TK_ARRAY_DEFINE(PromArr, PSList);
typedef struct Tk_BindingTable_ {
Event eventInfo[TK_LASTEVENT];
/* Containing the most recent event for every event type. */
PromArr *promArr; /* Contains the promoted pattern sequences. */
Event *curEvent; /* Pointing to most recent event. */
unsigned curModMask; /* Containing the current modifier mask. */
LookupTables lookupTables; /* Containing hash tables for fast lookup. */
Tcl_HashTable objectTable; /* Used to map from an object to a list of patterns associated with
* that object. Keys are ClientData, values are (PatSeq *). */
Tcl_Interp *interp; /* Interpreter in which commands are executed. */
} BindingTable;
/*
* The following structure represents virtual event table. A virtual event
* table provides a way to map from platform-specific physical events such as
* button clicks or key presses to virtual events such as <<Paste>>,
* <<Close>>, or <<ScrollWindow>>.
*
* A virtual event is usually never part of the event stream, but instead is
* synthesized inline by matching low-level events. However, a virtual event
* may be generated by platform-specific code or by Tcl commands. In that case,
* no lookup of the virtual event will need to be done using this table,
* because the virtual event is actually in the event stream.
*/
typedef struct {
LookupTables lookupTables; /* Providing fast lookup tables to lists of pattern sequences. */
Tcl_HashTable nameTable; /* Used to map a virtual event name to the array of physical events
* that can trigger it. Keys are the Tk_Uid names of the virtual
* events, values are PhysOwned structs. */
} VirtualEventTable;
/*
* The following structure is used as a key in a patternTable for both binding
* tables and a virtual event tables.
*
* In a binding table, the object field corresponds to the binding tag for the
* widget whose bindings are being accessed.
*
* In a virtual event table, the object field is always NULL. Virtual events
* are a global definiton and are not tied to a particular binding tag.
*
* The same key is used for both types of pattern tables so that the helper
* functions that traverse and match patterns will work for both binding
* tables and virtual event tables.
*/
typedef struct {
void *object; /* For binding table, identifies the binding tag of the object
* (or class of objects) relative to which the event occurred.
* For virtual event table, always NULL. */
unsigned type; /* Type of event (from X). */
Detail detail; /* Additional information, such as keysym, button, Tk_Uid, or zero
* if nothing additional. */
} PatternTableKey;
/*
* The following structure defines a pattern, which is matched against X
* events as part of the process of converting X events into Tcl commands.
*
* For technical reasons we do not use 'union Detail', although this would
* be possible, instead 'info' and 'name' are both included.
*/
typedef struct {
unsigned eventType; /* Type of X event, e.g. ButtonPress. */
unsigned count; /* Multi-event count, e.g. double-clicks, triple-clicks, etc. */
unsigned modMask; /* Mask of modifiers that must be present (zero means no modifiers
* are required). */
Info info; /* Additional information that must match event. Normally this is zero,
* meaning no additional information must match. For KeyPress and
* KeyRelease events, it may be specified to select a particular
* keystroke (zero means any keystrokes). For button events, specifies
* a particular button (zero means any buttons are OK). */
Tk_Uid name; /* Specifies the Tk_Uid of the virtual event name. NULL if not a
* virtual event. */
} TkPattern;
/*
* The following structure keeps track of all the virtual events that are
* associated with a particular physical event. It is pointed to by the 'owners'
* field in a PatSeq in the patternTable of a virtual event table.
*/
TK_PTR_ARRAY_DEFINE(VirtOwners, Tcl_HashEntry); /* define array of hash entries */
/*
* The following structure defines a pattern sequence, which consists of one
* or more patterns. In order to trigger, a pattern sequence must match the
* most recent X events (first pattern to most recent event, next pattern to
* next event, and so on). It is used as the hash value in a patternTable for
* both binding tables and virtual event tables.
*
* In a binding table, it is the sequence of physical events that make up a
* binding for an object.
*
* In a virtual event table, it is the sequence of physical events that define
* a virtual event.
*
* The same structure is used for both types of pattern tables so that the
* helper functions that traverse and match patterns will work for both
* binding tables and virtual event tables.
*/
typedef struct PatSeq {
unsigned numPats; /* Number of patterns in sequence (usually 1). */
unsigned count; /* Total number of repetition counts, summed over count in TkPattern. */
unsigned number; /* Needed for the decision whether a binding is less recently defined
* than another, it is guaranteed that the most recently bound event
* has the highest number. */
unsigned added:1; /* Is this pattern sequence already added to lookup table? */
unsigned modMaskUsed:1; /* Does at least one pattern contain a non-zero modifier mask? */
DEBUG(unsigned owned:1;) /* For debugging purposes. */
char *script; /* Binding script to evaluate when sequence matches (ckalloc()ed) */
Tcl_Obj* object; /* Token for object with which binding is associated. For virtual
* event table this is NULL. */
struct PatSeq *nextSeqPtr; /* Next in list of all pattern sequences that have the same initial
* pattern. NULL means end of list. */
Tcl_HashEntry *hPtr; /* Pointer to hash table entry for the initial pattern. This is the
* head of the list of which nextSeqPtr forms a part. */
union {
VirtOwners *owners; /* In a binding table it has no meaning. In a virtual event table,
* identifies the array of virtual events that can be triggered
* by this event. */
struct PatSeq *nextObj; /* In a binding table, next in list of all pattern sequences for
* the same object (NULL for end of list). Needed to implement
* Tk_DeleteAllBindings. In a virtual event table it has no meaning. */
} ptr;
TkPattern pats[1]; /* Array of "numPats" patterns. Only one element is declared here
* but in actuality enough space will be allocated for "numPats"
* patterns (but usually 1). */
} PatSeq;
/*
* Compute memory size of struct PatSeq with given pattern size.
* The caller must be sure that pattern size is greater than zero.
*/
#define PATSEQ_MEMSIZE(numPats) (sizeof(PatSeq) + (numPats - 1)*sizeof(TkPattern))
/*
* Constants that define how close together two events must be in milliseconds
* or pixels to be considered close in space or time.
*/
#define NEARBY_PIXELS 5
#define NEARBY_MS 500
/*
* The following structure is used in the nameTable of a virtual event table
* to associate a virtual event with all the physical events that can trigger
* it.
*/
TK_PTR_ARRAY_DEFINE(PhysOwned, PatSeq); /* define array of pattern seqs */
/*
* One of the following structures exists for each interpreter. This structure
* keeps track of the current display and screen in the interpreter, so that a
* command can be invoked whenever the display/screen changes (the command does
* things like point tk::Priv at a display-specific structure).
*/
typedef struct {
TkDisplay *curDispPtr; /* Display for last binding command invoked in this application. */
int curScreenIndex; /* Index of screen for last binding command */
unsigned bindingDepth; /* Number of active instances of Tk_BindEvent in this application. */
} ScreenInfo;
/*
* The following structure keeps track of all the information local to the
* binding package on a per interpreter basis.
*/
typedef struct TkBindInfo_ {
VirtualEventTable virtualEventTable;
/* The virtual events that exist in this interpreter. */
ScreenInfo screenInfo; /* Keeps track of the current display and screen, so it can be
* restored after a binding has executed. */
int deleted; /* 1 if the application has been deleted but the structure has been
* preserved. */
Time lastEventTime; /* Needed for time measurement. */
Time lastCurrentTime; /* Needed for time measurement. */
} BindInfo;
/*
* In X11R4 and earlier versions, XStringToKeysym is ridiculously slow. The
* data structure and hash table below, along with the code that uses them,
* implement a fast mapping from strings to keysyms. In X11R5 and later
* releases XStringToKeysym is plenty fast so this stuff isn't needed. The
* #define REDO_KEYSYM_LOOKUP is normally undefined, so that XStringToKeysym
* gets used. It can be set in the Makefile to enable the use of the hash
* table below.
*/
#ifdef REDO_KEYSYM_LOOKUP
typedef struct {
const char *name; /* Name of keysym. */
KeySym value; /* Numeric identifier for keysym. */
} KeySymInfo;
static const KeySymInfo keyArray[] = {
#ifndef lint
#include "ks_names.h"
#endif
{NULL, 0}
};
static Tcl_HashTable keySymTable; /* keyArray hashed by keysym value. */
static Tcl_HashTable nameTable; /* keyArray hashed by keysym name. */
#endif /* REDO_KEYSYM_LOOKUP */
/*
* A hash table is kept to map from the string names of event modifiers to
* information about those modifiers. The structure for storing this
* information, and the hash table built at initialization time, are defined
* below.
*/
typedef struct {
const char *name; /* Name of modifier. */
unsigned mask; /* Button/modifier mask value, such as Button1Mask. */
unsigned flags; /* Various flags; see below for definitions. */
} ModInfo;
/*
* Flags for ModInfo structures:
*
* DOUBLE - Non-zero means duplicate this event, e.g. for double-clicks.
* TRIPLE - Non-zero means triplicate this event, e.g. for triple-clicks.
* QUADRUPLE - Non-zero means quadruple this event, e.g. for 4-fold-clicks.
* MULT_CLICKS - Combination of all the above.
*/
#define DOUBLE (1<<0)
#define TRIPLE (1<<1)
#define QUADRUPLE (1<<2)
#define MULT_CLICKS (DOUBLE|TRIPLE|QUADRUPLE)
static const ModInfo modArray[] = {
{"Control", ControlMask, 0},
{"Shift", ShiftMask, 0},
{"Lock", LockMask, 0},
{"Meta", META_MASK, 0},
#ifndef TK_NO_DEPRECATED
{"M", META_MASK, 0},
#endif
{"Alt", ALT_MASK, 0},
{"Extended", EXTENDED_MASK, 0},
{"B1", Button1Mask, 0},
{"Button1", Button1Mask, 0},
{"B2", Button2Mask, 0},
{"Button2", Button2Mask, 0},
{"B3", Button3Mask, 0},
{"Button3", Button3Mask, 0},
{"B4", Button4Mask, 0},
{"Button4", Button4Mask, 0},
{"B5", Button5Mask, 0},
{"Button5", Button5Mask, 0},
{"B6", Button6Mask, 0},
{"Button6", Button6Mask, 0},
{"B7", Button7Mask, 0},
{"Button7", Button7Mask, 0},
{"B8", Button8Mask, 0},
{"Button8", Button8Mask, 0},
{"B9", Button9Mask, 0},
{"Button9", Button9Mask, 0},
{"Mod1", Mod1Mask, 0},
{"M1", Mod1Mask, 0},
#ifdef MAC_OSX_TK
{"Command", Mod1Mask, 0},
#elif defined (_WIN32)
{"Command", ControlMask, 0},
#else
{"Command", META_MASK, 0},
#endif
{"Mod2", Mod2Mask, 0},
{"M2", Mod2Mask, 0},
#ifdef MAC_OSX_TK
{"Option", Mod2Mask, 0},
#else
{"Option", ALT_MASK, 0},
#endif
{"Mod3", Mod3Mask, 0},
{"M3", Mod3Mask, 0},
{"Num", Mod3Mask, 0},
{"Mod4", Mod4Mask, 0},
{"Fn", Mod4Mask, 0},
{"M4", Mod4Mask, 0},
{"Mod5", Mod5Mask, 0},
{"M5", Mod5Mask, 0},
{"Double", 0, DOUBLE},
{"Triple", 0, TRIPLE},
{"Quadruple", 0, QUADRUPLE},
{"Any", 0, 0}, /* Ignored: historical relic */
{NULL, 0, 0}
};
static Tcl_HashTable modTable;
/*
* This module also keeps a hash table mapping from event names to information
* about those events. The structure, an array to use to initialize the hash
* table, and the hash table are all defined below.
*/
typedef struct {
const char *name; /* Name of event. */
unsigned type; /* Event type for X, such as ButtonPress. */
unsigned eventMask; /* Mask bits (for XSelectInput) for this event type. */
} EventInfo;
/*
* Note: some of the masks below are an OR-ed combination of several masks.
* This is necessary because X doesn't report up events unless you also ask
* for down events. Also, X doesn't report button state in motion events
* unless you've asked about button events.
*/
static const EventInfo eventArray[] = {
{"Key", KeyPress, KeyPressMask},
#ifndef TK_NO_DEPRECATED
{"KeyPress", KeyPress, KeyPressMask},
#endif
{"KeyRelease", KeyRelease, KeyPressMask|KeyReleaseMask},
{"Button", ButtonPress, ButtonPressMask},
#ifndef TK_NO_DEPRECATED
{"ButtonPress", ButtonPress, ButtonPressMask},
#endif
{"ButtonRelease", ButtonRelease, ButtonPressMask|ButtonReleaseMask},
{"Motion", MotionNotify, ButtonPressMask|PointerMotionMask},
{"Enter", EnterNotify, EnterWindowMask},
{"Leave", LeaveNotify, LeaveWindowMask},
{"FocusIn", FocusIn, FocusChangeMask},
{"FocusOut", FocusOut, FocusChangeMask},
{"Expose", Expose, ExposureMask},
{"Visibility", VisibilityNotify, VisibilityChangeMask},
{"Destroy", DestroyNotify, StructureNotifyMask},
{"Unmap", UnmapNotify, StructureNotifyMask},
{"Map", MapNotify, StructureNotifyMask},
{"Reparent", ReparentNotify, StructureNotifyMask},
{"Configure", ConfigureNotify, StructureNotifyMask},
{"Gravity", GravityNotify, StructureNotifyMask},
{"Circulate", CirculateNotify, StructureNotifyMask},
{"Property", PropertyNotify, PropertyChangeMask},
{"Colormap", ColormapNotify, ColormapChangeMask},
{"Activate", ActivateNotify, ActivateMask},
{"Deactivate", DeactivateNotify, ActivateMask},
{"MouseWheel", MouseWheelEvent, MouseWheelMask},
{"TouchpadScroll", TouchpadScroll, TouchpadScrollMask},
{"CirculateRequest", CirculateRequest, SubstructureRedirectMask},
{"ConfigureRequest", ConfigureRequest, SubstructureRedirectMask},
{"Create", CreateNotify, SubstructureNotifyMask},
{"MapRequest", MapRequest, SubstructureRedirectMask},
{"ResizeRequest", ResizeRequest, ResizeRedirectMask},
{NULL, 0, 0}
};
static Tcl_HashTable eventTable;
static int eventArrayIndex[TK_LASTEVENT];
/*
* The defines and table below are used to classify events into various
* groups. The reason for this is that logically identical fields (e.g.
* "state") appear at different places in different types of events. The
* classification masks can be used to figure out quickly where to extract
* information from events.
*/
#define KEY (1<<0)
#define BUTTON (1<<1)
#define MOTION (1<<2)
#define CROSSING (1<<3)
#define FOCUS (1<<4)
#define EXPOSE (1<<5)
#define VISIBILITY (1<<6)
#define CREATE (1<<7)
#define DESTROY (1<<8)
#define UNMAP (1<<9)
#define MAP (1<<10)
#define REPARENT (1<<11)
#define CONFIG (1<<12)
#define GRAVITY (1<<13)
#define CIRC (1<<14)
#define PROP (1<<15)
#define COLORMAP (1<<16)
#define VIRTUAL (1<<17)
#define ACTIVATE (1<<18)
#define WHEEL (1<<19)
#define MAPREQ (1<<20)
#define CONFIGREQ (1<<21)
#define RESIZEREQ (1<<22)
#define CIRCREQ (1<<23)
/*
* These structs agree with xkey for the fields type, serial, send_event, display,
* window, root, subwindow, time, x, y, x_root, and y_root. So when accessing
* these fields we may pretend that we are using a struct xkey.
*/
#define HAS_XKEY_HEAD (KEY|BUTTON|MOTION|VIRTUAL|CROSSING|WHEEL)
/*
* The xcrossing struct puts the state field in a different location, but the other
* events above agree on where state is located.
*/
#define HAS_XKEY_HEAD_AND_STATE (KEY|BUTTON|MOTION|VIRTUAL|WHEEL)
/*
* Event types which support -warp.
*/
#define CAN_WARP (KEY|BUTTON|MOTION|WHEEL)
static const int flagArray[TK_LASTEVENT] = {
/* Not used */ 0,
/* Not used */ 0,
/* KeyPress */ KEY,
/* KeyRelease */ KEY,
/* ButtonPress */ BUTTON,
/* ButtonRelease */ BUTTON,
/* MotionNotify */ MOTION,
/* EnterNotify */ CROSSING,
/* LeaveNotify */ CROSSING,
/* FocusIn */ FOCUS,
/* FocusOut */ FOCUS,
/* KeymapNotify */ 0,
/* Expose */ EXPOSE,
/* GraphicsExpose */ EXPOSE,
/* NoExpose */ 0,
/* VisibilityNotify */ VISIBILITY,
/* CreateNotify */ CREATE,
/* DestroyNotify */ DESTROY,
/* UnmapNotify */ UNMAP,
/* MapNotify */ MAP,
/* MapRequest */ MAPREQ,
/* ReparentNotify */ REPARENT,
/* ConfigureNotify */ CONFIG,
/* ConfigureRequest */ CONFIGREQ,
/* GravityNotify */ GRAVITY,
/* ResizeRequest */ RESIZEREQ,
/* CirculateNotify */ CIRC,
/* CirculateRequest */ 0,
/* PropertyNotify */ PROP,
/* SelectionClear */ 0,
/* SelectionRequest */ 0,
/* SelectionNotify */ 0,
/* ColormapNotify */ COLORMAP,
/* ClientMessage */ 0,
/* MappingNotify */ 0,
/* VirtualEvent */ VIRTUAL,
/* Activate */ ACTIVATE,
/* Deactivate */ ACTIVATE,
/* MouseWheel */ WHEEL,
/* TouchpadScroll */ WHEEL
};
/*
* The following table is used to map between the location where an generated
* event should be queued and the string used to specify the location.
*/
static const TkStateMap queuePosition[] = {
{-1, "now"},
{TCL_QUEUE_HEAD, "head"},
{TCL_QUEUE_MARK, "mark"},
{TCL_QUEUE_TAIL, "tail"},
{-2, NULL}
};
/*
* The following tables are used as a two-way map between X's internal numeric
* values for fields in an XEvent and the strings used in Tcl. The tables are
* used both when constructing an XEvent from user input and when providing
* data from an XEvent to the user.
*/
static const TkStateMap notifyMode[] = {
{NotifyNormal, "NotifyNormal"},
{NotifyGrab, "NotifyGrab"},
{NotifyUngrab, "NotifyUngrab"},
{NotifyWhileGrabbed, "NotifyWhileGrabbed"},
{-1, NULL}
};
static const TkStateMap notifyDetail[] = {
{NotifyAncestor, "NotifyAncestor"},
{NotifyVirtual, "NotifyVirtual"},
{NotifyInferior, "NotifyInferior"},
{NotifyNonlinear, "NotifyNonlinear"},
{NotifyNonlinearVirtual, "NotifyNonlinearVirtual"},
{NotifyPointer, "NotifyPointer"},
{NotifyPointerRoot, "NotifyPointerRoot"},
{NotifyDetailNone, "NotifyDetailNone"},
{-1, NULL}
};
static const TkStateMap circPlace[] = {
{PlaceOnTop, "PlaceOnTop"},
{PlaceOnBottom, "PlaceOnBottom"},
{-1, NULL}
};
static const TkStateMap visNotify[] = {
{VisibilityUnobscured, "VisibilityUnobscured"},
{VisibilityPartiallyObscured, "VisibilityPartiallyObscured"},
{VisibilityFullyObscured, "VisibilityFullyObscured"},
{-1, NULL}
};
static const TkStateMap configureRequestDetail[] = {
{None, "None"},
{Above, "Above"},
{Below, "Below"},
{BottomIf, "BottomIf"},
{TopIf, "TopIf"},
{Opposite, "Opposite"},
{-1, NULL}
};
static const TkStateMap propNotify[] = {
{PropertyNewValue, "NewValue"},
{PropertyDelete, "Delete"},
{-1, NULL}
};
DEBUG(static int countTableItems = 0;)
DEBUG(static int countEntryItems = 0;)
DEBUG(static int countListItems = 0;)
DEBUG(static int countBindItems = 0;)
DEBUG(static int countSeqItems = 0;)
/*
* Prototypes for local functions defined in this file:
*/
static void ChangeScreen(Tcl_Interp *interp, char *dispName, int screenIndex);
static int CreateVirtualEvent(Tcl_Interp *interp, VirtualEventTable *vetPtr,
char *virtString, const char *eventString);
static int DeleteVirtualEvent(Tcl_Interp *interp, VirtualEventTable *vetPtr,
char *virtString, const char *eventString);
static void DeleteVirtualEventTable(VirtualEventTable *vetPtr);
static void ExpandPercents(TkWindow *winPtr, const char *before, Event *eventPtr,
unsigned scriptCount, Tcl_DString *dsPtr);
static PatSeq * FindSequence(Tcl_Interp *interp, LookupTables *lookupTables,
void *object, const char *eventString, int create,
int allowVirtual, unsigned *maskPtr);
static void GetAllVirtualEvents(Tcl_Interp *interp, VirtualEventTable *vetPtr);
static const char * GetField(const char *p, char *copy, unsigned size);
static Tcl_Obj * GetPatternObj(const PatSeq *psPtr);
static int GetVirtualEvent(Tcl_Interp *interp, VirtualEventTable *vetPtr,
Tcl_Obj *virtName);
static Tk_Uid GetVirtualEventUid(Tcl_Interp *interp, char *virtString);
static int HandleEventGenerate(Tcl_Interp *interp, Tk_Window main,
Tcl_Size objc, Tcl_Obj *const objv[]);
static void InitVirtualEventTable(VirtualEventTable *vetPtr);
static PatSeq * MatchPatterns(TkDisplay *dispPtr, Tk_BindingTable bindPtr, PSList *psList,
PSList *psSuccList, unsigned patIndex, const Event *eventPtr,
void *object, PatSeq **physPtrPtr);
static int NameToWindow(Tcl_Interp *interp, Tk_Window main,
Tcl_Obj *objPtr, Tk_Window *tkwinPtr);
static unsigned ParseEventDescription(Tcl_Interp *interp, const char **eventStringPtr,
TkPattern *patPtr, unsigned *eventMaskPtr);
static PSList * GetLookupForEvent(LookupTables* lookupPtr, const Event *eventPtr,
Tcl_Obj *object, int onlyConsiderDetailedEvents);
static void ClearLookupTable(LookupTables *lookupTables, void *object);
static void ClearPromotionLists(Tk_BindingTable bindPtr, void *object);
static PSEntry * MakeListEntry(PSList *pool, PatSeq *psPtr, int needModMasks);
static void RemovePatSeqFromLookup(LookupTables *lookupTables, PatSeq *psPtr);
static void RemovePatSeqFromPromotionLists(Tk_BindingTable bindPtr, PatSeq *psPtr);
static PatSeq * DeletePatSeq(PatSeq *psPtr);
static void InsertPatSeq(LookupTables *lookupTables, PatSeq *psPtr);
#if SUPPORT_DEBUGGING
void TkpDumpPS(const PatSeq *psPtr);
void TkpDumpPSList(const PSList *psList);
#endif
/*
* Some useful helper functions.
*/
#if SUPPORT_DEBUGGING
static int BindCount = 0; /* Can be set or queried from Tcl through 'event debug' subcommand. Otherwise not used. */
#endif
static Tcl_Size Max(Tcl_Size a, Tcl_Size b) { return a < b ? b : a; }
static int Abs(int n) { return n < 0 ? -n : n; }
static int IsOdd(int n) { return n & 1; }
static int TestNearbyTime(int lhs, int rhs) { return Abs(lhs - rhs) <= NEARBY_MS; }
static int TestNearbyCoords(int lhs, int rhs) { return Abs(lhs - rhs) <= NEARBY_PIXELS; }
static int
IsSubsetOf(
unsigned lhsMask, /* Is this a subset... */
unsigned rhsMask) /* ...of this bit field? */
{
return (lhsMask & rhsMask) == lhsMask;
}
static const char*
SkipSpaces(
const char* s)
{
assert(s);
while (isspace(UCHAR(*s)))
++s;
return s;
}
static const char*
SkipFieldDelims(
const char* s)
{
assert(s);
while (*s == '-' || isspace(UCHAR(*s))) {
++s;
}
return s;
}
static unsigned
GetButtonNumber(
const char *field)
{
unsigned button;
assert(field);
button = (field[0] >= '1' && field[0] <= '9' && field[1] == '\0') ? (unsigned)(field[0] - '0') : 0;
return (button > 3) ? (button + 4) : button;
}
static Time
CurrentTimeInMilliSecs(void)
{
Tcl_Time now;
Tcl_GetTime(&now);
return ((Time) now.sec)*1000 + ((Time) now.usec)/1000;
}
static Info
GetInfo(
const PatSeq *psPtr,
unsigned index)
{
assert(psPtr);
assert(index < psPtr->numPats);
return psPtr->pats[index].info;
}
static unsigned
GetCount(
const PatSeq *psPtr,
unsigned index)
{
assert(psPtr);
assert(index < psPtr->numPats);
return psPtr->pats[index].count;
}
static int
CountSpecialized(
const PatSeq *fstMatchPtr,
const PatSeq *sndMatchPtr)
{
int fstCount = 0;
int sndCount = 0;
unsigned i;
assert(fstMatchPtr);
assert(sndMatchPtr);
for (i = 0; i < fstMatchPtr->numPats; ++i) {
if (GetInfo(fstMatchPtr, i)) { fstCount += GetCount(fstMatchPtr, i); }
}
for (i = 0; i < sndMatchPtr->numPats; ++i) {
if (GetInfo(sndMatchPtr, i)) { sndCount += GetCount(sndMatchPtr, i); }
}
return sndCount - fstCount;
}
static int
IsKeyEventType(
int eventType)
{
return eventType == KeyPress || eventType == KeyRelease;
}
static int
IsButtonEventType(
unsigned eventType)
{
return eventType == ButtonPress || eventType == ButtonRelease;
}
static int
MatchEventNearby(
const XEvent *lhs, /* Previous button event */
const XEvent *rhs) /* Current button event */
{
assert(lhs);
assert(rhs);
assert(IsButtonEventType(lhs->type));
assert(lhs->type == rhs->type);
/* assert: lhs->xbutton.time <= rhs->xbutton.time */
return TestNearbyTime(rhs->xbutton.time, lhs->xbutton.time)
&& TestNearbyCoords(rhs->xbutton.x_root, lhs->xbutton.x_root)
&& TestNearbyCoords(rhs->xbutton.y_root, lhs->xbutton.y_root);
}
static int
MatchEventRepeat(
const XKeyEvent *lhs, /* Previous key event */
const XKeyEvent *rhs) /* Current key event */
{
assert(lhs);
assert(rhs);
assert(IsKeyEventType(lhs->type));
assert(lhs->type == rhs->type);
/* assert: lhs->time <= rhs->time */
return lhs->keycode == rhs->keycode && TestNearbyTime(lhs->time, rhs->time);
}
static void
FreePatSeq(
PatSeq *psPtr)
{
assert(psPtr);
assert(!psPtr->owned);
DEBUG(MARK_PSENTRY(psPtr);)
ckfree(psPtr->script);
if (!psPtr->object) {
VirtOwners_Free(&psPtr->ptr.owners);
}
ckfree(psPtr);
DEBUG(countSeqItems -= 1;)
}
static void
RemoveListEntry(
PSList *pool,
PSEntry *psEntry)
{
assert(pool);
assert(psEntry);
if (PSModMaskArr_Capacity(psEntry->lastModMaskArr) > MAX_MOD_MASK_ARR_SIZE) {
PSModMaskArr_Free(&psEntry->lastModMaskArr);
}
PSList_Remove(psEntry);
PSList_Append(pool, psEntry);
}
static void
ClearList(
PSList *psList,
PSList *pool,
void *object)
{
assert(psList);
assert(pool);
if (object) {
PSEntry *psEntry;
PSEntry *psNext;
for (psEntry = PSList_First(psList); psEntry; psEntry = psNext) {
psNext = PSList_Next(psEntry);
if (psEntry->psPtr->object == object) {
RemoveListEntry(pool, psEntry);
}
}
} else {
PSList_Move(pool, psList);
}
}
static PSEntry *
FreePatSeqEntry(
TCL_UNUSED(PSList *),
PSEntry *entry)
{
PSEntry *next = PSList_Next(entry);
PSModMaskArr_Free(&entry->lastModMaskArr);
ckfree(entry);
return next;
}
static unsigned
ResolveModifiers(
TkDisplay *dispPtr,
unsigned modMask)
{
assert(dispPtr);
if (dispPtr->metaModMask) {
if (modMask & META_MASK) {
modMask &= ~META_MASK;
modMask |= dispPtr->metaModMask;
}
}
if (dispPtr->altModMask) {
if (modMask & ALT_MASK) {
modMask &= ~ALT_MASK;
modMask |= dispPtr->altModMask;
}
}
return modMask;
}
static int
ButtonNumberFromState(
unsigned state)
{
if (!(state & ALL_BUTTONS)) { return 0; }
if (state & Button1Mask) { return 1; }
if (state & Button2Mask) { return 2; }
if (state & Button3Mask) { return 3; }
if (state & Button4Mask) { return 4; }
if (state & Button5Mask) { return 5; }
if (state & Button6Mask) { return 6; }
if (state & Button7Mask) { return 7; }
if (state & Button8Mask) { return 8; }
return 9;
}
static void
SetupPatternKey(
PatternTableKey *key,
const PatSeq *psPtr)
{
const TkPattern *patPtr;
assert(key);
assert(psPtr);
/* otherwise on some systems the key contains uninitialized bytes */
memset(key, 0, sizeof(PatternTableKey));