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_UNDEFINED},
 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         NSCommandKeyMask,
 282         //kCGSFlagsMaskAppleLeftCommandKey,
 283         //kCGSFlagsMaskAppleRightCommandKey,
 284         55,
 285         54,
 286         java_awt_event_InputEvent_META_DOWN_MASK,
 287         java_awt_event_InputEvent_META_MASK,
 288         java_awt_event_KeyEvent_VK_META
 289         },
 290     {
 291         NSAlternateKeyMask,
 292         //kCGSFlagsMaskAppleLeftAlternateKey,
 293         //kCGSFlagsMaskAppleRightAlternateKey,
 294         58,
 295         0,
 296         java_awt_event_InputEvent_ALT_DOWN_MASK,
 297         java_awt_event_InputEvent_ALT_MASK,
 298         java_awt_event_KeyEvent_VK_ALT
 299     },
 300     {
 301         NSAlternateKeyMask,
 302         0,
 303         61,
 304         java_awt_event_InputEvent_ALT_DOWN_MASK | java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK,
 305         java_awt_event_InputEvent_ALT_MASK | java_awt_event_InputEvent_ALT_GRAPH_MASK,
 306         java_awt_event_KeyEvent_VK_ALT | java_awt_event_KeyEvent_VK_ALT_GRAPH
 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 static BOOL altGRPressed = NO;
 323 
 324 /*
 325  * Almost all unicode characters just go from NS to Java with no translation.
 326  *  For the few exceptions, we handle it here with this small table.
 327  */
 328 #define ALL_NS_KEY_MODIFIERS_MASK \
 329     (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)
 330 
 331 static struct _char {
 332     NSUInteger modifier;
 333     unichar nsChar;
 334     unichar javaChar;
 335 }
 336 const charTable[] = {
 337     // map enter on keypad to same as return key
 338     {0,                         NSEnterCharacter,          NSNewlineCharacter},
 339 
 340     // [3134616] return newline instead of carriage return
 341     {0,                         NSCarriageReturnCharacter, NSNewlineCharacter},
 342 
 343     // "delete" means backspace in Java
 344     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteCharacter,         NSBackspaceCharacter},
 345     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteFunctionKey,       NSDeleteCharacter},
 346 
 347     // back-tab is only differentiated from tab by Shift flag
 348     {NSShiftKeyMask,            NSBackTabCharacter,        NSTabCharacter},
 349 
 350     {0, 0, 0}
 351 };
 352 
 353 unichar NsCharToJavaChar(unichar nsChar, NSUInteger modifiers)
 354 {
 355     const struct _char *cur;
 356     // Mask off just the keyboard modifiers from the event modifier mask.
 357     NSUInteger testableFlags = (modifiers & ALL_NS_KEY_MODIFIERS_MASK);
 358 
 359     // walk through table & find the match
 360     for (cur = charTable; cur->nsChar != 0 ; cur++) {
 361         // <rdar://Problem/3476426> Need to determine if we are looking at
 362         // a plain keypress or a modified keypress.  Don't adjust the
 363         // character of a keypress with a modifier.
 364         if (cur->nsChar == nsChar) {
 365             if (cur->modifier == 0 && testableFlags == 0) {
 366                 // If the modifier field is 0, that means to transform
 367                 // this character if no additional keyboard modifiers are set.
 368                 // This lets ctrl-C be reported as ctrl-C and not transformed
 369                 // into Newline.
 370                 return cur->javaChar;
 371             } else if (cur->modifier != 0 &&
 372                        (testableFlags & cur->modifier) == testableFlags)
 373             {
 374                 // Likewise, if the modifier field is nonzero, that means
 375                 // transform this character if only these modifiers are
 376                 // set in the testable flags.
 377                 return cur->javaChar;
 378             }
 379         }
 380     }
 381 
 382     if (nsChar >= NSUpArrowFunctionKey && nsChar <= NSModeSwitchFunctionKey) {
 383         return java_awt_event_KeyEvent_CHAR_UNDEFINED;
 384     }
 385 
 386     // otherwise return character unchanged
 387     return nsChar;
 388 }
 389 
 390 static unichar NsGetDeadKeyChar(unsigned short keyCode)
 391 {
 392     TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
 393     CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
 394     if (uchr == nil) { return 0; }
 395     const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
 396     // Carbon modifiers should be used instead of NSEvent modifiers
 397     UInt32 modifierKeyState = (GetCurrentEventKeyModifiers() >> 8) & 0xFF;
 398 
 399     if (keyboardLayout) {
 400         UInt32 deadKeyState = 0;
 401         UniCharCount maxStringLength = 255;
 402         UniCharCount actualStringLength = 0;
 403         UniChar unicodeString[maxStringLength];
 404 
 405         // get the deadKeyState
 406         OSStatus status = UCKeyTranslate(keyboardLayout,
 407                                          keyCode, kUCKeyActionDown, modifierKeyState,
 408                                          LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit,
 409                                          &deadKeyState,
 410                                          maxStringLength,
 411                                          &actualStringLength, unicodeString);
 412 
 413         if (status == noErr && deadKeyState != 0) {
 414             // Press SPACE to get the dead key char
 415             status = UCKeyTranslate(keyboardLayout,
 416                                     kVK_Space, kUCKeyActionDown, 0,
 417                                     LMGetKbdType(), 0,
 418                                     &deadKeyState,
 419                                     maxStringLength,
 420                                     &actualStringLength, unicodeString);
 421 
 422             if (status == noErr && actualStringLength > 0) {
 423                 return unicodeString[0];
 424             }
 425         }
 426     }
 427     return 0;
 428 }
 429 
 430 /*
 431  * This is the function that uses the table above to take incoming
 432  * NSEvent keyCodes and translate to the Java virtual key code.
 433  */
 434 static void
 435 NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
 436                            NSUInteger flags, unsigned short key,
 437                            jint *keyCode, jint *keyLocation, BOOL *postsTyped, unichar *deadChar)
 438 {
 439     static size_t size = sizeof(keyTable) / sizeof(struct _key);
 440     NSInteger offset;
 441 
 442     if (isDeadChar) {
 443         unichar testDeadChar = NsGetDeadKeyChar(key);
 444         const struct CharToVKEntry *map;
 445         for (map = charToDeadVKTable; map->c != 0; ++map) {
 446             if (testDeadChar == map->c) {
 447                 *keyCode = map->javaKey;
 448                 *postsTyped = NO;
 449                 // TODO: use UNKNOWN here?
 450                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 451                 *deadChar = testDeadChar;
 452                 return;
 453             }
 454         }
 455         // If we got here, we keep looking for a normal key.
 456     }
 457 
 458     if ([[NSCharacterSet letterCharacterSet] characterIsMember:ch]) {
 459         // key is an alphabetic character
 460         unichar lower;
 461         lower = tolower(ch);
 462         offset = lower - 'a';
 463         if (offset >= 0 && offset <= 25) {
 464             // some chars in letter set are NOT actually A-Z characters?!
 465             // skip them...
 466             *postsTyped = YES;
 467             // do quick conversion
 468             *keyCode = java_awt_event_KeyEvent_VK_A + offset;
 469             *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 470             return;
 471         }
 472     }
 473 
 474     if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
 475         // key is a digit
 476         offset = ch - '0';
 477         // make sure in range for decimal digits
 478         if (offset >= 0 && offset <= 9)    {
 479             jboolean numpad = (flags & NSNumericPadKeyMask) != 0;
 480             *postsTyped = YES;
 481             if (numpad) {
 482                 *keyCode = offset + java_awt_event_KeyEvent_VK_NUMPAD0;
 483                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD;
 484             } else {
 485                 *keyCode = offset + java_awt_event_KeyEvent_VK_0;
 486                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 487             }
 488             return;
 489         }
 490     }
 491 
 492     if (key < size) {
 493         *postsTyped = keyTable[key].postsTyped;
 494         *keyCode = keyTable[key].javaKeyCode;
 495         *keyLocation = keyTable[key].javaKeyLocation;
 496     } else {
 497         // Should we report this? This means we've got a keyboard
 498         // we don't know about...
 499         *postsTyped = NO;
 500         *keyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 501         *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 502     }
 503 }
 504 
 505 /*
 506  * This returns the java key data for the key NSEvent modifiers
 507  * (after NSFlagChanged).
 508  */
 509 static void
 510 NsKeyModifiersToJavaKeyInfo(NSUInteger nsFlags, unsigned short eventKeyCode,
 511                             jint *javaKeyCode,
 512                             jint *javaKeyLocation,
 513                             jint *javaKeyType)
 514 {
 515     static NSUInteger sPreviousNSFlags = 0;
 516 
 517     const struct _nsKeyToJavaModifier* cur;
 518     NSUInteger oldNSFlags = sPreviousNSFlags;
 519     NSUInteger changedNSFlags = oldNSFlags ^ nsFlags;
 520     sPreviousNSFlags = nsFlags;
 521 
 522     *javaKeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 523     *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 524     *javaKeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 525 
 526     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 527         if (changedNSFlags & cur->nsMask) {
 528             *javaKeyCode = cur->javaKey;
 529             *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 530             // TODO: uses SPI...
 531             //if (changedNSFlags & cur->cgsLeftMask) {
 532             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 533             //} else if (changedNSFlags & cur->cgsRightMask) {
 534             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 535             //}
 536             if (eventKeyCode == cur->leftKeyCode) {
 537                 leftAltKeyPressed = YES;
 538                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 539             } else if (eventKeyCode == cur->rightKeyCode) {
 540                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 541             } else if (cur->nsMask == NSAlternateKeyMask) {
 542                 leftAltKeyPressed = NO;
 543                 continue;
 544             }
 545             *javaKeyType = (cur->nsMask & nsFlags) ?
 546             java_awt_event_KeyEvent_KEY_PRESSED :
 547             java_awt_event_KeyEvent_KEY_RELEASED;
 548             break;
 549         }
 550     }
 551 }
 552 
 553 /*
 554  * This returns the java modifiers for a key NSEvent.
 555  */
 556 jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)
 557 {
 558         jint javaModifiers = 0;
 559         const struct _nsKeyToJavaModifier* cur;
 560         
 561     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 562         if ((cur->nsMask & nsFlags) != 0) {
 563                         
 564             if (cur->nsMask == NSAlternateKeyMask) {
 565                 if (leftAltKeyPressed == YES) {
 566                     javaModifiers |= isExtMods? cur->javaExtMask : cur->javaMask;
 567                     if (altGRPressed == NO)
 568                         break;
 569                 } else {
 570                     leftAltKeyPressed = YES;
 571                     altGRPressed = YES;
 572                     continue;
 573                 }
 574             }
 575             javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
 576         }
 577     }
 578 
 579     return javaModifiers;
 580 }
 581 
 582 /*
 583  * This returns the NSEvent flags for java key modifiers.
 584  */
 585 NSUInteger JavaModifiersToNsKeyModifiers(jint javaModifiers, BOOL isExtMods)
 586 {
 587     NSUInteger nsFlags = 0;
 588     const struct _nsKeyToJavaModifier* cur;
 589 
 590     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 591         jint mask = isExtMods? cur->javaExtMask : cur->javaMask;
 592         if ((mask & javaModifiers) != 0) {
 593             nsFlags |= cur->nsMask;
 594         }
 595     }
 596 
 597     // special case
 598     jint mask = isExtMods? java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK :
 599                            java_awt_event_InputEvent_ALT_GRAPH_MASK;
 600 
 601     if ((mask & javaModifiers) != 0) {
 602         nsFlags |= NSAlternateKeyMask;
 603     }
 604 
 605     return nsFlags;
 606 }
 607 
 608 
 609 jint GetJavaMouseModifiers(NSUInteger modifierFlags)
 610 {
 611     // Mousing needs the key modifiers
 612     jint modifiers = NsKeyModifiersToJavaModifiers(modifierFlags, YES);
 613 
 614 
 615     /*
 616      * Ask Quartz about mouse buttons state
 617      */
 618 
 619     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 620                                  kCGMouseButtonLeft)) {
 621         modifiers |= java_awt_event_InputEvent_BUTTON1_DOWN_MASK;
 622     }
 623 
 624     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 625                                  kCGMouseButtonRight)) {
 626         modifiers |= java_awt_event_InputEvent_BUTTON3_DOWN_MASK;
 627     }
 628 
 629     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 630                                  kCGMouseButtonCenter)) {
 631         modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
 632     }
 633 
 634     NSInteger extraButton = 3;
 635     for (; extraButton < gNumberOfButtons; extraButton++) {
 636         if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 637                                  extraButton)) {
 638             modifiers |= gButtonDownMasks[extraButton];
 639         }
 640     }
 641 
 642     return modifiers;
 643 }
 644 
 645 jlong UTC(NSEvent *event) {
 646     struct timeval tv;
 647     if (gettimeofday(&tv, NULL) == 0) {
 648         long long sec = (long long)tv.tv_sec;
 649         return (sec*1000) + (tv.tv_usec/1000);
 650     }
 651     return 0;
 652 }
 653 
 654 JNIEXPORT void JNICALL
 655 Java_java_awt_AWTEvent_nativeSetSource
 656     (JNIEnv *env, jobject self, jobject newSource)
 657 {
 658 }
 659 
 660 /*
 661  * Class:     sun_lwawt_macosx_NSEvent
 662  * Method:    nsToJavaModifiers
 663  * Signature: (II)I
 664  */
 665 JNIEXPORT jint JNICALL
 666 Java_sun_lwawt_macosx_NSEvent_nsToJavaModifiers
 667 (JNIEnv *env, jclass cls, jint modifierFlags)
 668 {
 669     jint jmodifiers = 0;
 670 
 671 JNF_COCOA_ENTER(env);
 672 
 673     jmodifiers = GetJavaMouseModifiers(modifierFlags);
 674 
 675 JNF_COCOA_EXIT(env);
 676 
 677     return jmodifiers;
 678 }
 679 
 680 /*
 681  * Class:     sun_lwawt_macosx_NSEvent
 682  * Method:    nsToJavaKeyInfo
 683  * Signature: ([I[I)Z
 684  */
 685 JNIEXPORT jboolean JNICALL
 686 Java_sun_lwawt_macosx_NSEvent_nsToJavaKeyInfo
 687 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 688 {
 689     BOOL postsTyped = NO;
 690 
 691 JNF_COCOA_ENTER(env);
 692 
 693     jboolean copy = JNI_FALSE;
 694     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 695     CHECK_NULL_RETURN(data, postsTyped);
 696 
 697     // in  = [testChar, testDeadChar, modifierFlags, keyCode]
 698     jchar testChar = (jchar)data[0];
 699     BOOL isDeadChar = (data[1] != 0);
 700     jint modifierFlags = data[2];
 701     jshort keyCode = (jshort)data[3];
 702 
 703     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 704     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 705     jchar testDeadChar = 0;
 706 
 707     NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
 708                                (NSUInteger)modifierFlags, (unsigned short)keyCode,
 709                                &jkeyCode, &jkeyLocation, &postsTyped, &testDeadChar);
 710 
 711     // out = [jkeyCode, jkeyLocation];
 712     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 713     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 714     (*env)->SetIntArrayRegion(env, outData, 2, 1, (jint *)&testDeadChar);
 715 
 716     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 717 
 718 JNF_COCOA_EXIT(env);
 719 
 720     return postsTyped;
 721 }
 722 
 723 /*
 724  * Class:     sun_lwawt_macosx_NSEvent
 725  * Method:    nsKeyModifiersToJavaKeyInfo
 726  * Signature: ([I[I)V
 727  */
 728 JNIEXPORT void JNICALL
 729 Java_sun_lwawt_macosx_NSEvent_nsKeyModifiersToJavaKeyInfo
 730 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 731 {
 732 JNF_COCOA_ENTER(env);
 733 
 734     jboolean copy = JNI_FALSE;
 735     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 736     CHECK_NULL(data);
 737 
 738     // in  = [modifierFlags, keyCode]
 739     jint modifierFlags = data[0];
 740     jshort keyCode = (jshort)data[1];
 741 
 742     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 743     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 744     jint jkeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 745 
 746     NsKeyModifiersToJavaKeyInfo(modifierFlags,
 747                                 keyCode,
 748                                 &jkeyCode,
 749                                 &jkeyLocation,
 750                                 &jkeyType);
 751 
 752     // out = [jkeyCode, jkeyLocation, jkeyType];
 753     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 754     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 755     (*env)->SetIntArrayRegion(env, outData, 2, 1, &jkeyType);
 756 
 757     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 758 
 759 JNF_COCOA_EXIT(env);
 760 }
 761 
 762 /*
 763  * Class:     sun_lwawt_macosx_NSEvent
 764  * Method:    nsToJavaChar
 765  * Signature: (CI)C
 766  */
 767 JNIEXPORT jint JNICALL
 768 Java_sun_lwawt_macosx_NSEvent_nsToJavaChar
 769 (JNIEnv *env, jclass cls, jchar nsChar, jint modifierFlags)
 770 {
 771     jchar javaChar = 0;
 772 
 773 JNF_COCOA_ENTER(env);
 774 
 775     javaChar = NsCharToJavaChar(nsChar, modifierFlags);
 776 
 777 JNF_COCOA_EXIT(env);
 778 
 779     return javaChar;
 780 }