--- old/src/java.desktop/unix/classes/sun/awt/X11/XMSelection.java 2018-06-20 20:42:32.093865937 +0900 +++ new/src/java.desktop/unix/classes/sun/awt/X11/XMSelection.java 2018-06-20 20:42:31.726865928 +0900 @@ -97,7 +97,15 @@ XToolkit.awtLock(); try { long root = XlibWrapper.RootWindow(display,screen); - XlibWrapper.XSelectInput(display, root, XConstants.StructureNotifyMask); + XWindowAttributes wattr = new XWindowAttributes(); + try{ + XlibWrapper.XGetWindowAttributes(display, root, wattr.pData); + XlibWrapper.XSelectInput(display, root, + XConstants.StructureNotifyMask | + wattr.get_your_event_mask()); + } finally { + wattr.dispose(); + } XToolkit.addEventDispatcher(root, new XEventDispatcher() { public void dispatchEvent(XEvent ev) { --- old/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java 2018-06-20 20:42:32.594865949 +0900 +++ new/src/java.desktop/unix/classes/sun/awt/X11InputMethod.java 2018-06-20 20:42:32.235865940 +0900 @@ -70,6 +70,7 @@ * Activate input method. */ public synchronized void activate() { + activatedInstance = this; clientComponentWindow = getClientComponentWindow(); if (clientComponentWindow == null) return; @@ -140,6 +141,7 @@ Last note, getCompositionState should be called before setXICFocus since setXICFocus here sets the XIC to 0. */ + activatedInstance = null; savedCompositionState = getCompositionState(); if (isTemporary) { --- old/src/java.desktop/unix/classes/sun/awt/X11InputMethodBase.java 2018-06-20 20:42:33.078865960 +0900 +++ new/src/java.desktop/unix/classes/sun/awt/X11InputMethodBase.java 2018-06-20 20:42:32.728865952 +0900 @@ -124,6 +124,9 @@ // this structure needs to be accessed within AWT_LOCK/UNLOCK protected transient long pData = 0; // accessed by native + // To keep the activated instance for IM resuming + protected static X11InputMethod activatedInstance = null; + // Initialize highlight mapping table static { @SuppressWarnings({"unchecked", "rawtypes"}) --- old/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c 2018-06-20 20:42:33.569865972 +0900 +++ new/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c 2018-06-20 20:42:33.221865964 +0900 @@ -1260,25 +1260,73 @@ } static void OpenXIMCallback(Display *display, XPointer client_data, XPointer call_data) { + JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); XIMCallback ximCallback; + jclass classClass; + jfieldID pID, lastComponentID; + jobject cdata; + AWT_LOCK(); X11im = XOpenIM(display, NULL, NULL, NULL); if (X11im == NULL) { + AWT_UNLOCK(); return; } +#if defined(__linux__) || defined(MACOSX) + classClass = (*env)->FindClass(env, "sun/awt/X11InputMethod"); + if (!JNU_IsNull(env, classClass)) { + pID = (*env)->GetStaticFieldID(env, classClass, + "activatedInstance", "Lsun/awt/X11InputMethod;"); + lastComponentID = (*env)->GetFieldID(env, classClass, + "lastXICFocussedComponent", "Ljava/awt/Component;"); + if (!JNU_IsNull(env, pID) && !JNU_IsNull(env, lastComponentID)) { + cdata = (*env)->GetStaticObjectField(env, classClass, pID); + if (!JNU_IsNull(env, cdata)) { + // Claer lastXICFocussedComponent before calling activate. + (*env)->SetObjectField(env, cdata, lastComponentID, NULL); + JNU_CallMethodByName(env, NULL, cdata, "activate", "()V"); + if ((*env)->ExceptionOccurred(env)) { + (*env)->ExceptionClear(env); + } + } + } + } +#endif + ximCallback.callback = (XIMProc)DestroyXIMCallback; ximCallback.client_data = NULL; XSetIMValues(X11im, XNDestroyCallback, &ximCallback, NULL); +#if defined(__linux__) || defined(MACOSX) + XUnregisterIMInstantiateCallback(display, NULL, NULL, NULL, + OpenXIMCallback, NULL); +#endif + AWT_UNLOCK(); } static void DestroyXIMCallback(XIM im, XPointer client_data, XPointer call_data) { + JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); + + AWT_LOCK(); /* mark that XIM server was destroyed */ X11im = NULL; - JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); - /* free the old pX11IMData and set it to null. this also avoids crashing - * the jvm if the XIM server reappears */ - X11InputMethodData *pX11IMData = getX11InputMethodData(env, currentX11InputMethodInstance); + + X11InputMethodGRefNode *pX11InputMethodGRef = x11InputMethodGRefListHead; + while (pX11InputMethodGRef != NULL) { + X11InputMethodGRefNode *next = pX11InputMethodGRef->next; + /* free the old pX11IMData and set it to null. this also avoids + * crashing the jvm if the XIM server reappears */ + getX11InputMethodData(env, pX11InputMethodGRef->inputMethodGRef); + pX11InputMethodGRef = next; + } + currentX11InputMethodInstance = NULL; + currentFocusWindow = 0; + +#if defined(__linux__) || defined(MACOSX) + XRegisterIMInstantiateCallback(dpy, NULL, NULL, NULL, + (XIDProc)OpenXIMCallback, NULL); +#endif + AWT_UNLOCK(); } JNIEXPORT jboolean JNICALL @@ -1344,16 +1392,16 @@ pX11IMData->lookup_buf = 0; pX11IMData->lookup_buf_len = 0; + setX11InputMethodData(env, this, pX11IMData); if (createXIC(env, pX11IMData, (Window)window) == False) { destroyX11InputMethodData((JNIEnv *) NULL, pX11IMData); pX11IMData = (X11InputMethodData *) NULL; + setX11InputMethodData(env, this, pX11IMData); if ((*env)->ExceptionCheck(env)) { goto finally; } } - setX11InputMethodData(env, this, pX11IMData); - finally: AWT_UNLOCK(); return (pX11IMData != NULL);