1 /*
   2  * Copyright (c) 2011, 2016, 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 #import "java_awt_event_InputEvent.h"
  27 #import "java_awt_event_KeyEvent.h"
  28 #import "LWCToolkit.h"
  29 
  30 #import "jni_util.h"
  31 
  32 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  33 #import <sys/time.h>
  34 #import <Carbon/Carbon.h>
  35 
  36 /*
  37  * Table to map typed characters to their Java virtual key equivalent and back.
  38  * We use the incoming unichar (ignoring all modifiers) and try to figure out
  39  * which virtual key code is appropriate. A lot of them just have direct
  40  * mappings (the function keys, arrow keys, etc.) so they aren't a problem.
  41  * We had to do something a little funky to catch the keys on the numeric
  42  * key pad (i.e. using event mask to distinguish between period on regular
  43  * keyboard and decimal on keypad). We also have to do something incredibly
  44  * hokey with regards to the shifted punctuation characters. For examples,
  45  * consider '&' which is usually Shift-7.  For the Java key typed events,
  46  * that's no problem, we just say pass the unichar. But for the
  47  * KeyPressed/Released events, we need to identify the virtual key code
  48  * (which roughly correspond to hardware keys) which means we are supposed
  49  * to say the virtual 7 key was pressed.  But how are we supposed to know
  50  * when we get a punctuation char what was the real hardware key was that
  51  * was pressed?  Although '&' often comes from Shift-7 the keyboard can be
  52  * remapped!  I don't think there really is a good answer, and hopefully
  53  * all good applets are only interested in logical key typed events not
  54  * press/release.  Meanwhile, we are hard-coding the shifted punctuation
  55  * to trigger the virtual keys that are the expected ones under a standard
  56  * keymapping. Looking at Windows & Mac, they don't actually do this, the
  57  * Mac seems to just put the ascii code in for the shifted punctuation
  58  * (which means they actually end up with bogus key codes on the Java side),
  59  * Windows I can't even figure out what it's doing.
  60  */
  61 #define KL_STANDARD java_awt_event_KeyEvent_KEY_LOCATION_STANDARD
  62 #define KL_NUMPAD   java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD
  63 #define KL_UNKNOWN  java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN
  64 static struct _key
  65 {
  66     unsigned short keyCode;
  67     BOOL postsTyped;
  68     jint javaKeyLocation;
  69     jint javaKeyCode;
  70 }
  71 const keyTable[] =
  72 {
  73     {0x00, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_A},
  74     {0x01, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_S},
  75     {0x02, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_D},
  76     {0x03, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_F},
  77     {0x04, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_H},
  78     {0x05, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_G},
  79     {0x06, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Z},
  80     {0x07, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_X},
  81     {0x08, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_C},
  82     {0x09, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_V},
  83     {0x0A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_QUOTE},
  84     {0x0B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_B},
  85     {0x0C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Q},
  86     {0x0D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_W},
  87     {0x0E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_E},
  88     {0x0F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_R},
  89     {0x10, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Y},
  90     {0x11, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_T},
  91     {0x12, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_1},
  92     {0x13, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_2},
  93     {0x14, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_3},
  94     {0x15, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_4},
  95     {0x16, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_6},
  96     {0x17, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_5},
  97     {0x18, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_EQUALS},
  98     {0x19, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_9},
  99     {0x1A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_7},
 100     {0x1B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_MINUS},
 101     {0x1C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_8},
 102     {0x1D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_0},
 103     {0x1E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_CLOSE_BRACKET},
 104     {0x1F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_O},
 105     {0x20, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_U},
 106     {0x21, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_OPEN_BRACKET},
 107     {0x22, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_I},
 108     {0x23, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_P},
 109     {0x24, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_ENTER},
 110     {0x25, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_L},
 111     {0x26, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_J},
 112     {0x27, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_QUOTE},
 113     {0x28, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_K},
 114     {0x29, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SEMICOLON},
 115     {0x2A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SLASH},
 116     {0x2B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_COMMA},
 117     {0x2C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SLASH},
 118     {0x2D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_N},
 119     {0x2E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_M},
 120     {0x2F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_PERIOD},
 121     {0x30, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_TAB},
 122     {0x31, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SPACE},
 123     {0x32, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_QUOTE},
 124     {0x33, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SPACE},
 125     {0x34, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ENTER},
 126     {0x35, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_ESCAPE},
 127     {0x36, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 128     {0x37, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_META},      // ****
 129     {0x38, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_SHIFT},     // ****
 130     {0x39, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_CAPS_LOCK},
 131     {0x3A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_ALT},       // ****
 132     {0x3B, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_CONTROL},   // ****
 133     {0x3C, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 134     {0x3D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_ALT_GRAPH},
 135     {0x3E, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 136     {0x3F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED}, // the 'fn' key on PowerBooks
 137     {0x40, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F17},
 138     {0x41, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_DECIMAL},
 139     {0x42, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 140     {0x43, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_MULTIPLY},
 141     {0x44, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 142     {0x45, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ADD},
 143     {0x46, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 144     {0x47, NO,  KL_NUMPAD,   java_awt_event_KeyEvent_VK_CLEAR},
 145     {0x48, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 146     {0x49, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 147     {0x4A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 148     {0x4B, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_DIVIDE},
 149     {0x4C, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ENTER},
 150     {0x4D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 151     {0x4E, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_SUBTRACT},
 152     {0x4F, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F18},
 153     {0x50, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F19},
 154     {0x51, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_EQUALS},
 155     {0x52, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD0},
 156     {0x53, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD1},
 157     {0x54, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD2},
 158     {0x55, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD3},
 159     {0x56, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD4},
 160     {0x57, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD5},
 161     {0x58, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD6},
 162     {0x59, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD7},
 163     {0x5A, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F20},
 164     {0x5B, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD8},
 165     {0x5C, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD9},
 166     {0x5D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SLASH}, // This is a combo yen/backslash on JIS keyboards.
 167     {0x5E, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_UNDERSCORE},
 168     {0x5F, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_COMMA},
 169     {0x60, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F5},
 170     {0x61, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F6},
 171     {0x62, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F7},
 172     {0x63, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F3},
 173     {0x64, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F8},
 174     {0x65, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F9},
 175     {0x66, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_ALPHANUMERIC},
 176     {0x67, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F11},
 177     {0x68, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_KATAKANA},
 178     {0x69, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F13},
 179     {0x6A, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F16},
 180     {0x6B, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F14},
 181     {0x6C, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 182     {0x6D, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F10},
 183     {0x6E, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 184     {0x6F, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F12},
 185     {0x70, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 186     {0x71, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F15},
 187     {0x72, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_HELP},
 188     {0x73, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_HOME},
 189     {0x74, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_PAGE_UP},
 190     {0x75, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_DELETE},
 191     {0x76, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F4},
 192     {0x77, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_END},
 193     {0x78, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F2},
 194     {0x79, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_PAGE_DOWN},
 195     {0x7A, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F1},
 196     {0x7B, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_LEFT},
 197     {0x7C, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_RIGHT},
 198     {0x7D, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_DOWN},
 199     {0x7E, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_UP},
 200     {0x7F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 201 };
 202 
 203 /*
 204  * This table was stolen from the Windows implementation for mapping
 205  * Unicode values to VK codes for dead keys.  On Windows, some layouts
 206  * return ASCII punctuation for dead accents, while some return spacing
 207  * accent chars, so both should be listed.  However, in all of the
 208  * keyboard layouts I tried only the Unicode values are used.
 209  */
 210 struct CharToVKEntry {
 211     UniChar c;
 212     jint javaKey;
 213 };
 214 static const struct CharToVKEntry charToDeadVKTable[] = {
 215     {0x0060, java_awt_event_KeyEvent_VK_DEAD_GRAVE},
 216     {0x00B4, java_awt_event_KeyEvent_VK_DEAD_ACUTE},
 217     {0x0384, java_awt_event_KeyEvent_VK_DEAD_ACUTE}, // Unicode "GREEK TONOS" -- Greek keyboard, semicolon key
 218     {0x005E, java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX},
 219     {0x007E, java_awt_event_KeyEvent_VK_DEAD_TILDE},
 220     {0x02DC, java_awt_event_KeyEvent_VK_DEAD_TILDE}, // Unicode "SMALL TILDE"
 221     {0x00AF, java_awt_event_KeyEvent_VK_DEAD_MACRON},
 222     {0x02D8, java_awt_event_KeyEvent_VK_DEAD_BREVE},
 223     {0x02D9, java_awt_event_KeyEvent_VK_DEAD_ABOVEDOT},
 224     {0x00A8, java_awt_event_KeyEvent_VK_DEAD_DIAERESIS},
 225     {0x02DA, java_awt_event_KeyEvent_VK_DEAD_ABOVERING},
 226     {0x02DD, java_awt_event_KeyEvent_VK_DEAD_DOUBLEACUTE},
 227     {0x02C7, java_awt_event_KeyEvent_VK_DEAD_CARON},
 228     {0x00B8, java_awt_event_KeyEvent_VK_DEAD_CEDILLA},
 229     {0x02DB, java_awt_event_KeyEvent_VK_DEAD_OGONEK},
 230     {0x037A, java_awt_event_KeyEvent_VK_DEAD_IOTA},
 231     {0x309B, java_awt_event_KeyEvent_VK_DEAD_VOICED_SOUND},
 232     {0x309C, java_awt_event_KeyEvent_VK_DEAD_SEMIVOICED_SOUND},
 233     {0,0}
 234 };
 235 
 236 // TODO: some constants below are part of CGS (private interfaces)...
 237 // for now we will look at the raw key code to determine left/right status
 238 // but not sure this is foolproof...
 239 static struct _nsKeyToJavaModifier
 240 {
 241     NSUInteger nsMask;
 242     //NSUInteger cgsLeftMask;
 243     //NSUInteger cgsRightMask;
 244     unsigned short leftKeyCode;
 245     unsigned short rightKeyCode;
 246     jint javaExtMask;
 247     jint javaMask;
 248     jint javaKey;
 249 }
 250 const nsKeyToJavaModifierTable[] =
 251 {
 252     {
 253         NSAlphaShiftKeyMask,
 254         0,
 255         0,
 256         0, // no Java equivalent
 257         0, // no Java equivalent
 258         java_awt_event_KeyEvent_VK_CAPS_LOCK
 259     },
 260     {
 261         NSShiftKeyMask,
 262         //kCGSFlagsMaskAppleShiftKey,
 263         //kCGSFlagsMaskAppleRightShiftKey,
 264         56,
 265         60,
 266         java_awt_event_InputEvent_SHIFT_DOWN_MASK,
 267         java_awt_event_InputEvent_SHIFT_MASK,
 268         java_awt_event_KeyEvent_VK_SHIFT
 269     },
 270     {
 271         NSControlKeyMask,
 272         //kCGSFlagsMaskAppleControlKey,
 273         //kCGSFlagsMaskAppleRightControlKey,
 274         59,
 275         62,
 276         java_awt_event_InputEvent_CTRL_DOWN_MASK,
 277         java_awt_event_InputEvent_CTRL_MASK,
 278         java_awt_event_KeyEvent_VK_CONTROL
 279     },
 280     {
 281         NSAlternateKeyMask,
 282         //kCGSFlagsMaskAppleLeftAlternateKey,
 283         //kCGSFlagsMaskAppleRightAlternateKey,
 284         58,
 285         0,
 286         java_awt_event_InputEvent_ALT_DOWN_MASK,
 287         java_awt_event_InputEvent_ALT_MASK,
 288         java_awt_event_KeyEvent_VK_ALT
 289     },
 290     {
 291         NSAlternateKeyMask,
 292         0,
 293         61,
 294         java_awt_event_InputEvent_ALT_DOWN_MASK | java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK,
 295         java_awt_event_InputEvent_ALT_MASK | java_awt_event_InputEvent_ALT_GRAPH_MASK,
 296         java_awt_event_KeyEvent_VK_ALT | java_awt_event_KeyEvent_VK_ALT_GRAPH
 297     },
 298         {
 299                 NSCommandKeyMask,
 300                 //kCGSFlagsMaskAppleLeftCommandKey,
 301                 //kCGSFlagsMaskAppleRightCommandKey,
 302                 55,
 303                 54,
 304                 java_awt_event_InputEvent_META_DOWN_MASK,
 305                 java_awt_event_InputEvent_META_MASK,
 306                 java_awt_event_KeyEvent_VK_META
 307         },
 308     // NSNumericPadKeyMask
 309     {
 310         NSHelpKeyMask,
 311         0,
 312         0,
 313         0, // no Java equivalent
 314         0, // no Java equivalent
 315         java_awt_event_KeyEvent_VK_HELP
 316     },
 317     // NSFunctionKeyMask
 318     {0, 0, 0, 0, 0, 0}
 319 };
 320 
 321 static BOOL leftAltKeyPressed;
 322 
 323 /*
 324  * Almost all unicode characters just go from NS to Java with no translation.
 325  *  For the few exceptions, we handle it here with this small table.
 326  */
 327 #define ALL_NS_KEY_MODIFIERS_MASK \
 328     (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)
 329 
 330 static struct _char {
 331     NSUInteger modifier;
 332     unichar nsChar;
 333     unichar javaChar;
 334 }
 335 const charTable[] = {
 336     // map enter on keypad to same as return key
 337     {0,                         NSEnterCharacter,          NSNewlineCharacter},
 338 
 339     // [3134616] return newline instead of carriage return
 340     {0,                         NSCarriageReturnCharacter, NSNewlineCharacter},
 341 
 342     // "delete" means backspace in Java
 343     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteCharacter,         NSBackspaceCharacter},
 344     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteFunctionKey,       NSDeleteCharacter},
 345 
 346     // back-tab is only differentiated from tab by Shift flag
 347     {NSShiftKeyMask,            NSBackTabCharacter,        NSTabCharacter},
 348 
 349     {0, 0, 0}
 350 };
 351 
 352 unichar NsCharToJavaChar(unichar nsChar, NSUInteger modifiers, BOOL spaceKeyTyped)
 353 {
 354     const struct _char *cur;
 355     // Mask off just the keyboard modifiers from the event modifier mask.
 356     NSUInteger testableFlags = (modifiers & ALL_NS_KEY_MODIFIERS_MASK);
 357 
 358     // walk through table & find the match
 359     for (cur = charTable; cur->nsChar != 0 ; cur++) {
 360         // <rdar://Problem/3476426> Need to determine if we are looking at
 361         // a plain keypress or a modified keypress.  Don't adjust the
 362         // character of a keypress with a modifier.
 363         if (cur->nsChar == nsChar) {
 364             if (cur->modifier == 0 && testableFlags == 0) {
 365                 // If the modifier field is 0, that means to transform
 366                 // this character if no additional keyboard modifiers are set.
 367                 // This lets ctrl-C be reported as ctrl-C and not transformed
 368                 // into Newline.
 369                 return cur->javaChar;
 370             } else if (cur->modifier != 0 &&
 371                        (testableFlags & cur->modifier) == testableFlags)
 372             {
 373                 // Likewise, if the modifier field is nonzero, that means
 374                 // transform this character if only these modifiers are
 375                 // set in the testable flags.
 376                 return cur->javaChar;
 377             }
 378         }
 379     }
 380 
 381     if (nsChar >= NSUpArrowFunctionKey && nsChar <= NSModeSwitchFunctionKey) {
 382         return java_awt_event_KeyEvent_CHAR_UNDEFINED;
 383     }
 384 
 385     // nsChar receives value 0 when SPACE key is typed.
 386     if (nsChar == 0 && spaceKeyTyped == YES) {
 387         return java_awt_event_KeyEvent_VK_SPACE;
 388     }
 389         
 390     // otherwise return character unchanged
 391     return nsChar;
 392 }
 393 
 394 static unichar NsGetDeadKeyChar(unsigned short keyCode)
 395 {
 396     TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
 397     CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
 398     if (uchr == nil) { return 0; }
 399     const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
 400     // Carbon modifiers should be used instead of NSEvent modifiers
 401     UInt32 modifierKeyState = (GetCurrentEventKeyModifiers() >> 8) & 0xFF;
 402 
 403     if (keyboardLayout) {
 404         UInt32 deadKeyState = 0;
 405         UniCharCount maxStringLength = 255;
 406         UniCharCount actualStringLength = 0;
 407         UniChar unicodeString[maxStringLength];
 408 
 409         // get the deadKeyState
 410         OSStatus status = UCKeyTranslate(keyboardLayout,
 411                                          keyCode, kUCKeyActionDown, modifierKeyState,
 412                                          LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit,
 413                                          &deadKeyState,
 414                                          maxStringLength,
 415                                          &actualStringLength, unicodeString);
 416 
 417         if (status == noErr && deadKeyState != 0) {
 418             // Press SPACE to get the dead key char
 419             status = UCKeyTranslate(keyboardLayout,
 420                                     kVK_Space, kUCKeyActionDown, 0,
 421                                     LMGetKbdType(), 0,
 422                                     &deadKeyState,
 423                                     maxStringLength,
 424                                     &actualStringLength, unicodeString);
 425 
 426             if (status == noErr && actualStringLength > 0) {
 427                 return unicodeString[0];
 428             }
 429         }
 430     }
 431     return 0;
 432 }
 433 
 434 /*
 435  * This is the function that uses the table above to take incoming
 436  * NSEvent keyCodes and translate to the Java virtual key code.
 437  */
 438 static void
 439 NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
 440                            NSUInteger flags, unsigned short key,
 441                            jint *keyCode, jint *keyLocation, BOOL *postsTyped, unichar *deadChar)
 442 {
 443     static size_t size = sizeof(keyTable) / sizeof(struct _key);
 444     NSInteger offset;
 445 
 446     if (isDeadChar) {
 447         unichar testDeadChar = NsGetDeadKeyChar(key);
 448         const struct CharToVKEntry *map;
 449         for (map = charToDeadVKTable; map->c != 0; ++map) {
 450             if (testDeadChar == map->c) {
 451                 *keyCode = map->javaKey;
 452                 *postsTyped = NO;
 453                 // TODO: use UNKNOWN here?
 454                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 455                 *deadChar = testDeadChar;
 456                 return;
 457             }
 458         }
 459         // If we got here, we keep looking for a normal key.
 460     }
 461 
 462     if ([[NSCharacterSet letterCharacterSet] characterIsMember:ch]) {
 463         // key is an alphabetic character
 464         unichar lower;
 465         lower = tolower(ch);
 466         offset = lower - 'a';
 467         if (offset >= 0 && offset <= 25) {
 468             // some chars in letter set are NOT actually A-Z characters?!
 469             // skip them...
 470             *postsTyped = YES;
 471             // do quick conversion
 472             *keyCode = java_awt_event_KeyEvent_VK_A + offset;
 473             *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 474             return;
 475         }
 476     }
 477 
 478     if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
 479         // key is a digit
 480         offset = ch - '0';
 481         // make sure in range for decimal digits
 482         if (offset >= 0 && offset <= 9)    {
 483             jboolean numpad = (flags & NSNumericPadKeyMask) != 0;
 484             *postsTyped = YES;
 485             if (numpad) {
 486                 *keyCode = offset + java_awt_event_KeyEvent_VK_NUMPAD0;
 487                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD;
 488             } else {
 489                 *keyCode = offset + java_awt_event_KeyEvent_VK_0;
 490                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 491             }
 492             return;
 493         }
 494     }
 495 
 496     if (key < size) {
 497         *postsTyped = keyTable[key].postsTyped;
 498         *keyCode = keyTable[key].javaKeyCode;
 499         *keyLocation = keyTable[key].javaKeyLocation;
 500     } else {
 501         // Should we report this? This means we've got a keyboard
 502         // we don't know about...
 503         *postsTyped = NO;
 504         *keyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 505         *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 506     }
 507 }
 508 
 509 /*
 510  * This returns the java key data for the key NSEvent modifiers
 511  * (after NSFlagChanged).
 512  */
 513 static void
 514 NsKeyModifiersToJavaKeyInfo(NSUInteger nsFlags, unsigned short eventKeyCode,
 515                             jint *javaKeyCode,
 516                             jint *javaKeyLocation,
 517                             jint *javaKeyType)
 518 {
 519     static NSUInteger sPreviousNSFlags = 0;
 520 
 521     const struct _nsKeyToJavaModifier* cur;
 522     NSUInteger oldNSFlags = sPreviousNSFlags;
 523     NSUInteger changedNSFlags = oldNSFlags ^ nsFlags;
 524     sPreviousNSFlags = nsFlags;
 525 
 526     *javaKeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 527     *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 528     *javaKeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 529 
 530     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 531         if (changedNSFlags & cur->nsMask) {
 532             *javaKeyCode = cur->javaKey;
 533             *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 534             // TODO: uses SPI...
 535             //if (changedNSFlags & cur->cgsLeftMask) {
 536             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 537             //} else if (changedNSFlags & cur->cgsRightMask) {
 538             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 539             //}
 540             if (eventKeyCode == cur->leftKeyCode) {
 541                 leftAltKeyPressed = YES;
 542                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 543             } else if (eventKeyCode == cur->rightKeyCode) {
 544                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 545             } else if (cur->nsMask == NSAlternateKeyMask) {
 546                 leftAltKeyPressed = NO;
 547                 continue;
 548             }
 549             *javaKeyType = (cur->nsMask & nsFlags) ?
 550             java_awt_event_KeyEvent_KEY_PRESSED :
 551             java_awt_event_KeyEvent_KEY_RELEASED;
 552             break;
 553         }
 554     }
 555 }
 556 
 557 /*
 558  * This returns the java modifiers for a key NSEvent.
 559  */
 560 jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)
 561 {
 562         jint javaModifiers = 0;
 563         const struct _nsKeyToJavaModifier* cur;
 564         
 565         for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 566                 if ((cur->nsMask & nsFlags) != 0) {
 567                                 javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
 568                                 if (cur->nsMask == NSAlternateKeyMask && leftAltKeyPressed == NO) {
 569                                         continue;
 570                         }
 571                         break;
 572                 }
 573         }
 574 
 575         return javaModifiers;
 576 }
 577 
 578 /*
 579  * This returns the NSEvent flags for java key modifiers.
 580  */
 581 NSUInteger JavaModifiersToNsKeyModifiers(jint javaModifiers, BOOL isExtMods)
 582 {
 583     NSUInteger nsFlags = 0;
 584     const struct _nsKeyToJavaModifier* cur;
 585 
 586     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 587         jint mask = isExtMods? cur->javaExtMask : cur->javaMask;
 588         if ((mask & javaModifiers) != 0) {
 589             nsFlags |= cur->nsMask;
 590         }
 591     }
 592 
 593     // special case
 594     jint mask = isExtMods? java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK :
 595                            java_awt_event_InputEvent_ALT_GRAPH_MASK;
 596 
 597     if ((mask & javaModifiers) != 0) {
 598         nsFlags |= NSAlternateKeyMask;
 599     }
 600 
 601     return nsFlags;
 602 }
 603 
 604 
 605 jint GetJavaMouseModifiers(NSUInteger modifierFlags)
 606 {
 607     // Mousing needs the key modifiers
 608     jint modifiers = NsKeyModifiersToJavaModifiers(modifierFlags, YES);
 609 
 610 
 611     /*
 612      * Ask Quartz about mouse buttons state
 613      */
 614 
 615     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 616                                  kCGMouseButtonLeft)) {
 617         modifiers |= java_awt_event_InputEvent_BUTTON1_DOWN_MASK;
 618     }
 619 
 620     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 621                                  kCGMouseButtonRight)) {
 622         modifiers |= java_awt_event_InputEvent_BUTTON3_DOWN_MASK;
 623     }
 624 
 625     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 626                                  kCGMouseButtonCenter)) {
 627         modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
 628     }
 629 
 630     NSInteger extraButton = 3;
 631     for (; extraButton < gNumberOfButtons; extraButton++) {
 632         if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 633                                  extraButton)) {
 634             modifiers |= gButtonDownMasks[extraButton];
 635         }
 636     }
 637 
 638     return modifiers;
 639 }
 640 
 641 jlong UTC(NSEvent *event) {
 642     struct timeval tv;
 643     if (gettimeofday(&tv, NULL) == 0) {
 644         long long sec = (long long)tv.tv_sec;
 645         return (sec*1000) + (tv.tv_usec/1000);
 646     }
 647     return 0;
 648 }
 649 
 650 JNIEXPORT void JNICALL
 651 Java_java_awt_AWTEvent_nativeSetSource
 652     (JNIEnv *env, jobject self, jobject newSource)
 653 {
 654 }
 655 
 656 /*
 657  * Class:     sun_lwawt_macosx_NSEvent
 658  * Method:    nsToJavaModifiers
 659  * Signature: (II)I
 660  */
 661 JNIEXPORT jint JNICALL
 662 Java_sun_lwawt_macosx_NSEvent_nsToJavaModifiers
 663 (JNIEnv *env, jclass cls, jint modifierFlags)
 664 {
 665     jint jmodifiers = 0;
 666 
 667 JNF_COCOA_ENTER(env);
 668 
 669     jmodifiers = GetJavaMouseModifiers(modifierFlags);
 670 
 671 JNF_COCOA_EXIT(env);
 672 
 673     return jmodifiers;
 674 }
 675 
 676 /*
 677  * Class:     sun_lwawt_macosx_NSEvent
 678  * Method:    nsToJavaKeyInfo
 679  * Signature: ([I[I)Z
 680  */
 681 JNIEXPORT jboolean JNICALL
 682 Java_sun_lwawt_macosx_NSEvent_nsToJavaKeyInfo
 683 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 684 {
 685     BOOL postsTyped = NO;
 686 
 687 JNF_COCOA_ENTER(env);
 688 
 689     jboolean copy = JNI_FALSE;
 690     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 691     CHECK_NULL_RETURN(data, postsTyped);
 692 
 693     // in  = [testChar, testDeadChar, modifierFlags, keyCode]
 694     jchar testChar = (jchar)data[0];
 695     BOOL isDeadChar = (data[1] != 0);
 696     jint modifierFlags = data[2];
 697     jshort keyCode = (jshort)data[3];
 698 
 699     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 700     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 701     jchar testDeadChar = 0;
 702 
 703     NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
 704                                (NSUInteger)modifierFlags, (unsigned short)keyCode,
 705                                &jkeyCode, &jkeyLocation, &postsTyped, &testDeadChar);
 706 
 707     // out = [jkeyCode, jkeyLocation];
 708     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 709     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 710     (*env)->SetIntArrayRegion(env, outData, 2, 1, (jint *)&testDeadChar);
 711 
 712     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 713 
 714 JNF_COCOA_EXIT(env);
 715 
 716     return postsTyped;
 717 }
 718 
 719 /*
 720  * Class:     sun_lwawt_macosx_NSEvent
 721  * Method:    nsKeyModifiersToJavaKeyInfo
 722  * Signature: ([I[I)V
 723  */
 724 JNIEXPORT void JNICALL
 725 Java_sun_lwawt_macosx_NSEvent_nsKeyModifiersToJavaKeyInfo
 726 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 727 {
 728 JNF_COCOA_ENTER(env);
 729 
 730     jboolean copy = JNI_FALSE;
 731     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 732     CHECK_NULL(data);
 733 
 734     // in  = [modifierFlags, keyCode]
 735     jint modifierFlags = data[0];
 736     jshort keyCode = (jshort)data[1];
 737 
 738     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 739     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 740     jint jkeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 741 
 742     NsKeyModifiersToJavaKeyInfo(modifierFlags,
 743                                 keyCode,
 744                                 &jkeyCode,
 745                                 &jkeyLocation,
 746                                 &jkeyType);
 747 
 748     // out = [jkeyCode, jkeyLocation, jkeyType];
 749     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 750     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 751     (*env)->SetIntArrayRegion(env, outData, 2, 1, &jkeyType);
 752 
 753     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 754 
 755 JNF_COCOA_EXIT(env);
 756 }
 757 
 758 /*
 759  * Class:     sun_lwawt_macosx_NSEvent
 760  * Method:    nsToJavaChar
 761  * Signature: (CI)C
 762  */
 763 JNIEXPORT jint JNICALL
 764 Java_sun_lwawt_macosx_NSEvent_nsToJavaChar
 765 (JNIEnv *env, jclass cls, jchar nsChar, jint modifierFlags, jboolean spaceKeyTyped)
 766 {
 767     jchar javaChar = 0;
 768 
 769 JNF_COCOA_ENTER(env);
 770 
 771     javaChar = NsCharToJavaChar(nsChar, modifierFlags, spaceKeyTyped);
 772 
 773 JNF_COCOA_EXIT(env);
 774 
 775     return javaChar;
 776 }