osxInput.mm 23 KB

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