osxInput.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. CursorManager* cm = getCursorManager();
  247. if(cm)
  248. cm->pushCursor(cursorID);
  249. }
  250. //------------------------------------------------------------------------------
  251. // Not yet implemented. Will resolve in the next platform update
  252. void Input::popCursor()
  253. {
  254. CursorManager* cm = getCursorManager();
  255. if(cm)
  256. cm->popCursor();
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Not yet implemented. Will resolve in the next platform update
  260. void Input::refreshCursor()
  261. {
  262. }
  263. //------------------------------------------------------------------------------
  264. U32 Input::getDoubleClickTime()
  265. {
  266. // Get system specified double click time
  267. NSTimeInterval doubleInterval = [NSEvent doubleClickInterval];
  268. return doubleInterval * 1000;
  269. }
  270. //------------------------------------------------------------------------------
  271. S32 Input::getDoubleClickWidth()
  272. {
  273. // this is an arbitrary value.
  274. return 10;
  275. }
  276. //------------------------------------------------------------------------------
  277. S32 Input::getDoubleClickHeight()
  278. {
  279. return getDoubleClickWidth();
  280. }
  281. //------------------------------------------------------------------------------
  282. // Not yet implemented. Will resolve in the next platform update
  283. void Input::setCursorPos(S32 x, S32 y)
  284. {
  285. }
  286. //-----------------------------------------------------------------------------
  287. // Not yet implemented. Will resolve in the next platform update
  288. void Input::setCursorState(bool on)
  289. {
  290. on ? [NSCursor unhide] : [NSCursor hide];
  291. }
  292. //-----------------------------------------------------------------------------
  293. // Not yet implemented. Will resolve in the next platform update
  294. void Input::setCursorShape(U32 cursorID)
  295. {
  296. NSCursor *cur;
  297. switch (cursorID) {
  298. case CursorManager::curArrow:
  299. cur = [NSCursor arrowCursor];
  300. [cur set];
  301. default:
  302. cur = [NSCursor arrowCursor];
  303. [cur set];
  304. }
  305. }
  306. #pragma mark ---- Platform Namespace Functions ----
  307. //------------------------------------------------------------------------------
  308. // Not yet implemented. Will resolve in the next platform update
  309. const char *Platform::getClipboard()
  310. {
  311. return NULL;
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Not yet implemented. Will resolve in the next platform update
  315. bool Platform::setClipboard(const char *text)
  316. {
  317. return false;
  318. }
  319. //-----------------------------------------------------------------------------
  320. // Not yet implemented. Will resolve in the next platform update
  321. void Platform::enableKeyboardTranslation(void)
  322. {
  323. }
  324. //-----------------------------------------------------------------------------
  325. // Not yet implemented. Will resolve in the next platform update
  326. void Platform::disableKeyboardTranslation(void)
  327. {
  328. }
  329. //--------------------------------------------------------------------------
  330. //#pragma message("input remap table might need tweaking - rumors of ibooks having diff virt keycodes, might need intermediate remap...")
  331. static U8 VcodeRemap[256] =
  332. {
  333. KEY_A, // 0x00
  334. KEY_S, // 0x01
  335. KEY_D, // 0x02
  336. KEY_F, // 0x03
  337. KEY_H, // 0x04
  338. KEY_G, // 0x05
  339. KEY_Z, // 0x06
  340. KEY_X, // 0x07
  341. KEY_C, // 0x08
  342. KEY_V, // 0x09
  343. KEY_Y, // 0x0A // this is questionable - not normal Y code
  344. KEY_B, // 0x0B
  345. KEY_Q, // 0x0C
  346. KEY_W, // 0x0D
  347. KEY_E, // 0x0E
  348. KEY_R, // 0x0F
  349. KEY_Y, // 0x10
  350. KEY_T, // 0x11
  351. KEY_1, // 0x12
  352. KEY_2, // 0x13
  353. KEY_3, // 0x14
  354. KEY_4, // 0x15
  355. KEY_6, // 0x16
  356. KEY_5, // 0x17
  357. KEY_EQUALS, // 0x18
  358. KEY_9, // 0x19
  359. KEY_7, // 0x1A
  360. KEY_MINUS, // 0x1B
  361. KEY_8, // 0x1C
  362. KEY_0, // 0x1D
  363. KEY_RBRACKET, // 0x1E
  364. KEY_O, // 0x1F
  365. KEY_U, // 0x20
  366. KEY_LBRACKET, // 0x21
  367. KEY_I, // 0x22
  368. KEY_P, // 0x23
  369. KEY_RETURN, // 0x24
  370. KEY_L, // 0x25
  371. KEY_J, // 0x26
  372. KEY_APOSTROPHE, // 0x27
  373. KEY_K, // 0x28
  374. KEY_SEMICOLON, // 0x29
  375. KEY_BACKSLASH, // 0x2A
  376. KEY_COMMA, // 0x2B
  377. KEY_SLASH, // 0x2C
  378. KEY_N, // 0x2D
  379. KEY_M, // 0x2E
  380. KEY_PERIOD, // 0x2F
  381. KEY_TAB, // 0x30
  382. KEY_SPACE, // 0x31
  383. KEY_TILDE, // 0x32
  384. KEY_BACKSPACE, // 0x33
  385. 0, // 0x34 //?
  386. KEY_ESCAPE, // 0x35
  387. 0, // 0x36 //?
  388. KEY_ALT, // 0x37 // best mapping for mac Cmd key
  389. KEY_LSHIFT, // 0x38
  390. KEY_CAPSLOCK, // 0x39
  391. KEY_MAC_OPT, // 0x3A // direct map mac Option key -- better than KEY_WIN_WINDOWS
  392. KEY_CONTROL, // 0x3B
  393. KEY_RSHIFT, // 0x3C
  394. 0, // 0x3D
  395. 0, // 0x3E
  396. 0, // 0x3F
  397. 0, // 0x40
  398. KEY_DECIMAL, // 0x41
  399. 0, // 0x42
  400. KEY_MULTIPLY, // 0x43
  401. 0, // 0x44
  402. KEY_ADD, // 0x45
  403. KEY_SUBTRACT, // 0x46 // secondary code?
  404. KEY_NUMLOCK, // 0x47 // also known as Clear on mac...
  405. KEY_SEPARATOR, // 0x48 // secondary code? for KPEqual
  406. 0, // 0x49
  407. 0, // 0x4A
  408. KEY_DIVIDE, // 0x4B
  409. KEY_NUMPADENTER, // 0x4C
  410. KEY_DIVIDE, // 0x4D // secondary code?
  411. KEY_SUBTRACT, // 0x4E
  412. 0, // 0x4F
  413. 0, // 0x50
  414. KEY_SEPARATOR, // 0x51 // WHAT IS SEP? This is KPEqual on mac.
  415. KEY_NUMPAD0, // 0x52
  416. KEY_NUMPAD1, // 0x53
  417. KEY_NUMPAD2, // 0x54
  418. KEY_NUMPAD3, // 0x55
  419. KEY_NUMPAD4, // 0x56
  420. KEY_NUMPAD5, // 0x57
  421. KEY_NUMPAD6, // 0x58
  422. KEY_NUMPAD7, // 0x59
  423. 0, // 0x5A
  424. KEY_NUMPAD8, // 0x5B
  425. KEY_NUMPAD9, // 0x5C
  426. 0, // 0x5D
  427. 0, // 0x5E
  428. 0, // 0x5F
  429. KEY_F5, // 0x60
  430. KEY_F6, // 0x61
  431. KEY_F7, // 0x62
  432. KEY_F3, // 0x63
  433. KEY_F8, // 0x64
  434. KEY_F9, // 0x65
  435. 0, // 0x66
  436. KEY_F11, // 0x67
  437. 0, // 0x68
  438. KEY_PRINT, // 0x69
  439. 0, // 0x6A
  440. KEY_SCROLLLOCK, // 0x6B
  441. 0, // 0x6C
  442. KEY_F10, // 0x6D
  443. 0, // 0x6E
  444. KEY_F12, // 0x6F
  445. 0, // 0x70
  446. KEY_PAUSE, // 0x71
  447. KEY_INSERT, // 0x72 // also known as mac Help
  448. KEY_HOME, // 0x73
  449. KEY_PAGE_UP, // 0x74
  450. KEY_DELETE, // 0x75 // FwdDel
  451. KEY_F4, // 0x76
  452. KEY_END, // 0x77
  453. KEY_F2, // 0x78
  454. KEY_PAGE_DOWN, // 0x79
  455. KEY_F1, // 0x7A
  456. KEY_LEFT, // 0x7B
  457. KEY_RIGHT, // 0x7C
  458. KEY_DOWN, // 0x7D
  459. KEY_UP, // 0x7E
  460. 0, // 0x7F
  461. 0, // 0x80
  462. 0, // 0x81
  463. 0, // 0x82
  464. 0, // 0x83
  465. 0, // 0x84
  466. 0, // 0x85
  467. 0, // 0x86
  468. 0, // 0x87
  469. 0, // 0x88
  470. 0, // 0x89
  471. 0, // 0x8A
  472. 0, // 0x8B
  473. 0, // 0x8C
  474. 0, // 0x8D
  475. 0, // 0x8E
  476. 0, // 0x8F
  477. 0, // 0x90
  478. 0, // 0x91
  479. 0, // 0x92
  480. 0, // 0x93
  481. 0, // 0x94
  482. 0, // 0x95
  483. 0, // 0x96
  484. 0, // 0x97
  485. 0, // 0x98
  486. 0, // 0x99
  487. 0, // 0x9A
  488. 0, // 0x9B
  489. 0, // 0x9C
  490. 0, // 0x9D
  491. 0, // 0x9E
  492. 0, // 0x9F
  493. 0, // 0xA0
  494. 0, // 0xA1
  495. 0, // 0xA2
  496. 0, // 0xA3
  497. 0, // 0xA4
  498. 0, // 0xA5
  499. 0, // 0xA6
  500. 0, // 0xA7
  501. 0, // 0xA8
  502. 0, // 0xA9
  503. 0, // 0xAA
  504. 0, // 0xAB
  505. 0, // 0xAC
  506. 0, // 0xAD
  507. 0, // 0xAE
  508. 0, // 0xAF
  509. 0, // 0xB0
  510. 0, // 0xB1
  511. 0, // 0xB2
  512. 0, // 0xB3
  513. 0, // 0xB4
  514. 0, // 0xB5
  515. 0, // 0xB6
  516. 0, // 0xB7
  517. 0, // 0xB8
  518. 0, // 0xB9
  519. 0, // 0xBA
  520. 0, // 0xBB
  521. 0, // 0xBC
  522. 0, // 0xBD
  523. 0, // 0xBE
  524. 0, // 0xBF
  525. 0, // 0xC0
  526. 0, // 0xC1
  527. 0, // 0xC2
  528. 0, // 0xC3
  529. 0, // 0xC4
  530. 0, // 0xC5
  531. 0, // 0xC6
  532. 0, // 0xC7
  533. 0, // 0xC8
  534. 0, // 0xC9
  535. 0, // 0xCA
  536. 0, // 0xCB
  537. 0, // 0xCC
  538. 0, // 0xCD
  539. 0, // 0xCE
  540. 0, // 0xCF
  541. 0, // 0xD0
  542. 0, // 0xD1
  543. 0, // 0xD2
  544. 0, // 0xD3
  545. 0, // 0xD4
  546. 0, // 0xD5
  547. 0, // 0xD6
  548. 0, // 0xD7
  549. 0, // 0xD8
  550. 0, // 0xD9
  551. 0, // 0xDA
  552. 0, // 0xDB
  553. 0, // 0xDC
  554. 0, // 0xDD
  555. 0, // 0xDE
  556. 0, // 0xDF
  557. 0, // 0xE0
  558. 0, // 0xE1
  559. 0, // 0xE2
  560. 0, // 0xE3
  561. 0, // 0xE4
  562. 0, // 0xE5
  563. 0, // 0xE6
  564. 0, // 0xE7
  565. 0, // 0xE8
  566. 0, // 0xE9
  567. 0, // 0xEA
  568. 0, // 0xEB
  569. 0, // 0xEC
  570. 0, // 0xED
  571. 0, // 0xEE
  572. 0, // 0xEF
  573. 0, // 0xF0
  574. 0, // 0xF1
  575. 0, // 0xF2
  576. 0, // 0xF3
  577. 0, // 0xF4
  578. 0, // 0xF5
  579. 0, // 0xF6
  580. 0, // 0xF7
  581. 0, // 0xF8
  582. 0, // 0xF9
  583. 0, // 0xFA
  584. 0, // 0xFB
  585. 0, // 0xFC
  586. 0, // 0xFD
  587. 0, // 0xFE
  588. 0 // 0xFF
  589. };
  590. U8 TranslateOSKeyCode(U8 vcode)
  591. {
  592. return VcodeRemap[vcode];
  593. }