-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckBind.c
2113 lines (1938 loc) · 65.1 KB
/
ckBind.c
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
/*
* ckBind.c --
*
* This file provides procedures that associate Tcl commands
* with events or sequences of events.
*
* Copyright (c) 1989-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
* Copyright (c) 2019-2021 vzvca
*
* 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"
/*
* 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).
*
* Note: it is probably a bad idea to reduce EVENT_BUFFER_SIZE much
* below 30. To see this, consider a triple mouse button click while
* the Shift key is down (and auto-repeating). There may be as many
* as 3 auto-repeat events after each mouse button press or release
* (see the first large comment block within Ck_BindEvent for more on
* this), for a total of 20 events to cover the three button presses
* and two intervening releases. If you reduce EVENT_BUFFER_SIZE too
* much, shift multi-clicks will be lost.
*
*/
#define EVENT_BUFFER_SIZE 30
typedef struct BindingTable {
CkEvent eventRing[EVENT_BUFFER_SIZE];/* Circular queue of recent events
* (higher indices are for more recent
* events). */
int detailRing[EVENT_BUFFER_SIZE]; /* "Detail" information (keycodes for
* each entry in eventRing. */
int curEvent; /* Index in eventRing of most recent
* event. Newer events have higher
* indices. */
Tcl_HashTable patternTable; /* Used to map from an event to a list
* of patterns that may match that
* event. Keys are PatternTableKey
* structs, values are (PatSeq *). */
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;
/*
* Structures of the following form are used as keys in the patternTable
* for a binding table:
*/
typedef struct PatternTableKey {
ClientData object; /* Identifies object (or class of objects)
* relative to which event occurred. For
* example, in the widget binding table for
* an application this is the path name of
* a widget, or a widget class, or "all". */
int type; /* Type of event. */
int detail; /* Additional information, such as
* keycode, or 0 if nothing
* additional.*/
} PatternTableKey;
/*
* The following structure defines a pattern, which is matched
* against events as part of the process of converting events
* into Tcl commands.
*/
typedef struct Pattern {
int eventType; /* Type of event. */
int detail; /* Additional information that must
* match event. Normally this is 0,
* meaning no additional information
* must match. For keystrokes this
* is the keycode. Keycode 0 means
* any keystroke, keycode -1 means
* control keystroke. */
} Pattern;
/*
* The structure below 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).
*/
typedef struct PatSeq {
int numPats; /* Number of patterns in sequence
* (usually 1). */
char *command; /* Command to invoke when this
* pattern sequence matches (malloc-ed). */
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. */
ClientData object; /* Identifies object with which event is
* associated (e.g. window). */
struct PatSeq *nextObjPtr;
/* Next in list of all pattern
* sequences for the same object
* (NULL for end of list). Needed to
* implement Tk_DeleteAllBindings. */
Pattern pats[1]; /* Array of "numPats" patterns. Only
* one element is declared here but
* in actuality enough space will be
* allocated for "numPats" patterns.
* To match, pats[0] must match event
* n, pats[1] must match event n-1,
* etc. */
} PatSeq;
typedef struct {
char *name; /* Name of keysym. */
KeySym value; /* Numeric identifier for keysym. */
char *tiname; /* Terminfo name of keysym. */
} KeySymInfo;
static KeySymInfo keyArray[] = {
#include "ks_names.h"
{(char *) NULL, 0}
};
static Tcl_HashTable keySymTable; /* Hashed form of above structure. */
static Tcl_HashTable revKeySymTable; /* Ditto, reversed. */
static int initialized = 0;
/*
* 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 {
char *name; /* Name of event. */
int type; /* Event type for X, such as
* ButtonPress. */
int eventMask; /* Mask bits for this event type. */
} EventInfo;
#define CK_MAX_EV 128
#define CK_EV_BUTTON (CK_EV_MOUSE_DOWN | CK_EV_MOUSE_UP)
#define CK_EV_MOUSE (CK_EV_MOUSE_DOWN | CK_EV_MOUSE_UP | CK_EV_MOUSE_MOVE)
static EventInfo eventArray[CK_MAX_EV] =
{
{"Expose", CK_EV_EXPOSE, CK_EV_EXPOSE},
{"FocusIn", CK_EV_FOCUSIN, CK_EV_FOCUSIN},
{"FocusOut", CK_EV_FOCUSOUT, CK_EV_FOCUSOUT},
{"Key", CK_EV_KEYPRESS, CK_EV_KEYPRESS},
{"KeyPress", CK_EV_KEYPRESS, CK_EV_KEYPRESS},
{"Alt", 0, CK_EV_KEYPRESS | CK_EV_MOUSE},
{"Control", 0, CK_EV_KEYPRESS | CK_EV_MOUSE},
{"Shift", 0, CK_EV_KEYPRESS | CK_EV_MOUSE},
{"Double", 0, CK_EV_MOUSE_DOWN},
{"Triple", 0, CK_EV_MOUSE_DOWN},
{"Button1", 0, CK_EV_MOUSE_MOVE},
{"Button2", 0, CK_EV_MOUSE_MOVE},
{"Button3", 0, CK_EV_MOUSE_MOVE},
{"B1", 0, CK_EV_MOUSE_MOVE},
{"B2", 0, CK_EV_MOUSE_MOVE},
{"B3", 0, CK_EV_MOUSE_MOVE},
{"Destroy", CK_EV_DESTROY, CK_EV_DESTROY},
{"Map", CK_EV_MAP, CK_EV_MAP},
{"Unmap", CK_EV_UNMAP, CK_EV_UNMAP},
{"Button", CK_EV_MOUSE_DOWN, CK_EV_MOUSE_DOWN},
{"ButtonPress", CK_EV_MOUSE_DOWN, CK_EV_MOUSE_DOWN},
{"ButtonRelease", CK_EV_MOUSE_UP, CK_EV_MOUSE_UP},
{"Motion", CK_EV_MOUSE_MOVE, CK_EV_MOUSE_MOVE},
{"BarCode", CK_EV_BARCODE, CK_EV_BARCODE},
/* The following are virtual events.
* At present they are only used by the terminal widget
* which use them to report commands that have been detected.
*
* Will be improved to allow adding of new virtual events types.
*/
{"<Exited>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Subprocess exited */
{"<New>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Open a new terminal */
{"<Close>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Close terminal */
{"<Next>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Move to next terminal */
{"<Prev>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Move to previous terminal */
{"<Fullscreen>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* Go fullscreen */
{"<ChszH>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* inc/dec horizontal size */
{"<ChszV>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* inc/dec vertical size */
{"<SplitH>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* horizontal split */
{"<SplitV>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* vertical split */
{"<Copy>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* copy text */
{"<Paste>", CK_EV_VIRTUAL, CK_EV_VIRTUAL}, /* paste text */
{(char *) NULL, 0, 0}
};
static Tcl_HashTable eventTable;
/*
* Prototypes for local procedures defined in this file:
*/
static void ExpandPercents _ANSI_ARGS_((CkWindow *winPtr,
char *before, CkEvent *eventPtr, KeySym keySym,
Tcl_DString *dsPtr));
static PatSeq * FindSequence _ANSI_ARGS_((Tcl_Interp *interp,
BindingTable *bindPtr, ClientData object,
char *eventString, int create));
static char * GetField _ANSI_ARGS_((char *p, char *copy, int size));
static PatSeq * MatchPatterns _ANSI_ARGS_((BindingTable *bindPtr,
PatSeq *psPtr));
/*
*--------------------------------------------------------------
*
* Ck_InitKeys --
*
* This procedure defines new curses keys
* It will allow for extending bindings
*
* Results:
* None
*
* Side effects:
* New key are defined in ncurses
*
*--------------------------------------------------------------
*/
void
Ck_InitKeys()
{
#define CK_NEW_KEY(name, seq, keycode) define_key(seq, keycode);
#include "ckKeys.h"
#undef CK_NEW_KEY
}
/*
*--------------------------------------------------------------
*
* Ck_CreateBindingTable --
*
* Set up a new domain in which event bindings may be created.
*
* Results:
* The return value is a token for the new table, which must
* be passed to procedures like Ck_CreateBinding.
*
* Side effects:
* Memory is allocated for the new table.
*
*--------------------------------------------------------------
*/
Ck_BindingTable
Ck_CreateBindingTable(interp)
Tcl_Interp *interp; /* Interpreter to associate with the binding
* table: commands are executed in this
* interpreter. */
{
BindingTable *bindPtr;
int i;
/*
* If this is the first time a binding table has been created,
* initialize the global data structures.
*/
if (!initialized) {
Tcl_HashEntry *hPtr;
EventInfo *eiPtr;
KeySymInfo *kPtr;
int dummy;
Tcl_InitHashTable(&keySymTable, TCL_STRING_KEYS);
Tcl_InitHashTable(&revKeySymTable, TCL_ONE_WORD_KEYS);
for (kPtr = keyArray; kPtr->name != NULL; kPtr++) {
hPtr = Tcl_CreateHashEntry(&keySymTable, kPtr->name, &dummy);
Tcl_SetHashValue(hPtr, (char *) kPtr);
hPtr = Tcl_CreateHashEntry(&revKeySymTable, (char *) kPtr->value,
&dummy);
Tcl_SetHashValue(hPtr, (char *) kPtr);
}
Tcl_InitHashTable(&eventTable, TCL_STRING_KEYS);
for (eiPtr = eventArray; eiPtr->name != NULL; eiPtr++) {
hPtr = Tcl_CreateHashEntry(&eventTable, eiPtr->name, &dummy);
Tcl_SetHashValue(hPtr, eiPtr);
}
initialized = 1;
}
/*
* Create and initialize a new binding table.
*/
bindPtr = (BindingTable *) ckalloc(sizeof (BindingTable));
for (i = 0; i < EVENT_BUFFER_SIZE; i++) {
bindPtr->eventRing[i].type = -1;
}
bindPtr->curEvent = 0;
Tcl_InitHashTable(&bindPtr->patternTable,
sizeof(PatternTableKey)/sizeof(int));
Tcl_InitHashTable(&bindPtr->objectTable, TCL_ONE_WORD_KEYS);
bindPtr->interp = interp;
return (Ck_BindingTable) bindPtr;
}
/*
*--------------------------------------------------------------
*
* Ck_DeleteBindingTable --
*
* Destroy a binding table and free up all its memory.
* The caller should not use bindingTable again after
* this procedure returns.
*
* Results:
* None.
*
* Side effects:
* Memory is freed.
*
*--------------------------------------------------------------
*/
void
Ck_DeleteBindingTable(bindingTable)
Ck_BindingTable bindingTable; /* Token for the binding table to
* destroy. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
PatSeq *psPtr, *nextPtr;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
/*
* Find and delete all of the patterns associated with the binding
* table.
*/
for (hPtr = Tcl_FirstHashEntry(&bindPtr->patternTable, &search);
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
psPtr != NULL; psPtr = nextPtr) {
nextPtr = psPtr->nextSeqPtr;
ckfree((char *) psPtr->command);
ckfree((char *) psPtr);
}
}
/*
* Clean up the rest of the information associated with the
* binding table.
*/
Tcl_DeleteHashTable(&bindPtr->patternTable);
Tcl_DeleteHashTable(&bindPtr->objectTable);
ckfree((char *) bindPtr);
}
/*
*--------------------------------------------------------------
*
* Ck_CreateBinding --
*
* Add a binding to a binding table, so that future calls to
* Ck_BindEvent may execute the command in the binding.
*
* Results:
* The return value is TCL_ERROR if an error occurred while setting
* up the binding. In this case, an error message will be
* left in interp->result. If all went well then the return
* value is TCL_OK.
*
* Side effects:
* The new binding may cause future calls to Ck_BindEvent to
* behave differently than they did previously.
*
*--------------------------------------------------------------
*/
int
Ck_CreateBinding(interp, bindingTable, object, eventString, command, append)
Tcl_Interp *interp; /* Used for error reporting. */
Ck_BindingTable bindingTable; /* Table in which to create binding. */
ClientData object; /* Token for object with which binding
* is associated. */
char *eventString; /* String describing event sequence
* that triggers binding. */
char *command; /* Contains Tcl command to execute
* when binding triggers. */
int append; /* 0 means replace any existing
* binding for eventString; 1 means
* append to that binding. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
PatSeq *psPtr;
psPtr = FindSequence(interp, bindPtr, object, eventString, 1);
if (psPtr == NULL)
return TCL_ERROR;
if (append && (psPtr->command != NULL)) {
int length;
char *new;
length = strlen(psPtr->command) + strlen(command) + 2;
new = (char *) ckalloc((unsigned) length);
sprintf(new, "%s\n%s", psPtr->command, command);
ckfree((char *) psPtr->command);
psPtr->command = new;
} else {
if (psPtr->command != NULL) {
ckfree((char *) psPtr->command);
}
psPtr->command = (char *) ckalloc((unsigned) (strlen(command) + 1));
strcpy(psPtr->command, command);
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* Ck_DeleteBinding --
*
* Remove an event binding from a binding table.
*
* Results:
* The result is a standard Tcl return value. If an error
* occurs then interp->result will contain an error message.
*
* Side effects:
* The binding given by object and eventString is removed
* from bindingTable.
*
*--------------------------------------------------------------
*/
int
Ck_DeleteBinding(interp, bindingTable, object, eventString)
Tcl_Interp *interp; /* Used for error reporting. */
Ck_BindingTable bindingTable; /* Table in which to delete binding. */
ClientData object; /* Token for object with which binding
* is associated. */
char *eventString; /* String describing event sequence
* that triggers binding. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
register PatSeq *psPtr, *prevPtr;
Tcl_HashEntry *hPtr;
psPtr = FindSequence(interp, bindPtr, object, eventString, 0);
if (psPtr == NULL) {
Tcl_ResetResult(interp);
return TCL_OK;
}
/*
* Unlink the binding from the list for its object, then from the
* list for its pattern.
*/
hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
if (hPtr == NULL) {
panic("Ck_DeleteBinding couldn't find object table entry");
}
prevPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
if (prevPtr == psPtr) {
Tcl_SetHashValue(hPtr, psPtr->nextObjPtr);
} else {
for ( ; ; prevPtr = prevPtr->nextObjPtr) {
if (prevPtr == NULL) {
panic("Ck_DeleteBinding couldn't find on object list");
}
if (prevPtr->nextObjPtr == psPtr) {
prevPtr->nextObjPtr = psPtr->nextObjPtr;
break;
}
}
}
prevPtr = (PatSeq *) Tcl_GetHashValue(psPtr->hPtr);
if (prevPtr == psPtr) {
if (psPtr->nextSeqPtr == NULL) {
Tcl_DeleteHashEntry(psPtr->hPtr);
} else {
Tcl_SetHashValue(psPtr->hPtr, psPtr->nextSeqPtr);
}
} else {
for ( ; ; prevPtr = prevPtr->nextSeqPtr) {
if (prevPtr == NULL) {
panic("Tk_DeleteBinding couldn't find on hash chain");
}
if (prevPtr->nextSeqPtr == psPtr) {
prevPtr->nextSeqPtr = psPtr->nextSeqPtr;
break;
}
}
}
ckfree((char *) psPtr->command);
ckfree((char *) psPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* Ck_GetBinding --
*
* Return the command associated with a given event string.
*
* Results:
* The return value is a pointer to the command string
* associated with eventString for object in the domain
* given by bindingTable. If there is no binding for
* eventString, or if eventString is improperly formed,
* then NULL is returned and an error message is left in
* interp->result. The return value is semi-static: it
* will persist until the binding is changed or deleted.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
char *
Ck_GetBinding(interp, bindingTable, object, eventString)
Tcl_Interp *interp; /* Interpreter for error reporting. */
Ck_BindingTable bindingTable; /* Table in which to look for
* binding. */
ClientData object; /* Token for object with which binding
* is associated. */
char *eventString; /* String describing event sequence
* that triggers binding. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
PatSeq *psPtr;
psPtr = FindSequence(interp, bindPtr, object, eventString, 0);
if (psPtr == NULL) {
return NULL;
}
return psPtr->command;
}
/*
*--------------------------------------------------------------
*
* Ck_GetAllBindings --
*
* Return a list of event strings for all the bindings
* associated with a given object.
*
* Results:
* There is no return value. Interp->result is modified to
* hold a Tcl list with one entry for each binding associated
* with object in bindingTable. Each entry in the list
* contains the event string associated with one binding.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void
Ck_GetAllBindings(interp, bindingTable, object)
Tcl_Interp *interp; /* Interpreter returning result or
* error. */
Ck_BindingTable bindingTable; /* Table in which to look for
* bindings. */
ClientData object; /* Token for object. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
register PatSeq *psPtr;
register Pattern *patPtr;
Tcl_HashEntry *hPtr;
Tcl_DString ds;
char c, buffer[32];
int patsLeft;
register EventInfo *eiPtr;
hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
if (hPtr == NULL) {
return;
}
Tcl_DStringInit(&ds);
for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr); psPtr != NULL;
psPtr = psPtr->nextObjPtr) {
Tcl_DStringTrunc(&ds, 0);
/*
* For each binding, output information about each of the
* patterns in its sequence. The order of the patterns in
* the sequence is backwards from the order in which they
* must be output.
*/
for (patsLeft = psPtr->numPats,
patPtr = &psPtr->pats[psPtr->numPats - 1];
patsLeft > 0; patsLeft--, patPtr--) {
/*
* Check for simple button press.
*/
if ((patPtr->eventType == CK_EV_MOUSE_DOWN)
&& (patPtr->detail != 0) && ((patPtr->detail & CK_MOD_ALL) == 0)) {
sprintf(buffer, "<%d>", patPtr->detail);
Tcl_DStringAppend(&ds, buffer, -1);
continue;
}
/*
* Check for general mouse events
*/
if (((patPtr->eventType == CK_EV_MOUSE_DOWN) ||
(patPtr->eventType == CK_EV_MOUSE_UP))
&& (patPtr->detail != 0)) {
int modifiers = patPtr->detail & CK_MOD_ALL;
Tcl_DStringAppend(&ds, "<", -1);
if ( modifiers ) {
if ( modifiers & CK_MOD_CONTROL ) {
Tcl_DStringAppend(&ds, "Control-", -1);
}
if ( modifiers & CK_MOD_ALT ) {
Tcl_DStringAppend(&ds, "Alt-", -1);
}
if ( modifiers & CK_MOD_SHIFT ) {
Tcl_DStringAppend(&ds, "Shift-", -1);
}
if ( modifiers & CK_MOD_DOUBLE ) {
Tcl_DStringAppend(&ds, "Double-", -1);
}
if ( modifiers & CK_MOD_TRIPLE ) {
Tcl_DStringAppend(&ds, "Triple-", -1);
}
}
if (patPtr->eventType == CK_EV_MOUSE_MOVE) {
Tcl_DStringAppend(&ds, "Motion>", -1);
}
else {
int button = patPtr->detail & ~CK_MOD_ALL;
if (patPtr->eventType == CK_EV_MOUSE_DOWN) {
sprintf(buffer, "Button-%d>", button);
}
else {
sprintf(buffer, "ButtonRelease-%d>", button);
}
Tcl_DStringAppend(&ds, buffer, -1);
}
continue;
}
/*
* Check for simple case of an ASCII character.
*/
if ((patPtr->eventType == CK_EV_KEYPRESS)
&& (patPtr->detail < 128)
&& isprint((unsigned char) patPtr->detail)
&& (patPtr->detail != '<')
&& (patPtr->detail != ' ')) {
c = patPtr->detail;
Tcl_DStringAppend(&ds, &c, 1);
continue;
}
/*
* Virtual events
*/
if (patPtr->eventType == CK_EV_VIRTUAL) {
Tcl_DStringAppend(&ds, "<", 1);
Tcl_DStringAppend(&ds, eventArray[patPtr->detail].name, -1);
Tcl_DStringAppend(&ds, ">", 1);
}
/*
* It's a more general event specification. First check
* event type, then keysym or button detail.
*/
Tcl_DStringAppend(&ds, "<", 1);
for (eiPtr = eventArray; eiPtr->name != NULL; eiPtr++) {
if (eiPtr->type == patPtr->eventType) {
if (patPtr->eventType == CK_EV_KEYPRESS &&
patPtr->detail == -1) {
Tcl_DStringAppend(&ds, "Control", -1);
goto endPat;
}
if (patPtr->eventType == CK_EV_KEYPRESS &&
patPtr->detail > 0 && patPtr->detail < 0x20) {
char *string;
string = CkKeysymToString((KeySym) patPtr->detail, 0);
if (string == NULL) {
sprintf(buffer, "Control-%c",
patPtr->detail + 0x40);
string = buffer;
}
Tcl_DStringAppend(&ds, string, -1);
goto endPat;
}
Tcl_DStringAppend(&ds, eiPtr->name, -1);
if (patPtr->detail != 0) {
Tcl_DStringAppend(&ds, "-", 1);
}
break;
}
}
if (patPtr->detail != 0) {
if (patPtr->eventType == CK_EV_KEYPRESS) {
char *string;
string = CkKeysymToString((KeySym) patPtr->detail, 0);
if (string != NULL) {
Tcl_DStringAppend(&ds, string, -1);
}
} else {
sprintf(buffer, "%d", patPtr->detail);
Tcl_DStringAppend(&ds, buffer, -1);
}
}
endPat:
Tcl_DStringAppend(&ds, ">", 1);
}
Tcl_AppendElement(interp, Tcl_DStringValue(&ds));
}
Tcl_DStringFree(&ds);
}
/*
*--------------------------------------------------------------
*
* Ck_DeleteAllBindings --
*
* Remove all bindings associated with a given object in a
* given binding table.
*
* Results:
* All bindings associated with object are removed from
* bindingTable.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void
Ck_DeleteAllBindings(bindingTable, object)
Ck_BindingTable bindingTable; /* Table in which to delete
* bindings. */
ClientData object; /* Token for object. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
PatSeq *psPtr, *prevPtr;
PatSeq *nextPtr;
Tcl_HashEntry *hPtr;
hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
if (hPtr == NULL) {
return;
}
for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr); psPtr != NULL;
psPtr = nextPtr) {
nextPtr = psPtr->nextObjPtr;
/*
* Be sure to remove each binding from its hash chain in the
* pattern table. If this is the last pattern in the chain,
* then delete the hash entry too.
*/
prevPtr = (PatSeq *) Tcl_GetHashValue(psPtr->hPtr);
if (prevPtr == psPtr) {
if (psPtr->nextSeqPtr == NULL) {
Tcl_DeleteHashEntry(psPtr->hPtr);
} else {
Tcl_SetHashValue(psPtr->hPtr, psPtr->nextSeqPtr);
}
} else {
for ( ; ; prevPtr = prevPtr->nextSeqPtr) {
if (prevPtr == NULL) {
panic("Ck_DeleteAllBindings couldn't find on hash chain");
}
if (prevPtr->nextSeqPtr == psPtr) {
prevPtr->nextSeqPtr = psPtr->nextSeqPtr;
break;
}
}
}
ckfree((char *) psPtr->command);
ckfree((char *) psPtr);
}
Tcl_DeleteHashEntry(hPtr);
}
/*
*--------------------------------------------------------------
*
* Ck_BindEvent --
*
* This procedure is invoked to process an event. The
* event is added to those recorded for the binding table.
* Then each of the objects at *objectPtr is checked in
* order to see if it has a binding that matches the recent
* events. If so, that binding is invoked and the rest of
* objects are skipped.
*
* Results:
* None.
*
* Side effects:
* Depends on the command associated with the matching
* binding.
*
*--------------------------------------------------------------
*/
void
Ck_BindEvent(bindingTable, eventPtr, winPtr, numObjects, objectPtr)
Ck_BindingTable bindingTable; /* Table in which to look for
* bindings. */
CkEvent *eventPtr; /* What actually happened. */
CkWindow *winPtr; /* Window where event occurred. */
int numObjects; /* Number of objects at *objectPtr. */
ClientData *objectPtr; /* Array of one or more objects
* to check for a matching binding. */
{
BindingTable *bindPtr = (BindingTable *) bindingTable;
CkMainInfo *mainPtr;
CkEvent *ringPtr;
PatSeq *matchPtr;
PatternTableKey key;
Tcl_HashEntry *hPtr;
int detail, code;
Tcl_Interp *interp;
Tcl_DString scripts, savedResult;
char *p, *end;
/*
* Add the new event to the ring of saved events for the
* binding table.
*/
bindPtr->curEvent++;
if (bindPtr->curEvent >= EVENT_BUFFER_SIZE)
bindPtr->curEvent = 0;
ringPtr = &bindPtr->eventRing[bindPtr->curEvent];
memcpy((VOID *) ringPtr, (VOID *) eventPtr, sizeof (CkEvent));
detail = 0;
bindPtr->detailRing[bindPtr->curEvent] = 0;
if (ringPtr->type == CK_EV_KEYPRESS) {
detail = ringPtr->key.keycode;
}
else if (ringPtr->type == CK_EV_MOUSE_DOWN ||
ringPtr->type == CK_EV_MOUSE_UP ||
ringPtr->type == CK_EV_MOUSE_MOVE) {
detail = ringPtr->mouse.button;
}
else if (ringPtr->type == CK_EV_VIRTUAL) {
//@todo : get rid of this, it is inefficient
int i;
for (i = 0; eventArray[i].name != NULL; ++i) {
if ( eventArray[i].name[0] != '<' ) {
continue;
}
if (!strcmp (eventArray[i].name, ringPtr->virt.evtype)) {
break;
}
}
detail = i;
}
bindPtr->detailRing[bindPtr->curEvent] = detail;
/*
* Loop over all the objects, finding the binding script for each
* one. Append all of the binding scripts, with %-sequences expanded,
* to "scripts", with null characters separating the scripts for
* each object.
*/
Tcl_DStringInit(&scripts);
for ( ; numObjects > 0; numObjects--, objectPtr++) {
/*
* Match the new event against those recorded in the
* pattern table, saving the longest matching pattern.
* For events with details (key events) first
* look for a binding for the specific key or button.
* If none is found, then look for a binding for all
* control-keys (detail of -1, if the keycode is a control
* character), else look for a binding for all keys
* (detail of 0).
*/
matchPtr = NULL;
key.object = *objectPtr;
key.type = ringPtr->type;
key.detail = detail;
hPtr = Tcl_FindHashEntry(&bindPtr->patternTable, (char *) &key);
if (hPtr != NULL) {
matchPtr = MatchPatterns(bindPtr,
(PatSeq *) Tcl_GetHashValue(hPtr));
}
if (ringPtr->type == CK_EV_KEYPRESS && detail > 0 && detail < 0x20 &&
matchPtr == NULL) {
key.detail = -1;
hPtr = Tcl_FindHashEntry(&bindPtr->patternTable, (char *) &key);
if (hPtr != NULL) {
matchPtr = MatchPatterns(bindPtr,
(PatSeq *) Tcl_GetHashValue(hPtr));
}
}
if (detail != 0 && matchPtr == NULL) {
key.detail = 0;
hPtr = Tcl_FindHashEntry(&bindPtr->patternTable, (char *) &key);
if (hPtr != NULL) {
matchPtr = MatchPatterns(bindPtr,
(PatSeq *) Tcl_GetHashValue(hPtr));
}
}
if (matchPtr != NULL) {
ExpandPercents(winPtr, matchPtr->command, eventPtr,
(KeySym) detail, &scripts);
Tcl_DStringAppend(&scripts, "", 1);
}
}
/*
* Now go back through and evaluate the script for each object,
* in order, dealing with "break" and "continue" exceptions
* appropriately.
*
* There are two tricks here:
* 1. Bindings can be invoked from in the middle of Tcl commands,
* where interp->result is significant (for example, a widget
* might be deleted because of an error in creating it, so the
* result contains an error message that is eventually going to
* be returned by the creating command). To preserve the result,
* we save it in a dynamic string.
* 2. The binding's action can potentially delete the binding,
* so bindPtr may not point to anything valid once the action
* completes. Thus we have to save bindPtr->interp in a
* local variable in order to restore the result.
* 3. When the screen changes, must invoke a Tcl script to update
* Tcl level information such as tkPriv.
*/
mainPtr = winPtr->mainPtr;
interp = bindPtr->interp;
Tcl_DStringInit(&savedResult);
Tcl_DStringGetResult(interp, &savedResult);
p = Tcl_DStringValue(&scripts);
end = p + Tcl_DStringLength(&scripts);
while (p != end) {
Tcl_AllowExceptions(interp);
code = Tcl_GlobalEval(interp, p);
if (code != TCL_OK) {
if (code == TCL_CONTINUE) {
/*
* Do nothing: just go on to the next script.
*/
} else if (code == TCL_BREAK) {
break;
} else {
Tcl_AddErrorInfo(interp, "\n (command bound to event)");
Tk_BackgroundError(interp);
break;
}
}
/*
* Skip over the current script and its terminating null character.
*/