1 /*
   2  * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* Note that the contents of this file were taken from canvas.c
  27  * in the old motif-based AWT.
  28  */
  29 
  30 #ifdef HEADLESS
  31     #error This file should not be included in headless library
  32 #endif
  33 
  34 #include <X11/Xlib.h>
  35 #include <X11/Xutil.h>
  36 #include <X11/Xos.h>
  37 #include <X11/Xatom.h>
  38 #include <ctype.h>
  39 
  40 #include <jvm.h>
  41 #include <jni.h>
  42 #include <jlong.h>
  43 #include <jni_util.h>
  44 
  45 #include "sun_awt_X11_XWindow.h"
  46 
  47 #include "awt_p.h"
  48 #include "awt_GraphicsEnv.h"
  49 
  50 #define XK_KATAKANA
  51 #include <X11/keysym.h>     /* standard X keysyms */
  52 #include <X11/DECkeysym.h>  /* DEC vendor-specific */
  53 #include <X11/Sunkeysym.h>  /* Sun vendor-specific */
  54 #include <X11/ap_keysym.h>  /* Apollo (HP) vendor-specific */
  55 /*
  56  * HPKeysym.h is used not only for the hp keysyms, but also to
  57  * give us the osf keysyms that are also defined in HPkeysym.h.
  58  * However, HPkeysym.h is missing a couple of osf keysyms,
  59  * so I have #defined them below.
  60  */
  61 #include <X11/HPkeysym.h>   /* HP vendor-specific */
  62 
  63 #include "java_awt_event_KeyEvent.h"
  64 #include "java_awt_event_InputEvent.h"
  65 #include "java_awt_event_MouseEvent.h"
  66 #include "java_awt_event_MouseWheelEvent.h"
  67 #include "java_awt_AWTEvent.h"
  68 
  69 /*
  70  * Two osf keys are not defined in standard keysym.h,
  71  * /Xm/VirtKeys.h, or HPkeysym.h, so I added them below.
  72  * I found them in /usr/openwin/lib/X11/XKeysymDB
  73  */
  74 #ifndef osfXK_Prior
  75 #define osfXK_Prior 0x1004FF55
  76 #endif
  77 #ifndef osfXK_Next
  78 #define osfXK_Next 0x1004FF56
  79 #endif
  80 
  81 jfieldID windowID;
  82 jfieldID drawStateID;
  83 jfieldID targetID;
  84 jfieldID graphicsConfigID;
  85 
  86 extern jobject currentX11InputMethodInstance;
  87 extern Boolean awt_x11inputmethod_lookupString(XKeyPressedEvent *, KeySym *);
  88 Boolean awt_UseType4Patch = False;
  89 Boolean awt_ServerDetected = False;
  90 Boolean awt_XKBDetected = False;
  91 Boolean awt_IsXsun = False;
  92 Boolean awt_UseXKB = False;
  93 
  94 typedef struct KEYMAP_ENTRY {
  95     jint awtKey;
  96     KeySym x11Key;
  97     Boolean mapsToUnicodeChar;
  98     jint keyLocation;
  99 } KeymapEntry;
 100 
 101 /* NB: XK_R? keysyms are for Type 4 keyboards.
 102  * The corresponding XK_F? keysyms are for Type 5
 103  *
 104  * Note: this table must be kept in sorted order, since it is traversed
 105  * according to both Java keycode and X keysym.  There are a number of
 106  * keycodes that map to more than one corresponding keysym, and we need
 107  * to choose the right one.  Unfortunately, there are some keysyms that
 108  * can map to more than one keycode, depending on what kind of keyboard
 109  * is in use (e.g. F11 and F12).
 110  */
 111 
 112 KeymapEntry keymapTable[] =
 113 {
 114     {java_awt_event_KeyEvent_VK_A, XK_a, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 115     {java_awt_event_KeyEvent_VK_B, XK_b, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 116     {java_awt_event_KeyEvent_VK_C, XK_c, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 117     {java_awt_event_KeyEvent_VK_D, XK_d, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 118     {java_awt_event_KeyEvent_VK_E, XK_e, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 119     {java_awt_event_KeyEvent_VK_F, XK_f, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 120     {java_awt_event_KeyEvent_VK_G, XK_g, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 121     {java_awt_event_KeyEvent_VK_H, XK_h, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 122     {java_awt_event_KeyEvent_VK_I, XK_i, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 123     {java_awt_event_KeyEvent_VK_J, XK_j, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 124     {java_awt_event_KeyEvent_VK_K, XK_k, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 125     {java_awt_event_KeyEvent_VK_L, XK_l, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 126     {java_awt_event_KeyEvent_VK_M, XK_m, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 127     {java_awt_event_KeyEvent_VK_N, XK_n, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 128     {java_awt_event_KeyEvent_VK_O, XK_o, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 129     {java_awt_event_KeyEvent_VK_P, XK_p, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 130     {java_awt_event_KeyEvent_VK_Q, XK_q, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 131     {java_awt_event_KeyEvent_VK_R, XK_r, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 132     {java_awt_event_KeyEvent_VK_S, XK_s, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 133     {java_awt_event_KeyEvent_VK_T, XK_t, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 134     {java_awt_event_KeyEvent_VK_U, XK_u, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 135     {java_awt_event_KeyEvent_VK_V, XK_v, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 136     {java_awt_event_KeyEvent_VK_W, XK_w, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 137     {java_awt_event_KeyEvent_VK_X, XK_x, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 138     {java_awt_event_KeyEvent_VK_Y, XK_y, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 139     {java_awt_event_KeyEvent_VK_Z, XK_z, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 140 
 141     /* TTY Function keys */
 142     {java_awt_event_KeyEvent_VK_BACK_SPACE, XK_BackSpace, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 143     {java_awt_event_KeyEvent_VK_TAB, XK_Tab, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 144     {java_awt_event_KeyEvent_VK_TAB, XK_ISO_Left_Tab, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 145     {java_awt_event_KeyEvent_VK_CLEAR, XK_Clear, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 146     {java_awt_event_KeyEvent_VK_ENTER, XK_Return, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 147     {java_awt_event_KeyEvent_VK_ENTER, XK_Linefeed, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 148     {java_awt_event_KeyEvent_VK_PAUSE, XK_Pause, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 149     {java_awt_event_KeyEvent_VK_PAUSE, XK_F21, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 150     {java_awt_event_KeyEvent_VK_PAUSE, XK_R1, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 151     {java_awt_event_KeyEvent_VK_SCROLL_LOCK, XK_Scroll_Lock, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 152     {java_awt_event_KeyEvent_VK_SCROLL_LOCK, XK_F23, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 153     {java_awt_event_KeyEvent_VK_SCROLL_LOCK, XK_R3, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 154     {java_awt_event_KeyEvent_VK_ESCAPE, XK_Escape, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 155 
 156     /* Other vendor-specific versions of TTY Function keys */
 157     {java_awt_event_KeyEvent_VK_BACK_SPACE, osfXK_BackSpace, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 158     {java_awt_event_KeyEvent_VK_CLEAR, osfXK_Clear, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 159     {java_awt_event_KeyEvent_VK_ESCAPE, osfXK_Escape, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 160 
 161     /* Modifier keys */
 162     {java_awt_event_KeyEvent_VK_SHIFT, XK_Shift_L, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_LEFT},
 163     {java_awt_event_KeyEvent_VK_SHIFT, XK_Shift_R, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_RIGHT},
 164     {java_awt_event_KeyEvent_VK_CONTROL, XK_Control_L, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_LEFT},
 165     {java_awt_event_KeyEvent_VK_CONTROL, XK_Control_R, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_RIGHT},
 166     {java_awt_event_KeyEvent_VK_ALT, XK_Alt_L, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_LEFT},
 167     {java_awt_event_KeyEvent_VK_ALT, XK_Alt_R, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_RIGHT},
 168     {java_awt_event_KeyEvent_VK_META, XK_Meta_L, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_LEFT},
 169     {java_awt_event_KeyEvent_VK_META, XK_Meta_R, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_RIGHT},
 170     {java_awt_event_KeyEvent_VK_CAPS_LOCK, XK_Caps_Lock, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 171     {java_awt_event_KeyEvent_VK_CAPS_LOCK, XK_Shift_Lock, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 172 
 173     /* Misc Functions */
 174     {java_awt_event_KeyEvent_VK_PRINTSCREEN, XK_Print, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 175     {java_awt_event_KeyEvent_VK_PRINTSCREEN, XK_F22, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 176     {java_awt_event_KeyEvent_VK_PRINTSCREEN, XK_R2, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 177     {java_awt_event_KeyEvent_VK_CANCEL, XK_Cancel, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 178     {java_awt_event_KeyEvent_VK_HELP, XK_Help, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 179     {java_awt_event_KeyEvent_VK_NUM_LOCK, XK_Num_Lock, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 180 
 181     /* Other vendor-specific versions of Misc Functions */
 182     {java_awt_event_KeyEvent_VK_CANCEL, osfXK_Cancel, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 183     {java_awt_event_KeyEvent_VK_HELP, osfXK_Help, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 184 
 185     /* Rectangular Navigation Block */
 186     {java_awt_event_KeyEvent_VK_HOME, XK_Home, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 187     {java_awt_event_KeyEvent_VK_HOME, XK_R7, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 188     {java_awt_event_KeyEvent_VK_PAGE_UP, XK_Page_Up, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 189     {java_awt_event_KeyEvent_VK_PAGE_UP, XK_Prior, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 190     {java_awt_event_KeyEvent_VK_PAGE_UP, XK_R9, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 191     {java_awt_event_KeyEvent_VK_PAGE_DOWN, XK_Page_Down, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 192     {java_awt_event_KeyEvent_VK_PAGE_DOWN, XK_Next, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 193     {java_awt_event_KeyEvent_VK_PAGE_DOWN, XK_R15, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 194     {java_awt_event_KeyEvent_VK_END, XK_End, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 195     {java_awt_event_KeyEvent_VK_END, XK_R13, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 196     {java_awt_event_KeyEvent_VK_INSERT, XK_Insert, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 197     {java_awt_event_KeyEvent_VK_DELETE, XK_Delete, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 198 
 199     /* Keypad equivalents of Rectangular Navigation Block */
 200     {java_awt_event_KeyEvent_VK_HOME, XK_KP_Home, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 201     {java_awt_event_KeyEvent_VK_PAGE_UP, XK_KP_Page_Up, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 202     {java_awt_event_KeyEvent_VK_PAGE_UP, XK_KP_Prior, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 203     {java_awt_event_KeyEvent_VK_PAGE_DOWN, XK_KP_Page_Down, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 204     {java_awt_event_KeyEvent_VK_PAGE_DOWN, XK_KP_Next, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 205     {java_awt_event_KeyEvent_VK_END, XK_KP_End, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 206     {java_awt_event_KeyEvent_VK_INSERT, XK_KP_Insert, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 207     {java_awt_event_KeyEvent_VK_DELETE, XK_KP_Delete, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 208 
 209     /* Other vendor-specific Rectangular Navigation Block */
 210     {java_awt_event_KeyEvent_VK_PAGE_UP, osfXK_PageUp, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 211     {java_awt_event_KeyEvent_VK_PAGE_UP, osfXK_Prior, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 212     {java_awt_event_KeyEvent_VK_PAGE_DOWN, osfXK_PageDown, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 213     {java_awt_event_KeyEvent_VK_PAGE_DOWN, osfXK_Next, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 214     {java_awt_event_KeyEvent_VK_END, osfXK_EndLine, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 215     {java_awt_event_KeyEvent_VK_INSERT, osfXK_Insert, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 216     {java_awt_event_KeyEvent_VK_DELETE, osfXK_Delete, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 217 
 218     /* Triangular Navigation Block */
 219     {java_awt_event_KeyEvent_VK_LEFT, XK_Left, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 220     {java_awt_event_KeyEvent_VK_UP, XK_Up, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 221     {java_awt_event_KeyEvent_VK_RIGHT, XK_Right, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 222     {java_awt_event_KeyEvent_VK_DOWN, XK_Down, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 223 
 224     /* Keypad equivalents of Triangular Navigation Block */
 225     {java_awt_event_KeyEvent_VK_KP_LEFT, XK_KP_Left, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 226     {java_awt_event_KeyEvent_VK_KP_UP, XK_KP_Up, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 227     {java_awt_event_KeyEvent_VK_KP_RIGHT, XK_KP_Right, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 228     {java_awt_event_KeyEvent_VK_KP_DOWN, XK_KP_Down, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 229 
 230     /* Other vendor-specific Triangular Navigation Block */
 231     {java_awt_event_KeyEvent_VK_LEFT, osfXK_Left, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 232     {java_awt_event_KeyEvent_VK_UP, osfXK_Up, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 233     {java_awt_event_KeyEvent_VK_RIGHT, osfXK_Right, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 234     {java_awt_event_KeyEvent_VK_DOWN, osfXK_Down, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 235 
 236     /* Remaining Cursor control & motion */
 237     {java_awt_event_KeyEvent_VK_BEGIN, XK_Begin, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 238     {java_awt_event_KeyEvent_VK_BEGIN, XK_KP_Begin, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 239 
 240     {java_awt_event_KeyEvent_VK_0, XK_0, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 241     {java_awt_event_KeyEvent_VK_1, XK_1, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 242     {java_awt_event_KeyEvent_VK_2, XK_2, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 243     {java_awt_event_KeyEvent_VK_3, XK_3, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 244     {java_awt_event_KeyEvent_VK_4, XK_4, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 245     {java_awt_event_KeyEvent_VK_5, XK_5, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 246     {java_awt_event_KeyEvent_VK_6, XK_6, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 247     {java_awt_event_KeyEvent_VK_7, XK_7, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 248     {java_awt_event_KeyEvent_VK_8, XK_8, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 249     {java_awt_event_KeyEvent_VK_9, XK_9, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 250 
 251     {java_awt_event_KeyEvent_VK_SPACE, XK_space, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 252     {java_awt_event_KeyEvent_VK_EXCLAMATION_MARK, XK_exclam, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 253     {java_awt_event_KeyEvent_VK_QUOTEDBL, XK_quotedbl, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 254     {java_awt_event_KeyEvent_VK_NUMBER_SIGN, XK_numbersign, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 255     {java_awt_event_KeyEvent_VK_DOLLAR, XK_dollar, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 256     {java_awt_event_KeyEvent_VK_AMPERSAND, XK_ampersand, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 257     {java_awt_event_KeyEvent_VK_QUOTE, XK_apostrophe, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 258     {java_awt_event_KeyEvent_VK_LEFT_PARENTHESIS, XK_parenleft, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 259     {java_awt_event_KeyEvent_VK_RIGHT_PARENTHESIS, XK_parenright, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 260     {java_awt_event_KeyEvent_VK_ASTERISK, XK_asterisk, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 261     {java_awt_event_KeyEvent_VK_PLUS, XK_plus, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 262     {java_awt_event_KeyEvent_VK_COMMA, XK_comma, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 263     {java_awt_event_KeyEvent_VK_MINUS, XK_minus, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 264     {java_awt_event_KeyEvent_VK_PERIOD, XK_period, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 265     {java_awt_event_KeyEvent_VK_SLASH, XK_slash, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 266 
 267     {java_awt_event_KeyEvent_VK_COLON, XK_colon, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 268     {java_awt_event_KeyEvent_VK_SEMICOLON, XK_semicolon, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 269     {java_awt_event_KeyEvent_VK_LESS, XK_less, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 270     {java_awt_event_KeyEvent_VK_EQUALS, XK_equal, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 271     {java_awt_event_KeyEvent_VK_GREATER, XK_greater, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 272 
 273     {java_awt_event_KeyEvent_VK_AT, XK_at, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 274 
 275     {java_awt_event_KeyEvent_VK_OPEN_BRACKET, XK_bracketleft, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 276     {java_awt_event_KeyEvent_VK_BACK_SLASH, XK_backslash, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 277     {java_awt_event_KeyEvent_VK_CLOSE_BRACKET, XK_bracketright, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 278     {java_awt_event_KeyEvent_VK_CIRCUMFLEX, XK_asciicircum, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 279     {java_awt_event_KeyEvent_VK_UNDERSCORE, XK_underscore, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 280     {java_awt_event_KeyEvent_VK_BACK_QUOTE, XK_grave, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 281 
 282     {java_awt_event_KeyEvent_VK_BRACELEFT, XK_braceleft, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 283     {java_awt_event_KeyEvent_VK_BRACERIGHT, XK_braceright, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 284 
 285     {java_awt_event_KeyEvent_VK_INVERTED_EXCLAMATION_MARK, XK_exclamdown, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 286 
 287     /* Remaining Numeric Keypad Keys */
 288     {java_awt_event_KeyEvent_VK_NUMPAD0, XK_KP_0, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 289     {java_awt_event_KeyEvent_VK_NUMPAD1, XK_KP_1, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 290     {java_awt_event_KeyEvent_VK_NUMPAD2, XK_KP_2, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 291     {java_awt_event_KeyEvent_VK_NUMPAD3, XK_KP_3, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 292     {java_awt_event_KeyEvent_VK_NUMPAD4, XK_KP_4, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 293     {java_awt_event_KeyEvent_VK_NUMPAD5, XK_KP_5, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 294     {java_awt_event_KeyEvent_VK_NUMPAD6, XK_KP_6, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 295     {java_awt_event_KeyEvent_VK_NUMPAD7, XK_KP_7, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 296     {java_awt_event_KeyEvent_VK_NUMPAD8, XK_KP_8, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 297     {java_awt_event_KeyEvent_VK_NUMPAD9, XK_KP_9, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 298     {java_awt_event_KeyEvent_VK_SPACE, XK_KP_Space, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 299     {java_awt_event_KeyEvent_VK_TAB, XK_KP_Tab, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 300     {java_awt_event_KeyEvent_VK_ENTER, XK_KP_Enter, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 301     {java_awt_event_KeyEvent_VK_EQUALS, XK_KP_Equal, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 302     {java_awt_event_KeyEvent_VK_EQUALS, XK_R4, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 303     {java_awt_event_KeyEvent_VK_MULTIPLY, XK_KP_Multiply, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 304     {java_awt_event_KeyEvent_VK_MULTIPLY, XK_F26, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 305     {java_awt_event_KeyEvent_VK_MULTIPLY, XK_R6, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 306     {java_awt_event_KeyEvent_VK_ADD, XK_KP_Add, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 307     {java_awt_event_KeyEvent_VK_SEPARATOR, XK_KP_Separator, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 308     {java_awt_event_KeyEvent_VK_SUBTRACT, XK_KP_Subtract, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 309     {java_awt_event_KeyEvent_VK_SUBTRACT, XK_F24, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 310     {java_awt_event_KeyEvent_VK_DECIMAL, XK_KP_Decimal, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 311     {java_awt_event_KeyEvent_VK_DIVIDE, XK_KP_Divide, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 312     {java_awt_event_KeyEvent_VK_DIVIDE, XK_F25, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 313     {java_awt_event_KeyEvent_VK_DIVIDE, XK_R5, TRUE, java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD},
 314 
 315     /* Function Keys */
 316     {java_awt_event_KeyEvent_VK_F1, XK_F1, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 317     {java_awt_event_KeyEvent_VK_F2, XK_F2, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 318     {java_awt_event_KeyEvent_VK_F3, XK_F3, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 319     {java_awt_event_KeyEvent_VK_F4, XK_F4, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 320     {java_awt_event_KeyEvent_VK_F5, XK_F5, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 321     {java_awt_event_KeyEvent_VK_F6, XK_F6, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 322     {java_awt_event_KeyEvent_VK_F7, XK_F7, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 323     {java_awt_event_KeyEvent_VK_F8, XK_F8, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 324     {java_awt_event_KeyEvent_VK_F9, XK_F9, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 325     {java_awt_event_KeyEvent_VK_F10, XK_F10, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 326     {java_awt_event_KeyEvent_VK_F11, XK_F11, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 327     {java_awt_event_KeyEvent_VK_F12, XK_F12, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 328 
 329     /* Sun vendor-specific version of F11 and F12 */
 330     {java_awt_event_KeyEvent_VK_F11, SunXK_F36, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 331     {java_awt_event_KeyEvent_VK_F12, SunXK_F37, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 332 
 333     /* X11 keysym names for input method related keys don't always
 334      * match keytop engravings or Java virtual key names, so here we
 335      * only map constants that we've found on real keyboards.
 336      */
 337     /* Type 5c Japanese keyboard: kakutei */
 338     {java_awt_event_KeyEvent_VK_ACCEPT, XK_Execute, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 339     /* Type 5c Japanese keyboard: henkan */
 340     {java_awt_event_KeyEvent_VK_CONVERT, XK_Kanji, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 341     /* Type 5c Japanese keyboard: nihongo */
 342     {java_awt_event_KeyEvent_VK_INPUT_METHOD_ON_OFF, XK_Henkan_Mode, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 343     /* VK_KANA_LOCK is handled separately because it generates the
 344      * same keysym as ALT_GRAPH in spite of its different behavior.
 345      */
 346 
 347     {java_awt_event_KeyEvent_VK_ALL_CANDIDATES, XK_Zen_Koho, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 348     {java_awt_event_KeyEvent_VK_ALPHANUMERIC, XK_Eisu_Shift, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 349     {java_awt_event_KeyEvent_VK_ALPHANUMERIC, XK_Eisu_toggle, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 350     {java_awt_event_KeyEvent_VK_CODE_INPUT, XK_Kanji_Bangou, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 351     {java_awt_event_KeyEvent_VK_FULL_WIDTH, XK_Zenkaku, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 352     {java_awt_event_KeyEvent_VK_HALF_WIDTH, XK_Hankaku, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 353     {java_awt_event_KeyEvent_VK_HIRAGANA, XK_Hiragana, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 354     {java_awt_event_KeyEvent_VK_JAPANESE_HIRAGANA, XK_Hiragana, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 355     {java_awt_event_KeyEvent_VK_KATAKANA, XK_Katakana, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 356     {java_awt_event_KeyEvent_VK_JAPANESE_KATAKANA, XK_Katakana, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 357     {java_awt_event_KeyEvent_VK_JAPANESE_ROMAN, XK_Romaji, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 358     {java_awt_event_KeyEvent_VK_KANA, XK_Kana_Shift, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 359     {java_awt_event_KeyEvent_VK_KANA_LOCK, XK_Kana_Lock, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 360     {java_awt_event_KeyEvent_VK_KANJI, XK_Kanji, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 361     {java_awt_event_KeyEvent_VK_NONCONVERT, XK_Muhenkan, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 362     {java_awt_event_KeyEvent_VK_PREVIOUS_CANDIDATE, XK_Mae_Koho, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 363     {java_awt_event_KeyEvent_VK_ROMAN_CHARACTERS, XK_Romaji, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 364 
 365     {java_awt_event_KeyEvent_VK_COMPOSE, XK_Multi_key, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 366     {java_awt_event_KeyEvent_VK_ALT_GRAPH, XK_ISO_Level3_Shift, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 367 
 368     /* Editing block */
 369     {java_awt_event_KeyEvent_VK_AGAIN, XK_Redo, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 370     {java_awt_event_KeyEvent_VK_AGAIN, XK_L2, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 371     {java_awt_event_KeyEvent_VK_UNDO, XK_Undo, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 372     {java_awt_event_KeyEvent_VK_UNDO, XK_L4, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 373     {java_awt_event_KeyEvent_VK_COPY, XK_L6, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 374     {java_awt_event_KeyEvent_VK_PASTE, XK_L8, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 375     {java_awt_event_KeyEvent_VK_CUT, XK_L10, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 376     {java_awt_event_KeyEvent_VK_FIND, XK_Find, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 377     {java_awt_event_KeyEvent_VK_FIND, XK_L9, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 378     {java_awt_event_KeyEvent_VK_PROPS, XK_L3, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 379     {java_awt_event_KeyEvent_VK_STOP, XK_L1, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 380 
 381     /* Sun vendor-specific versions for editing block */
 382     {java_awt_event_KeyEvent_VK_AGAIN, SunXK_Again, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 383     {java_awt_event_KeyEvent_VK_UNDO, SunXK_Undo, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 384     {java_awt_event_KeyEvent_VK_COPY, SunXK_Copy, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 385     {java_awt_event_KeyEvent_VK_PASTE, SunXK_Paste, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 386     {java_awt_event_KeyEvent_VK_CUT, SunXK_Cut, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 387     {java_awt_event_KeyEvent_VK_FIND, SunXK_Find, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 388     {java_awt_event_KeyEvent_VK_PROPS, SunXK_Props, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 389     {java_awt_event_KeyEvent_VK_STOP, SunXK_Stop, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 390 
 391     /* Apollo (HP) vendor-specific versions for editing block */
 392     {java_awt_event_KeyEvent_VK_COPY, apXK_Copy, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 393     {java_awt_event_KeyEvent_VK_CUT, apXK_Cut, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 394     {java_awt_event_KeyEvent_VK_PASTE, apXK_Paste, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 395 
 396     /* Other vendor-specific versions for editing block */
 397     {java_awt_event_KeyEvent_VK_COPY, osfXK_Copy, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 398     {java_awt_event_KeyEvent_VK_CUT, osfXK_Cut, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 399     {java_awt_event_KeyEvent_VK_PASTE, osfXK_Paste, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 400     {java_awt_event_KeyEvent_VK_UNDO, osfXK_Undo, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 401 
 402     /* Dead key mappings (for European keyboards) */
 403     {java_awt_event_KeyEvent_VK_DEAD_GRAVE, XK_dead_grave, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 404     {java_awt_event_KeyEvent_VK_DEAD_ACUTE, XK_dead_acute, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 405     {java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX, XK_dead_circumflex, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 406     {java_awt_event_KeyEvent_VK_DEAD_TILDE, XK_dead_tilde, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 407     {java_awt_event_KeyEvent_VK_DEAD_MACRON, XK_dead_macron, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 408     {java_awt_event_KeyEvent_VK_DEAD_BREVE, XK_dead_breve, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 409     {java_awt_event_KeyEvent_VK_DEAD_ABOVEDOT, XK_dead_abovedot, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 410     {java_awt_event_KeyEvent_VK_DEAD_DIAERESIS, XK_dead_diaeresis, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 411     {java_awt_event_KeyEvent_VK_DEAD_ABOVERING, XK_dead_abovering, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 412     {java_awt_event_KeyEvent_VK_DEAD_DOUBLEACUTE, XK_dead_doubleacute, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 413     {java_awt_event_KeyEvent_VK_DEAD_CARON, XK_dead_caron, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 414     {java_awt_event_KeyEvent_VK_DEAD_CEDILLA, XK_dead_cedilla, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 415     {java_awt_event_KeyEvent_VK_DEAD_OGONEK, XK_dead_ogonek, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 416     {java_awt_event_KeyEvent_VK_DEAD_IOTA, XK_dead_iota, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 417     {java_awt_event_KeyEvent_VK_DEAD_VOICED_SOUND, XK_dead_voiced_sound, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 418     {java_awt_event_KeyEvent_VK_DEAD_SEMIVOICED_SOUND, XK_dead_semivoiced_sound, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 419 
 420     /* Sun vendor-specific dead key mappings (for European keyboards) */
 421     {java_awt_event_KeyEvent_VK_DEAD_GRAVE, SunXK_FA_Grave, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 422     {java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX, SunXK_FA_Circum, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 423     {java_awt_event_KeyEvent_VK_DEAD_TILDE, SunXK_FA_Tilde, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 424     {java_awt_event_KeyEvent_VK_DEAD_ACUTE, SunXK_FA_Acute, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 425     {java_awt_event_KeyEvent_VK_DEAD_DIAERESIS, SunXK_FA_Diaeresis, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 426     {java_awt_event_KeyEvent_VK_DEAD_CEDILLA, SunXK_FA_Cedilla, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 427 
 428     /* DEC vendor-specific dead key mappings (for European keyboards) */
 429     {java_awt_event_KeyEvent_VK_DEAD_ABOVERING, DXK_ring_accent, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 430     {java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX, DXK_circumflex_accent, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 431     {java_awt_event_KeyEvent_VK_DEAD_CEDILLA, DXK_cedilla_accent, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 432     {java_awt_event_KeyEvent_VK_DEAD_ACUTE, DXK_acute_accent, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 433     {java_awt_event_KeyEvent_VK_DEAD_GRAVE, DXK_grave_accent, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 434     {java_awt_event_KeyEvent_VK_DEAD_TILDE, DXK_tilde, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 435     {java_awt_event_KeyEvent_VK_DEAD_DIAERESIS, DXK_diaeresis, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 436 
 437     /* Other vendor-specific dead key mappings (for European keyboards) */
 438     {java_awt_event_KeyEvent_VK_DEAD_ACUTE, hpXK_mute_acute, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 439     {java_awt_event_KeyEvent_VK_DEAD_GRAVE, hpXK_mute_grave, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 440     {java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX, hpXK_mute_asciicircum, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 441     {java_awt_event_KeyEvent_VK_DEAD_DIAERESIS, hpXK_mute_diaeresis, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 442     {java_awt_event_KeyEvent_VK_DEAD_TILDE, hpXK_mute_asciitilde, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_STANDARD},
 443 
 444     {java_awt_event_KeyEvent_VK_UNDEFINED, NoSymbol, FALSE, java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN}
 445 };
 446 
 447 static Boolean
 448 keyboardHasKanaLockKey()
 449 {
 450     static Boolean haveResult = FALSE;
 451     static Boolean result = FALSE;
 452 
 453     int32_t minKeyCode, maxKeyCode, keySymsPerKeyCode;
 454     KeySym *keySyms, *keySymsStart, keySym;
 455     int32_t i;
 456     int32_t kanaCount = 0;
 457 
 458     // Solaris doesn't let you swap keyboards without rebooting,
 459     // so there's no need to check for the kana lock key more than once.
 460     if (haveResult) {
 461        return result;
 462     }
 463 
 464     // There's no direct way to determine whether the keyboard has
 465     // a kana lock key. From available keyboard mapping tables, it looks
 466     // like only keyboards with the kana lock key can produce keysyms
 467     // for kana characters. So, as an indirect test, we check for those.
 468     XDisplayKeycodes(awt_display, &minKeyCode, &maxKeyCode);
 469     keySyms = XGetKeyboardMapping(awt_display, minKeyCode, maxKeyCode - minKeyCode + 1, &keySymsPerKeyCode);
 470     keySymsStart = keySyms;
 471     for (i = 0; i < (maxKeyCode - minKeyCode + 1) * keySymsPerKeyCode; i++) {
 472         keySym = *keySyms++;
 473         if ((keySym & 0xff00) == 0x0400) {
 474             kanaCount++;
 475         }
 476     }
 477     XFree(keySymsStart);
 478 
 479     // use a (somewhat arbitrary) minimum so we don't get confused by a stray function key
 480     result = kanaCount > 10;
 481     haveResult = TRUE;
 482     return result;
 483 }
 484 
 485 static void
 486 keysymToAWTKeyCode(KeySym x11Key, jint *keycode, Boolean *mapsToUnicodeChar,
 487   jint *keyLocation)
 488 {
 489     int32_t i;
 490 
 491     // Solaris uses XK_Mode_switch for both the non-locking AltGraph
 492     // and the locking Kana key, but we want to keep them separate for
 493     // KeyEvent.
 494     if (x11Key == XK_Mode_switch && keyboardHasKanaLockKey()) {
 495         *keycode = java_awt_event_KeyEvent_VK_KANA_LOCK;
 496         *mapsToUnicodeChar = FALSE;
 497         *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 498         return;
 499     }
 500 
 501     for (i = 0;
 502          keymapTable[i].awtKey != java_awt_event_KeyEvent_VK_UNDEFINED;
 503          i++)
 504     {
 505         if (keymapTable[i].x11Key == x11Key) {
 506             *keycode = keymapTable[i].awtKey;
 507             *mapsToUnicodeChar = keymapTable[i].mapsToUnicodeChar;
 508             *keyLocation = keymapTable[i].keyLocation;
 509             return;
 510         }
 511     }
 512 
 513     *keycode = java_awt_event_KeyEvent_VK_UNDEFINED;
 514     *mapsToUnicodeChar = FALSE;
 515     *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 516 
 517     DTRACE_PRINTLN1("keysymToAWTKeyCode: no key mapping found: keysym = 0x%x", x11Key);
 518 }
 519 
 520 KeySym
 521 awt_getX11KeySym(jint awtKey)
 522 {
 523     int32_t i;
 524 
 525     if (awtKey == java_awt_event_KeyEvent_VK_KANA_LOCK && keyboardHasKanaLockKey()) {
 526         return XK_Mode_switch;
 527     }
 528 
 529     for (i = 0; keymapTable[i].awtKey != 0; i++) {
 530         if (keymapTable[i].awtKey == awtKey) {
 531             return keymapTable[i].x11Key;
 532         }
 533     }
 534 
 535     DTRACE_PRINTLN1("awt_getX11KeySym: no key mapping found: awtKey = 0x%x", awtKey);
 536     return NoSymbol;
 537 }
 538 
 539 /* Called from handleKeyEvent.  The purpose of this function is
 540  * to check for a list of vendor-specific keysyms, most of which
 541  * have values greater than 0xFFFF.  Most of these keys don't map
 542  * to unicode characters, but some do.
 543  *
 544  * For keys that don't map to unicode characters, the keysym
 545  * is irrelevant at this point.  We set the keysym to zero
 546  * to ensure that the switch statement immediately below
 547  * this function call (in adjustKeySym) won't incorrectly act
 548  * on them after the high bits are stripped off.
 549  *
 550  * For keys that do map to unicode characters, we change the keysym
 551  * to the equivalent that is < 0xFFFF
 552  */
 553 static void
 554 handleVendorKeySyms(XEvent *event, KeySym *keysym)
 555 {
 556     KeySym originalKeysym = *keysym;
 557 
 558     switch (*keysym) {
 559         /* Apollo (HP) vendor-specific from <X11/ap_keysym.h> */
 560         case apXK_Copy:
 561         case apXK_Cut:
 562         case apXK_Paste:
 563         /* DEC vendor-specific from <X11/DECkeysym.h> */
 564         case DXK_ring_accent:         /* syn usldead_ring */
 565         case DXK_circumflex_accent:
 566         case DXK_cedilla_accent:      /* syn usldead_cedilla */
 567         case DXK_acute_accent:
 568         case DXK_grave_accent:
 569         case DXK_tilde:
 570         case DXK_diaeresis:
 571         /* Sun vendor-specific from <X11/Sunkeysym.h> */
 572         case SunXK_FA_Grave:
 573         case SunXK_FA_Circum:
 574         case SunXK_FA_Tilde:
 575         case SunXK_FA_Acute:
 576         case SunXK_FA_Diaeresis:
 577         case SunXK_FA_Cedilla:
 578         case SunXK_F36:                /* Labeled F11 */
 579         case SunXK_F37:                /* Labeled F12 */
 580         case SunXK_Props:
 581         case SunXK_Copy:
 582         case SunXK_Open:
 583         case SunXK_Paste:
 584         case SunXK_Cut:
 585         /* Other vendor-specific from HPkeysym.h */
 586         case hpXK_mute_acute:          /* syn usldead_acute */
 587         case hpXK_mute_grave:          /* syn usldead_grave */
 588         case hpXK_mute_asciicircum:    /* syn usldead_asciicircum */
 589         case hpXK_mute_diaeresis:      /* syn usldead_diaeresis */
 590         case hpXK_mute_asciitilde:     /* syn usldead_asciitilde */
 591         case osfXK_Copy:
 592         case osfXK_Cut:
 593         case osfXK_Paste:
 594         case osfXK_PageUp:
 595         case osfXK_PageDown:
 596         case osfXK_EndLine:
 597         case osfXK_Clear:
 598         case osfXK_Left:
 599         case osfXK_Up:
 600         case osfXK_Right:
 601         case osfXK_Down:
 602         case osfXK_Prior:
 603         case osfXK_Next:
 604         case osfXK_Insert:
 605         case osfXK_Undo:
 606         case osfXK_Help:
 607             *keysym = 0;
 608             break;
 609         /*
 610          * The rest DO map to unicode characters, so translate them
 611          */
 612         case osfXK_BackSpace:
 613             *keysym = XK_BackSpace;
 614             break;
 615         case osfXK_Escape:
 616             *keysym = XK_Escape;
 617             break;
 618         case osfXK_Cancel:
 619             *keysym = XK_Cancel;
 620             break;
 621         case osfXK_Delete:
 622             *keysym = XK_Delete;
 623             break;
 624         default:
 625             break;
 626     }
 627 
 628     if (originalKeysym != *keysym) {
 629         DTRACE_PRINTLN3("%s originalKeysym=0x%x, keysym=0x%x",
 630           "In handleVendorKeySyms:", originalKeysym, *keysym);
 631     }
 632 }
 633 
 634 /* Called from handleKeyEvent.
 635  * The purpose of this function is to adjust the keysym and XEvent
 636  * keycode for a key event.  This is basically a conglomeration of
 637  * bugfixes that require these adjustments.
 638  * Note that none of the keysyms in this function are less than 256.
 639  */
 640 static void
 641 adjustKeySym(XEvent *event, KeySym *keysym)
 642 {
 643     KeySym originalKeysym = *keysym;
 644     KeyCode originalKeycode = event->xkey.keycode;
 645 
 646     /* We have seen bits set in the high two bytes on Linux,
 647      * which prevents this switch statement from executing
 648      * correctly.  Strip off the high order bits.
 649      */
 650     *keysym &= 0x0000FFFF;
 651 
 652     switch (*keysym) {
 653         case XK_ISO_Left_Tab:        /* shift-tab on Linux */
 654             *keysym = XK_Tab;
 655             break;
 656         case XK_KP_Decimal:
 657             *keysym = '.';
 658             break;
 659         case XK_KP_Add:
 660             *keysym = '+';
 661             break;
 662         case XK_F24:           /* NumLock off */
 663         case XK_KP_Subtract:   /* NumLock on */
 664             *keysym = '-';
 665             break;
 666         case XK_F25:           /* NumLock off */
 667         case XK_KP_Divide:     /* NumLock on */
 668             *keysym = '/';
 669             break;
 670         case XK_F26:           /* NumLock off */
 671         case XK_KP_Multiply:   /* NumLock on */
 672             *keysym = '*';
 673             break;
 674         case XK_KP_Equal:
 675             *keysym = '=';
 676             break;
 677         case XK_KP_0:
 678             *keysym = '0';
 679             break;
 680         case XK_KP_1:
 681             *keysym = '1';
 682             break;
 683         case XK_KP_2:
 684             *keysym = '2';
 685             break;
 686         case XK_KP_3:
 687             *keysym = '3';
 688             break;
 689         case XK_KP_4:
 690             *keysym = '4';
 691             break;
 692         case XK_KP_5:
 693             *keysym = '5';
 694             break;
 695         case XK_KP_6:
 696             *keysym = '6';
 697             break;
 698         case XK_KP_7:
 699             *keysym = '7';
 700             break;
 701         case XK_KP_8:
 702             *keysym = '8';
 703             break;
 704         case XK_KP_9:
 705             *keysym = '9';
 706             break;
 707         case XK_KP_Left:  /* Bug 4350175 */
 708             *keysym = XK_Left;
 709             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 710             break;
 711         case XK_KP_Up:
 712             *keysym = XK_Up;
 713             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 714             break;
 715         case XK_KP_Right:
 716             *keysym = XK_Right;
 717             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 718             break;
 719         case XK_KP_Down:
 720             *keysym = XK_Down;
 721             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 722             break;
 723         case XK_KP_Home:
 724             *keysym = XK_Home;
 725             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 726             break;
 727         case XK_KP_End:
 728             *keysym = XK_End;
 729             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 730             break;
 731         case XK_KP_Page_Up:
 732             *keysym = XK_Page_Up;
 733             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 734             break;
 735         case XK_KP_Page_Down:
 736             *keysym = XK_Page_Down;
 737             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 738             break;
 739         case XK_KP_Begin:
 740             *keysym = XK_Begin;
 741             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 742             break;
 743         case XK_KP_Insert:
 744             *keysym = XK_Insert;
 745             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 746             break;
 747         case XK_KP_Delete:
 748             *keysym = XK_Delete;
 749             event->xkey.keycode = XKeysymToKeycode(awt_display, *keysym);
 750             break;
 751         case XK_KP_Enter:
 752             *keysym = XK_Linefeed;
 753             event->xkey.keycode = XKeysymToKeycode(awt_display, XK_Return);
 754             break;
 755         default:
 756             break;
 757     }
 758 
 759     if (originalKeysym != *keysym) {
 760         DTRACE_PRINTLN2("In adjustKeySym: originalKeysym=0x%x, keysym=0x%x",
 761           originalKeysym, *keysym);
 762     }
 763     if (originalKeycode != event->xkey.keycode) {
 764         DTRACE_PRINTLN2("In adjustKeySym: originalKeycode=0x%x, keycode=0x%x",
 765           originalKeycode, event->xkey.keycode);
 766     }
 767 }
 768 
 769 /*
 770  * What a sniffer sez?
 771  * Xsun and Xorg if NumLock is on do two thing different:
 772  * keep Keypad key in different places of keysyms array and
 773  * ignore/obey "ModLock is ShiftLock", so we should choose.
 774  * People say, it's right to use behavior and not Vendor tags to decide.
 775  * Maybe. But why these tags were invented, then?
 776  * TODO: use behavior, not tags. Maybe.
 777  */
 778 static Boolean
 779 isXsunServer(XEvent *event) {
 780     if( awt_ServerDetected ) return awt_IsXsun;
 781     if( (strncmp( ServerVendor( event->xkey.display ), "Sun Microsystems, Inc.", 22) != 0) &&
 782         (strncmp( ServerVendor( event->xkey.display ), "Oracle Corporation", 18) != 0) )
 783     {
 784         awt_ServerDetected = True;
 785         awt_IsXsun = False;
 786         return False;
 787     }
 788     // Now, it's Sun. It still may be Xorg though, eg on Solaris 10, x86.
 789     // Today (2005), VendorRelease of Xorg is a Big Number unlike Xsun.
 790     if( VendorRelease( event->xkey.display ) > 10000 ) {
 791         awt_ServerDetected = True;
 792         awt_IsXsun = False;
 793         return False;
 794     }
 795     awt_ServerDetected = True;
 796     awt_IsXsun = True;
 797     return True;
 798 }
 799 /*
 800  * +kb or -kb ?
 801  */
 802 static Boolean
 803 isXKBenabled(Display *display) {
 804     int mop, beve, berr;
 805     if( !awt_XKBDetected ) {
 806         /*
 807          * NB: TODO: hope it will return False if XkbIgnoreExtension was called!
 808          */
 809         awt_UseXKB = XQueryExtension(display, "XKEYBOARD", &mop, &beve, &berr);
 810         awt_XKBDetected = True;
 811     }
 812     return awt_UseXKB;
 813 }
 814 
 815 /*
 816  * Map a keycode to the corresponding keysym.
 817  * This replaces the deprecated X11 function XKeycodeToKeysym
 818  */
 819 KeySym
 820 keycodeToKeysym(Display *display, KeyCode keycode, int index) {
 821     static int min_kc = -1;
 822     static int max_kc;
 823     if (min_kc == -1) {
 824         (void) XDisplayKeycodes(display, &min_kc, &max_kc);
 825     }
 826     if (keycode < min_kc || keycode > max_kc || index < 0) {
 827         return NoSymbol;
 828     }
 829     int num_syms;
 830     KeySym *key_syms = XGetKeyboardMapping(display, keycode, 1, &num_syms);
 831     if (index >= num_syms) {
 832         XFree(key_syms);
 833         return NoSymbol;
 834     }
 835     KeySym ks = key_syms[index];
 836     XFree(key_syms);
 837     return ks;
 838 }
 839 
 840 static Boolean
 841 isKPevent(XEvent *event)
 842 {
 843     /*
 844      *  Xlib manual, ch 12.7 says, as a first rule for choice of keysym:
 845      *  The numlock modifier is on and the second KeySym is a keypad KeySym. In this case,
 846      *  if the Shift modifier is on, or if the Lock modifier is on and is interpreted as ShiftLock,
 847      *  then the first KeySym is used, otherwise the second KeySym is used.
 848      *
 849      *  However, Xsun server does ignore ShiftLock and always takes 3-rd element from an array.
 850      *
 851      *  So, is it a keypad keysym?
 852      */
 853     Boolean bsun = isXsunServer( event );
 854     Boolean bxkb = isXKBenabled( event->xkey.display );
 855     return IsKeypadKey( keycodeToKeysym(event->xkey.display, event->xkey.keycode,(bsun && !bxkb ? 2 : 1) ) );
 856 }
 857 static void
 858 dumpKeysymArray(XEvent *event) {
 859     printf("    0x%lX\n", (unsigned long)keycodeToKeysym(event->xkey.display, event->xkey.keycode, 0));
 860     printf("    0x%lX\n", (unsigned long)keycodeToKeysym(event->xkey.display, event->xkey.keycode, 1));
 861     printf("    0x%lX\n", (unsigned long)keycodeToKeysym(event->xkey.display, event->xkey.keycode, 2));
 862     printf("    0x%lX\n", (unsigned long)keycodeToKeysym(event->xkey.display, event->xkey.keycode, 3));
 863 }
 864 /*
 865  * In a next redesign, get rid of this code altogether.
 866  *
 867  */
 868 static void
 869 handleKeyEventWithNumLockMask_New(XEvent *event, KeySym *keysym)
 870 {
 871     KeySym originalKeysym = *keysym;
 872     if( !isKPevent( event ) ) {
 873         return;
 874     }
 875     if( isXsunServer( event ) && !awt_UseXKB) {
 876         if( (event->xkey.state & ShiftMask) ) { // shift modifier is on
 877             *keysym = keycodeToKeysym(event->xkey.display,
 878                                    event->xkey.keycode, 3);
 879          }else {
 880             *keysym = keycodeToKeysym(event->xkey.display,
 881                                    event->xkey.keycode, 2);
 882          }
 883     } else {
 884         if( (event->xkey.state & ShiftMask) || // shift modifier is on
 885             ((event->xkey.state & LockMask) && // lock modifier is on
 886              (awt_ModLockIsShiftLock)) ) {     // it is interpreted as ShiftLock
 887             *keysym = keycodeToKeysym(event->xkey.display,
 888                                    event->xkey.keycode, 0);
 889         }else{
 890             *keysym = keycodeToKeysym(event->xkey.display,
 891                                    event->xkey.keycode, 1);
 892         }
 893     }
 894 }
 895 
 896 /* Called from handleKeyEvent.
 897  * The purpose of this function is to make some adjustments to keysyms
 898  * that have been found to be necessary when the NumLock mask is set.
 899  * They come from various bug fixes and rearchitectures.
 900  * This function is meant to be called when
 901  * (event->xkey.state & awt_NumLockMask) is TRUE.
 902  */
 903 static void
 904 handleKeyEventWithNumLockMask(XEvent *event, KeySym *keysym)
 905 {
 906     KeySym originalKeysym = *keysym;
 907 
 908 #if !defined(__linux__)
 909     /* The following code on Linux will cause the keypad keys
 910      * not to echo on JTextField when the NumLock is on. The
 911      * keysyms will be 0, because the last parameter 2 is not defined.
 912      * See Xlib Programming Manual, O'Reilly & Associates, Section
 913      * 9.1.5 "Other Keyboard-handling Routines", "The meaning of
 914      * the keysym list beyond the first two (unmodified, Shift or
 915      * Shift Lock) is not defined."
 916      */
 917 
 918     /* Translate again with NumLock as modifier. */
 919     /* ECH - I wonder why we think that NumLock corresponds to 2?
 920        On Linux, we've seen xmodmap -pm yield mod2 as NumLock,
 921        but I don't know that it will be for every configuration.
 922        Perhaps using the index (modn in awt_MToolkit.c:setup_modifier_map)
 923        would be more correct.
 924      */
 925     *keysym = keycodeToKeysym(event->xkey.display,
 926                                event->xkey.keycode, 2);
 927     if (originalKeysym != *keysym) {
 928         DTRACE_PRINTLN3("%s originalKeysym=0x%x, keysym=0x%x",
 929           "In handleKeyEventWithNumLockMask ifndef linux:",
 930           originalKeysym, *keysym);
 931     }
 932 #endif
 933 
 934     /* Note: the XK_R? key assignments are for Type 4 kbds */
 935     switch (*keysym) {
 936         case XK_R13:
 937             *keysym = XK_KP_1;
 938             break;
 939         case XK_R14:
 940             *keysym = XK_KP_2;
 941             break;
 942         case XK_R15:
 943             *keysym = XK_KP_3;
 944             break;
 945         case XK_R10:
 946             *keysym = XK_KP_4;
 947             break;
 948         case XK_R11:
 949             *keysym = XK_KP_5;
 950             break;
 951         case XK_R12:
 952             *keysym = XK_KP_6;
 953             break;
 954         case XK_R7:
 955             *keysym = XK_KP_7;
 956             break;
 957         case XK_R8:
 958             *keysym = XK_KP_8;
 959             break;
 960         case XK_R9:
 961             *keysym = XK_KP_9;
 962             break;
 963         case XK_KP_Insert:
 964             *keysym = XK_KP_0;
 965             break;
 966         case XK_KP_Delete:
 967             *keysym = XK_KP_Decimal;
 968             break;
 969         case XK_R4:
 970             *keysym = XK_KP_Equal;  /* Type 4 kbd */
 971             break;
 972         case XK_R5:
 973             *keysym = XK_KP_Divide;
 974             break;
 975         case XK_R6:
 976             *keysym = XK_KP_Multiply;
 977             break;
 978         /*
 979          * Need the following keysym changes for Linux key releases.
 980          * Sometimes the modifier state gets messed up, so we get a
 981          * KP_Left when we should get a KP_4, for example.
 982          * XK_KP_Insert and XK_KP_Delete were already handled above.
 983          */
 984         case XK_KP_Left:
 985             *keysym = XK_KP_4;
 986             break;
 987         case XK_KP_Up:
 988             *keysym = XK_KP_8;
 989             break;
 990         case XK_KP_Right:
 991             *keysym = XK_KP_6;
 992             break;
 993         case XK_KP_Down:
 994             *keysym = XK_KP_2;
 995             break;
 996         case XK_KP_Home:
 997             *keysym = XK_KP_7;
 998             break;
 999         case XK_KP_End:
1000             *keysym = XK_KP_1;
1001             break;
1002         case XK_KP_Page_Up:
1003             *keysym = XK_KP_9;
1004             break;
1005         case XK_KP_Page_Down:
1006             *keysym = XK_KP_3;
1007             break;
1008         case XK_KP_Begin:
1009             *keysym = XK_KP_5;
1010             break;
1011         default:
1012             break;
1013     }
1014 
1015     if (originalKeysym != *keysym) {
1016         DTRACE_PRINTLN3("%s originalKeysym=0x%x, keysym=0x%x",
1017           "In handleKeyEventWithNumLockMask:", originalKeysym, *keysym);
1018     }
1019 }
1020 
1021 /* This function is called as the keyChar parameter of a call to
1022  * awt_post_java_key_event.  It depends on being called after adjustKeySym.
1023  *
1024  * This function just handles a few values where we know that the
1025  * keysym is not the same as the unicode value.  For values that
1026  * we don't handle explicitly, we just cast the keysym to a jchar.
1027  * Most of the real mapping work that gets the correct keysym is handled
1028  * in the mapping table, adjustKeySym, etc.
1029  *
1030  * XXX
1031  * Maybe we should enumerate the keysyms for which we have a mapping
1032  * in the keyMap, but that don't map to unicode chars, and return
1033  * CHAR_UNDEFINED?  Then use the buffer value from XLookupString
1034  * instead of the keysym as the keychar when posting.  Then we don't
1035  * need to test using mapsToUnicodeChar.  That way, we would post keyTyped
1036  * for all the chars that generate unicode chars, including LATIN2-4, etc.
1037  * Note: what does the buffer from XLookupString contain when
1038  * the character is a non-printable unicode character like Cancel or Delete?
1039  */
1040 jchar
1041 keySymToUnicodeCharacter(KeySym keysym) {
1042     jchar unicodeValue = (jchar) keysym;
1043 
1044     switch (keysym) {
1045       case XK_BackSpace:
1046       case XK_Tab:
1047       case XK_Linefeed:
1048       case XK_Escape:
1049       case XK_Delete:
1050           /* Strip off highorder bits defined in xkeysymdef.h
1051            * I think doing this converts them to values that
1052            * we can cast to jchars and use as java keychars.
1053            */
1054           unicodeValue = (jchar) (keysym & 0x007F);
1055           break;
1056       case XK_Return:
1057           unicodeValue = (jchar) 0x000a;  /* the unicode char for Linefeed */
1058           break;
1059       case XK_Cancel:
1060           unicodeValue = (jchar) 0x0018;  /* the unicode char for Cancel */
1061           break;
1062       default:
1063           break;
1064     }
1065 
1066     if (unicodeValue != (jchar)keysym) {
1067         DTRACE_PRINTLN3("%s originalKeysym=0x%x, keysym=0x%x",
1068           "In keysymToUnicode:", keysym, unicodeValue);
1069     }
1070 
1071     return unicodeValue;
1072 }
1073 
1074 
1075 void
1076 awt_post_java_key_event(JNIEnv *env, jobject peer, jint id,
1077   jlong when, jint keyCode, jchar keyChar, jint keyLocation, jint state, XEvent * event)
1078 {
1079     JNU_CallMethodByName(env, NULL, peer, "postKeyEvent", "(IJICIIJI)V", id,
1080         when, keyCode, keyChar, keyLocation, state, ptr_to_jlong(event), (jint)sizeof(XEvent));
1081 } /* awt_post_java_key_event() */
1082 
1083 
1084 
1085 JNIEXPORT jint JNICALL
1086 Java_sun_awt_X11_XWindow_getAWTKeyCodeForKeySym(JNIEnv *env, jclass clazz, jint keysym) {
1087     jint keycode = java_awt_event_KeyEvent_VK_UNDEFINED;
1088     Boolean mapsToUnicodeChar;
1089     jint keyLocation;
1090     keysymToAWTKeyCode(keysym, &keycode, &mapsToUnicodeChar, &keyLocation);
1091     return keycode;
1092 }
1093 
1094 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XWindow_haveCurrentX11InputMethodInstance
1095 (JNIEnv *env, jobject object) {
1096     /*printf("Java_sun_awt_X11_XWindow_haveCurrentX11InputMethodInstance: %s\n", (currentX11InputMethodInstance==NULL? "NULL":" notnull"));
1097     */
1098     return currentX11InputMethodInstance != NULL ? JNI_TRUE : JNI_FALSE;
1099 }
1100 
1101 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XWindow_x11inputMethodLookupString
1102 (JNIEnv *env, jobject object, jlong event, jlongArray keysymArray) {
1103    KeySym keysym = NoSymbol;
1104    Boolean boo;
1105    /* keysymArray (and testbuf[]) have dimension 2 because we put there two
1106     * perhaps different values of keysyms.
1107     * XXX: not anymore at the moment, but I'll still keep them as arrays
1108     * for a while.  If in the course of testing we will be satisfied with
1109     * a current single result from awt_x11inputmethod_lookupString, we'll
1110     * change this.
1111     */
1112    jlong testbuf[2];
1113 
1114    testbuf[1]=0;
1115 
1116    boo = awt_x11inputmethod_lookupString((XKeyPressedEvent*)jlong_to_ptr(event), &keysym);
1117    testbuf[0] = keysym;
1118 
1119    (*env)->SetLongArrayRegion(env, keysymArray, 0, 2, (jlong *)(testbuf));
1120    return boo ? JNI_TRUE : JNI_FALSE;
1121 }
1122 
1123 
1124 extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
1125 
1126 /*
1127  * Class:     Java_sun_awt_X11_XWindow_getNativeColor
1128  * Method:    getNativeColor
1129  * Signature  (Ljava/awt/Color;Ljava/awt/GraphicsConfiguration;)I
1130  */
1131 JNIEXPORT jint JNICALL Java_sun_awt_X11_XWindow_getNativeColor
1132 (JNIEnv *env, jobject this, jobject color, jobject gc_object) {
1133     AwtGraphicsConfigDataPtr adata;
1134     /* fire warning because JNU_GetLongFieldAsPtr casts jlong to (void *) */
1135     adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, gc_object, x11GraphicsConfigIDs.aData);
1136     return awtJNI_GetColorForVis(env, color, adata);
1137 }
1138 
1139 /* syncTopLevelPos() is necessary to insure that the window manager has in
1140  * fact moved us to our final position relative to the reParented WM window.
1141  * We have noted a timing window which our shell has not been moved so we
1142  * screw up the insets thinking they are 0,0.  Wait (for a limited period of
1143  * time to let the WM hava a chance to move us
1144  */
1145 void syncTopLevelPos( Display *d, Window w, XWindowAttributes *winAttr ) {
1146     int32_t i = 0;
1147     do {
1148          XGetWindowAttributes( d, w, winAttr );
1149          /* Sometimes we get here before the WM has updated the
1150          ** window data struct with the correct position.  Loop
1151          ** until we get a non-zero position.
1152          */
1153          if ((winAttr->x != 0) || (winAttr->y != 0)) {
1154              break;
1155          }
1156          else {
1157              /* What we really want here is to sync with the WM,
1158              ** but there's no explicit way to do this, so we
1159              ** call XSync for a delay.
1160              */
1161              XSync(d, False);
1162          }
1163     } while (i++ < 50);
1164 }
1165 
1166 static Window getTopWindow(Window win, Window *rootWin)
1167 {
1168     Window root=None, current_window=win, parent=None, *ignore_children=NULL;
1169     Window prev_window=None;
1170     unsigned int ignore_uint=0;
1171     Status status = 0;
1172 
1173     if (win == None) return None;
1174     do {
1175         status = XQueryTree(awt_display,
1176                             current_window,
1177                             &root,
1178                             &parent,
1179                             &ignore_children,
1180                             &ignore_uint);
1181         XFree(ignore_children);
1182         if (status == 0) return None;
1183         prev_window = current_window;
1184         current_window = parent;
1185     } while (parent != root);
1186     *rootWin = root;
1187     return prev_window;
1188 }
1189 
1190 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XWindow_getTopWindow
1191 (JNIEnv *env, jclass clazz, jlong win, jlong rootWin) {
1192     return getTopWindow((Window) win, (Window*) jlong_to_ptr(rootWin));
1193 }
1194 
1195 static void
1196 getWMInsets
1197 (Window window, int *left, int *top, int *right, int *bottom, int *border) {
1198     // window is event->xreparent.window
1199     Window topWin = None, rootWin = None, containerWindow = None;
1200     XWindowAttributes winAttr, topAttr;
1201     int screenX, screenY;
1202     topWin = getTopWindow(window, &rootWin);
1203     syncTopLevelPos(awt_display, topWin, &topAttr);
1204     // (screenX, screenY) is (0,0) of the reparented window
1205     // converted to screen coordinates.
1206     XTranslateCoordinates(awt_display, window, rootWin,
1207         0,0, &screenX, &screenY, &containerWindow);
1208     *left = screenX - topAttr.x - topAttr.border_width;
1209     *top  = screenY - topAttr.y - topAttr.border_width;
1210     XGetWindowAttributes(awt_display, window, &winAttr);
1211     *right  = topAttr.width  - ((winAttr.width)  + *left);
1212     *bottom = topAttr.height - ((winAttr.height) + *top);
1213     *border = topAttr.border_width;
1214 }
1215 
1216 JNIEXPORT void JNICALL Java_sun_awt_X11_XWindow_getWMInsets
1217 (JNIEnv *env, jclass clazz, jlong window, jlong left, jlong top, jlong right, jlong bottom, jlong border) {
1218     getWMInsets((Window) window,
1219                 (int*) jlong_to_ptr(left),
1220                 (int*) jlong_to_ptr(top),
1221                 (int*) jlong_to_ptr(right),
1222                 (int*) jlong_to_ptr(bottom),
1223                 (int*) jlong_to_ptr(border));
1224 }
1225 
1226 static void
1227 getWindowBounds
1228 (Window window, int *x, int *y, int *width, int *height) {
1229     XWindowAttributes winAttr;
1230     XSync(awt_display, False);
1231     XGetWindowAttributes(awt_display, window, &winAttr);
1232     *x = winAttr.x;
1233     *y = winAttr.y;
1234     *width = winAttr.width;
1235     *height = winAttr.height;
1236 }
1237 
1238 JNIEXPORT void JNICALL Java_sun_awt_X11_XWindow_getWindowBounds
1239 (JNIEnv *env, jclass clazz, jlong window, jlong x, jlong y, jlong width, jlong height) {
1240     getWindowBounds((Window) window, (int*) jlong_to_ptr(x), (int*) jlong_to_ptr(y),
1241                     (int*) jlong_to_ptr(width), (int*) jlong_to_ptr(height));
1242 }
1243 
1244 JNIEXPORT void JNICALL Java_sun_awt_X11_XWindow_setSizeHints
1245 (JNIEnv *env, jclass clazz, jlong window, jlong x, jlong y, jlong width, jlong height) {
1246     XSizeHints *size_hints = XAllocSizeHints();
1247     size_hints->flags = USPosition | PPosition | PSize;
1248     size_hints->x = (int)x;
1249     size_hints->y = (int)y;
1250     size_hints->width = (int)width;
1251     size_hints->height = (int)height;
1252     XSetWMNormalHints(awt_display, (Window)window, size_hints);
1253     XFree((char*)size_hints);
1254 }
1255 
1256 
1257 JNIEXPORT void JNICALL
1258 Java_sun_awt_X11_XWindow_initIDs
1259   (JNIEnv *env, jclass clazz)
1260 {
1261    char *ptr = NULL;
1262    windowID = (*env)->GetFieldID(env, clazz, "window", "J");
1263    CHECK_NULL(windowID);
1264    targetID = (*env)->GetFieldID(env, clazz, "target", "Ljava/awt/Component;");
1265    CHECK_NULL(targetID);
1266    graphicsConfigID = (*env)->GetFieldID(env, clazz, "graphicsConfig", "Lsun/awt/X11GraphicsConfig;");
1267    CHECK_NULL(graphicsConfigID);
1268    drawStateID = (*env)->GetFieldID(env, clazz, "drawState", "I");
1269    CHECK_NULL(drawStateID);
1270    ptr = getenv("_AWT_USE_TYPE4_PATCH");
1271    if( ptr != NULL && ptr[0] != 0 ) {
1272        if( strncmp("true", ptr, 4) == 0 ) {
1273           awt_UseType4Patch = True;
1274        }else if( strncmp("false", ptr, 5) == 0 ) {
1275           awt_UseType4Patch = False;
1276        }
1277    }
1278 }
1279 
1280 JNIEXPORT jint JNICALL
1281 Java_sun_awt_X11_XWindow_getKeySymForAWTKeyCode(JNIEnv* env, jclass clazz, jint keycode) {
1282     return awt_getX11KeySym(keycode);
1283 }