From 50838933175fe188d3b4c7d5ebf5266dbfd1077e Mon Sep 17 00:00:00 2001 From: Steve Checkoway Date: Thu, 14 Sep 2006 09:13:46 +0000 Subject: [PATCH] Use UCKeyTranslate if the 'uchr' resource exists. If not, fall back on the 'KCHR' resource. --- .../arch/InputHandler/InputHandler_Carbon.cpp | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/stepmania/src/arch/InputHandler/InputHandler_Carbon.cpp b/stepmania/src/arch/InputHandler/InputHandler_Carbon.cpp index 1c5209ca7e..18784bf7ca 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_Carbon.cpp +++ b/stepmania/src/arch/InputHandler/InputHandler_Carbon.cpp @@ -373,20 +373,63 @@ wchar_t InputHandler_Carbon::DeviceButtonToChar( DeviceButton button, bool bUseC UInt32 modifiers = 0; if( bUseCurrentKeyModifiers ) modifiers = GetCurrentKeyModifiers(); + + SInt16 iCurrentKeyScript = GetScriptManagerVariable( smKeyScript ); + SInt16 iCurrentKeyLayoutID = GetScriptVariable( iCurrentKeyScript, smScriptKeys ); + static SInt16 iLastKeyLayoutID = !iCurrentKeyLayoutID; // Just be different. + static UInt32 iDeadKeyState; + static UCKeyboardLayout **KeyLayout; - static unsigned long state = 0; - static Ptr keymap = NULL; - Ptr new_keymap; - - new_keymap = (Ptr)GetScriptManagerVariable(smKCHRCache); - if( new_keymap != keymap ) + if( iCurrentKeyLayoutID != iLastKeyLayoutID ) { - keymap = new_keymap; - state = 0; + iDeadKeyState = 0; + KeyLayout = (UCKeyboardLayout **)GetResource( 'uchr', iCurrentKeyLayoutID ); + iLastKeyLayoutID = iCurrentKeyLayoutID; + } + if( KeyLayout ) + { + UInt32 keyboardType = LMGetKbdType(); + UInt32 modifiers = bUseCurrentKeyModifiers ? GetCurrentKeyModifiers() : 0; + UniChar unicodeInputString[4]; + UniCharCount length; + OSStatus status = UCKeyTranslate( *KeyLayout, iMacVirtualKey, kUCKeyActionDown, modifiers, + keyboardType, 0, &iDeadKeyState, ARRAYLEN(unicodeInputString), + &length, unicodeInputString ); + + if( status ) + return L'\0'; + + CFStringRef inputString = CFStringCreateWithCharacters( NULL, unicodeInputString, length ); + char utf8InputString[7]; // Max size is 6 (although really only 4 are used) + null. + + if( !CFStringGetCString(inputString, utf8InputString, 7, kCFStringEncodingUTF8) ) + { + CFRelease( inputString ); + return L'\0'; + } + + wchar_t ch = utf8_get_char( utf8InputString ); + + CFRelease( inputString ); + return ch == INVALID_CHAR ? L'\0' : ch; + + } + else + { + // Fall back on the 'KCHR' resource. + static unsigned long state = 0; + static Ptr keymap = NULL; + Ptr new_keymap; + + new_keymap = (Ptr)GetScriptManagerVariable(smKCHRCache); + if( new_keymap != keymap ) + { + keymap = new_keymap; + state = 0; + } + // XXX: Only returns ascii. Ž will be returned as e. + return KeyTranslate( keymap, UInt16(iMacVirtualKey)|modifiers, &state ) & 0xFF; } - // TODO: Use UCKeyTranslate - wchar_t ch = KeyTranslate( keymap, ((int)iMacVirtualKey)|modifiers, &state ) & 0xFFFF; - return ch; } return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers );