macCarbInput.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <ApplicationServices/ApplicationServices.h>
  23. #include <Carbon/Carbon.h>
  24. #include "platform/platformInput.h"
  25. #include "console/console.h"
  26. #include "core/strings/unicode.h"
  27. #include "core/util/tVector.h"
  28. // Static class variables:
  29. InputManager* Input::smManager;
  30. bool Input::smActive;
  31. U8 Input::smModifierKeys;
  32. InputEvent Input::smInputEvent;
  33. //-----------------------------------------------------------------------------
  34. // Keycode mapping.
  35. struct KeyCode
  36. {
  37. U32 mKeyCode;
  38. UniChar mCharLower;
  39. UniChar mCharUpper;
  40. KeyCode( U32 keyCode )
  41. : mKeyCode( keyCode ) {}
  42. };
  43. static KeyCode sOSToKeyCode[] =
  44. {
  45. KEY_A, // 0x00
  46. KEY_S, // 0x01
  47. KEY_D, // 0x02
  48. KEY_F, // 0x03
  49. KEY_H, // 0x04
  50. KEY_G, // 0x05
  51. KEY_Z, // 0x06
  52. KEY_X, // 0x07
  53. KEY_C, // 0x08
  54. KEY_V, // 0x09
  55. 0, // 0x0A
  56. KEY_B, // 0x0B
  57. KEY_Q, // 0x0C
  58. KEY_W, // 0x0D
  59. KEY_E, // 0x0E
  60. KEY_R, // 0x0F
  61. KEY_Y, // 0x10
  62. KEY_T, // 0x11
  63. KEY_1, // 0x12
  64. KEY_2, // 0x13
  65. KEY_3, // 0x14
  66. KEY_4, // 0x15
  67. KEY_6, // 0x16
  68. KEY_5, // 0x17
  69. KEY_EQUALS, // 0x18
  70. KEY_9, // 0x19
  71. KEY_7, // 0x1A
  72. KEY_MINUS, // 0x1B
  73. KEY_8, // 0x1C
  74. KEY_0, // 0x1D
  75. KEY_RBRACKET, // 0x1E
  76. KEY_O, // 0x1F
  77. KEY_U, // 0x20
  78. KEY_LBRACKET, // 0x21
  79. KEY_I, // 0x22
  80. KEY_P, // 0x23
  81. KEY_RETURN, // 0x24
  82. KEY_L, // 0x25
  83. KEY_J, // 0x26
  84. KEY_APOSTROPHE, // 0x27
  85. KEY_K, // 0x28
  86. KEY_SEMICOLON, // 0x29
  87. KEY_BACKSLASH, // 0x2A
  88. KEY_COMMA, // 0x2B
  89. KEY_SLASH, // 0x2C
  90. KEY_N, // 0x2D
  91. KEY_M, // 0x2E
  92. KEY_PERIOD, // 0x2F
  93. KEY_TAB, // 0x30
  94. KEY_SPACE, // 0x31
  95. KEY_TILDE, // 0x32
  96. KEY_BACKSPACE, // 0x33
  97. 0, // 0x34
  98. KEY_ESCAPE, // 0x35
  99. KEY_RALT, // 0x36
  100. KEY_LALT, // 0x37
  101. KEY_LSHIFT, // 0x38
  102. KEY_CAPSLOCK, // 0x39
  103. KEY_MAC_LOPT, // 0x3A
  104. KEY_LCONTROL, // 0x3B
  105. KEY_RSHIFT, // 0x3C
  106. KEY_MAC_ROPT, // 0x3D
  107. KEY_RCONTROL, // 0x3E
  108. 0, // 0x3F
  109. 0, // 0x40
  110. KEY_DECIMAL, // 0x41
  111. 0, // 0x42
  112. KEY_MULTIPLY, // 0x43
  113. 0, // 0x44
  114. KEY_ADD, // 0x45
  115. 0, // 0x46
  116. KEY_NUMLOCK, // 0x47
  117. 0, // 0x48
  118. 0, // 0x49
  119. 0, // 0x4A
  120. KEY_DIVIDE, // 0x4B
  121. KEY_NUMPADENTER, // 0x4C
  122. 0, // 0x4D
  123. KEY_SUBTRACT, // 0x4E
  124. 0, // 0x4F
  125. 0, // 0x50
  126. KEY_SEPARATOR, // 0x51
  127. KEY_NUMPAD0, // 0x52
  128. KEY_NUMPAD1, // 0x53
  129. KEY_NUMPAD2, // 0x54
  130. KEY_NUMPAD3, // 0x55
  131. KEY_NUMPAD4, // 0x56
  132. KEY_NUMPAD5, // 0x57
  133. KEY_NUMPAD6, // 0x58
  134. KEY_NUMPAD7, // 0x59
  135. 0, // 0x5A
  136. KEY_NUMPAD8, // 0x5B
  137. KEY_NUMPAD9, // 0x5C
  138. 0, // 0x5D
  139. 0, // 0x5E
  140. 0, // 0x5F
  141. KEY_F5, // 0x60
  142. KEY_F6, // 0x61
  143. KEY_F7, // 0x62
  144. KEY_F3, // 0x63
  145. KEY_F8, // 0x64
  146. KEY_F9, // 0x65
  147. 0, // 0x66
  148. KEY_F11, // 0x67
  149. 0, // 0x68
  150. KEY_F13, // 0x69
  151. KEY_F16, // 0x6A
  152. KEY_F14, // 0x6B
  153. 0, // 0x6C
  154. KEY_F10, // 0x6D
  155. 0, // 0x6E
  156. KEY_F12, // 0x6F
  157. 0, // 0x70
  158. KEY_F15, // 0x71
  159. KEY_INSERT, // 0x72
  160. KEY_HOME, // 0x73
  161. KEY_PAGE_UP, // 0x74
  162. KEY_DELETE, // 0x75
  163. KEY_F4, // 0x76
  164. KEY_END, // 0x77
  165. KEY_F2, // 0x78
  166. KEY_PAGE_DOWN, // 0x79
  167. KEY_F1, // 0x7A
  168. KEY_LEFT, // 0x7B
  169. KEY_RIGHT, // 0x7C
  170. KEY_DOWN, // 0x7D
  171. KEY_UP, // 0x7E
  172. };
  173. static Vector< U8 > sKeyCodeToOS( __FILE__, __LINE__ );
  174. #define NSShiftKeyMask ( 1 << 17 )
  175. static KeyboardLayoutRef sKeyLayout;
  176. static SInt32 sKeyLayoutKind = -1;
  177. static SInt32 sKeyLayoutID = -1;
  178. static SInt32 sLastKeyLayoutID = -1;
  179. static void GetKeyboardLayout()
  180. {
  181. KLGetCurrentKeyboardLayout( &sKeyLayout );
  182. KLGetKeyboardLayoutProperty( sKeyLayout, kKLKind, ( const void** ) &sKeyLayoutKind );
  183. KLGetKeyboardLayoutProperty( sKeyLayout, kKLIdentifier, ( const void** ) &sKeyLayoutID );
  184. }
  185. static bool KeyboardLayoutHasChanged()
  186. {
  187. GetKeyboardLayout();
  188. return ( sKeyLayoutID != sLastKeyLayoutID );
  189. }
  190. static UniChar OSKeyCodeToUnicode( UInt16 osKeyCode, bool shift = false )
  191. {
  192. // Translate the key code.
  193. UniChar uniChar = 0;
  194. if( sKeyLayoutKind == kKLKCHRKind )
  195. {
  196. // KCHR mapping.
  197. void* KCHRData;
  198. KLGetKeyboardLayoutProperty( sKeyLayout, kKLKCHRData, ( const void** ) & KCHRData );
  199. UInt16 key = ( osKeyCode & 0x7f );
  200. if( shift )
  201. key |= NSShiftKeyMask;
  202. UInt32 keyTranslateState = 0;
  203. UInt32 charCode = KeyTranslate( KCHRData, key, &keyTranslateState );
  204. charCode &= 0xff;
  205. if( keyTranslateState == 0 && charCode )
  206. uniChar = charCode;
  207. }
  208. else
  209. {
  210. // UCHR mapping.
  211. UCKeyboardLayout* uchrData;
  212. KLGetKeyboardLayoutProperty( sKeyLayout, kKLuchrData, ( const void** ) &uchrData );
  213. UInt32 deadKeyState;
  214. UniCharCount actualStringLength;
  215. UniChar unicodeString[ 4 ];
  216. UCKeyTranslate( uchrData,
  217. osKeyCode,
  218. kUCKeyActionDown,
  219. ( shift ? 0x02 : 0 ), // Oh yeah... Apple docs are fun...
  220. LMGetKbdType(),
  221. 0,
  222. &deadKeyState,
  223. sizeof( unicodeString ) / sizeof( unicodeString[ 0 ] ),
  224. &actualStringLength,
  225. unicodeString );
  226. if( actualStringLength )
  227. uniChar = unicodeString[ 0 ]; // Well, Unicode is something else, but...
  228. }
  229. return uniChar;
  230. }
  231. static void InitKeyCodeMapping()
  232. {
  233. const U32 numOSKeyCodes = sizeof( sOSToKeyCode ) / sizeof( sOSToKeyCode[ 0 ] );
  234. GetKeyboardLayout();
  235. sLastKeyLayoutID = sKeyLayoutID;
  236. U32 maxKeyCode = 0;
  237. for( U32 i = 0; i < numOSKeyCodes; ++ i )
  238. {
  239. sOSToKeyCode[ i ].mCharLower = OSKeyCodeToUnicode( i, false );
  240. sOSToKeyCode[ i ].mCharUpper = OSKeyCodeToUnicode( i, true );
  241. if( sOSToKeyCode[ i ].mKeyCode > maxKeyCode )
  242. maxKeyCode = sOSToKeyCode[ i ].mKeyCode;
  243. }
  244. if( !sKeyCodeToOS.size() )
  245. {
  246. sKeyCodeToOS.setSize( maxKeyCode + 1 );
  247. dMemset( sKeyCodeToOS.address(), 0, sKeyCodeToOS.size() );
  248. for( U32 i = 0; i < numOSKeyCodes; ++ i )
  249. sKeyCodeToOS[ sOSToKeyCode[ i ].mKeyCode ] = i;
  250. }
  251. }
  252. U8 TranslateOSKeyCode(U8 macKeycode)
  253. {
  254. AssertWarn(macKeycode < sizeof(sOSToKeyCode) / sizeof(sOSToKeyCode[0]), avar("TranslateOSKeyCode - could not translate code %i", macKeycode));
  255. if(macKeycode >= sizeof(sOSToKeyCode) / sizeof(sOSToKeyCode[0]))
  256. return KEY_NULL;
  257. return sOSToKeyCode[ macKeycode ].mKeyCode;
  258. }
  259. U8 TranslateKeyCodeToOS( U8 keycode )
  260. {
  261. return sKeyCodeToOS[ keycode ];
  262. }
  263. #pragma mark ---- Clipboard functions ----
  264. //-----------------------------------------------------------------------------
  265. const char* Platform::getClipboard()
  266. {
  267. // mac clipboards can contain multiple items,
  268. // and each item can be in several differnt flavors,
  269. // such as unicode or plaintext or pdf, etc.
  270. // scan through the clipboard, and return the 1st piece of actual text.
  271. ScrapRef clip;
  272. char *retBuf = "";
  273. OSStatus err = noErr;
  274. char *dataBuf = "";
  275. // get a local ref to the system clipboard
  276. GetScrapByName( kScrapClipboardScrap, kScrapGetNamedScrap, &clip );
  277. // First try to get unicode data, then try to get plain text data.
  278. Size dataSize = 0;
  279. bool plaintext = false;
  280. err = GetScrapFlavorSize(clip, kScrapFlavorTypeUnicode, &dataSize);
  281. if( err != noErr || dataSize <= 0)
  282. {
  283. Con::errorf("some error getting unicode clip");
  284. plaintext = true;
  285. err = GetScrapFlavorSize(clip, kScrapFlavorTypeText, &dataSize);
  286. }
  287. // kick out if we don't have any data.
  288. if( err != noErr || dataSize <= 0)
  289. {
  290. Con::errorf("no data, kicking out. size = %i",dataSize);
  291. return "";
  292. }
  293. if( err == noErr && dataSize > 0 )
  294. {
  295. // ok, we've got something! allocate a buffer and copy it in.
  296. char buf[dataSize+1];
  297. dMemset(buf, 0, dataSize+1);
  298. dataBuf = buf;
  299. // plain text needs no conversion.
  300. // unicode data needs to be converted to normalized utf-8 format.
  301. if(plaintext)
  302. {
  303. GetScrapFlavorData(clip, kScrapFlavorTypeText, &dataSize, &buf);
  304. retBuf = Con::getReturnBuffer(dataSize + 1);
  305. dMemcpy(retBuf,buf,dataSize);
  306. }
  307. else
  308. {
  309. GetScrapFlavorData(clip, kScrapFlavorTypeUnicode, &dataSize, &buf);
  310. // normalize
  311. CFStringRef cfBuf = CFStringCreateWithBytes(NULL, (const UInt8*)buf, dataSize, kCFStringEncodingUnicode, false);
  312. CFMutableStringRef normBuf = CFStringCreateMutableCopy(NULL, 0, cfBuf);
  313. CFStringNormalize(normBuf, kCFStringNormalizationFormC);
  314. // convert to utf-8
  315. U32 normBufLen = CFStringGetLength(normBuf);
  316. U32 retBufLen = CFStringGetMaximumSizeForEncoding(normBufLen,kCFStringEncodingUTF8) + 1; // +1 for the null terminator
  317. retBuf = Con::getReturnBuffer(retBufLen);
  318. CFStringGetCString( normBuf, retBuf, retBufLen, kCFStringEncodingUTF8);
  319. dataSize = retBufLen;
  320. }
  321. // manually null terminate, just in case.
  322. retBuf[dataSize] = 0;
  323. }
  324. // return the data, or the empty string if we did not find any data.
  325. return retBuf;
  326. }
  327. //-----------------------------------------------------------------------------
  328. bool Platform::setClipboard(const char *text)
  329. {
  330. ScrapRef clip;
  331. U32 textSize;
  332. OSStatus err = noErr;
  333. // make sure we have something to copy
  334. textSize = dStrlen(text);
  335. if(textSize == 0)
  336. return false;
  337. // get a local ref to the system clipboard
  338. GetScrapByName( kScrapClipboardScrap, kScrapClearNamedScrap, &clip );
  339. // put the data on the clipboard as text
  340. err = PutScrapFlavor( clip, kScrapFlavorTypeText, kScrapFlavorMaskNone, textSize, text);
  341. // put the data on the clipboard as unicode
  342. const UTF16 *utf16Data = convertUTF8toUTF16(text);
  343. err |= PutScrapFlavor( clip, kScrapFlavorTypeUnicode, kScrapFlavorMaskNone,
  344. dStrlen(utf16Data) * sizeof(UTF16), utf16Data);
  345. delete [] utf16Data;
  346. // and see if we were successful.
  347. if( err == noErr )
  348. return true;
  349. else
  350. return false;
  351. }
  352. void Input::init()
  353. {
  354. smManager = NULL;
  355. smActive = false;
  356. InitKeyCodeMapping();
  357. }
  358. U16 Input::getKeyCode( U16 asciiCode )
  359. {
  360. if( KeyboardLayoutHasChanged() )
  361. InitKeyCodeMapping();
  362. for( U32 i = 0; i < ( sizeof( sOSToKeyCode ) / sizeof( sOSToKeyCode[ 0 ] ) ); ++ i )
  363. if( sOSToKeyCode[ i ].mCharLower == asciiCode
  364. || sOSToKeyCode[ i ].mCharUpper == asciiCode )
  365. return sOSToKeyCode[ i ].mKeyCode;
  366. return 0;
  367. }
  368. U16 Input::getAscii( U16 keyCode, KEY_STATE keyState )
  369. {
  370. GetKeyboardLayout();
  371. return OSKeyCodeToUnicode( TranslateKeyCodeToOS( keyCode ), ( keyState == STATE_UPPER ? true : false ) );
  372. }
  373. void Input::destroy()
  374. {
  375. }
  376. bool Input::enable()
  377. {
  378. return true;
  379. }
  380. void Input::disable()
  381. {
  382. }
  383. void Input::activate()
  384. {
  385. }
  386. void Input::deactivate()
  387. {
  388. }
  389. bool Input::isEnabled()
  390. {
  391. return true;
  392. }
  393. bool Input::isActive()
  394. {
  395. return true;
  396. }
  397. void Input::process()
  398. {
  399. }
  400. InputManager* Input::getManager()
  401. {
  402. return smManager;
  403. }
  404. ConsoleFunction( enableMouse, bool, 1, 1, "enableMouse()" )
  405. {
  406. return true;
  407. }
  408. ConsoleFunction( disableMouse, void, 1, 1, "disableMouse()" )
  409. {
  410. }
  411. ConsoleFunction( echoInputState, void, 1, 1, "echoInputState()" )
  412. {
  413. }
  414. ConsoleFunction( toggleInputState, void, 1, 1, "toggleInputState()" )
  415. {
  416. }
  417. ConsoleFunction( isJoystickDetected, bool, 1, 1, "Always false on the MAC." )
  418. {
  419. return false;
  420. }
  421. ConsoleFunction( getJoystickAxes, const char*, 2, 2, "(handle instance)" )
  422. {
  423. return "";
  424. }
  425. ConsoleFunction( deactivateKeyboard, void, 1, 1, "deactivateKeyboard();")
  426. {
  427. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  428. }
  429. ConsoleFunction( activateKeyboard, void, 1, 1, "activateKeyboard();")
  430. {
  431. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  432. }