osxInput.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #import "platform/platform.h"
  23. #import "platform/platformInput.h"
  24. #import "platformOSX/osxInputManager.h"
  25. #import "platform/event.h"
  26. #pragma mark ---- Static Variables ----
  27. InputManager *Input::smManager = 0;
  28. CursorManager *Input::smCursorManager = 0;
  29. bool Input::smActive;
  30. bool Input::smCursorGuard;
  31. #pragma mark ---- Input Namespace Functions ----
  32. //--------------------------------------------------------------------------
  33. void Input::init()
  34. {
  35. Con::printSeparator();
  36. Con::printf("Input initialization:");
  37. // Cleanup if initialized previously
  38. destroy();
  39. smManager = NULL;
  40. smActive = false;
  41. smCursorGuard = true; //cursor starts visible
  42. if (!smManager)
  43. smManager = new osxInputManager();
  44. Input::enable();
  45. if (!smCursorManager)
  46. {
  47. smCursorManager = new CursorManager();
  48. // Add the arrow cursor to the stack
  49. if (smCursorManager)
  50. smCursorManager->pushCursor(CursorManager::curArrow);
  51. else
  52. Con::printf("Cursor Manager not enabled");
  53. }
  54. }
  55. //--------------------------------------------------------------------------
  56. void Input::destroy()
  57. {
  58. if (smManager)
  59. {
  60. smManager->disable();
  61. delete smManager;
  62. smManager = NULL;
  63. }
  64. if (smCursorManager)
  65. {
  66. smCursorManager->popCursor();
  67. delete smCursorManager;
  68. smCursorManager = NULL;
  69. }
  70. smActive = false;
  71. }
  72. //------------------------------------------------------------------------------
  73. // Disable all input for the application
  74. bool Input::enable()
  75. {
  76. bool enabledValue = false;
  77. // Check for any restrictions or errors
  78. // If the manager exists and is not enabled, dos o
  79. if (smManager && !smManager->isEnabled())
  80. enabledValue = smManager->enable();
  81. // Also enable mouse and keyboard automatically
  82. enableMouse();
  83. enableKeyboard();
  84. return enabledValue;
  85. }
  86. //------------------------------------------------------------------------------
  87. // Disable all input in the application
  88. void Input::disable()
  89. {
  90. if (smManager && smManager->isEnabled())
  91. return smManager->disable();
  92. disableMouse();
  93. disableKeyboard();
  94. }
  95. //------------------------------------------------------------------------------
  96. // Not used on OS X
  97. void Input::activate()
  98. {
  99. }
  100. //------------------------------------------------------------------------------
  101. // Not used on OS X
  102. void Input::deactivate()
  103. {
  104. }
  105. //------------------------------------------------------------------------------
  106. // Not used on OS X
  107. void Input::reactivate()
  108. {
  109. }
  110. //------------------------------------------------------------------------------
  111. // Print the entire input state to the console
  112. // Not yet implemented or unnecessary. Will resolve in the next platform update
  113. void Input::echoInputState()
  114. {
  115. }
  116. //------------------------------------------------------------------------------
  117. // Accesses the global input manager to see if it is enabled
  118. bool Input::isEnabled()
  119. {
  120. if (smManager)
  121. return smManager->isEnabled();
  122. return false;
  123. }
  124. //------------------------------------------------------------------------------
  125. // Accesses the global input manager to see if its mouse is enabled
  126. bool Input::isMouseEnabled()
  127. {
  128. osxInputManager* inputManager = (osxInputManager*)getManager();
  129. if (inputManager)
  130. return inputManager->isMouseEnabled();
  131. return false;
  132. }
  133. //------------------------------------------------------------------------------
  134. // Accesses the global input manager to see if its keyboard is enabled
  135. bool Input::isKeyboardEnabled()
  136. {
  137. osxInputManager* inputManager = (osxInputManager*)getManager();
  138. if (inputManager)
  139. return inputManager->isKeyboardEnabled();
  140. return false;
  141. }
  142. //------------------------------------------------------------------------------
  143. // Does nothing on OS X.
  144. bool Input::isActive()
  145. {
  146. // Input is always "active" in an OS X application
  147. // We manually enable or disable it, but the OS
  148. // always assumes it is running
  149. return true;
  150. }
  151. //------------------------------------------------------------------------------
  152. // Access the global osxInputManager and enables its mouse
  153. void Input::enableMouse()
  154. {
  155. osxInputManager *inputManager = (osxInputManager *) getManager();
  156. if (inputManager)
  157. return inputManager->enableMouse();
  158. }
  159. //------------------------------------------------------------------------------
  160. // Access the global osxInputManager and disables its mouse
  161. void Input::disableMouse()
  162. {
  163. osxInputManager *inputManager = (osxInputManager *) getManager();
  164. if (inputManager)
  165. return inputManager->disableMouse();
  166. }
  167. //------------------------------------------------------------------------------
  168. // Access the global osxInputManager and enables its keyboard
  169. void Input::enableKeyboard()
  170. {
  171. osxInputManager *inputManager = (osxInputManager *) getManager();
  172. if (inputManager)
  173. inputManager->enableKeyboard();
  174. }
  175. //------------------------------------------------------------------------------
  176. // Access the global osxInputManager and enables its keyboard
  177. void Input::disableKeyboard()
  178. {
  179. osxInputManager *inputManager = (osxInputManager *) getManager();
  180. if (inputManager)
  181. return inputManager->disableKeyboard();
  182. }
  183. //------------------------------------------------------------------------------
  184. // This is essentially the same as enableKeyboard. The redunancy is due to
  185. // keep parallel functionality with Windows, which has a different usage
  186. // for activateKeyboard due to DirectInput
  187. bool Input::activateKeyboard()
  188. {
  189. osxInputManager *inputManager = (osxInputManager *) getManager();
  190. if (inputManager)
  191. {
  192. inputManager->enableKeyboard();
  193. return true;
  194. }
  195. return false;
  196. }
  197. //------------------------------------------------------------------------------
  198. // This is essentially the same as disableKeyboard. The redunancy is due to
  199. // keep parallel functionality with Windows, which has a different usage
  200. // for deactivateKeyboard due to DirectInput
  201. void Input::deactivateKeyboard()
  202. {
  203. osxInputManager *inputManager = (osxInputManager *) getManager();
  204. if (inputManager)
  205. inputManager->disableKeyboard();
  206. }
  207. //------------------------------------------------------------------------------
  208. // Enable joystick input
  209. bool Input::enableJoystick()
  210. {
  211. return false;
  212. }
  213. //------------------------------------------------------------------------------
  214. // Disable joystick input
  215. void Input::disableJoystick()
  216. {
  217. }
  218. //------------------------------------------------------------------------------
  219. // Handles platform and game input processing separately
  220. void Input::process()
  221. {
  222. // Perform any initial processing
  223. // Run the input manager's process
  224. smManager->process();
  225. }
  226. //------------------------------------------------------------------------------
  227. // Returns the single instances of smManager, an osxInputManager in this case
  228. InputManager *Input::getManager()
  229. {
  230. return smManager;
  231. }
  232. //------------------------------------------------------------------------------
  233. // Not yet implemented. Will resolve in the next platform update
  234. U16 Input::getKeyCode(U16 asciiCode)
  235. {
  236. return 0;
  237. }
  238. //------------------------------------------------------------------------------
  239. // Not yet implemented. Will resolve in the next platform update
  240. U16 Input::getAscii(U16 keyCode, KEY_STATE keyState)
  241. {
  242. return 0;
  243. }
  244. //------------------------------------------------------------------------------
  245. // Not yet implemented. Will resolve in the next platform update
  246. void Input::pushCursor(S32 cursorID)
  247. {
  248. CursorManager* cm = getCursorManager();
  249. if(cm)
  250. cm->pushCursor(cursorID);
  251. }
  252. //------------------------------------------------------------------------------
  253. // Not yet implemented. Will resolve in the next platform update
  254. void Input::popCursor()
  255. {
  256. CursorManager* cm = getCursorManager();
  257. if(cm)
  258. cm->popCursor();
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Not yet implemented. Will resolve in the next platform update
  262. void Input::refreshCursor()
  263. {
  264. }
  265. //------------------------------------------------------------------------------
  266. U32 Input::getDoubleClickTime()
  267. {
  268. // Get system specified double click time
  269. NSTimeInterval doubleInterval = [NSEvent doubleClickInterval];
  270. return doubleInterval * 1000;
  271. }
  272. //------------------------------------------------------------------------------
  273. S32 Input::getDoubleClickWidth()
  274. {
  275. // this is an arbitrary value.
  276. return 10;
  277. }
  278. //------------------------------------------------------------------------------
  279. S32 Input::getDoubleClickHeight()
  280. {
  281. return getDoubleClickWidth();
  282. }
  283. //------------------------------------------------------------------------------
  284. // Not yet implemented. Will resolve in the next platform update
  285. void Input::setCursorPos(S32 x, S32 y)
  286. {
  287. }
  288. //-----------------------------------------------------------------------------
  289. // Not yet implemented. Will resolve in the next platform update
  290. void Input::setCursorState(bool on)
  291. {
  292. if(!smCursorGuard && on)
  293. {
  294. //We are turning on the native cursor
  295. [NSCursor unhide];
  296. }
  297. else if(smCursorGuard && !on)
  298. {
  299. //We are turning off the native cursor
  300. [NSCursor hide];
  301. }
  302. smCursorGuard = on;
  303. }
  304. //-----------------------------------------------------------------------------
  305. // Not yet implemented. Will resolve in the next platform update
  306. void Input::setCursorShape(U32 cursorID)
  307. {
  308. NSCursor *cur;
  309. switch (cursorID) {
  310. case CursorManager::curArrow:
  311. cur = [NSCursor arrowCursor];
  312. [cur set];
  313. default:
  314. cur = [NSCursor arrowCursor];
  315. [cur set];
  316. }
  317. }
  318. #pragma mark ---- Platform Namespace Functions ----
  319. //------------------------------------------------------------------------------
  320. // Not yet implemented. Will resolve in the next platform update
  321. const char *Platform::getClipboard()
  322. {
  323. return NULL;
  324. }
  325. //-----------------------------------------------------------------------------
  326. // Not yet implemented. Will resolve in the next platform update
  327. bool Platform::setClipboard(const char *text)
  328. {
  329. return false;
  330. }
  331. //-----------------------------------------------------------------------------
  332. // Not yet implemented. Will resolve in the next platform update
  333. void Platform::enableKeyboardTranslation(void)
  334. {
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Not yet implemented. Will resolve in the next platform update
  338. void Platform::disableKeyboardTranslation(void)
  339. {
  340. }
  341. //--------------------------------------------------------------------------
  342. //#pragma message("input remap table might need tweaking - rumors of ibooks having diff virt keycodes, might need intermediate remap...")
  343. static U8 VcodeRemap[256] =
  344. {
  345. KEY_A, // 0x00
  346. KEY_S, // 0x01
  347. KEY_D, // 0x02
  348. KEY_F, // 0x03
  349. KEY_H, // 0x04
  350. KEY_G, // 0x05
  351. KEY_Z, // 0x06
  352. KEY_X, // 0x07
  353. KEY_C, // 0x08
  354. KEY_V, // 0x09
  355. KEY_Y, // 0x0A // this is questionable - not normal Y code
  356. KEY_B, // 0x0B
  357. KEY_Q, // 0x0C
  358. KEY_W, // 0x0D
  359. KEY_E, // 0x0E
  360. KEY_R, // 0x0F
  361. KEY_Y, // 0x10
  362. KEY_T, // 0x11
  363. KEY_1, // 0x12
  364. KEY_2, // 0x13
  365. KEY_3, // 0x14
  366. KEY_4, // 0x15
  367. KEY_6, // 0x16
  368. KEY_5, // 0x17
  369. KEY_EQUALS, // 0x18
  370. KEY_9, // 0x19
  371. KEY_7, // 0x1A
  372. KEY_MINUS, // 0x1B
  373. KEY_8, // 0x1C
  374. KEY_0, // 0x1D
  375. KEY_RBRACKET, // 0x1E
  376. KEY_O, // 0x1F
  377. KEY_U, // 0x20
  378. KEY_LBRACKET, // 0x21
  379. KEY_I, // 0x22
  380. KEY_P, // 0x23
  381. KEY_RETURN, // 0x24
  382. KEY_L, // 0x25
  383. KEY_J, // 0x26
  384. KEY_APOSTROPHE, // 0x27
  385. KEY_K, // 0x28
  386. KEY_SEMICOLON, // 0x29
  387. KEY_BACKSLASH, // 0x2A
  388. KEY_COMMA, // 0x2B
  389. KEY_SLASH, // 0x2C
  390. KEY_N, // 0x2D
  391. KEY_M, // 0x2E
  392. KEY_PERIOD, // 0x2F
  393. KEY_TAB, // 0x30
  394. KEY_SPACE, // 0x31
  395. KEY_TILDE, // 0x32
  396. KEY_BACKSPACE, // 0x33
  397. 0, // 0x34 //?
  398. KEY_ESCAPE, // 0x35
  399. 0, // 0x36 //?
  400. KEY_ALT, // 0x37 // best mapping for mac Cmd key
  401. KEY_LSHIFT, // 0x38
  402. KEY_CAPSLOCK, // 0x39
  403. KEY_MAC_OPT, // 0x3A // direct map mac Option key -- better than KEY_WIN_WINDOWS
  404. KEY_CONTROL, // 0x3B
  405. KEY_RSHIFT, // 0x3C
  406. 0, // 0x3D
  407. 0, // 0x3E
  408. 0, // 0x3F
  409. 0, // 0x40
  410. KEY_DECIMAL, // 0x41
  411. 0, // 0x42
  412. KEY_MULTIPLY, // 0x43
  413. 0, // 0x44
  414. KEY_ADD, // 0x45
  415. KEY_SUBTRACT, // 0x46 // secondary code?
  416. KEY_NUMLOCK, // 0x47 // also known as Clear on mac...
  417. KEY_SEPARATOR, // 0x48 // secondary code? for KPEqual
  418. 0, // 0x49
  419. 0, // 0x4A
  420. KEY_DIVIDE, // 0x4B
  421. KEY_NUMPADENTER, // 0x4C
  422. KEY_DIVIDE, // 0x4D // secondary code?
  423. KEY_SUBTRACT, // 0x4E
  424. 0, // 0x4F
  425. 0, // 0x50
  426. KEY_SEPARATOR, // 0x51 // WHAT IS SEP? This is KPEqual on mac.
  427. KEY_NUMPAD0, // 0x52
  428. KEY_NUMPAD1, // 0x53
  429. KEY_NUMPAD2, // 0x54
  430. KEY_NUMPAD3, // 0x55
  431. KEY_NUMPAD4, // 0x56
  432. KEY_NUMPAD5, // 0x57
  433. KEY_NUMPAD6, // 0x58
  434. KEY_NUMPAD7, // 0x59
  435. 0, // 0x5A
  436. KEY_NUMPAD8, // 0x5B
  437. KEY_NUMPAD9, // 0x5C
  438. 0, // 0x5D
  439. 0, // 0x5E
  440. 0, // 0x5F
  441. KEY_F5, // 0x60
  442. KEY_F6, // 0x61
  443. KEY_F7, // 0x62
  444. KEY_F3, // 0x63
  445. KEY_F8, // 0x64
  446. KEY_F9, // 0x65
  447. 0, // 0x66
  448. KEY_F11, // 0x67
  449. 0, // 0x68
  450. KEY_PRINT, // 0x69
  451. 0, // 0x6A
  452. KEY_SCROLLLOCK, // 0x6B
  453. 0, // 0x6C
  454. KEY_F10, // 0x6D
  455. 0, // 0x6E
  456. KEY_F12, // 0x6F
  457. 0, // 0x70
  458. KEY_PAUSE, // 0x71
  459. KEY_INSERT, // 0x72 // also known as mac Help
  460. KEY_HOME, // 0x73
  461. KEY_PAGE_UP, // 0x74
  462. KEY_DELETE, // 0x75 // FwdDel
  463. KEY_F4, // 0x76
  464. KEY_END, // 0x77
  465. KEY_F2, // 0x78
  466. KEY_PAGE_DOWN, // 0x79
  467. KEY_F1, // 0x7A
  468. KEY_LEFT, // 0x7B
  469. KEY_RIGHT, // 0x7C
  470. KEY_DOWN, // 0x7D
  471. KEY_UP, // 0x7E
  472. 0, // 0x7F
  473. 0, // 0x80
  474. 0, // 0x81
  475. 0, // 0x82
  476. 0, // 0x83
  477. 0, // 0x84
  478. 0, // 0x85
  479. 0, // 0x86
  480. 0, // 0x87
  481. 0, // 0x88
  482. 0, // 0x89
  483. 0, // 0x8A
  484. 0, // 0x8B
  485. 0, // 0x8C
  486. 0, // 0x8D
  487. 0, // 0x8E
  488. 0, // 0x8F
  489. 0, // 0x90
  490. 0, // 0x91
  491. 0, // 0x92
  492. 0, // 0x93
  493. 0, // 0x94
  494. 0, // 0x95
  495. 0, // 0x96
  496. 0, // 0x97
  497. 0, // 0x98
  498. 0, // 0x99
  499. 0, // 0x9A
  500. 0, // 0x9B
  501. 0, // 0x9C
  502. 0, // 0x9D
  503. 0, // 0x9E
  504. 0, // 0x9F
  505. 0, // 0xA0
  506. 0, // 0xA1
  507. 0, // 0xA2
  508. 0, // 0xA3
  509. 0, // 0xA4
  510. 0, // 0xA5
  511. 0, // 0xA6
  512. 0, // 0xA7
  513. 0, // 0xA8
  514. 0, // 0xA9
  515. 0, // 0xAA
  516. 0, // 0xAB
  517. 0, // 0xAC
  518. 0, // 0xAD
  519. 0, // 0xAE
  520. 0, // 0xAF
  521. 0, // 0xB0
  522. 0, // 0xB1
  523. 0, // 0xB2
  524. 0, // 0xB3
  525. 0, // 0xB4
  526. 0, // 0xB5
  527. 0, // 0xB6
  528. 0, // 0xB7
  529. 0, // 0xB8
  530. 0, // 0xB9
  531. 0, // 0xBA
  532. 0, // 0xBB
  533. 0, // 0xBC
  534. 0, // 0xBD
  535. 0, // 0xBE
  536. 0, // 0xBF
  537. 0, // 0xC0
  538. 0, // 0xC1
  539. 0, // 0xC2
  540. 0, // 0xC3
  541. 0, // 0xC4
  542. 0, // 0xC5
  543. 0, // 0xC6
  544. 0, // 0xC7
  545. 0, // 0xC8
  546. 0, // 0xC9
  547. 0, // 0xCA
  548. 0, // 0xCB
  549. 0, // 0xCC
  550. 0, // 0xCD
  551. 0, // 0xCE
  552. 0, // 0xCF
  553. 0, // 0xD0
  554. 0, // 0xD1
  555. 0, // 0xD2
  556. 0, // 0xD3
  557. 0, // 0xD4
  558. 0, // 0xD5
  559. 0, // 0xD6
  560. 0, // 0xD7
  561. 0, // 0xD8
  562. 0, // 0xD9
  563. 0, // 0xDA
  564. 0, // 0xDB
  565. 0, // 0xDC
  566. 0, // 0xDD
  567. 0, // 0xDE
  568. 0, // 0xDF
  569. 0, // 0xE0
  570. 0, // 0xE1
  571. 0, // 0xE2
  572. 0, // 0xE3
  573. 0, // 0xE4
  574. 0, // 0xE5
  575. 0, // 0xE6
  576. 0, // 0xE7
  577. 0, // 0xE8
  578. 0, // 0xE9
  579. 0, // 0xEA
  580. 0, // 0xEB
  581. 0, // 0xEC
  582. 0, // 0xED
  583. 0, // 0xEE
  584. 0, // 0xEF
  585. 0, // 0xF0
  586. 0, // 0xF1
  587. 0, // 0xF2
  588. 0, // 0xF3
  589. 0, // 0xF4
  590. 0, // 0xF5
  591. 0, // 0xF6
  592. 0, // 0xF7
  593. 0, // 0xF8
  594. 0, // 0xF9
  595. 0, // 0xFA
  596. 0, // 0xFB
  597. 0, // 0xFC
  598. 0, // 0xFD
  599. 0, // 0xFE
  600. 0 // 0xFF
  601. };
  602. U8 TranslateOSKeyCode(U8 vcode)
  603. {
  604. return VcodeRemap[vcode];
  605. }