| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "tkInt.h" |
| 16 |
#include "tkUnixInt.h" |
| 17 |
#include <sys/time.h> |
| 18 |
#include <signal.h> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
typedef struct ThreadSpecificData { |
| 26 |
int initialized; |
| 27 |
} ThreadSpecificData; |
| 28 |
static Tcl_ThreadDataKey dataKey; |
| 29 |
|
| 30 |
#if defined(TK_USE_INPUT_METHODS) && defined(PEEK_XCLOSEIM) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
struct XIMPeek { |
| 36 |
void *junk1, *junk2; |
| 37 |
XIC ic_chain; |
| 38 |
}; |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static void DisplayCheckProc _ANSI_ARGS_((ClientData clientData, |
| 46 |
int flags)); |
| 47 |
static void DisplayExitHandler _ANSI_ARGS_(( |
| 48 |
ClientData clientData)); |
| 49 |
static void DisplayFileProc _ANSI_ARGS_((ClientData clientData, |
| 50 |
int flags)); |
| 51 |
static void DisplaySetupProc _ANSI_ARGS_((ClientData clientData, |
| 52 |
int flags)); |
| 53 |
static void TransferXEventsToTcl _ANSI_ARGS_((Display *display)); |
| 54 |
#ifdef TK_USE_INPUT_METHODS |
| 55 |
static void OpenIM _ANSI_ARGS_((TkDisplay *dispPtr)); |
| 56 |
#endif |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void |
| 77 |
TkCreateXEventSource() |
| 78 |
{ |
| 79 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 80 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 81 |
|
| 82 |
if (!tsdPtr->initialized) { |
| 83 |
tsdPtr->initialized = 1; |
| 84 |
Tcl_CreateEventSource(DisplaySetupProc, DisplayCheckProc, NULL); |
| 85 |
Tcl_CreateExitHandler(DisplayExitHandler, NULL); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static void |
| 107 |
DisplayExitHandler(clientData) |
| 108 |
ClientData clientData; |
| 109 |
{ |
| 110 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 111 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 112 |
|
| 113 |
Tcl_DeleteEventSource(DisplaySetupProc, DisplayCheckProc, NULL); |
| 114 |
tsdPtr->initialized = 0; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
TkDisplay * |
| 135 |
TkpOpenDisplay(display_name) |
| 136 |
CONST char *display_name; |
| 137 |
{ |
| 138 |
TkDisplay *dispPtr; |
| 139 |
Display *display = XOpenDisplay(display_name); |
| 140 |
|
| 141 |
if (display == NULL) { |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
dispPtr = (TkDisplay *) ckalloc(sizeof(TkDisplay)); |
| 145 |
memset(dispPtr, 0, sizeof(TkDisplay)); |
| 146 |
dispPtr->display = display; |
| 147 |
#ifdef TK_USE_INPUT_METHODS |
| 148 |
OpenIM(dispPtr); |
| 149 |
#endif |
| 150 |
Tcl_CreateFileHandler(ConnectionNumber(display), TCL_READABLE, |
| 151 |
DisplayFileProc, (ClientData) dispPtr); |
| 152 |
return dispPtr; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
void |
| 172 |
TkpCloseDisplay(dispPtr) |
| 173 |
TkDisplay *dispPtr; |
| 174 |
{ |
| 175 |
TkSendCleanup(dispPtr); |
| 176 |
|
| 177 |
TkFreeXId(dispPtr); |
| 178 |
|
| 179 |
TkWmCleanup(dispPtr); |
| 180 |
|
| 181 |
#ifdef TK_USE_INPUT_METHODS |
| 182 |
#if TK_XIM_SPOT |
| 183 |
if (dispPtr->inputXfs) { |
| 184 |
XFreeFontSet(dispPtr->display, dispPtr->inputXfs); |
| 185 |
} |
| 186 |
#endif |
| 187 |
if (dispPtr->inputMethod) { |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#if defined(TK_USE_INPUT_METHODS) && defined(PEEK_XCLOSEIM) |
| 205 |
int do_peek = 0; |
| 206 |
struct XIMPeek *peek; |
| 207 |
|
| 208 |
if (strstr(ServerVendor(dispPtr->display), "XFree86")) { |
| 209 |
int vendrel = VendorRelease(dispPtr->display); |
| 210 |
if (vendrel < 336) { |
| 211 |
|
| 212 |
do_peek = 1; |
| 213 |
} else if (vendrel < 3900) { |
| 214 |
|
| 215 |
do_peek = 1; |
| 216 |
} else if (vendrel < 40000000) { |
| 217 |
|
| 218 |
do_peek = 1; |
| 219 |
} else { |
| 220 |
|
| 221 |
do_peek = 0; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if (do_peek) { |
| 226 |
peek = (struct XIMPeek *) dispPtr->inputMethod; |
| 227 |
if (peek->ic_chain != NULL) |
| 228 |
panic("input contexts not freed before XCloseIM"); |
| 229 |
} |
| 230 |
#endif |
| 231 |
XCloseIM(dispPtr->inputMethod); |
| 232 |
} |
| 233 |
#endif |
| 234 |
|
| 235 |
if (dispPtr->display != 0) { |
| 236 |
Tcl_DeleteFileHandler(ConnectionNumber(dispPtr->display)); |
| 237 |
(void) XSync(dispPtr->display, False); |
| 238 |
(void) XCloseDisplay(dispPtr->display); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static void |
| 263 |
DisplaySetupProc(clientData, flags) |
| 264 |
ClientData clientData; |
| 265 |
int flags; |
| 266 |
{ |
| 267 |
TkDisplay *dispPtr; |
| 268 |
static Tcl_Time blockTime = { 0, 0 }; |
| 269 |
|
| 270 |
if (!(flags & TCL_WINDOW_EVENTS)) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
for (dispPtr = TkGetDisplayList(); dispPtr != NULL; |
| 275 |
dispPtr = dispPtr->nextPtr) { |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
XFlush(dispPtr->display); |
| 285 |
if (QLength(dispPtr->display) > 0) { |
| 286 |
Tcl_SetMaxBlockTime(&blockTime); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
static void |
| 308 |
TransferXEventsToTcl( |
| 309 |
Display *display) |
| 310 |
{ |
| 311 |
XEvent event; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
while (QLength(display) > 0) { |
| 321 |
XNextEvent(display, &event); |
| 322 |
if (event.type != KeyPress && event.type != KeyRelease) { |
| 323 |
if (XFilterEvent(&event, None)) { |
| 324 |
continue; |
| 325 |
} |
| 326 |
} |
| 327 |
Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static void |
| 349 |
DisplayCheckProc(clientData, flags) |
| 350 |
ClientData clientData; |
| 351 |
int flags; |
| 352 |
{ |
| 353 |
TkDisplay *dispPtr; |
| 354 |
|
| 355 |
if (!(flags & TCL_WINDOW_EVENTS)) { |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
for (dispPtr = TkGetDisplayList(); dispPtr != NULL; |
| 360 |
dispPtr = dispPtr->nextPtr) { |
| 361 |
XFlush(dispPtr->display); |
| 362 |
TransferXEventsToTcl(dispPtr->display); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
static void |
| 384 |
DisplayFileProc(clientData, flags) |
| 385 |
ClientData clientData; |
| 386 |
int flags; |
| 387 |
{ |
| 388 |
TkDisplay *dispPtr = (TkDisplay *) clientData; |
| 389 |
Display *display = dispPtr->display; |
| 390 |
int numFound; |
| 391 |
|
| 392 |
XFlush(display); |
| 393 |
numFound = XEventsQueued(display, QueuedAfterReading); |
| 394 |
if (numFound == 0) { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| •419 |
__sighandler_t oldHandler; |
| 420 |
|
| •421 |
oldHandler = signal(SIGPIPE, SIG_IGN); |
| 422 |
XNoOp(display); |
| 423 |
XFlush(display); |
| 424 |
(void) signal(SIGPIPE, oldHandler); |
| 425 |
} |
| 426 |
|
| 427 |
TransferXEventsToTcl(display); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
int |
| 453 |
TkUnixDoOneXEvent(timePtr) |
| 454 |
Tcl_Time *timePtr; |
| 455 |
|
| 456 |
{ |
| 457 |
TkDisplay *dispPtr; |
| 458 |
static fd_mask readMask[MASK_SIZE]; |
| 459 |
struct timeval blockTime, *timeoutPtr; |
| 460 |
Tcl_Time now; |
| 461 |
int fd, index, numFound, numFdBits = 0; |
| 462 |
fd_mask bit; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
if (Tcl_ServiceEvent(TCL_WINDOW_EVENTS)) { |
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if (timePtr) { |
| 479 |
Tcl_GetTime(&now); |
| 480 |
blockTime.tv_sec = timePtr->sec; |
| 481 |
blockTime.tv_usec = timePtr->usec - now.usec; |
| 482 |
if (blockTime.tv_usec < 0) { |
| 483 |
now.sec += 1; |
| 484 |
blockTime.tv_usec += 1000000; |
| 485 |
} |
| 486 |
if (blockTime.tv_sec < now.sec) { |
| 487 |
blockTime.tv_sec = 0; |
| 488 |
blockTime.tv_usec = 0; |
| 489 |
} else { |
| 490 |
blockTime.tv_sec -= now.sec; |
| 491 |
} |
| 492 |
timeoutPtr = &blockTime; |
| 493 |
} else { |
| 494 |
timeoutPtr = NULL; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
memset((VOID *) readMask, 0, MASK_SIZE*sizeof(fd_mask)); |
| 503 |
for (dispPtr = TkGetDisplayList(); dispPtr != NULL; |
| 504 |
dispPtr = dispPtr->nextPtr) { |
| 505 |
XFlush(dispPtr->display); |
| 506 |
if (QLength(dispPtr->display) > 0) { |
| 507 |
blockTime.tv_sec = 0; |
| 508 |
blockTime.tv_usec = 0; |
| 509 |
} |
| 510 |
fd = ConnectionNumber(dispPtr->display); |
| 511 |
index = fd/(NBBY*sizeof(fd_mask)); |
| 512 |
bit = ((fd_mask)1) << (fd%(NBBY*sizeof(fd_mask))); |
| 513 |
readMask[index] |= bit; |
| 514 |
if (numFdBits <= fd) { |
| 515 |
numFdBits = fd+1; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
numFound = select(numFdBits, (SELECT_MASK *) &readMask[0], NULL, NULL, |
| 520 |
timeoutPtr); |
| 521 |
if (numFound <= 0) { |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
memset((VOID *) readMask, 0, MASK_SIZE*sizeof(fd_mask)); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
for (dispPtr = TkGetDisplayList(); dispPtr != NULL; |
| 535 |
dispPtr = dispPtr->nextPtr) { |
| 536 |
fd = ConnectionNumber(dispPtr->display); |
| 537 |
index = fd/(NBBY*sizeof(fd_mask)); |
| 538 |
bit = ((fd_mask)1) << (fd%(NBBY*sizeof(fd_mask))); |
| 539 |
if ((readMask[index] & bit) || (QLength(dispPtr->display) > 0)) { |
| 540 |
DisplayFileProc((ClientData)dispPtr, TCL_READABLE); |
| 541 |
} |
| 542 |
} |
| 543 |
if (Tcl_ServiceEvent(TCL_WINDOW_EVENTS)) { |
| 544 |
return 1; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
if (timePtr) { |
| 552 |
Tcl_GetTime(&now); |
| 553 |
if ((now.sec > timePtr->sec) || ((now.sec == timePtr->sec) |
| 554 |
&& (now.usec > timePtr->usec))) { |
| 555 |
return 0; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
return 1; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
void |
| 586 |
TkpSync(display) |
| 587 |
Display *display; |
| 588 |
{ |
| 589 |
XSync(display, False); |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
TransferXEventsToTcl(display); |
| 595 |
} |
| 596 |
#ifdef TK_USE_INPUT_METHODS |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
static void |
| 618 |
OpenIM(dispPtr) |
| 619 |
TkDisplay *dispPtr; |
| 620 |
{ |
| 621 |
unsigned short i; |
| 622 |
XIMStyles *stylePtr; |
| 623 |
|
| 624 |
if (XSetLocaleModifiers("") == NULL) { |
| 625 |
goto error; |
| 626 |
} |
| 627 |
|
| 628 |
dispPtr->inputMethod = XOpenIM(dispPtr->display, NULL, NULL, NULL); |
| 629 |
if (dispPtr->inputMethod == NULL) { |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
if ((XGetIMValues(dispPtr->inputMethod, XNQueryInputStyle, &stylePtr, |
| 634 |
NULL) != NULL) || (stylePtr == NULL)) { |
| 635 |
goto error; |
| 636 |
} |
| 637 |
#if TK_XIM_SPOT |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
for (i = 0; i < stylePtr->count_styles; i++) { |
| 643 |
if (stylePtr->supported_styles[i] |
| 644 |
== (XIMPreeditPosition | XIMStatusNothing)) { |
| 645 |
dispPtr->flags |= TK_DISPLAY_XIM_SPOT; |
| 646 |
XFree(stylePtr); |
| 647 |
return; |
| 648 |
} |
| 649 |
} |
| 650 |
#endif |
| 651 |
for (i = 0; i < stylePtr->count_styles; i++) { |
| 652 |
if (stylePtr->supported_styles[i] |
| 653 |
== (XIMPreeditNothing | XIMStatusNothing)) { |
| 654 |
XFree(stylePtr); |
| 655 |
return; |
| 656 |
} |
| 657 |
} |
| 658 |
XFree(stylePtr); |
| 659 |
|
| 660 |
error: |
| 661 |
|
| 662 |
if (dispPtr->inputMethod) { |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
XCloseIM(dispPtr->inputMethod); |
| 668 |
dispPtr->inputMethod = NULL; |
| 669 |
} |
| 670 |
} |
| 671 |
#endif |
| 672 |
|