diff options
-rw-r--r-- | make/build-newt.xml | 5 | ||||
-rw-r--r-- | src/newt/classes/com/jogamp/newt/event/UTFKeyUtil.java | 82 | ||||
-rw-r--r-- | src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java | 21 | ||||
-rw-r--r-- | src/newt/native/KeyEvent.h | 3 | ||||
-rw-r--r-- | src/newt/native/NewtMacWindow.m | 77 |
5 files changed, 175 insertions, 13 deletions
diff --git a/make/build-newt.xml b/make/build-newt.xml index 864e0f93e..c601bc120 100644 --- a/make/build-newt.xml +++ b/make/build-newt.xml @@ -364,6 +364,8 @@ <linkerarg value="-weak_framework" /> <linkerarg value="AppKit" /> <linkerarg value="-weak_framework" /> + <linkerarg value="Carbon" /> + <linkerarg value="-weak_framework" /> <linkerarg value="Cocoa" /> </linker> @@ -583,7 +585,8 @@ <include name="${rootrel.src.c}/NewtCommon.c" /> <!-- include name="${rootrel.src.c}/timespec.c" /--> <!-- currently used for X11 and OSX with special PERF DEBUG MODE--> <include name="${rootrel.src.c}/WindowsWindow.c" if="isWindows"/> - <include name="${rootrel.src.c}/*.m" if="isOSX"/> + <include name="${rootrel.src.c}/MacWindow.m" if="isOSX"/> + <include name="${rootrel.src.c}/NewtMacWindow.m" if="isOSX"/> <include name="${rootrel.src.c}/AndroidWindow.c" if="isAndroid"/> <include name="${rootrel.src.c}/X11Display.c" if="isX11"/> <!-- include name="${rootrel.src.c}/X11Event.c" if="isX11"/--> diff --git a/src/newt/classes/com/jogamp/newt/event/UTFKeyUtil.java b/src/newt/classes/com/jogamp/newt/event/UTFKeyUtil.java new file mode 100644 index 000000000..73afa0993 --- /dev/null +++ b/src/newt/classes/com/jogamp/newt/event/UTFKeyUtil.java @@ -0,0 +1,82 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.newt.event; + +import com.jogamp.newt.event.KeyEvent; + +public class UTFKeyUtil { + + // + // UTF Key Constants + // + private static final char UTF_Equal = '='; + private static final char UTF_Minus = '-'; + private static final char UTF_RightBracket = ']'; + private static final char UTF_LeftBracket = '['; + private static final char UTF_Quote = '\''; + private static final char UTF_Semicolon = ';'; + private static final char UTF_Backslash = '\\'; + private static final char UTF_Comma = ','; + private static final char UTF_Slash = '/'; + private static final char UTF_Period = '.'; + private static final char UTF_Grave = '`'; // back quote + + /** + * @param keyChar UTF16 value to map. Note: Lower case values are preferred. + * @return {@link KeyEvent} virtual key (VK) value if possible, + * otherwise simply the UTF16 value of type short as a last resort. + */ + public static short utf16ToVKey(char keyChar) { + if( 'a' <= keyChar && keyChar <= 'z' ) { + return (short) ( ( keyChar - 'a' ) + KeyEvent.VK_A ); + } + if( '0' <= keyChar && keyChar <= '9' ) { + return (short) ( ( keyChar - '0' ) + KeyEvent.VK_0 ); + } + switch(keyChar) { + // + // KeyCodes (Layout Dependent) + // + case UTF_Equal: return KeyEvent.VK_EQUALS; + case UTF_Minus: return KeyEvent.VK_MINUS; + case UTF_RightBracket: return KeyEvent.VK_CLOSE_BRACKET; + case UTF_LeftBracket: return KeyEvent.VK_OPEN_BRACKET; + case UTF_Quote: return KeyEvent.VK_QUOTE; + case UTF_Semicolon: return KeyEvent.VK_SEMICOLON; + case UTF_Backslash: return KeyEvent.VK_BACK_SLASH; + case UTF_Comma: return KeyEvent.VK_COMMA; + case UTF_Slash: return KeyEvent.VK_SLASH; + case UTF_Period: return KeyEvent.VK_PERIOD; + case UTF_Grave: return KeyEvent.VK_BACK_QUOTE; // KeyEvent.VK_DEAD_GRAVE + } + if( 'A' <= keyChar && keyChar <= 'Z' ) { + return (short) ( ( keyChar - 'A' ) + KeyEvent.VK_A ); + } + return (short) keyChar; + } +} diff --git a/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java b/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java index 965138ddf..b1d18e487 100644 --- a/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java +++ b/src/newt/classes/jogamp/newt/driver/macosx/WindowDriver.java @@ -51,6 +51,7 @@ import jogamp.newt.driver.DriverUpdatePosition; import com.jogamp.newt.event.InputEvent; import com.jogamp.newt.event.KeyEvent; +import com.jogamp.newt.event.UTFKeyUtil; public class WindowDriver extends WindowImpl implements MutableSurface, DriverClearFocus, DriverUpdatePosition { @@ -401,13 +402,25 @@ public class WindowDriver extends WindowImpl implements MutableSurface, DriverCl } @Override - public final void enqueueKeyEvent(boolean wait, short eventType, int modifiers, short _keyCode, short keySym, char keyChar) { + public final void enqueueKeyEvent(boolean wait, short eventType, int modifiers, short _keyCode, short _keySym, char keyChar) { + throw new InternalError("XXX: Adapt Java Code to Native Code Changes"); + } + + protected final void enqueueKeyEvent(boolean wait, short eventType, int modifiers, short _keyCode, char keyChar, char keySymChar) { // Note that we send the key char for the key code on this // platform -- we do not get any useful key codes out of the system final short keyCode = MacKeyUtil.validateKeyCode(_keyCode, keyChar); + final short keySym; + { + short _keySym = KeyEvent.NULL_CHAR != keySymChar ? UTFKeyUtil.utf16ToVKey(keySymChar) : KeyEvent.VK_UNDEFINED; + keySym = KeyEvent.VK_UNDEFINED != _keySym ? _keySym : keyCode; + } /* { final boolean isModifierKeyCode = KeyEvent.isModifierKey(keyCode); - System.err.println("*** handleKeyEvent: event "+KeyEvent.getEventTypeString(eventType)+", key 0x"+Integer.toHexString(_keyCode)+" -> 0x"+Integer.toHexString(keyCode)+", mods "+toHexString(modifiers)+ + System.err.println("*** handleKeyEvent: event "+KeyEvent.getEventTypeString(eventType)+ + ", keyCode 0x"+Integer.toHexString(_keyCode)+" -> 0x"+Integer.toHexString(keyCode)+ + ", keySymChar '"+keySymChar+"', 0x"+Integer.toHexString(keySymChar)+" -> 0x"+Integer.toHexString(keySym)+ + ", mods "+toHexString(modifiers)+ ", was: pressed "+isKeyPressed(keyCode)+", repeat "+isKeyInAutoRepeat(keyCode)+", isModifierKeyCode "+isModifierKeyCode); } */ @@ -426,12 +439,12 @@ public class WindowDriver extends WindowImpl implements MutableSurface, DriverCl // key was already pressed keyRepeatState.put(keyCode, true); // prev == false -> AR in modifiers |= InputEvent.AUTOREPEAT_MASK; - super.enqueueKeyEvent(wait, KeyEvent.EVENT_KEY_RELEASED, modifiers, keyCode, keyCode, keyChar); // RELEASED + super.enqueueKeyEvent(wait, KeyEvent.EVENT_KEY_RELEASED, modifiers, keyCode, keySym, keyChar); // RELEASED } } break; } - super.enqueueKeyEvent(wait, eventType, modifiers, keyCode, keyCode, keyChar); + super.enqueueKeyEvent(wait, eventType, modifiers, keyCode, keySym, keyChar); } //---------------------------------------------------------------------- diff --git a/src/newt/native/KeyEvent.h b/src/newt/native/KeyEvent.h index 0f7b1606b..946b3c904 100644 --- a/src/newt/native/KeyEvent.h +++ b/src/newt/native/KeyEvent.h @@ -33,7 +33,6 @@ #define EVENT_KEY_RELEASED 301 #define EVENT_KEY_TYPED 302 -#define J_CHAR_UNDEFINED 0xFFFF; #define J_VK_ENTER '\n' #define J_VK_BACK_SPACE '\b' #define J_VK_TAB '\t' @@ -221,7 +220,7 @@ #define J_VK_COMPOSE 0xFF20 #define J_VK_ALT_GRAPH 0xFF7E #define J_VK_BEGIN 0xFF58 -#define J_VK_UNDEFINED 0x0 +#define J_VK_UNDEFINED 0x0 #endif diff --git a/src/newt/native/NewtMacWindow.m b/src/newt/native/NewtMacWindow.m index a396c104c..005e82d72 100644 --- a/src/newt/native/NewtMacWindow.m +++ b/src/newt/native/NewtMacWindow.m @@ -36,6 +36,9 @@ #import "KeyEvent.h" #import "MouseEvent.h" +#include <CoreFoundation/CoreFoundation.h> +#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */ + #include <math.h> static jfloat GetDelta(NSEvent *event, jint javaMods[]) { @@ -350,12 +353,71 @@ static jmethodID windowRepaintID = NULL; @end +static CFStringRef CKCH_CreateStringForKey(CGKeyCode keyCode, const UCKeyboardLayout *keyboardLayout) { + UInt32 keysDown = 0; + UniChar chars[4]; + UniCharCount realLength; + + UCKeyTranslate(keyboardLayout, keyCode, + kUCKeyActionDisplay, 0, + LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit, + &keysDown, sizeof(chars) / sizeof(chars[0]), &realLength, chars); + + return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1); +} + +static CFMutableDictionaryRef CKCH_CreateCodeToCharDict(TISInputSourceRef keyboard) { + CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData); + const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData); + + CFMutableDictionaryRef codeToCharDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 128, NULL, NULL); + if ( NULL != codeToCharDict ) { + intptr_t i; + for (i = 0; i < 128; ++i) { + CFStringRef string = CKCH_CreateStringForKey((CGKeyCode)i, keyboardLayout); + if( NULL != string ) { + CFIndex stringLen = CFStringGetLength (string); + if ( 0 < stringLen ) { + UniChar character = CFStringGetCharacterAtIndex(string, 0); + DBG_PRINT("CKCH: MAP 0x%X -> %c\n", (int)i, character); + CFDictionaryAddValue(codeToCharDict, (const void *)i, (const void *)(intptr_t)character); + } + CFRelease(string); + } + } + } + return codeToCharDict; +} + +static CFMutableDictionaryRef CKCH_USCodeToNNChar = NULL; + +static void CKCH_CreateDictionaries() { + TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); + CKCH_USCodeToNNChar = CKCH_CreateCodeToCharDict(currentKeyboard); + CFRelease(currentKeyboard); +} + +static UniChar CKCH_CharForKeyCode(jshort keyCode) { + UniChar rChar = 0; + + if ( NULL != CKCH_USCodeToNNChar ) { + intptr_t code = (intptr_t) keyCode; + intptr_t character = 0; + + if ( CFDictionaryGetValueIfPresent(CKCH_USCodeToNNChar, (void *)code, (const void **)&character) ) { + rChar = (UniChar) character; + DBG_PRINT("CKCH: OK 0x%X -> 0x%X\n", (int)keyCode, (int)rChar); + } + } + return rChar; +} + @implementation NewtMacWindow + (BOOL) initNatives: (JNIEnv*) env forClass: (jclass) clazz { enqueueMouseEventID = (*env)->GetMethodID(env, clazz, "enqueueMouseEvent", "(ZSIIISF)V"); - enqueueKeyEventID = (*env)->GetMethodID(env, clazz, "enqueueKeyEvent", "(ZSISSC)V"); + enqueueKeyEventID = (*env)->GetMethodID(env, clazz, "enqueueKeyEvent", "(ZSISCC)V"); sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(ZIIZ)V"); visibleChangedID = (*env)->GetMethodID(env, clazz, "visibleChanged", "(ZZ)V"); insetsChangedID = (*env)->GetMethodID(env, clazz, "insetsChanged", "(ZIIII)V"); @@ -367,6 +429,7 @@ static jmethodID windowRepaintID = NULL; if (enqueueMouseEventID && enqueueKeyEventID && sizeChangedID && visibleChangedID && insetsChangedID && positionChangedID && focusChangedID && windowDestroyNotifyID && requestFocusID && windowRepaintID) { + CKCH_CreateDictionaries(); return YES; } return NO; @@ -683,21 +746,22 @@ static jint mods2JavaMods(NSUInteger mods) // printable chars for (i = 0; i < len; i++) { // Note: the key code in the NSEvent does not map to anything we can use - jchar keyChar = (jchar) [chars characterAtIndex: i]; + UniChar keyChar = (UniChar) [chars characterAtIndex: i]; + UniChar keySymChar = CKCH_CharForKeyCode(keyCode); - DBG_PRINT("sendKeyEvent: %d/%d char 0x%X, code 0x%X\n", i, len, (int)keyChar, (int)keyCode); + DBG_PRINT("sendKeyEvent: %d/%d code 0x%X, char 0x%X -> keySymChar 0x%X\n", i, len, (int)keyCode, (int)keyChar, (int)keySymChar); (*env)->CallVoidMethod(env, javaWindowObject, enqueueKeyEventID, JNI_FALSE, - evType, javaMods, keyCode, keyCode, keyChar); + evType, javaMods, keyCode, (jchar)keyChar, (jchar)keySymChar); } } else { // non-printable chars - jchar keyChar = (jchar) -1; + jchar keyChar = (jchar) 0; DBG_PRINT("sendKeyEvent: code 0x%X\n", (int)keyCode); (*env)->CallVoidMethod(env, javaWindowObject, enqueueKeyEventID, JNI_FALSE, - evType, javaMods, keyCode, keyCode, keyChar); + evType, javaMods, keyCode, keyChar, keyChar); } /* if (shallBeDetached) { @@ -1094,3 +1158,4 @@ static jint mods2JavaMods(NSUInteger mods) } @end + |