Attachment "xim_cache.patch" to
ticket [1924761f]
added by
dkf
2010-01-02 17:45:57.
Index: ChangeLog
===================================================================
RCS file: /cvsroot/tktoolkit/tk/ChangeLog,v
retrieving revision 1.1895.2.186
diff -u -r1.1895.2.186 ChangeLog
--- ChangeLog 1 Jan 2010 23:03:42 -0000 1.1895.2.186
+++ ChangeLog 2 Jan 2010 10:38:43 -0000
@@ -1,3 +1,21 @@
+2010-01-02 Donal K. Fellows <[email protected]>
+
+ * unix/tkUnixEvent.c (TransferXEventsToTcl): [Bug 1924761]: Use the
+ new cache mechanism to force the extraction of the string of a key
+ event from XIM at the right time rather than after queueing when it
+ can be quashed by a race condition centered on the limited amount of
+ state in some XIM implementations.
+
+ * unix/tkUnixKey.c (TkpGetString): [Bug 1373712]: Cache the value that
+ * generic/tkInt.h (TkKeyEvent): will be substituted via %A so
+ * generic/tkEvent.c (CleanUpTkEvent): that we do not need to make it
+ * doc/HandleEvent.3 (ARGUMENTS): fresh each time, which causes
+ * doc/QWinEvent.3 (ARGUMENTS): trouble with some input
+ * macosx/tkMacOSXKeyEvent.c (InitKeyEvent): methods. Also includes the
+ * win/tkWinX.c (GenerateXEvent): factoring out of some code and
+ update of documentation to describe the slightly increased constraints
+ on how Tk_HandleEvent can be used.
+
2010-01-01 Donal K. Fellows <[email protected]>
* unix/tkUnixEvent.c (TransferXEventsToTcl): [Bug 1924761]: Move the
Index: doc/HandleEvent.3
===================================================================
RCS file: /cvsroot/tktoolkit/tk/doc/HandleEvent.3,v
retrieving revision 1.4
diff -u -r1.4 HandleEvent.3
--- doc/HandleEvent.3 21 Apr 1999 21:53:22 -0000 1.4
+++ doc/HandleEvent.3 2 Jan 2010 10:38:43 -0000
@@ -20,14 +20,15 @@
.SH ARGUMENTS
.AS XEvent *eventPtr
.AP XEvent *eventPtr in
-Pointer to X event to dispatch to relevant handler(s).
+Pointer to X event to dispatch to relevant handler(s). It is important
+that all unused fields of the structure be set to zero.
.BE
.SH DESCRIPTION
.PP
\fBTk_HandleEvent\fR is a lower-level procedure that deals with window
events. It is called by \fBTcl_ServiceEvent\fR (and indirectly by
-\fBTk_DoOneEvent\fR), and in a few other cases within Tk.
+\fBTcl_DoOneEvent\fR), and in a few other cases within Tk.
It makes callbacks to any window event
handlers (created by calls to \fBTk_CreateEventHandler\fR)
that match \fIeventPtr\fR and then returns. In some cases
Index: doc/QWinEvent.3
===================================================================
RCS file: /cvsroot/tktoolkit/tk/doc/QWinEvent.3,v
retrieving revision 1.3
diff -u -r1.3 QWinEvent.3
--- doc/QWinEvent.3 15 Jun 2002 00:21:42 -0000 1.3
+++ doc/QWinEvent.3 2 Jan 2010 10:38:43 -0000
@@ -26,7 +26,8 @@
.AP int collapse in
Indicates whether motion events should be collapsed or not.
.AP XEvent *eventPtr in
-An event to add to the event queue.
+An event to add to the event queue. It is important
+that all unused fields of the structure be set to zero.
.AP Tcl_QueuePosition position in
Where to add the new event in the queue: \fBTCL_QUEUE_TAIL\fR,
\fBTCL_QUEUE_HEAD\fR, or \fBTCL_QUEUE_MARK\fR.
Index: generic/tkEvent.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkEvent.c,v
retrieving revision 1.35.2.3
diff -u -r1.35.2.3 tkEvent.c
--- generic/tkEvent.c 1 Jan 2010 23:03:42 -0000 1.35.2.3
+++ generic/tkEvent.c 2 Jan 2010 10:38:46 -0000
@@ -193,6 +193,7 @@
* Prototypes for functions that are only referenced locally within this file.
*/
+static void CleanUpTkEvent(XEvent *eventPtr);
static void DelayedMotionProc(ClientData clientData);
static int GetButtonMask(unsigned int Button);
static unsigned long GetEventMaskFromXEvent(XEvent *eventPtr);
@@ -1235,7 +1236,7 @@
*/
if (InvokeGenericHandlers(tsdPtr, eventPtr)) {
- goto releaseUserData;
+ goto releaseEventResources;
}
if (RefreshKeyboardMappingIfNeeded(eventPtr)) {
@@ -1243,14 +1244,14 @@
* We are done with a MappingNotify event.
*/
- goto releaseUserData;
+ goto releaseEventResources;
}
mask = GetEventMaskFromXEvent(eventPtr);
winPtr = GetTkWindowFromXEvent(eventPtr);
if (winPtr == NULL) {
- goto releaseUserData;
+ goto releaseEventResources;
}
/*
@@ -1264,7 +1265,7 @@
if ((winPtr->flags & TK_ALREADY_DEAD)
&& (eventPtr->type != DestroyNotify)) {
- goto releaseUserData;
+ goto releaseEventResources;
}
if (winPtr->mainPtr != NULL) {
@@ -1388,18 +1389,11 @@
* Release the user_data from the event (if it is a virtual event and the
* field was non-NULL in the first place.) Note that this is done using a
* Tcl_Obj interface, and we set the field back to NULL afterwards out of
- * paranoia.
+ * paranoia. Also clean up any cached %A substitutions from key events.
*/
- releaseUserData:
- if (eventPtr->type == VirtualEvent) {
- XVirtualEvent *vePtr = (XVirtualEvent *) eventPtr;
-
- if (vePtr->user_data != NULL) {
- Tcl_DecrRefCount(vePtr->user_data);
- vePtr->user_data = NULL;
- }
- }
+ releaseEventResources:
+ CleanUpTkEvent(eventPtr);
}
/*
@@ -1761,17 +1755,67 @@
* even though we didn't do anything at all.
*/
+ CleanUpTkEvent(&wevPtr->event);
return 1;
}
}
}
Tk_HandleEvent(&wevPtr->event);
+ CleanUpTkEvent(&wevPtr->event);
return 1;
}
/*
*----------------------------------------------------------------------
*
+ * CleanUpTkEvent --
+ *
+ * This function is called to remove and deallocate any information in
+ * the event which is not directly in the event structure itself. It may
+ * be called multiple times per event, so it takes care to set the
+ * cleared pointer fields to NULL afterwards.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * Makes the event no longer have any external resources.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+CleanUpTkEvent(
+ XEvent *eventPtr)
+{
+ switch (eventPtr->type) {
+ case KeyPress:
+ case KeyRelease: {
+ TkKeyEvent *kePtr = (TkKeyEvent *) eventPtr;
+
+ if (kePtr->charValuePtr != NULL) {
+ ckfree(kePtr->charValuePtr);
+ kePtr->charValuePtr = NULL;
+ kePtr->charValueLen = 0;
+ }
+ break;
+ }
+
+ case VirtualEvent: {
+ XVirtualEvent *vePtr = (XVirtualEvent *) eventPtr;
+
+ if (vePtr->user_data != NULL) {
+ Tcl_DecrRefCount(vePtr->user_data);
+ vePtr->user_data = NULL;
+ }
+ break;
+ }
+ }
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
* DelayedMotionProc --
*
* This function is invoked as an idle handler when a mouse motion event
Index: generic/tkInt.h
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkInt.h,v
retrieving revision 1.82.2.5
diff -u -r1.82.2.5 tkInt.h
--- generic/tkInt.h 1 Jan 2010 23:03:42 -0000 1.82.2.5
+++ generic/tkInt.h 2 Jan 2010 10:38:46 -0000
@@ -853,6 +853,22 @@
} TkWindow;
/*
+ * Real definition of some events. Note that these events come from outside
+ * but have internally generated pieces added to them.
+ */
+
+typedef struct {
+ XKeyEvent keyEvent; /* The real event from X11. */
+ char *charValuePtr; /* A pointer to a string that holds the key's
+ * %A substitution text (before backslash
+ * adding), or NULL if that has not been
+ * computed yet. If non-NULL, this string was
+ * allocated with ckalloc(). */
+ int charValueLen; /* Length of string in charValuePtr when that
+ * is non-NULL. */
+} TkKeyEvent;
+
+/*
* The following structure is used as a two way map between integers and
* strings, usually to map between an internal C representation and the
* strings used in Tcl.
@@ -1113,9 +1129,8 @@
int objc, Tcl_Obj *const objv[]);
MODULE_SCOPE int Tk_GetDoublePixelsFromObj(Tcl_Interp *interp,
- Tk_Window tkwin,
- Tcl_Obj *objPtr,
- double *doublePtr);
+ Tk_Window tkwin, Tcl_Obj *objPtr,
+ double *doublePtr);
MODULE_SCOPE void TkEventInit(void);
MODULE_SCOPE void TkRegisterObjTypes(void);
Index: macosx/tkMacOSXKeyEvent.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/macosx/tkMacOSXKeyEvent.c,v
retrieving revision 1.24.2.1
diff -u -r1.24.2.1 tkMacOSXKeyEvent.c
--- macosx/tkMacOSXKeyEvent.c 2 Sep 2008 16:14:18 -0000 1.24.2.1
+++ macosx/tkMacOSXKeyEvent.c 2 Jan 2010 10:38:46 -0000
@@ -496,6 +496,7 @@
return -1;
}
+ memset(eventPtr, 0, sizeof(XEvent));
eventPtr->xany.send_event = false;
eventPtr->xany.serial = Tk_Display(tkwin)->request;
Index: unix/tkUnixEvent.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/unix/tkUnixEvent.c,v
retrieving revision 1.27.2.2
diff -u -r1.27.2.2 tkUnixEvent.c
--- unix/tkUnixEvent.c 2 Jan 2010 00:00:20 -0000 1.27.2.2
+++ unix/tkUnixEvent.c 2 Jan 2010 10:38:47 -0000
@@ -278,8 +278,13 @@
TransferXEventsToTcl(
Display *display)
{
- XEvent event;
+ union {
+ int type;
+ XEvent x;
+ TkKeyEvent k;
+ } event;
Window w;
+ TkDisplay *dispPtr = NULL;
/*
* Transfer events from the X event queue to the Tk event queue after XIM
@@ -293,15 +298,13 @@
*/
while (QLength(display) > 0) {
- XNextEvent(display, &event);
+ XNextEvent(display, &event.x);
w = None;
if (event.type == KeyPress || event.type == KeyRelease) {
- TkDisplay *dispPtr;
-
for (dispPtr = TkGetDisplayList(); ; dispPtr = dispPtr->nextPtr) {
if (dispPtr == NULL) {
break;
- } else if (dispPtr->display == event.xany.display) {
+ } else if (dispPtr->display == event.x.xany.display) {
if (dispPtr->focusPtr != NULL) {
w = dispPtr->focusPtr->window;
}
@@ -309,10 +312,34 @@
}
}
}
- if (XFilterEvent(&event, w)) {
+ if (XFilterEvent(&event.x, w)) {
continue;
}
- Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
+ if (event.type == KeyPress || event.type == KeyRelease) {
+ event.k.charValuePtr = NULL;
+ event.k.charValueLen = 0;
+
+ /*
+ * Force the calling of the input method engine now. The results
+ * from it will be cached in the event so that they don't get lost
+ * (to a race condition with other XIM-handled key events) between
+ * entering the event queue and getting serviced. [Bug 1924761]
+ */
+
+#ifdef TK_USE_INPUT_METHODS
+ if (event.type == KeyPress && dispPtr &&
+ (dispPtr->flags & TK_DISPLAY_USE_IM)) {
+ if (dispPtr->focusPtr && dispPtr->focusPtr->inputContext) {
+ Tcl_DString ds;
+
+ Tcl_DStringInit(&ds);
+ (void) TkpGetString(dispPtr->focusPtr, &event.x, &ds);
+ Tcl_DStringFree(&ds);
+ }
+ }
+#endif
+ }
+ Tk_QueueWindowEvent(&event.x, TCL_QUEUE_TAIL);
}
}
Index: unix/tkUnixKey.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/unix/tkUnixKey.c,v
retrieving revision 1.13.2.1
diff -u -r1.13.2.1 tkUnixKey.c
--- unix/tkUnixKey.c 11 Jun 2008 00:41:25 -0000 1.13.2.1
+++ unix/tkUnixKey.c 2 Jan 2010 10:38:47 -0000
@@ -78,48 +78,6 @@
/*
*----------------------------------------------------------------------
*
- * TkpGetChar --
- *
- * Convert a keyboard event to a UTF-8 string using XLookupString.
- *
- * This is used as a fallback instead of Xutf8LookupString
- * or XmbLookupString if input methods are turned off
- * and for KeyRelease events.
- *
- * Notes:
- * XLookupString() normally returns a single ISO Latin 1
- * or ASCII control character.
- *
- *----------------------------------------------------------------------
- */
-static char *
-TkpGetChar(
- XEvent *eventPtr, /* KeyPress or KeyRelease event */
- Tcl_DString *dsPtr) /* Initialized, empty string to hold result. */
-{
- int len;
- char buf[TCL_DSTRING_STATIC_SIZE];
-
- len = XLookupString(&eventPtr->xkey, buf, TCL_DSTRING_STATIC_SIZE, 0, 0);
- buf[len] = '\0';
-
- if (len == 1) {
- len = Tcl_UniCharToUtf((unsigned char)buf[0], Tcl_DStringValue(dsPtr));
- Tcl_DStringSetLength(dsPtr, len);
- } else {
- /*
- * len > 1 should only happen if someone has called XRebindKeysym().
- * Assume UTF-8.
- */
- Tcl_DStringSetLength(dsPtr, len);
- strncpy(Tcl_DStringValue(dsPtr), buf, len);
- }
- return Tcl_DStringValue(dsPtr);
-}
-
-/*
- *----------------------------------------------------------------------
- *
* TkpGetString --
*
* Retrieve the UTF string associated with a keyboard event.
@@ -140,16 +98,30 @@
TkWindow *winPtr, /* Window where event occurred */
XEvent *eventPtr, /* X keyboard event. */
Tcl_DString *dsPtr) /* Initialized, empty string to hold result. */
-#ifdef TK_USE_INPUT_METHODS
-#if X_HAVE_UTF8_STRING
{
+ int len;
+ Tcl_DString buf;
+ TkKeyEvent *kePtr = (TkKeyEvent *) eventPtr;
+
+ /*
+ * If we have the value cached already, use it now. [Bug 1373712]
+ */
+
+ if (kePtr->charValuePtr != NULL) {
+ Tcl_DStringSetLength(dsPtr, kePtr->charValueLen);
+ memcpy(Tcl_DStringValue(dsPtr), kePtr->charValuePtr,
+ (unsigned) kePtr->charValueLen+1);
+ return Tcl_DStringValue(dsPtr);
+ }
+
+#ifdef TK_USE_INPUT_METHODS
if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)
&& (winPtr->inputContext != NULL)
&& (eventPtr->type == KeyPress))
{
- int len;
Status status;
+#if X_HAVE_UTF8_STRING
Tcl_DStringSetLength(dsPtr, TCL_DSTRING_STATIC_SIZE-1);
len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey,
Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr),
@@ -162,32 +134,16 @@
NULL, &status);
}
if ((status != XLookupChars) && (status != XLookupBoth)) {
- Tcl_DStringSetLength(dsPtr, 0);
len = 0;
}
Tcl_DStringSetLength(dsPtr, len);
-
- return Tcl_DStringValue(dsPtr);
- } else {
- return TkpGetChar(eventPtr, dsPtr);
- }
-}
#else /* !X_HAVE_UTF8_STRING */
-{
- int len;
- Tcl_DString buf;
- Status status;
-
- /*
- * Overallocate the dstring to the maximum stack amount.
- */
+ /*
+ * Overallocate the dstring to the maximum stack amount.
+ */
- Tcl_DStringInit(&buf);
- Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
-
- if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)
- && (winPtr->inputContext != NULL)
- && (eventPtr->type == KeyPress)) {
+ Tcl_DStringInit(&buf);
+ Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
len = XmbLookupString(winPtr->inputContext, &eventPtr->xkey,
Tcl_DStringValue(&buf), Tcl_DStringLength(&buf), NULL,
@@ -205,24 +161,55 @@
if ((status != XLookupChars) && (status != XLookupBoth)) {
len = 0;
}
- } else {
- return TkpGetChar(eventPtr, dsPtr);
+
+ Tcl_DStringSetLength(&buf, len);
+ Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr);
+ Tcl_DStringFree(&buf);
+#endif /* X_HAVE_UTF8_STRING */
+ } else
+#endif /* TK_USE_INPUT_METHODS */
+ {
+ /*
+ * Fall back to convert a keyboard event to a UTF-8 string using
+ * XLookupString. This is used when input methods are turned off and
+ * for KeyRelease events.
+ *
+ * Note: XLookupString() normally returns a single ISO Latin 1 or
+ * ASCII control character.
+ */
+
+ Tcl_DStringInit(&buf);
+ Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
+ len = XLookupString(&eventPtr->xkey, Tcl_DStringValue(&buf),
+ TCL_DSTRING_STATIC_SIZE, 0, 0);
+ Tcl_DStringValue(&buf)[len] = '\0';
+
+ if (len == 1) {
+ len = Tcl_UniCharToUtf((unsigned char) Tcl_DStringValue(&buf)[0],
+ Tcl_DStringValue(dsPtr));
+ Tcl_DStringSetLength(dsPtr, len);
+ } else {
+ /*
+ * len > 1 should only happen if someone has called XRebindKeysym.
+ * Assume UTF-8.
+ */
+
+ Tcl_DStringSetLength(dsPtr, len);
+ strncpy(Tcl_DStringValue(dsPtr), Tcl_DStringValue(&buf), len);
+ }
}
- Tcl_DStringSetLength(&buf, len);
- Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr);
- Tcl_DStringFree(&buf);
+ /*
+ * Cache the string in the event so that if/when we return to this
+ * function, we will be able to produce it without asking X. This stops us
+ * from having to reenter the XIM engine. [Bug 1373712]
+ */
+ kePtr->charValuePtr = ckalloc((unsigned) len + 1);
+ kePtr->charValueLen = len;
+ memcpy(kePtr->charValuePtr, Tcl_DStringValue(dsPtr), (unsigned) len + 1);
return Tcl_DStringValue(dsPtr);
}
-
-#endif /* X_HAVE_UTF8_STRING */
-#else /* !TK_USE_INPUT_METHODS */
-{
- return TkpGetChar(eventPtr, dsPtr);
-}
-#endif
-
/*
* When mapping from a keysym to a keycode, need information about the
Index: win/tkWinX.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinX.c,v
retrieving revision 1.57
diff -u -r1.57 tkWinX.c
--- win/tkWinX.c 13 Dec 2007 15:28:56 -0000 1.57
+++ win/tkWinX.c 2 Jan 2010 10:38:48 -0000
@@ -1010,6 +1010,7 @@
return;
}
+ memset(&event, 0, sizeof(XEvent));
event.xany.serial = winPtr->display->request++;
event.xany.send_event = False;
event.xany.display = winPtr->display;