Debian Codegraph @

pTk/mTk/generic/tkBind.c

raw symbols SCIP
1 /*
2 * tkBind.c --
3 *
4 * This file provides procedures that associate Tcl commands
5 * with X events or sequences of X events.
6 *
7 * Copyright (c) 1989-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 * Copyright (c) 1998 by Scriptics Corporation.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkBind.c,v 1.28 2003/02/28 15:55:33 dkf Exp $
15 */
16
17 #include "tkPort.h"
18 #include "tkInt.h"
19
20 #ifdef __WIN32__
21 #include "tkWinInt.h"
22 #endif
23
24 #if !(defined(__WIN32__) || defined(MAC_TCL) || defined(MAC_OSX_TK)) /* UNIX */
25 #include "tkUnixInt.h"
26 #endif
27
28
29 /*
30 * File structure:
31 *
32 * Structure definitions and static variables.
33 *
34 * Init/Free this package.
35 *
36 * Tcl "bind" command (actually located in tkCmds.c).
37 * "bind" command implementation.
38 * "bind" implementation helpers.
39 *
40 * Tcl "event" command.
41 * "event" command implementation.
42 * "event" implementation helpers.
43 *
44 * Package-specific common helpers.
45 *
46 * Non-package-specific helpers.
47 */
48
49
50 /*
51 * The following union is used to hold the detail information from an
52 * XEvent (including Tk's XVirtualEvent extension).
53 */
54 typedef union {
55 KeySym keySym; /* KeySym that corresponds to xkey.keycode. */
56 int button; /* Button that was pressed (xbutton.button). */
57 Tk_Uid name; /* Tk_Uid of virtual event. */
58 ClientData clientData; /* Used when type of Detail is unknown, and to
59 * ensure that all bytes of Detail are initialized
60 * when this structure is used in a hash key. */
61 } Detail;
62
63 /*
64 * The structure below represents a binding table. A binding table
65 * represents a domain in which event bindings may occur. It includes
66 * a space of objects relative to which events occur (usually windows,
67 * but not always), a history of recent events in the domain, and
68 * a set of mappings that associate particular Tcl commands with sequences
69 * of events in the domain. Multiple binding tables may exist at once,
70 * either because there are multiple applications open, or because there
71 * are multiple domains within an application with separate event
72 * bindings for each (for example, each canvas widget has a separate
73 * binding table for associating events with the items in the canvas).
74 *
75 * Note: it is probably a bad idea to reduce EVENT_BUFFER_SIZE much
76 * below 30. To see this, consider a triple mouse button click while
77 * the Shift key is down (and auto-repeating). There may be as many
78 * as 3 auto-repeat events after each mouse button press or release
79 * (see the first large comment block within Tk_BindEvent for more on
80 * this), for a total of 20 events to cover the three button presses
81 * and two intervening releases. If you reduce EVENT_BUFFER_SIZE too
82 * much, shift multi-clicks will be lost.
83 *
84 */
85
86 #define EVENT_BUFFER_SIZE 30
87 typedef struct BindingTable {
88 XEvent eventRing[EVENT_BUFFER_SIZE];/* Circular queue of recent events
89 * (higher indices are for more recent
90 * events). */
91 Detail detailRing[EVENT_BUFFER_SIZE];/* "Detail" information (keySym,
92 * button, Tk_Uid, or 0) for each
93 * entry in eventRing. */
94 int curEvent; /* Index in eventRing of most recent
95 * event. Newer events have higher
96 * indices. */
97 Tcl_HashTable patternTable; /* Used to map from an event to a
98 * list of patterns that may match that
99 * event. Keys are PatternTableKey
100 * structs, values are (PatSeq *). */
101 Tcl_HashTable objectTable; /* Used to map from an object to a
102 * list of patterns associated with
103 * that object. Keys are ClientData,
104 * values are (PatSeq *). */
105 Tcl_Interp *interp; /* Interpreter in which commands are
106 * executed. */
107 } BindingTable;
108
109 /*
110 * The following structure represents virtual event table. A virtual event
111 * table provides a way to map from platform-specific physical events such
112 * as button clicks or key presses to virtual events such as <<Paste>>,
113 * <<Close>>, or <<ScrollWindow>>.
114 *
115 * A virtual event is usually never part of the event stream, but instead is
116 * synthesized inline by matching low-level events. However, a virtual
117 * event may be generated by platform-specific code or by Tcl scripts. In
118 * that case, no lookup of the virtual event will need to be done using
119 * this table, because the virtual event is actually in the event stream.
120 */
121
122 typedef struct VirtualEventTable {
123 Tcl_HashTable patternTable; /* Used to map from a physical event to
124 * a list of patterns that may match that
125 * event. Keys are PatternTableKey
126 * structs, values are (PatSeq *). */
127 Tcl_HashTable nameTable; /* Used to map a virtual event name to
128 * the array of physical events that can
129 * trigger it. Keys are the Tk_Uid names
130 * of the virtual events, values are
131 * PhysicalsOwned structs. */
132 } VirtualEventTable;
133
134 /*
135 * The following structure is used as a key in a patternTable for both
136 * binding tables and a virtual event tables.
137 *
138 * In a binding table, the object field corresponds to the binding tag
139 * for the widget whose bindings are being accessed.
140 *
141 * In a virtual event table, the object field is always NULL. Virtual
142 * events are a global definiton and are not tied to a particular
143 * binding tag.
144 *
145 * The same key is used for both types of pattern tables so that the
146 * helper functions that traverse and match patterns will work for both
147 * binding tables and virtual event tables.
148 */
149 typedef struct PatternTableKey {
150 ClientData object; /* For binding table, identifies the binding
151 * tag of the object (or class of objects)
152 * relative to which the event occurred.
153 * For virtual event table, always NULL. */
154 int type; /* Type of event (from X). */
155 Detail detail; /* Additional information, such as keysym,
156 * button, Tk_Uid, or 0 if nothing
157 * additional. */
158 } PatternTableKey;
159
160 /*
161 * The following structure defines a pattern, which is matched against X
162 * events as part of the process of converting X events into Tcl commands.
163 */
164
165 typedef struct Pattern {
166 int eventType; /* Type of X event, e.g. ButtonPress. */
167 int needMods; /* Mask of modifiers that must be
168 * present (0 means no modifiers are
169 * required). */
170 Detail detail; /* Additional information that must
171 * match event. Normally this is 0,
172 * meaning no additional information
173 * must match. For KeyPress and
174 * KeyRelease events, a keySym may
175 * be specified to select a
176 * particular keystroke (0 means any
177 * keystrokes). For button events,
178 * specifies a particular button (0
179 * means any buttons are OK). For virtual
180 * events, specifies the Tk_Uid of the
181 * virtual event name (never 0). */
182 } Pattern;
183
184 /*
185 * The following structure defines a pattern sequence, which consists of one
186 * or more patterns. In order to trigger, a pattern sequence must match
187 * the most recent X events (first pattern to most recent event, next
188 * pattern to next event, and so on). It is used as the hash value in a
189 * patternTable for both binding tables and virtual event tables.
190 *
191 * In a binding table, it is the sequence of physical events that make up
192 * a binding for an object.
193 *
194 * In a virtual event table, it is the sequence of physical events that
195 * define a virtual event.
196 *
197 * The same structure is used for both types of pattern tables so that the
198 * helper functions that traverse and match patterns will work for both
199 * binding tables and virtual event tables.
200 */
201
202 typedef struct PatSeq {
203 int numPats; /* Number of patterns in sequence (usually
204 * 1). */
205 TkBindEvalProc *eventProc; /* The procedure that will be invoked on
206 * the clientData when this pattern sequence
207 * matches. */
208 TkBindFreeProc *freeProc; /* The procedure that will be invoked to
209 * release the clientData when this pattern
210 * sequence is freed. */
211 ClientData clientData; /* Arbitray data passed to eventProc and
212 * freeProc when sequence matches. */
213 int flags; /* Miscellaneous flag values; see below for
214 * definitions. */
215 int refCount; /* Number of times that this binding is in
216 * the midst of executing. If greater than 1,
217 * then a recursive invocation is happening.
218 * Only when this is zero can the binding
219 * actually be freed. */
220 struct PatSeq *nextSeqPtr; /* Next in list of all pattern sequences
221 * that have the same initial pattern. NULL
222 * means end of list. */
223 Tcl_HashEntry *hPtr; /* Pointer to hash table entry for the
224 * initial pattern. This is the head of the
225 * list of which nextSeqPtr forms a part. */
226 struct VirtualOwners *voPtr;/* In a binding table, always NULL. In a
227 * virtual event table, identifies the array
228 * of virtual events that can be triggered by
229 * this event. */
230 struct PatSeq *nextObjPtr; /* In a binding table, next in list of all
231 * pattern sequences for the same object (NULL
232 * for end of list). Needed to implement
233 * Tk_DeleteAllBindings. In a virtual event
234 * table, always NULL. */
235 Pattern pats[1]; /* Array of "numPats" patterns. Only one
236 * element is declared here but in actuality
237 * enough space will be allocated for "numPats"
238 * patterns. To match, pats[0] must match
239 * event n, pats[1] must match event n-1, etc.
240 */
241 ClientData object; /* Identifies object with which event is
242 * associated (e.g. window). */
243 } PatSeq;
244
245 /*
246 * Flag values for PatSeq structures:
247 *
248 * PAT_NEARBY 1 means that all of the events matching
249 * this sequence must occur with nearby X
250 * and Y mouse coordinates and close in time.
251 * This is typically used to restrict multiple
252 * button presses.
253 * MARKED_DELETED 1 means that this binding has been marked as deleted
254 * and removed from the binding table, but its memory
255 * could not be released because it was already queued for
256 * execution. When the binding is actually about to be
257 * executed, this flag will be checked and the binding
258 * skipped if set.
259 */
260
261 #define PAT_NEARBY 0x1
262 #define MARKED_DELETED 0x2
263
264 /*
265 * Constants that define how close together two events must be
266 * in milliseconds or pixels to meet the PAT_NEARBY constraint:
267 */
268
269 #define NEARBY_PIXELS 5
270 #define NEARBY_MS 500
271
272
273 /*
274 * The following structure keeps track of all the virtual events that are
275 * associated with a particular physical event. It is pointed to by the
276 * voPtr field in a PatSeq in the patternTable of a virtual event table.
277 */
278
279 typedef struct VirtualOwners {
280 int numOwners; /* Number of virtual events to trigger. */
281 Tcl_HashEntry *owners[1]; /* Array of pointers to entries in
282 * nameTable. Enough space will
283 * actually be allocated for numOwners
284 * hash entries. */
285 } VirtualOwners;
286
287 /*
288 * The following structure is used in the nameTable of a virtual event
289 * table to associate a virtual event with all the physical events that can
290 * trigger it.
291 */
292 typedef struct PhysicalsOwned {
293 int numOwned; /* Number of physical events owned. */
294 PatSeq *patSeqs[1]; /* Array of pointers to physical event
295 * patterns. Enough space will actually
296 * be allocated to hold numOwned. */
297 } PhysicalsOwned;
298
299 /*
300 * One of the following structures exists for each interpreter. This
301 * structure keeps track of the current display and screen in the
302 * interpreter, so that a script can be invoked whenever the display/screen
303 * changes (the script does things like point tk::Priv at a display-specific
304 * structure).
305 */
306
307 typedef struct {
308 TkDisplay *curDispPtr; /* Display for last binding command invoked
309 * in this application. */
310 int curScreenIndex; /* Index of screen for last binding command. */
311 int bindingDepth; /* Number of active instances of Tk_BindEvent
312 * in this application. */
313 } ScreenInfo;
314
315 /*
316 * The following structure is used to keep track of all the C bindings that
317 * are awaiting invocation and whether the window they refer to has been
318 * destroyed. If the window is destroyed, then all pending callbacks for
319 * that window will be cancelled. The Tcl bindings will still all be
320 * invoked, however.
321 */
322
323 typedef struct PendingBinding {
324 struct PendingBinding *nextPtr;
325 /* Next in chain of pending bindings, in
326 * case a recursive binding evaluation is in
327 * progress. */
328 Tk_Window tkwin; /* The window that the following bindings
329 * depend upon. */
330 int deleted; /* Set to non-zero by window cleanup code
331 * if tkwin is deleted. */
332 PatSeq *matchArray[5]; /* Array of pending C bindings. The actual
333 * size of this depends on how many C bindings
334 * matched the event passed to Tk_BindEvent.
335 * THIS FIELD MUST BE THE LAST IN THE
336 * STRUCTURE. */
337 } PendingBinding;
338
339 /*
340 * The following structure keeps track of all the information local to
341 * the binding package on a per interpreter basis.
342 */
343
344 typedef struct BindInfo {
345 VirtualEventTable virtualEventTable;
346 /* The virtual events that exist in this
347 * interpreter. */
348 ScreenInfo screenInfo; /* Keeps track of the current display and
349 * screen, so it can be restored after
350 * a binding has executed. */
351 PendingBinding *pendingList;/* The list of pending C bindings, kept in
352 * case a C or Tcl binding causes the target
353 * window to be deleted. */
354 int deleted; /* 1 the application has been deleted but
355 * the structure has been preserved. */
356 } BindInfo;
357
358 /*
359 * In X11R4 and earlier versions, XStringToKeysym is ridiculously
360 * slow. The data structure and hash table below, along with the
361 * code that uses them, implement a fast mapping from strings to
362 * keysyms. In X11R5 and later releases XStringToKeysym is plenty
363 * fast so this stuff isn't needed. The #define REDO_KEYSYM_LOOKUP
364 * is normally undefined, so that XStringToKeysym gets used. It
365 * can be set in the Makefile to enable the use of the hash table
366 * below.
367 */
368
369 #ifdef REDO_KEYSYM_LOOKUP
370 typedef struct {
371 char *name; /* Name of keysym. */
372 KeySym value; /* Numeric identifier for keysym. */
373 } KeySymInfo;
374 static KeySymInfo keyArray[] = {
375 #ifndef lint
376 #include "ks_names.h"
377 #endif
378 {(char *) NULL, 0}
379 };
380 static Tcl_HashTable keySymTable; /* keyArray hashed by keysym value. */
381 static Tcl_HashTable nameTable; /* keyArray hashed by keysym name. */
382 #endif /* REDO_KEYSYM_LOOKUP */
383
384 /*
385 * Set to non-zero when the package-wide static variables have been
386 * initialized.
387 */
388
389 static int initialized = 0;
390 TCL_DECLARE_MUTEX(bindMutex)
391
392 /*
393 * A hash table is kept to map from the string names of event
394 * modifiers to information about those modifiers. The structure
395 * for storing this information, and the hash table built at
396 * initialization time, are defined below.
397 */
398
399 typedef struct {
400 char *name; /* Name of modifier. */
401 int mask; /* Button/modifier mask value, * such as Button1Mask. */
402 int flags; /* Various flags; see below for
403 * definitions. */
404 } ModInfo;
405
406 /*
407 * Flags for ModInfo structures:
408 *
409 * DOUBLE - Non-zero means duplicate this event,
410 * e.g. for double-clicks.
411 * TRIPLE - Non-zero means triplicate this event,
412 * e.g. for triple-clicks.
413 * QUADRUPLE - Non-zero means quadruple this event,
414 * e.g. for 4-fold-clicks.
415 * MULT_CLICKS - Combination of all of above.
416 */
417
418 #define DOUBLE 1
419 #define TRIPLE 2
420 #define QUADRUPLE 4
421 #define MULT_CLICKS 7
422
423 static ModInfo modArray[] = {
424 {"Control", ControlMask, 0},
425 {"Shift", ShiftMask, 0},
426 {"Lock", LockMask, 0},
427 {"Meta", META_MASK, 0},
428 {"M", META_MASK, 0},
429 {"Alt", ALT_MASK, 0},
430 {"B1", Button1Mask, 0},
431 {"Button1", Button1Mask, 0},
432 {"B2", Button2Mask, 0},
433 {"Button2", Button2Mask, 0},
434 {"B3", Button3Mask, 0},
435 {"Button3", Button3Mask, 0},
436 {"B4", Button4Mask, 0},
437 {"Button4", Button4Mask, 0},
438 {"B5", Button5Mask, 0},
439 {"Button5", Button5Mask, 0},
440 {"Mod1", Mod1Mask, 0},
441 {"M1", Mod1Mask, 0},
442 {"Command", Mod1Mask, 0},
443 {"Mod2", Mod2Mask, 0},
444 {"M2", Mod2Mask, 0},
445 {"Option", Mod2Mask, 0},
446 {"Mod3", Mod3Mask, 0},
447 {"M3", Mod3Mask, 0},
448 {"Mod4", Mod4Mask, 0},
449 {"M4", Mod4Mask, 0},
450 {"Mod5", Mod5Mask, 0},
451 {"M5", Mod5Mask, 0},
452 {"Double", 0, DOUBLE},
453 {"Triple", 0, TRIPLE},
454 {"Quadruple", 0, QUADRUPLE},
455 {"Any", 0, 0}, /* Ignored: historical relic. */
456 {NULL, 0, 0}
457 };
458 static Tcl_HashTable modTable;
459
460 /*
461 * This module also keeps a hash table mapping from event names
462 * to information about those events. The structure, an array
463 * to use to initialize the hash table, and the hash table are
464 * all defined below.
465 */
466
467 typedef struct {
468 char *name; /* Name of event. */
469 int type; /* Event type for X, such as
470 * ButtonPress. */
471 int eventMask; /* Mask bits (for XSelectInput)
472 * for this event type. */
473 } EventInfo;
474
475 /*
476 * Note: some of the masks below are an OR-ed combination of
477 * several masks. This is necessary because X doesn't report
478 * up events unless you also ask for down events. Also, X
479 * doesn't report button state in motion events unless you've
480 * asked about button events.
481 */
482
483 static EventInfo eventArray[] = {
484 {"Key", KeyPress, KeyPressMask},
485 {"KeyPress", KeyPress, KeyPressMask},
486 {"KeyRelease", KeyRelease, KeyPressMask|KeyReleaseMask},
487 {"Button", ButtonPress, ButtonPressMask},
488 {"ButtonPress", ButtonPress, ButtonPressMask},
489 {"ButtonRelease", ButtonRelease,
490 ButtonPressMask|ButtonReleaseMask},
491 {"Motion", MotionNotify,
492 ButtonPressMask|PointerMotionMask},
493 {"Enter", EnterNotify, EnterWindowMask},
494 {"Leave", LeaveNotify, LeaveWindowMask},
495 {"FocusIn", FocusIn, FocusChangeMask},
496 {"FocusOut", FocusOut, FocusChangeMask},
497 {"Expose", Expose, ExposureMask},
498 {"Visibility", VisibilityNotify, VisibilityChangeMask},
499 {"Destroy", DestroyNotify, StructureNotifyMask},
500 {"Unmap", UnmapNotify, StructureNotifyMask},
501 {"Map", MapNotify, StructureNotifyMask},
502 {"Reparent", ReparentNotify, StructureNotifyMask},
503 {"Configure", ConfigureNotify, StructureNotifyMask},
504 {"Gravity", GravityNotify, StructureNotifyMask},
505 {"Circulate", CirculateNotify, StructureNotifyMask},
506 {"Property", PropertyNotify, PropertyChangeMask},
507 {"Colormap", ColormapNotify, ColormapChangeMask},
508 {"Activate", ActivateNotify, ActivateMask},
509 {"Deactivate", DeactivateNotify, ActivateMask},
510 {"MouseWheel", MouseWheelEvent, MouseWheelMask},
511 {"CirculateRequest", CirculateRequest, SubstructureRedirectMask},
512 {"ConfigureRequest", ConfigureRequest, SubstructureRedirectMask},
513 {"Create", CreateNotify, SubstructureNotifyMask},
514 {"MapRequest", MapRequest, SubstructureRedirectMask},
515 {"ResizeRequest", ResizeRequest, ResizeRedirectMask},
516 {(char *) NULL, 0, 0}
517 };
518 static Tcl_HashTable eventTable;
519
520 /*
521 * The defines and table below are used to classify events into
522 * various groups. The reason for this is that logically identical
523 * fields (e.g. "state") appear at different places in different
524 * types of events. The classification masks can be used to figure
525 * out quickly where to extract information from events.
526 */
527
528 #define KEY 0x1
529 #define BUTTON 0x2
530 #define MOTION 0x4
531 #define CROSSING 0x8
532 #define FOCUS 0x10
533 #define EXPOSE 0x20
534 #define VISIBILITY 0x40
535 #define CREATE 0x80
536 #define DESTROY 0x100
537 #define UNMAP 0x200
538 #define MAP 0x400
539 #define REPARENT 0x800
540 #define CONFIG 0x1000
541 #define GRAVITY 0x2000
542 #define CIRC 0x4000
543 #define PROP 0x8000
544 #define COLORMAP 0x10000
545 #define VIRTUAL 0x20000
546 #define ACTIVATE 0x40000
547 #define MAPREQ 0x80000
548 #define CONFIGREQ 0x100000
549 #define RESIZEREQ 0x200000
550 #define CIRCREQ 0x400000
551
552 char *eventTypeName[TK_LASTEVENT] = {
553 NULL,
554 NULL,
555 "KeyPress",
556 "KeyRelease",
557 "ButtonPress",
558 "ButtonRelease",
559 "MotionNotify",
560 "EnterNotify",
561 "LeaveNotify",
562 "FocusIn",
563 "FocusOut",
564 "KeymapNotify",
565 "Expose",
566 "GraphicsExpose",
567 "NoExpose",
568 "VisibilityNotify",
569 "CreateNotify",
570 "DestroyNotify",
571 "UnmapNotify",
572 "MapNotify",
573 "MapRequest",
574 "ReparentNotify",
575 "ConfigureNotify",
576 "ConfigureRequest",
577 "GravityNotify",
578 "ResizeRequest",
579 "CirculateNotify",
580 "CirculateRequest",
581 "PropertyNotify",
582 "SelectionClear",
583 "SelectionRequest",
584 "SelectionNotify",
585 "ColormapNotify",
586 "ClientMessage",
587 "MappingNotify",
588 "VirtualEvent",
589 "Activate",
590 "Deactivate"
591 };
592
593 #define KEY_BUTTON_MOTION_VIRTUAL (KEY|BUTTON|MOTION|VIRTUAL)
594 #define KEY_BUTTON_MOTION_CROSSING (KEY|BUTTON|MOTION|CROSSING|VIRTUAL)
595
596 static int flagArray[TK_LASTEVENT] = {
597 /* Not used */ 0,
598 /* Not used */ 0,
599 /* KeyPress */ KEY,
600 /* KeyRelease */ KEY,
601 /* ButtonPress */ BUTTON,
602 /* ButtonRelease */ BUTTON,
603 /* MotionNotify */ MOTION,
604 /* EnterNotify */ CROSSING,
605 /* LeaveNotify */ CROSSING,
606 /* FocusIn */ FOCUS,
607 /* FocusOut */ FOCUS,
608 /* KeymapNotify */ 0,
609 /* Expose */ EXPOSE,
610 /* GraphicsExpose */ EXPOSE,
611 /* NoExpose */ 0,
612 /* VisibilityNotify */ VISIBILITY,
613 /* CreateNotify */ CREATE,
614 /* DestroyNotify */ DESTROY,
615 /* UnmapNotify */ UNMAP,
616 /* MapNotify */ MAP,
617 /* MapRequest */ MAPREQ,
618 /* ReparentNotify */ REPARENT,
619 /* ConfigureNotify */ CONFIG,
620 /* ConfigureRequest */ CONFIGREQ,
621 /* GravityNotify */ GRAVITY,
622 /* ResizeRequest */ RESIZEREQ,
623 /* CirculateNotify */ CIRC,
624 /* CirculateRequest */ 0,
625 /* PropertyNotify */ PROP,
626 /* SelectionClear */ 0,
627 /* SelectionRequest */ 0,
628 /* SelectionNotify */ 0,
629 /* ColormapNotify */ COLORMAP,
630 /* ClientMessage */ 0,
631 /* MappingNotify */ 0,
632 /* VirtualEvent */ VIRTUAL,
633 /* Activate */ ACTIVATE,
634 /* Deactivate */ ACTIVATE,
635 /* MouseWheel */ KEY
636 };
637
638 /*
639 * The following table is used to map between the location where an
640 * generated event should be queued and the string used to specify the
641 * location.
642 */
643
644 static TkStateMap queuePosition[] = {
645 {-1, "now"},
646 {TCL_QUEUE_HEAD, "head"},
647 {TCL_QUEUE_MARK, "mark"},
648 {TCL_QUEUE_TAIL, "tail"},
649 {-2, NULL}
650 };
651
652 /*
653 * The following tables are used as a two-way map between X's internal
654 * numeric values for fields in an XEvent and the strings used in Tcl. The
655 * tables are used both when constructing an XEvent from user input and
656 * when providing data from an XEvent to the user.
657 */
658
659 static TkStateMap notifyMode[] = {
660 {NotifyNormal, "NotifyNormal"},
661 {NotifyGrab, "NotifyGrab"},
662 {NotifyUngrab, "NotifyUngrab"},
663 {NotifyWhileGrabbed, "NotifyWhileGrabbed"},
664 {-1, NULL}
665 };
666
667 static TkStateMap notifyDetail[] = {
668 {NotifyAncestor, "NotifyAncestor"},
669 {NotifyVirtual, "NotifyVirtual"},
670 {NotifyInferior, "NotifyInferior"},
671 {NotifyNonlinear, "NotifyNonlinear"},
672 {NotifyNonlinearVirtual, "NotifyNonlinearVirtual"},
673 {NotifyPointer, "NotifyPointer"},
674 {NotifyPointerRoot, "NotifyPointerRoot"},
675 {NotifyDetailNone, "NotifyDetailNone"},
676 {-1, NULL}
677 };
678
679 static TkStateMap circPlace[] = {
680 {PlaceOnTop, "PlaceOnTop"},
681 {PlaceOnBottom, "PlaceOnBottom"},
682 {-1, NULL}
683 };
684
685 static TkStateMap visNotify[] = {
686 {VisibilityUnobscured, "VisibilityUnobscured"},
687 {VisibilityPartiallyObscured, "VisibilityPartiallyObscured"},
688 {VisibilityFullyObscured, "VisibilityFullyObscured"},
689 {-1, NULL}
690 };
691
692 static TkStateMap configureRequestDetail[] = {
693 {None, "None"},
694 {Above, "Above"},
695 {Below, "Below"},
696 {BottomIf, "BottomIf"},
697 {TopIf, "TopIf"},
698 {Opposite, "Opposite"},
699 {-1, NULL}
700 };
701
702 static TkStateMap propNotify[] = {
703 {PropertyNewValue, "NewValue"},
704 {PropertyDelete, "Delete"},
705 {-1, NULL}
706 };
707
708 /*
709 * Prototypes for local procedures defined in this file:
710 */
711
712 #if 0
713 static void ChangeScreen _ANSI_ARGS_((Tcl_Interp *interp,
714 char *dispName, int screenIndex));
715 #endif
716 static int CreateVirtualEvent _ANSI_ARGS_((Tcl_Interp *interp,
717 VirtualEventTable *vetPtr, char *virtString,
718 char *eventString));
719 static int DeleteVirtualEvent _ANSI_ARGS_((Tcl_Interp *interp,
720 VirtualEventTable *vetPtr, char *virtString,
721 char *eventString));
722 static void DeleteVirtualEventTable _ANSI_ARGS_((
723 VirtualEventTable *vetPtr));
724 static void ExpandPercents _ANSI_ARGS_((TkWindow *winPtr,
725 CONST char *before, XEvent *eventPtr, KeySym keySym,
726 Tcl_DString *dsPtr));
727 static void FreeTclBinding _ANSI_ARGS_((ClientData clientData));
728 static PatSeq * FindSequence _ANSI_ARGS_((Tcl_Interp *interp,
729 Tcl_HashTable *patternTablePtr, ClientData object,
730 CONST char *eventString, int create,
731 int allowVirtual, unsigned long *maskPtr));
732 static void GetAllVirtualEvents _ANSI_ARGS_((Tcl_Interp *interp,
733 VirtualEventTable *vetPtr));
734 static char * GetField _ANSI_ARGS_((char *p, char *copy, int size));
735 static void GetPatternString _ANSI_ARGS_((PatSeq *psPtr,
736 Tcl_DString *dsPtr));
737 static int GetVirtualEvent _ANSI_ARGS_((Tcl_Interp *interp,
738 VirtualEventTable *vetPtr, char *virtString));
739 static Tk_Uid GetVirtualEventUid _ANSI_ARGS_((Tcl_Interp *interp,
740 char *virtString));
741 static int HandleEventGenerate _ANSI_ARGS_((Tcl_Interp *interp,
742 Tk_Window mainw, int objc,
743 Tcl_Obj *CONST objv[]));
744 static void InitVirtualEventTable _ANSI_ARGS_((
745 VirtualEventTable *vetPtr));
746 static PatSeq * MatchPatterns _ANSI_ARGS_((TkDisplay *dispPtr,
747 BindingTable *bindPtr, PatSeq *psPtr,
748 PatSeq *bestPtr, ClientData *objectPtr,
749 PatSeq **sourcePtrPtr));
750 static int NameToWindow _ANSI_ARGS_((Tcl_Interp *interp,
751 Tk_Window main, Tcl_Obj *objPtr,
752 Tk_Window *tkwinPtr));
753 static int ParseEventDescription _ANSI_ARGS_((Tcl_Interp *interp,
754 CONST char **eventStringPtr, Pattern *patPtr,
755 unsigned long *eventMaskPtr));
756 static void DoWarp _ANSI_ARGS_((ClientData clientData));
757
758 /*
759 * The following define is used as a short circuit for the callback
760 * procedure to evaluate a TclBinding. The actual evaluation of the
761 * binding is handled inline, because special things have to be done
762 * with a Tcl binding before evaluation time.
763 */
764
765 #define EvalTclBinding ((TkBindEvalProc *) 1)
766
767
768 /*
769 *---------------------------------------------------------------------------
770 *
771 * TkBindInit --
772 *
773 * This procedure is called when an application is created. It
774 * initializes all the structures used by bindings and virtual
775 * events. It must be called before any other functions in this
776 * file are called.
777 *
778 * Results:
779 * None.
780 *
781 * Side effects:
782 * Memory allocated.
783 *
784 *---------------------------------------------------------------------------
785 */
786
787 void
788 TkBindInit(mainPtr)
789 TkMainInfo *mainPtr; /* The newly created application. */
790 {
791 BindInfo *bindInfoPtr;
792
793 if (sizeof(XEvent) < sizeof(XVirtualEvent)) {
794 panic("TkBindInit: virtual events can't be supported");
795 }
796
797 /*
798 * Initialize the static data structures used by the binding package.
799 * They are only initialized once, no matter how many interps are
800 * created.
801 */
802
803 if (!initialized) {
804 Tcl_MutexLock(&bindMutex);
805 if (!initialized) {
806 Tcl_HashEntry *hPtr;
807 ModInfo *modPtr;
808 EventInfo *eiPtr;
809 int dummy;
810
811 #ifdef REDO_KEYSYM_LOOKUP
812 KeySymInfo *kPtr;
813
814 Tcl_InitHashTable(&keySymTable, TCL_STRING_KEYS);
815 Tcl_InitHashTable(&nameTable, TCL_ONE_WORD_KEYS);
816 for (kPtr = keyArray; kPtr->name != NULL; kPtr++) {
817 hPtr = Tcl_CreateHashEntry(&keySymTable, kPtr->name, &dummy);
818 Tcl_SetHashValue(hPtr, kPtr->value);
819 hPtr = Tcl_CreateHashEntry(&nameTable, (char *) kPtr->value,
820 &dummy);
821 Tcl_SetHashValue(hPtr, kPtr->name);
822 }
823 #endif /* REDO_KEYSYM_LOOKUP */
824
825 Tcl_InitHashTable(&modTable, TCL_STRING_KEYS);
826 for (modPtr = modArray; modPtr->name != NULL; modPtr++) {
827 hPtr = Tcl_CreateHashEntry(&modTable, modPtr->name, &dummy);
828 Tcl_SetHashValue(hPtr, modPtr);
829 }
830
831 Tcl_InitHashTable(&eventTable, TCL_STRING_KEYS);
832 for (eiPtr = eventArray; eiPtr->name != NULL; eiPtr++) {
833 hPtr = Tcl_CreateHashEntry(&eventTable, eiPtr->name, &dummy);
834 Tcl_SetHashValue(hPtr, eiPtr);
835 }
836 initialized = 1;
837 }
838 Tcl_MutexUnlock(&bindMutex);
839 }
840
841 mainPtr->bindingTable = Tk_CreateBindingTable(mainPtr->interp);
842
843 bindInfoPtr = (BindInfo *) ckalloc(sizeof(BindInfo));
844 InitVirtualEventTable(&bindInfoPtr->virtualEventTable);
845 bindInfoPtr->screenInfo.curDispPtr = NULL;
846 bindInfoPtr->screenInfo.curScreenIndex = -1;
847 bindInfoPtr->screenInfo.bindingDepth = 0;
848 bindInfoPtr->pendingList = NULL;
849 bindInfoPtr->deleted = 0;
850 mainPtr->bindInfo = (TkBindInfo) bindInfoPtr;
851
852 TkpInitializeMenuBindings(mainPtr->interp, mainPtr->bindingTable);
853 }
854
855 /*
856 *---------------------------------------------------------------------------
857 *
858 * TkBindFree --
859 *
860 * This procedure is called when an application is deleted. It
861 * deletes all the structures used by bindings and virtual events.
862 *
863 * Results:
864 * None.
865 *
866 * Side effects:
867 * Memory freed.
868 *
869 *---------------------------------------------------------------------------
870 */
871
872 void
873 TkBindFree(mainPtr)
874 TkMainInfo *mainPtr; /* The newly created application. */
875 {
876 BindInfo *bindInfoPtr;
877
878 Tk_DeleteBindingTable(mainPtr->bindingTable);
879 mainPtr->bindingTable = NULL;
880
881 bindInfoPtr = (BindInfo *) mainPtr->bindInfo;
882 DeleteVirtualEventTable(&bindInfoPtr->virtualEventTable);
883 bindInfoPtr->deleted = 1;
884 Tcl_EventuallyFree((ClientData) bindInfoPtr, TCL_DYNAMIC);
885 mainPtr->bindInfo = NULL;
886 }
887
888 /*
889 *--------------------------------------------------------------
890 *
891 * Tk_CreateBindingTable --
892 *
893 * Set up a new domain in which event bindings may be created.
894 *
895 * Results:
896 * The return value is a token for the new table, which must
897 * be passed to procedures like Tk_CreateBinding.
898 *
899 * Side effects:
900 * Memory is allocated for the new table.
901 *
902 *--------------------------------------------------------------
903 */
904
905 Tk_BindingTable
906 Tk_CreateBindingTable(interp)
907 Tcl_Interp *interp; /* Interpreter to associate with the binding
908 * table: commands are executed in this
909 * interpreter. */
910 {
911 BindingTable *bindPtr;
912 int i;
913
914 /*
915 * Create and initialize a new binding table.
916 */
917
918 bindPtr = (BindingTable *) ckalloc(sizeof(BindingTable));
919 for (i = 0; i < EVENT_BUFFER_SIZE; i++) {
920 bindPtr->eventRing[i].type = -1;
921 }
922 bindPtr->curEvent = 0;
923 Tcl_InitHashTable(&bindPtr->patternTable,
924 sizeof(PatternTableKey)/sizeof(int));
925 Tcl_InitHashTable(&bindPtr->objectTable, TCL_ONE_WORD_KEYS);
926 bindPtr->interp = interp;
927 return (Tk_BindingTable) bindPtr;
928 }
929
930 /*
931 *--------------------------------------------------------------
932 *
933 * Tk_DeleteBindingTable --
934 *
935 * Destroy a binding table and free up all its memory.
936 * The caller should not use bindingTable again after
937 * this procedure returns.
938 *
939 * Results:
940 * None.
941 *
942 * Side effects:
943 * Memory is freed.
944 *
945 *--------------------------------------------------------------
946 */
947
948 void
949 Tk_DeleteBindingTable(bindingTable)
950 Tk_BindingTable bindingTable; /* Token for the binding table to
951 * destroy. */
952 {
953 BindingTable *bindPtr = (BindingTable *) bindingTable;
954 PatSeq *psPtr, *nextPtr;
955 Tcl_HashEntry *hPtr;
956 Tcl_HashSearch search;
957
958 /*
959 * Find and delete all of the patterns associated with the binding
960 * table.
961 */
962
963 for (hPtr = Tcl_FirstHashEntry(&bindPtr->patternTable, &search);
964 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
965 for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
966 psPtr != NULL; psPtr = nextPtr) {
967 nextPtr = psPtr->nextSeqPtr;
968 psPtr->flags |= MARKED_DELETED;
969 if (psPtr->refCount == 0) {
970 if (psPtr->freeProc != NULL) {
971 (*psPtr->freeProc)(psPtr->clientData);
972 }
973 ckfree((char *) psPtr);
974 }
975 }
976 }
977
978 /*
979 * Clean up the rest of the information associated with the
980 * binding table.
981 */
982
983 Tcl_DeleteHashTable(&bindPtr->patternTable);
984 Tcl_DeleteHashTable(&bindPtr->objectTable);
985 ckfree((char *) bindPtr);
986 }
987
988 /*
989 *--------------------------------------------------------------
990 *
991 * Tk_CreateBinding --
992 *
993 * Add a binding to a binding table, so that future calls to
994 * Tk_BindEvent may execute the command in the binding.
995 *
996 * Results:
997 * The return value is 0 if an error occurred while setting
998 * up the binding. In this case, an error message will be
999 * left in the interp's result. If all went well then the return
1000 * value is a mask of the event types that must be made
1001 * available to Tk_BindEvent in order to properly detect when
1002 * this binding triggers. This value can be used to determine
1003 * what events to select for in a window, for example.
1004 *
1005 * Side effects:
1006 * An existing binding on the same event sequence may be
1007 * replaced.
1008 * The new binding may cause future calls to Tk_BindEvent to
1009 * behave differently than they did previously.
1010 *
1011 *--------------------------------------------------------------
1012 */
1013
1014 unsigned long
1015 Tk_CreateBinding(interp, bindingTable, object, eventString, command, append)
1016 Tcl_Interp *interp; /* Used for error reporting. */
1017 Tk_BindingTable bindingTable;
1018 /* Table in which to create binding. */
1019 ClientData object; /* Token for object with which binding is
1020 * associated. */
1021 CONST char *eventString; /* String describing event sequence that
1022 * triggers binding. */
1023 Tcl_Obj *command; /* Contains Tcl command to execute when
1024 * binding triggers. */
1025 int append; /* 0 means replace any existing binding for
1026 * eventString; 1 means append to that
1027 * binding. If the existing binding is for a
1028 * callback function and not a Tcl command
1029 * string, the existing binding will always be
1030 * replaced. */
1031 {
1032 BindingTable *bindPtr = (BindingTable *) bindingTable;
1033 PatSeq *psPtr;
1034 unsigned long eventMask;
1035 char *new, *old;
1036 LangCallback *callback = NULL;
1037
1038 psPtr = FindSequence(interp, &bindPtr->patternTable, object, eventString,
1039 1, 1, &eventMask);
1040 if (psPtr == NULL) {
1041 return 0;
1042 }
1043 if (psPtr->eventProc == NULL) {
1044 int new;
1045 Tcl_HashEntry *hPtr;
1046
1047 /*
1048 * This pattern sequence was just created.
1049 * Link the pattern into the list associated with the object, so
1050 * that if the object goes away, these bindings will all
1051 * automatically be deleted.
1052 */
1053
1054 hPtr = Tcl_CreateHashEntry(&bindPtr->objectTable, (char *) object,
1055 &new);
1056 if (new) {
1057 psPtr->nextObjPtr = NULL;
1058 } else {
1059 psPtr->nextObjPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
1060 }
1061 Tcl_SetHashValue(hPtr, psPtr);
1062 } else if (psPtr->eventProc != EvalTclBinding) {
1063 /*
1064 * Free existing procedural binding.
1065 */
1066
1067 if (psPtr->freeProc != NULL) {
1068 (*psPtr->freeProc)(psPtr->clientData);
1069 }
1070 psPtr->clientData = NULL;
1071 append = 0;
1072 }
1073
1074 old = (char *) psPtr->clientData;
1075 if ((append != 0) && (old != NULL)) {
1076 int length;
1077
1078 length = strlen(old) + strlen(Tcl_GetString(command)) + 2;
1079 new = (char *) ckalloc((unsigned) length);
1080 sprintf(new, "%s\n%s", old, Tcl_GetString(command));
1081 } else {
1082 callback = LangMakeCallback(command);
1083 }
1084 if (old != NULL) {
1085 ckfree(old);
1086 }
1087 psPtr->eventProc = LangEventCallback;
1088 psPtr->freeProc = (TkBindFreeProc *) LangFreeCallback;
1089 psPtr->clientData = (ClientData) callback;
1090 return eventMask;
1091 }
1092
1093 /*
1094 *---------------------------------------------------------------------------
1095 *
1096 * TkCreateBindingProcedure --
1097 *
1098 * Add a C binding to a binding table, so that future calls to
1099 * Tk_BindEvent may callback the procedure in the binding.
1100 *
1101 * Results:
1102 * The return value is 0 if an error occurred while setting
1103 * up the binding. In this case, an error message will be
1104 * left in the interp's result. If all went well then the return
1105 * value is a mask of the event types that must be made
1106 * available to Tk_BindEvent in order to properly detect when
1107 * this binding triggers. This value can be used to determine
1108 * what events to select for in a window, for example.
1109 *
1110 * Side effects:
1111 * Any existing binding on the same event sequence will be
1112 * replaced.
1113 *
1114 *---------------------------------------------------------------------------
1115 */
1116
1117 unsigned long
1118 TkCreateBindingProcedure(interp, bindingTable, object, eventString,
1119 eventProc, freeProc, clientData)
1120 Tcl_Interp *interp; /* Used for error reporting. */
1121 Tk_BindingTable bindingTable;
1122 /* Table in which to create binding. */
1123 ClientData object; /* Token for object with which binding is
1124 * associated. */
1125 CONST char *eventString; /* String describing event sequence that
1126 * triggers binding. */
1127 TkBindEvalProc *eventProc; /* Procedure to invoke when binding
1128 * triggers. Must not be NULL. */
1129 TkBindFreeProc *freeProc; /* Procedure to invoke when binding is
1130 * freed. May be NULL for no procedure. */
1131 ClientData clientData; /* Arbitrary ClientData to pass to eventProc
1132 * and freeProc. */
1133 {
1134 BindingTable *bindPtr = (BindingTable *) bindingTable;
1135 PatSeq *psPtr;
1136 unsigned long eventMask;
1137
1138 psPtr = FindSequence(interp, &bindPtr->patternTable, object, eventString,
1139 1, 1, &eventMask);
1140 if (psPtr == NULL) {
1141 return 0;
1142 }
1143 if (psPtr->eventProc == NULL) {
1144 int new;
1145 Tcl_HashEntry *hPtr;
1146
1147 /*
1148 * This pattern sequence was just created.
1149 * Link the pattern into the list associated with the object, so
1150 * that if the object goes away, these bindings will all
1151 * automatically be deleted.
1152 */
1153
1154 hPtr = Tcl_CreateHashEntry(&bindPtr->objectTable, (char *) object,
1155 &new);
1156 if (new) {
1157 psPtr->nextObjPtr = NULL;
1158 } else {
1159 psPtr->nextObjPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
1160 }
1161 Tcl_SetHashValue(hPtr, psPtr);
1162 } else {
1163
1164 /*
1165 * Free existing callback.
1166 */
1167
1168 if (psPtr->freeProc != NULL) {
1169 (*psPtr->freeProc)(psPtr->clientData);
1170 }
1171 }
1172
1173 psPtr->eventProc = eventProc;
1174 psPtr->freeProc = freeProc;
1175 psPtr->clientData = clientData;
1176 return eventMask;
1177 }
1178
1179 /*
1180 *--------------------------------------------------------------
1181 *
1182 * Tk_DeleteBinding --
1183 *
1184 * Remove an event binding from a binding table.
1185 *
1186 * Results:
1187 * The result is a standard Tcl return value. If an error
1188 * occurs then the interp's result will contain an error message.
1189 *
1190 * Side effects:
1191 * The binding given by object and eventString is removed
1192 * from bindingTable.
1193 *
1194 *--------------------------------------------------------------
1195 */
1196
1197 int
1198 Tk_DeleteBinding(interp, bindingTable, object, eventString)
1199 Tcl_Interp *interp; /* Used for error reporting. */
1200 Tk_BindingTable bindingTable; /* Table in which to delete binding. */
1201 ClientData object; /* Token for object with which binding
1202 * is associated. */
1203 CONST char *eventString; /* String describing event sequence
1204 * that triggers binding. */
1205 {
1206 BindingTable *bindPtr = (BindingTable *) bindingTable;
1207 PatSeq *psPtr, *prevPtr;
1208 unsigned long eventMask;
1209 Tcl_HashEntry *hPtr;
1210
1211 psPtr = FindSequence(interp, &bindPtr->patternTable, object, eventString,
1212 0, 1, &eventMask);
1213 if (psPtr == NULL) {
1214 Tcl_ResetResult(interp);
1215 return TCL_OK;
1216 }
1217
1218 /*
1219 * Unlink the binding from the list for its object, then from the
1220 * list for its pattern.
1221 */
1222
1223 hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
1224 if (hPtr == NULL) {
1225 panic("Tk_DeleteBinding couldn't find object table entry");
1226 }
1227 prevPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
1228 if (prevPtr == psPtr) {
1229 Tcl_SetHashValue(hPtr, psPtr->nextObjPtr);
1230 } else {
1231 for ( ; ; prevPtr = prevPtr->nextObjPtr) {
1232 if (prevPtr == NULL) {
1233 panic("Tk_DeleteBinding couldn't find on object list");
1234 }
1235 if (prevPtr->nextObjPtr == psPtr) {
1236 prevPtr->nextObjPtr = psPtr->nextObjPtr;
1237 break;
1238 }
1239 }
1240 }
1241 prevPtr = (PatSeq *) Tcl_GetHashValue(psPtr->hPtr);
1242 if (prevPtr == psPtr) {
1243 if (psPtr->nextSeqPtr == NULL) {
1244 Tcl_DeleteHashEntry(psPtr->hPtr);
1245 } else {
1246 Tcl_SetHashValue(psPtr->hPtr, psPtr->nextSeqPtr);
1247 }
1248 } else {
1249 for ( ; ; prevPtr = prevPtr->nextSeqPtr) {
1250 if (prevPtr == NULL) {
1251 panic("Tk_DeleteBinding couldn't find on hash chain");
1252 }
1253 if (prevPtr->nextSeqPtr == psPtr) {
1254 prevPtr->nextSeqPtr = psPtr->nextSeqPtr;
1255 break;
1256 }
1257 }
1258 }
1259
1260 psPtr->flags |= MARKED_DELETED;
1261 if (psPtr->refCount == 0) {
1262 if (psPtr->freeProc != NULL) {
1263 (*psPtr->freeProc)(psPtr->clientData);
1264 }
1265 ckfree((char *) psPtr);
1266 }
1267 return TCL_OK;
1268 }
1269
1270 /*
1271 *--------------------------------------------------------------
1272 *
1273 * Tk_GetBinding --
1274 *
1275 * Return the command associated with a given event string.
1276 *
1277 * Results:
1278 * The return value is a pointer to the command string
1279 * associated with eventString for object in the domain
1280 * given by bindingTable. If there is no binding for
1281 * eventString, or if eventString is improperly formed,
1282 * then NULL is returned and an error message is left in
1283 * the interp's result. The return value is semi-static: it
1284 * will persist until the binding is changed or deleted.
1285 *
1286 * Side effects:
1287 * None.
1288 *
1289 *--------------------------------------------------------------
1290 */
1291
1292 Tcl_Obj *
1293 Tk_GetBinding(interp, bindingTable, object, eventString)
1294 Tcl_Interp *interp; /* Interpreter for error reporting. */
1295 Tk_BindingTable bindingTable; /* Table in which to look for
1296 * binding. */
1297 ClientData object; /* Token for object with which binding
1298 * is associated. */
1299 CONST char *eventString; /* String describing event sequence
1300 * that triggers binding. */
1301 {
1302 BindingTable *bindPtr = (BindingTable *) bindingTable;
1303 PatSeq *psPtr;
1304 unsigned long eventMask;
1305 Tcl_Obj *obj = NULL;
1306
1307 psPtr = FindSequence(interp, &bindPtr->patternTable, object, eventString,
1308 0, 1, &eventMask);
1309 if (psPtr == NULL) {
1310 return NULL;
1311 }
1312 if (psPtr->eventProc == EvalTclBinding) {
1313 return Tcl_NewStringObj((char *) psPtr->clientData, -1);
1314 }
1315 if (psPtr->eventProc == LangEventCallback) {
1316 /* REFCNT is not incremented so mimic what tkConfig.c does */
1317 return LangCallbackObj((LangCallback *) psPtr->clientData);
1318 }
1319 return Tcl_NewStringObj("",0);
1320 }
1321
1322 /*
1323 *--------------------------------------------------------------
1324 *
1325 * Tk_GetAllBindings --
1326 *
1327 * Return a list of event strings for all the bindings
1328 * associated with a given object.
1329 *
1330 * Results:
1331 * There is no return value. The interp's result is modified to
1332 * hold a Tcl list with one entry for each binding associated
1333 * with object in bindingTable. Each entry in the list
1334 * contains the event string associated with one binding.
1335 *
1336 * Side effects:
1337 * None.
1338 *
1339 *--------------------------------------------------------------
1340 */
1341
1342 void
1343 Tk_GetAllBindings(interp, bindingTable, object)
1344 Tcl_Interp *interp; /* Interpreter returning result or
1345 * error. */
1346 Tk_BindingTable bindingTable; /* Table in which to look for
1347 * bindings. */
1348 ClientData object; /* Token for object. */
1349
1350 {
1351 BindingTable *bindPtr = (BindingTable *) bindingTable;
1352 PatSeq *psPtr;
1353 Tcl_HashEntry *hPtr;
1354 Tcl_DString ds;
1355
1356 hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
1357 if (hPtr == NULL) {
1358 return;
1359 }
1360 Tcl_DStringInit(&ds);
1361 for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr); psPtr != NULL;
1362 psPtr = psPtr->nextObjPtr) {
1363 /*
1364 * For each binding, output information about each of the
1365 * patterns in its sequence.
1366 */
1367
1368 Tcl_DStringSetLength(&ds, 0);
1369 GetPatternString(psPtr, &ds);
1370 Tcl_AppendElement(interp, Tcl_DStringValue(&ds));
1371 }
1372 Tcl_DStringFree(&ds);
1373 }
1374
1375 /*
1376 *--------------------------------------------------------------
1377 *
1378 * Tk_DeleteAllBindings --
1379 *
1380 * Remove all bindings associated with a given object in a
1381 * given binding table.
1382 *
1383 * Results:
1384 * All bindings associated with object are removed from
1385 * bindingTable.
1386 *
1387 * Side effects:
1388 * None.
1389 *
1390 *--------------------------------------------------------------
1391 */
1392
1393 void
1394 Tk_DeleteAllBindings(bindingTable, object)
1395 Tk_BindingTable bindingTable; /* Table in which to delete
1396 * bindings. */
1397 ClientData object; /* Token for object. */
1398 {
1399 BindingTable *bindPtr = (BindingTable *) bindingTable;
1400 PatSeq *psPtr, *prevPtr;
1401 PatSeq *nextPtr;
1402 Tcl_HashEntry *hPtr;
1403
1404 hPtr = Tcl_FindHashEntry(&bindPtr->objectTable, (char *) object);
1405 if (hPtr == NULL) {
1406 return;
1407 }
1408 for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr); psPtr != NULL;
1409 psPtr = nextPtr) {
1410 nextPtr = psPtr->nextObjPtr;
1411
1412 /*
1413 * Be sure to remove each binding from its hash chain in the
1414 * pattern table. If this is the last pattern in the chain,
1415 * then delete the hash entry too.
1416 */
1417
1418 prevPtr = (PatSeq *) Tcl_GetHashValue(psPtr->hPtr);
1419 if (prevPtr == psPtr) {
1420 if (psPtr->nextSeqPtr == NULL) {
1421 Tcl_DeleteHashEntry(psPtr->hPtr);
1422 } else {
1423 Tcl_SetHashValue(psPtr->hPtr, psPtr->nextSeqPtr);
1424 }
1425 } else {
1426 for ( ; ; prevPtr = prevPtr->nextSeqPtr) {
1427 if (prevPtr == NULL) {
1428 panic("Tk_DeleteAllBindings couldn't find on hash chain");
1429 }
1430 if (prevPtr->nextSeqPtr == psPtr) {
1431 prevPtr->nextSeqPtr = psPtr->nextSeqPtr;
1432 break;
1433 }
1434 }
1435 }
1436 psPtr->flags |= MARKED_DELETED;
1437
1438 if (psPtr->refCount == 0) {
1439 if (psPtr->freeProc != NULL) {
1440 (*psPtr->freeProc)(psPtr->clientData);
1441 }
1442 ckfree((char *) psPtr);
1443 }
1444 }
1445 Tcl_DeleteHashEntry(hPtr);
1446 }
1447
1448
1449 Tk_Window
1450 Tk_EventWindow(eventPtr)
1451 XEvent *eventPtr;
1452 {
1453 Tk_Window tkwin = Tk_IdToWindow(eventPtr->xany.display, eventPtr->xany.window);
1454 return tkwin;
1455 }
1456
1457
1458 /*
1459 *---------------------------------------------------------------------------
1460 *
1461 * Tk_BindEvent --
1462 *
1463 * This procedure is invoked to process an X event. The
1464 * event is added to those recorded for the binding table.
1465 * Then each of the objects at *objectPtr is checked in
1466 * order to see if it has a binding that matches the recent
1467 * events. If so, the most specific binding is invoked for
1468 * each object.
1469 *
1470 * Results:
1471 * None.
1472 *
1473 * Side effects:
1474 * Depends on the command associated with the matching binding.
1475 *
1476 * All Tcl bindings scripts for each object are accumulated before
1477 * the first binding is evaluated. If the action of a Tcl binding
1478 * is to change or delete a binding, or delete the window associated
1479 * with the binding, all the original Tcl binding scripts will still
1480 * fire. Contrast this with C binding procedures. If a pending C
1481 * binding (one that hasn't fired yet, but is queued to be fired for
1482 * this window) is deleted, it will not be called, and if it is
1483 * changed, then the new binding procedure will be called. If the
1484 * window itself is deleted, no further C binding procedures will be
1485 * called for this window. When both Tcl binding scripts and C binding
1486 * procedures are interleaved, the above rules still apply.
1487 *
1488 *---------------------------------------------------------------------------
1489 */
1490
1491 void
1492 Tk_BindEvent(bindingTable, eventPtr, tkwin, numObjects, objectPtr)
1493 Tk_BindingTable bindingTable; /* Table in which to look for
1494 * bindings. */
1495 XEvent *eventPtr; /* What actually happened. */
1496 Tk_Window tkwin; /* Window on display where event
1497 * occurred (needed in order to
1498 * locate display information). */
1499 int numObjects; /* Number of objects at *objectPtr. */
1500 ClientData *objectPtr; /* Array of one or more objects
1501 * to check for a matching binding. */
1502 {
1503 BindingTable *bindPtr;
1504 TkDisplay *dispPtr;
1505 ScreenInfo *screenPtr;
1506 BindInfo *bindInfoPtr;
1507 TkDisplay *oldDispPtr;
1508 XEvent *ringPtr;
1509 PatSeq *vMatchDetailList, *vMatchNoDetailList;
1510 int flags, oldScreen, i, deferModal;
1511 unsigned int matchCount, matchSpace;
1512 Tcl_Interp *interp;
1513 Tcl_DString scripts;
1514 Tcl_Obj *savedResult;
1515 Detail detail;
1516 char *p, *end;
1517
1518 PendingBinding *pendingPtr;
1519 PendingBinding staticPending;
1520 TkWindow *winPtr = (TkWindow *)tkwin;
1521 PatternTableKey key;
1522 Tk_ClassModalProc *modalProc;
1523 /*
1524 * Ignore events on windows that don't have names: these are windows
1525 * like wrapper windows that shouldn't be visible to the
1526 * application.
1527 */
1528
1529 if (winPtr->pathName == NULL) {
1530 return;
1531 }
1532
1533 /*
1534 * Ignore the event completely if it is an Enter, Leave, FocusIn,
1535 * or FocusOut event with detail NotifyInferior. The reason for
1536 * ignoring these events is that we don't want transitions between
1537 * a window and its children to visible to bindings on the parent:
1538 * this would cause problems for mega-widgets, since the internal
1539 * structure of a mega-widget isn't supposed to be visible to
1540 * people watching the parent.
1541 */
1542
1543 if ((eventPtr->type == EnterNotify) || (eventPtr->type == LeaveNotify)) {
1544 if (eventPtr->xcrossing.detail == NotifyInferior) {
1545 return;
1546 }
1547 }
1548 if ((eventPtr->type == FocusIn) || (eventPtr->type == FocusOut)) {
1549 if (eventPtr->xfocus.detail == NotifyInferior) {
1550 return;
1551 }
1552 }
1553
1554 bindPtr = (BindingTable *) bindingTable;
1555 dispPtr = ((TkWindow *) tkwin)->dispPtr;
1556 bindInfoPtr = (BindInfo *) winPtr->mainPtr->bindInfo;
1557
1558 /*
1559 * Add the new event to the ring of saved events for the
1560 * binding table. Two tricky points:
1561 *
1562 * 1. Combine consecutive MotionNotify events. Do this by putting
1563 * the new event *on top* of the previous event.
1564 * 2. If a modifier key is held down, it auto-repeats to generate
1565 * continuous KeyPress and KeyRelease events. These can flush
1566 * the event ring so that valuable information is lost (such
1567 * as repeated button clicks). To handle this, check for the
1568 * special case of a modifier KeyPress arriving when the previous
1569 * two events are a KeyRelease and KeyPress of the same key.
1570 * If this happens, mark the most recent event (the KeyRelease)
1571 * invalid and put the new event on top of the event before that
1572 * (the KeyPress).
1573 */
1574
1575 if ((eventPtr->type == MotionNotify)
1576 && (bindPtr->eventRing[bindPtr->curEvent].type == MotionNotify)) {
1577 /*
1578 * Don't advance the ring pointer.
1579 */
1580 } else if (eventPtr->type == KeyPress) {
1581 int i;
1582 for (i = 0; ; i++) {
1583 if (i >= dispPtr->numModKeyCodes) {
1584 goto advanceRingPointer;
1585 }
1586 if (dispPtr->modKeyCodes[i] == eventPtr->xkey.keycode) {
1587 break;
1588 }
1589 }
1590 ringPtr = &bindPtr->eventRing[bindPtr->curEvent];
1591 if ((ringPtr->type != KeyRelease)
1592 || (ringPtr->xkey.keycode != eventPtr->xkey.keycode)) {
1593 goto advanceRingPointer;
1594 }
1595 if (bindPtr->curEvent <= 0) {
1596 i = EVENT_BUFFER_SIZE - 1;
1597 } else {
1598 i = bindPtr->curEvent - 1;
1599 }
1600 ringPtr = &bindPtr->eventRing[i];
1601 if ((ringPtr->type != KeyPress)
1602 || (ringPtr->xkey.keycode != eventPtr->xkey.keycode)) {
1603 goto advanceRingPointer;
1604 }
1605 bindPtr->eventRing[bindPtr->curEvent].type = -1;
1606 bindPtr->curEvent = i;
1607 } else {
1608 advanceRingPointer:
1609 bindPtr->curEvent++;
1610 if (bindPtr->curEvent >= EVENT_BUFFER_SIZE) {
1611 bindPtr->curEvent = 0;
1612 }
1613 }
1614 ringPtr = &bindPtr->eventRing[bindPtr->curEvent];
1615 memcpy((VOID *) ringPtr, (VOID *) eventPtr, sizeof(XEvent));
1616 detail.clientData = 0;
1617 flags = flagArray[ringPtr->type];
1618 if (flags & KEY) {
1619 detail.keySym = TkpGetKeySym(dispPtr, ringPtr);
1620 if (detail.keySym == NoSymbol) {
1621 detail.keySym = 0;
1622 }
1623 } else if (flags & BUTTON) {
1624 detail.button = ringPtr->xbutton.button;
1625 } else if (flags & VIRTUAL) {
1626 detail.name = ((XVirtualEvent *) ringPtr)->name;
1627 }
1628 bindPtr->detailRing[bindPtr->curEvent] = detail;
1629
1630 /*
1631 * Find out if there are any virtual events that correspond to this
1632 * physical event (or sequence of physical events).
1633 */
1634
1635 vMatchDetailList = NULL;
1636 vMatchNoDetailList = NULL;
1637 memset(&key, 0, sizeof(key));
1638
1639 if (ringPtr->type != VirtualEvent) {
1640 Tcl_HashTable *veptPtr;
1641 Tcl_HashEntry *hPtr;
1642
1643 veptPtr = &bindInfoPtr->virtualEventTable.patternTable;
1644
1645 key.object = NULL;
1646 key.type = ringPtr->type;
1647 key.detail = detail;
1648
1649 hPtr = Tcl_FindHashEntry(veptPtr, (char *) &key);
1650 if (hPtr != NULL) {
1651 vMatchDetailList = (PatSeq *) Tcl_GetHashValue(hPtr);
1652 }
1653
1654 if (key.detail.clientData != 0) {
1655 key.detail.clientData = 0;
1656 hPtr = Tcl_FindHashEntry(veptPtr, (char *) &key);
1657 if (hPtr != NULL) {
1658 vMatchNoDetailList = (PatSeq *) Tcl_GetHashValue(hPtr);
1659 }
1660 }
1661 }
1662
1663 /*
1664 * Loop over all the binding tags, finding the binding script or
1665 * callback for each one. Append all of the binding scripts, with
1666 * %-sequences expanded, to "scripts", with null characters separating
1667 * the scripts for each object. Append all the callbacks to the array
1668 * of pending callbacks.
1669 */
1670
1671 /* WARNING: this stores stack address - longjmp() and
1672 similar "exception" mechanisms need to take steps
1673 to avoid bypassing the cleanup in this code
1674 */
1675 pendingPtr = &staticPending;
1676 matchCount = 0;
1677 matchSpace = sizeof(staticPending.matchArray) / sizeof(PatSeq *);
1678 Tcl_DStringInit(&scripts);
1679
1680 for ( ; numObjects > 0; numObjects--, objectPtr++) {
1681 PatSeq *matchPtr, *sourcePtr;
1682 Tcl_HashEntry *hPtr;
1683
1684 matchPtr = NULL;
1685 sourcePtr = NULL;
1686
1687 /*
1688 * Match the new event against those recorded in the pattern table,
1689 * saving the longest matching pattern. For events with details
1690 * (button and key events), look for a binding for the specific
1691 * key or button. First see if the event matches a physical event
1692 * that the object is interested in, then look for a virtual event.
1693 */
1694
1695 key.object = *objectPtr;
1696 key.type = ringPtr->type;
1697 key.detail = detail;
1698 hPtr = Tcl_FindHashEntry(&bindPtr->patternTable, (char *) &key);
1699 if (hPtr != NULL) {
1700 matchPtr = MatchPatterns(dispPtr, bindPtr,
1701 (PatSeq *) Tcl_GetHashValue(hPtr), matchPtr, NULL,
1702 &sourcePtr);
1703 }
1704
1705 if (vMatchDetailList != NULL) {
1706 matchPtr = MatchPatterns(dispPtr, bindPtr, vMatchDetailList,
1707 matchPtr, objectPtr, &sourcePtr);
1708 }
1709
1710 /*
1711 * If no match was found, look for a binding for all keys or buttons
1712 * (detail of 0). Again, first match on a virtual event.
1713 */
1714
1715 if ((detail.clientData != 0) && (matchPtr == NULL)) {
1716 key.detail.clientData = 0;
1717 hPtr = Tcl_FindHashEntry(&bindPtr->patternTable, (char *) &key);
1718 if (hPtr != NULL) {
1719 matchPtr = MatchPatterns(dispPtr, bindPtr,
1720 (PatSeq *) Tcl_GetHashValue(hPtr), matchPtr, NULL,
1721 &sourcePtr);
1722 }
1723
1724 if (vMatchNoDetailList != NULL) {
1725 matchPtr = MatchPatterns(dispPtr, bindPtr, vMatchNoDetailList,
1726 matchPtr, objectPtr, &sourcePtr);
1727 }
1728
1729 }
1730
1731 if (matchPtr != NULL) {
1732 if (sourcePtr->eventProc == NULL) {
1733 panic("Tk_BindEvent: missing command");
1734 }
1735 if (sourcePtr->eventProc == EvalTclBinding) {
1736 ExpandPercents(winPtr, (char *) sourcePtr->clientData,
1737 eventPtr, detail.keySym, &scripts);
1738 } else {
1739 if (matchCount >= matchSpace) {
1740 PendingBinding *new;
1741 unsigned int oldSize, newSize;
1742
1743 oldSize = sizeof(staticPending)
1744 - sizeof(staticPending.matchArray)
1745 + matchSpace * sizeof(PatSeq*);
1746 matchSpace *= 2;
1747 newSize = sizeof(staticPending)
1748 - sizeof(staticPending.matchArray)
1749 + matchSpace * sizeof(PatSeq*);
1750 new = (PendingBinding *) ckalloc(newSize);
1751 memcpy((VOID *) new, (VOID *) pendingPtr, oldSize);
1752 if (pendingPtr != &staticPending) {
1753 ckfree((char *) pendingPtr);
1754 }
1755 pendingPtr = new;
1756 }
1757 sourcePtr->refCount++;
1758 pendingPtr->matchArray[matchCount] = sourcePtr;
1759 matchCount++;
1760 }
1761 /*
1762 * A "" is added to the scripts string to separate the
1763 * various scripts that should be invoked.
1764 */
1765
1766 Tcl_DStringAppend(&scripts, "", 1);
1767 }
1768 }
1769 if (Tcl_DStringLength(&scripts) == 0) {
1770 Tcl_DStringFree(&scripts);
1771 return;
1772 }
1773
1774 /*
1775 * Now go back through and evaluate the binding for each object,
1776 * in order, dealing with "break" and "continue" exceptions
1777 * appropriately.
1778 *
1779 * There are two tricks here:
1780 * 1. Bindings can be invoked from in the middle of Tcl commands,
1781 * where the interp's result is significant (for example, a widget
1782 * might be deleted because of an error in creating it, so the
1783 * result contains an error message that is eventually going to
1784 * be returned by the creating command). To preserve the result,
1785 * we save it in a dynamic string.
1786 * 2. The binding's action can potentially delete the binding,
1787 * so bindPtr may not point to anything valid once the action
1788 * completes. Thus we have to save bindPtr->interp in a
1789 * local variable in order to restore the result.
1790 */
1791
1792 interp = bindPtr->interp;
1793 savedResult = Tcl_GetObjResult(interp);
1794 Tcl_IncrRefCount(savedResult);
1795 Tcl_ResetResult(interp);
1796
1797 /*
1798 * Save information about the current screen, then invoke a script
1799 * if the screen has changed.
1800 */
1801
1802 screenPtr = &bindInfoPtr->screenInfo;
1803 oldDispPtr = screenPtr->curDispPtr;
1804 oldScreen = screenPtr->curScreenIndex;
1805 if ((dispPtr != screenPtr->curDispPtr)
1806 || (Tk_ScreenNumber(tkwin) != screenPtr->curScreenIndex)) {
1807 screenPtr->curDispPtr = dispPtr;
1808 screenPtr->curScreenIndex = Tk_ScreenNumber(tkwin);
1809 Tk_ChangeScreen(interp, dispPtr->name, screenPtr->curScreenIndex);
1810 }
1811
1812 if (matchCount > 0) {
1813 /*
1814 * Remember the list of pending C binding callbacks, so we can mark
1815 * them as deleted and not call them if the act of evaluating a C
1816 * or Tcl binding deletes a C binding callback or even the whole
1817 * window.
1818 */
1819
1820 pendingPtr->nextPtr = bindInfoPtr->pendingList;
1821 pendingPtr->tkwin = tkwin;
1822 pendingPtr->deleted = 0;
1823 bindInfoPtr->pendingList = pendingPtr;
1824 }
1825
1826 /*
1827 * Save the current value of the TK_DEFER_MODAL flag so we can
1828 * restore it at the end of the loop. Clear the flag so we can
1829 * detect any recursive requests for a modal loop.
1830 */
1831
1832 flags = winPtr->flags;
1833 winPtr->flags &= ~TK_DEFER_MODAL;
1834
1835 p = Tcl_DStringValue(&scripts);
1836 end = p + Tcl_DStringLength(&scripts);
1837 i = 0;
1838
1839 /*
1840 * Be carefule when dereferencing screenPtr or bindInfoPtr. If we
1841 * evaluate something that destroys ".", bindInfoPtr would have been
1842 * freed, but we can tell that by first checking to see if
1843 * winPtr->mainPtr == NULL.
1844 */
1845
1846 Tcl_Preserve((ClientData) bindInfoPtr);
1847 while (p < end) {
1848 int code;
1849
1850 if (!bindInfoPtr->deleted) {
1851 screenPtr->bindingDepth++;
1852 }
1853 Tcl_AllowExceptions(interp);
1854
1855 if (*p == '\0') {
1856 PatSeq *psPtr;
1857
1858 psPtr = pendingPtr->matchArray[i];
1859 i++;
1860 code = TCL_OK;
1861 if ((pendingPtr->deleted == 0)
1862 && ((psPtr->flags & MARKED_DELETED) == 0)) {
1863 code = (*psPtr->eventProc)(psPtr->clientData, interp, eventPtr,
1864 tkwin, detail.keySym);
1865 if (code == TCL_ERROR) {
1866 Tcl_DString ds;
1867 Tcl_DStringInit(&ds);
1868 GetPatternString(psPtr, &ds);
1869 Tcl_AddErrorInfo(interp, Tcl_DStringValue(&ds));
1870 Tcl_DStringFree(&ds);
1871 }
1872 }
1873 psPtr->refCount--;
1874 if ((psPtr->refCount == 0) && (psPtr->flags & MARKED_DELETED)) {
1875 if (psPtr->freeProc != NULL) {
1876 (*psPtr->freeProc)(psPtr->clientData);
1877 }
1878 ckfree((char *) psPtr);
1879 }
1880 } else {
1881 code = Tcl_GlobalEval(interp, p);
1882 p += strlen(p);
1883 }
1884 p++;
1885
1886 if (!bindInfoPtr->deleted) {
1887 screenPtr->bindingDepth--;
1888 }
1889 if (code != TCL_OK) {
1890 if (code == TCL_CONTINUE) {
1891 /*
1892 * Do nothing: just go on to the next command.
1893 */
1894 } else if (code == TCL_BREAK) {
1895 break;
1896 } else {
1897 Tcl_AddErrorInfo(interp, "\n (command bound to event)");
1898 Tcl_BackgroundError(interp);
1899 break;
1900 }
1901 }
1902 }
1903
1904 if (matchCount > 0 && !pendingPtr->deleted) {
1905 /*
1906 * Restore the original modal flag value and invoke the modal loop
1907 * if needed.
1908 */
1909
1910 deferModal = winPtr->flags & TK_DEFER_MODAL;
1911 winPtr->flags = (winPtr->flags & (unsigned int) ~TK_DEFER_MODAL)
1912 | (flags & TK_DEFER_MODAL);
1913 if (deferModal) {
1914 modalProc = Tk_GetClassProc(winPtr->classProcsPtr, modalProc);
1915 if (modalProc != NULL) {
1916 (*modalProc)(tkwin, eventPtr);
1917 }
1918 }
1919 }
1920
1921 if (!bindInfoPtr->deleted && (screenPtr->bindingDepth != 0)
1922 && ((oldDispPtr != screenPtr->curDispPtr)
1923 || (oldScreen != screenPtr->curScreenIndex))) {
1924
1925 /*
1926 * Some other binding script is currently executing, but its
1927 * screen is no longer current. Change the current display
1928 * back again.
1929 */
1930
1931 screenPtr->curDispPtr = oldDispPtr;
1932 screenPtr->curScreenIndex = oldScreen;
1933 Tk_ChangeScreen(interp, oldDispPtr->name, oldScreen);
1934 }
1935 Tcl_SetObjResult(interp, savedResult);
1936 Tcl_DStringFree(&scripts);
1937
1938 if (matchCount > 0) {
1939 if (!bindInfoPtr->deleted) {
1940 /*
1941 * Delete the pending list from the list of pending scripts
1942 * for this window.
1943 */
1944
1945 PendingBinding **curPtrPtr;
1946
1947 for (curPtrPtr = &bindInfoPtr->pendingList; ; ) {
1948 if (*curPtrPtr == pendingPtr) {
1949 *curPtrPtr = pendingPtr->nextPtr;
1950 break;
1951 }
1952 curPtrPtr = &(*curPtrPtr)->nextPtr;
1953 }
1954 }
1955 if (pendingPtr != &staticPending) {
1956 ckfree((char *) pendingPtr);
1957 }
1958 }
1959 Tcl_Release((ClientData) bindInfoPtr);
1960 }
1961
1962 /*
1963 *---------------------------------------------------------------------------
1964 *
1965 * TkBindDeadWindow --
1966 *
1967 * This procedure is invoked when it is determined that a window is
1968 * dead. It cleans up bind-related information about the window
1969 *
1970 * Results:
1971 * None.
1972 *
1973 * Side effects:
1974 * Any pending C bindings for this window are cancelled.
1975 *
1976 *---------------------------------------------------------------------------
1977 */
1978
1979 void
1980 TkBindDeadWindow(winPtr)
1981 TkWindow *winPtr; /* The window that is being deleted. */
1982 {
1983 BindInfo *bindInfoPtr;
1984 PendingBinding *curPtr;
1985
1986 /*
1987 * Certain special windows like those used for send and clipboard
1988 * have no mainPtr.
1989 */
1990 if (winPtr->mainPtr == NULL)
1991 return;
1992
1993 bindInfoPtr = (BindInfo *) winPtr->mainPtr->bindInfo;
1994 curPtr = bindInfoPtr->pendingList;
1995 while (curPtr != NULL) {
1996 if (curPtr->tkwin == (Tk_Window) winPtr) {
1997 curPtr->deleted = 1;
1998 }
1999 curPtr = curPtr->nextPtr;
2000 }
2001 }
2002
2003 /*
2004 *----------------------------------------------------------------------
2005 *
2006 * MatchPatterns --
2007 *
2008 * Given a list of pattern sequences and a list of recent events,
2009 * return the pattern sequence that best matches the event list,
2010 * if there is one.
2011 *
2012 * This procedure is used in two different ways. In the simplest
2013 * use, "object" is NULL and psPtr is a list of pattern sequences,
2014 * each of which corresponds to a binding. In this case, the
2015 * procedure finds the pattern sequences that match the event list
2016 * and returns the most specific of those, if there is more than one.
2017 *
2018 * In the second case, psPtr is a list of pattern sequences, each
2019 * of which corresponds to a definition for a virtual binding.
2020 * In order for one of these sequences to "match", it must match
2021 * the events (as above) but in addition there must be a binding
2022 * for its associated virtual event on the current object. The
2023 * "object" argument indicates which object the binding must be for.
2024 *
2025 * Results:
2026 * The return value is NULL if bestPtr is NULL and no pattern matches
2027 * the recent events from bindPtr. Otherwise the return value is
2028 * the most specific pattern sequence among bestPtr and all those
2029 * at psPtr that match the event list and object. If a pattern
2030 * sequence other than bestPtr is returned, then *bestCommandPtr
2031 * is filled in with a pointer to the command from the best sequence.
2032 *
2033 * Side effects:
2034 * None.
2035 *
2036 *----------------------------------------------------------------------
2037 */
2038 static PatSeq *
2039 MatchPatterns(dispPtr, bindPtr, psPtr, bestPtr, objectPtr, sourcePtrPtr)
2040 TkDisplay *dispPtr; /* Display from which the event came. */
2041 BindingTable *bindPtr; /* Information about binding table, such as
2042 * ring of recent events. */
2043 PatSeq *psPtr; /* List of pattern sequences. */
2044 PatSeq *bestPtr; /* The best match seen so far, from a
2045 * previous call to this procedure. NULL
2046 * means no prior best match. */
2047 ClientData *objectPtr; /* If NULL, the sequences at psPtr
2048 * correspond to "normal" bindings. If
2049 * non-NULL, the sequences at psPtr correspond
2050 * to virtual bindings; in order to match each
2051 * sequence must correspond to a virtual
2052 * binding for which a binding exists for
2053 * object in bindPtr. */
2054 PatSeq **sourcePtrPtr; /* Filled with the pattern sequence that
2055 * contains the eventProc and clientData
2056 * associated with the best match. If this
2057 * differs from the return value, it is the
2058 * virtual event that most closely matched the
2059 * return value (a physical event). Not
2060 * modified unless a result other than bestPtr
2061 * is returned. */
2062 {
2063 PatSeq *matchPtr, *bestSourcePtr, *sourcePtr;
2064
2065 bestSourcePtr = *sourcePtrPtr;
2066
2067 /*
2068 * Iterate over all the pattern sequences.
2069 */
2070
2071 for ( ; psPtr != NULL; psPtr = psPtr->nextSeqPtr) {
2072 XEvent *eventPtr;
2073 Pattern *patPtr;
2074 Window window;
2075 Detail *detailPtr;
2076 int patCount, ringCount, flags, state;
2077 int modMask;
2078
2079 /*
2080 * Iterate over all the patterns in a sequence to be
2081 * sure that they all match.
2082 */
2083
2084 eventPtr = &bindPtr->eventRing[bindPtr->curEvent];
2085 detailPtr = &bindPtr->detailRing[bindPtr->curEvent];
2086 window = eventPtr->xany.window;
2087 patPtr = psPtr->pats;
2088 patCount = psPtr->numPats;
2089 ringCount = EVENT_BUFFER_SIZE;
2090 while (patCount > 0) {
2091 if (ringCount <= 0) {
2092 goto nextSequence;
2093 }
2094 if (eventPtr->xany.type != patPtr->eventType) {
2095 /*
2096 * Most of the event types are considered superfluous
2097 * in that they are ignored if they occur in the middle
2098 * of a pattern sequence and have mismatching types. The
2099 * only ones that cannot be ignored are ButtonPress and
2100 * ButtonRelease events (if the next event in the pattern
2101 * is a KeyPress or KeyRelease) and KeyPress and KeyRelease
2102 * events (if the next pattern event is a ButtonPress or
2103 * ButtonRelease). Here are some tricky cases to consider:
2104 * 1. Double-Button or Double-Key events.
2105 * 2. Double-ButtonRelease or Double-KeyRelease events.
2106 * 3. The arrival of various events like Enter and Leave
2107 * and FocusIn and GraphicsExpose between two button
2108 * presses or key presses.
2109 * 4. Modifier keys like Shift and Control shouldn't
2110 * generate conflicts with button events.
2111 */
2112
2113 if ((patPtr->eventType == KeyPress)
2114 || (patPtr->eventType == KeyRelease)) {
2115 if ((eventPtr->xany.type == ButtonPress)
2116 || (eventPtr->xany.type == ButtonRelease)) {
2117 goto nextSequence;
2118 }
2119 } else if ((patPtr->eventType == ButtonPress)
2120 || (patPtr->eventType == ButtonRelease)) {
2121 if ((eventPtr->xany.type == KeyPress)
2122 || (eventPtr->xany.type == KeyRelease)) {
2123 int i;
2124
2125 /*
2126 * Ignore key events if they are modifier keys.
2127 */
2128
2129 for (i = 0; i < dispPtr->numModKeyCodes; i++) {
2130 if (dispPtr->modKeyCodes[i]
2131 == eventPtr->xkey.keycode) {
2132 /*
2133 * This key is a modifier key, so ignore it.
2134 */
2135 goto nextEvent;
2136 }
2137 }
2138 goto nextSequence;
2139 }
2140 }
2141 goto nextEvent;
2142 }
2143 if (eventPtr->xany.type == CreateNotify
2144 && eventPtr->xcreatewindow.parent != window) {
2145 goto nextSequence;
2146 } else
2147 if (eventPtr->xany.window != window) {
2148 goto nextSequence;
2149 }
2150
2151 /*
2152 * Note: it's important for the keysym check to go before
2153 * the modifier check, so we can ignore unwanted modifier
2154 * keys before choking on the modifier check.
2155 */
2156
2157 if ((patPtr->detail.clientData != 0)
2158 && (patPtr->detail.clientData != detailPtr->clientData)) {
2159 /*
2160 * The detail appears not to match. However, if the event
2161 * is a KeyPress for a modifier key then just ignore the
2162 * event. Otherwise event sequences like "aD" never match
2163 * because the shift key goes down between the "a" and the
2164 * "D".
2165 */
2166
2167 if (eventPtr->xany.type == KeyPress) {
2168 int i;
2169
2170 for (i = 0; i < dispPtr->numModKeyCodes; i++) {
2171 if (dispPtr->modKeyCodes[i] == eventPtr->xkey.keycode) {
2172 goto nextEvent;
2173 }
2174 }
2175 }
2176 goto nextSequence;
2177 }
2178 flags = flagArray[eventPtr->type];
2179 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2180 state = eventPtr->xkey.state;
2181 } else if (flags & CROSSING) {
2182 state = eventPtr->xcrossing.state;
2183 } else {
2184 state = 0;
2185 }
2186 if (patPtr->needMods != 0) {
2187 modMask = patPtr->needMods;
2188 if ((modMask & META_MASK) && (dispPtr->metaModMask != 0)) {
2189 modMask = (modMask & ~META_MASK) | dispPtr->metaModMask;
2190 }
2191 if ((modMask & ALT_MASK) && (dispPtr->altModMask != 0)) {
2192 modMask = (modMask & ~ALT_MASK) | dispPtr->altModMask;
2193 }
2194
2195 if ((state & META_MASK) && (dispPtr->metaModMask != 0)) {
2196 state = (state & ~META_MASK) | dispPtr->metaModMask;
2197 }
2198 if ((state & ALT_MASK) && (dispPtr->altModMask != 0)) {
2199 state = (state & ~ALT_MASK) | dispPtr->altModMask;
2200 }
2201
2202 if ((state & modMask) != modMask) {
2203 goto nextSequence;
2204 }
2205 }
2206 if (psPtr->flags & PAT_NEARBY) {
2207 XEvent *firstPtr;
2208 int timeDiff;
2209
2210 firstPtr = &bindPtr->eventRing[bindPtr->curEvent];
2211 timeDiff = (Time) firstPtr->xkey.time - eventPtr->xkey.time;
2212 if ((firstPtr->xkey.x_root
2213 < (eventPtr->xkey.x_root - NEARBY_PIXELS))
2214 || (firstPtr->xkey.x_root
2215 > (eventPtr->xkey.x_root + NEARBY_PIXELS))
2216 || (firstPtr->xkey.y_root
2217 < (eventPtr->xkey.y_root - NEARBY_PIXELS))
2218 || (firstPtr->xkey.y_root
2219 > (eventPtr->xkey.y_root + NEARBY_PIXELS))
2220 || (timeDiff > NEARBY_MS)) {
2221 goto nextSequence;
2222 }
2223 }
2224 patPtr++;
2225 patCount--;
2226 nextEvent:
2227 if (eventPtr == bindPtr->eventRing) {
2228 eventPtr = &bindPtr->eventRing[EVENT_BUFFER_SIZE-1];
2229 detailPtr = &bindPtr->detailRing[EVENT_BUFFER_SIZE-1];
2230 } else {
2231 eventPtr--;
2232 detailPtr--;
2233 }
2234 ringCount--;
2235 }
2236
2237 matchPtr = psPtr;
2238 sourcePtr = psPtr;
2239
2240 if (objectPtr != NULL) {
2241 int iVirt;
2242 VirtualOwners *voPtr;
2243 PatternTableKey key;
2244
2245 /*
2246 * The sequence matches the physical constraints.
2247 * Is this object interested in any of the virtual events
2248 * that correspond to this sequence?
2249 */
2250
2251 voPtr = psPtr->voPtr;
2252
2253 memset(&key, 0, sizeof(key));
2254 key.object = *objectPtr;
2255 key.type = VirtualEvent;
2256 key.detail.clientData = 0;
2257
2258 for (iVirt = 0; iVirt < voPtr->numOwners; iVirt++) {
2259 Tcl_HashEntry *hPtr = voPtr->owners[iVirt];
2260
2261 key.detail.name = (Tk_Uid) Tcl_GetHashKey(hPtr->tablePtr,
2262 hPtr);
2263 hPtr = Tcl_FindHashEntry(&bindPtr->patternTable,
2264 (char *) &key);
2265 if (hPtr != NULL) {
2266
2267 /*
2268 * This tag is interested in this virtual event and its
2269 * corresponding physical event is a good match with the
2270 * virtual event's definition.
2271 */
2272
2273 PatSeq *virtMatchPtr;
2274
2275 virtMatchPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
2276 if ((virtMatchPtr->numPats != 1)
2277 || (virtMatchPtr->nextSeqPtr != NULL)) {
2278 panic("MatchPattern: badly constructed virtual event");
2279 }
2280 sourcePtr = virtMatchPtr;
2281 goto match;
2282 }
2283 }
2284
2285 /*
2286 * The physical event matches a virtual event's definition, but
2287 * the tag isn't interested in it.
2288 */
2289 goto nextSequence;
2290 }
2291 match:
2292
2293 /*
2294 * This sequence matches. If we've already got another match,
2295 * pick whichever is most specific. Detail is most important,
2296 * then needMods.
2297 */
2298
2299 if (bestPtr != NULL) {
2300 Pattern *patPtr2;
2301 int i;
2302
2303 if (matchPtr->numPats != bestPtr->numPats) {
2304 if (bestPtr->numPats > matchPtr->numPats) {
2305 goto nextSequence;
2306 } else {
2307 goto newBest;
2308 }
2309 }
2310 for (i = 0, patPtr = matchPtr->pats, patPtr2 = bestPtr->pats;
2311 i < matchPtr->numPats; i++, patPtr++, patPtr2++) {
2312 if (patPtr->detail.clientData != patPtr2->detail.clientData) {
2313 if (patPtr->detail.clientData == 0) {
2314 goto nextSequence;
2315 } else {
2316 goto newBest;
2317 }
2318 }
2319 if (patPtr->needMods != patPtr2->needMods) {
2320 if ((patPtr->needMods & patPtr2->needMods)
2321 == patPtr->needMods) {
2322 goto nextSequence;
2323 } else if ((patPtr->needMods & patPtr2->needMods)
2324 == patPtr2->needMods) {
2325 goto newBest;
2326 }
2327 }
2328 }
2329 /*
2330 * Tie goes to current best pattern.
2331 *
2332 * (1) For virtual vs. virtual, the least recently defined
2333 * virtual wins, because virtuals are examined in order of
2334 * definition. This order is _not_ guaranteed in the
2335 * documentation.
2336 *
2337 * (2) For virtual vs. physical, the physical wins because all
2338 * the physicals are examined before the virtuals. This order
2339 * is guaranteed in the documentation.
2340 *
2341 * (3) For physical vs. physical pattern, the most recently
2342 * defined physical wins, because physicals are examined in
2343 * reverse order of definition. This order is guaranteed in
2344 * the documentation.
2345 */
2346
2347 goto nextSequence;
2348 }
2349 newBest:
2350 bestPtr = matchPtr;
2351 bestSourcePtr = sourcePtr;
2352
2353 nextSequence:
2354 continue;
2355 }
2356
2357 *sourcePtrPtr = bestSourcePtr;
2358 return bestPtr;
2359 }
2360
2361
2362
2363 #ifdef __OPEN32__
2364 extern char StashedKey;
2365 #endif
2366 /*
2367 *--------------------------------------------------------------
2368 *
2369 * ExpandPercents --
2370 *
2371 * Given a command and an event, produce a new command
2372 * by replacing % constructs in the original command
2373 * with information from the X event.
2374 *
2375 * Results:
2376 * The new expanded command is appended to the dynamic string
2377 * given by dsPtr.
2378 *
2379 * Side effects:
2380 * None.
2381 *
2382 *--------------------------------------------------------------
2383 */
2384 static void
2385 ExpandPercents(winPtr, before, eventPtr, keySym, dsPtr)
2386 TkWindow *winPtr; /* Window where event occurred: needed to
2387 * get input context. */
2388 CONST char *before; /* Command containing percent expressions
2389 * to be replaced. */
2390 XEvent *eventPtr; /* X event containing information to be
2391 * used in % replacements. */
2392 KeySym keySym; /* KeySym: only relevant for KeyPress and
2393 * KeyRelease events). */
2394 Tcl_DString *dsPtr; /* Dynamic string in which to append new
2395 * command. */
2396 {
2397 int spaceNeeded, cvtFlags; /* Used to substitute string as proper Tcl
2398 * list element. */
2399 int number, flags, length;
2400 #define NUM_SIZE 40
2401 CONST char *string;
2402 Tcl_DString buf;
2403 char numStorage[NUM_SIZE+1];
2404
2405 Tcl_DStringInit(&buf);
2406
2407 if (eventPtr->type < TK_LASTEVENT) {
2408 flags = flagArray[eventPtr->type];
2409 } else {
2410 flags = 0;
2411 }
2412 while (1) {
2413 /*
2414 * Find everything up to the next % character and append it
2415 * to the result string.
2416 */
2417
2418 for (string = before; (*string != 0) && (*string != '%'); string++) {
2419 /* Empty loop body. */
2420 }
2421 if (string != before) {
2422 Tcl_DStringAppend(dsPtr, before, (int) (string-before));
2423 before = string;
2424 }
2425 if (*before == 0) {
2426 break;
2427 }
2428
2429 /*
2430 * There's a percent sequence here. Process it.
2431 */
2432
2433 number = 0;
2434 string = "??";
2435 switch (before[1]) {
2436 case '#':
2437 number = eventPtr->xany.serial;
2438 goto doNumber;
2439 case 'a':
2440 if (flags & CONFIG) {
2441 TkpPrintWindowId(numStorage, eventPtr->xconfigure.above);
2442 string = numStorage;
2443 }
2444 goto doString;
2445 case 'b':
2446 if (flags & BUTTON) {
2447 number = eventPtr->xbutton.button;
2448 goto doNumber;
2449 }
2450 goto doString;
2451 case 'c':
2452 if (flags & EXPOSE) {
2453 number = eventPtr->xexpose.count;
2454 goto doNumber;
2455 }
2456 goto doString;
2457 case 'd':
2458 if (flags & (CROSSING|FOCUS)) {
2459 if (flags & FOCUS) {
2460 number = eventPtr->xfocus.detail;
2461 } else {
2462 number = eventPtr->xcrossing.detail;
2463 }
2464 string = TkFindStateString(notifyDetail, number);
2465 } else if (flags & CONFIGREQ) {
2466 if (eventPtr->xconfigurerequest.value_mask & CWStackMode) {
2467 string = TkFindStateString(configureRequestDetail,
2468 eventPtr->xconfigurerequest.detail);
2469 } else {
2470 string = "";
2471 }
2472 }
2473 goto doString;
2474 case 'f':
2475 if (flags & CROSSING) {
2476 number = eventPtr->xcrossing.focus;
2477 goto doNumber;
2478 }
2479 goto doString;
2480 case 'h':
2481 if (flags & EXPOSE) {
2482 number = eventPtr->xexpose.height;
2483 } else if (flags & (CONFIG)) {
2484 number = eventPtr->xconfigure.height;
2485 } else if (flags & CREATE) {
2486 number = eventPtr->xcreatewindow.height;
2487 } else if (flags & CONFIGREQ) {
2488 number = eventPtr->xconfigurerequest.height;
2489 } else if (flags & RESIZEREQ) {
2490 number = eventPtr->xresizerequest.height;
2491 } else {
2492 goto doString;
2493 }
2494 goto doNumber;
2495 case 'i':
2496 if (flags & CREATE) {
2497 TkpPrintWindowId(numStorage, eventPtr->xcreatewindow.window);
2498 } else if (flags & CONFIGREQ) {
2499 TkpPrintWindowId(numStorage, eventPtr->xconfigurerequest.window);
2500 } else if (flags & MAPREQ) {
2501 TkpPrintWindowId(numStorage, eventPtr->xmaprequest.window);
2502 } else {
2503 TkpPrintWindowId(numStorage, eventPtr->xany.window);
2504 }
2505 string = numStorage;
2506 goto doString;
2507 case 'k':
2508 if (flags & KEY) {
2509 number = eventPtr->xkey.keycode;
2510 goto doNumber;
2511 }
2512 goto doString;
2513 case 'm':
2514 if (flags & CROSSING) {
2515 number = eventPtr->xcrossing.mode;
2516 string = TkFindStateString(notifyMode, number);
2517 } else if (flags & FOCUS) {
2518 number = eventPtr->xfocus.mode;
2519 string = TkFindStateString(notifyMode, number);
2520 }
2521 goto doString;
2522 case 'o':
2523 if (flags & CREATE) {
2524 number = eventPtr->xcreatewindow.override_redirect;
2525 } else if (flags & MAP) {
2526 number = eventPtr->xmap.override_redirect;
2527 } else if (flags & REPARENT) {
2528 number = eventPtr->xreparent.override_redirect;
2529 } else if (flags & CONFIG) {
2530 number = eventPtr->xconfigure.override_redirect;
2531 } else {
2532 goto doString;
2533 }
2534 goto doNumber;
2535 case 'p':
2536 if (flags & CIRC) {
2537 string = TkFindStateString(circPlace, eventPtr->xcirculate.place);
2538 } else if (flags & CIRCREQ) {
2539 string = TkFindStateString(circPlace, eventPtr->xcirculaterequest.place);
2540 }
2541 goto doString;
2542 case 's':
2543 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2544 number = eventPtr->xkey.state;
2545 } else if (flags & CROSSING) {
2546 number = eventPtr->xcrossing.state;
2547 } else if (flags & PROP) {
2548 string = TkFindStateString(propNotify,
2549 eventPtr->xproperty.state);
2550 goto doString;
2551 } else if (flags & VISIBILITY) {
2552 string = TkFindStateString(visNotify,
2553 eventPtr->xvisibility.state);
2554 goto doString;
2555 } else {
2556 goto doString;
2557 }
2558 goto doNumber;
2559 case 't':
2560 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2561 number = (int) eventPtr->xkey.time;
2562 } else if (flags & CROSSING) {
2563 number = (int) eventPtr->xcrossing.time;
2564 } else if (flags & PROP) {
2565 number = (int) eventPtr->xproperty.time;
2566 } else {
2567 goto doString;
2568 }
2569 goto doNumber;
2570 case 'v':
2571 number = eventPtr->xconfigurerequest.value_mask;
2572 goto doNumber;
2573 case 'w':
2574 if (flags & EXPOSE) {
2575 number = eventPtr->xexpose.width;
2576 } else if (flags & CONFIG) {
2577 number = eventPtr->xconfigure.width;
2578 } else if (flags & CREATE) {
2579 number = eventPtr->xcreatewindow.width;
2580 } else if (flags & CONFIGREQ) {
2581 number = eventPtr->xconfigurerequest.width;
2582 } else if (flags & RESIZEREQ) {
2583 number = eventPtr->xresizerequest.width;
2584 } else {
2585 goto doString;
2586 }
2587 goto doNumber;
2588 case 'x':
2589 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2590 number = eventPtr->xkey.x;
2591 } else if (flags & CROSSING) {
2592 number = eventPtr->xcrossing.x;
2593 } else if (flags & EXPOSE) {
2594 number = eventPtr->xexpose.x;
2595 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
2596 number = eventPtr->xcreatewindow.x;
2597 } else if (flags & REPARENT) {
2598 number = eventPtr->xreparent.x;
2599 } else if (flags & CREATE) {
2600 number = eventPtr->xcreatewindow.x;
2601 } else if (flags & CONFIGREQ) {
2602 number = eventPtr->xconfigurerequest.x;
2603 } else {
2604 goto doString;
2605 }
2606 goto doNumber;
2607 case 'y':
2608 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2609 number = eventPtr->xkey.y;
2610 } else if (flags & EXPOSE) {
2611 number = eventPtr->xexpose.y;
2612 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
2613 number = eventPtr->xcreatewindow.y;
2614 } else if (flags & REPARENT) {
2615 number = eventPtr->xreparent.y;
2616 } else if (flags & CROSSING) {
2617 number = eventPtr->xcrossing.y;
2618 } else if (flags & CREATE) {
2619 number = eventPtr->xcreatewindow.y;
2620 } else if (flags & CONFIGREQ) {
2621 number = eventPtr->xconfigurerequest.y;
2622 } else {
2623 goto doString;
2624 }
2625 goto doNumber;
2626 case 'A':
2627 if (flags & KEY) {
2628 Tcl_DStringFree(&buf);
2629 string = TkpGetString(winPtr, eventPtr, &buf);
2630 }
2631 goto doString;
2632 case 'B':
2633 if (flags & CREATE) {
2634 number = eventPtr->xcreatewindow.border_width;
2635 } else if (flags & CONFIGREQ) {
2636 number = eventPtr->xconfigurerequest.border_width;
2637 } else if (flags & CONFIG) {
2638 number = eventPtr->xconfigure.border_width;
2639 } else {
2640 goto doString;
2641 }
2642 goto doNumber;
2643 case 'D':
2644 /*
2645 * This is used only by the MouseWheel event.
2646 */
2647 if (flags & KEY) {
2648 number = eventPtr->xkey.keycode;
2649 goto doNumber;
2650 }
2651 goto doString;
2652 case 'E':
2653 number = (int) eventPtr->xany.send_event;
2654 goto doNumber;
2655 case 'K':
2656 if (flags & KEY) {
2657 char *name;
2658
2659 name = TkKeysymToString(keySym);
2660 if (name != NULL) {
2661 string = name;
2662 }
2663 }
2664 goto doString;
2665 case 'N':
2666 if (flags & KEY) {
2667 number = (int) keySym;
2668 goto doNumber;
2669 }
2670 goto doString;
2671 case 'P':
2672 if (flags & PROP) {
2673 string = Tk_GetAtomName((Tk_Window) winPtr, eventPtr->xproperty.atom);
2674 }
2675 goto doString;
2676 case 'R':
2677 if (flags & KEY_BUTTON_MOTION_CROSSING) {
2678 TkpPrintWindowId(numStorage, eventPtr->xkey.root);
2679 string = numStorage;
2680 }
2681 goto doString;
2682 case 'S':
2683 if (flags & KEY_BUTTON_MOTION_CROSSING) {
2684 TkpPrintWindowId(numStorage, eventPtr->xkey.subwindow);
2685 string = numStorage;
2686 }
2687 goto doString;
2688 case 'T':
2689 number = eventPtr->type;
2690 goto doNumber;
2691 case 'W': {
2692 Tk_Window tkwin;
2693
2694 tkwin = Tk_IdToWindow(eventPtr->xany.display,
2695 eventPtr->xany.window);
2696 if (tkwin != NULL) {
2697 string = Tk_PathName(tkwin);
2698 } else {
2699 string = "??";
2700 }
2701 goto doString;
2702 }
2703 case 'X':
2704 if (flags & KEY_BUTTON_MOTION_CROSSING) {
2705 Tk_Window tkwin;
2706 int x, y;
2707 int width, height;
2708
2709 number = eventPtr->xkey.x_root;
2710 tkwin = Tk_IdToWindow(eventPtr->xany.display,
2711 eventPtr->xany.window);
2712 if (tkwin != NULL) {
2713 Tk_GetVRootGeometry(tkwin, &x, &y, &width, &height);
2714 number -= x;
2715 }
2716 goto doNumber;
2717 }
2718 goto doString;
2719 case 'Y':
2720 if (flags & KEY_BUTTON_MOTION_CROSSING) {
2721 Tk_Window tkwin;
2722 int x, y;
2723 int width, height;
2724
2725 number = eventPtr->xkey.y_root;
2726 tkwin = Tk_IdToWindow(eventPtr->xany.display,
2727 eventPtr->xany.window);
2728 if (tkwin != NULL) {
2729 Tk_GetVRootGeometry(tkwin, &x, &y, &width, &height);
2730 number -= y;
2731 }
2732 goto doNumber;
2733 }
2734 goto doString;
2735 default:
2736 numStorage[0] = before[1];
2737 numStorage[1] = '\0';
2738 string = numStorage;
2739 goto doString;
2740 }
2741
2742 doNumber:
2743 sprintf(numStorage, "%d", number);
2744 string = numStorage;
2745
2746 doString:
2747 #if 0
2748 spaceNeeded = Tcl_ScanElement(string, &cvtFlags);
2749 length = Tcl_DStringLength(dsPtr);
2750 Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
2751 spaceNeeded = Tcl_ConvertElement(string,
2752 Tcl_DStringValue(dsPtr) + length,
2753 cvtFlags | TCL_DONT_USE_BRACES);
2754 Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
2755 #endif
2756 before += 2;
2757 }
2758 Tcl_DStringFree(&buf);
2759 }
2760
2761
2762 char *
2763 Tk_EventInfo(letter,tkwin,eventPtr,keySym,numPtr,isNum,type, num_size, numStorage)
2764 int letter;
2765 Tk_Window tkwin; /* Window where event occurred: needed to
2766 * get input context. */
2767 XEvent *eventPtr;
2768 KeySym keySym;
2769 int *numPtr;
2770 int *isNum;
2771 int *type;
2772 int num_size;
2773 char *numStorage;
2774 {
2775 TkWindow *winPtr = (TkWindow *) tkwin; /* Window where event occurred: needed to
2776 * get input context. */
2777 Tcl_Obj *arg = NULL;
2778 int number = 0;
2779 int flags = 0;
2780 char *string = NULL;
2781 if (isNum)
2782 *isNum = 0;
2783 if (type)
2784 *type = TK_EVENTTYPE_NONE;
2785
2786 if (eventPtr->type < TK_LASTEVENT) {
2787 flags = flagArray[eventPtr->type];
2788 }
2789
2790 switch (letter) {
2791 case '#':
2792 number = eventPtr->xany.serial;
2793 goto doNumber;
2794 case 'a':
2795 number = (int) eventPtr->xconfigure.above;
2796 TkpPrintWindowId(numStorage, eventPtr->xconfigure.above);
2797 string = numStorage;
2798 goto doWindow;
2799 case 'b':
2800 number = eventPtr->xbutton.button;
2801 goto doNumber;
2802 case 'c':
2803 if (flags & EXPOSE) {
2804 number = eventPtr->xexpose.count;
2805 }
2806 goto doNumber;
2807 case 'd':
2808 if (eventPtr->type == ClientMessage) {
2809 number = (int) eventPtr->xclient.message_type;
2810 goto doAtom;
2811 } else if (eventPtr->type == PropertyNotify) {
2812 number = (int) eventPtr->xproperty.atom;
2813 goto doAtom;
2814 } else if (flags & (CROSSING|FOCUS)) {
2815 if (flags & FOCUS) {
2816 number = eventPtr->xfocus.detail;
2817 } else {
2818 number = eventPtr->xcrossing.detail;
2819 }
2820 string = TkFindStateString(notifyDetail, number);
2821 }
2822 goto doString;
2823
2824 case 'f':
2825 if (eventPtr->type == ClientMessage) {
2826 number = (int) eventPtr->xclient.format;
2827 } else {
2828 number = eventPtr->xcrossing.focus;
2829 }
2830 goto doNumber;
2831
2832 case 'h':
2833 if (flags & EXPOSE) {
2834 number = eventPtr->xexpose.height;
2835 } else if (flags & (CONFIG)) {
2836 number = eventPtr->xconfigure.height;
2837 }
2838 goto doNumber;
2839 case 'k':
2840 number = eventPtr->xkey.keycode;
2841 goto doNumber;
2842 case 'm':
2843 if (flags & CROSSING) {
2844 number = eventPtr->xcrossing.mode;
2845 } else if (flags & FOCUS) {
2846 number = eventPtr->xfocus.mode;
2847 }
2848 string = TkFindStateString(notifyMode, number);
2849 goto doBoth;
2850 case 'o':
2851 if (flags & CREATE) {
2852 number = eventPtr->xcreatewindow.override_redirect;
2853 } else if (flags & MAP) {
2854 number = eventPtr->xmap.override_redirect;
2855 } else if (flags & REPARENT) {
2856 number = eventPtr->xreparent.override_redirect;
2857 } else if (flags & CONFIG) {
2858 number = eventPtr->xconfigure.override_redirect;
2859 }
2860 goto doNumber;
2861 case 'p':
2862 string = TkFindStateString(circPlace, eventPtr->xcirculate.place);
2863 goto doString;
2864 case 's':
2865 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2866 number = eventPtr->xkey.state;
2867 goto doState;
2868 } else if (flags & CROSSING) {
2869 number = eventPtr->xcrossing.state;
2870 goto doState;
2871 } else if (flags & VISIBILITY) {
2872 string = TkFindStateString(visNotify,
2873 eventPtr->xvisibility.state);
2874 goto doString;
2875 } else if (eventPtr->type == PropertyNotify) {
2876 number = (int) eventPtr->xproperty.state;
2877 }
2878 goto doNumber;
2879 case 't':
2880 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2881 number = (int) eventPtr->xkey.time;
2882 } else if (flags & CROSSING) {
2883 number = (int) eventPtr->xcrossing.time;
2884 } else if (flags & PROP) {
2885 number = (int) eventPtr->xproperty.time;
2886 }
2887 goto doNumber;
2888 case 'v':
2889 number = eventPtr->xconfigurerequest.value_mask;
2890 goto doNumber;
2891 case 'w':
2892 if (flags & EXPOSE) {
2893 number = eventPtr->xexpose.width;
2894 } else if (flags & CONFIG) {
2895 number = eventPtr->xconfigure.width;
2896 }
2897 goto doNumber;
2898 case 'x':
2899 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2900 number = eventPtr->xkey.x;
2901 } else if (flags & CROSSING) {
2902 number = eventPtr->xcrossing.x;
2903 } else if (flags & EXPOSE) {
2904 number = eventPtr->xexpose.x;
2905 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
2906 number = eventPtr->xcreatewindow.x;
2907 } else if (flags & REPARENT) {
2908 number = eventPtr->xreparent.x;
2909 }
2910 goto doNumber;
2911 case 'y':
2912 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
2913 number = eventPtr->xkey.y;
2914 } else if (flags & EXPOSE) {
2915 number = eventPtr->xexpose.y;
2916 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
2917 number = eventPtr->xcreatewindow.y;
2918 } else if (flags & REPARENT) {
2919 number = eventPtr->xreparent.y;
2920 } else if (flags & CROSSING) {
2921 number = eventPtr->xcrossing.y;
2922
2923 }
2924 goto doNumber;
2925 case 'A':
2926 if (flags & KEY) {
2927 Tcl_DString buf;
2928 int numChars;
2929 Tcl_DStringInit(&buf);
2930 Tcl_DStringFree(&buf);
2931 string = TkpGetString(winPtr, eventPtr, &buf);
2932 numChars = Tcl_DStringLength(&buf);
2933 if (numChars >= num_size)
2934 numChars = num_size-1;
2935 strncpy(numStorage,string,numChars);
2936 numStorage[numChars] = '\0';
2937 string = numStorage;
2938 Tcl_DStringFree(&buf);
2939 } else if (eventPtr->type == ClientMessage) {
2940 string = (char *) (&eventPtr->xclient.data);
2941 number = sizeof(eventPtr->xclient.data);
2942 goto doData;
2943 }
2944 goto doString;
2945 case 'B':
2946 number = eventPtr->xcreatewindow.border_width;
2947 goto doNumber;
2948 case 'D':
2949 /*
2950 * This is used only by the MouseWheel event.
2951 */
2952
2953 number = eventPtr->xkey.keycode;
2954 goto doNumber;
2955 #if 0
2956 case 'D':
2957 number = (int) eventPtr->xany.display;
2958 goto doDisplay;
2959 #endif
2960 case 'E':
2961 number = (int) eventPtr->xany.send_event;
2962 goto doNumber;
2963 case 'K':
2964 if (flags & KEY) {
2965 char *name;
2966
2967 name = TkKeysymToString(keySym);
2968 if (name != NULL) {
2969 string = name;
2970 }
2971 }
2972 goto doBoth;
2973 case 'N':
2974 number = (int) keySym;
2975 goto doNumber;
2976 case 'R':
2977 number = (int) eventPtr->xkey.root;
2978 TkpPrintWindowId(numStorage, eventPtr->xkey.root);
2979 string = numStorage;
2980 goto doWindow;
2981 case 'S':
2982 number = (int) eventPtr->xkey.subwindow;
2983 TkpPrintWindowId(numStorage, eventPtr->xkey.subwindow);
2984 string = numStorage;
2985 goto doWindow;
2986 case 'T':
2987 number = eventPtr->type;
2988 if (eventPtr->type < LASTEvent)
2989 string = eventTypeName[number];
2990 goto doBoth;
2991 case 'W': {
2992 number = eventPtr->xany.window;
2993 goto doWindow;
2994 }
2995 case 'X': {
2996 Tk_Window tkwin;
2997 int x, y;
2998 int width, height;
2999
3000 number = eventPtr->xkey.x_root;
3001 tkwin = Tk_IdToWindow(eventPtr->xany.display,
3002 eventPtr->xany.window);
3003 if (tkwin != NULL) {
3004 Tk_GetVRootGeometry(tkwin, &x, &y, &width, &height);
3005 number -= x;
3006 }
3007 goto doNumber;
3008 }
3009 case 'Y': {
3010 Tk_Window tkwin;
3011 int x, y;
3012 int width, height;
3013
3014 number = eventPtr->xkey.y_root;
3015 tkwin = Tk_IdToWindow(eventPtr->xany.display,
3016 eventPtr->xany.window);
3017 if (tkwin != NULL) {
3018 Tk_GetVRootGeometry(tkwin, &x, &y, &width, &height);
3019 number -= y;
3020 }
3021 goto doNumber;
3022 }
3023 default:
3024 goto doString;
3025 }
3026
3027 doWindow:
3028 {Tk_Window tkwin = Tk_IdToWindow(eventPtr->xany.display, (Window) number);
3029 if (tkwin)
3030 string = Tk_PathName(tkwin);
3031 else
3032 sprintf(string = numStorage, "0x%x", number);
3033 }
3034 if (type)
3035 *type = TK_EVENTTYPE_WINDOW;
3036 goto doNumeric;
3037
3038 doDisplay:
3039 if (type)
3040 *type = TK_EVENTTYPE_DISPLAY;
3041 string = DisplayString((Display *) number);
3042 goto doNumeric;
3043
3044 doAtom:
3045 {Tk_Window tkwin;
3046 if (type)
3047 *type = TK_EVENTTYPE_ATOM;
3048 if ((tkwin = Tk_EventWindow(eventPtr)))
3049 string = (char *) Tk_GetAtomName(tkwin, (Atom) number);
3050 else
3051 sprintf(string = numStorage, "0x%x", number);
3052 }
3053 goto doNumeric;
3054
3055 doData:
3056 if (type)
3057 *type = TK_EVENTTYPE_DATA;
3058 goto returnNum;
3059
3060 doState:
3061 {
3062 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
3063 ModInfo *modPtr = modArray;
3064 int mods = number;
3065 *type = TK_EVENTTYPE_NUMBER;
3066 string = numStorage;
3067 *string = '\0';
3068 if (dispPtr->metaModMask && mods & dispPtr->metaModMask)
3069 {
3070 strcat(string,"Meta-");
3071 mods &= ~dispPtr->metaModMask;
3072 }
3073 if (dispPtr->altModMask && mods & dispPtr->altModMask)
3074 {
3075 strcat(string,"Alt-");
3076 mods &= ~dispPtr->altModMask;
3077 }
3078 while (mods != 0 && modPtr->name)
3079 {
3080 if (mods & modPtr->mask)
3081 {
3082 strcat(string,modPtr->name);
3083 strcat(string,"-");
3084 mods &= ~modPtr->mask;
3085 }
3086 modPtr++;
3087 }
3088 }
3089 doBoth:
3090 if (type)
3091 *type = TK_EVENTTYPE_STRING;
3092 goto doNumeric;
3093
3094 doNumber:
3095 if (type)
3096 *type = TK_EVENTTYPE_NUMBER;
3097 sprintf(string = numStorage, "%d", number);
3098 doNumeric:
3099 if (isNum)
3100 *isNum = 1;
3101 returnNum:
3102 if (numPtr)
3103 *numPtr = number;
3104 return string;
3105
3106 doString:
3107 if (type)
3108 *type = TK_EVENTTYPE_STRING;
3109 return string;
3110 }
3111
3112
3113 /*
3114 *----------------------------------------------------------------------
3115 *
3116 * FreeScreenInfo --
3117 *
3118 * This procedure is invoked when an interpreter is deleted in
3119 * order to free the ScreenInfo structure associated with the
3120 * "tkBind" AssocData.
3121 *
3122 * Results:
3123 * None.
3124 *
3125 * Side effects:
3126 * Storage is freed.
3127 *
3128 *----------------------------------------------------------------------
3129 */
3130
3131 static void
3132 FreeScreenInfo(clientData, interp)
3133 ClientData clientData; /* Pointer to ScreenInfo structure. */
3134 Tcl_Interp *interp; /* Interpreter that is being deleted. */
3135 {
3136 ckfree((char *) clientData);
3137 }
3138
3139 #if 0
3140 /*
3141 *----------------------------------------------------------------------
3142 *
3143 * ChangeScreen --
3144 *
3145 * This procedure is invoked whenever the current screen changes
3146 * in an application. It invokes a Tcl procedure named
3147 * "tk::ScreenChanged", passing it the screen name as argument.
3148 * tk::ScreenChanged does things like making the tk::Priv variable
3149 * point to an array for the current display.
3150 *
3151 * Results:
3152 * None.
3153 *
3154 * Side effects:
3155 * Depends on what tk::ScreenChanged does. If an error occurs
3156 * them bgerror will be invoked.
3157 *
3158 *----------------------------------------------------------------------
3159 */
3160
3161 static void
3162 ChangeScreen(interp, dispName, screenIndex)
3163 Tcl_Interp *interp; /* Interpreter in which to invoke
3164 * command. */
3165 char *dispName; /* Name of new display. */
3166 int screenIndex; /* Index of new screen. */
3167 {
3168 Tcl_DString cmd;
3169 int code;
3170 char screen[TCL_INTEGER_SPACE];
3171
3172 Tcl_DStringInit(&cmd);
3173 Tcl_DStringAppend(&cmd, "tk::ScreenChanged ", 18);
3174 Tcl_DStringAppend(&cmd, dispName, -1);
3175 sprintf(screen, ".%d", screenIndex);
3176 Tcl_DStringAppend(&cmd, screen, -1);
3177 code = Tcl_GlobalEval(interp, Tcl_DStringValue(&cmd));
3178 if (code != TCL_OK) {
3179 Tcl_AddErrorInfo(interp,
3180 "\n (changing screen in event binding)");
3181 Tcl_BackgroundError(interp);
3182 }
3183 }
3184
3185
3186 #endif
3187
3188 /*
3189 *----------------------------------------------------------------------
3190 *
3191 * Tk_EventCmd --
3192 *
3193 * This procedure is invoked to process the "event" Tcl command.
3194 * It is used to define and generate events.
3195 *
3196 * Results:
3197 * A standard Tcl result.
3198 *
3199 * Side effects:
3200 * See the user documentation.
3201 *
3202 *----------------------------------------------------------------------
3203 */
3204
3205 int
3206 Tk_EventObjCmd(clientData, interp, objc, objv)
3207 ClientData clientData; /* Main window associated with interpreter. */
3208 Tcl_Interp *interp; /* Current interpreter. */
3209 int objc; /* Number of arguments. */
3210 Tcl_Obj *CONST objv[]; /* Argument objects. */
3211 {
3212 int index;
3213 Tk_Window tkwin;
3214 VirtualEventTable *vetPtr;
3215 TkBindInfo bindInfo;
3216 static CONST char *optionStrings[] = {
3217 "add", "delete", "generate", "info",
3218 NULL
3219 };
3220 enum options {
3221 EVENT_ADD, EVENT_DELETE, EVENT_GENERATE, EVENT_INFO
3222 };
3223
3224 tkwin = (Tk_Window) clientData;
3225 bindInfo = ((TkWindow *) tkwin)->mainPtr->bindInfo;
3226 vetPtr = &((BindInfo *) bindInfo)->virtualEventTable;
3227
3228 if (objc < 2) {
3229 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg?");
3230 return TCL_ERROR;
3231 }
3232 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
3233 &index) != TCL_OK) {
3234 return TCL_ERROR;
3235 }
3236
3237 switch ((enum options) index) {
3238 case EVENT_ADD: {
3239 int i;
3240 char *name, *event;
3241
3242 if (objc < 4) {
3243 Tcl_WrongNumArgs(interp, 2, objv,
3244 "virtual sequence ?sequence ...?");
3245 return TCL_ERROR;
3246 }
3247 name = Tcl_GetStringFromObj(objv[2], NULL);
3248 for (i = 3; i < objc; i++) {
3249 event = Tcl_GetStringFromObj(objv[i], NULL);
3250 if (CreateVirtualEvent(interp, vetPtr, name, event) != TCL_OK) {
3251 return TCL_ERROR;
3252 }
3253 }
3254 break;
3255 }
3256 case EVENT_DELETE: {
3257 int i;
3258 char *name, *event;
3259
3260 if (objc < 3) {
3261 Tcl_WrongNumArgs(interp, 2, objv,
3262 "virtual ?sequence sequence ...?");
3263 return TCL_ERROR;
3264 }
3265 name = Tcl_GetStringFromObj(objv[2], NULL);
3266 if (objc == 3) {
3267 return DeleteVirtualEvent(interp, vetPtr, name, NULL);
3268 }
3269 for (i = 3; i < objc; i++) {
3270 event = Tcl_GetStringFromObj(objv[i], NULL);
3271 if (DeleteVirtualEvent(interp, vetPtr, name, event) != TCL_OK) {
3272 return TCL_ERROR;
3273 }
3274 }
3275 break;
3276 }
3277 case EVENT_GENERATE: {
3278 if (objc < 4) {
3279 Tcl_WrongNumArgs(interp, 2, objv, "window event ?options?");
3280 return TCL_ERROR;
3281 }
3282 return HandleEventGenerate(interp, tkwin, objc - 2, objv + 2);
3283 }
3284 case EVENT_INFO: {
3285 if (objc == 2) {
3286 GetAllVirtualEvents(interp, vetPtr);
3287 return TCL_OK;
3288 } else if (objc == 3) {
3289 return GetVirtualEvent(interp, vetPtr,
3290 Tcl_GetStringFromObj(objv[2], NULL));
3291 } else {
3292 Tcl_WrongNumArgs(interp, 2, objv, "?virtual?");
3293 return TCL_ERROR;
3294 }
3295 }
3296 }
3297 return TCL_OK;
3298 }
3299
3300 /*
3301 *---------------------------------------------------------------------------
3302 *
3303 * InitVirtualEventTable --
3304 *
3305 * Given storage for a virtual event table, set up the fields to
3306 * prepare a new domain in which virtual events may be defined.
3307 *
3308 * Results:
3309 * None.
3310 *
3311 * Side effects:
3312 * *vetPtr is now initialized.
3313 *
3314 *---------------------------------------------------------------------------
3315 */
3316
3317 static void
3318 InitVirtualEventTable(vetPtr)
3319 VirtualEventTable *vetPtr; /* Pointer to virtual event table. Memory
3320 * is supplied by the caller. */
3321 {
3322 Tcl_InitHashTable(&vetPtr->patternTable,
3323 sizeof(PatternTableKey) / sizeof(int));
3324 Tcl_InitHashTable(&vetPtr->nameTable, TCL_ONE_WORD_KEYS);
3325 }
3326
3327 /*
3328 *---------------------------------------------------------------------------
3329 *
3330 * DeleteVirtualEventTable --
3331 *
3332 * Delete the contents of a virtual event table. The caller is
3333 * responsible for freeing any memory used by the table itself.
3334 *
3335 * Results:
3336 * None.
3337 *
3338 * Side effects:
3339 * Memory is freed.
3340 *
3341 *---------------------------------------------------------------------------
3342 */
3343
3344 static void
3345 DeleteVirtualEventTable(vetPtr)
3346 VirtualEventTable *vetPtr; /* The virtual event table to delete. */
3347 {
3348 Tcl_HashEntry *hPtr;
3349 Tcl_HashSearch search;
3350 PatSeq *psPtr, *nextPtr;
3351
3352 hPtr = Tcl_FirstHashEntry(&vetPtr->patternTable, &search);
3353 for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
3354 psPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
3355 for ( ; psPtr != NULL; psPtr = nextPtr) {
3356 nextPtr = psPtr->nextSeqPtr;
3357 ckfree((char *) psPtr->voPtr);
3358 ckfree((char *) psPtr);
3359 }
3360 }
3361 Tcl_DeleteHashTable(&vetPtr->patternTable);
3362
3363 hPtr = Tcl_FirstHashEntry(&vetPtr->nameTable, &search);
3364 for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
3365 ckfree((char *) Tcl_GetHashValue(hPtr));
3366 }
3367 Tcl_DeleteHashTable(&vetPtr->nameTable);
3368 }
3369
3370 /*
3371 *----------------------------------------------------------------------
3372 *
3373 * CreateVirtualEvent --
3374 *
3375 * Add a new definition for a virtual event. If the virtual event
3376 * is already defined, the new definition augments those that
3377 * already exist.
3378 *
3379 * Results:
3380 * The return value is TCL_ERROR if an error occured while
3381 * creating the virtual binding. In this case, an error message
3382 * will be left in the interp's result. If all went well then the
3383 * return value is TCL_OK.
3384 *
3385 * Side effects:
3386 * The virtual event may cause future calls to Tk_BindEvent to
3387 * behave differently than they did previously.
3388 *
3389 *----------------------------------------------------------------------
3390 */
3391
3392 static int
3393 CreateVirtualEvent(interp, vetPtr, virtString, eventString)
3394 Tcl_Interp *interp; /* Used for error reporting. */
3395 VirtualEventTable *vetPtr;/* Table in which to augment virtual event. */
3396 char *virtString; /* Name of new virtual event. */
3397 char *eventString; /* String describing physical event that
3398 * triggers virtual event. */
3399 {
3400 PatSeq *psPtr;
3401 int dummy;
3402 Tcl_HashEntry *vhPtr;
3403 unsigned long eventMask;
3404 PhysicalsOwned *poPtr;
3405 VirtualOwners *voPtr;
3406 Tk_Uid virtUid;
3407
3408 virtUid = GetVirtualEventUid(interp, virtString);
3409 if (virtUid == NULL) {
3410 return TCL_ERROR;
3411 }
3412
3413 /*
3414 * Find/create physical event
3415 */
3416
3417 psPtr = FindSequence(interp, &vetPtr->patternTable, NULL, eventString,
3418 1, 0, &eventMask);
3419 if (psPtr == NULL) {
3420 return TCL_ERROR;
3421 }
3422
3423 /*
3424 * Find/create virtual event.
3425 */
3426
3427 vhPtr = Tcl_CreateHashEntry(&vetPtr->nameTable, virtUid, &dummy);
3428
3429 /*
3430 * Make virtual event own the physical event.
3431 */
3432
3433 poPtr = (PhysicalsOwned *) Tcl_GetHashValue(vhPtr);
3434 if (poPtr == NULL) {
3435 poPtr = (PhysicalsOwned *) ckalloc(sizeof(PhysicalsOwned));
3436 poPtr->numOwned = 0;
3437 } else {
3438 /*
3439 * See if this virtual event is already defined for this physical
3440 * event and just return if it is.
3441 */
3442
3443 int i;
3444 for (i = 0; i < poPtr->numOwned; i++) {
3445 if (poPtr->patSeqs[i] == psPtr) {
3446 return TCL_OK;
3447 }
3448 }
3449 poPtr = (PhysicalsOwned *) ckrealloc((char *) poPtr,
3450 sizeof(PhysicalsOwned) + poPtr->numOwned * sizeof(PatSeq *));
3451 }
3452 Tcl_SetHashValue(vhPtr, (ClientData) poPtr);
3453 poPtr->patSeqs[poPtr->numOwned] = psPtr;
3454 poPtr->numOwned++;
3455
3456 /*
3457 * Make physical event so it can trigger the virtual event.
3458 */
3459
3460 voPtr = psPtr->voPtr;
3461 if (voPtr == NULL) {
3462 voPtr = (VirtualOwners *) ckalloc(sizeof(VirtualOwners));
3463 voPtr->numOwners = 0;
3464 } else {
3465 voPtr = (VirtualOwners *) ckrealloc((char *) voPtr,
3466 sizeof(VirtualOwners)
3467 + voPtr->numOwners * sizeof(Tcl_HashEntry *));
3468 }
3469 psPtr->voPtr = voPtr;
3470 voPtr->owners[voPtr->numOwners] = vhPtr;
3471 voPtr->numOwners++;
3472
3473 return TCL_OK;
3474 }
3475
3476 /*
3477 *--------------------------------------------------------------
3478 *
3479 * DeleteVirtualEvent --
3480 *
3481 * Remove the definition of a given virtual event. If the
3482 * event string is NULL, all definitions of the virtual event
3483 * will be removed. Otherwise, just the specified definition
3484 * of the virtual event will be removed.
3485 *
3486 * Results:
3487 * The result is a standard Tcl return value. If an error
3488 * occurs then the interp's result will contain an error message.
3489 * It is not an error to attempt to delete a virtual event that
3490 * does not exist or a definition that does not exist.
3491 *
3492 * Side effects:
3493 * The virtual event given by virtString may be removed from the
3494 * virtual event table.
3495 *
3496 *--------------------------------------------------------------
3497 */
3498
3499 static int
3500 DeleteVirtualEvent(interp, vetPtr, virtString, eventString)
3501 Tcl_Interp *interp; /* Used for error reporting. */
3502 VirtualEventTable *vetPtr;/* Table in which to delete event. */
3503 char *virtString; /* String describing event sequence that
3504 * triggers binding. */
3505 char *eventString; /* The event sequence that should be deleted,
3506 * or NULL to delete all event sequences for
3507 * the entire virtual event. */
3508 {
3509 int iPhys;
3510 Tk_Uid virtUid;
3511 Tcl_HashEntry *vhPtr;
3512 PhysicalsOwned *poPtr;
3513 PatSeq *eventPSPtr;
3514
3515 virtUid = GetVirtualEventUid(interp, virtString);
3516 if (virtUid == NULL) {
3517 return TCL_ERROR;
3518 }
3519
3520 vhPtr = Tcl_FindHashEntry(&vetPtr->nameTable, virtUid);
3521 if (vhPtr == NULL) {
3522 return TCL_OK;
3523 }
3524 poPtr = (PhysicalsOwned *) Tcl_GetHashValue(vhPtr);
3525
3526 eventPSPtr = NULL;
3527 if (eventString != NULL) {
3528 unsigned long eventMask;
3529
3530 /*
3531 * Delete only the specific physical event associated with the
3532 * virtual event. If the physical event doesn't already exist, or
3533 * the virtual event doesn't own that physical event, return w/o
3534 * doing anything.
3535 */
3536
3537 eventPSPtr = FindSequence(interp, &vetPtr->patternTable, NULL,
3538 eventString, 0, 0, &eventMask);
3539 if (eventPSPtr == NULL) {
3540 CONST char *string;
3541
3542 string = Tcl_GetStringResult(interp);
3543 return (string[0] != '\0') ? TCL_ERROR : TCL_OK;
3544 }
3545 }
3546
3547 for (iPhys = poPtr->numOwned; --iPhys >= 0; ) {
3548 PatSeq *psPtr = poPtr->patSeqs[iPhys];
3549 if ((eventPSPtr == NULL) || (psPtr == eventPSPtr)) {
3550 int iVirt;
3551 VirtualOwners *voPtr;
3552
3553 /*
3554 * Remove association between this physical event and the given
3555 * virtual event that it triggers.
3556 */
3557
3558 voPtr = psPtr->voPtr;
3559 for (iVirt = 0; iVirt < voPtr->numOwners; iVirt++) {
3560 if (voPtr->owners[iVirt] == vhPtr) {
3561 break;
3562 }
3563 }
3564 if (iVirt == voPtr->numOwners) {
3565 panic("DeleteVirtualEvent: couldn't find owner");
3566 }
3567 voPtr->numOwners--;
3568 if (voPtr->numOwners == 0) {
3569 /*
3570 * Removed last reference to this physical event, so
3571 * remove it from physical->virtual map.
3572 */
3573 PatSeq *prevPtr = (PatSeq *) Tcl_GetHashValue(psPtr->hPtr);
3574 if (prevPtr == psPtr) {
3575 if (psPtr->nextSeqPtr == NULL) {
3576 Tcl_DeleteHashEntry(psPtr->hPtr);
3577 } else {
3578 Tcl_SetHashValue(psPtr->hPtr,
3579 psPtr->nextSeqPtr);
3580 }
3581 } else {
3582 for ( ; ; prevPtr = prevPtr->nextSeqPtr) {
3583 if (prevPtr == NULL) {
3584 panic("DeleteVirtualEvent couldn't find on hash chain");
3585 }
3586 if (prevPtr->nextSeqPtr == psPtr) {
3587 prevPtr->nextSeqPtr = psPtr->nextSeqPtr;
3588 break;
3589 }
3590 }
3591 }
3592 ckfree((char *) psPtr->voPtr);
3593 ckfree((char *) psPtr);
3594 } else {
3595 /*
3596 * This physical event still triggers some other virtual
3597 * event(s). Consolidate the list of virtual owners for
3598 * this physical event so it no longer triggers the
3599 * given virtual event.
3600 */
3601 voPtr->owners[iVirt] = voPtr->owners[voPtr->numOwners];
3602 }
3603
3604 /*
3605 * Now delete the virtual event's reference to the physical
3606 * event.
3607 */
3608
3609 poPtr->numOwned--;
3610 if (eventPSPtr != NULL && poPtr->numOwned != 0) {
3611 /*
3612 * Just deleting this one physical event. Consolidate list
3613 * of owned physical events and return.
3614 */
3615
3616 poPtr->patSeqs[iPhys] = poPtr->patSeqs[poPtr->numOwned];
3617 return TCL_OK;
3618 }
3619 }
3620 }
3621
3622 if (poPtr->numOwned == 0) {
3623 /*
3624 * All the physical events for this virtual event were deleted,
3625 * either because there was only one associated physical event or
3626 * because the caller was deleting the entire virtual event. Now
3627 * the virtual event itself should be deleted.
3628 */
3629
3630 ckfree((char *) poPtr);
3631 Tcl_DeleteHashEntry(vhPtr);
3632 }
3633 return TCL_OK;
3634 }
3635
3636 /*
3637 *---------------------------------------------------------------------------
3638 *
3639 * GetVirtualEvent --
3640 *
3641 * Return the list of physical events that can invoke the
3642 * given virtual event.
3643 *
3644 * Results:
3645 * The return value is TCL_OK and the interp's result is filled with the
3646 * string representation of the physical events associated with the
3647 * virtual event; if there are no physical events for the given virtual
3648 * event, the interp's result is filled with and empty string. If the
3649 * virtual event string is improperly formed, then TCL_ERROR is
3650 * returned and an error message is left in the interp's result.
3651 *
3652 * Side effects:
3653 * None.
3654 *
3655 *---------------------------------------------------------------------------
3656 */
3657
3658 static int
3659 GetVirtualEvent(interp, vetPtr, virtString)
3660 Tcl_Interp *interp; /* Interpreter for reporting. */
3661 VirtualEventTable *vetPtr;/* Table in which to look for event. */
3662 char *virtString; /* String describing virtual event. */
3663 {
3664 Tcl_HashEntry *vhPtr;
3665 Tcl_DString ds;
3666 int iPhys;
3667 PhysicalsOwned *poPtr;
3668 Tk_Uid virtUid;
3669
3670 virtUid = GetVirtualEventUid(interp, virtString);
3671 if (virtUid == NULL) {
3672 return TCL_ERROR;
3673 }
3674
3675 vhPtr = Tcl_FindHashEntry(&vetPtr->nameTable, virtUid);
3676 if (vhPtr == NULL) {
3677 return TCL_OK;
3678 }
3679
3680 Tcl_DStringInit(&ds);
3681
3682 poPtr = (PhysicalsOwned *) Tcl_GetHashValue(vhPtr);
3683 for (iPhys = 0; iPhys < poPtr->numOwned; iPhys++) {
3684 Tcl_DStringSetLength(&ds, 0);
3685 GetPatternString(poPtr->patSeqs[iPhys], &ds);
3686 Tcl_AppendElement(interp, Tcl_DStringValue(&ds));
3687 }
3688 Tcl_DStringFree(&ds);
3689
3690 return TCL_OK;
3691 }
3692
3693 /*
3694 *--------------------------------------------------------------
3695 *
3696 * GetAllVirtualEvents --
3697 *
3698 * Return a list that contains the names of all the virtual
3699 * event defined.
3700 *
3701 * Results:
3702 * There is no return value. The interp's result is modified to
3703 * hold a Tcl list with one entry for each virtual event in
3704 * nameTable.
3705 *
3706 * Side effects:
3707 * None.
3708 *
3709 *--------------------------------------------------------------
3710 */
3711
3712 static void
3713 GetAllVirtualEvents(interp, vetPtr)
3714 Tcl_Interp *interp; /* Interpreter returning result. */
3715 VirtualEventTable *vetPtr;/* Table containing events. */
3716 {
3717 Tcl_HashEntry *hPtr;
3718 Tcl_HashSearch search;
3719 Tcl_DString ds;
3720
3721 Tcl_DStringInit(&ds);
3722
3723 hPtr = Tcl_FirstHashEntry(&vetPtr->nameTable, &search);
3724 for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
3725 Tcl_DStringSetLength(&ds, 0);
3726 Tcl_DStringAppend(&ds, "<<", 2);
3727 Tcl_DStringAppend(&ds, Tcl_GetHashKey(hPtr->tablePtr, hPtr), -1);
3728 Tcl_DStringAppend(&ds, ">>", 2);
3729 Tcl_AppendElement(interp, Tcl_DStringValue(&ds));
3730 }
3731
3732 Tcl_DStringFree(&ds);
3733 }
3734
3735 /*
3736 *---------------------------------------------------------------------------
3737 *
3738 * HandleEventGenerate --
3739 *
3740 * Helper function for the "event generate" command. Generate and
3741 * process an XEvent, constructed from information parsed from the
3742 * event description string and its optional arguments.
3743 *
3744 * argv[0] contains name of the target window.
3745 * argv[1] contains pattern string for one event (e.g, <Control-v>).
3746 * argv[2..argc-1] contains -field/option pairs for specifying
3747 * additional detail in the generated event.
3748 *
3749 * Either virtual or physical events can be generated this way.
3750 * The event description string must contain the specification
3751 * for only one event.
3752 *
3753 * Results:
3754 * None.
3755 *
3756 * Side effects:
3757 * When constructing the event,
3758 * event.xany.serial is filled with the current X serial number.
3759 * event.xany.window is filled with the target window.
3760 * event.xany.display is filled with the target window's display.
3761 * Any other fields in eventPtr which are not specified by the pattern
3762 * string or the optional arguments, are set to 0.
3763 *
3764 * The event may be handled sychronously or asynchronously, depending
3765 * on the value specified by the optional "-when" option. The
3766 * default setting is synchronous.
3767 *
3768 *---------------------------------------------------------------------------
3769 */
3770 static int
3771 HandleEventGenerate(interp, mainWin, objc, objv)
3772 Tcl_Interp *interp; /* Interp for errors return and name lookup. */
3773 Tk_Window mainWin; /* Main window associated with interp. */
3774 int objc; /* Number of arguments. */
3775 Tcl_Obj *CONST objv[]; /* Argument objects. */
3776 {
3777 XEvent event;
3778 CONST char *p;
3779 char *name, *windowName;
3780 int count, flags, synch, i, number, warp;
3781 Tcl_QueuePosition pos;
3782 Pattern pat;
3783 Tk_Window tkwin, tkwin2;
3784 TkWindow *mainPtr;
3785 unsigned long eventMask;
3786 static CONST char *fieldStrings[] = {
3787 "-when", "-above", "-borderwidth", "-button",
3788 "-count", "-delta", "-detail", "-focus",
3789 "-height",
3790 "-keycode", "-keysym", "-mode", "-override",
3791 "-place", "-root", "-rootx", "-rooty",
3792 "-sendevent", "-serial", "-state", "-subwindow",
3793 "-time", "-warp", "-width", "-window",
3794 "-x", "-y", NULL
3795 };
3796 enum field {
3797 EVENT_WHEN, EVENT_ABOVE, EVENT_BORDER, EVENT_BUTTON,
3798 EVENT_COUNT, EVENT_DELTA, EVENT_DETAIL, EVENT_FOCUS,
3799 EVENT_HEIGHT,
3800 EVENT_KEYCODE, EVENT_KEYSYM, EVENT_MODE, EVENT_OVERRIDE,
3801 EVENT_PLACE, EVENT_ROOT, EVENT_ROOTX, EVENT_ROOTY,
3802 EVENT_SEND, EVENT_SERIAL, EVENT_STATE, EVENT_SUBWINDOW,
3803 EVENT_TIME, EVENT_WARP, EVENT_WIDTH, EVENT_WINDOW,
3804 EVENT_X, EVENT_Y
3805 };
3806
3807 windowName = Tcl_GetStringFromObj(objv[0], NULL);
3808 if (!windowName[0]) {
3809 tkwin = mainWin;
3810 } else if (NameToWindow(interp, mainWin, objv[0], &tkwin) != TCL_OK) {
3811 return TCL_ERROR;
3812 }
3813
3814 mainPtr = (TkWindow *) mainWin;
3815 if ((tkwin == NULL)
3816 || (mainPtr->mainPtr != ((TkWindow *) tkwin)->mainPtr)) {
3817 char *name;
3818
3819 name = Tcl_GetStringFromObj(objv[0], NULL);
3820 Tcl_AppendResult(interp, "window id \"", name,
3821 "\" doesn't exist in this application", (char *) NULL);
3822 return TCL_ERROR;
3823 }
3824
3825 name = Tcl_GetStringFromObj(objv[1], NULL);
3826
3827 p = name;
3828 eventMask = 0;
3829 count = ParseEventDescription(interp, &p, &pat, &eventMask);
3830 if (count == 0) {
3831 return TCL_ERROR;
3832 }
3833 if (count != 1) {
3834 Tcl_SetResult(interp, "Double or Triple modifier not allowed",
3835 TCL_STATIC);
3836 return TCL_ERROR;
3837 }
3838 if (*p != '\0') {
3839 Tcl_SetResult(interp, "only one event specification allowed",
3840 TCL_STATIC);
3841 return TCL_ERROR;
3842 }
3843
3844 memset((VOID *) &event, 0, sizeof(event));
3845 event.xany.type = pat.eventType;
3846 event.xany.serial = NextRequest(Tk_Display(tkwin));
3847 event.xany.send_event = False;
3848 if (windowName[0]) {
3849 event.xany.window = Tk_WindowId(tkwin);
3850 } else {
3851 event.xany.window = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin));
3852 }
3853 event.xany.display = Tk_Display(tkwin);
3854
3855 flags = flagArray[event.xany.type];
3856 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
3857 event.xkey.state = pat.needMods;
3858 if ((flags & KEY) && (event.xany.type != MouseWheelEvent)) {
3859 TkpSetKeycodeAndState(tkwin, pat.detail.keySym, &event);
3860 } else if (flags & BUTTON) {
3861 event.xbutton.button = pat.detail.button;
3862 } else if (flags & VIRTUAL) {
3863 ((XVirtualEvent *) &event)->name = pat.detail.name;
3864 }
3865 }
3866 if (flags & (CREATE|DESTROY|UNMAP|MAP|REPARENT|CONFIG|GRAVITY|CIRC)) {
3867 event.xcreatewindow.window = event.xany.window;
3868 }
3869
3870 /*
3871 * Process the remaining arguments to fill in additional fields
3872 * of the event.
3873 */
3874
3875 synch = 1;
3876 warp = 0;
3877 pos = TCL_QUEUE_TAIL;
3878 event.xkey.x_root = -1;
3879 event.xkey.y_root = -1;
3880 for (i = 2; i < objc; i += 2) {
3881 Tcl_Obj *optionPtr, *valuePtr;
3882 int index;
3883
3884 optionPtr = objv[i];
3885 valuePtr = objv[i + 1];
3886
3887 if (Tcl_GetIndexFromObj(interp, optionPtr, fieldStrings, "option",
3888 TCL_EXACT, &index) != TCL_OK) {
3889 return TCL_ERROR;
3890 }
3891 if (objc & 1) {
3892 /*
3893 * This test occurs after Tcl_GetIndexFromObj() so that
3894 * "event generate <Button> -xyz" will return the error message
3895 * that "-xyz" is a bad option, rather than that the value
3896 * for "-xyz" is missing.
3897 */
3898
3899 Tcl_AppendResult(interp, "value for \"",
3900 Tcl_GetStringFromObj(optionPtr, NULL), "\" missing",
3901 (char *) NULL);
3902 return TCL_ERROR;
3903 }
3904
3905 switch ((enum field) index) {
3906 case EVENT_WARP: {
3907 if (Tcl_GetBooleanFromObj(interp, valuePtr, &warp) != TCL_OK) {
3908 return TCL_ERROR;
3909 }
3910 if (!(flags & (KEY_BUTTON_MOTION_VIRTUAL))) {
3911 goto badopt;
3912 }
3913 break;
3914 }
3915 case EVENT_WHEN: {
3916 pos = (Tcl_QueuePosition) TkFindStateNumObj(interp, optionPtr,
3917 queuePosition, valuePtr);
3918 if ((int) pos < -1) {
3919 return TCL_ERROR;
3920 }
3921 synch = 0;
3922 if ((int) pos == -1) {
3923 synch = 1;
3924 }
3925 break;
3926 }
3927 case EVENT_ABOVE: {
3928 if (NameToWindow(interp, tkwin, valuePtr, &tkwin2) != TCL_OK) {
3929 return TCL_ERROR;
3930 }
3931 if (flags & CONFIG) {
3932 event.xconfigure.above = Tk_WindowId(tkwin2);
3933 } else {
3934 goto badopt;
3935 }
3936 break;
3937 }
3938 case EVENT_BORDER: {
3939 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number) != TCL_OK) {
3940 return TCL_ERROR;
3941 }
3942 if (flags & (CREATE|CONFIG)) {
3943 event.xcreatewindow.border_width = number;
3944 } else {
3945 goto badopt;
3946 }
3947 break;
3948 }
3949 case EVENT_BUTTON: {
3950 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
3951 return TCL_ERROR;
3952 }
3953 if (flags & BUTTON) {
3954 event.xbutton.button = number;
3955 } else {
3956 goto badopt;
3957 }
3958 break;
3959 }
3960 case EVENT_COUNT: {
3961 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
3962 return TCL_ERROR;
3963 }
3964 if (flags & EXPOSE) {
3965 event.xexpose.count = number;
3966 } else {
3967 goto badopt;
3968 }
3969 break;
3970 }
3971 case EVENT_DELTA: {
3972 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
3973 return TCL_ERROR;
3974 }
3975 if ((flags & KEY) && (event.xkey.type == MouseWheelEvent)) {
3976 event.xkey.keycode = number;
3977 } else {
3978 goto badopt;
3979 }
3980 break;
3981 }
3982 case EVENT_DETAIL: {
3983 number = TkFindStateNumObj(interp, optionPtr, notifyDetail,
3984 valuePtr);
3985 if (number < 0) {
3986 return TCL_ERROR;
3987 }
3988 if (flags & FOCUS) {
3989 event.xfocus.detail = number;
3990 } else if (flags & CROSSING) {
3991 event.xcrossing.detail = number;
3992 } else {
3993 goto badopt;
3994 }
3995 break;
3996 }
3997 case EVENT_FOCUS: {
3998 if (Tcl_GetBooleanFromObj(interp, valuePtr, &number) != TCL_OK) {
3999 return TCL_ERROR;
4000 }
4001 if (flags & CROSSING) {
4002 event.xcrossing.focus = number;
4003 } else {
4004 goto badopt;
4005 }
4006 break;
4007 }
4008 case EVENT_HEIGHT: {
4009 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number) != TCL_OK) {
4010 return TCL_ERROR;
4011 }
4012 if (flags & EXPOSE) {
4013 event.xexpose.height = number;
4014 } else if (flags & CONFIG) {
4015 event.xconfigure.height = number;
4016 } else {
4017 goto badopt;
4018 }
4019 break;
4020 }
4021 case EVENT_KEYCODE: {
4022 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
4023 return TCL_ERROR;
4024 }
4025 if ((flags & KEY) && (event.xkey.type != MouseWheelEvent)) {
4026 event.xkey.keycode = number;
4027 } else {
4028 goto badopt;
4029 }
4030 break;
4031 }
4032 case EVENT_KEYSYM: {
4033 KeySym keysym;
4034 char *value;
4035
4036 value = Tcl_GetStringFromObj(valuePtr, NULL);
4037 keysym = TkStringToKeysym(value);
4038 if (keysym == NoSymbol) {
4039 Tcl_AppendResult(interp, "unknown keysym \"", value, "\"",
4040 (char *) NULL);
4041 return TCL_ERROR;
4042 }
4043
4044 TkpSetKeycodeAndState(tkwin, keysym, &event);
4045 if (event.xkey.keycode == 0) {
4046 Tcl_AppendResult(interp, "no keycode for keysym \"", value,
4047 "\"", (char *) NULL);
4048 return TCL_ERROR;
4049 }
4050 if (!(flags & KEY) || (event.xkey.type == MouseWheelEvent)) {
4051 goto badopt;
4052 }
4053 break;
4054 }
4055 case EVENT_MODE: {
4056 number = TkFindStateNumObj(interp, optionPtr, notifyMode,
4057 valuePtr);
4058 if (number < 0) {
4059 return TCL_ERROR;
4060 }
4061 if (flags & CROSSING) {
4062 event.xcrossing.mode = number;
4063 } else if (flags & FOCUS) {
4064 event.xfocus.mode = number;
4065 } else {
4066 goto badopt;
4067 }
4068 break;
4069 }
4070 case EVENT_OVERRIDE: {
4071 if (Tcl_GetBooleanFromObj(interp, valuePtr, &number) != TCL_OK) {
4072 return TCL_ERROR;
4073 }
4074 if (flags & CREATE) {
4075 event.xcreatewindow.override_redirect = number;
4076 } else if (flags & MAP) {
4077 event.xmap.override_redirect = number;
4078 } else if (flags & REPARENT) {
4079 event.xreparent.override_redirect = number;
4080 } else if (flags & CONFIG) {
4081 event.xconfigure.override_redirect = number;
4082 } else {
4083 goto badopt;
4084 }
4085 break;
4086 }
4087 case EVENT_PLACE: {
4088 number = TkFindStateNumObj(interp, optionPtr, circPlace,
4089 valuePtr);
4090 if (number < 0) {
4091 return TCL_ERROR;
4092 }
4093 if (flags & CIRC) {
4094 event.xcirculate.place = number;
4095 } else {
4096 goto badopt;
4097 }
4098 break;
4099 }
4100 case EVENT_ROOT: {
4101 if (NameToWindow(interp, tkwin, valuePtr, &tkwin2) != TCL_OK) {
4102 return TCL_ERROR;
4103 }
4104 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4105 event.xkey.root = Tk_WindowId(tkwin2);
4106 } else {
4107 goto badopt;
4108 }
4109 break;
4110 }
4111 case EVENT_ROOTX: {
4112 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number) != TCL_OK) {
4113 return TCL_ERROR;
4114 }
4115 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4116 event.xkey.x_root = number;
4117 } else {
4118 goto badopt;
4119 }
4120 break;
4121 }
4122 case EVENT_ROOTY: {
4123 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number) != TCL_OK) {
4124 return TCL_ERROR;
4125 }
4126 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4127 event.xkey.y_root = number;
4128 } else {
4129 goto badopt;
4130 }
4131 break;
4132 }
4133 case EVENT_SEND: {
4134 CONST char *value;
4135
4136 value = Tcl_GetStringFromObj(valuePtr, NULL);
4137 if (isdigit(UCHAR(value[0]))) {
4138 /*
4139 * Allow arbitrary integer values for the field; they
4140 * are needed by a few of the tests in the Tk test suite.
4141 */
4142
4143 if (Tcl_GetIntFromObj(interp, valuePtr, &number)
4144 != TCL_OK) {
4145 return TCL_ERROR;
4146 }
4147 } else {
4148 if (Tcl_GetBooleanFromObj(interp, valuePtr, &number)
4149 != TCL_OK) {
4150 return TCL_ERROR;
4151 }
4152 }
4153 event.xany.send_event = number;
4154 break;
4155 }
4156 case EVENT_SERIAL: {
4157 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
4158 return TCL_ERROR;
4159 }
4160 event.xany.serial = number;
4161 break;
4162 }
4163 case EVENT_STATE: {
4164 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4165 if (Tcl_GetIntFromObj(interp, valuePtr, &number)
4166 != TCL_OK) {
4167 return TCL_ERROR;
4168 }
4169 if (flags & (KEY_BUTTON_MOTION_VIRTUAL)) {
4170 event.xkey.state = number;
4171 } else {
4172 event.xcrossing.state = number;
4173 }
4174 } else if (flags & VISIBILITY) {
4175 number = TkFindStateNumObj(interp, optionPtr, visNotify,
4176 valuePtr);
4177 if (number < 0) {
4178 return TCL_ERROR;
4179 }
4180 event.xvisibility.state = number;
4181 } else {
4182 goto badopt;
4183 }
4184 break;
4185 }
4186 case EVENT_SUBWINDOW: {
4187 if (NameToWindow(interp, tkwin, valuePtr, &tkwin2) != TCL_OK) {
4188 return TCL_ERROR;
4189 }
4190 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4191 event.xkey.subwindow = Tk_WindowId(tkwin2);
4192 } else {
4193 goto badopt;
4194 }
4195 break;
4196 }
4197 case EVENT_TIME: {
4198 if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {
4199 return TCL_ERROR;
4200 }
4201 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4202 event.xkey.time = (Time) number;
4203 } else if (flags & PROP) {
4204 event.xproperty.time = (Time) number;
4205 } else {
4206 goto badopt;
4207 }
4208 break;
4209 }
4210 case EVENT_WIDTH: {
4211 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number)
4212 != TCL_OK) {
4213 return TCL_ERROR;
4214 }
4215 if (flags & EXPOSE) {
4216 event.xexpose.width = number;
4217 } else if (flags & (CREATE|CONFIG)) {
4218 event.xcreatewindow.width = number;
4219 } else {
4220 goto badopt;
4221 }
4222 break;
4223 }
4224 case EVENT_WINDOW: {
4225 if (NameToWindow(interp, tkwin, valuePtr, &tkwin2) != TCL_OK) {
4226 return TCL_ERROR;
4227 }
4228 if (flags & (CREATE|DESTROY|UNMAP|MAP|REPARENT|CONFIG
4229 |GRAVITY|CIRC)) {
4230 event.xcreatewindow.window = Tk_WindowId(tkwin2);
4231 } else {
4232 goto badopt;
4233 }
4234 break;
4235 }
4236 case EVENT_X: {
4237 int rootX, rootY;
4238
4239 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number)
4240 != TCL_OK) {
4241 return TCL_ERROR;
4242 }
4243 Tk_GetRootCoords(tkwin, &rootX, &rootY);
4244 rootX += number;
4245 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4246 event.xkey.x = number;
4247 if ( event.xkey.x_root == -1 ) {
4248 event.xkey.x_root = rootX;
4249 }
4250
4251 } else if (flags & EXPOSE) {
4252 event.xexpose.x = number;
4253 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
4254 event.xcreatewindow.x = number;
4255 } else if (flags & REPARENT) {
4256 event.xreparent.x = number;
4257 } else {
4258 goto badopt;
4259 }
4260 break;
4261 }
4262 case EVENT_Y: {
4263 int rootX, rootY;
4264
4265 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr, &number)
4266 != TCL_OK) {
4267 return TCL_ERROR;
4268 }
4269 Tk_GetRootCoords(tkwin, &rootX, &rootY);
4270 rootY += number;
4271 if (flags & (KEY_BUTTON_MOTION_VIRTUAL|CROSSING)) {
4272 event.xkey.y = number;
4273 if ( event.xkey.y_root == -1 ) {
4274 event.xkey.y_root = rootY;
4275 }
4276 } else if (flags & EXPOSE) {
4277 event.xexpose.y = number;
4278 } else if (flags & (CREATE|CONFIG|GRAVITY)) {
4279 event.xcreatewindow.y = number;
4280 } else if (flags & REPARENT) {
4281 event.xreparent.y = number;
4282 } else {
4283 goto badopt;
4284 }
4285 break;
4286 }
4287 }
4288 continue;
4289
4290 badopt:
4291 Tcl_AppendResult(interp, name, " event doesn't accept \"",
4292 Tcl_GetStringFromObj(optionPtr, NULL), "\" option", NULL);
4293 return TCL_ERROR;
4294 }
4295 if (synch != 0) {
4296 Tk_HandleEvent(&event);
4297 } else {
4298 Tk_QueueWindowEvent(&event, pos);
4299 }
4300 /*
4301 * We only allow warping if the window is mapped
4302 */
4303 if ((warp != 0) && Tk_IsMapped(tkwin)) {
4304 TkDisplay *dispPtr;
4305 dispPtr = TkGetDisplay(event.xmotion.display);
4306 if (!(dispPtr->flags & TK_DISPLAY_IN_WARP)) {
4307 Tcl_DoWhenIdle(DoWarp, (ClientData) dispPtr);
4308 dispPtr->flags |= TK_DISPLAY_IN_WARP;
4309 }
4310 dispPtr->warpWindow = event.xany.window;
4311 dispPtr->warpX = event.xkey.x;
4312 dispPtr->warpY = event.xkey.y;
4313 }
4314 Tcl_ResetResult(interp);
4315 return TCL_OK;
4316
4317 }
4318 static int
4319 NameToWindow(interp, mainWin, objPtr, tkwinPtr)
4320 Tcl_Interp *interp; /* Interp for error return and name lookup. */
4321 Tk_Window mainWin; /* Main window of application. */
4322 Tcl_Obj *objPtr; /* Contains name or id string of window. */
4323 Tk_Window *tkwinPtr; /* Filled with token for window. */
4324 {
4325 char *name;
4326 Tk_Window tkwin;
4327 Window id;
4328
4329 name = Tcl_GetStringFromObj(objPtr, NULL);
4330 if (name[0] == '.') {
4331 tkwin = Tk_NameToWindow(interp, name, mainWin);
4332 if (tkwin == NULL) {
4333 return TCL_ERROR;
4334 }
4335 *tkwinPtr = tkwin;
4336 } else {
4337 /*
4338 * Check for the winPtr being valid, even if it looks ok to
4339 * TkpScanWindowId. [Bug #411307]
4340 */
4341
4342 if ((TkpScanWindowId(NULL, objPtr, &id) != TCL_OK) ||
4343 ((*tkwinPtr = Tk_IdToWindow(Tk_Display(mainWin), id))
4344 == NULL)) {
4345 Tcl_AppendResult(interp, "bad window name/identifier \"",
4346 name, "\"", (char *) NULL);
4347 return TCL_ERROR;
4348 }
4349 }
4350 return TCL_OK;
4351 }
4352
4353 /*
4354 *-------------------------------------------------------------------------
4355 *
4356 * DoWarp --
4357 *
4358 * Perform Warping of X pointer. Executed as an idle handler only.
4359 *
4360 * Results:
4361 * None
4362 *
4363 * Side effects:
4364 * X Pointer will move to a new location.
4365 *
4366 *-------------------------------------------------------------------------
4367 */
4368 static void
4369 DoWarp(clientData)
4370 ClientData clientData;
4371 {
4372 TkDisplay *dispPtr = (TkDisplay *) clientData;
4373
4374 XWarpPointer(dispPtr->display, (Window) None, (Window) dispPtr->warpWindow,
4375 0, 0, 0, 0, (int) dispPtr->warpX, (int) dispPtr->warpY);
4376 XForceScreenSaver(dispPtr->display, ScreenSaverReset);
4377 dispPtr->flags &= ~TK_DISPLAY_IN_WARP;
4378 }
4379
4380 /*
4381 *-------------------------------------------------------------------------
4382 *
4383 * GetVirtualEventUid --
4384 *
4385 * Determine if the given string is in the proper format for a
4386 * virtual event.
4387 *
4388 * Results:
4389 * The return value is NULL if the virtual event string was
4390 * not in the proper format. In this case, an error message
4391 * will be left in the interp's result. Otherwise the return
4392 * value is a Tk_Uid that represents the virtual event.
4393 *
4394 * Side effects:
4395 * None.
4396 *
4397 *-------------------------------------------------------------------------
4398 */
4399 static Tk_Uid
4400 GetVirtualEventUid(interp, virtString)
4401 Tcl_Interp *interp;
4402 char *virtString;
4403 {
4404 Tk_Uid uid;
4405 int length;
4406
4407 length = strlen(virtString);
4408
4409 if (length < 5 || virtString[0] != '<' || virtString[1] != '<' ||
4410 virtString[length - 2] != '>' || virtString[length - 1] != '>') {
4411 Tcl_AppendResult(interp, "virtual event \"", virtString,
4412 "\" is badly formed", (char *) NULL);
4413 return NULL;
4414 }
4415 virtString[length - 2] = '\0';
4416 uid = Tk_GetUid(virtString + 2);
4417 virtString[length - 2] = '>';
4418
4419 return uid;
4420 }
4421
4422
4423 /*
4424 *----------------------------------------------------------------------
4425 *
4426 * FindSequence --
4427 *
4428 * Find the entry in the pattern table that corresponds to a
4429 * particular pattern string, and return a pointer to that
4430 * entry.
4431 *
4432 * Results:
4433 * The return value is normally a pointer to the PatSeq
4434 * in patternTable that corresponds to eventString. If an error
4435 * was found while parsing eventString, or if "create" is 0 and
4436 * no pattern sequence previously existed, then NULL is returned
4437 * and the interp's result contains a message describing the problem.
4438 * If no pattern sequence previously existed for eventString, then
4439 * a new one is created with a NULL command field. In a successful
4440 * return, *maskPtr is filled in with a mask of the event types
4441 * on which the pattern sequence depends.
4442 *
4443 * Side effects:
4444 * A new pattern sequence may be allocated.
4445 *
4446 *----------------------------------------------------------------------
4447 */
4448
4449 static PatSeq *
4450 FindSequence(interp, patternTablePtr, object, eventString, create,
4451 allowVirtual, maskPtr)
4452 Tcl_Interp *interp; /* Interpreter to use for error
4453 * reporting. */
4454 Tcl_HashTable *patternTablePtr; /* Table to use for lookup. */
4455 ClientData object; /* For binding table, token for object with
4456 * which binding is associated.
4457 * For virtual event table, NULL. */
4458 CONST char *eventString; /* String description of pattern to
4459 * match on. See user documentation
4460 * for details. */
4461 int create; /* 0 means don't create the entry if
4462 * it doesn't already exist. Non-zero
4463 * means create. */
4464 int allowVirtual; /* 0 means that virtual events are not
4465 * allowed in the sequence. Non-zero
4466 * otherwise. */
4467 unsigned long *maskPtr; /* *maskPtr is filled in with the event
4468 * types on which this pattern sequence
4469 * depends. */
4470 {
4471
4472 Pattern pats[EVENT_BUFFER_SIZE];
4473 int numPats, virtualFound;
4474 CONST char *p;
4475 Pattern *patPtr;
4476 PatSeq *psPtr;
4477 Tcl_HashEntry *hPtr;
4478 int flags, count, new;
4479 size_t sequenceSize;
4480 unsigned long eventMask;
4481 PatternTableKey key;
4482
4483 /*
4484 *-------------------------------------------------------------
4485 * Step 1: parse the pattern string to produce an array
4486 * of Patterns. The array is generated backwards, so
4487 * that the lowest-indexed pattern corresponds to the last
4488 * event that must occur.
4489 *-------------------------------------------------------------
4490 */
4491
4492 p = eventString;
4493 flags = 0;
4494 eventMask = 0;
4495 virtualFound = 0;
4496
4497 patPtr = &pats[EVENT_BUFFER_SIZE-1];
4498 for (numPats = 0; numPats < EVENT_BUFFER_SIZE; numPats++, patPtr--) {
4499 while (isspace(UCHAR(*p))) {
4500 p++;
4501 }
4502 if (*p == '\0') {
4503 break;
4504 }
4505
4506 count = ParseEventDescription(interp, &p, patPtr, &eventMask);
4507 if (count == 0) {
4508 return NULL;
4509 }
4510
4511 if (eventMask & VirtualEventMask) {
4512 if (allowVirtual == 0) {
4513 Tcl_SetResult(interp,
4514 "virtual event not allowed in definition of another virtual event",
4515 TCL_STATIC);
4516 return NULL;
4517 }
4518 virtualFound = 1;
4519 }
4520
4521 /*
4522 * Replicate events for DOUBLE, TRIPLE, QUADRUPLE.
4523 */
4524
4525 while ((count-- > 1) && (numPats < EVENT_BUFFER_SIZE-1)) {
4526 flags |= PAT_NEARBY;
4527 patPtr[-1] = patPtr[0];
4528 patPtr--;
4529 numPats++;
4530 }
4531 }
4532
4533 /*
4534 *-------------------------------------------------------------
4535 * Step 2: find the sequence in the binding table if it exists,
4536 * and add a new sequence to the table if it doesn't.
4537 *-------------------------------------------------------------
4538 */
4539
4540 if (numPats == 0) {
4541 Tcl_SetResult(interp, "no events specified in binding", TCL_STATIC);
4542 return NULL;
4543 }
4544 if ((numPats > 1) && (virtualFound != 0)) {
4545 Tcl_SetResult(interp, "virtual events may not be composed",
4546 TCL_STATIC);
4547 return NULL;
4548 }
4549
4550 patPtr = &pats[EVENT_BUFFER_SIZE-numPats];
4551 memset(&key, 0, sizeof(key));
4552 key.object = object;
4553 key.type = patPtr->eventType;
4554 key.detail = patPtr->detail;
4555 hPtr = Tcl_CreateHashEntry(patternTablePtr, (char *) &key, &new);
4556 sequenceSize = numPats*sizeof(Pattern);
4557 if (!new) {
4558 for (psPtr = (PatSeq *) Tcl_GetHashValue(hPtr); psPtr != NULL;
4559 psPtr = psPtr->nextSeqPtr) {
4560 if ((numPats == psPtr->numPats)
4561 && ((flags & PAT_NEARBY) == (psPtr->flags & PAT_NEARBY))
4562 && (memcmp((char *) patPtr, (char *) psPtr->pats,
4563 sequenceSize) == 0)) {
4564 goto done;
4565 }
4566 }
4567 }
4568 if (!create) {
4569 if (new) {
4570 Tcl_DeleteHashEntry(hPtr);
4571 }
4572 /*
4573 * No binding exists for the sequence, so return an empty error.
4574 * This is a special error that the caller will check for in order
4575 * to silently ignore this case. This is a hack that maintains
4576 * backward compatibility for Tk_GetBinding but the various "bind"
4577 * commands silently ignore missing bindings.
4578 */
4579
4580 return NULL;
4581 }
4582 psPtr = (PatSeq *) ckalloc((unsigned) (sizeof(PatSeq)
4583 + (numPats-1)*sizeof(Pattern)));
4584 psPtr->numPats = numPats;
4585 psPtr->eventProc = NULL;
4586 psPtr->freeProc = NULL;
4587 psPtr->clientData = NULL;
4588 psPtr->flags = flags;
4589 psPtr->refCount = 0;
4590 psPtr->nextSeqPtr = (PatSeq *) Tcl_GetHashValue(hPtr);
4591 psPtr->hPtr = hPtr;
4592 psPtr->voPtr = NULL;
4593 psPtr->nextObjPtr = NULL;
4594 Tcl_SetHashValue(hPtr, psPtr);
4595
4596 memcpy((VOID *) psPtr->pats, (VOID *) patPtr, sequenceSize);
4597
4598 done:
4599 *maskPtr = eventMask;
4600 return psPtr;
4601 }
4602
4603 /*
4604 *---------------------------------------------------------------------------
4605 *
4606 * ParseEventDescription --
4607 *
4608 * Fill Pattern buffer with information about event from
4609 * event string.
4610 *
4611 * Results:
4612 * Leaves error message in interp and returns 0 if there was an
4613 * error due to a badly formed event string. Returns 1 if proper
4614 * event was specified, 2 if Double modifier was used in event
4615 * string, or 3 if Triple was used.
4616 *
4617 * Side effects:
4618 * On exit, eventStringPtr points to rest of event string (after the
4619 * closing '>', so that this procedure can be called repeatedly to
4620 * parse all the events in the entire sequence.
4621 *
4622 *---------------------------------------------------------------------------
4623 */
4624
4625 static int
4626 ParseEventDescription(interp, eventStringPtr, patPtr,
4627 eventMaskPtr)
4628 Tcl_Interp *interp; /* For error messages. */
4629 CONST char **eventStringPtr;/* On input, holds a pointer to start of
4630 * event string. On exit, gets pointer to
4631 * rest of string after parsed event. */
4632 Pattern *patPtr; /* Filled with the pattern parsed from the
4633 * event string. */
4634 unsigned long *eventMaskPtr;/* Filled with event mask of matched event. */
4635
4636 {
4637 char *p;
4638 unsigned long eventMask;
4639 int count, eventFlags;
4640 #define FIELD_SIZE 48
4641 char field[FIELD_SIZE];
4642 Tcl_HashEntry *hPtr;
4643 Tcl_DString copy;
4644
4645 Tcl_DStringInit(&copy);
4646 p = Tcl_DStringAppend(&copy, *eventStringPtr, -1);
4647
4648 patPtr->eventType = -1;
4649 patPtr->needMods = 0;
4650 patPtr->detail.clientData = 0;
4651
4652 eventMask = 0;
4653 count = 1;
4654
4655 /*
4656 * Handle simple ASCII characters.
4657 */
4658
4659 if (*p != '<') {
4660 char string[2];
4661
4662 patPtr->eventType = KeyPress;
4663 eventMask = KeyPressMask;
4664 string[0] = *p;
4665 string[1] = 0;
4666 patPtr->detail.keySym = TkStringToKeysym(string);
4667 if (patPtr->detail.keySym == NoSymbol) {
4668 if (isprint(UCHAR(*p))) {
4669 patPtr->detail.keySym = *p;
4670 } else {
4671 char buf[64];
4672
4673 sprintf(buf, "bad ASCII character 0x%x", (unsigned char) *p);
4674 Tcl_SetResult(interp, buf, TCL_VOLATILE);
4675 count = 0;
4676 goto done;
4677 }
4678 }
4679 p++;
4680 goto end;
4681 }
4682
4683 /*
4684 * A fancier event description. This can be either a virtual event
4685 * or a physical event.
4686 *
4687 * A virtual event description consists of:
4688 *
4689 * 1. double open angle brackets.
4690 * 2. virtual event name.
4691 * 3. double close angle brackets.
4692 *
4693 * A physical event description consists of:
4694 *
4695 * 1. open angle bracket.
4696 * 2. any number of modifiers, each followed by spaces
4697 * or dashes.
4698 * 3. an optional event name.
4699 * 4. an option button or keysym name. Either this or
4700 * item 3 *must* be present; if both are present
4701 * then they are separated by spaces or dashes.
4702 * 5. a close angle bracket.
4703 */
4704
4705 p++;
4706 if (*p == '<') {
4707 /*
4708 * This is a virtual event: soak up all the characters up to
4709 * the next '>'.
4710 */
4711
4712 char *field = p + 1;
4713 p = strchr(field, '>');
4714 if (p == field) {
4715 Tcl_SetResult(interp, "virtual event \"<<>>\" is badly formed",
4716 TCL_STATIC);
4717 count = 0;
4718 goto done;
4719 }
4720 if ((p == NULL) || (p[1] != '>')) {
4721 Tcl_SetResult(interp, "missing \">\" in virtual binding",
4722 TCL_STATIC);
4723 count = 0;
4724 goto done;
4725 }
4726 *p = '\0';
4727 patPtr->eventType = VirtualEvent;
4728 eventMask = VirtualEventMask;
4729 patPtr->detail.name = Tk_GetUid(field);
4730 *p = '>';
4731
4732 p += 2;
4733 goto end;
4734 }
4735
4736 while (1) {
4737 ModInfo *modPtr;
4738 p = GetField(p, field, FIELD_SIZE);
4739 if (*p == '>') {
4740 /*
4741 * This solves the problem of, e.g., <Control-M> being
4742 * misinterpreted as Control + Meta + missing keysym
4743 * instead of Control + KeyPress + M.
4744 */
4745 break;
4746 }
4747 hPtr = Tcl_FindHashEntry(&modTable, field);
4748 if (hPtr == NULL) {
4749 break;
4750 }
4751 modPtr = (ModInfo *) Tcl_GetHashValue(hPtr);
4752 patPtr->needMods |= modPtr->mask;
4753 if (modPtr->flags & (MULT_CLICKS)) {
4754 int i = modPtr->flags & MULT_CLICKS;
4755 count = 2;
4756 while (i >>= 1) count++;
4757 }
4758 while ((*p == '-') || isspace(UCHAR(*p))) {
4759 p++;
4760 }
4761 }
4762
4763 eventFlags = 0;
4764 hPtr = Tcl_FindHashEntry(&eventTable, field);
4765 if (hPtr != NULL) {
4766 EventInfo *eiPtr;
4767 eiPtr = (EventInfo *) Tcl_GetHashValue(hPtr);
4768
4769 patPtr->eventType = eiPtr->type;
4770 eventFlags = flagArray[eiPtr->type];
4771 eventMask = eiPtr->eventMask;
4772 while ((*p == '-') || isspace(UCHAR(*p))) {
4773 p++;
4774 }
4775 p = GetField(p, field, FIELD_SIZE);
4776 }
4777 if (*field != '\0') {
4778 if ((*field >= '1') && (*field <= '7') && (field[1] == '\0')) {
4779 if (eventFlags == 0) {
4780 if (*field <= '5') {
4781 patPtr->eventType = ButtonPress;
4782 eventMask = ButtonPressMask;
4783 } else {
4784 goto getKeysym;
4785 }
4786 } else if (eventFlags & KEY) {
4787 goto getKeysym;
4788 } else if ((eventFlags & BUTTON) == 0) {
4789 Tcl_AppendResult(interp, "specified button \"", field,
4790 "\" for non-button event", (char *) NULL);
4791 count = 0;
4792 goto done;
4793 }
4794 patPtr->detail.button = (*field - '0');
4795 } else {
4796 getKeysym:
4797 patPtr->detail.keySym = TkStringToKeysym(field);
4798 if (patPtr->detail.keySym == NoSymbol) {
4799 Tcl_AppendResult(interp, "bad event type or keysym \"",
4800 field, "\"", (char *) NULL);
4801 count = 0;
4802 goto done;
4803 }
4804 if (eventFlags == 0) {
4805 patPtr->eventType = KeyPress;
4806 eventMask = KeyPressMask;
4807 } else if ((eventFlags & KEY) == 0) {
4808 Tcl_AppendResult(interp, "specified keysym \"", field,
4809 "\" for non-key event", (char *) NULL);
4810 count = 0;
4811 goto done;
4812 }
4813 }
4814 } else if (eventFlags == 0) {
4815 Tcl_SetResult(interp, "no event type or button # or keysym",
4816 TCL_STATIC);
4817 count = 0;
4818 goto done;
4819 }
4820
4821 while ((*p == '-') || isspace(UCHAR(*p))) {
4822 p++;
4823 }
4824 if (*p != '>') {
4825 while (*p != '\0') {
4826 p++;
4827 if (*p == '>') {
4828 Tcl_SetResult(interp,
4829 "extra characters after detail in binding",
4830 TCL_STATIC);
4831 count = 0;
4832 goto done;
4833 }
4834 }
4835 Tcl_SetResult(interp, "missing \">\" in binding", TCL_STATIC);
4836 count = 0;
4837 goto done;
4838 }
4839 p++;
4840
4841 end:
4842 *eventStringPtr += (p - Tcl_DStringValue(&copy));
4843 *eventMaskPtr |= eventMask;
4844 done:
4845 Tcl_DStringFree(&copy);
4846 return count;
4847 }
4848
4849 /*
4850 *----------------------------------------------------------------------
4851 *
4852 * GetField --
4853 *
4854 * Used to parse pattern descriptions. Copies up to
4855 * size characters from p to copy, stopping at end of
4856 * string, space, "-", ">", or whenever size is
4857 * exceeded.
4858 *
4859 * Results:
4860 * The return value is a pointer to the character just
4861 * after the last one copied (usually "-" or space or
4862 * ">", but could be anything if size was exceeded).
4863 * Also places NULL-terminated string (up to size
4864 * character, including NULL), at copy.
4865 *
4866 * Side effects:
4867 * None.
4868 *
4869 *----------------------------------------------------------------------
4870 */
4871
4872 static char *
4873 GetField(p, copy, size)
4874 char *p; /* Pointer to part of pattern. */
4875 char *copy; /* Place to copy field. */
4876 int size; /* Maximum number of characters to
4877 * copy. */
4878 {
4879 while ((*p != '\0') && !isspace(UCHAR(*p)) && (*p != '>')
4880 && (*p != '-') && (size > 1)) {
4881 *copy = *p;
4882 p++;
4883 copy++;
4884 size--;
4885 }
4886 *copy = '\0';
4887 return p;
4888 }
4889
4890 /*
4891 *---------------------------------------------------------------------------
4892 *
4893 * GetPatternString --
4894 *
4895 * Produce a string version of the given event, for displaying to
4896 * the user.
4897 *
4898 * Results:
4899 * The string is left in dsPtr.
4900 *
4901 * Side effects:
4902 * It is the caller's responsibility to initialize the DString before
4903 * and to free it after calling this procedure.
4904 *
4905 *---------------------------------------------------------------------------
4906 */
4907 static void
4908 GetPatternString(psPtr, dsPtr)
4909 PatSeq *psPtr;
4910 Tcl_DString *dsPtr;
4911 {
4912 Pattern *patPtr;
4913 char c, buffer[TCL_INTEGER_SPACE];
4914 int patsLeft, needMods;
4915 ModInfo *modPtr;
4916 EventInfo *eiPtr;
4917
4918 /*
4919 * The order of the patterns in the sequence is backwards from the order
4920 * in which they must be output.
4921 */
4922
4923 for (patsLeft = psPtr->numPats, patPtr = &psPtr->pats[psPtr->numPats - 1];
4924 patsLeft > 0; patsLeft--, patPtr--) {
4925
4926 /*
4927 * Check for simple case of an ASCII character.
4928 */
4929
4930 if ((patPtr->eventType == KeyPress)
4931 && ((psPtr->flags & PAT_NEARBY) == 0)
4932 && (patPtr->needMods == 0)
4933 && (patPtr->detail.keySym < 128)
4934 && isprint(UCHAR(patPtr->detail.keySym))
4935 && (patPtr->detail.keySym != '<')
4936 && (patPtr->detail.keySym != ' ')) {
4937
4938 c = (char) patPtr->detail.keySym;
4939 Tcl_DStringAppend(dsPtr, &c, 1);
4940 continue;
4941 }
4942
4943 /*
4944 * Check for virtual event.
4945 */
4946
4947 if (patPtr->eventType == VirtualEvent) {
4948 Tcl_DStringAppend(dsPtr, "<<", 2);
4949 Tcl_DStringAppend(dsPtr, patPtr->detail.name, -1);
4950 Tcl_DStringAppend(dsPtr, ">>", 2);
4951 continue;
4952 }
4953
4954 /*
4955 * It's a more general event specification. First check
4956 * for "Double", "Triple", "Quadruple", then modifiers,
4957 * then event type, then keysym or button detail.
4958 */
4959
4960 Tcl_DStringAppend(dsPtr, "<", 1);
4961 if ((psPtr->flags & PAT_NEARBY) && (patsLeft > 1)
4962 && (memcmp((char *) patPtr, (char *) (patPtr-1),
4963 sizeof(Pattern)) == 0)) {
4964 patsLeft--;
4965 patPtr--;
4966 if ((patsLeft > 1) && (memcmp((char *) patPtr,
4967 (char *) (patPtr-1), sizeof(Pattern)) == 0)) {
4968 patsLeft--;
4969 patPtr--;
4970 if ((patsLeft > 1) && (memcmp((char *) patPtr,
4971 (char *) (patPtr-1), sizeof(Pattern)) == 0)) {
4972 patsLeft--;
4973 patPtr--;
4974 Tcl_DStringAppend(dsPtr, "Quadruple-", 10);
4975 } else {
4976 Tcl_DStringAppend(dsPtr, "Triple-", 7);
4977 }
4978 } else {
4979 Tcl_DStringAppend(dsPtr, "Double-", 7);
4980 }
4981 }
4982 for (needMods = patPtr->needMods, modPtr = modArray;
4983 needMods != 0; modPtr++) {
4984 if (modPtr->mask & needMods) {
4985 needMods &= ~modPtr->mask;
4986 Tcl_DStringAppend(dsPtr, modPtr->name, -1);
4987 Tcl_DStringAppend(dsPtr, "-", 1);
4988 }
4989 }
4990 for (eiPtr = eventArray; eiPtr->name != NULL; eiPtr++) {
4991 if (eiPtr->type == patPtr->eventType) {
4992 Tcl_DStringAppend(dsPtr, eiPtr->name, -1);
4993 if (patPtr->detail.clientData != 0) {
4994 Tcl_DStringAppend(dsPtr, "-", 1);
4995 }
4996 break;
4997 }
4998 }
4999
5000 if (patPtr->detail.clientData != 0) {
5001 if ((patPtr->eventType == KeyPress)
5002 || (patPtr->eventType == KeyRelease)) {
5003 char *string;
5004
5005 string = TkKeysymToString(patPtr->detail.keySym);
5006 if (string != NULL) {
5007 Tcl_DStringAppend(dsPtr, string, -1);
5008 }
5009 } else {
5010 sprintf(buffer, "%d", patPtr->detail.button);
5011 Tcl_DStringAppend(dsPtr, buffer, -1);
5012 }
5013 }
5014 Tcl_DStringAppend(dsPtr, ">", 1);
5015 }
5016 }
5017
5018 /*
5019 *---------------------------------------------------------------------------
5020 *
5021 * EvalTclBinding --
5022 *
5023 * The procedure that is invoked by Tk_BindEvent when a Tcl binding
5024 * is fired.
5025 *
5026 * Results:
5027 * A standard Tcl result code, the result of globally evaluating the
5028 * percent-substitued binding string.
5029 *
5030 * Side effects:
5031 * Normal side effects due to eval.
5032 *
5033 *---------------------------------------------------------------------------
5034 */
5035
5036 static void
5037 FreeTclBinding(clientData)
5038 ClientData clientData;
5039 {
5040 ckfree((char *) clientData);
5041 }
5042
5043 /*
5044 *----------------------------------------------------------------------
5045 *
5046 * TkStringToKeysym --
5047 *
5048 * This procedure finds the keysym associated with a given keysym
5049 * name.
5050 *
5051 * Results:
5052 * The return value is the keysym that corresponds to name, or
5053 * NoSymbol if there is no such keysym.
5054 *
5055 * Side effects:
5056 * None.
5057 *
5058 *----------------------------------------------------------------------
5059 */
5060
5061 KeySym
5062 TkStringToKeysym(name)
5063 char *name; /* Name of a keysym. */
5064 {
5065 #ifdef REDO_KEYSYM_LOOKUP
5066 Tcl_HashEntry *hPtr;
5067 KeySym keysym;
5068
5069 hPtr = Tcl_FindHashEntry(&keySymTable, name);
5070 if (hPtr != NULL) {
5071 return (KeySym) Tcl_GetHashValue(hPtr);
5072 }
5073 if (strlen(name) == 1) {
5074 keysym = (KeySym) (unsigned char) name[0];
5075 if (TkKeysymToString(keysym) != NULL) {
5076 return keysym;
5077 }
5078 }
5079 #endif /* REDO_KEYSYM_LOOKUP */
5080 return XStringToKeysym(name);
5081 }
5082
5083 /*
5084 *----------------------------------------------------------------------
5085 *
5086 * TkKeysymToString --
5087 *
5088 * This procedure finds the keysym name associated with a given
5089 * keysym.
5090 *
5091 * Results:
5092 * The return value is a pointer to a static string containing
5093 * the name of the given keysym, or NULL if there is no known name.
5094 *
5095 * Side effects:
5096 * None.
5097 *
5098 *----------------------------------------------------------------------
5099 */
5100
5101 char *
5102 TkKeysymToString(keysym)
5103 KeySym keysym;
5104 {
5105 #ifdef REDO_KEYSYM_LOOKUP
5106 Tcl_HashEntry *hPtr;
5107
5108 hPtr = Tcl_FindHashEntry(&nameTable, (char *)keysym);
5109 if (hPtr != NULL) {
5110 return (char *) Tcl_GetHashValue(hPtr);
5111 }
5112 #endif /* REDO_KEYSYM_LOOKUP */
5113 return XKeysymToString(keysym);
5114 }
5115
5116 /*
5117 *----------------------------------------------------------------------
5118 *
5119 * TkCopyAndGlobalEval --
5120 *
5121 * This procedure makes a copy of a script then calls Tcl_GlobalEval
5122 * to evaluate it. It's used in situations where the execution of
5123 * a command may cause the original command string to be reallocated.
5124 *
5125 * Results:
5126 * Returns the result of evaluating script, including both a standard
5127 * Tcl completion code and a string in the interp's result.
5128 *
5129 * Side effects:
5130 * None.
5131 *
5132 *----------------------------------------------------------------------
5133 */
5134
5135 int
5136 TkCopyAndGlobalEval(interp, script)
5137 Tcl_Interp *interp; /* Interpreter in which to evaluate
5138 * script. */
5139 char *script; /* Script to evaluate. */
5140 {
5141 Tcl_DString buffer;
5142 int code;
5143
5144 Tcl_DStringInit(&buffer);
5145 Tcl_DStringAppend(&buffer, script, -1);
5146 code = Tcl_GlobalEval(interp, Tcl_DStringValue(&buffer));
5147 Tcl_DStringFree(&buffer);
5148 return code;
5149 }
5150
5151
5152
5153
5154
5155
5156