2006-07-23 09:11:10 +00:00
/ *
SDL - Simple DirectMedia Layer
2008-12-08 00:27:32 +00:00
Copyright ( C ) 1997 -2009 Sam Lantinga
2006-07-23 09:11:10 +00:00
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2.1 of the License , or ( at your option ) any later version .
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 -1301 USA
Sam Lantinga
slouken @ libsdl . org
* /
# include "SDL_config.h"
# include "SDL_cocoavideo.h"
# include "../../events/SDL_keyboard_c.h"
2008-02-07 15:31:09 +00:00
# include "../../events/scancodes_darwin.h"
2006-07-23 09:11:10 +00:00
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
# include < Carbon / Carbon . h >
2006-07-30 05:18:33 +00:00
# ifndef NX_DEVICERCTLKEYMASK
# define NX_DEVICELCTLKEYMASK 0 x00000001
# endif
# ifndef NX_DEVICELSHIFTKEYMASK
# define NX_DEVICELSHIFTKEYMASK 0 x00000002
# endif
# ifndef NX_DEVICERSHIFTKEYMASK
# define NX_DEVICERSHIFTKEYMASK 0 x00000004
# endif
# ifndef NX_DEVICELCMDKEYMASK
# define NX_DEVICELCMDKEYMASK 0 x00000008
# endif
# ifndef NX_DEVICERCMDKEYMASK
# define NX_DEVICERCMDKEYMASK 0 x00000010
# endif
# ifndef NX_DEVICELALTKEYMASK
# define NX_DEVICELALTKEYMASK 0 x00000020
# endif
# ifndef NX_DEVICERALTKEYMASK
# define NX_DEVICERALTKEYMASK 0 x00000040
# endif
# ifndef NX_DEVICERCTLKEYMASK
# define NX_DEVICERCTLKEYMASK 0 x00002000
# endif
2007-12-29 21:36:17 +00:00
@ interface SDLTranslatorResponder : NSTextView
{
}
- ( void ) doCommandBySelector : ( SEL ) myselector ;
@ end
@ implementation SDLTranslatorResponder
- ( void ) doCommandBySelector : ( SEL ) myselector { }
@ end
2006-07-30 05:18:33 +00:00
/ * This is the original behavior , before support was added for
* differentiating between left and right versions of the keys .
* /
static void
DoUnsidedModifiers ( int keyboard , unsigned short scancode ,
unsigned int oldMods , unsigned int newMods )
{
2008-02-05 07:19:23 +00:00
const int mapping [ ] = {
SDL_SCANCODE _CAPSLOCK ,
SDL_SCANCODE _LSHIFT ,
SDL_SCANCODE _LCTRL ,
SDL_SCANCODE _LALT ,
SDL_SCANCODE _LGUI
} ;
2006-07-30 05:18:33 +00:00
unsigned int i , bit ;
/ * Iterate through the bits , testing each against the current modifiers * /
for ( i = 0 , bit = NSAlphaShiftKeyMask ; bit <= NSCommandKeyMask ; bit < <= 1 , + + i ) {
unsigned int oldMask , newMask ;
oldMask = oldMods & bit ;
newMask = newMods & bit ;
if ( oldMask && oldMask ! = newMask ) { / * modifier up event * /
/ * If this was Caps Lock , we need some additional voodoo to make SDL happy * /
if ( bit = = NSAlphaShiftKeyMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , mapping [ i ] ) ;
2006-07-30 05:18:33 +00:00
}
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , mapping [ i ] ) ;
2006-07-30 05:18:33 +00:00
} else if ( newMask && oldMask ! = newMask ) { / * modifier down event * /
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , mapping [ i ] ) ;
2006-07-30 05:18:33 +00:00
/ * If this was Caps Lock , we need some additional voodoo to make SDL happy * /
if ( bit = = NSAlphaShiftKeyMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , mapping [ i ] ) ;
2006-07-30 05:18:33 +00:00
}
}
}
}
/ * This is a helper function for HandleModifierSide . This
* function reverts back to behavior before the distinction between
* sides was made .
* /
static void
2008-02-05 07:19:23 +00:00
HandleNonDeviceModifier ( int keyboard ,
2006-07-30 05:18:33 +00:00
unsigned int device_independent _mask ,
unsigned int oldMods ,
unsigned int newMods ,
2008-02-05 07:19:23 +00:00
SDL_scancode scancode )
2006-07-30 05:18:33 +00:00
{
unsigned int oldMask , newMask ;
/ * Isolate just the bits we care about in the depedent bits so we can
* figure out what changed
* /
oldMask = oldMods & device_independent _mask ;
newMask = newMods & device_independent _mask ;
if ( oldMask && oldMask ! = newMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , scancode ) ;
2006-07-30 05:18:33 +00:00
} else if ( newMask && oldMask ! = newMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , scancode ) ;
2006-07-30 05:18:33 +00:00
}
}
/ * This is a helper function for HandleModifierSide .
* This function sets the actual SDL_PrivateKeyboard event .
* /
static void
2008-02-05 07:19:23 +00:00
HandleModifierOneSide ( int keyboard ,
2006-07-30 05:18:33 +00:00
unsigned int oldMods , unsigned int newMods ,
2008-02-05 07:19:23 +00:00
SDL_scancode scancode ,
2006-07-30 05:18:33 +00:00
unsigned int sided_device _dependent _mask )
{
unsigned int old_dep _mask , new_dep _mask ;
/ * Isolate just the bits we care about in the depedent bits so we can
* figure out what changed
* /
old_dep _mask = oldMods & sided_device _dependent _mask ;
new_dep _mask = newMods & sided_device _dependent _mask ;
/ * We now know that this side bit flipped . But we don ' t know if
* it went pressed to released or released to pressed , so we must
* find out which it is .
* /
if ( new_dep _mask && old_dep _mask ! = new_dep _mask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , scancode ) ;
2006-07-30 05:18:33 +00:00
} else {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , scancode ) ;
2006-07-30 05:18:33 +00:00
}
}
/ * This is a helper function for DoSidedModifiers .
* This function will figure out if the modifier key is the left or right side ,
* e . g . left - shift vs right - shift .
* /
static void
2008-02-05 07:19:23 +00:00
HandleModifierSide ( int keyboard ,
2006-07-30 05:18:33 +00:00
int device_independent _mask ,
unsigned int oldMods , unsigned int newMods ,
2008-02-05 07:19:23 +00:00
SDL_scancode left_scancode ,
SDL_scancode right_scancode ,
2006-07-30 05:18:33 +00:00
unsigned int left_device _dependent _mask ,
unsigned int right_device _dependent _mask )
{
unsigned int device_dependent _mask = ( left_device _dependent _mask |
right_device _dependent _mask ) ;
unsigned int diff_mod ;
/ * On the basis that the device independent mask is set , but there are
* no device dependent flags set , we ' ll assume that we can ' t detect this
* keyboard and revert to the unsided behavior .
* /
if ( ( device_dependent _mask & newMods ) = = 0 ) {
/ * Revert to the old behavior * /
2008-02-05 07:19:23 +00:00
HandleNonDeviceModifier ( keyboard , device_independent _mask , oldMods , newMods , left_scancode ) ;
2006-07-30 05:18:33 +00:00
return ;
}
/ * XOR the previous state against the new state to see if there ' s a change * /
diff_mod = ( device_dependent _mask & oldMods ) ^
( device_dependent _mask & newMods ) ;
if ( diff_mod ) {
/ * A change in state was found . Isolate the left and right bits
* to handle them separately just in case the values can simulataneously
* change or if the bits don ' t both exist .
* /
if ( left_device _dependent _mask & diff_mod ) {
2008-02-05 07:19:23 +00:00
HandleModifierOneSide ( keyboard , oldMods , newMods , left_scancode , left_device _dependent _mask ) ;
2006-07-30 05:18:33 +00:00
}
if ( right_device _dependent _mask & diff_mod ) {
2008-02-05 07:19:23 +00:00
HandleModifierOneSide ( keyboard , oldMods , newMods , right_scancode , right_device _dependent _mask ) ;
2006-07-30 05:18:33 +00:00
}
}
}
/ * This is a helper function for DoSidedModifiers .
* This function will release a key press in the case that
* it is clear that the modifier has been released ( i . e . one side
* can ' t still be down ) .
* /
static void
2008-02-05 07:19:23 +00:00
ReleaseModifierSide ( int keyboard ,
2006-07-30 05:18:33 +00:00
unsigned int device_independent _mask ,
unsigned int oldMods , unsigned int newMods ,
2008-02-05 07:19:23 +00:00
SDL_scancode left_scancode ,
SDL_scancode right_scancode ,
2006-07-30 05:18:33 +00:00
unsigned int left_device _dependent _mask ,
unsigned int right_device _dependent _mask )
{
unsigned int device_dependent _mask = ( left_device _dependent _mask |
right_device _dependent _mask ) ;
/ * On the basis that the device independent mask is set , but there are
* no device dependent flags set , we ' ll assume that we can ' t detect this
* keyboard and revert to the unsided behavior .
* /
if ( ( device_dependent _mask & oldMods ) = = 0 ) {
/ * In this case , we can ' t detect the keyboard , so use the left side
* to represent both , and release it .
* /
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , left_scancode ) ;
2006-07-30 05:18:33 +00:00
return ;
}
/ *
* This could have been done in an if - else case because at this point ,
* we know that all keys have been released when calling this function .
* But I ' m being paranoid so I want to handle each separately ,
* so I hope this doesn ' t cause other problems .
* /
if ( left_device _dependent _mask & oldMods ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , left_scancode ) ;
2006-07-30 05:18:33 +00:00
}
if ( right_device _dependent _mask & oldMods ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , right_scancode ) ;
2006-07-30 05:18:33 +00:00
}
}
/ * This is a helper function for DoSidedModifiers .
* This function handles the CapsLock case .
* /
static void
HandleCapsLock ( int keyboard , unsigned short scancode ,
unsigned int oldMods , unsigned int newMods )
{
unsigned int oldMask , newMask ;
oldMask = oldMods & NSAlphaShiftKeyMask ;
newMask = newMods & NSAlphaShiftKeyMask ;
if ( oldMask ! = newMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , SDL_SCANCODE _CAPSLOCK ) ;
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , SDL_SCANCODE _CAPSLOCK ) ;
2006-07-30 05:18:33 +00:00
}
2006-07-30 06:11:24 +00:00
oldMask = oldMods & NSNumericPadKeyMask ;
newMask = newMods & NSNumericPadKeyMask ;
if ( oldMask ! = newMask ) {
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( keyboard , SDL_PRESSED , SDL_SCANCODE _NUMLOCKCLEAR ) ;
SDL_SendKeyboardKey ( keyboard , SDL_RELEASED , SDL_SCANCODE _NUMLOCKCLEAR ) ;
2006-07-30 06:11:24 +00:00
}
2006-07-30 05:18:33 +00:00
}
/ * This function will handle the modifier keys and also determine the
* correct side of the key .
* /
static void
DoSidedModifiers ( int keyboard , unsigned short scancode ,
unsigned int oldMods , unsigned int newMods )
{
/ * Set up arrays for the key syms for the left and right side . * /
2008-02-05 07:19:23 +00:00
const SDL_scancode left_mapping [ ] = {
SDL_SCANCODE _LSHIFT ,
SDL_SCANCODE _LCTRL ,
SDL_SCANCODE _LALT ,
SDL_SCANCODE _LGUI
} ;
const SDL_scancode right_mapping [ ] = {
SDL_SCANCODE _RSHIFT ,
SDL_SCANCODE _RCTRL ,
SDL_SCANCODE _RALT ,
SDL_SCANCODE _RGUI
} ;
2006-07-30 05:18:33 +00:00
/ * Set up arrays for the device dependent masks with indices that
* correspond to the _mapping arrays
* /
const unsigned int left_device _mapping [ ] = { NX_DEVICELSHIFTKEYMASK , NX_DEVICELCTLKEYMASK , NX_DEVICELALTKEYMASK , NX_DEVICELCMDKEYMASK } ;
const unsigned int right_device _mapping [ ] = { NX_DEVICERSHIFTKEYMASK , NX_DEVICERCTLKEYMASK , NX_DEVICERALTKEYMASK , NX_DEVICERCMDKEYMASK } ;
unsigned int i , bit ;
/ * Handle CAPSLOCK separately because it doesn ' t have a left / right side * /
HandleCapsLock ( keyboard , scancode , oldMods , newMods ) ;
/ * Iterate through the bits , testing each against the old modifiers * /
for ( i = 0 , bit = NSShiftKeyMask ; bit <= NSCommandKeyMask ; bit < <= 1 , + + i ) {
unsigned int oldMask , newMask ;
oldMask = oldMods & bit ;
newMask = newMods & bit ;
/ * If the bit is set , we must always examine it because the left
* and right side keys may alternate or both may be pressed .
* /
if ( newMask ) {
2008-02-05 07:19:23 +00:00
HandleModifierSide ( keyboard , bit , oldMods , newMods ,
2006-07-30 05:18:33 +00:00
left_mapping [ i ] , right_mapping [ i ] ,
left_device _mapping [ i ] , right_device _mapping [ i ] ) ;
}
/ * If the state changed from pressed to unpressed , we must examine
* the device dependent bits to release the correct keys .
* /
else if ( oldMask && oldMask ! = newMask ) {
2008-02-05 07:19:23 +00:00
ReleaseModifierSide ( keyboard , bit , oldMods , newMods ,
2006-07-30 05:18:33 +00:00
left_mapping [ i ] , right_mapping [ i ] ,
left_device _mapping [ i ] , right_device _mapping [ i ] ) ;
}
}
}
static void
HandleModifiers ( _THIS , unsigned short scancode , unsigned int modifierFlags )
{
SDL_VideoData * data = ( SDL_VideoData * ) _this -> driverdata ;
if ( modifierFlags = = data -> modifierFlags ) {
return ;
}
/ *
* Starting with Panther ( 10.3 .0 ) , the ability to distinguish between
* left side and right side modifiers is available .
* /
if ( data -> osversion >= 0 x1030 ) {
DoSidedModifiers ( data -> keyboard , scancode , data -> modifierFlags , modifierFlags ) ;
} else {
DoUnsidedModifiers ( data -> keyboard , scancode , data -> modifierFlags , modifierFlags ) ;
}
data -> modifierFlags = modifierFlags ;
}
2008-02-05 07:19:23 +00:00
static void
UpdateKeymap ( SDL_VideoData * data )
{
2009-09-05 09:55:25 +00:00
# if MAC_OS _X _VERSION _MAX _ALLOWED >= 1050
TISInputSourceRef key_layout ;
# else
2008-02-05 07:19:23 +00:00
KeyboardLayoutRef key_layout ;
2009-09-05 09:55:25 +00:00
# endif
2008-02-05 07:19:23 +00:00
const void * chr_data ;
int i ;
SDL_scancode scancode ;
SDLKey keymap [ SDL_NUM _SCANCODES ] ;
/ * See if the keymap needs to be updated * /
2009-09-05 09:55:25 +00:00
# if MAC_OS _X _VERSION _MAX _ALLOWED >= 1050
key_layout = TISCopyCurrentKeyboardLayoutInputSource ( ) ;
# else
2008-02-05 07:19:23 +00:00
KLGetCurrentKeyboardLayout ( & key_layout ) ;
2009-09-05 09:55:25 +00:00
# endif
2008-02-05 07:19:23 +00:00
if ( key_layout = = data -> key_layout ) {
return ;
}
data -> key_layout = key_layout ;
SDL_GetDefaultKeymap ( keymap ) ;
/ * Try Unicode data first ( preferred as of Mac OS X 10.5 ) * /
2009-09-05 09:55:25 +00:00
# if MAC_OS _X _VERSION _MAX _ALLOWED >= 1050
CFDataRef uchrDataRef = TISGetInputSourceProperty ( key_layout , kTISPropertyUnicodeKeyLayoutData ) ;
if ( uchrDataRef )
chr_data = CFDataGetBytePtr ( uchrDataRef ) ;
else
goto cleanup ;
# else
2008-02-05 07:19:23 +00:00
KLGetKeyboardLayoutProperty ( key_layout , kKLuchrData , & chr_data ) ;
2009-09-05 09:55:25 +00:00
# endif
2008-02-05 07:19:23 +00:00
if ( chr_data ) {
UInt32 keyboard_type = LMGetKbdType ( ) ;
OSStatus err ;
2008-02-07 15:31:09 +00:00
for ( i = 0 ; i < SDL_arraysize ( darwin_scancode _table ) ; i + + ) {
2008-02-05 07:19:23 +00:00
UniChar s [ 8 ] ;
UniCharCount len ;
UInt32 dead_key _state ;
/ * Make sure this scancode is a valid character scancode * /
2008-02-07 15:31:09 +00:00
scancode = darwin_scancode _table [ i ] ;
2008-02-05 07:19:23 +00:00
if ( scancode = = SDL_SCANCODE _UNKNOWN ||
( keymap [ scancode ] & SDLK_SCANCODE _MASK ) ) {
continue ;
}
dead_key _state = 0 ;
2009-09-05 09:55:25 +00:00
err = UCKeyTranslate ( ( UCKeyboardLayout * ) chr_data ,
i , kUCKeyActionDown ,
2008-02-05 07:19:23 +00:00
0 , keyboard_type ,
kUCKeyTranslateNoDeadKeysMask ,
& dead_key _state , 8 , & len , s ) ;
if ( err ! = noErr )
continue ;
if ( len > 0 && s [ 0 ] ! = 0 x10 ) {
keymap [ scancode ] = s [ 0 ] ;
}
}
SDL_SetKeymap ( data -> keyboard , 0 , keymap , SDL_NUM _SCANCODES ) ;
return ;
}
2009-09-05 09:55:25 +00:00
# if MAC_OS _X _VERSION _MAX _ALLOWED >= 1050
cleanup :
CFRelease ( key_layout ) ;
# else
2008-02-05 07:19:23 +00:00
/ * Fall back to older style key map data * /
KLGetKeyboardLayoutProperty ( key_layout , kKLKCHRData , & chr_data ) ;
if ( chr_data ) {
for ( i = 0 ; i < 128 ; i + + ) {
UInt32 c , state = 0 ;
/ * Make sure this scancode is a valid character scancode * /
2008-02-07 15:31:09 +00:00
scancode = darwin_scancode _table [ i ] ;
2008-02-05 07:19:23 +00:00
if ( scancode = = SDL_SCANCODE _UNKNOWN ||
( keymap [ scancode ] & SDLK_SCANCODE _MASK ) ) {
continue ;
}
2008-02-05 07:30:50 +00:00
c = KeyTranslate ( chr_data , i , & state ) & 255 ;
2008-02-05 07:19:23 +00:00
if ( state ) {
/ * Dead key , process key up * /
2008-02-05 07:30:50 +00:00
c = KeyTranslate ( chr_data , i | 128 , & state ) & 255 ;
2008-02-05 07:19:23 +00:00
}
2008-02-05 07:30:50 +00:00
if ( c ! = 0 && c ! = 0 x10 ) {
2008-02-05 07:19:23 +00:00
/ * MacRoman to Unicode table , taken from X . org sources * /
static const unsigned short macroman_table [ 128 ] = {
0 xc4 , 0 xc5 , 0 xc7 , 0 xc9 , 0 xd1 , 0 xd6 , 0 xdc , 0 xe1 ,
0 xe0 , 0 xe2 , 0 xe4 , 0 xe3 , 0 xe5 , 0 xe7 , 0 xe9 , 0 xe8 ,
0 xea , 0 xeb , 0 xed , 0 xec , 0 xee , 0 xef , 0 xf1 , 0 xf3 ,
0 xf2 , 0 xf4 , 0 xf6 , 0 xf5 , 0 xfa , 0 xf9 , 0 xfb , 0 xfc ,
0 x2020 , 0 xb0 , 0 xa2 , 0 xa3 , 0 xa7 , 0 x2022 , 0 xb6 , 0 xdf ,
0 xae , 0 xa9 , 0 x2122 , 0 xb4 , 0 xa8 , 0 x2260 , 0 xc6 , 0 xd8 ,
0 x221e , 0 xb1 , 0 x2264 , 0 x2265 , 0 xa5 , 0 xb5 , 0 x2202 , 0 x2211 ,
0 x220f , 0 x3c0 , 0 x222b , 0 xaa , 0 xba , 0 x3a9 , 0 xe6 , 0 xf8 ,
0 xbf , 0 xa1 , 0 xac , 0 x221a , 0 x192 , 0 x2248 , 0 x2206 , 0 xab ,
0 xbb , 0 x2026 , 0 xa0 , 0 xc0 , 0 xc3 , 0 xd5 , 0 x152 , 0 x153 ,
0 x2013 , 0 x2014 , 0 x201c , 0 x201d , 0 x2018 , 0 x2019 , 0 xf7 , 0 x25ca ,
0 xff , 0 x178 , 0 x2044 , 0 x20ac , 0 x2039 , 0 x203a , 0 xfb01 , 0 xfb02 ,
0 x2021 , 0 xb7 , 0 x201a , 0 x201e , 0 x2030 , 0 xc2 , 0 xca , 0 xc1 ,
0 xcb , 0 xc8 , 0 xcd , 0 xce , 0 xcf , 0 xcc , 0 xd3 , 0 xd4 ,
0 xf8ff , 0 xd2 , 0 xda , 0 xdb , 0 xd9 , 0 x131 , 0 x2c6 , 0 x2dc ,
0 xaf , 0 x2d8 , 0 x2d9 , 0 x2da , 0 xb8 , 0 x2dd , 0 x2db , 0 x2c7 ,
} ;
if ( c >= 128 ) {
c = macroman_table [ c - 128 ] ;
}
keymap [ scancode ] = c ;
}
}
SDL_SetKeymap ( data -> keyboard , 0 , keymap , SDL_NUM _SCANCODES ) ;
return ;
}
2009-09-05 09:55:25 +00:00
# endif
2008-02-05 07:19:23 +00:00
}
2006-07-23 09:11:10 +00:00
void
Cocoa_InitKeyboard ( _THIS )
{
SDL_VideoData * data = ( SDL_VideoData * ) _this -> driverdata ;
SDL_Keyboard keyboard ;
2007-07-11 14:44:28 +00:00
NSAutoreleasePool * pool ;
2006-07-23 09:11:10 +00:00
2007-07-11 14:44:28 +00:00
pool = [ [ NSAutoreleasePool alloc ] init ] ;
2007-12-29 21:36:17 +00:00
data -> fieldEdit = [ [ SDLTranslatorResponder alloc ] initWithFrame : NSMakeRect ( 0.0 , 0.0 , 0.0 , 0.0 ) ] ;
2007-07-11 14:44:28 +00:00
[ pool release ] ;
2007-07-11 08:09:20 +00:00
2006-07-23 09:11:10 +00:00
SDL_zero ( keyboard ) ;
data -> keyboard = SDL_AddKeyboard ( & keyboard , -1 ) ;
2008-02-05 07:19:23 +00:00
UpdateKeymap ( data ) ;
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
/ * Set our own names for the platform - dependent but layout - independent keys * /
2008-02-05 07:19:23 +00:00
/ * This key is NumLock on the MacBook keyboard . : ) * /
/ * SDL_SetScancodeName ( SDL_SCANCODE _NUMLOCKCLEAR , "Clear" ) ; * /
SDL_SetScancodeName ( SDL_SCANCODE _LALT , "Left Option" ) ;
SDL_SetScancodeName ( SDL_SCANCODE _LGUI , "Left Command" ) ;
SDL_SetScancodeName ( SDL_SCANCODE _RALT , "Right Option" ) ;
SDL_SetScancodeName ( SDL_SCANCODE _RGUI , "Right Command" ) ;
2006-07-23 09:11:10 +00:00
}
2006-07-30 05:18:33 +00:00
void
Cocoa_HandleKeyEvent ( _THIS , NSEvent * event )
{
SDL_VideoData * data = ( SDL_VideoData * ) _this -> driverdata ;
unsigned short scancode = [ event keyCode ] ;
2008-02-05 07:19:23 +00:00
SDL_scancode code ;
2006-07-30 05:18:33 +00:00
const char * text ;
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
if ( ( scancode = = 10 || scancode = = 50 ) && KBGetLayoutType ( LMGetKbdType ( ) ) = = kKeyboardISO ) {
/ * see comments in SDL_cocoakeys . h * /
scancode = 60 - scancode ;
}
2008-02-07 15:31:09 +00:00
if ( scancode < SDL_arraysize ( darwin_scancode _table ) ) {
code = darwin_scancode _table [ scancode ] ;
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
}
else {
2006-07-30 05:18:33 +00:00
/ * Hmm , does this ever happen ? If so , need to extend the keymap . . . * /
2008-02-05 07:19:23 +00:00
code = SDL_SCANCODE _UNKNOWN ;
2006-07-30 05:18:33 +00:00
}
switch ( [ event type ] ) {
case NSKeyDown :
2007-06-16 15:32:04 +00:00
if ( ! [ event isARepeat ] ) {
2008-02-05 07:19:23 +00:00
/ * See if we need to rebuild the keyboard layout * /
UpdateKeymap ( data ) ;
SDL_SendKeyboardKey ( data -> keyboard , SDL_PRESSED , code ) ;
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
# if 1
2008-02-05 07:19:23 +00:00
if ( code = = SDL_SCANCODE _UNKNOWN ) {
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
fprintf ( stderr , "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n" , scancode ) ;
}
# endif
2006-07-30 05:18:33 +00:00
}
2007-06-16 15:32:04 +00:00
if ( SDL_EventState ( SDL_TEXTINPUT , SDL_QUERY ) ) {
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
/ * FIXME CW 2007 -08 -16 : only send those events to the field editor for which we actually want text events , not e . g . esc or function keys . Arrow keys in particular seem to produce crashes sometimes . * /
2007-07-11 08:09:20 +00:00
[ data -> fieldEdit interpretKeyEvents : [ NSArray arrayWithObject : event ] ] ;
2007-06-16 15:32:04 +00:00
text = [ [ event characters ] UTF8String ] ;
if ( text && * text ) {
SDL_SendKeyboardText ( data -> keyboard , text ) ;
2007-08-21 06:54:07 +00:00
[ data -> fieldEdit setString : @ "" ] ;
2007-06-16 15:32:04 +00:00
}
2006-07-30 05:18:33 +00:00
}
break ;
case NSKeyUp :
2008-02-05 07:19:23 +00:00
SDL_SendKeyboardKey ( data -> keyboard , SDL_RELEASED , code ) ;
2006-07-30 05:18:33 +00:00
break ;
case NSFlagsChanged :
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
/ * FIXME CW 2007 -08 -14 : check if this whole mess that takes up half of this file is really necessary * /
2006-07-30 05:18:33 +00:00
HandleModifiers ( _this , scancode , [ event modifierFlags ] ) ;
break ;
Date: Thu, 05 Jul 2007 14:02:33 -0700
From: Sam Lantinga
Subject: SDL 1.3 keyboard plan
After lots of discussion with Christian, this is what we came up with:
> So, to sum up...
> SDLK_* become the physical keys, starting at > (1<<21)
> We create a macro SDLK_INDEX(X)
> We have two functions SDL_GetLayoutKey(SDLKey) and SDL_GetKeyName()
> SDL_GetLayoutKey maps to UCS4 for printable characters, and SDLK* for
non-printable characters
> and does so based on the OS's current keyboard layout
> SDL_GetKeyName() handles both SDLK_* and UCS4, converting UCS4 to UTF-8 and
converting SDLK_* into our names, which are UTF-8 for printable characters.
> WASD folks use SDLK_*, and 'I' folks use SDL_GetLayoutKey(SDLK_*)
Here is the patch he came up with, and his e-mail about it:
Date: Fri, 17 Aug 2007 19:50:28 +0200
From: Christian Walther
Subject: Re: SDL 1.3 keyboard plan
> Sounds great, go ahead and send me a patch.
Here goes! Thanks for having a look. Don't hesitate to comment if
anything does not conform to your ideas.
One caveat: Committing this now may break compilability of some video
drivers - specifically, if they use any of the SDLK_* codes that were
obsoleted and moved into SDL_compat.h. I only tried Cocoa (which did
break, but is already fixed) and X11 (which didn't, but then its key
handling is #iffed out). If that's a problem, it may need to go into
a branch.
-Christian
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402630
2007-08-19 14:52:52 +00:00
default : / * just to avoid compiler warnings * /
break ;
}
}
2006-07-23 09:11:10 +00:00
void
Cocoa_QuitKeyboard ( _THIS )
{
SDL_VideoData * data = ( SDL_VideoData * ) _this -> driverdata ;
2007-08-19 16:36:51 +00:00
NSAutoreleasePool * pool ;
2006-07-23 09:11:10 +00:00
SDL_DelKeyboard ( data -> keyboard ) ;
2007-07-11 08:09:20 +00:00
2007-08-19 16:36:51 +00:00
pool = [ [ NSAutoreleasePool alloc ] init ] ;
2007-07-11 08:09:20 +00:00
[ data -> fieldEdit release ] ;
2007-08-19 16:36:51 +00:00
[ pool release ] ;
2006-07-23 09:11:10 +00:00
}
/ * vi : set ts = 4 sw = 4 expandtab : * /