iOSInput.mm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. #include "platformiOS/platformiOS.h"
  23. #include "platformiOS/iOSUtil.h"
  24. #include "platform/platformInput.h"
  25. #include "platform/event.h"
  26. #include "console/console.h"
  27. #include "game/gameInterface.h"
  28. #include "string/Unicode.h"
  29. #include "gui/guiCanvas.h"
  30. // <Mat> just some random number 50, we'll get a proper value later
  31. #define IOS_DOUBLE_CLICK_TIME ( 50 * 60.0f * 1000.0f)
  32. // Static class variables:
  33. InputManager* Input::smManager;
  34. bool Input::smActive;
  35. CursorManager* Input::smCursorManager = 0;
  36. bool gInputEnabled = false;
  37. bool gMouseEnabled = false;
  38. bool gKBEnabled = false;
  39. bool gMouseActive = false;
  40. bool gKBActive = false;
  41. //------------------------------------------------------------------------------
  42. // Helper functions. Should migrate to an InputManager object at some point.
  43. bool enableKeyboard(void);
  44. void disableKeyboard(void);
  45. bool activateKeyboard(void);
  46. void deactivateKeyboard(void);
  47. bool enableMouse(void);
  48. void disableMouse(void);
  49. bool activateMouse(void);
  50. void deactivateMouse(void);
  51. static void fillAsciiTable();
  52. struct touchEvent {
  53. S32 number;
  54. S32 x;
  55. S32 y;
  56. touchEvent( S32 a, S32 b, S32 c ) {
  57. number = a;
  58. x = b;
  59. y = c;
  60. }
  61. };
  62. Vector<touchEvent> TouchMoveEvents;//<Mat> to make sure we don't have multiple events per frame
  63. Vector<touchEvent> TouchDownEvents;
  64. Vector<touchEvent> TouchUpEvents;
  65. // EFM - BEGIN TOUCH CHANGES
  66. #define MAX_TOUCH_EVENTS 5
  67. struct touchTracker {
  68. S32 lastX;
  69. S32 lastY;
  70. Vector<touchEvent> downEvents;
  71. Vector<touchEvent> moveEvents;
  72. Vector<touchEvent> upEvents;
  73. };
  74. touchTracker CombinedTouchEvents[MAX_TOUCH_EVENTS];
  75. struct touchCorrelation {
  76. S32 lastX;
  77. S32 lastY;
  78. };
  79. touchCorrelation lastTouches[MAX_TOUCH_EVENTS];
  80. // EFM - END TOUCH CHANGES
  81. //Luma: Tap support
  82. Vector<touchEvent> TouchTapEvents;
  83. int processMultipleTouches();
  84. //------------------------------------------------------------------------------
  85. //
  86. // This function gets the standard ASCII code corresponding to our key code
  87. // and the existing modifier key state.
  88. //
  89. //------------------------------------------------------------------------------
  90. struct AsciiData
  91. {
  92. struct KeyData
  93. {
  94. U16 ascii;
  95. bool isDeadChar;
  96. };
  97. KeyData upper;
  98. KeyData lower;
  99. KeyData goofy;
  100. };
  101. #define NUM_KEYS ( KEY_OEM_102 + 1 )
  102. #define KEY_FIRST KEY_ESCAPE
  103. static AsciiData AsciiTable[NUM_KEYS];
  104. void Input::enableMouse()
  105. {
  106. // Do nothing on iOS
  107. }
  108. void Input::disableMouse()
  109. {
  110. // Do nothing on iOS
  111. }
  112. void Input::enableKeyboard()
  113. {
  114. // Do nothing on iOS
  115. }
  116. void Input::disableKeyboard()
  117. {
  118. // Do nothing on iOS
  119. }
  120. bool Input::isMouseEnabled()
  121. {
  122. return true;
  123. }
  124. bool Input::isKeyboardEnabled()
  125. {
  126. return false;
  127. }
  128. //--------------------------------------------------------------------------
  129. void Input::init()
  130. {
  131. Con::printf( "Input Init:" );
  132. destroy();
  133. smManager = NULL;
  134. //smManager = new iOSInputManager();
  135. smActive = false;
  136. // stop the double-cursor thing
  137. Con::setBoolVariable("$pref::Gui::noClampTorqueCursorToWindow", true);
  138. // enable main input
  139. Input::enable();
  140. // Startup the Cursor Manager
  141. if(!smCursorManager)
  142. {
  143. smCursorManager = new CursorManager();
  144. if(smCursorManager)
  145. {
  146. // Add the arrow cursor to the stack
  147. smCursorManager->pushCursor(CursorManager::curArrow);
  148. }
  149. else
  150. {
  151. Con::printf(" Cursor Manager not enabled.");
  152. }
  153. }
  154. for(int i = 0 ; i < MAX_TOUCH_EVENTS; i++ )
  155. {
  156. lastTouches[i].lastX = -1;
  157. lastTouches[i].lastY = -1;
  158. }
  159. Con::printf( "" );
  160. }
  161. //------------------------------------------------------------------------------
  162. ConsoleFunction( isJoystickDetected, bool, 1, 1, "Always false on the iOS." )
  163. {
  164. /*
  165. argc; argv;
  166. return( DInputDevice::joystickDetected() );
  167. */
  168. return(false);
  169. }
  170. //------------------------------------------------------------------------------
  171. ConsoleFunction( getJoystickAxes, const char*, 2, 2, "(handle instance)" )
  172. {
  173. return( "" );
  174. }
  175. //------------------------------------------------------------------------------
  176. static void fillAsciiTable()
  177. {
  178. }
  179. //------------------------------------------------------------------------------
  180. U16 Input::getKeyCode( U16 asciiCode )
  181. {
  182. U16 keyCode = 0;
  183. U16 i;
  184. // This is done three times so the lowerkey will always
  185. // be found first. Some foreign keyboards have duplicate
  186. // chars on some keys.
  187. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  188. {
  189. if ( AsciiTable[i].lower.ascii == asciiCode )
  190. {
  191. keyCode = i;
  192. break;
  193. };
  194. }
  195. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  196. {
  197. if ( AsciiTable[i].upper.ascii == asciiCode )
  198. {
  199. keyCode = i;
  200. break;
  201. };
  202. }
  203. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  204. {
  205. if ( AsciiTable[i].goofy.ascii == asciiCode )
  206. {
  207. keyCode = i;
  208. break;
  209. };
  210. }
  211. return( keyCode );
  212. }
  213. //------------------------------------------------------------------------------
  214. U16 Input::getAscii( U16 keyCode, KEY_STATE keyState )
  215. {
  216. if ( keyCode >= NUM_KEYS )
  217. return 0;
  218. switch ( keyState )
  219. {
  220. case STATE_LOWER:
  221. return AsciiTable[keyCode].lower.ascii;
  222. case STATE_UPPER:
  223. return AsciiTable[keyCode].upper.ascii;
  224. case STATE_GOOFY:
  225. return AsciiTable[keyCode].goofy.ascii;
  226. default:
  227. return(0);
  228. }
  229. }
  230. //------------------------------------------------------------------------------
  231. void Input::destroy()
  232. {
  233. // turn us off.
  234. if (gInputEnabled)
  235. disable();
  236. if ( smManager && smManager->isEnabled() )
  237. {
  238. smManager->disable();
  239. delete smManager;
  240. smManager = NULL;
  241. }
  242. }
  243. //------------------------------------------------------------------------------
  244. bool Input::enable()
  245. {
  246. Con::printf( "[]Input::enable." );
  247. gInputEnabled = true;
  248. if ( smManager && !smManager->isEnabled() )
  249. return( smManager->enable() );
  250. enableMouse();
  251. //enableKeyboard();
  252. return( gInputEnabled );
  253. }
  254. //------------------------------------------------------------------------------
  255. void Input::disable()
  256. {
  257. Con::printf( "[]Input::disable." );
  258. gInputEnabled = false;
  259. if ( smManager && smManager->isEnabled() )
  260. smManager->disable();
  261. disableMouse();
  262. //disableKeyboard();
  263. }
  264. //------------------------------------------------------------------------------
  265. void Input::activate()
  266. {
  267. smActive = true;
  268. enableMouse();
  269. // enableKeyboard();
  270. }
  271. //------------------------------------------------------------------------------
  272. void Input::deactivate()
  273. {
  274. //Con::printf( "[]Input::deactivate." );
  275. deactivateMouse();
  276. //deactivateKeyboard();
  277. smActive = false;
  278. }
  279. //------------------------------------------------------------------------------
  280. void Input::reactivate()
  281. {
  282. // don't think mac needs to do anything right now!!!!!! TBD
  283. // This is soo hacky...
  284. // SetForegroundWindow( winState.appWindow );
  285. // PostMessage( winState.appWindow, WM_ACTIVATE, WA_ACTIVE, NULL );
  286. }
  287. //------------------------------------------------------------------------------
  288. bool Input::isEnabled()
  289. {
  290. if ( smManager )
  291. return smManager->isEnabled();
  292. return(gInputEnabled);
  293. }
  294. //------------------------------------------------------------------------------
  295. bool Input::isActive()
  296. {
  297. return smActive;
  298. }
  299. //------------------------------------------------------------------------------
  300. void Input::process()
  301. {
  302. //only gets called once per frame, create touches and accelerometer events here
  303. //post, then pop each event
  304. if(platState.multipleTouchesEnabled) processMultipleTouches();
  305. if (!smActive || !gInputEnabled)
  306. return;
  307. if (!gMouseEnabled || !gMouseActive)
  308. return;
  309. if ( smManager && smManager->isEnabled() && smActive )
  310. smManager->process();
  311. }
  312. //------------------------------------------------------------------------------
  313. InputManager* Input::getManager()
  314. {
  315. return( smManager );
  316. }
  317. //--------------------------------------------------------------------------
  318. //#pragma message("input remap table might need tweaking - rumors of ibooks having diff virt keycodes, might need intermediate remap...")
  319. static U8 VcodeRemap[256] =
  320. {
  321. KEY_A, // 0x00
  322. KEY_S, // 0x01
  323. KEY_D, // 0x02
  324. KEY_F, // 0x03
  325. KEY_H, // 0x04
  326. KEY_G, // 0x05
  327. KEY_Z, // 0x06
  328. KEY_X, // 0x07
  329. KEY_C, // 0x08
  330. KEY_V, // 0x09
  331. KEY_Y, // 0x0A // this is questionable - not normal Y code
  332. KEY_B, // 0x0B
  333. KEY_Q, // 0x0C
  334. KEY_W, // 0x0D
  335. KEY_E, // 0x0E
  336. KEY_R, // 0x0F
  337. KEY_Y, // 0x10
  338. KEY_T, // 0x11
  339. KEY_1, // 0x12
  340. KEY_2, // 0x13
  341. KEY_3, // 0x14
  342. KEY_4, // 0x15
  343. KEY_6, // 0x16
  344. KEY_5, // 0x17
  345. KEY_EQUALS, // 0x18
  346. KEY_9, // 0x19
  347. KEY_7, // 0x1A
  348. KEY_MINUS, // 0x1B
  349. KEY_8, // 0x1C
  350. KEY_0, // 0x1D
  351. KEY_RBRACKET, // 0x1E
  352. KEY_O, // 0x1F
  353. KEY_U, // 0x20
  354. KEY_LBRACKET, // 0x21
  355. KEY_I, // 0x22
  356. KEY_P, // 0x23
  357. KEY_RETURN, // 0x24
  358. KEY_L, // 0x25
  359. KEY_J, // 0x26
  360. KEY_APOSTROPHE, // 0x27
  361. KEY_K, // 0x28
  362. KEY_SEMICOLON, // 0x29
  363. KEY_BACKSLASH, // 0x2A
  364. KEY_COMMA, // 0x2B
  365. KEY_SLASH, // 0x2C
  366. KEY_N, // 0x2D
  367. KEY_M, // 0x2E
  368. KEY_PERIOD, // 0x2F
  369. KEY_TAB, // 0x30
  370. KEY_SPACE, // 0x31
  371. KEY_TILDE, // 0x32
  372. KEY_BACKSPACE, // 0x33
  373. 0, // 0x34 //?
  374. KEY_ESCAPE, // 0x35
  375. 0, // 0x36 //?
  376. KEY_ALT, // 0x37 // best mapping for mac Cmd key
  377. KEY_LSHIFT, // 0x38
  378. KEY_CAPSLOCK, // 0x39
  379. KEY_MAC_OPT, // 0x3A // direct map mac Option key -- better than KEY_WIN_WINDOWS
  380. KEY_CONTROL, // 0x3B
  381. KEY_RSHIFT, // 0x3C
  382. 0, // 0x3D
  383. 0, // 0x3E
  384. 0, // 0x3F
  385. 0, // 0x40
  386. KEY_DECIMAL, // 0x41
  387. 0, // 0x42
  388. KEY_MULTIPLY, // 0x43
  389. 0, // 0x44
  390. KEY_ADD, // 0x45
  391. KEY_SUBTRACT, // 0x46 // secondary code?
  392. KEY_NUMLOCK, // 0x47 // also known as Clear on mac...
  393. KEY_SEPARATOR, // 0x48 // secondary code? for KPEqual
  394. 0, // 0x49
  395. 0, // 0x4A
  396. KEY_DIVIDE, // 0x4B
  397. KEY_NUMPADENTER, // 0x4C
  398. KEY_DIVIDE, // 0x4D // secondary code?
  399. KEY_SUBTRACT, // 0x4E
  400. 0, // 0x4F
  401. 0, // 0x50
  402. KEY_SEPARATOR, // 0x51 // WHAT IS SEP? This is KPEqual on mac.
  403. KEY_NUMPAD0, // 0x52
  404. KEY_NUMPAD1, // 0x53
  405. KEY_NUMPAD2, // 0x54
  406. KEY_NUMPAD3, // 0x55
  407. KEY_NUMPAD4, // 0x56
  408. KEY_NUMPAD5, // 0x57
  409. KEY_NUMPAD6, // 0x58
  410. KEY_NUMPAD7, // 0x59
  411. 0, // 0x5A
  412. KEY_NUMPAD8, // 0x5B
  413. KEY_NUMPAD9, // 0x5C
  414. 0, // 0x5D
  415. 0, // 0x5E
  416. 0, // 0x5F
  417. KEY_F5, // 0x60
  418. KEY_F6, // 0x61
  419. KEY_F7, // 0x62
  420. KEY_F3, // 0x63
  421. KEY_F8, // 0x64
  422. KEY_F9, // 0x65
  423. 0, // 0x66
  424. KEY_F11, // 0x67
  425. 0, // 0x68
  426. KEY_PRINT, // 0x69
  427. 0, // 0x6A
  428. KEY_SCROLLLOCK, // 0x6B
  429. 0, // 0x6C
  430. KEY_F10, // 0x6D
  431. 0, // 0x6E
  432. KEY_F12, // 0x6F
  433. 0, // 0x70
  434. KEY_PAUSE, // 0x71
  435. KEY_INSERT, // 0x72 // also known as mac Help
  436. KEY_HOME, // 0x73
  437. KEY_PAGE_UP, // 0x74
  438. KEY_DELETE, // 0x75 // FwdDel
  439. KEY_F4, // 0x76
  440. KEY_END, // 0x77
  441. KEY_F2, // 0x78
  442. KEY_PAGE_DOWN, // 0x79
  443. KEY_F1, // 0x7A
  444. KEY_LEFT, // 0x7B
  445. KEY_RIGHT, // 0x7C
  446. KEY_DOWN, // 0x7D
  447. KEY_UP, // 0x7E
  448. 0, // 0x7F
  449. 0, // 0x80
  450. 0, // 0x81
  451. 0, // 0x82
  452. 0, // 0x83
  453. 0, // 0x84
  454. 0, // 0x85
  455. 0, // 0x86
  456. 0, // 0x87
  457. 0, // 0x88
  458. 0, // 0x89
  459. 0, // 0x8A
  460. 0, // 0x8B
  461. 0, // 0x8C
  462. 0, // 0x8D
  463. 0, // 0x8E
  464. 0, // 0x8F
  465. 0, // 0x90
  466. 0, // 0x91
  467. 0, // 0x92
  468. 0, // 0x93
  469. 0, // 0x94
  470. 0, // 0x95
  471. 0, // 0x96
  472. 0, // 0x97
  473. 0, // 0x98
  474. 0, // 0x99
  475. 0, // 0x9A
  476. 0, // 0x9B
  477. 0, // 0x9C
  478. 0, // 0x9D
  479. 0, // 0x9E
  480. 0, // 0x9F
  481. 0, // 0xA0
  482. 0, // 0xA1
  483. 0, // 0xA2
  484. 0, // 0xA3
  485. 0, // 0xA4
  486. 0, // 0xA5
  487. 0, // 0xA6
  488. 0, // 0xA7
  489. 0, // 0xA8
  490. 0, // 0xA9
  491. 0, // 0xAA
  492. 0, // 0xAB
  493. 0, // 0xAC
  494. 0, // 0xAD
  495. 0, // 0xAE
  496. 0, // 0xAF
  497. 0, // 0xB0
  498. 0, // 0xB1
  499. 0, // 0xB2
  500. 0, // 0xB3
  501. 0, // 0xB4
  502. 0, // 0xB5
  503. 0, // 0xB6
  504. 0, // 0xB7
  505. 0, // 0xB8
  506. 0, // 0xB9
  507. 0, // 0xBA
  508. 0, // 0xBB
  509. 0, // 0xBC
  510. 0, // 0xBD
  511. 0, // 0xBE
  512. 0, // 0xBF
  513. 0, // 0xC0
  514. 0, // 0xC1
  515. 0, // 0xC2
  516. 0, // 0xC3
  517. 0, // 0xC4
  518. 0, // 0xC5
  519. 0, // 0xC6
  520. 0, // 0xC7
  521. 0, // 0xC8
  522. 0, // 0xC9
  523. 0, // 0xCA
  524. 0, // 0xCB
  525. 0, // 0xCC
  526. 0, // 0xCD
  527. 0, // 0xCE
  528. 0, // 0xCF
  529. 0, // 0xD0
  530. 0, // 0xD1
  531. 0, // 0xD2
  532. 0, // 0xD3
  533. 0, // 0xD4
  534. 0, // 0xD5
  535. 0, // 0xD6
  536. 0, // 0xD7
  537. 0, // 0xD8
  538. 0, // 0xD9
  539. 0, // 0xDA
  540. 0, // 0xDB
  541. 0, // 0xDC
  542. 0, // 0xDD
  543. 0, // 0xDE
  544. 0, // 0xDF
  545. 0, // 0xE0
  546. 0, // 0xE1
  547. 0, // 0xE2
  548. 0, // 0xE3
  549. 0, // 0xE4
  550. 0, // 0xE5
  551. 0, // 0xE6
  552. 0, // 0xE7
  553. 0, // 0xE8
  554. 0, // 0xE9
  555. 0, // 0xEA
  556. 0, // 0xEB
  557. 0, // 0xEC
  558. 0, // 0xED
  559. 0, // 0xEE
  560. 0, // 0xEF
  561. 0, // 0xF0
  562. 0, // 0xF1
  563. 0, // 0xF2
  564. 0, // 0xF3
  565. 0, // 0xF4
  566. 0, // 0xF5
  567. 0, // 0xF6
  568. 0, // 0xF7
  569. 0, // 0xF8
  570. 0, // 0xF9
  571. 0, // 0xFA
  572. 0, // 0xFB
  573. 0, // 0xFC
  574. 0, // 0xFD
  575. 0, // 0xFE
  576. 0 // 0xFF
  577. };
  578. U8 TranslateOSKeyCode(U8 vcode)
  579. {
  580. return VcodeRemap[vcode];
  581. }
  582. #pragma mark ---- Clipboard functions ----
  583. //-----------------------------------------------------------------------------
  584. const char* Platform::getClipboard()
  585. {
  586. return NULL;//no clipboard on iOS
  587. }
  588. //-----------------------------------------------------------------------------
  589. bool Platform::setClipboard(const char *text)
  590. {
  591. return NULL;//no clipboard on iOS
  592. }
  593. #pragma mark ---- Cursor Functions ----
  594. //------------------------------------------------------------------------------
  595. void Input::pushCursor(S32 cursorID)
  596. {
  597. CursorManager* cm = getCursorManager();
  598. if(cm)
  599. cm->pushCursor(cursorID);
  600. }
  601. //------------------------------------------------------------------------------
  602. void Input::popCursor()
  603. {
  604. CursorManager* cm = getCursorManager();
  605. if(cm)
  606. cm->popCursor();
  607. }
  608. //------------------------------------------------------------------------------
  609. void Input::refreshCursor()
  610. {
  611. CursorManager* cm = getCursorManager();
  612. if(cm)
  613. cm->refreshCursor();
  614. }
  615. #pragma mark ---- DoubleClick Functions ----
  616. //------------------------------------------------------------------------------
  617. U32 Input::getDoubleClickTime()
  618. {
  619. return IOS_DOUBLE_CLICK_TIME;
  620. }
  621. //------------------------------------------------------------------------------
  622. S32 Input::getDoubleClickWidth()
  623. {
  624. // this is an arbitrary value.
  625. return 10;
  626. }
  627. //------------------------------------------------------------------------------
  628. S32 Input::getDoubleClickHeight()
  629. {
  630. return getDoubleClickWidth();
  631. }
  632. #pragma mark -
  633. //------------------------------------------------------------------------------
  634. bool enableKeyboard()
  635. {
  636. if ( !gInputEnabled )
  637. return( false );
  638. if ( gKBEnabled && gKBActive )
  639. return( true );
  640. gKBEnabled = true;
  641. if ( Input::isActive() )
  642. gKBEnabled = activateKeyboard();
  643. if ( gKBEnabled )
  644. {
  645. Con::printf( "Hardware-direct keyboard enabled." );
  646. }
  647. else
  648. {
  649. Con::warnf( "Hardware-direct keyboard failed to enable!" );
  650. }
  651. return( gKBEnabled );
  652. }
  653. //------------------------------------------------------------------------------
  654. void disableKeyboard()
  655. {
  656. if ( !gInputEnabled || !gKBEnabled )
  657. return;
  658. deactivateKeyboard();
  659. gKBEnabled = false;
  660. Con::printf( "Hardware-direct keyboard disabled." );
  661. }
  662. //------------------------------------------------------------------------------
  663. bool activateKeyboard()
  664. {
  665. if ( !gInputEnabled || !Input::isActive() || !gKBEnabled )
  666. return( false );
  667. OSStatus status = noErr;
  668. if (status==noErr)
  669. gKBActive = true;
  670. return( gKBActive );
  671. }
  672. //------------------------------------------------------------------------------
  673. void deactivateKeyboard()
  674. {
  675. if ( gInputEnabled && gKBActive )
  676. {
  677. gKBActive = false;
  678. }
  679. }
  680. //------------------------------------------------------------------------------
  681. bool enableMouse()
  682. {
  683. if ( !gInputEnabled )
  684. return( false );
  685. if ( gMouseEnabled && gMouseActive )
  686. return( true );
  687. gMouseEnabled = activateMouse();
  688. return( gMouseEnabled );
  689. }
  690. //------------------------------------------------------------------------------
  691. void disableMouse()
  692. {
  693. if ( !gInputEnabled || !gMouseEnabled )
  694. return;
  695. deactivateMouse();
  696. gMouseEnabled = false;
  697. bool hwMouse = false;
  698. Con::printf( "%s disabled", hwMouse?"Hardware-direct mouse":"Basic mouse capture");
  699. }
  700. //------------------------------------------------------------------------------
  701. bool activateMouse()
  702. {
  703. if ( !gInputEnabled || !Input::isActive() || !gMouseEnabled )
  704. return( false );
  705. if (gMouseActive)
  706. return(true);
  707. gMouseActive = true;
  708. return( gMouseActive );
  709. }
  710. //------------------------------------------------------------------------------
  711. void deactivateMouse()
  712. {
  713. if ( !gInputEnabled || !gMouseActive )
  714. return;
  715. gMouseActive = false;
  716. }
  717. //------------------------------------------------------------------------------
  718. ConsoleFunction( enableMouse, bool, 1, 1, "enableMouse()" )
  719. {
  720. return( enableMouse() );
  721. }
  722. //------------------------------------------------------------------------------
  723. ConsoleFunction( disableMouse, void, 1, 1, "disableMouse()" )
  724. {
  725. disableMouse();
  726. }
  727. //------------------------------------------------------------------------------
  728. void printInputState(void)
  729. {
  730. if ( gInputEnabled )
  731. {
  732. Con::printf( "Low-level input system is enabled." );
  733. Con::printf( "- Keyboard is %sabled and %sactive.",
  734. gKBEnabled ? "en" : "dis",
  735. gKBActive ? "" : "in" );
  736. Con::printf( "- Mouse is %sabled and %sactive.",
  737. gMouseEnabled ? "en" : "dis",
  738. gMouseActive ? "" : "in" );
  739. /*
  740. Con::printf( "- Joystick is %sabled and %sactive.",
  741. gJoystickEnabled() ? "en" : "dis",
  742. gJoystickActive() ? "" : "in" );
  743. */
  744. }
  745. else
  746. {
  747. Con::printf( "Low-level input system is disabled." );
  748. }
  749. }
  750. //------------------------------------------------------------------------------
  751. ConsoleFunction( echoInputState, void, 1, 1, "echoInputState()" )
  752. {
  753. printInputState();
  754. }
  755. //------------------------------------------------------------------------------
  756. ConsoleFunction( toggleInputState, void, 1, 1, "toggleInputState()" )
  757. {
  758. if (gInputEnabled)
  759. Input::disable();
  760. else
  761. Input::enable();
  762. printInputState();
  763. }
  764. //------------------------------------------------------------------------------
  765. ConsoleFunction( deactivateKeyboard, void, 1, 1, "deactivateKeyboard();")
  766. {
  767. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  768. }
  769. ConsoleFunction( activateKeyboard, void, 1, 1, "activateKeyboard();")
  770. {
  771. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  772. }
  773. //------------------------------------------------------------------------------
  774. void Input::setCursorPos(S32 x, S32 y)
  775. {
  776. //this gets called from GuiCanvas to set the game mouse cursor
  777. }
  778. int processMultipleTouches()
  779. {
  780. char posX[256], posY[256], temp[10], touchNums[256];
  781. dMemset(posX, 0, sizeof(posX));
  782. dMemset(posY, 0, sizeof(posY));
  783. dMemset(touchNums, 0, sizeof(touchNums));
  784. touchEvent *currentEvent;
  785. //Down Events
  786. int numTouchDownEvents = TouchDownEvents.size();
  787. while( TouchDownEvents.size() > 0 )
  788. {
  789. currentEvent = &TouchDownEvents.last();
  790. dItoa( currentEvent->x, temp );
  791. dStrcat( posX, temp );
  792. dStrcat( posX, " " );
  793. dItoa( currentEvent->y, temp );
  794. dStrcat( posY, temp );
  795. dStrcat( posY, " " );
  796. dItoa( currentEvent->number, temp );
  797. dStrcat( touchNums, temp );
  798. dStrcat( touchNums, " " );
  799. TouchDownEvents.pop_back();
  800. }
  801. dItoa( numTouchDownEvents, temp );
  802. if( numTouchDownEvents > 0 )
  803. {
  804. InputEvent touchEvent;
  805. touchEvent.deviceInst = 0;
  806. touchEvent.objInst = SI_TOUCHDOWN;
  807. touchEvent.deviceType = ScreenTouchDeviceType;
  808. touchEvent.action = SI_TOUCH;
  809. touchEvent.objType = SI_TOUCHDOWN;
  810. dStrcpy(touchEvent.fingersX, posX);
  811. dStrcpy(touchEvent.fingersY, posY);
  812. dStrcpy(touchEvent.fingerIDs, touchNums);
  813. touchEvent.modifier = 0;
  814. Game->postEvent(touchEvent);
  815. }
  816. // Deprecated in 1.5
  817. //if( numTouchDownEvents > 0 )
  818. // Con::executef( 4, "oniOSTouchDown", touchNums , posX, posY );
  819. //Move events
  820. int numTouchMoveEvents = TouchMoveEvents.size();
  821. while( TouchMoveEvents.size() > 0 )
  822. {
  823. currentEvent = &TouchMoveEvents.last();
  824. dItoa( currentEvent->x, temp );
  825. dStrcat( posX, temp );
  826. dStrcat( posX, " " );
  827. dItoa( currentEvent->y, temp );
  828. dStrcat( posY, temp );
  829. dStrcat( posY, " " );
  830. dItoa( currentEvent->number, temp );
  831. dStrcat( touchNums, temp );
  832. dStrcat( touchNums, " " );
  833. TouchMoveEvents.pop_back();
  834. }
  835. dItoa( numTouchMoveEvents, temp );
  836. if( numTouchMoveEvents > 0 )
  837. {
  838. InputEvent touchEvent;
  839. touchEvent.deviceInst = 0;
  840. touchEvent.objInst = SI_TOUCHMOVE;
  841. touchEvent.deviceType = ScreenTouchDeviceType;
  842. touchEvent.action = SI_TOUCH;
  843. touchEvent.objType = SI_TOUCHMOVE;
  844. dStrcpy(touchEvent.fingersX, posX);
  845. dStrcpy(touchEvent.fingersY, posY);
  846. dStrcpy(touchEvent.fingerIDs, touchNums);
  847. touchEvent.modifier = 0;
  848. Game->postEvent(touchEvent);
  849. }
  850. // Deprecated in 1.5 -MP
  851. //if( numTouchMoveEvents > 0 )
  852. // Con::executef( 4, "oniOSTouchMove", touchNums , posX, posY );
  853. //Up events
  854. int numTouchUpEvents = TouchUpEvents.size();
  855. dMemset(posX, 0, sizeof(posX));
  856. dMemset(posY, 0, sizeof(posY));
  857. dMemset(touchNums, 0, sizeof(touchNums));
  858. while( TouchUpEvents.size() > 0 )
  859. {
  860. currentEvent = &TouchUpEvents.last();
  861. dItoa( currentEvent->x, temp );
  862. dStrcat( posX, temp );
  863. dStrcat( posX, " " );
  864. dItoa( currentEvent->y, temp );
  865. dStrcat( posY, temp );
  866. dStrcat( posY, " " );
  867. dItoa( currentEvent->number, temp );
  868. dStrcat( touchNums, temp );
  869. dStrcat( touchNums, " " );
  870. lastTouches[currentEvent->number].lastX = -1;
  871. lastTouches[currentEvent->number].lastY = -1;
  872. int x;
  873. x = -1;
  874. lastTouches[currentEvent->number].lastX = -1;
  875. lastTouches[currentEvent->number].lastY = -1;
  876. TouchUpEvents.pop_back();
  877. }
  878. dItoa( numTouchUpEvents, temp );
  879. if( numTouchUpEvents > 0 )
  880. {
  881. InputEvent touchEvent;
  882. touchEvent.deviceInst = 0;
  883. touchEvent.objInst = SI_TOUCHUP;
  884. touchEvent.deviceType = ScreenTouchDeviceType;
  885. touchEvent.action = SI_TOUCH;
  886. touchEvent.objType = SI_TOUCHUP;
  887. dStrcpy(touchEvent.fingersX, posX);
  888. dStrcpy(touchEvent.fingersY, posY);
  889. dStrcpy(touchEvent.fingerIDs, touchNums);
  890. touchEvent.modifier = 0;
  891. Game->postEvent(touchEvent);
  892. }
  893. // Deprecated in 1.5 -MP
  894. //if( numTouchUpEvents > 0 )
  895. // Con::executef( 4, "oniOSTouchUp", touchNums , posX, posY );
  896. //Luma: Tap support
  897. posX[0] = '\0';
  898. posY[0] = '\0';
  899. int numTapEvents = TouchTapEvents.size();
  900. while( TouchTapEvents.size() > 0 )
  901. {
  902. currentEvent = &TouchTapEvents.last();
  903. sprintf( temp, "%d ", currentEvent->x );
  904. dStrcat( posX, temp );
  905. sprintf( temp, "%d ", currentEvent->y );
  906. dStrcat( posY, temp );
  907. TouchTapEvents.pop_back();
  908. }
  909. sprintf( temp, "%d", numTapEvents );
  910. //if( numTapEvents > 0 )
  911. // Con::executef( 4, "oniOSTouchTap", temp , posX, posY );
  912. return numTouchDownEvents + numTouchMoveEvents + numTouchUpEvents;
  913. }
  914. //we want these to only be called once per frame!!
  915. bool createMouseMoveEvent( S32 touchNumber, S32 x, S32 y, S32 lastX, S32 lastY ) //EFM
  916. {
  917. S32 currentSlot = -1;
  918. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (currentSlot == -1) ; i++ )
  919. {
  920. if( (lastX == lastTouches[i].lastX ) &&
  921. (lastY == lastTouches[i].lastY ) )
  922. {
  923. currentSlot = i;
  924. }
  925. }
  926. if( currentSlot == -1 ) return false;
  927. ScreenTouchEvent event;
  928. event.xPos = x;
  929. event.yPos = y;
  930. event.action = SI_MOVE;
  931. event.touchID = currentSlot;
  932. event.numTouches = 0;
  933. //Luma: Mouse not moving (no hover for mouse fingers!)
  934. Canvas->setCursorPos( Point2I( x, y ) );
  935. if( currentSlot != -1 )
  936. {
  937. lastTouches[currentSlot].lastX = x;
  938. lastTouches[currentSlot].lastY = y;
  939. }
  940. TouchMoveEvents.push_back( touchEvent( currentSlot, x, y ) );
  941. Game->postEvent(event);
  942. return true;//return false if we get bad values or something
  943. }
  944. bool createMouseDownEvent( S32 touchNumber, S32 x, S32 y, U32 numTouches )
  945. {
  946. S32 vacantSlot = -1;
  947. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (vacantSlot == -1) ; i++ )
  948. {
  949. if( lastTouches[i].lastX == -1 )
  950. {
  951. vacantSlot = i;
  952. }
  953. }
  954. if( vacantSlot == -1 )
  955. return false;
  956. ScreenTouchEvent event;
  957. event.xPos = x;
  958. event.yPos = y;
  959. event.touchID = vacantSlot;
  960. event.action = SI_MAKE;
  961. event.numTouches = numTouches;
  962. //Luma: Update position
  963. Canvas->setCursorPos( Point2I( x, y ) );
  964. if( vacantSlot != -1 )
  965. {
  966. lastTouches[vacantSlot].lastX = x;
  967. lastTouches[vacantSlot].lastY = y;
  968. }
  969. TouchDownEvents.push_back( touchEvent( vacantSlot, x, y ) );
  970. Game->postEvent(event);
  971. return true;//return false if we get bad values or something
  972. }
  973. bool createMouseUpEvent( S32 touchNumber, S32 x, S32 y, S32 lastX, S32 lastY, U32 numTouches ) //EFM
  974. {
  975. S32 currentSlot = -1;
  976. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (currentSlot == -1) ; i++ )
  977. {
  978. if(( (x == lastTouches[i].lastX) && (y == lastTouches[i].lastY )) ||
  979. ( (lastX == lastTouches[i].lastX ) && (lastY == lastTouches[i].lastY )))
  980. {
  981. currentSlot = i;
  982. }
  983. }
  984. if( currentSlot == -1 )
  985. return false;
  986. ScreenTouchEvent event;
  987. event.xPos = x;
  988. event.yPos = y;
  989. event.action = SI_BREAK;
  990. event.touchID = currentSlot;
  991. event.numTouches = numTouches;
  992. TouchUpEvents.push_back( touchEvent( currentSlot, x, y ) );
  993. Game->postEvent(event);
  994. return true;//return false if we get bad values or something
  995. }
  996. void clearPendingMultitouchEvents( void ) //EFM
  997. {
  998. for( int i = 0 ; i < MAX_TOUCH_EVENTS ; i++ )
  999. {
  1000. lastTouches[i].lastX = -1;
  1001. lastTouches[i].lastY = -1;
  1002. }
  1003. TouchMoveEvents.clear();
  1004. TouchDownEvents.clear();
  1005. TouchUpEvents.clear();
  1006. }
  1007. //Luma: Tap support
  1008. void createMouseTapEvent( S32 nbrTaps, S32 x, S32 y ) {
  1009. TouchTapEvents.push_back( touchEvent( nbrTaps, x, y ) );
  1010. }
  1011. // 0 = x, 1 = y, 2 = z
  1012. UIAccelerationValue g_lastAccel[3];
  1013. // PUAP -Mat this is polled at AccelerometerUpdateMS, which should be set at around 33
  1014. /*
  1015. bool createAccelMoveEvent( UIAccelerationValue *accel ) {
  1016. bool returnVal = false;
  1017. U32 axis[3] = { SI_XAXIS, SI_YAXIS, SI_ZAXIS };
  1018. for( int i = 0; i < 3; i++ ) {
  1019. if(accel[i] != g_lastAccel[i] ) {
  1020. InputEvent event;
  1021. event.deviceInst = 0;//joystick number
  1022. event.fValue = accel[i];
  1023. event.deviceType = JoystickDeviceType;
  1024. event.objType = axis[i];
  1025. event.objInst = i;//axis number
  1026. event.action = SI_MOVE;
  1027. event.modifier = 0;
  1028. Game->postEvent(event);
  1029. g_lastAccel[i] = accel[i];
  1030. returnVal = true;
  1031. }
  1032. }
  1033. return false;
  1034. }
  1035. */