| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <X11/Intrinsic.h> |
| 16 |
#include <tclInt.h> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
typedef struct FileHandler { |
| 24 |
int fd; |
| 25 |
int mask; |
| 26 |
int readyMask; |
| 27 |
|
| 28 |
|
| 29 |
XtInputId read; |
| 30 |
XtInputId write; |
| 31 |
XtInputId except; |
| 32 |
Tcl_FileProc *proc; |
| 33 |
|
| 34 |
ClientData clientData; |
| 35 |
struct FileHandler *nextPtr; |
| 36 |
} FileHandler; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
typedef struct FileHandlerEvent { |
| 44 |
Tcl_Event header; |
| 45 |
|
| 46 |
int fd; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
} FileHandlerEvent; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static struct NotifierState { |
| 59 |
XtAppContext appContext; |
| 60 |
|
| 61 |
|
| 62 |
int appContextCreated; |
| 63 |
XtIntervalId currentTimeout; |
| 64 |
FileHandler *firstFileHandlerPtr; |
| 65 |
|
| 66 |
} notifier; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static int initialized = 0; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static int FileHandlerEventProc _ANSI_ARGS_((Tcl_Event *evPtr, |
| 79 |
int flags)); |
| 80 |
static void FileProc _ANSI_ARGS_((caddr_t clientData, |
| 81 |
int *source, XtInputId *id)); |
| 82 |
void InitNotifier _ANSI_ARGS_((void)); |
| 83 |
static void NotifierExitHandler _ANSI_ARGS_(( |
| 84 |
ClientData clientData)); |
| 85 |
static void TimerProc _ANSI_ARGS_((caddr_t clientData, |
| 86 |
XtIntervalId *id)); |
| 87 |
static void CreateFileHandler _ANSI_ARGS_((int fd, int mask, |
| 88 |
Tcl_FileProc * proc, ClientData clientData)); |
| 89 |
static void DeleteFileHandler _ANSI_ARGS_((int fd)); |
| 90 |
static void SetTimer _ANSI_ARGS_((Tcl_Time * timePtr)); |
| 91 |
static int WaitForEvent _ANSI_ARGS_((Tcl_Time * timePtr)); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
EXTERN XtAppContext TclSetAppContext _ANSI_ARGS_((XtAppContext ctx)); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
XtAppContext |
| 117 |
TclSetAppContext(appContext) |
| 118 |
XtAppContext appContext; |
| 119 |
{ |
| 120 |
if (!initialized) { |
| 121 |
InitNotifier(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if (notifier.appContext != NULL) { |
| 131 |
if (appContext != NULL) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
panic("TclSetAppContext: multiple application contexts"); |
| 139 |
|
| 140 |
} |
| 141 |
} else { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (appContext == NULL) { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
notifier.appContext = XtCreateApplicationContext(); |
| 156 |
notifier.appContextCreated = 1; |
| 157 |
} else { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
notifier.appContextCreated = 0; |
| 165 |
notifier.appContext = appContext; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return notifier.appContext; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
void |
| 189 |
InitNotifier() |
| 190 |
{ |
| 191 |
Tcl_NotifierProcs notifier; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if (TclInExit()) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
notifier.createFileHandlerProc = CreateFileHandler; |
| 203 |
notifier.deleteFileHandlerProc = DeleteFileHandler; |
| 204 |
notifier.setTimerProc = SetTimer; |
| 205 |
notifier.waitForEventProc = WaitForEvent; |
| 206 |
Tcl_SetNotifier(¬ifier); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
initialized = 1; |
| 214 |
memset(¬ifier, 0, sizeof(notifier)); |
| 215 |
Tcl_CreateExitHandler(NotifierExitHandler, NULL); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static void |
| 236 |
NotifierExitHandler( |
| 237 |
ClientData clientData) |
| 238 |
{ |
| 239 |
if (notifier.currentTimeout != 0) { |
| 240 |
XtRemoveTimeOut(notifier.currentTimeout); |
| 241 |
} |
| 242 |
for (; notifier.firstFileHandlerPtr != NULL; ) { |
| 243 |
Tcl_DeleteFileHandler(notifier.firstFileHandlerPtr->fd); |
| 244 |
} |
| 245 |
if (notifier.appContextCreated) { |
| 246 |
XtDestroyApplicationContext(notifier.appContext); |
| 247 |
notifier.appContextCreated = 0; |
| 248 |
notifier.appContext = NULL; |
| 249 |
} |
| 250 |
initialized = 0; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static void |
| 270 |
SetTimer(timePtr) |
| 271 |
Tcl_Time *timePtr; |
| 272 |
{ |
| 273 |
long timeout; |
| 274 |
|
| 275 |
if (!initialized) { |
| 276 |
InitNotifier(); |
| 277 |
} |
| 278 |
|
| 279 |
TclSetAppContext(NULL); |
| 280 |
if (notifier.currentTimeout != 0) { |
| 281 |
XtRemoveTimeOut(notifier.currentTimeout); |
| 282 |
} |
| 283 |
if (timePtr) { |
| 284 |
timeout = timePtr->sec * 1000 + timePtr->usec / 1000; |
| 285 |
notifier.currentTimeout = |
| 286 |
XtAppAddTimeOut(notifier.appContext, (unsigned long) timeout, |
| 287 |
TimerProc, NULL); |
| 288 |
} else { |
| 289 |
notifier.currentTimeout = 0; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
static void |
| 311 |
TimerProc(data, id) |
| 312 |
caddr_t data; |
| 313 |
XtIntervalId *id; |
| 314 |
{ |
| 315 |
if (*id != notifier.currentTimeout) { |
| 316 |
return; |
| 317 |
} |
| 318 |
notifier.currentTimeout = 0; |
| 319 |
|
| 320 |
Tcl_ServiceAll(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
static void |
| 341 |
CreateFileHandler(fd, mask, proc, clientData) |
| 342 |
int fd; |
| 343 |
int mask; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
Tcl_FileProc *proc; |
| 348 |
|
| 349 |
ClientData clientData; |
| 350 |
{ |
| 351 |
FileHandler *filePtr; |
| 352 |
|
| 353 |
if (!initialized) { |
| 354 |
InitNotifier(); |
| 355 |
} |
| 356 |
|
| 357 |
TclSetAppContext(NULL); |
| 358 |
|
| 359 |
for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL; |
| 360 |
filePtr = filePtr->nextPtr) { |
| 361 |
if (filePtr->fd == fd) { |
| 362 |
break; |
| 363 |
} |
| 364 |
} |
| 365 |
if (filePtr == NULL) { |
| 366 |
filePtr = (FileHandler*) ckalloc(sizeof(FileHandler)); |
| 367 |
filePtr->fd = fd; |
| 368 |
filePtr->read = 0; |
| 369 |
filePtr->write = 0; |
| 370 |
filePtr->except = 0; |
| 371 |
filePtr->readyMask = 0; |
| 372 |
filePtr->mask = 0; |
| 373 |
filePtr->nextPtr = notifier.firstFileHandlerPtr; |
| 374 |
notifier.firstFileHandlerPtr = filePtr; |
| 375 |
} |
| 376 |
filePtr->proc = proc; |
| 377 |
filePtr->clientData = clientData; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
if (mask & TCL_READABLE) { |
| 384 |
if (!(filePtr->mask & TCL_READABLE)) { |
| 385 |
filePtr->read = |
| 386 |
XtAppAddInput(notifier.appContext, fd, XtInputReadMask, |
| 387 |
FileProc, filePtr); |
| 388 |
} |
| 389 |
} else { |
| 390 |
if (filePtr->mask & TCL_READABLE) { |
| 391 |
XtRemoveInput(filePtr->read); |
| 392 |
} |
| 393 |
} |
| 394 |
if (mask & TCL_WRITABLE) { |
| 395 |
if (!(filePtr->mask & TCL_WRITABLE)) { |
| 396 |
filePtr->write = |
| 397 |
XtAppAddInput(notifier.appContext, fd, XtInputWriteMask, |
| 398 |
FileProc, filePtr); |
| 399 |
} |
| 400 |
} else { |
| 401 |
if (filePtr->mask & TCL_WRITABLE) { |
| 402 |
XtRemoveInput(filePtr->write); |
| 403 |
} |
| 404 |
} |
| 405 |
if (mask & TCL_EXCEPTION) { |
| 406 |
if (!(filePtr->mask & TCL_EXCEPTION)) { |
| 407 |
filePtr->except = |
| 408 |
XtAppAddInput(notifier.appContext, fd, XtInputExceptMask, |
| 409 |
FileProc, filePtr); |
| 410 |
} |
| 411 |
} else { |
| 412 |
if (filePtr->mask & TCL_EXCEPTION) { |
| 413 |
XtRemoveInput(filePtr->except); |
| 414 |
} |
| 415 |
} |
| 416 |
filePtr->mask = mask; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
static void |
| 437 |
DeleteFileHandler(fd) |
| 438 |
int fd; |
| 439 |
|
| 440 |
{ |
| 441 |
FileHandler *filePtr, *prevPtr; |
| 442 |
|
| 443 |
if (!initialized) { |
| 444 |
InitNotifier(); |
| 445 |
} |
| 446 |
|
| 447 |
TclSetAppContext(NULL); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
for (prevPtr = NULL, filePtr = notifier.firstFileHandlerPtr; ; |
| 455 |
prevPtr = filePtr, filePtr = filePtr->nextPtr) { |
| 456 |
if (filePtr == NULL) { |
| 457 |
return; |
| 458 |
} |
| 459 |
if (filePtr->fd == fd) { |
| 460 |
break; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
if (prevPtr == NULL) { |
| 469 |
notifier.firstFileHandlerPtr = filePtr->nextPtr; |
| 470 |
} else { |
| 471 |
prevPtr->nextPtr = filePtr->nextPtr; |
| 472 |
} |
| 473 |
if (filePtr->mask & TCL_READABLE) { |
| 474 |
XtRemoveInput(filePtr->read); |
| 475 |
} |
| 476 |
if (filePtr->mask & TCL_WRITABLE) { |
| 477 |
XtRemoveInput(filePtr->write); |
| 478 |
} |
| 479 |
if (filePtr->mask & TCL_EXCEPTION) { |
| 480 |
XtRemoveInput(filePtr->except); |
| 481 |
} |
| 482 |
ckfree((char *) filePtr); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static void |
| 504 |
FileProc(clientData, fd, id) |
| 505 |
caddr_t clientData; |
| 506 |
int *fd; |
| 507 |
XtInputId *id; |
| 508 |
{ |
| 509 |
FileHandler *filePtr = (FileHandler *)clientData; |
| 510 |
FileHandlerEvent *fileEvPtr; |
| 511 |
int mask = 0; |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
if (*id == filePtr->read) { |
| 518 |
mask = TCL_READABLE; |
| 519 |
} else if (*id == filePtr->write) { |
| 520 |
mask = TCL_WRITABLE; |
| 521 |
} else if (*id == filePtr->except) { |
| 522 |
mask = TCL_EXCEPTION; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
if (!(filePtr->mask & mask) || (filePtr->readyMask & mask)) { |
| 530 |
return; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
filePtr->readyMask |= mask; |
| 538 |
fileEvPtr = (FileHandlerEvent *) ckalloc(sizeof(FileHandlerEvent)); |
| 539 |
fileEvPtr->header.proc = FileHandlerEventProc; |
| 540 |
fileEvPtr->fd = filePtr->fd; |
| 541 |
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL); |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
Tcl_ServiceAll(); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static int |
| 573 |
FileHandlerEventProc(evPtr, flags) |
| 574 |
Tcl_Event *evPtr; |
| 575 |
int flags; |
| 576 |
|
| 577 |
{ |
| 578 |
FileHandler *filePtr; |
| 579 |
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr; |
| 580 |
int mask; |
| 581 |
|
| 582 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 583 |
return 0; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL; |
| 594 |
filePtr = filePtr->nextPtr) { |
| 595 |
if (filePtr->fd != fileEvPtr->fd) { |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
mask = filePtr->readyMask & filePtr->mask; |
| 612 |
filePtr->readyMask = 0; |
| 613 |
if (mask != 0) { |
| 614 |
(*filePtr->proc)(filePtr->clientData, mask); |
| 615 |
} |
| 616 |
break; |
| 617 |
} |
| 618 |
return 1; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
static int |
| 642 |
WaitForEvent( |
| 643 |
Tcl_Time *timePtr) |
| 644 |
{ |
| 645 |
int timeout; |
| 646 |
|
| 647 |
if (!initialized) { |
| 648 |
InitNotifier(); |
| 649 |
} |
| 650 |
|
| 651 |
TclSetAppContext(NULL); |
| 652 |
|
| 653 |
if (timePtr) { |
| 654 |
timeout = timePtr->sec * 1000 + timePtr->usec / 1000; |
| 655 |
if (timeout == 0) { |
| 656 |
if (XtAppPending(notifier.appContext)) { |
| 657 |
goto process; |
| 658 |
} else { |
| 659 |
return 0; |
| 660 |
} |
| 661 |
} else { |
| 662 |
Tcl_SetTimer(timePtr); |
| 663 |
} |
| 664 |
} |
| 665 |
process: |
| 666 |
XtAppProcessEvent(notifier.appContext, XtIMAll); |
| 667 |
return 1; |
| 668 |
} |