1 /*
   2  * Copyright (c) 2011, 2019, 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         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 
 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,
 442                            unichar *deadChar)
 443 {
 444     static size_t size = sizeof(keyTable) / sizeof(struct _key);
 445     NSInteger offset;
 446 
 447     if (isDeadChar) {
 448         unichar testDeadChar = NsGetDeadKeyChar(key);
 449         const struct CharToVKEntry *map;
 450         for (map = charToDeadVKTable; map->c != 0; ++map) {
 451             if (testDeadChar == map->c) {
 452                 *keyCode = map->javaKey;
 453                 *postsTyped = NO;
 454                 // TODO: use UNKNOWN here?
 455                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 456                 *deadChar = testDeadChar;
 457                 return;
 458             }
 459         }
 460         // If we got here, we keep looking for a normal key.
 461     }
 462 
 463     if ([[NSCharacterSet letterCharacterSet] characterIsMember:ch]) {
 464         // key is an alphabetic character
 465         unichar lower;
 466         lower = tolower(ch);
 467         offset = lower - 'a';
 468         if (offset >= 0 && offset <= 25) {
 469             // some chars in letter set are NOT actually A-Z characters?!
 470             // skip them...
 471             *postsTyped = YES;
 472             // do quick conversion
 473             *keyCode = java_awt_event_KeyEvent_VK_A + offset;
 474             *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 475             return;
 476         }
 477     }
 478 
 479     if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
 480         // key is a digit
 481         offset = ch - '0';
 482         // make sure in range for decimal digits
 483         if (offset >= 0 && offset <= 9)    {
 484             jboolean numpad = ((flags & NSNumericPadKeyMask) &&
 485                                (key > 81 && key < 93));
 486             *postsTyped = YES;
 487             if (numpad) {
 488                 *keyCode = offset + java_awt_event_KeyEvent_VK_NUMPAD0;
 489                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD;
 490             } else {
 491                 *keyCode = offset + java_awt_event_KeyEvent_VK_0;
 492                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 493             }
 494             return;
 495         }
 496     }
 497 
 498     if (key < size) {
 499         *postsTyped = keyTable[key].postsTyped;
 500         *keyCode = keyTable[key].javaKeyCode;
 501         *keyLocation = keyTable[key].javaKeyLocation;
 502     } else {
 503         // Should we report this? This means we've got a keyboard
 504         // we don't know about...
 505         *postsTyped = NO;
 506         *keyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 507         *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 508     }
 509 }
 510 
 511 /*
 512  * This returns the java key data for the key NSEvent modifiers
 513  * (after NSFlagChanged).
 514  */
 515 static void
 516 NsKeyModifiersToJavaKeyInfo(NSUInteger nsFlags, unsigned short eventKeyCode,
 517                             jint *javaKeyCode,
 518                             jint *javaKeyLocation,
 519                             jint *javaKeyType)
 520 {
 521     static NSUInteger sPreviousNSFlags = 0;
 522 
 523     const struct _nsKeyToJavaModifier* cur;
 524     NSUInteger oldNSFlags = sPreviousNSFlags;
 525     NSUInteger changedNSFlags = oldNSFlags ^ nsFlags;
 526     sPreviousNSFlags = nsFlags;
 527 
 528     *javaKeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 529     *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 530     *javaKeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 531 
 532     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 533         if (changedNSFlags & cur->nsMask) {
 534             *javaKeyCode = cur->javaKey;
 535             *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 536             // TODO: uses SPI...
 537             //if (changedNSFlags & cur->cgsLeftMask) {
 538             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 539             //} else if (changedNSFlags & cur->cgsRightMask) {
 540             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 541             //}
 542             if (eventKeyCode == cur->leftKeyCode) {
 543                 leftAltKeyPressed = YES;
 544                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 545             } else if (eventKeyCode == cur->rightKeyCode) {
 546                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 547             } else if (cur->nsMask == NSAlternateKeyMask) {
 548                 leftAltKeyPressed = NO;
 549                 continue;
 550             }
 551             *javaKeyType = (cur->nsMask & nsFlags) ?
 552             java_awt_event_KeyEvent_KEY_PRESSED :
 553             java_awt_event_KeyEvent_KEY_RELEASED;
 554             break;
 555         }
 556     }
 557 }
 558 
 559 /*
 560  * This returns the java modifiers for a key NSEvent.
 561  */
 562 jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)
 563 {
 564     jint javaModifiers = 0;
 565     const struct _nsKeyToJavaModifier* cur;
 566 
 567     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 568         if ((cur->nsMask & nsFlags) != 0) {
 569             //This code will consider the mask value for left alt as well as
 570             //right alt, but that should be ok, since right alt contains left alt
 571             //mask value.









 572             javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
 573             if (cur->nsMask == NSAlternateKeyMask && leftAltKeyPressed) {
 574                     break; //since right alt key struct is defined last, break out of the loop                }
 575             }
 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     jint testDeadChar = 0;
 706 
 707     NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
 708                                (NSUInteger)modifierFlags, (unsigned short)keyCode,
 709                                &jkeyCode, &jkeyLocation, &postsTyped,
 710                                (unichar *) &testDeadChar);
 711 
 712     // out = [jkeyCode, jkeyLocation, deadChar];
 713     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 714     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 715     (*env)->SetIntArrayRegion(env, outData, 2, 1, &testDeadChar);
 716 
 717     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 718 
 719 JNF_COCOA_EXIT(env);
 720 
 721     return postsTyped;
 722 }
 723 
 724 /*
 725  * Class:     sun_lwawt_macosx_NSEvent
 726  * Method:    nsKeyModifiersToJavaKeyInfo
 727  * Signature: ([I[I)V
 728  */
 729 JNIEXPORT void JNICALL
 730 Java_sun_lwawt_macosx_NSEvent_nsKeyModifiersToJavaKeyInfo
 731 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 732 {
 733 JNF_COCOA_ENTER(env);
 734 
 735     jboolean copy = JNI_FALSE;
 736     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 737     CHECK_NULL(data);
 738 
 739     // in  = [modifierFlags, keyCode]
 740     jint modifierFlags = data[0];
 741     jshort keyCode = (jshort)data[1];
 742 
 743     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 744     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 745     jint jkeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 746 
 747     NsKeyModifiersToJavaKeyInfo(modifierFlags,
 748                                 keyCode,
 749                                 &jkeyCode,
 750                                 &jkeyLocation,
 751                                 &jkeyType);
 752 
 753     // out = [jkeyCode, jkeyLocation, jkeyType];
 754     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 755     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 756     (*env)->SetIntArrayRegion(env, outData, 2, 1, &jkeyType);
 757 
 758     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 759 
 760 JNF_COCOA_EXIT(env);
 761 }
 762 
 763 /*
 764  * Class:     sun_lwawt_macosx_NSEvent
 765  * Method:    nsToJavaChar
 766  * Signature: (CI)C
 767  */
 768 JNIEXPORT jint JNICALL
 769 Java_sun_lwawt_macosx_NSEvent_nsToJavaChar
 770 (JNIEnv *env, jclass cls, jchar nsChar, jint modifierFlags, jboolean spaceKeyTyped)
 771 {
 772     jchar javaChar = 0;
 773 
 774 JNF_COCOA_ENTER(env);
 775 
 776     javaChar = NsCharToJavaChar(nsChar, modifierFlags, spaceKeyTyped);
 777 
 778 JNF_COCOA_EXIT(env);
 779 
 780     return javaChar;
 781 }
--- EOF ---