Debian Codegraph @

pTk/mTk/unix/tkUnixEvent.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * tkUnixEvent.c --
3 *
4 * This file implements an event source for X displays for the
5 * UNIX version of Tk.
6 *
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkUnixEvent.c,v 1.11.2.1 2003/07/16 01:20:13 mdejong Exp $
13 */
14
15 #include "tkInt.h"
16 #include "tkUnixInt.h"
17 #include <sys/time.h>
18 #include <signal.h>
19
20 /*
21 * The following static indicates whether this module has been initialized
22 * in the current thread.
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 * Structure used to peek into internal XIM data structure.
33 * This is only known to work with XFree86.
34 */
35 struct XIMPeek {
36 void *junk1, *junk2;
37 XIC ic_chain;
38 };
39 #endif
40
41 /*
42 * Prototypes for procedures that are referenced only in this file:
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 * TkCreateXEventSource --
63 *
64 * This procedure is called during Tk initialization to create
65 * the event source for X Window events.
66 *
67 * Results:
68 * None.
69 *
70 * Side effects:
71 * A new event source is created.
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 * DisplayExitHandler --
93 *
94 * This function is called during finalization to clean up the
95 * display module.
96 *
97 * Results:
98 * None.
99 *
100 * Side effects:
101 * None.
102 *
103 *----------------------------------------------------------------------
104 */
105
106 static void
107 DisplayExitHandler(clientData)
108 ClientData clientData; /* Not used. */
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 * TkpOpenDisplay --
121 *
122 * Allocates a new TkDisplay, opens the X display, and establishes
123 * the file handler for the connection.
124 *
125 * Results:
126 * A pointer to a Tk display structure.
127 *
128 * Side effects:
129 * Opens a display.
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 * TkpCloseDisplay --
159 *
160 * Cancels notifier callbacks and closes a display.
161 *
162 * Results:
163 * None.
164 *
165 * Side effects:
166 * Deallocates the displayPtr and unix-specific resources.
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 * Calling XCloseIM with an input context that has not
190 * been freed can cause a crash. This crash has been
191 * reproduced under Linux systems with XFree86 3.3
192 * and may have also been seen under Solaris 2.3.
193 * The crash is caused by a double free of memory
194 * inside the X library. Memory that was already
195 * deallocated may be accessed again inside XCloseIM.
196 * This bug can be avoided by making sure that a
197 * call to XDestroyIC is made for each XCreateIC call.
198 * This bug has been fixed in XFree86 4.2.99.2.
199 * The internal layout of the XIM structure changed
200 * in the XFree86 4.2 release so the test should
201 * not be run for with these new releases.
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 /* 3.3.4 and 3.3.5 */
212 do_peek = 1;
213 } else if (vendrel < 3900) {
214 /* Other 3.3.x versions */
215 do_peek = 1;
216 } else if (vendrel < 40000000) {
217 /* 4.0.x versions */
218 do_peek = 1;
219 } else {
220 /* Newer than 4.0 */
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 * DisplaySetupProc --
246 *
247 * This procedure implements the setup part of the UNIX X display
248 * event source. It is invoked by Tcl_DoOneEvent before entering
249 * the notifier to check for events on all displays.
250 *
251 * Results:
252 * None.
253 *
254 * Side effects:
255 * If data is queued on a display inside Xlib, then the maximum
256 * block time will be set to 0 to ensure that the notifier returns
257 * control to Tcl even if there is no more data on the X connection.
258 *
259 *----------------------------------------------------------------------
260 */
261
262 static void
263 DisplaySetupProc(clientData, flags)
264 ClientData clientData; /* Not used. */
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 * Flush the display. If data is pending on the X queue, set
279 * the block time to zero. This ensures that we won't block
280 * in the notifier if there is data in the X queue, but not on
281 * the server socket.
282 */
283
284 XFlush(dispPtr->display);
285 if (QLength(dispPtr->display) > 0) {
286 Tcl_SetMaxBlockTime(&blockTime);
287 }
288 }
289 }
290
291 /*
292 *----------------------------------------------------------------------
293 *
294 * TransferXEventsToTcl
295 *
296 * Transfer events from the X event queue to the Tk event queue.
297 *
298 * Results:
299 * None.
300 *
301 * Side effects:
302 * Moves queued X events onto the Tcl event queue.
303 *
304 *----------------------------------------------------------------------
305 */
306
307 static void
308 TransferXEventsToTcl(
309 Display *display)
310 {
311 XEvent event;
312
313 /*
314 * Transfer events from the X event queue to the Tk event queue after XIM
315 * event filtering. KeyPress and KeyRelease events are filtered in
316 * Tk_HandleEvent instead of here, so that Tk's focus management code can
317 * redirect them.
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 * DisplayCheckProc --
335 *
336 * This procedure checks for events sitting in the X event
337 * queue.
338 *
339 * Results:
340 * None.
341 *
342 * Side effects:
343 * Moves queued events onto the Tcl event queue.
344 *
345 *----------------------------------------------------------------------
346 */
347
348 static void
349 DisplayCheckProc(clientData, flags)
350 ClientData clientData; /* Not used. */
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 * DisplayFileProc --
370 *
371 * This procedure implements the file handler for the X connection.
372 *
373 * Results:
374 * None.
375 *
376 * Side effects:
377 * Makes entries on the Tcl event queue for all the events available
378 * from all the displays.
379 *
380 *----------------------------------------------------------------------
381 */
382
383 static void
384 DisplayFileProc(clientData, flags)
385 ClientData clientData; /* The display pointer. */
386 int flags; /* Should be TCL_READABLE. */
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 * Things are very tricky if there aren't any events readable
398 * at this point (after all, there was supposedly data
399 * available on the connection). A couple of things could
400 * have occurred:
401 *
402 * One possibility is that there were only error events in the
403 * input from the server. If this happens, we should return
404 * (we don't want to go to sleep in XNextEvent below, since
405 * this would block out other sources of input to the
406 * process).
407 *
408 * Another possibility is that our connection to the server
409 * has been closed. This will not necessarily be detected in
410 * XEventsQueued (!!), so if we just return then there will be
411 * an infinite loop. To detect such an error, generate a NoOp
412 * protocol request to exercise the connection to the server,
413 * then return. However, must disable SIGPIPE while sending
414 * the request, or else the process will die from the signal
415 * and won't invoke the X error function to print a nice (?!)
416 * message.
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 * TkUnixDoOneXEvent --
434 *
435 * This routine waits for an X event to be processed or for
436 * a timeout to occur. The timeout is specified as an absolute
437 * time. This routine is called when Tk needs to wait for a
438 * particular X event without letting arbitrary events be
439 * processed. The caller will typically call Tk_RestrictEvents
440 * to set up an event filter before calling this routine. This
441 * routine will service at most one event per invocation.
442 *
443 * Results:
444 * Returns 0 if the timeout has expired, otherwise returns 1.
445 *
446 * Side effects:
447 * Can invoke arbitrary Tcl scripts.
448 *
449 *----------------------------------------------------------------------
450 */
451
452 int
453 TkUnixDoOneXEvent(timePtr)
454 Tcl_Time *timePtr; /* Specifies the absolute time when the
455 * call should time out. */
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 * Look for queued events first.
466 */
467
468 if (Tcl_ServiceEvent(TCL_WINDOW_EVENTS)) {
469 return 1;
470 }
471
472 /*
473 * Compute the next block time and check to see if we have timed out.
474 * Note that HP-UX defines tv_sec to be unsigned so we have to be
475 * careful in our arithmetic.
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 * Set up the select mask for all of the displays. If a display has
499 * data pending, then we want to poll instead of blocking.
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 * Some systems don't clear the masks after an error, so
524 * we have to do it here.
525 */
526
527 memset((VOID *) readMask, 0, MASK_SIZE*sizeof(fd_mask));
528 }
529
530 /*
531 * Process any new events on the display connections.
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 * Check to see if we timed out.
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 * We had an event but we did not generate a Tcl event from it. Behave
561 * as though we dealt with it. (JYL&SS)
562 */
563
564 return 1;
565 }
566
567 /*
568 *----------------------------------------------------------------------
569 *
570 * TkpSync --
571 *
572 * This routine ensures that all pending X requests have been
573 * seen by the server, and that any pending X events have been
574 * moved onto the Tk event queue.
575 *
576 * Results:
577 * None.
578 *
579 * Side effects:
580 * Places new events on the Tk event queue.
581 *
582 *----------------------------------------------------------------------
583 */
584
585 void
586 TkpSync(display)
587 Display *display; /* Display to sync. */
588 {
589 XSync(display, False);
590
591 /*
592 * Transfer events from the X event queue to the Tk event queue.
593 */
594 TransferXEventsToTcl(display);
595 }
596 #ifdef TK_USE_INPUT_METHODS
597
598 /*
599 *--------------------------------------------------------------
600 *
601 * OpenIM --
602 *
603 * Tries to open an X input method, associated with the
604 * given display. Right now we can only deal with a bare-bones
605 * input style: no preedit, and no status.
606 *
607 * Results:
608 * Stores the input method in dispPtr->inputMethod; if there isn't
609 * a suitable input method, then NULL is stored in dispPtr->inputMethod.
610 *
611 * Side effects:
612 * An input method gets opened.
613 *
614 *--------------------------------------------------------------
615 */
616
617 static void
618 OpenIM(dispPtr)
619 TkDisplay *dispPtr; /* Tk's structure for the display. */
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 * If we want to do over-the-spot XIM, we have to check that this
640 * mode is supported. If not we will fall-through to the check below.
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 * This call should not suffer from any core dumping problems
665 * since we have not allocated any input contexts.
666 */
667 XCloseIM(dispPtr->inputMethod);
668 dispPtr->inputMethod = NULL;
669 }
670 }
671 #endif /* TK_USE_INPUT_METHODS */
672