AndroidInput.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 "platformAndroid/platformAndroid.h"
  23. #include "platformAndroid/AndroidUtil.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 Android_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 Android
  107. }
  108. void Input::disableMouse()
  109. {
  110. // Do nothing on Android
  111. }
  112. void Input::enableKeyboard()
  113. {
  114. // Do nothing on Android
  115. }
  116. void Input::disableKeyboard()
  117. {
  118. // Do nothing on Android
  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 AndroidInputManager();
  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 Android." )
  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. //TODO: is this table right for android too?
  318. //--------------------------------------------------------------------------
  319. //#pragma message("input remap table might need tweaking - rumors of ibooks having diff virt keycodes, might need intermediate remap...")
  320. static U8 VcodeRemap[256] =
  321. {
  322. KEY_A, // 0x00
  323. KEY_S, // 0x01
  324. KEY_D, // 0x02
  325. KEY_F, // 0x03
  326. KEY_H, // 0x04
  327. KEY_G, // 0x05
  328. KEY_Z, // 0x06
  329. KEY_X, // 0x07
  330. KEY_C, // 0x08
  331. KEY_V, // 0x09
  332. KEY_Y, // 0x0A // this is questionable - not normal Y code
  333. KEY_B, // 0x0B
  334. KEY_Q, // 0x0C
  335. KEY_W, // 0x0D
  336. KEY_E, // 0x0E
  337. KEY_R, // 0x0F
  338. KEY_Y, // 0x10
  339. KEY_T, // 0x11
  340. KEY_1, // 0x12
  341. KEY_2, // 0x13
  342. KEY_3, // 0x14
  343. KEY_4, // 0x15
  344. KEY_6, // 0x16
  345. KEY_5, // 0x17
  346. KEY_EQUALS, // 0x18
  347. KEY_9, // 0x19
  348. KEY_7, // 0x1A
  349. KEY_MINUS, // 0x1B
  350. KEY_8, // 0x1C
  351. KEY_0, // 0x1D
  352. KEY_RBRACKET, // 0x1E
  353. KEY_O, // 0x1F
  354. KEY_U, // 0x20
  355. KEY_LBRACKET, // 0x21
  356. KEY_I, // 0x22
  357. KEY_P, // 0x23
  358. KEY_RETURN, // 0x24
  359. KEY_L, // 0x25
  360. KEY_J, // 0x26
  361. KEY_APOSTROPHE, // 0x27
  362. KEY_K, // 0x28
  363. KEY_SEMICOLON, // 0x29
  364. KEY_BACKSLASH, // 0x2A
  365. KEY_COMMA, // 0x2B
  366. KEY_SLASH, // 0x2C
  367. KEY_N, // 0x2D
  368. KEY_M, // 0x2E
  369. KEY_PERIOD, // 0x2F
  370. KEY_TAB, // 0x30
  371. KEY_SPACE, // 0x31
  372. KEY_TILDE, // 0x32
  373. KEY_BACKSPACE, // 0x33
  374. 0, // 0x34 //?
  375. KEY_ESCAPE, // 0x35
  376. 0, // 0x36 //?
  377. KEY_ALT, // 0x37 // best mapping for mac Cmd key
  378. KEY_LSHIFT, // 0x38
  379. KEY_CAPSLOCK, // 0x39
  380. KEY_MAC_OPT, // 0x3A // direct map mac Option key -- better than KEY_WIN_WINDOWS
  381. KEY_CONTROL, // 0x3B
  382. KEY_RSHIFT, // 0x3C
  383. 0, // 0x3D
  384. 0, // 0x3E
  385. 0, // 0x3F
  386. 0, // 0x40
  387. KEY_DECIMAL, // 0x41
  388. 0, // 0x42
  389. KEY_MULTIPLY, // 0x43
  390. 0, // 0x44
  391. KEY_ADD, // 0x45
  392. KEY_SUBTRACT, // 0x46 // secondary code?
  393. KEY_NUMLOCK, // 0x47 // also known as Clear on mac...
  394. KEY_SEPARATOR, // 0x48 // secondary code? for KPEqual
  395. 0, // 0x49
  396. 0, // 0x4A
  397. KEY_DIVIDE, // 0x4B
  398. KEY_NUMPADENTER, // 0x4C
  399. KEY_DIVIDE, // 0x4D // secondary code?
  400. KEY_SUBTRACT, // 0x4E
  401. 0, // 0x4F
  402. 0, // 0x50
  403. KEY_SEPARATOR, // 0x51 // WHAT IS SEP? This is KPEqual on mac.
  404. KEY_NUMPAD0, // 0x52
  405. KEY_NUMPAD1, // 0x53
  406. KEY_NUMPAD2, // 0x54
  407. KEY_NUMPAD3, // 0x55
  408. KEY_NUMPAD4, // 0x56
  409. KEY_NUMPAD5, // 0x57
  410. KEY_NUMPAD6, // 0x58
  411. KEY_NUMPAD7, // 0x59
  412. 0, // 0x5A
  413. KEY_NUMPAD8, // 0x5B
  414. KEY_NUMPAD9, // 0x5C
  415. 0, // 0x5D
  416. 0, // 0x5E
  417. 0, // 0x5F
  418. KEY_F5, // 0x60
  419. KEY_F6, // 0x61
  420. KEY_F7, // 0x62
  421. KEY_F3, // 0x63
  422. KEY_F8, // 0x64
  423. KEY_F9, // 0x65
  424. 0, // 0x66
  425. KEY_F11, // 0x67
  426. 0, // 0x68
  427. KEY_PRINT, // 0x69
  428. 0, // 0x6A
  429. KEY_SCROLLLOCK, // 0x6B
  430. 0, // 0x6C
  431. KEY_F10, // 0x6D
  432. 0, // 0x6E
  433. KEY_F12, // 0x6F
  434. 0, // 0x70
  435. KEY_PAUSE, // 0x71
  436. KEY_INSERT, // 0x72 // also known as mac Help
  437. KEY_HOME, // 0x73
  438. KEY_PAGE_UP, // 0x74
  439. KEY_DELETE, // 0x75 // FwdDel
  440. KEY_F4, // 0x76
  441. KEY_END, // 0x77
  442. KEY_F2, // 0x78
  443. KEY_PAGE_DOWN, // 0x79
  444. KEY_F1, // 0x7A
  445. KEY_LEFT, // 0x7B
  446. KEY_RIGHT, // 0x7C
  447. KEY_DOWN, // 0x7D
  448. KEY_UP, // 0x7E
  449. 0, // 0x7F
  450. 0, // 0x80
  451. 0, // 0x81
  452. 0, // 0x82
  453. 0, // 0x83
  454. 0, // 0x84
  455. 0, // 0x85
  456. 0, // 0x86
  457. 0, // 0x87
  458. 0, // 0x88
  459. 0, // 0x89
  460. 0, // 0x8A
  461. 0, // 0x8B
  462. 0, // 0x8C
  463. 0, // 0x8D
  464. 0, // 0x8E
  465. 0, // 0x8F
  466. 0, // 0x90
  467. 0, // 0x91
  468. 0, // 0x92
  469. 0, // 0x93
  470. 0, // 0x94
  471. 0, // 0x95
  472. 0, // 0x96
  473. 0, // 0x97
  474. 0, // 0x98
  475. 0, // 0x99
  476. 0, // 0x9A
  477. 0, // 0x9B
  478. 0, // 0x9C
  479. 0, // 0x9D
  480. 0, // 0x9E
  481. 0, // 0x9F
  482. 0, // 0xA0
  483. 0, // 0xA1
  484. 0, // 0xA2
  485. 0, // 0xA3
  486. 0, // 0xA4
  487. 0, // 0xA5
  488. 0, // 0xA6
  489. 0, // 0xA7
  490. 0, // 0xA8
  491. 0, // 0xA9
  492. 0, // 0xAA
  493. 0, // 0xAB
  494. 0, // 0xAC
  495. 0, // 0xAD
  496. 0, // 0xAE
  497. 0, // 0xAF
  498. 0, // 0xB0
  499. 0, // 0xB1
  500. 0, // 0xB2
  501. 0, // 0xB3
  502. 0, // 0xB4
  503. 0, // 0xB5
  504. 0, // 0xB6
  505. 0, // 0xB7
  506. 0, // 0xB8
  507. 0, // 0xB9
  508. 0, // 0xBA
  509. 0, // 0xBB
  510. 0, // 0xBC
  511. 0, // 0xBD
  512. 0, // 0xBE
  513. 0, // 0xBF
  514. 0, // 0xC0
  515. 0, // 0xC1
  516. 0, // 0xC2
  517. 0, // 0xC3
  518. 0, // 0xC4
  519. 0, // 0xC5
  520. 0, // 0xC6
  521. 0, // 0xC7
  522. 0, // 0xC8
  523. 0, // 0xC9
  524. 0, // 0xCA
  525. 0, // 0xCB
  526. 0, // 0xCC
  527. 0, // 0xCD
  528. 0, // 0xCE
  529. 0, // 0xCF
  530. 0, // 0xD0
  531. 0, // 0xD1
  532. 0, // 0xD2
  533. 0, // 0xD3
  534. 0, // 0xD4
  535. 0, // 0xD5
  536. 0, // 0xD6
  537. 0, // 0xD7
  538. 0, // 0xD8
  539. 0, // 0xD9
  540. 0, // 0xDA
  541. 0, // 0xDB
  542. 0, // 0xDC
  543. 0, // 0xDD
  544. 0, // 0xDE
  545. 0, // 0xDF
  546. 0, // 0xE0
  547. 0, // 0xE1
  548. 0, // 0xE2
  549. 0, // 0xE3
  550. 0, // 0xE4
  551. 0, // 0xE5
  552. 0, // 0xE6
  553. 0, // 0xE7
  554. 0, // 0xE8
  555. 0, // 0xE9
  556. 0, // 0xEA
  557. 0, // 0xEB
  558. 0, // 0xEC
  559. 0, // 0xED
  560. 0, // 0xEE
  561. 0, // 0xEF
  562. 0, // 0xF0
  563. 0, // 0xF1
  564. 0, // 0xF2
  565. 0, // 0xF3
  566. 0, // 0xF4
  567. 0, // 0xF5
  568. 0, // 0xF6
  569. 0, // 0xF7
  570. 0, // 0xF8
  571. 0, // 0xF9
  572. 0, // 0xFA
  573. 0, // 0xFB
  574. 0, // 0xFC
  575. 0, // 0xFD
  576. 0, // 0xFE
  577. 0 // 0xFF
  578. };
  579. U8 TranslateOSKeyCode(U8 vcode)
  580. {
  581. return VcodeRemap[vcode];
  582. }
  583. #pragma mark ---- Clipboard functions ----
  584. //-----------------------------------------------------------------------------
  585. const char* Platform::getClipboard()
  586. {
  587. return NULL;//no clipboard on Android
  588. }
  589. //-----------------------------------------------------------------------------
  590. bool Platform::setClipboard(const char *text)
  591. {
  592. return NULL;//no clipboard on Android
  593. }
  594. #pragma mark ---- Cursor Functions ----
  595. //------------------------------------------------------------------------------
  596. void Input::pushCursor(S32 cursorID)
  597. {
  598. CursorManager* cm = getCursorManager();
  599. if(cm)
  600. cm->pushCursor(cursorID);
  601. }
  602. //------------------------------------------------------------------------------
  603. void Input::popCursor()
  604. {
  605. CursorManager* cm = getCursorManager();
  606. if(cm)
  607. cm->popCursor();
  608. }
  609. //------------------------------------------------------------------------------
  610. void Input::refreshCursor()
  611. {
  612. CursorManager* cm = getCursorManager();
  613. if(cm)
  614. cm->refreshCursor();
  615. }
  616. #pragma mark ---- DoubleClick Functions ----
  617. //------------------------------------------------------------------------------
  618. U32 Input::getDoubleClickTime()
  619. {
  620. return Android_DOUBLE_CLICK_TIME;
  621. }
  622. //------------------------------------------------------------------------------
  623. S32 Input::getDoubleClickWidth()
  624. {
  625. // this is an arbitrary value.
  626. return 10;
  627. }
  628. //------------------------------------------------------------------------------
  629. S32 Input::getDoubleClickHeight()
  630. {
  631. return getDoubleClickWidth();
  632. }
  633. #pragma mark -
  634. //------------------------------------------------------------------------------
  635. bool enableKeyboard()
  636. {
  637. if ( !gInputEnabled )
  638. return( false );
  639. if ( gKBEnabled && gKBActive )
  640. return( true );
  641. gKBEnabled = true;
  642. if ( Input::isActive() )
  643. gKBEnabled = activateKeyboard();
  644. if ( gKBEnabled )
  645. {
  646. Con::printf( "Hardware-direct keyboard enabled." );
  647. }
  648. else
  649. {
  650. Con::warnf( "Hardware-direct keyboard failed to enable!" );
  651. }
  652. return( gKBEnabled );
  653. }
  654. //------------------------------------------------------------------------------
  655. void disableKeyboard()
  656. {
  657. if ( !gInputEnabled || !gKBEnabled )
  658. return;
  659. deactivateKeyboard();
  660. gKBEnabled = false;
  661. Con::printf( "Hardware-direct keyboard disabled." );
  662. }
  663. //------------------------------------------------------------------------------
  664. bool activateKeyboard()
  665. {
  666. if ( !gInputEnabled || !Input::isActive() || !gKBEnabled )
  667. return( false );
  668. //TODO: android
  669. /*OSStatus status = noErr;
  670. if (status==noErr)
  671. gKBActive = true;
  672. */
  673. return( gKBActive );
  674. }
  675. //------------------------------------------------------------------------------
  676. void deactivateKeyboard()
  677. {
  678. if ( gInputEnabled && gKBActive )
  679. {
  680. gKBActive = false;
  681. }
  682. }
  683. //------------------------------------------------------------------------------
  684. bool enableMouse()
  685. {
  686. if ( !gInputEnabled )
  687. return( false );
  688. if ( gMouseEnabled && gMouseActive )
  689. return( true );
  690. gMouseEnabled = activateMouse();
  691. return( gMouseEnabled );
  692. }
  693. //------------------------------------------------------------------------------
  694. void disableMouse()
  695. {
  696. if ( !gInputEnabled || !gMouseEnabled )
  697. return;
  698. deactivateMouse();
  699. gMouseEnabled = false;
  700. bool hwMouse = false;
  701. Con::printf( "%s disabled", hwMouse?"Hardware-direct mouse":"Basic mouse capture");
  702. }
  703. //------------------------------------------------------------------------------
  704. bool activateMouse()
  705. {
  706. if ( !gInputEnabled || !Input::isActive() || !gMouseEnabled )
  707. return( false );
  708. if (gMouseActive)
  709. return(true);
  710. gMouseActive = true;
  711. return( gMouseActive );
  712. }
  713. //------------------------------------------------------------------------------
  714. void deactivateMouse()
  715. {
  716. if ( !gInputEnabled || !gMouseActive )
  717. return;
  718. gMouseActive = false;
  719. }
  720. //------------------------------------------------------------------------------
  721. ConsoleFunction( enableMouse, bool, 1, 1, "enableMouse()" )
  722. {
  723. return( enableMouse() );
  724. }
  725. //------------------------------------------------------------------------------
  726. ConsoleFunction( disableMouse, void, 1, 1, "disableMouse()" )
  727. {
  728. disableMouse();
  729. }
  730. //------------------------------------------------------------------------------
  731. void printInputState(void)
  732. {
  733. if ( gInputEnabled )
  734. {
  735. Con::printf( "Low-level input system is enabled." );
  736. Con::printf( "- Keyboard is %sabled and %sactive.",
  737. gKBEnabled ? "en" : "dis",
  738. gKBActive ? "" : "in" );
  739. Con::printf( "- Mouse is %sabled and %sactive.",
  740. gMouseEnabled ? "en" : "dis",
  741. gMouseActive ? "" : "in" );
  742. /*
  743. Con::printf( "- Joystick is %sabled and %sactive.",
  744. gJoystickEnabled() ? "en" : "dis",
  745. gJoystickActive() ? "" : "in" );
  746. */
  747. }
  748. else
  749. {
  750. Con::printf( "Low-level input system is disabled." );
  751. }
  752. }
  753. //------------------------------------------------------------------------------
  754. ConsoleFunction( echoInputState, void, 1, 1, "echoInputState()" )
  755. {
  756. printInputState();
  757. }
  758. //------------------------------------------------------------------------------
  759. ConsoleFunction( toggleInputState, void, 1, 1, "toggleInputState()" )
  760. {
  761. if (gInputEnabled)
  762. Input::disable();
  763. else
  764. Input::enable();
  765. printInputState();
  766. }
  767. //------------------------------------------------------------------------------
  768. ConsoleFunction( deactivateKeyboard, void, 1, 1, "deactivateKeyboard();")
  769. {
  770. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  771. }
  772. ConsoleFunction( activateKeyboard, void, 1, 1, "activateKeyboard();")
  773. {
  774. // these are only useful on the windows side. They deal with some vagaries of win32 DirectInput.
  775. }
  776. //------------------------------------------------------------------------------
  777. void Input::setCursorPos(S32 x, S32 y)
  778. {
  779. //this gets called from GuiCanvas to set the game mouse cursor
  780. }
  781. int processMultipleTouches()
  782. {
  783. char posX[256], posY[256], temp[10], touchNums[256];
  784. dMemset(posX, 0, sizeof(posX));
  785. dMemset(posY, 0, sizeof(posY));
  786. dMemset(touchNums, 0, sizeof(touchNums));
  787. touchEvent *currentEvent;
  788. //Down Events
  789. int numTouchDownEvents = TouchDownEvents.size();
  790. while( TouchDownEvents.size() > 0 )
  791. {
  792. currentEvent = &TouchDownEvents.last();
  793. dItoa( currentEvent->x, temp );
  794. dStrcat( posX, temp );
  795. dStrcat( posX, " " );
  796. dItoa( currentEvent->y, temp );
  797. dStrcat( posY, temp );
  798. dStrcat( posY, " " );
  799. dItoa( currentEvent->number, temp );
  800. dStrcat( touchNums, temp );
  801. dStrcat( touchNums, " " );
  802. TouchDownEvents.pop_back();
  803. }
  804. dItoa( numTouchDownEvents, temp );
  805. if( numTouchDownEvents > 0 )
  806. {
  807. InputEvent touchEvent;
  808. touchEvent.deviceInst = 0;
  809. touchEvent.objInst = SI_TOUCHDOWN;
  810. touchEvent.deviceType = ScreenTouchDeviceType;
  811. touchEvent.action = SI_TOUCH;
  812. touchEvent.objType = SI_TOUCHDOWN;
  813. dStrcpy(touchEvent.touchesX, posX);
  814. dStrcpy(touchEvent.touchesY, posY);
  815. dStrcpy(touchEvent.touchIDs, touchNums);
  816. touchEvent.modifier = 0;
  817. Game->postEvent(touchEvent);
  818. }
  819. // Deprecated in 1.5
  820. //if( numTouchDownEvents > 0 )
  821. // Con::executef( 4, "onAndroidTouchDown", touchNums , posX, posY );
  822. //Move events
  823. int numTouchMoveEvents = TouchMoveEvents.size();
  824. while( TouchMoveEvents.size() > 0 )
  825. {
  826. currentEvent = &TouchMoveEvents.last();
  827. dItoa( currentEvent->x, temp );
  828. dStrcat( posX, temp );
  829. dStrcat( posX, " " );
  830. dItoa( currentEvent->y, temp );
  831. dStrcat( posY, temp );
  832. dStrcat( posY, " " );
  833. dItoa( currentEvent->number, temp );
  834. dStrcat( touchNums, temp );
  835. dStrcat( touchNums, " " );
  836. TouchMoveEvents.pop_back();
  837. }
  838. dItoa( numTouchMoveEvents, temp );
  839. if( numTouchMoveEvents > 0 )
  840. {
  841. InputEvent touchEvent;
  842. touchEvent.deviceInst = 0;
  843. touchEvent.objInst = SI_TOUCHMOVE;
  844. touchEvent.deviceType = ScreenTouchDeviceType;
  845. touchEvent.action = SI_TOUCH;
  846. touchEvent.objType = SI_TOUCHMOVE;
  847. dStrcpy(touchEvent.touchesX, posX);
  848. dStrcpy(touchEvent.touchesY, posY);
  849. dStrcpy(touchEvent.touchIDs, touchNums);
  850. touchEvent.modifier = 0;
  851. Game->postEvent(touchEvent);
  852. }
  853. // Deprecated in 1.5 -MP
  854. //if( numTouchMoveEvents > 0 )
  855. // Con::executef( 4, "onAndroidTouchMove", touchNums , posX, posY );
  856. //Up events
  857. int numTouchUpEvents = TouchUpEvents.size();
  858. dMemset(posX, 0, sizeof(posX));
  859. dMemset(posY, 0, sizeof(posY));
  860. dMemset(touchNums, 0, sizeof(touchNums));
  861. while( TouchUpEvents.size() > 0 )
  862. {
  863. currentEvent = &TouchUpEvents.last();
  864. dItoa( currentEvent->x, temp );
  865. dStrcat( posX, temp );
  866. dStrcat( posX, " " );
  867. dItoa( currentEvent->y, temp );
  868. dStrcat( posY, temp );
  869. dStrcat( posY, " " );
  870. dItoa( currentEvent->number, temp );
  871. dStrcat( touchNums, temp );
  872. dStrcat( touchNums, " " );
  873. lastTouches[currentEvent->number].lastX = -1;
  874. lastTouches[currentEvent->number].lastY = -1;
  875. int x;
  876. x = -1;
  877. lastTouches[currentEvent->number].lastX = -1;
  878. lastTouches[currentEvent->number].lastY = -1;
  879. TouchUpEvents.pop_back();
  880. }
  881. dItoa( numTouchUpEvents, temp );
  882. if( numTouchUpEvents > 0 )
  883. {
  884. InputEvent touchEvent;
  885. touchEvent.deviceInst = 0;
  886. touchEvent.objInst = SI_TOUCHUP;
  887. touchEvent.deviceType = ScreenTouchDeviceType;
  888. touchEvent.action = SI_TOUCH;
  889. touchEvent.objType = SI_TOUCHUP;
  890. dStrcpy(touchEvent.touchesX, posX);
  891. dStrcpy(touchEvent.touchesY, posY);
  892. dStrcpy(touchEvent.touchIDs, touchNums);
  893. touchEvent.modifier = 0;
  894. Game->postEvent(touchEvent);
  895. }
  896. // Deprecated in 1.5 -MP
  897. //if( numTouchUpEvents > 0 )
  898. // Con::executef( 4, "onAndroidTouchUp", touchNums , posX, posY );
  899. //Luma: Tap support
  900. posX[0] = '\0';
  901. posY[0] = '\0';
  902. int numTapEvents = TouchTapEvents.size();
  903. while( TouchTapEvents.size() > 0 )
  904. {
  905. currentEvent = &TouchTapEvents.last();
  906. sprintf( temp, "%d ", currentEvent->x );
  907. dStrcat( posX, temp );
  908. sprintf( temp, "%d ", currentEvent->y );
  909. dStrcat( posY, temp );
  910. TouchTapEvents.pop_back();
  911. }
  912. sprintf( temp, "%d", numTapEvents );
  913. //if( numTapEvents > 0 )
  914. // Con::executef( 4, "onAndroidTouchTap", temp , posX, posY );
  915. return numTouchDownEvents + numTouchMoveEvents + numTouchUpEvents;
  916. }
  917. //we want these to only be called once per frame!!
  918. bool createMouseMoveEvent( S32 touchNumber, S32 x, S32 y, S32 lastX, S32 lastY ) //EFM
  919. {
  920. S32 currentSlot = -1;
  921. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (currentSlot == -1) ; i++ )
  922. {
  923. if( (lastX == lastTouches[i].lastX ) &&
  924. (lastY == lastTouches[i].lastY ) )
  925. {
  926. currentSlot = i;
  927. }
  928. }
  929. if( currentSlot == -1 ) return false;
  930. ScreenTouchEvent event;
  931. event.xPos = x;
  932. event.yPos = y;
  933. event.action = SI_MOVE;
  934. event.touchID = currentSlot;
  935. event.numTouches = 0;
  936. //Luma: Mouse not moving (no hover for mouse fingers!)
  937. Canvas->setCursorPos( Point2I( x, y ) );
  938. if( currentSlot != -1 )
  939. {
  940. lastTouches[currentSlot].lastX = x;
  941. lastTouches[currentSlot].lastY = y;
  942. }
  943. TouchMoveEvents.push_back( touchEvent( currentSlot, x, y ) );
  944. Game->postEvent(event);
  945. return true;//return false if we get bad values or something
  946. }
  947. bool createMouseDownEvent( S32 touchNumber, S32 x, S32 y, U32 numTouches )
  948. {
  949. S32 vacantSlot = -1;
  950. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (vacantSlot == -1) ; i++ )
  951. {
  952. if( lastTouches[i].lastX == -1 )
  953. {
  954. vacantSlot = i;
  955. }
  956. }
  957. if( vacantSlot == -1 )
  958. return false;
  959. ScreenTouchEvent event;
  960. event.xPos = x;
  961. event.yPos = y;
  962. event.touchID = vacantSlot;
  963. event.action = SI_MAKE;
  964. event.numTouches = numTouches;
  965. //Luma: Update position
  966. Canvas->setCursorPos( Point2I( x, y ) );
  967. if( vacantSlot != -1 )
  968. {
  969. lastTouches[vacantSlot].lastX = x;
  970. lastTouches[vacantSlot].lastY = y;
  971. }
  972. TouchDownEvents.push_back( touchEvent( vacantSlot, x, y ) );
  973. Game->postEvent(event);
  974. return true;//return false if we get bad values or something
  975. }
  976. bool createMouseUpEvent( S32 touchNumber, S32 x, S32 y, S32 lastX, S32 lastY, U32 numTouches ) //EFM
  977. {
  978. S32 currentSlot = -1;
  979. for( int i = 0 ; (i < MAX_TOUCH_EVENTS) && (currentSlot == -1) ; i++ )
  980. {
  981. if(( (x == lastTouches[i].lastX) && (y == lastTouches[i].lastY )) ||
  982. ( (lastX == lastTouches[i].lastX ) && (lastY == lastTouches[i].lastY )))
  983. {
  984. currentSlot = i;
  985. }
  986. }
  987. if( currentSlot == -1 )
  988. return false;
  989. ScreenTouchEvent event;
  990. event.xPos = x;
  991. event.yPos = y;
  992. event.action = SI_BREAK;
  993. event.touchID = currentSlot;
  994. event.numTouches = numTouches;
  995. TouchUpEvents.push_back( touchEvent( currentSlot, x, y ) );
  996. Game->postEvent(event);
  997. return true;//return false if we get bad values or something
  998. }
  999. void clearPendingMultitouchEvents( void ) //EFM
  1000. {
  1001. for( int i = 0 ; i < MAX_TOUCH_EVENTS ; i++ )
  1002. {
  1003. lastTouches[i].lastX = -1;
  1004. lastTouches[i].lastY = -1;
  1005. }
  1006. TouchMoveEvents.clear();
  1007. TouchDownEvents.clear();
  1008. TouchUpEvents.clear();
  1009. }
  1010. //Luma: Tap support
  1011. void createMouseTapEvent( S32 nbrTaps, S32 x, S32 y ) {
  1012. TouchTapEvents.push_back( touchEvent( nbrTaps, x, y ) );
  1013. }
  1014. // 0 = x, 1 = y, 2 = z
  1015. //TODO: android
  1016. //UIAccelerationValue g_lastAccel[3];
  1017. // PUAP -Mat this is polled at AccelerometerUpdateMS, which should be set at around 33
  1018. /*
  1019. bool createAccelMoveEvent( UIAccelerationValue *accel ) {
  1020. bool returnVal = false;
  1021. U32 axis[3] = { SI_XAXIS, SI_YAXIS, SI_ZAXIS };
  1022. for( int i = 0; i < 3; i++ ) {
  1023. if(accel[i] != g_lastAccel[i] ) {
  1024. InputEvent event;
  1025. event.deviceInst = 0;//joystick number
  1026. event.fValue = accel[i];
  1027. event.deviceType = JoystickDeviceType;
  1028. event.objType = axis[i];
  1029. event.objInst = i;//axis number
  1030. event.action = SI_MOVE;
  1031. event.modifier = 0;
  1032. Game->postEvent(event);
  1033. g_lastAccel[i] = accel[i];
  1034. returnVal = true;
  1035. }
  1036. }
  1037. return false;
  1038. }
  1039. */