| 1 |
/* |
| 2 |
* tkEvent.c -- |
| 3 |
* |
| 4 |
* This file provides basic low-level facilities for managing |
| 5 |
* X events in Tk. |
| 6 |
* |
| 7 |
* Copyright (c) 1990-1994 The Regents of the University of California. |
| 8 |
* Copyright (c) 1994-1995 Sun Microsystems, Inc. |
| 9 |
* Copyright (c) 1998-2000 Ajuba Solutions. |
| 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: tkEvent.c,v 1.17.2.1 2003/07/19 01:03:25 hobbs Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "tkPort.h" |
| 18 |
#include "tkInt.h" |
| 19 |
#include <signal.h> |
| 20 |
|
| 21 |
/* This does not belong here it belong in the platform window manager code! */ |
| 22 |
extern Window XmuClientWindow _ANSI_ARGS_((Display *dpy, Window win)); |
| 23 |
|
| 24 |
/* |
| 25 |
* There's a potential problem if a handler is deleted while it's |
| 26 |
* current (i.e. its procedure is executing), since Tk_HandleEvent |
| 27 |
* will need to read the handler's "nextPtr" field when the procedure |
| 28 |
* returns. To handle this problem, structures of the type below |
| 29 |
* indicate the next handler to be processed for any (recursively |
| 30 |
* nested) dispatches in progress. The nextHandler fields get |
| 31 |
* updated if the handlers pointed to are deleted. Tk_HandleEvent |
| 32 |
* also needs to know if the entire window gets deleted; the winPtr |
| 33 |
* field is set to zero if that particular window gets deleted. |
| 34 |
*/ |
| 35 |
|
| 36 |
typedef struct InProgress { |
| 37 |
XEvent *eventPtr; /* Event currently being handled. */ |
| 38 |
TkWindow *winPtr; /* Window for event. Gets set to None if |
| 39 |
* window is deleted while event is being |
| 40 |
* handled. */ |
| 41 |
TkEventHandler *nextHandler; /* Next handler in search. */ |
| 42 |
struct InProgress *nextPtr; /* Next higher nested search. */ |
| 43 |
} InProgress; |
| 44 |
|
| 45 |
/* |
| 46 |
* For each call to Tk_CreateGenericHandler, an instance of the following |
| 47 |
* structure will be created. All of the active handlers are linked into a |
| 48 |
* list. |
| 49 |
*/ |
| 50 |
|
| 51 |
typedef struct GenericHandler { |
| 52 |
Tk_GenericProc *proc; /* Procedure to dispatch on all X events. */ |
| 53 |
ClientData clientData; /* Client data to pass to procedure. */ |
| 54 |
int deleteFlag; /* Flag to set when this handler is deleted. */ |
| 55 |
struct GenericHandler *nextPtr; |
| 56 |
/* Next handler in list of all generic |
| 57 |
* handlers, or NULL for end of list. */ |
| 58 |
} GenericHandler; |
| 59 |
|
| 60 |
/* |
| 61 |
* There's a potential problem if Tk_HandleEvent is entered recursively. |
| 62 |
* A handler cannot be deleted physically until we have returned from |
| 63 |
* calling it. Otherwise, we're looking at unallocated memory in advancing to |
| 64 |
* its `next' entry. We deal with the problem by using the `delete flag' and |
| 65 |
* deleting handlers only when it's known that there's no handler active. |
| 66 |
* |
| 67 |
*/ |
| 68 |
|
| 69 |
/* |
| 70 |
* The following structure is used for queueing X-style events on the |
| 71 |
* Tcl event queue. |
| 72 |
*/ |
| 73 |
|
| 74 |
typedef struct TkWindowEvent { |
| 75 |
Tcl_Event header; /* Standard information for all events. */ |
| 76 |
XEvent event; /* The X event. */ |
| 77 |
} TkWindowEvent; |
| 78 |
|
| 79 |
/* |
| 80 |
* Array of event masks corresponding to each X event: |
| 81 |
*/ |
| 82 |
|
| 83 |
static unsigned long eventMasks[TK_LASTEVENT] = { |
| 84 |
0, |
| 85 |
0, |
| 86 |
KeyPressMask, /* KeyPress */ |
| 87 |
KeyReleaseMask, /* KeyRelease */ |
| 88 |
ButtonPressMask, /* ButtonPress */ |
| 89 |
ButtonReleaseMask, /* ButtonRelease */ |
| 90 |
PointerMotionMask|PointerMotionHintMask|ButtonMotionMask |
| 91 |
|Button1MotionMask|Button2MotionMask|Button3MotionMask |
| 92 |
|Button4MotionMask|Button5MotionMask, |
| 93 |
/* MotionNotify */ |
| 94 |
EnterWindowMask, /* EnterNotify */ |
| 95 |
LeaveWindowMask, /* LeaveNotify */ |
| 96 |
FocusChangeMask, /* FocusIn */ |
| 97 |
FocusChangeMask, /* FocusOut */ |
| 98 |
KeymapStateMask, /* KeymapNotify */ |
| 99 |
ExposureMask, /* Expose */ |
| 100 |
ExposureMask, /* GraphicsExpose */ |
| 101 |
ExposureMask, /* NoExpose */ |
| 102 |
VisibilityChangeMask, /* VisibilityNotify */ |
| 103 |
SubstructureNotifyMask, /* CreateNotify */ |
| 104 |
StructureNotifyMask, /* DestroyNotify */ |
| 105 |
StructureNotifyMask, /* UnmapNotify */ |
| 106 |
StructureNotifyMask, /* MapNotify */ |
| 107 |
SubstructureRedirectMask, /* MapRequest */ |
| 108 |
StructureNotifyMask, /* ReparentNotify */ |
| 109 |
StructureNotifyMask, /* ConfigureNotify */ |
| 110 |
SubstructureRedirectMask, /* ConfigureRequest */ |
| 111 |
StructureNotifyMask, /* GravityNotify */ |
| 112 |
ResizeRedirectMask, /* ResizeRequest */ |
| 113 |
StructureNotifyMask, /* CirculateNotify */ |
| 114 |
SubstructureRedirectMask, /* CirculateRequest */ |
| 115 |
PropertyChangeMask, /* PropertyNotify */ |
| 116 |
0, /* SelectionClear */ |
| 117 |
0, /* SelectionRequest */ |
| 118 |
0, /* SelectionNotify */ |
| 119 |
ColormapChangeMask, /* ColormapNotify */ |
| 120 |
0, /* ClientMessage */ |
| 121 |
0, /* Mapping Notify */ |
| 122 |
VirtualEventMask, /* VirtualEvents */ |
| 123 |
ActivateMask, /* ActivateNotify */ |
| 124 |
ActivateMask, /* DeactivateNotify */ |
| 125 |
MouseWheelMask /* MouseWheelEvent */ |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/* |
| 130 |
* The structure below is used to store Data for the Event module that |
| 131 |
* must be kept thread-local. The "dataKey" is used to fetch the |
| 132 |
* thread-specific storage for the current thread. |
| 133 |
*/ |
| 134 |
|
| 135 |
typedef struct ThreadSpecificData { |
| 136 |
int handlersActive; /* The following variable has a non-zero |
| 137 |
* value when a handler is active. */ |
| 138 |
InProgress *pendingPtr; /* Topmost search in progress, or |
| 139 |
* NULL if none. */ |
| 140 |
|
| 141 |
GenericHandler *genericList; /* First handler in the list, or NULL. */ |
| 142 |
GenericHandler *lastGenericPtr; /* Last handler in list. */ |
| 143 |
|
| 144 |
GenericHandler *cmList; /* First handler in the list, or NULL. */ |
| 145 |
GenericHandler *lastCmPtr; /* Last handler in list. */ |
| 146 |
|
| 147 |
/* |
| 148 |
* If someone has called Tk_RestrictEvents, the information below |
| 149 |
* keeps track of it. |
| 150 |
*/ |
| 151 |
|
| 152 |
Tk_RestrictProc *restrictProc; |
| 153 |
/* Procedure to call. NULL means no |
| 154 |
* restrictProc is currently in effect. */ |
| 155 |
ClientData restrictArg; /* Argument to pass to restrictProc. */ |
| 156 |
} ThreadSpecificData; |
| 157 |
static Tcl_ThreadDataKey dataKey; |
| 158 |
|
| 159 |
/* |
| 160 |
* Prototypes for procedures that are only referenced locally within |
| 161 |
* this file. |
| 162 |
*/ |
| 163 |
|
| 164 |
static void DelayedMotionProc _ANSI_ARGS_((ClientData clientData)); |
| 165 |
static int WindowEventProc _ANSI_ARGS_((Tcl_Event *evPtr, |
| 166 |
int flags)); |
| 167 |
static int TkXErrorHandler _ANSI_ARGS_((ClientData clientData, |
| 168 |
XErrorEvent *errEventPtr)); |
| 169 |
|
| 170 |
|
| 171 |
/* |
| 172 |
*-------------------------------------------------------------- |
| 173 |
* |
| 174 |
* Tk_CreateEventHandler -- |
| 175 |
* |
| 176 |
* Arrange for a given procedure to be invoked whenever |
| 177 |
* events from a given class occur in a given window. |
| 178 |
* |
| 179 |
* Results: |
| 180 |
* None. |
| 181 |
* |
| 182 |
* Side effects: |
| 183 |
* From now on, whenever an event of the type given by |
| 184 |
* mask occurs for token and is processed by Tk_HandleEvent, |
| 185 |
* proc will be called. See the manual entry for details |
| 186 |
* of the calling sequence and return value for proc. |
| 187 |
* |
| 188 |
*-------------------------------------------------------------- |
| 189 |
*/ |
| 190 |
|
| 191 |
void |
| 192 |
Tk_CreateEventHandler(token, mask, proc, clientData) |
| 193 |
Tk_Window token; /* Token for window in which to |
| 194 |
* create handler. */ |
| 195 |
unsigned long mask; /* Events for which proc should |
| 196 |
* be called. */ |
| 197 |
Tk_EventProc *proc; /* Procedure to call for each |
| 198 |
* selected event */ |
| 199 |
ClientData clientData; /* Arbitrary data to pass to proc. */ |
| 200 |
{ |
| 201 |
register TkEventHandler *handlerPtr; |
| 202 |
register TkWindow *winPtr = (TkWindow *) token; |
| 203 |
int found; |
| 204 |
|
| 205 |
/* |
| 206 |
* Skim through the list of existing handlers to (a) compute the |
| 207 |
* overall event mask for the window (so we can pass this new |
| 208 |
* value to the X system) and (b) see if there's already a handler |
| 209 |
* declared with the same callback and clientData (if so, just |
| 210 |
* change the mask). If no existing handler matches, then create |
| 211 |
* a new handler. |
| 212 |
*/ |
| 213 |
|
| 214 |
found = 0; |
| 215 |
if (winPtr->handlerList == NULL) { |
| 216 |
handlerPtr = (TkEventHandler *) ckalloc( |
| 217 |
(unsigned) sizeof(TkEventHandler)); |
| 218 |
winPtr->handlerList = handlerPtr; |
| 219 |
goto initHandler; |
| 220 |
} else { |
| 221 |
for (handlerPtr = winPtr->handlerList; ; |
| 222 |
handlerPtr = handlerPtr->nextPtr) { |
| 223 |
if ((handlerPtr->proc == proc) |
| 224 |
&& (handlerPtr->clientData == clientData)) { |
| 225 |
handlerPtr->mask = mask; |
| 226 |
found = 1; |
| 227 |
} |
| 228 |
if (handlerPtr->nextPtr == NULL) { |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* Create a new handler if no matching old handler was found. |
| 236 |
*/ |
| 237 |
|
| 238 |
if (!found) { |
| 239 |
handlerPtr->nextPtr = (TkEventHandler *) |
| 240 |
ckalloc(sizeof(TkEventHandler)); |
| 241 |
handlerPtr = handlerPtr->nextPtr; |
| 242 |
initHandler: |
| 243 |
handlerPtr->mask = mask; |
| 244 |
handlerPtr->proc = proc; |
| 245 |
handlerPtr->clientData = clientData; |
| 246 |
handlerPtr->nextPtr = NULL; |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* No need to call XSelectInput: Tk always selects on all events |
| 251 |
* for all windows (needed to support bindings on classes and "all"). |
| 252 |
*/ |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
*-------------------------------------------------------------- |
| 257 |
* |
| 258 |
* Tk_DeleteEventHandler -- |
| 259 |
* |
| 260 |
* Delete a previously-created handler. |
| 261 |
* |
| 262 |
* Results: |
| 263 |
* None. |
| 264 |
* |
| 265 |
* Side effects: |
| 266 |
* If there existed a handler as described by the |
| 267 |
* parameters, the handler is deleted so that proc |
| 268 |
* will not be invoked again. |
| 269 |
* |
| 270 |
*-------------------------------------------------------------- |
| 271 |
*/ |
| 272 |
|
| 273 |
void |
| 274 |
Tk_DeleteEventHandler(token, mask, proc, clientData) |
| 275 |
Tk_Window token; /* Same as corresponding arguments passed */ |
| 276 |
unsigned long mask; /* previously to Tk_CreateEventHandler. */ |
| 277 |
Tk_EventProc *proc; |
| 278 |
ClientData clientData; |
| 279 |
{ |
| 280 |
register TkEventHandler *handlerPtr; |
| 281 |
register InProgress *ipPtr; |
| 282 |
TkEventHandler *prevPtr; |
| 283 |
register TkWindow *winPtr = (TkWindow *) token; |
| 284 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 285 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 286 |
|
| 287 |
/* |
| 288 |
* Find the event handler to be deleted, or return |
| 289 |
* immediately if it doesn't exist. |
| 290 |
*/ |
| 291 |
|
| 292 |
for (handlerPtr = winPtr->handlerList, prevPtr = NULL; ; |
| 293 |
prevPtr = handlerPtr, handlerPtr = handlerPtr->nextPtr) { |
| 294 |
if (handlerPtr == NULL) { |
| 295 |
return; |
| 296 |
} |
| 297 |
if ((handlerPtr->mask == mask) && (handlerPtr->proc == proc) |
| 298 |
&& (handlerPtr->clientData == clientData)) { |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
* If Tk_HandleEvent is about to process this handler, tell it to |
| 305 |
* process the next one instead. |
| 306 |
*/ |
| 307 |
|
| 308 |
for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) { |
| 309 |
if (ipPtr->nextHandler == handlerPtr) { |
| 310 |
ipPtr->nextHandler = handlerPtr->nextPtr; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
* Free resources associated with the handler. |
| 316 |
*/ |
| 317 |
|
| 318 |
if (prevPtr == NULL) { |
| 319 |
winPtr->handlerList = handlerPtr->nextPtr; |
| 320 |
} else { |
| 321 |
prevPtr->nextPtr = handlerPtr->nextPtr; |
| 322 |
} |
| 323 |
ckfree((char *) handlerPtr); |
| 324 |
|
| 325 |
|
| 326 |
/* |
| 327 |
* No need to call XSelectInput: Tk always selects on all events |
| 328 |
* for all windows (needed to support bindings on classes and "all"). |
| 329 |
*/ |
| 330 |
} |
| 331 |
|
| 332 |
/*-------------------------------------------------------------- |
| 333 |
* |
| 334 |
* Tk_CreateGenericHandler -- |
| 335 |
* |
| 336 |
* Register a procedure to be called on each X event, regardless |
| 337 |
* of display or window. Generic handlers are useful for capturing |
| 338 |
* events that aren't associated with windows, or events for windows |
| 339 |
* not managed by Tk. |
| 340 |
* |
| 341 |
* Results: |
| 342 |
* None. |
| 343 |
* |
| 344 |
* Side Effects: |
| 345 |
* From now on, whenever an X event is given to Tk_HandleEvent, |
| 346 |
* invoke proc, giving it clientData and the event as arguments. |
| 347 |
* |
| 348 |
*-------------------------------------------------------------- |
| 349 |
*/ |
| 350 |
|
| 351 |
void |
| 352 |
Tk_CreateGenericHandler(proc, clientData) |
| 353 |
Tk_GenericProc *proc; /* Procedure to call on every event. */ |
| 354 |
ClientData clientData; /* One-word value to pass to proc. */ |
| 355 |
{ |
| 356 |
GenericHandler *handlerPtr; |
| 357 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 358 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 359 |
|
| 360 |
handlerPtr = (GenericHandler *) ckalloc (sizeof (GenericHandler)); |
| 361 |
|
| 362 |
handlerPtr->proc = proc; |
| 363 |
handlerPtr->clientData = clientData; |
| 364 |
handlerPtr->deleteFlag = 0; |
| 365 |
handlerPtr->nextPtr = NULL; |
| 366 |
if (tsdPtr->genericList == NULL) { |
| 367 |
tsdPtr->genericList = handlerPtr; |
| 368 |
} else { |
| 369 |
tsdPtr->lastGenericPtr->nextPtr = handlerPtr; |
| 370 |
} |
| 371 |
tsdPtr->lastGenericPtr = handlerPtr; |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
*-------------------------------------------------------------- |
| 376 |
* |
| 377 |
* Tk_DeleteGenericHandler -- |
| 378 |
* |
| 379 |
* Delete a previously-created generic handler. |
| 380 |
* |
| 381 |
* Results: |
| 382 |
* None. |
| 383 |
* |
| 384 |
* Side Effects: |
| 385 |
* If there existed a handler as described by the parameters, |
| 386 |
* that handler is logically deleted so that proc will not be |
| 387 |
* invoked again. The physical deletion happens in the event |
| 388 |
* loop in Tk_HandleEvent. |
| 389 |
* |
| 390 |
*-------------------------------------------------------------- |
| 391 |
*/ |
| 392 |
|
| 393 |
void |
| 394 |
Tk_DeleteGenericHandler(proc, clientData) |
| 395 |
Tk_GenericProc *proc; |
| 396 |
ClientData clientData; |
| 397 |
{ |
| 398 |
GenericHandler * handler; |
| 399 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 400 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 401 |
|
| 402 |
for (handler = tsdPtr->genericList; handler; handler = handler->nextPtr) { |
| 403 |
if ((handler->proc == proc) && (handler->clientData == clientData)) { |
| 404 |
handler->deleteFlag = 1; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
/*-------------------------------------------------------------- |
| 410 |
* |
| 411 |
* Tk_CreateClientMessageHandler -- |
| 412 |
* |
| 413 |
* Register a procedure to be called on each ClientMessage event. |
| 414 |
* ClientMessage handlers are useful for Drag&Drop extensions. |
| 415 |
* |
| 416 |
* Results: |
| 417 |
* None. |
| 418 |
* |
| 419 |
* Side Effects: |
| 420 |
* From now on, whenever a ClientMessage event is received that isn't |
| 421 |
* a WM_PROTOCOL event or SelectionEvent, invoke proc, giving it |
| 422 |
* tkwin and the event as arguments. |
| 423 |
* |
| 424 |
*-------------------------------------------------------------- |
| 425 |
*/ |
| 426 |
|
| 427 |
void |
| 428 |
Tk_CreateClientMessageHandler(proc) |
| 429 |
Tk_ClientMessageProc *proc; /* Procedure to call on event. */ |
| 430 |
{ |
| 431 |
GenericHandler *handlerPtr; |
| 432 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 433 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 434 |
|
| 435 |
/* |
| 436 |
* We use a GenericHandler struct, because it's basically the same, |
| 437 |
* except with an extra clientData field we'll never use. |
| 438 |
*/ |
| 439 |
handlerPtr = (GenericHandler *) |
| 440 |
ckalloc (sizeof (GenericHandler)); |
| 441 |
|
| 442 |
handlerPtr->proc = (Tk_GenericProc *) proc; |
| 443 |
handlerPtr->clientData = NULL; /* never used */ |
| 444 |
handlerPtr->deleteFlag = 0; |
| 445 |
handlerPtr->nextPtr = NULL; |
| 446 |
if (tsdPtr->cmList == NULL) { |
| 447 |
tsdPtr->cmList = handlerPtr; |
| 448 |
} else { |
| 449 |
tsdPtr->lastCmPtr->nextPtr = handlerPtr; |
| 450 |
} |
| 451 |
tsdPtr->lastCmPtr = handlerPtr; |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
*-------------------------------------------------------------- |
| 456 |
* |
| 457 |
* Tk_DeleteClientMessageHandler -- |
| 458 |
* |
| 459 |
* Delete a previously-created ClientMessage handler. |
| 460 |
* |
| 461 |
* Results: |
| 462 |
* None. |
| 463 |
* |
| 464 |
* Side Effects: |
| 465 |
* If there existed a handler as described by the parameters, |
| 466 |
* that handler is logically deleted so that proc will not be |
| 467 |
* invoked again. The physical deletion happens in the event |
| 468 |
* loop in TkClientMessageEventProc. |
| 469 |
* |
| 470 |
*-------------------------------------------------------------- |
| 471 |
*/ |
| 472 |
|
| 473 |
void |
| 474 |
Tk_DeleteClientMessageHandler(proc) |
| 475 |
Tk_ClientMessageProc *proc; |
| 476 |
{ |
| 477 |
GenericHandler * handler; |
| 478 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 479 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 480 |
|
| 481 |
for (handler = tsdPtr->cmList; handler != NULL; |
| 482 |
handler = handler->nextPtr) { |
| 483 |
if (handler->proc == (Tk_GenericProc *) proc) { |
| 484 |
handler->deleteFlag = 1; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/* |
| 490 |
*-------------------------------------------------------------- |
| 491 |
* |
| 492 |
* TkEventInit -- |
| 493 |
* |
| 494 |
* This procedures initializes all the event module |
| 495 |
* structures used by the current thread. It must be |
| 496 |
* called before any other procedure in this file is |
| 497 |
* called. |
| 498 |
* |
| 499 |
* Results: |
| 500 |
* None. |
| 501 |
* |
| 502 |
* Side Effects: |
| 503 |
* None. |
| 504 |
* |
| 505 |
*-------------------------------------------------------------- |
| 506 |
*/ |
| 507 |
|
| 508 |
void |
| 509 |
TkEventInit _ANSI_ARGS_((void)) |
| 510 |
{ |
| 511 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 512 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 513 |
|
| 514 |
tsdPtr->handlersActive = 0; |
| 515 |
tsdPtr->pendingPtr = NULL; |
| 516 |
tsdPtr->genericList = NULL; |
| 517 |
tsdPtr->lastGenericPtr = NULL; |
| 518 |
tsdPtr->cmList = NULL; |
| 519 |
tsdPtr->lastCmPtr = NULL; |
| 520 |
tsdPtr->restrictProc = NULL; |
| 521 |
tsdPtr->restrictArg = NULL; |
| 522 |
} |
| 523 |
|
| 524 |
/* |
| 525 |
*-------------------------------------------------------------- |
| 526 |
* |
| 527 |
* TkXErrorHandler -- |
| 528 |
* |
| 529 |
* TkXErrorHandler is an error handler, to be installed |
| 530 |
* via Tk_CreateErrorHandler, that will set a flag if an |
| 531 |
* X error occurred. |
| 532 |
* |
| 533 |
* Results: |
| 534 |
* Always returns 0, indicating that the X error was |
| 535 |
* handled. |
| 536 |
* |
| 537 |
* Side effects: |
| 538 |
* None. |
| 539 |
* |
| 540 |
*-------------------------------------------------------------- |
| 541 |
*/ |
| 542 |
|
| 543 |
static int |
| 544 |
TkXErrorHandler (clientData, errEventPtr) |
| 545 |
ClientData clientData; /* Pointer to flag we set */ |
| 546 |
XErrorEvent *errEventPtr; /* X error info */ |
| 547 |
{ |
| 548 |
int *error; |
| 549 |
|
| 550 |
error = (int *) clientData; |
| 551 |
*error = 1; |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
/* |
| 556 |
*-------------------------------------------------------------- |
| 557 |
* |
| 558 |
* ParentXId -- |
| 559 |
* |
| 560 |
* Returns the parent of the given window, or "None" |
| 561 |
* if the window doesn't exist. |
| 562 |
* |
| 563 |
* Results: |
| 564 |
* Returns an X window ID. |
| 565 |
* |
| 566 |
* Side effects: |
| 567 |
* None. |
| 568 |
* |
| 569 |
*-------------------------------------------------------------- |
| 570 |
*/ |
| 571 |
|
| 572 |
static Window |
| 573 |
ParentXId(display, w) |
| 574 |
Display *display; |
| 575 |
Window w; |
| 576 |
{ |
| 577 |
Tk_ErrorHandler handler; |
| 578 |
int gotXError; |
| 579 |
Status status; |
| 580 |
Window parent; |
| 581 |
Window root; |
| 582 |
Window *childList; |
| 583 |
unsigned int nChildren; |
| 584 |
|
| 585 |
/* Handle errors ourselves. */ |
| 586 |
|
| 587 |
gotXError = 0; |
| 588 |
handler = Tk_CreateErrorHandler(display, -1, -1, -1, |
| 589 |
TkXErrorHandler, (ClientData) (&gotXError)); |
| 590 |
|
| 591 |
/* Get the parent window. */ |
| 592 |
|
| 593 |
status = XQueryTree(display, w, &root, &parent, &childList, &nChildren); |
| 594 |
|
| 595 |
/* Do some cleanup; gotta return "None" if we got an error. */ |
| 596 |
|
| 597 |
Tk_DeleteErrorHandler(handler); |
| 598 |
XSync(display, False); |
| 599 |
if (status != 0 && childList != NULL) { |
| 600 |
XFree(childList); |
| 601 |
} |
| 602 |
if (status == 0) { |
| 603 |
parent = None; |
| 604 |
} |
| 605 |
|
| 606 |
return parent; |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
*-------------------------------------------------------------- |
| 611 |
* |
| 612 |
* Tk_HandleEvent -- |
| 613 |
* |
| 614 |
* Given an event, invoke all the handlers that have |
| 615 |
* been registered for the event. |
| 616 |
* |
| 617 |
* Results: |
| 618 |
* None. |
| 619 |
* |
| 620 |
* Side effects: |
| 621 |
* Depends on the handlers. |
| 622 |
* |
| 623 |
*-------------------------------------------------------------- |
| 624 |
*/ |
| 625 |
|
| 626 |
void |
| 627 |
Tk_HandleEvent(eventPtr) |
| 628 |
XEvent *eventPtr; /* Event to dispatch. */ |
| 629 |
{ |
| 630 |
register TkEventHandler *handlerPtr; |
| 631 |
register GenericHandler *genericPtr; |
| 632 |
register GenericHandler *genPrevPtr; |
| 633 |
TkWindow *winPtr; |
| 634 |
unsigned long mask; |
| 635 |
InProgress ip; |
| 636 |
Window handlerWindow; |
| 637 |
Window parentXId; |
| 638 |
TkDisplay *dispPtr; |
| 639 |
Tcl_Interp *interp = (Tcl_Interp *) NULL; |
| 640 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 641 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 642 |
|
| 643 |
/* |
| 644 |
* Hack for simulated X-events: Correct the state field |
| 645 |
* of the event record to match with the ButtonPress |
| 646 |
* and ButtonRelease events. |
| 647 |
*/ |
| 648 |
|
| 649 |
if (eventPtr->type==ButtonPress) { |
| 650 |
dispPtr = TkGetDisplay(eventPtr->xbutton.display); |
| 651 |
dispPtr->mouseButtonWindow = eventPtr->xbutton.window; |
| 652 |
eventPtr->xbutton.state |= dispPtr->mouseButtonState; |
| 653 |
switch (eventPtr->xbutton.button) { |
| 654 |
case 1: dispPtr->mouseButtonState |= Button1Mask; break; |
| 655 |
case 2: dispPtr->mouseButtonState |= Button2Mask; break; |
| 656 |
case 3: dispPtr->mouseButtonState |= Button3Mask; break; |
| 657 |
} |
| 658 |
} else if (eventPtr->type==ButtonRelease) { |
| 659 |
dispPtr = TkGetDisplay(eventPtr->xbutton.display); |
| 660 |
dispPtr->mouseButtonWindow = 0; |
| 661 |
switch (eventPtr->xbutton.button) { |
| 662 |
case 1: dispPtr->mouseButtonState &= ~Button1Mask; break; |
| 663 |
case 2: dispPtr->mouseButtonState &= ~Button2Mask; break; |
| 664 |
case 3: dispPtr->mouseButtonState &= ~Button3Mask; break; |
| 665 |
} |
| 666 |
eventPtr->xbutton.state |= dispPtr->mouseButtonState; |
| 667 |
} else if (eventPtr->type==MotionNotify) { |
| 668 |
dispPtr = TkGetDisplay(eventPtr->xmotion.display); |
| 669 |
if (dispPtr->mouseButtonState & (Button1Mask|Button2Mask|Button3Mask)) { |
| 670 |
if (eventPtr->xbutton.window != dispPtr->mouseButtonWindow) { |
| 671 |
/* |
| 672 |
* This motion event should not be interpreted as a button |
| 673 |
* press + motion event since this is not the same window |
| 674 |
* the button was pressed down in. |
| 675 |
*/ |
| 676 |
dispPtr->mouseButtonState &= |
| 677 |
~(Button1Mask|Button2Mask|Button3Mask); |
| 678 |
dispPtr->mouseButtonWindow = 0; |
| 679 |
} else { |
| 680 |
eventPtr->xmotion.state |= dispPtr->mouseButtonState; |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
/* |
| 686 |
* Next, invoke all the generic event handlers (those that are |
| 687 |
* invoked for all events). If a generic event handler reports that |
| 688 |
* an event is fully processed, go no further. |
| 689 |
*/ |
| 690 |
|
| 691 |
for (genPrevPtr = NULL, genericPtr = tsdPtr->genericList; |
| 692 |
genericPtr != NULL; ) { |
| 693 |
if (genericPtr->deleteFlag) { |
| 694 |
if (!tsdPtr->handlersActive) { |
| 695 |
GenericHandler *tmpPtr; |
| 696 |
|
| 697 |
/* |
| 698 |
* This handler needs to be deleted and there are no |
| 699 |
* calls pending through the handler, so now is a safe |
| 700 |
* time to delete it. |
| 701 |
*/ |
| 702 |
|
| 703 |
tmpPtr = genericPtr->nextPtr; |
| 704 |
if (genPrevPtr == NULL) { |
| 705 |
tsdPtr->genericList = tmpPtr; |
| 706 |
} else { |
| 707 |
genPrevPtr->nextPtr = tmpPtr; |
| 708 |
} |
| 709 |
if (tmpPtr == NULL) { |
| 710 |
tsdPtr->lastGenericPtr = genPrevPtr; |
| 711 |
} |
| 712 |
(void) ckfree((char *) genericPtr); |
| 713 |
genericPtr = tmpPtr; |
| 714 |
continue; |
| 715 |
} |
| 716 |
} else { |
| 717 |
int done; |
| 718 |
|
| 719 |
tsdPtr->handlersActive++; |
| 720 |
done = (*genericPtr->proc)(genericPtr->clientData, eventPtr); |
| 721 |
tsdPtr->handlersActive--; |
| 722 |
if (done) { |
| 723 |
return; |
| 724 |
} |
| 725 |
} |
| 726 |
genPrevPtr = genericPtr; |
| 727 |
genericPtr = genPrevPtr->nextPtr; |
| 728 |
} |
| 729 |
|
| 730 |
/* |
| 731 |
* If the event is a MappingNotify event, find its display and |
| 732 |
* refresh the keyboard mapping information for the display. |
| 733 |
* After that there's nothing else to do with the event, so just |
| 734 |
* quit. |
| 735 |
*/ |
| 736 |
|
| 737 |
if (eventPtr->type == MappingNotify) { |
| 738 |
dispPtr = TkGetDisplay(eventPtr->xmapping.display); |
| 739 |
if (dispPtr != NULL) { |
| 740 |
XRefreshKeyboardMapping(&eventPtr->xmapping); |
| 741 |
dispPtr->bindInfoStale = 1; |
| 742 |
} |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
/* |
| 747 |
* Events selected by StructureNotify require special handling. |
| 748 |
* They look the same as those selected by SubstructureNotify. |
| 749 |
* The only difference is whether the "event" and "window" fields |
| 750 |
* are the same. Compare the two fields and convert StructureNotify |
| 751 |
* to SubstructureNotify if necessary. |
| 752 |
*/ |
| 753 |
|
| 754 |
handlerWindow = eventPtr->xany.window; |
| 755 |
mask = eventMasks[eventPtr->xany.type]; |
| 756 |
if (mask == StructureNotifyMask) { |
| 757 |
if (eventPtr->xmap.event != eventPtr->xmap.window) { |
| 758 |
mask = SubstructureNotifyMask; |
| 759 |
handlerWindow = eventPtr->xmap.event; |
| 760 |
} |
| 761 |
} |
| 762 |
winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, handlerWindow); |
| 763 |
|
| 764 |
/* Some Drag&Drop messages get sent to wrappers for which there is |
| 765 |
* an X ICCM scheme to locate the "real" window - it is not clear |
| 766 |
* if this is "done right" yet but should be harmless. |
| 767 |
*/ |
| 768 |
if (winPtr == NULL && eventPtr->type == ClientMessage) { |
| 769 |
handlerWindow = XmuClientWindow(eventPtr->xany.display, handlerWindow); |
| 770 |
winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, handlerWindow); |
| 771 |
} |
| 772 |
|
| 773 |
if (winPtr == NULL) { |
| 774 |
/* |
| 775 |
* There isn't a TkWindow structure for this window. |
| 776 |
* However, if the event is a PropertyNotify event then call |
| 777 |
* the selection manager (it deals beneath-the-table with |
| 778 |
* certain properties). Also, if the window's parent is a |
| 779 |
* Tk window that has the TK_PROP_PROPCHANGE flag set, then |
| 780 |
* we must propagate the PropertyNotify event up to the parent. |
| 781 |
*/ |
| 782 |
|
| 783 |
if (eventPtr->type != PropertyNotify) { |
| 784 |
return; |
| 785 |
} |
| 786 |
|
| 787 |
TkSelPropProc(eventPtr); |
| 788 |
|
| 789 |
/* Get handlerWindow's parent. */ |
| 790 |
|
| 791 |
parentXId = ParentXId(eventPtr->xany.display, handlerWindow); |
| 792 |
if (parentXId == None) { |
| 793 |
return; |
| 794 |
} |
| 795 |
|
| 796 |
winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, parentXId); |
| 797 |
if (winPtr == NULL) { |
| 798 |
return; |
| 799 |
} |
| 800 |
|
| 801 |
if (!(winPtr->flags & TK_PROP_PROPCHANGE)) { |
| 802 |
return; |
| 803 |
} |
| 804 |
|
| 805 |
handlerWindow = parentXId; |
| 806 |
} |
| 807 |
|
| 808 |
/* |
| 809 |
* Once a window has started getting deleted, don't process any more |
| 810 |
* events for it except for the DestroyNotify event. This check is |
| 811 |
* needed because a DestroyNotify handler could re-invoke the event |
| 812 |
* loop, causing other pending events to be handled for the window |
| 813 |
* (the window doesn't get totally expunged from our tables until |
| 814 |
* after the DestroyNotify event has been completely handled). |
| 815 |
*/ |
| 816 |
|
| 817 |
if ((winPtr->flags & TK_ALREADY_DEAD) |
| 818 |
&& (eventPtr->type != DestroyNotify)) { |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
if (winPtr->mainPtr != NULL) { |
| 823 |
|
| 824 |
/* |
| 825 |
* Protect interpreter for this window from possible deletion |
| 826 |
* while we are dealing with the event for this window. Thus, |
| 827 |
* widget writers do not have to worry about protecting the |
| 828 |
* interpreter in their own code. |
| 829 |
*/ |
| 830 |
|
| 831 |
interp = winPtr->mainPtr->interp; |
| 832 |
Tcl_Preserve((ClientData) interp); |
| 833 |
|
| 834 |
/* |
| 835 |
* Call focus-related code to look at FocusIn, FocusOut, Enter, |
| 836 |
* and Leave events; depending on its return value, ignore the |
| 837 |
* event. |
| 838 |
*/ |
| 839 |
|
| 840 |
if ((mask & (FocusChangeMask|EnterWindowMask|LeaveWindowMask)) |
| 841 |
&& !TkFocusFilterEvent(winPtr, eventPtr)) { |
| 842 |
Tcl_Release((ClientData) interp); |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
/* |
| 847 |
* Redirect KeyPress and KeyRelease events to the focus window, |
| 848 |
* or ignore them entirely if there is no focus window. We also |
| 849 |
* route the MouseWheel event to the focus window. The MouseWheel |
| 850 |
* event is an extension to the X event set. Currently, it is only |
| 851 |
* available on the Windows version of Tk. |
| 852 |
*/ |
| 853 |
|
| 854 |
if (mask & (KeyPressMask|KeyReleaseMask|MouseWheelMask)) { |
| 855 |
winPtr->dispPtr->lastEventTime = eventPtr->xkey.time; |
| 856 |
winPtr = TkFocusKeyEvent(winPtr, eventPtr); |
| 857 |
if (winPtr == NULL) { |
| 858 |
Tcl_Release((ClientData) interp); |
| 859 |
return; |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
/* |
| 864 |
* Call a grab-related procedure to do special processing on |
| 865 |
* pointer events. |
| 866 |
*/ |
| 867 |
|
| 868 |
if (mask & (ButtonPressMask|ButtonReleaseMask|PointerMotionMask |
| 869 |
|EnterWindowMask|LeaveWindowMask)) { |
| 870 |
if (mask & (ButtonPressMask|ButtonReleaseMask)) { |
| 871 |
winPtr->dispPtr->lastEventTime = eventPtr->xbutton.time; |
| 872 |
} else if (mask & PointerMotionMask) { |
| 873 |
winPtr->dispPtr->lastEventTime = eventPtr->xmotion.time; |
| 874 |
} else { |
| 875 |
winPtr->dispPtr->lastEventTime = eventPtr->xcrossing.time; |
| 876 |
} |
| 877 |
if (TkPointerEvent(eventPtr, winPtr) == 0) { |
| 878 |
goto done; |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
#ifdef TK_USE_INPUT_METHODS |
| 884 |
/* |
| 885 |
* Pass the event to the input method(s), if there are any, and |
| 886 |
* discard the event if the input method(s) insist. Create the |
| 887 |
* input context for the window if it hasn't already been done |
| 888 |
* (XFilterEvent needs this context). XIM is only ever enabled on |
| 889 |
* Unix, but this hasn't been factored out of the generic code yet. |
| 890 |
*/ |
| 891 |
dispPtr = winPtr->dispPtr; |
| 892 |
if ((dispPtr->flags & TK_DISPLAY_USE_IM)) { |
| 893 |
if (!(winPtr->flags & (TK_CHECKED_IC|TK_ALREADY_DEAD))) { |
| 894 |
winPtr->flags |= TK_CHECKED_IC; |
| 895 |
if (dispPtr->inputMethod != NULL) { |
| 896 |
#if TK_XIM_SPOT |
| 897 |
if (dispPtr->flags & TK_DISPLAY_XIM_SPOT) { |
| 898 |
XVaNestedList preedit_attr; |
| 899 |
XPoint spot = {0, 0}; |
| 900 |
|
| 901 |
if (dispPtr->inputXfs == NULL) { |
| 902 |
/* |
| 903 |
* We only need to create one XFontSet |
| 904 |
*/ |
| 905 |
char **missing_list; |
| 906 |
int missing_count; |
| 907 |
char *def_string; |
| 908 |
|
| 909 |
dispPtr->inputXfs = XCreateFontSet(dispPtr->display, |
| 910 |
"-*-*-*-R-Normal--14-130-75-75-*-*", |
| 911 |
&missing_list, &missing_count, &def_string); |
| 912 |
if (missing_count > 0) { |
| 913 |
XFreeStringList(missing_list); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
preedit_attr = XVaCreateNestedList(0, XNSpotLocation, |
| 918 |
&spot, XNFontSet, dispPtr->inputXfs, NULL); |
| 919 |
if (winPtr->inputContext != NULL) |
| 920 |
panic("inputContext not NULL"); |
| 921 |
winPtr->inputContext = XCreateIC(dispPtr->inputMethod, |
| 922 |
XNInputStyle, XIMPreeditPosition|XIMStatusNothing, |
| 923 |
XNClientWindow, winPtr->window, |
| 924 |
XNFocusWindow, winPtr->window, |
| 925 |
XNPreeditAttributes, preedit_attr, |
| 926 |
NULL); |
| 927 |
XFree(preedit_attr); |
| 928 |
} else { |
| 929 |
if (winPtr->inputContext != NULL) |
| 930 |
panic("inputContext not NULL"); |
| 931 |
winPtr->inputContext = XCreateIC(dispPtr->inputMethod, |
| 932 |
XNInputStyle, XIMPreeditNothing|XIMStatusNothing, |
| 933 |
XNClientWindow, winPtr->window, |
| 934 |
XNFocusWindow, winPtr->window, |
| 935 |
NULL); |
| 936 |
} |
| 937 |
#else |
| 938 |
if (winPtr->inputContext != NULL) |
| 939 |
panic("inputContext not NULL"); |
| 940 |
winPtr->inputContext = XCreateIC(dispPtr->inputMethod, |
| 941 |
XNInputStyle, XIMPreeditNothing|XIMStatusNothing, |
| 942 |
XNClientWindow, winPtr->window, |
| 943 |
XNFocusWindow, winPtr->window, |
| 944 |
NULL); |
| 945 |
#endif |
| 946 |
} |
| 947 |
} |
| 948 |
if (eventPtr->type == KeyPress || eventPtr->type == KeyRelease) { |
| 949 |
if (XFilterEvent(eventPtr, None)) { |
| 950 |
goto done; |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
#endif /* TK_USE_INPUT_METHODS */ |
| 955 |
|
| 956 |
/* |
| 957 |
* For events where it hasn't already been done, update the current |
| 958 |
* time in the display. |
| 959 |
*/ |
| 960 |
|
| 961 |
if (eventPtr->type == PropertyNotify) { |
| 962 |
winPtr->dispPtr->lastEventTime = eventPtr->xproperty.time; |
| 963 |
} |
| 964 |
|
| 965 |
/* |
| 966 |
* There's a potential interaction here with Tk_DeleteEventHandler. |
| 967 |
* Read the documentation for pendingPtr. |
| 968 |
*/ |
| 969 |
ip.eventPtr = eventPtr; |
| 970 |
ip.winPtr = winPtr; |
| 971 |
ip.nextHandler = NULL; |
| 972 |
ip.nextPtr = tsdPtr->pendingPtr; |
| 973 |
|
| 974 |
/* WARNING: this stores stack address - longjmp() and |
| 975 |
similar "exception" mechanisms need to take steps |
| 976 |
to avoid bypassing the cleanup in this code |
| 977 |
*/ |
| 978 |
|
| 979 |
tsdPtr->pendingPtr = &ip; |
| 980 |
if (mask == 0) { |
| 981 |
if ((eventPtr->type == SelectionClear) |
| 982 |
|| (eventPtr->type == SelectionRequest) |
| 983 |
|| (eventPtr->type == SelectionNotify)) { |
| 984 |
TkSelEventProc((Tk_Window) winPtr, eventPtr); |
| 985 |
} else if (eventPtr->type == ClientMessage) { |
| 986 |
if (eventPtr->xclient.message_type == |
| 987 |
Tk_InternAtom((Tk_Window) winPtr, "WM_PROTOCOLS")) { |
| 988 |
TkWmProtocolEventProc(winPtr, eventPtr); |
| 989 |
} else { |
| 990 |
/* Perl/Tk had/has its own hook for this */ |
| 991 |
if (eventPtr && winPtr && winPtr->mainPtr) { |
| 992 |
LangClientMessage(winPtr->mainPtr->interp, (Tk_Window) winPtr, eventPtr); |
| 993 |
} |
| 994 |
|
| 995 |
/* |
| 996 |
* Finally, invoke any ClientMessage event handlers. |
| 997 |
*/ |
| 998 |
|
| 999 |
for (genPrevPtr = NULL, genericPtr = tsdPtr->cmList; |
| 1000 |
genericPtr != NULL; ) { |
| 1001 |
if (genericPtr->deleteFlag) { |
| 1002 |
if (!tsdPtr->handlersActive) { |
| 1003 |
GenericHandler *tmpPtr; |
| 1004 |
|
| 1005 |
/* |
| 1006 |
* This handler needs to be deleted and there are |
| 1007 |
* no calls pending through any handlers, so now |
| 1008 |
* is a safe time to delete it. |
| 1009 |
*/ |
| 1010 |
|
| 1011 |
tmpPtr = genericPtr->nextPtr; |
| 1012 |
if (genPrevPtr == NULL) { |
| 1013 |
tsdPtr->cmList = tmpPtr; |
| 1014 |
} else { |
| 1015 |
genPrevPtr->nextPtr = tmpPtr; |
| 1016 |
} |
| 1017 |
if (tmpPtr == NULL) { |
| 1018 |
tsdPtr->lastGenericPtr = genPrevPtr; |
| 1019 |
} |
| 1020 |
(void) ckfree((char *) genericPtr); |
| 1021 |
genericPtr = tmpPtr; |
| 1022 |
continue; |
| 1023 |
} |
| 1024 |
} else { |
| 1025 |
int done; |
| 1026 |
|
| 1027 |
tsdPtr->handlersActive++; |
| 1028 |
done = (*(Tk_ClientMessageProc *)genericPtr->proc) |
| 1029 |
((Tk_Window) winPtr, eventPtr); |
| 1030 |
tsdPtr->handlersActive--; |
| 1031 |
if (done) { |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
genPrevPtr = genericPtr; |
| 1036 |
genericPtr = genPrevPtr->nextPtr; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} else { |
| 1041 |
for (handlerPtr = winPtr->handlerList; handlerPtr != NULL; ) { |
| 1042 |
if ((handlerPtr->mask & mask) != 0) { |
| 1043 |
ip.nextHandler = handlerPtr->nextPtr; |
| 1044 |
(*(handlerPtr->proc))(handlerPtr->clientData, eventPtr); |
| 1045 |
handlerPtr = ip.nextHandler; |
| 1046 |
} else { |
| 1047 |
handlerPtr = handlerPtr->nextPtr; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
/* |
| 1052 |
* Pass the event to the "bind" command mechanism. But, don't |
| 1053 |
* do this for SubstructureNotify events. The "bind" command |
| 1054 |
* doesn't support them anyway, and it's easier to filter out |
| 1055 |
* these events here than in the lower-level procedures. |
| 1056 |
*/ |
| 1057 |
|
| 1058 |
/* |
| 1059 |
* ...well, except when we use the tkwm patches, in which case |
| 1060 |
* we DO handle CreateNotify events, so we gotta pass 'em through. |
| 1061 |
*/ |
| 1062 |
|
| 1063 |
if ((ip.winPtr != None) |
| 1064 |
&& ((mask != SubstructureNotifyMask) |
| 1065 |
|| (eventPtr->type == CreateNotify))) { |
| 1066 |
TkBindEventProc(winPtr, eventPtr); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
tsdPtr->pendingPtr = ip.nextPtr; |
| 1070 |
done: |
| 1071 |
/* |
| 1072 |
* Release the interpreter for this window so that it can be potentially |
| 1073 |
* deleted if requested. |
| 1074 |
*/ |
| 1075 |
|
| 1076 |
if (interp != (Tcl_Interp *) NULL) { |
| 1077 |
Tcl_Release((ClientData) interp); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
/* |
| 1082 |
*-------------------------------------------------------------- |
| 1083 |
* |
| 1084 |
* TkEventDeadWindow -- |
| 1085 |
* |
| 1086 |
* This procedure is invoked when it is determined that |
| 1087 |
* a window is dead. It cleans up event-related information |
| 1088 |
* about the window. |
| 1089 |
* |
| 1090 |
* Results: |
| 1091 |
* None. |
| 1092 |
* |
| 1093 |
* Side effects: |
| 1094 |
* Various things get cleaned up and recycled. |
| 1095 |
* |
| 1096 |
*-------------------------------------------------------------- |
| 1097 |
*/ |
| 1098 |
|
| 1099 |
void |
| 1100 |
TkEventDeadWindow(winPtr) |
| 1101 |
TkWindow *winPtr; /* Information about the window |
| 1102 |
* that is being deleted. */ |
| 1103 |
{ |
| 1104 |
register TkEventHandler *handlerPtr; |
| 1105 |
register InProgress *ipPtr; |
| 1106 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 1107 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 1108 |
|
| 1109 |
/* |
| 1110 |
* While deleting all the handlers, be careful to check for |
| 1111 |
* Tk_HandleEvent being about to process one of the deleted |
| 1112 |
* handlers. If it is, tell it to quit (all of the handlers |
| 1113 |
* are being deleted). |
| 1114 |
*/ |
| 1115 |
|
| 1116 |
while (winPtr->handlerList != NULL) { |
| 1117 |
handlerPtr = winPtr->handlerList; |
| 1118 |
winPtr->handlerList = handlerPtr->nextPtr; |
| 1119 |
for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL; |
| 1120 |
ipPtr = ipPtr->nextPtr) { |
| 1121 |
if (ipPtr->nextHandler == handlerPtr) { |
| 1122 |
ipPtr->nextHandler = NULL; |
| 1123 |
} |
| 1124 |
if (ipPtr->winPtr == winPtr) { |
| 1125 |
ipPtr->winPtr = None; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
ckfree((char *) handlerPtr); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
/* |
| 1133 |
*---------------------------------------------------------------------- |
| 1134 |
* |
| 1135 |
* TkCurrentTime -- |
| 1136 |
* |
| 1137 |
* Try to deduce the current time. "Current time" means the time |
| 1138 |
* of the event that led to the current code being executed, which |
| 1139 |
* means the time in the most recently-nested invocation of |
| 1140 |
* Tk_HandleEvent. |
| 1141 |
* |
| 1142 |
* Results: |
| 1143 |
* The return value is the time from the current event, or |
| 1144 |
* CurrentTime if there is no current event or if the current |
| 1145 |
* event contains no time. |
| 1146 |
* |
| 1147 |
* Side effects: |
| 1148 |
* None. |
| 1149 |
* |
| 1150 |
*---------------------------------------------------------------------- |
| 1151 |
*/ |
| 1152 |
|
| 1153 |
Time |
| 1154 |
TkCurrentTime(dispPtr, fallbackCurrent) |
| 1155 |
TkDisplay *dispPtr; /* Display for which the time is desired. */ |
| 1156 |
{ |
| 1157 |
register XEvent *eventPtr; |
| 1158 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 1159 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 1160 |
InProgress *pending; |
| 1161 |
|
| 1162 |
pending = tsdPtr->pendingPtr; |
| 1163 |
while (pending != NULL) { |
| 1164 |
eventPtr = pending->eventPtr; |
| 1165 |
switch (eventPtr->type) { |
| 1166 |
case ButtonPress: |
| 1167 |
case ButtonRelease: |
| 1168 |
return eventPtr->xbutton.time; |
| 1169 |
case KeyPress: |
| 1170 |
case KeyRelease: |
| 1171 |
return eventPtr->xkey.time; |
| 1172 |
case MotionNotify: |
| 1173 |
return eventPtr->xmotion.time; |
| 1174 |
case EnterNotify: |
| 1175 |
case LeaveNotify: |
| 1176 |
return eventPtr->xcrossing.time; |
| 1177 |
case PropertyNotify: |
| 1178 |
return eventPtr->xproperty.time; |
| 1179 |
} |
| 1180 |
pending = pending->nextPtr; |
| 1181 |
} |
| 1182 |
return (fallbackCurrent) ? CurrentTime : dispPtr->lastEventTime; |
| 1183 |
} |
| 1184 |
|
| 1185 |
/* |
| 1186 |
*---------------------------------------------------------------------- |
| 1187 |
* |
| 1188 |
* Tk_RestrictEvents -- |
| 1189 |
* |
| 1190 |
* This procedure is used to globally restrict the set of events |
| 1191 |
* that will be dispatched. The restriction is done by filtering |
| 1192 |
* all incoming X events through a procedure that determines |
| 1193 |
* whether they are to be processed immediately, deferred, or |
| 1194 |
* discarded. |
| 1195 |
* |
| 1196 |
* Results: |
| 1197 |
* The return value is the previous restriction procedure in effect, |
| 1198 |
* if there was one, or NULL if there wasn't. |
| 1199 |
* |
| 1200 |
* Side effects: |
| 1201 |
* From now on, proc will be called to determine whether to process, |
| 1202 |
* defer or discard each incoming X event. |
| 1203 |
* |
| 1204 |
*---------------------------------------------------------------------- |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
Tk_RestrictProc * |
| 1208 |
Tk_RestrictEvents(proc, arg, prevArgPtr) |
| 1209 |
Tk_RestrictProc *proc; /* Procedure to call for each incoming |
| 1210 |
* event. */ |
| 1211 |
ClientData arg; /* Arbitrary argument to pass to proc. */ |
| 1212 |
ClientData *prevArgPtr; /* Place to store information about previous |
| 1213 |
* argument. */ |
| 1214 |
{ |
| 1215 |
Tk_RestrictProc *prev; |
| 1216 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 1217 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 1218 |
|
| 1219 |
prev = tsdPtr->restrictProc; |
| 1220 |
*prevArgPtr = tsdPtr->restrictArg; |
| 1221 |
tsdPtr->restrictProc = proc; |
| 1222 |
tsdPtr->restrictArg = arg; |
| 1223 |
return prev; |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* |
| 1227 |
*---------------------------------------------------------------------- |
| 1228 |
* |
| 1229 |
* Tk_CollapseMotionEvents -- |
| 1230 |
* |
| 1231 |
* This procedure controls whether we collapse motion events in a |
| 1232 |
* particular display or not. |
| 1233 |
* |
| 1234 |
* Results: |
| 1235 |
* The return value is the previous collapse value in effect. |
| 1236 |
* |
| 1237 |
* Side effects: |
| 1238 |
* Filtering of motion events may be changed after calling this. |
| 1239 |
* |
| 1240 |
*---------------------------------------------------------------------- |
| 1241 |
*/ |
| 1242 |
|
| 1243 |
int |
| 1244 |
Tk_CollapseMotionEvents(display, collapse) |
| 1245 |
Display *display; /* Display handling these events. */ |
| 1246 |
int collapse; /* boolean value that specifies whether |
| 1247 |
* motion events should be collapsed. */ |
| 1248 |
{ |
| 1249 |
TkDisplay *dispPtr = (TkDisplay *) display; |
| 1250 |
int prev = (dispPtr->flags & TK_DISPLAY_COLLAPSE_MOTION_EVENTS); |
| 1251 |
|
| 1252 |
if (collapse) { |
| 1253 |
dispPtr->flags |= TK_DISPLAY_COLLAPSE_MOTION_EVENTS; |
| 1254 |
} else { |
| 1255 |
dispPtr->flags &= ~TK_DISPLAY_COLLAPSE_MOTION_EVENTS; |
| 1256 |
} |
| 1257 |
return prev; |
| 1258 |
} |
| 1259 |
|
| 1260 |
/* |
| 1261 |
*---------------------------------------------------------------------- |
| 1262 |
* |
| 1263 |
* Tk_QueueWindowEvent -- |
| 1264 |
* |
| 1265 |
* Given an X-style window event, this procedure adds it to the |
| 1266 |
* Tcl event queue at the given position. This procedure also |
| 1267 |
* performs mouse motion event collapsing if possible. |
| 1268 |
* |
| 1269 |
* Results: |
| 1270 |
* None. |
| 1271 |
* |
| 1272 |
* Side effects: |
| 1273 |
* Adds stuff to the event queue, which will eventually be |
| 1274 |
* processed. |
| 1275 |
* |
| 1276 |
*---------------------------------------------------------------------- |
| 1277 |
*/ |
| 1278 |
|
| 1279 |
void |
| 1280 |
Tk_QueueWindowEvent(eventPtr, position) |
| 1281 |
XEvent *eventPtr; /* Event to add to queue. This |
| 1282 |
* procedures copies it before adding |
| 1283 |
* it to the queue. */ |
| 1284 |
Tcl_QueuePosition position; /* Where to put it on the queue: |
| 1285 |
* TCL_QUEUE_TAIL, TCL_QUEUE_HEAD, |
| 1286 |
* or TCL_QUEUE_MARK. */ |
| 1287 |
{ |
| 1288 |
TkWindowEvent *wevPtr; |
| 1289 |
TkDisplay *dispPtr; |
| 1290 |
|
| 1291 |
/* |
| 1292 |
* Find our display structure for the event's display. |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
for (dispPtr = TkGetDisplayList(); ; dispPtr = dispPtr->nextPtr) { |
| 1296 |
if (dispPtr == NULL) { |
| 1297 |
return; |
| 1298 |
} |
| 1299 |
if (dispPtr->display == eventPtr->xany.display) { |
| 1300 |
break; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
/* |
| 1305 |
* Don't filter motion events if the user |
| 1306 |
* defaulting to true (1), which could be set to false (0) when the |
| 1307 |
* user wishes to receive all the motion data) |
| 1308 |
*/ |
| 1309 |
if (!(dispPtr->flags & TK_DISPLAY_COLLAPSE_MOTION_EVENTS)) { |
| 1310 |
wevPtr = (TkWindowEvent *) ckalloc(sizeof(TkWindowEvent)); |
| 1311 |
wevPtr->header.proc = WindowEventProc; |
| 1312 |
wevPtr->event = *eventPtr; |
| 1313 |
Tcl_QueueEvent(&wevPtr->header, position); |
| 1314 |
return; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (eventPtr->xany.window == PointerWindow && eventPtr->type == ClientMessage) |
| 1318 |
{ |
| 1319 |
/* XDE Drag&Drop does SendEvent to 0 which is "PointerWindow". |
| 1320 |
X does not fill in real Window so do so here |
| 1321 |
The pointer may have moved - but what else can we do ? |
| 1322 |
*/ |
| 1323 |
Window root = DefaultRootWindow(eventPtr->xany.display); |
| 1324 |
Window child = None; |
| 1325 |
int rx, ry, wx, wy, mask; |
| 1326 |
if (!XQueryPointer(eventPtr->xany.display, root, &root, &child, &rx, &ry, &wx, &wy, &mask) || child == None) |
| 1327 |
child = root; |
| 1328 |
while (child != None) |
| 1329 |
{ |
| 1330 |
eventPtr->xany.window = child; |
| 1331 |
XTranslateCoordinates(eventPtr->xany.display, root, child, rx, ry, &wx, &wy, &child); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
if ((dispPtr->delayedMotionPtr != NULL) && (position == TCL_QUEUE_TAIL)) { |
| 1336 |
if ((eventPtr->type == MotionNotify) && (eventPtr->xmotion.window |
| 1337 |
== dispPtr->delayedMotionPtr->event.xmotion.window)) { |
| 1338 |
/* |
| 1339 |
* The new event is a motion event in the same window as the |
| 1340 |
* saved motion event. Just replace the saved event with the |
| 1341 |
* new one. |
| 1342 |
*/ |
| 1343 |
|
| 1344 |
dispPtr->delayedMotionPtr->event = *eventPtr; |
| 1345 |
return; |
| 1346 |
} else if ((eventPtr->type != GraphicsExpose) |
| 1347 |
&& (eventPtr->type != NoExpose) |
| 1348 |
&& (eventPtr->type != Expose)) { |
| 1349 |
/* |
| 1350 |
* The new event may conflict with the saved motion event. Queue |
| 1351 |
* the saved motion event now so that it will be processed before |
| 1352 |
* the new event. |
| 1353 |
*/ |
| 1354 |
|
| 1355 |
Tcl_QueueProcEvent(WindowEventProc, &dispPtr->delayedMotionPtr->header, position); |
| 1356 |
dispPtr->delayedMotionPtr = NULL; |
| 1357 |
Tcl_CancelIdleCall(DelayedMotionProc, (ClientData) dispPtr); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
wevPtr = (TkWindowEvent *) ckalloc(sizeof(TkWindowEvent)); |
| 1362 |
wevPtr->event = *eventPtr; |
| 1363 |
if ((eventPtr->type == MotionNotify) && (position == TCL_QUEUE_TAIL)) { |
| 1364 |
/* |
| 1365 |
* The new event is a motion event so don't queue it immediately; |
| 1366 |
* save it around in case another motion event arrives that it can |
| 1367 |
* be collapsed with. |
| 1368 |
*/ |
| 1369 |
|
| 1370 |
if (dispPtr->delayedMotionPtr != NULL) { |
| 1371 |
panic("Tk_QueueWindowEvent found unexpected delayed motion event"); |
| 1372 |
} |
| 1373 |
dispPtr->delayedMotionPtr = wevPtr; |
| 1374 |
Tcl_DoWhenIdle(DelayedMotionProc, (ClientData) dispPtr); |
| 1375 |
} else { |
| 1376 |
Tcl_QueueProcEvent(WindowEventProc, &wevPtr->header, position); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
/* |
| 1381 |
*--------------------------------------------------------------------------- |
| 1382 |
* |
| 1383 |
* TkQueueEventForAllChildren -- |
| 1384 |
* |
| 1385 |
* Given an XEvent, recursively queue the event for this window and |
| 1386 |
* all non-toplevel children of the given window. |
| 1387 |
* |
| 1388 |
* Results: |
| 1389 |
* None. |
| 1390 |
* |
| 1391 |
* Side effects: |
| 1392 |
* Events queued. |
| 1393 |
* |
| 1394 |
*--------------------------------------------------------------------------- |
| 1395 |
*/ |
| 1396 |
|
| 1397 |
void |
| 1398 |
TkQueueEventForAllChildren(winPtr, eventPtr) |
| 1399 |
TkWindow *winPtr; /* Window to which event is sent. */ |
| 1400 |
XEvent *eventPtr; /* The event to be sent. */ |
| 1401 |
{ |
| 1402 |
TkWindow *childPtr; |
| 1403 |
|
| 1404 |
eventPtr->xany.window = winPtr->window; |
| 1405 |
Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_TAIL); |
| 1406 |
|
| 1407 |
childPtr = winPtr->childList; |
| 1408 |
while (childPtr != NULL) { |
| 1409 |
if (!Tk_TopWinHierarchy(childPtr)) { |
| 1410 |
TkQueueEventForAllChildren(childPtr, eventPtr); |
| 1411 |
} |
| 1412 |
childPtr = childPtr->nextPtr; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
/* |
| 1417 |
*---------------------------------------------------------------------- |
| 1418 |
* |
| 1419 |
* WindowEventProc -- |
| 1420 |
* |
| 1421 |
* This procedure is called by Tcl_DoOneEvent when a window event |
| 1422 |
* reaches the front of the event queue. This procedure is responsible |
| 1423 |
* for actually handling the event. |
| 1424 |
* |
| 1425 |
* Results: |
| 1426 |
* Returns 1 if the event was handled, meaning it should be removed |
| 1427 |
* from the queue. Returns 0 if the event was not handled, meaning |
| 1428 |
* it should stay on the queue. The event isn't handled if the |
| 1429 |
* TCL_WINDOW_EVENTS bit isn't set in flags, if a restrict proc |
| 1430 |
* prevents the event from being handled. |
| 1431 |
* |
| 1432 |
* Side effects: |
| 1433 |
* Whatever the event handlers for the event do. |
| 1434 |
* |
| 1435 |
*---------------------------------------------------------------------- |
| 1436 |
*/ |
| 1437 |
|
| 1438 |
static int |
| 1439 |
WindowEventProc(evPtr, flags) |
| 1440 |
Tcl_Event *evPtr; /* Event to service. */ |
| 1441 |
int flags; /* Flags that indicate what events to |
| 1442 |
* handle, such as TCL_WINDOW_EVENTS. */ |
| 1443 |
{ |
| 1444 |
TkWindowEvent *wevPtr = (TkWindowEvent *) evPtr; |
| 1445 |
Tk_RestrictAction result; |
| 1446 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 1447 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 1448 |
|
| 1449 |
if (!(flags & TCL_WINDOW_EVENTS)) { |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
if (tsdPtr->restrictProc != NULL) { |
| 1453 |
result = (*tsdPtr->restrictProc)(tsdPtr->restrictArg, &wevPtr->event); |
| 1454 |
if (result != TK_PROCESS_EVENT) { |
| 1455 |
if (result == TK_DEFER_EVENT) { |
| 1456 |
/* TK_DEFER_EVENT */ |
| 1457 |
/* WARNING - Beware this happening to SelectionNotify events |
| 1458 |
* DragDrop is broken if they don't happen... |
| 1459 |
*/ |
| 1460 |
return 0; |
| 1461 |
} else { |
| 1462 |
/* |
| 1463 |
* TK_DISCARD_EVENT: return and say we processed the event, |
| 1464 |
* even though we didn't do anything at all. |
| 1465 |
*/ |
| 1466 |
if (result != TK_DISCARD_EVENT) { |
| 1467 |
LangDebug("Bad restrict proc code %d\n", result); |
| 1468 |
return 0; /* DEFER instead */ |
| 1469 |
} |
| 1470 |
return 1; |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
Tk_HandleEvent(&wevPtr->event); |
| 1475 |
return 1; |
| 1476 |
} |
| 1477 |
|
| 1478 |
/* |
| 1479 |
*---------------------------------------------------------------------- |
| 1480 |
* |
| 1481 |
* DelayedMotionProc -- |
| 1482 |
* |
| 1483 |
* This procedure is invoked as an idle handler when a mouse motion |
| 1484 |
* event has been delayed. It queues the delayed event so that it |
| 1485 |
* will finally be serviced. |
| 1486 |
* |
| 1487 |
* Results: |
| 1488 |
* None. |
| 1489 |
* |
| 1490 |
* Side effects: |
| 1491 |
* The delayed mouse motion event gets added to the Tcl event |
| 1492 |
* queue for servicing. |
| 1493 |
* |
| 1494 |
*---------------------------------------------------------------------- |
| 1495 |
*/ |
| 1496 |
|
| 1497 |
static void |
| 1498 |
DelayedMotionProc(clientData) |
| 1499 |
ClientData clientData; /* Pointer to display containing a delayed |
| 1500 |
* motion event to be serviced. */ |
| 1501 |
{ |
| 1502 |
TkDisplay *dispPtr = (TkDisplay *) clientData; |
| 1503 |
|
| 1504 |
if (dispPtr->delayedMotionPtr == NULL) { |
| 1505 |
panic("DelayedMotionProc found no delayed mouse motion event"); |
| 1506 |
} |
| 1507 |
Tcl_QueueProcEvent(WindowEventProc, &dispPtr->delayedMotionPtr->header, TCL_QUEUE_TAIL); |
| 1508 |
dispPtr->delayedMotionPtr = NULL; |
| 1509 |
} |
| 1510 |
|
| 1511 |
/* |
| 1512 |
*-------------------------------------------------------------- |
| 1513 |
* |
| 1514 |
* Tk_MainLoop -- |
| 1515 |
* |
| 1516 |
* Call Tcl_DoOneEvent over and over again in an infinite |
| 1517 |
* loop as long as there exist any main windows. |
| 1518 |
* |
| 1519 |
* Results: |
| 1520 |
* None. |
| 1521 |
* |
| 1522 |
* Side effects: |
| 1523 |
* Arbitrary; depends on handlers for events. |
| 1524 |
* |
| 1525 |
*-------------------------------------------------------------- |
| 1526 |
*/ |
| 1527 |
|
| 1528 |
void |
| 1529 |
Tk_MainLoop() |
| 1530 |
{ |
| 1531 |
while (Tk_GetNumMainWindows() > 0) { |
| 1532 |
Tcl_DoOneEvent(0); |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|