8165232: XKeycodeToKeysym is deprecated and should be replaced
authoralanbur
Thu, 06 Oct 2016 11:39:20 -0700
changeset 41779 fef33230eaf8
parent 41778 13effd4d383b
child 41780 9deca564da25
8165232: XKeycodeToKeysym is deprecated and should be replaced Reviewed-by: prr, azvegint
jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XWindow.c
jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XlibWrapper.c
--- a/jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XWindow.c	Thu Oct 06 20:51:34 2016 +0530
+++ b/jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XWindow.c	Thu Oct 06 11:39:20 2016 -0700
@@ -818,6 +818,32 @@
     }
     return awt_UseXKB;
 }
+
+/*
+ * Map a keycode to the corresponding keysym.
+ * This replaces the deprecated X11 function XKeycodeToKeysym
+ */
+KeySym
+keycodeToKeysym(Display *display, KeyCode keycode, int index) {
+    static int min_kc = -1;
+    static int max_kc;
+    if (min_kc == -1) {
+        (void) XDisplayKeycodes(display, &min_kc, &max_kc);
+    }
+    if (keycode < min_kc || keycode > max_kc || index < 0) {
+        return NoSymbol;
+    }
+    int num_syms;
+    KeySym *key_syms = XGetKeyboardMapping(display, keycode, 1, &num_syms);
+    if (index >= num_syms) {
+        XFree(key_syms);
+        return NoSymbol;
+    }
+    KeySym ks = key_syms[index];
+    XFree(key_syms);
+    return ks;
+}
+
 static Boolean
 isKPevent(XEvent *event)
 {
@@ -833,14 +859,14 @@
      */
     Boolean bsun = isXsunServer( event );
     Boolean bxkb = isXKBenabled( event->xkey.display );
-    return IsKeypadKey( XKeycodeToKeysym(event->xkey.display, event->xkey.keycode,(bsun && !bxkb ? 2 : 1) ) );
+    return IsKeypadKey( keycodeToKeysym(event->xkey.display, event->xkey.keycode,(bsun && !bxkb ? 2 : 1) ) );
 }
 static void
 dumpKeysymArray(XEvent *event) {
-    printf("    0x%X\n",XKeycodeToKeysym(event->xkey.display, event->xkey.keycode, 0));
-    printf("    0x%X\n",XKeycodeToKeysym(event->xkey.display, event->xkey.keycode, 1));
-    printf("    0x%X\n",XKeycodeToKeysym(event->xkey.display, event->xkey.keycode, 2));
-    printf("    0x%X\n",XKeycodeToKeysym(event->xkey.display, event->xkey.keycode, 3));
+    printf("    0x%X\n",keycodeToKeysym(event->xkey.display, event->xkey.keycode, 0));
+    printf("    0x%X\n",keycodeToKeysym(event->xkey.display, event->xkey.keycode, 1));
+    printf("    0x%X\n",keycodeToKeysym(event->xkey.display, event->xkey.keycode, 2));
+    printf("    0x%X\n",keycodeToKeysym(event->xkey.display, event->xkey.keycode, 3));
 }
 /*
  * In a next redesign, get rid of this code altogether.
@@ -855,20 +881,20 @@
     }
     if( isXsunServer( event ) && !awt_UseXKB) {
         if( (event->xkey.state & ShiftMask) ) { // shift modifier is on
-            *keysym = XKeycodeToKeysym(event->xkey.display,
+            *keysym = keycodeToKeysym(event->xkey.display,
                                    event->xkey.keycode, 3);
          }else {
-            *keysym = XKeycodeToKeysym(event->xkey.display,
+            *keysym = keycodeToKeysym(event->xkey.display,
                                    event->xkey.keycode, 2);
          }
     } else {
         if( (event->xkey.state & ShiftMask) || // shift modifier is on
             ((event->xkey.state & LockMask) && // lock modifier is on
              (awt_ModLockIsShiftLock)) ) {     // it is interpreted as ShiftLock
-            *keysym = XKeycodeToKeysym(event->xkey.display,
+            *keysym = keycodeToKeysym(event->xkey.display,
                                    event->xkey.keycode, 0);
         }else{
-            *keysym = XKeycodeToKeysym(event->xkey.display,
+            *keysym = keycodeToKeysym(event->xkey.display,
                                    event->xkey.keycode, 1);
         }
     }
@@ -903,7 +929,7 @@
        Perhaps using the index (modn in awt_MToolkit.c:setup_modifier_map)
        would be more correct.
      */
-    *keysym = XKeycodeToKeysym(event->xkey.display,
+    *keysym = keycodeToKeysym(event->xkey.display,
                                event->xkey.keycode, 2);
     if (originalKeysym != *keysym) {
         DTRACE_PRINTLN3("%s originalKeysym=0x%x, keysym=0x%x",
@@ -999,7 +1025,6 @@
     }
 }
 
-
 /* This function is called as the keyChar parameter of a call to
  * awt_post_java_key_event.  It depends on being called after adjustKeySym.
  *
--- a/jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XlibWrapper.c	Thu Oct 06 20:51:34 2016 +0530
+++ b/jdk/src/java.desktop/unix/native/libawt_xawt/xawt/XlibWrapper.c	Thu Oct 06 11:39:20 2016 -0700
@@ -49,6 +49,9 @@
 
 #include <X11/XKBlib.h>
 
+// From XWindow.c
+extern KeySym keycodeToKeysym(Display *display, KeyCode keycode, int index);
+
 #if defined(DEBUG)
 static jmethodID lockIsHeldMID = NULL;
 
@@ -1286,7 +1289,7 @@
         // report arbitrarily false.
         return JNI_FALSE;
     } else {
-        long ks2 = XKeycodeToKeysym((Display*)jlong_to_ptr(display), kc7, 2);
+        long ks2 = keycodeToKeysym((Display*)jlong_to_ptr(display), kc7, 2);
         if( ks2 == XK_KP_7 ) {
             //XXX If some Xorg server would put XK_KP_7 in keysymarray[2] as well,
             //XXX for yet unknown to me reason, the sniffer would lie.
@@ -1915,12 +1918,13 @@
     XQueryKeymap( (Display *) jlong_to_ptr(display), (char *) jlong_to_ptr(vector));
 }
 
+// XKeycodeToKeysym is deprecated but for compatibility we keep the API.
 JNIEXPORT jlong JNICALL
 Java_sun_awt_X11_XlibWrapper_XKeycodeToKeysym(JNIEnv *env, jclass clazz,
                                               jlong display, jint keycode,
                                               jint index) {
     AWT_CHECK_HAVE_LOCK_RETURN(0);
-    return XKeycodeToKeysym((Display*) jlong_to_ptr(display), (unsigned int)keycode, (int)index);
+    return keycodeToKeysym((Display*)jlong_to_ptr(display), (unsigned int)keycode, (int)index);
 }
 
 JNIEXPORT jint JNICALL