input.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Portions Copyright (c) 2009, netwan.
  5. //
  6. // All rights reserved.
  7. //
  8. // This file is part of SDLPAL.
  9. //
  10. // SDLPAL is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #include "main.h"
  24. #include <math.h>
  25. #ifdef __N3DS__
  26. #include <3ds.h>
  27. #endif
  28. volatile PALINPUTSTATE g_InputState;
  29. #if PAL_HAS_JOYSTICKS
  30. static SDL_Joystick *g_pJoy = NULL;
  31. #endif
  32. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  33. #define SDL_JoystickNameForIndex SDL_JoystickName
  34. #endif
  35. BOOL g_fUseJoystick = TRUE;
  36. #if defined(GPH)
  37. #define MIN_DEADZONE -16384
  38. #define MAX_DEADZONE 16384
  39. #endif
  40. static VOID
  41. PAL_KeyboardEventFilter(
  42. const SDL_Event *lpEvent
  43. )
  44. /*++
  45. Purpose:
  46. Handle keyboard events.
  47. Parameters:
  48. [IN] lpEvent - pointer to the event.
  49. Return value:
  50. None.
  51. --*/
  52. {
  53. switch (lpEvent->type)
  54. {
  55. case SDL_KEYDOWN:
  56. //
  57. // Pressed a key
  58. //
  59. if (lpEvent->key.keysym.mod & KMOD_ALT)
  60. {
  61. if (lpEvent->key.keysym.sym == SDLK_RETURN)
  62. {
  63. //
  64. // Pressed Alt+Enter (toggle fullscreen)...
  65. //
  66. VIDEO_ToggleFullscreen();
  67. return;
  68. }
  69. else if (lpEvent->key.keysym.sym == SDLK_F4)
  70. {
  71. //
  72. // Pressed Alt+F4 (Exit program)...
  73. //
  74. PAL_Shutdown(0);
  75. }
  76. }
  77. switch (lpEvent->key.keysym.sym)
  78. {
  79. #ifdef __SYMBIAN32__
  80. //
  81. // Symbian-specific stuff
  82. //
  83. case SDLK_0:
  84. VIDEO_ToggleScaleScreen();
  85. break;
  86. case SDLK_1:
  87. AUDIO_DecreaseVolume();
  88. break;
  89. case SDLK_3:
  90. AUDIO_IncreaseVolume();
  91. break;
  92. #endif
  93. case SDLK_UP:
  94. case SDLK_KP8:
  95. if (gpGlobals->fInBattle || g_InputState.dir != kDirNorth)
  96. {
  97. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  98. g_InputState.dir = kDirNorth;
  99. g_InputState.dwKeyPress |= kKeyUp;
  100. }
  101. break;
  102. case SDLK_DOWN:
  103. case SDLK_KP2:
  104. if (gpGlobals->fInBattle || g_InputState.dir != kDirSouth)
  105. {
  106. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  107. g_InputState.dir = kDirSouth;
  108. g_InputState.dwKeyPress |= kKeyDown;
  109. }
  110. break;
  111. case SDLK_LEFT:
  112. case SDLK_KP4:
  113. if (gpGlobals->fInBattle || g_InputState.dir != kDirWest)
  114. {
  115. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  116. g_InputState.dir = kDirWest;
  117. g_InputState.dwKeyPress |= kKeyLeft;
  118. }
  119. break;
  120. case SDLK_RIGHT:
  121. case SDLK_KP6:
  122. if (gpGlobals->fInBattle || g_InputState.dir != kDirEast)
  123. {
  124. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  125. g_InputState.dir = kDirEast;
  126. g_InputState.dwKeyPress |= kKeyRight;
  127. }
  128. break;
  129. #if defined(DINGOO)
  130. case SDLK_SPACE:
  131. g_InputState.dwKeyPress = kKeyMenu;
  132. break;
  133. case SDLK_LCTRL:
  134. g_InputState.dwKeyPress = kKeySearch;
  135. break;
  136. #else
  137. case SDLK_ESCAPE:
  138. case SDLK_INSERT:
  139. case SDLK_LALT:
  140. case SDLK_RALT:
  141. case SDLK_KP0:
  142. g_InputState.dwKeyPress |= kKeyMenu;
  143. break;
  144. case SDLK_RETURN:
  145. case SDLK_SPACE:
  146. case SDLK_KP_ENTER:
  147. case SDLK_LCTRL:
  148. g_InputState.dwKeyPress |= kKeySearch;
  149. break;
  150. case SDLK_PAGEUP:
  151. case SDLK_KP9:
  152. g_InputState.dwKeyPress |= kKeyPgUp;
  153. break;
  154. case SDLK_PAGEDOWN:
  155. case SDLK_KP3:
  156. g_InputState.dwKeyPress |= kKeyPgDn;
  157. break;
  158. case SDLK_HOME:
  159. g_InputState.dwKeyPress |= kKeyHome;
  160. break;
  161. case SDLK_END:
  162. g_InputState.dwKeyPress |= kKeyEnd;
  163. break;
  164. case SDLK_7: //7 for mobile device
  165. case SDLK_r:
  166. g_InputState.dwKeyPress |= kKeyRepeat;
  167. break;
  168. case SDLK_2: //2 for mobile device
  169. case SDLK_a:
  170. g_InputState.dwKeyPress |= kKeyAuto;
  171. break;
  172. case SDLK_d:
  173. g_InputState.dwKeyPress |= kKeyDefend;
  174. break;
  175. case SDLK_e:
  176. g_InputState.dwKeyPress |= kKeyUseItem;
  177. break;
  178. case SDLK_w:
  179. g_InputState.dwKeyPress |= kKeyThrowItem;
  180. break;
  181. #if defined(__WINRT__)
  182. case SDLK_AC_BACK:
  183. // If game not started, exit directly
  184. if (!gpGlobals->fInMainGame)
  185. PAL_Shutdown(0);
  186. #endif
  187. case SDLK_q:
  188. g_InputState.dwKeyPress |= kKeyFlee;
  189. break;
  190. case SDLK_s:
  191. g_InputState.dwKeyPress |= kKeyStatus;
  192. break;
  193. case SDLK_f:
  194. case SDLK_5: // 5 for mobile device
  195. g_InputState.dwKeyPress |= kKeyForce;
  196. break;
  197. case SDLK_HASH: //# for mobile device
  198. case SDLK_p:
  199. VIDEO_SaveScreenshot();
  200. break;
  201. #endif
  202. default:
  203. break;
  204. }
  205. break;
  206. case SDL_KEYUP:
  207. //
  208. // Released a key
  209. //
  210. switch (lpEvent->key.keysym.sym)
  211. {
  212. case SDLK_UP:
  213. case SDLK_KP8:
  214. if (g_InputState.dir == kDirNorth)
  215. {
  216. g_InputState.dir = g_InputState.prevdir;
  217. }
  218. g_InputState.prevdir = kDirUnknown;
  219. break;
  220. case SDLK_DOWN:
  221. case SDLK_KP2:
  222. if (g_InputState.dir == kDirSouth)
  223. {
  224. g_InputState.dir = g_InputState.prevdir;
  225. }
  226. g_InputState.prevdir = kDirUnknown;
  227. break;
  228. case SDLK_LEFT:
  229. case SDLK_KP4:
  230. if (g_InputState.dir == kDirWest)
  231. {
  232. g_InputState.dir = g_InputState.prevdir;
  233. }
  234. g_InputState.prevdir = kDirUnknown;
  235. break;
  236. case SDLK_RIGHT:
  237. case SDLK_KP6:
  238. if (g_InputState.dir == kDirEast)
  239. {
  240. g_InputState.dir = g_InputState.prevdir;
  241. }
  242. g_InputState.prevdir = kDirUnknown;
  243. break;
  244. default:
  245. break;
  246. }
  247. break;
  248. }
  249. }
  250. static VOID
  251. PAL_MouseEventFilter(
  252. const SDL_Event *lpEvent
  253. )
  254. /*++
  255. Purpose:
  256. Handle mouse events.
  257. Parameters:
  258. [IN] lpEvent - pointer to the event.
  259. Return value:
  260. None.
  261. --*/
  262. {
  263. #if PAL_HAS_MOUSE
  264. static short hitTest = 0; // Double click detect;
  265. const SDL_VideoInfo *vi;
  266. double screenWidth, gridWidth;
  267. double screenHeight, gridHeight;
  268. double mx, my;
  269. double thumbx;
  270. double thumby;
  271. INT gridIndex;
  272. BOOL isLeftMouseDBClick = FALSE;
  273. BOOL isLeftMouseClick = FALSE;
  274. BOOL isRightMouseClick = FALSE;
  275. static INT lastReleaseButtonTime, lastPressButtonTime, betweenTime;
  276. static INT lastPressx = 0;
  277. static INT lastPressy = 0;
  278. static INT lastReleasex = 0;
  279. static INT lastReleasey = 0;
  280. if (lpEvent->type!= SDL_MOUSEBUTTONDOWN && lpEvent->type != SDL_MOUSEBUTTONUP)
  281. return;
  282. vi = SDL_GetVideoInfo();
  283. screenWidth = vi->current_w;
  284. screenHeight = vi->current_h;
  285. gridWidth = screenWidth / 3;
  286. gridHeight = screenHeight / 3;
  287. mx = lpEvent->button.x;
  288. my = lpEvent->button.y;
  289. thumbx = ceil(mx / gridWidth);
  290. thumby = floor(my / gridHeight);
  291. gridIndex = thumbx + thumby * 3 - 1;
  292. switch (lpEvent->type)
  293. {
  294. case SDL_MOUSEBUTTONDOWN:
  295. lastPressButtonTime = SDL_GetTicks();
  296. lastPressx = lpEvent->button.x;
  297. lastPressy = lpEvent->button.y;
  298. switch (gridIndex)
  299. {
  300. case 2:
  301. g_InputState.prevdir = g_InputState.dir;
  302. g_InputState.dir = kDirNorth;
  303. break;
  304. case 6:
  305. g_InputState.prevdir = g_InputState.dir;
  306. g_InputState.dir = kDirSouth;
  307. break;
  308. case 0:
  309. g_InputState.prevdir = g_InputState.dir;
  310. g_InputState.dir = kDirWest;
  311. break;
  312. case 8:
  313. g_InputState.prevdir = g_InputState.dir;
  314. g_InputState.dir = kDirEast;
  315. break;
  316. case 1:
  317. //g_InputState.prevdir = g_InputState.dir;
  318. //g_InputState.dir = kDirNorth;
  319. g_InputState.dwKeyPress |= kKeyUp;
  320. break;
  321. case 7:
  322. //g_InputState.prevdir = g_InputState.dir;
  323. //g_InputState.dir = kDirSouth;
  324. g_InputState.dwKeyPress |= kKeyDown;
  325. break;
  326. case 3:
  327. //g_InputState.prevdir = g_InputState.dir;
  328. //g_InputState.dir = kDirWest;
  329. g_InputState.dwKeyPress |= kKeyLeft;
  330. break;
  331. case 5:
  332. //g_InputState.prevdir = g_InputState.dir;
  333. //g_InputState.dir = kDirEast;
  334. g_InputState.dwKeyPress |= kKeyRight;
  335. break;
  336. }
  337. break;
  338. case SDL_MOUSEBUTTONUP:
  339. lastReleaseButtonTime = SDL_GetTicks();
  340. lastReleasex = lpEvent->button.x;
  341. lastReleasey = lpEvent->button.y;
  342. hitTest ++;
  343. if (abs(lastPressx - lastReleasex) < 25 &&
  344. abs(lastPressy - lastReleasey) < 25)
  345. {
  346. betweenTime = lastReleaseButtonTime - lastPressButtonTime;
  347. if (betweenTime >500)
  348. {
  349. isRightMouseClick = TRUE;
  350. }
  351. else if (betweenTime >=0)
  352. {
  353. if((betweenTime < 100) && (hitTest >= 2))
  354. {
  355. isLeftMouseClick = TRUE;
  356. hitTest = 0;
  357. }
  358. else
  359. {
  360. isLeftMouseClick = TRUE;
  361. if(betweenTime > 100)
  362. {
  363. hitTest = 0;
  364. }
  365. }
  366. }
  367. }
  368. switch (gridIndex)
  369. {
  370. case 2:
  371. if( isLeftMouseDBClick )
  372. {
  373. AUDIO_IncreaseVolume();
  374. break;
  375. }
  376. case 6:
  377. case 0:
  378. if( isLeftMouseDBClick )
  379. {
  380. AUDIO_DecreaseVolume();
  381. break;
  382. }
  383. case 7:
  384. if (isRightMouseClick) //repeat attack
  385. {
  386. g_InputState.dwKeyPress |= kKeyRepeat;
  387. break;
  388. }
  389. case 8:
  390. g_InputState.dir = kDirUnknown;
  391. g_InputState.prevdir = kDirUnknown;
  392. break;
  393. case 1:
  394. if( isRightMouseClick )
  395. {
  396. g_InputState.dwKeyPress |= kKeyForce;
  397. }
  398. break;
  399. case 3:
  400. if( isRightMouseClick )
  401. {
  402. g_InputState.dwKeyPress |= kKeyAuto;
  403. }
  404. break;
  405. case 5:
  406. if( isRightMouseClick )
  407. {
  408. g_InputState.dwKeyPress |= kKeyDefend;
  409. }
  410. break;
  411. case 4:
  412. if (isRightMouseClick) // menu
  413. {
  414. g_InputState.dwKeyPress |= kKeyMenu;
  415. }
  416. else if (isLeftMouseClick) // search
  417. {
  418. g_InputState.dwKeyPress |= kKeySearch;
  419. }
  420. break;
  421. }
  422. break;
  423. }
  424. #endif
  425. }
  426. static VOID
  427. PAL_JoystickEventFilter(
  428. const SDL_Event *lpEvent
  429. )
  430. /*++
  431. Purpose:
  432. Handle joystick events.
  433. Parameters:
  434. [IN] lpEvent - pointer to the event.
  435. Return value:
  436. None.
  437. --*/
  438. {
  439. #if PAL_HAS_JOYSTICKS
  440. switch (lpEvent->type)
  441. {
  442. #if defined (GEKKO)
  443. case SDL_JOYHATMOTION:
  444. switch (lpEvent->jhat.value)
  445. {
  446. case SDL_HAT_LEFT:
  447. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  448. g_InputState.dir = kDirWest;
  449. g_InputState.dwKeyPress = kKeyLeft;
  450. break;
  451. case SDL_HAT_RIGHT:
  452. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  453. g_InputState.dir = kDirEast;
  454. g_InputState.dwKeyPress = kKeyRight;
  455. break;
  456. case SDL_HAT_UP:
  457. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  458. g_InputState.dir = kDirNorth;
  459. g_InputState.dwKeyPress = kKeyUp;
  460. break;
  461. case SDL_HAT_DOWN:
  462. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  463. g_InputState.dir = kDirSouth;
  464. g_InputState.dwKeyPress = kKeyDown;
  465. break;
  466. }
  467. break;
  468. #else
  469. case SDL_JOYAXISMOTION:
  470. //
  471. // Moved an axis on joystick
  472. //
  473. switch (lpEvent->jaxis.axis)
  474. {
  475. case 0:
  476. //
  477. // X axis
  478. //
  479. #if defined(GPH)
  480. if (lpEvent->jaxis.value > MAX_DEADZONE) {
  481. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  482. g_InputState.dir = kDirEast;
  483. g_InputState.dwKeyPress = kKeyRight;
  484. } else if (lpEvent->jaxis.value < MIN_DEADZONE) {
  485. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  486. g_InputState.dir = kDirWest;
  487. g_InputState.dwKeyPress = kKeyLeft;
  488. } else {
  489. g_InputState.dir = kDirUnknown;
  490. }
  491. #else
  492. if (lpEvent->jaxis.value > 20000)
  493. {
  494. if (g_InputState.dir != kDirEast)
  495. {
  496. g_InputState.dwKeyPress |= kKeyRight;
  497. }
  498. g_InputState.prevdir = g_InputState.dir;
  499. g_InputState.dir = kDirEast;
  500. }
  501. else if (lpEvent->jaxis.value < -20000)
  502. {
  503. if (g_InputState.dir != kDirWest)
  504. {
  505. g_InputState.dwKeyPress |= kKeyLeft;
  506. }
  507. g_InputState.prevdir = g_InputState.dir;
  508. g_InputState.dir = kDirWest;
  509. }
  510. else
  511. {
  512. if (g_InputState.prevdir != kDirEast &&
  513. g_InputState.prevdir != kDirWest)
  514. {
  515. g_InputState.dir = g_InputState.prevdir;
  516. }
  517. g_InputState.prevdir = kDirUnknown;
  518. }
  519. #endif
  520. break;
  521. case 1:
  522. //
  523. // Y axis
  524. //
  525. #if defined(GPH)
  526. if (lpEvent->jaxis.value > MAX_DEADZONE) {
  527. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  528. g_InputState.dir = kDirSouth;
  529. g_InputState.dwKeyPress = kKeyDown;
  530. } else if (lpEvent->jaxis.value < MIN_DEADZONE) {
  531. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  532. g_InputState.dir = kDirNorth;
  533. g_InputState.dwKeyPress = kKeyUp;
  534. } else {
  535. g_InputState.dir = kDirUnknown;
  536. }
  537. #else
  538. if (lpEvent->jaxis.value > 20000)
  539. {
  540. if (g_InputState.dir != kDirSouth)
  541. {
  542. g_InputState.dwKeyPress |= kKeyDown;
  543. }
  544. g_InputState.prevdir = g_InputState.dir;
  545. g_InputState.dir = kDirSouth;
  546. }
  547. else if (lpEvent->jaxis.value < -20000)
  548. {
  549. if (g_InputState.dir != kDirNorth)
  550. {
  551. g_InputState.dwKeyPress |= kKeyUp;
  552. }
  553. g_InputState.prevdir = g_InputState.dir;
  554. g_InputState.dir = kDirNorth;
  555. }
  556. else
  557. {
  558. if (g_InputState.prevdir != kDirNorth &&
  559. g_InputState.prevdir != kDirSouth)
  560. {
  561. g_InputState.dir = g_InputState.prevdir;
  562. }
  563. g_InputState.prevdir = kDirUnknown;
  564. }
  565. #endif
  566. break;
  567. }
  568. break;
  569. #endif
  570. case SDL_JOYBUTTONDOWN:
  571. //
  572. // Pressed the joystick button
  573. //
  574. #if defined(GPH)
  575. switch (lpEvent->jbutton.button)
  576. {
  577. #if defined(GP2XWIZ)
  578. case 14:
  579. #elif defined(CAANOO)
  580. case 3:
  581. #endif
  582. g_InputState.dwKeyPress = kKeyMenu;
  583. break;
  584. #if defined(GP2XWIZ)
  585. case 13:
  586. #elif defined(CAANOO)
  587. case 2:
  588. #endif
  589. g_InputState.dwKeyPress = kKeySearch;
  590. break;
  591. #else
  592. #if defined(GEKKO)
  593. switch (lpEvent->jbutton.button)
  594. {
  595. case 2:
  596. g_InputState.dwKeyPress |= kKeyMenu;
  597. break;
  598. case 3:
  599. g_InputState.dwKeyPress |= kKeySearch;
  600. break;
  601. #else
  602. switch (lpEvent->jbutton.button & 1)
  603. {
  604. case 0:
  605. g_InputState.dwKeyPress |= kKeyMenu;
  606. break;
  607. case 1:
  608. g_InputState.dwKeyPress |= kKeySearch;
  609. break;
  610. #endif
  611. #endif
  612. }
  613. break;
  614. }
  615. #endif
  616. }
  617. #if PAL_HAS_TOUCH
  618. #define TOUCH_NONE 0
  619. #define TOUCH_UP 1
  620. #define TOUCH_DOWN 2
  621. #define TOUCH_LEFT 3
  622. #define TOUCH_RIGHT 4
  623. #define TOUCH_BUTTON1 5
  624. #define TOUCH_BUTTON2 6
  625. #define TOUCH_BUTTON3 7
  626. #define TOUCH_BUTTON4 8
  627. static float gfTouchXMin = 0.0f;
  628. static float gfTouchXMax = 1.0f;
  629. static float gfTouchYMin = 0.0f;
  630. static float gfTouchYMax = 1.0f;
  631. VOID
  632. PAL_SetTouchBounds(
  633. DWORD dwScreenWidth,
  634. DWORD dwScreenHeight,
  635. SDL_Rect renderRect
  636. )
  637. {
  638. gfTouchXMin = (float)renderRect.x / dwScreenWidth;
  639. gfTouchXMax = (float)(renderRect.x + renderRect.w) / dwScreenWidth;
  640. gfTouchYMin = (float)renderRect.y / dwScreenHeight;
  641. gfTouchYMax = (float)(renderRect.y + renderRect.h) / dwScreenHeight;
  642. }
  643. static int
  644. PAL_GetTouchArea(
  645. float X,
  646. float Y
  647. )
  648. {
  649. if (X < gfTouchXMin || X > gfTouchXMax || Y < 0.5f || Y > gfTouchYMax)
  650. {
  651. //
  652. // Upper area or cropped area
  653. //
  654. return TOUCH_NONE;
  655. }
  656. else
  657. {
  658. X = (X - gfTouchXMin) / (gfTouchXMax - gfTouchXMin);
  659. Y = (Y - gfTouchYMin) / (gfTouchYMax - gfTouchYMin);
  660. }
  661. if (X < 1.0f / 3)
  662. {
  663. if (Y - 0.5f < (1.0f / 6 - fabsf(X - 1.0f / 3 / 2)) * (0.5f / (1.0f / 3)))
  664. {
  665. return TOUCH_UP;
  666. }
  667. else if (Y - 0.75f > fabsf(X - 1.0f / 3 / 2) * (0.5f / (1.0f / 3)))
  668. {
  669. return TOUCH_DOWN;
  670. }
  671. else if (X < 1.0f / 3 / 2 && fabsf(Y - 0.75f) < 0.25f - X * (0.5f / (1.0f / 3)))
  672. {
  673. return TOUCH_LEFT;
  674. }
  675. else
  676. {
  677. return TOUCH_RIGHT;
  678. }
  679. }
  680. else if (X > 1.0f - 1.0f / 3)
  681. {
  682. if (X < 1.0f - (1.0f / 3 / 2))
  683. {
  684. if (Y < 0.75f)
  685. {
  686. return TOUCH_BUTTON1;
  687. }
  688. else
  689. {
  690. return TOUCH_BUTTON3;
  691. }
  692. }
  693. else
  694. {
  695. if (Y < 0.75f)
  696. {
  697. return TOUCH_BUTTON2;
  698. }
  699. else
  700. {
  701. return TOUCH_BUTTON4;
  702. }
  703. }
  704. }
  705. else
  706. {
  707. return TOUCH_NONE;
  708. }
  709. }
  710. static VOID
  711. PAL_SetTouchAction(
  712. int area
  713. )
  714. {
  715. switch (area)
  716. {
  717. case TOUCH_UP:
  718. g_InputState.dir = kDirNorth;
  719. g_InputState.dwKeyPress |= kKeyUp;
  720. break;
  721. case TOUCH_DOWN:
  722. g_InputState.dir = kDirSouth;
  723. g_InputState.dwKeyPress |= kKeyDown;
  724. break;
  725. case TOUCH_LEFT:
  726. g_InputState.dir = kDirWest;
  727. g_InputState.dwKeyPress |= kKeyLeft;
  728. break;
  729. case TOUCH_RIGHT:
  730. g_InputState.dir = kDirEast;
  731. g_InputState.dwKeyPress |= kKeyRight;
  732. break;
  733. case TOUCH_BUTTON1:
  734. if (gpGlobals->fInBattle)
  735. {
  736. g_InputState.dwKeyPress |= kKeyRepeat;
  737. }
  738. else
  739. {
  740. g_InputState.dwKeyPress |= kKeyForce;
  741. }
  742. break;
  743. case TOUCH_BUTTON2:
  744. g_InputState.dwKeyPress |= kKeyMenu;
  745. break;
  746. case TOUCH_BUTTON3:
  747. g_InputState.dwKeyPress |= kKeyUseItem;
  748. break;
  749. case TOUCH_BUTTON4:
  750. g_InputState.dwKeyPress |= kKeySearch;
  751. break;
  752. }
  753. }
  754. static VOID
  755. PAL_UnsetTouchAction(
  756. int area
  757. )
  758. {
  759. switch (area)
  760. {
  761. case TOUCH_UP:
  762. case TOUCH_DOWN:
  763. case TOUCH_LEFT:
  764. case TOUCH_RIGHT:
  765. g_InputState.dir = kDirUnknown;
  766. break;
  767. }
  768. }
  769. #endif
  770. static VOID
  771. PAL_TouchEventFilter(
  772. const SDL_Event *lpEvent
  773. )
  774. /*++
  775. Purpose:
  776. Handle touch events.
  777. Parameters:
  778. [IN] lpEvent - pointer to the event.
  779. Return value:
  780. None.
  781. --*/
  782. {
  783. #if PAL_HAS_TOUCH
  784. static SDL_TouchID finger1 = -1, finger2 = -1;
  785. static int prev_touch1 = TOUCH_NONE;
  786. static int prev_touch2 = TOUCH_NONE;
  787. switch (lpEvent->type)
  788. {
  789. case SDL_FINGERDOWN:
  790. if (finger1 == -1)
  791. {
  792. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  793. finger1 = lpEvent->tfinger.fingerId;
  794. prev_touch1 = area;
  795. PAL_SetTouchAction(area);
  796. }
  797. else if (finger2 == -1)
  798. {
  799. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  800. finger2 = lpEvent->tfinger.fingerId;
  801. prev_touch2 = area;
  802. PAL_SetTouchAction(area);
  803. }
  804. break;
  805. case SDL_FINGERUP:
  806. if (lpEvent->tfinger.fingerId == finger1)
  807. {
  808. PAL_UnsetTouchAction(prev_touch1);
  809. finger1 = -1;
  810. prev_touch1 = TOUCH_NONE;
  811. }
  812. else if (lpEvent->tfinger.fingerId == finger2)
  813. {
  814. PAL_UnsetTouchAction(prev_touch2);
  815. finger2 = -1;
  816. prev_touch2 = TOUCH_NONE;
  817. }
  818. break;
  819. case SDL_FINGERMOTION:
  820. if (lpEvent->tfinger.fingerId == finger1)
  821. {
  822. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  823. if (prev_touch1 != area && area != TOUCH_NONE)
  824. {
  825. PAL_UnsetTouchAction(prev_touch1);
  826. prev_touch1 = area;
  827. PAL_SetTouchAction(area);
  828. }
  829. }
  830. else if (lpEvent->tfinger.fingerId == finger2)
  831. {
  832. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  833. if (prev_touch2 != area && area != TOUCH_NONE)
  834. {
  835. PAL_UnsetTouchAction(prev_touch2);
  836. prev_touch2 = area;
  837. PAL_SetTouchAction(area);
  838. }
  839. }
  840. break;
  841. }
  842. #endif
  843. }
  844. static int SDLCALL
  845. PAL_EventFilter(
  846. const SDL_Event *lpEvent
  847. )
  848. /*++
  849. Purpose:
  850. SDL event filter function. A filter to process all events.
  851. Parameters:
  852. [IN] lpEvent - pointer to the event.
  853. Return value:
  854. 1 = the event will be added to the internal queue.
  855. 0 = the event will be dropped from the queue.
  856. --*/
  857. {
  858. switch (lpEvent->type)
  859. {
  860. #if SDL_VERSION_ATLEAST(2,0,0)
  861. case SDL_WINDOWEVENT:
  862. if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED)
  863. {
  864. //
  865. // resized the window
  866. //
  867. VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
  868. }
  869. break;
  870. #ifdef __IOS__
  871. case SDL_APP_WILLENTERBACKGROUND:
  872. g_bRenderPaused = TRUE;
  873. break;
  874. case SDL_APP_DIDENTERFOREGROUND:
  875. g_bRenderPaused = FALSE;
  876. VIDEO_UpdateScreen(NULL);
  877. break;
  878. #endif
  879. #else
  880. case SDL_VIDEORESIZE:
  881. //
  882. // resized the window
  883. //
  884. VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
  885. break;
  886. #endif
  887. case SDL_QUIT:
  888. //
  889. // clicked on the close button of the window. Quit immediately.
  890. //
  891. PAL_Shutdown(0);
  892. }
  893. PAL_KeyboardEventFilter(lpEvent);
  894. PAL_MouseEventFilter(lpEvent);
  895. PAL_JoystickEventFilter(lpEvent);
  896. PAL_TouchEventFilter(lpEvent);
  897. //
  898. // All events are handled here; don't put anything to the internal queue
  899. //
  900. return 0;
  901. }
  902. VOID
  903. PAL_ClearKeyState(
  904. VOID
  905. )
  906. /*++
  907. Purpose:
  908. Clear the record of pressed keys.
  909. Parameters:
  910. None.
  911. Return value:
  912. None.
  913. --*/
  914. {
  915. g_InputState.dwKeyPress = 0;
  916. }
  917. VOID
  918. PAL_InitInput(
  919. VOID
  920. )
  921. /*++
  922. Purpose:
  923. Initialize the input subsystem.
  924. Parameters:
  925. None.
  926. Return value:
  927. None.
  928. --*/
  929. {
  930. memset((void *)&g_InputState, 0, sizeof(g_InputState));
  931. g_InputState.dir = kDirUnknown;
  932. g_InputState.prevdir = kDirUnknown;
  933. //
  934. // Check for joystick
  935. //
  936. #if PAL_HAS_JOYSTICKS
  937. if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
  938. {
  939. int i;
  940. for (i = 0; i < SDL_NumJoysticks(); i++)
  941. {
  942. //
  943. // HACKHACK: applesmc and Android Accelerometer shouldn't be considered as real joysticks
  944. //
  945. if (strcmp(SDL_JoystickNameForIndex(i), "applesmc") != 0 && strcmp(SDL_JoystickNameForIndex(i), "Android Accelerometer") != 0)
  946. {
  947. g_pJoy = SDL_JoystickOpen(i);
  948. break;
  949. }
  950. }
  951. if (g_pJoy != NULL)
  952. {
  953. SDL_JoystickEventState(SDL_ENABLE);
  954. }
  955. }
  956. #endif
  957. #ifdef PAL_ALLOW_KEYREPEAT
  958. SDL_EnableKeyRepeat(0, 0);
  959. #endif
  960. #ifdef __N3DS__
  961. SDL_N3DSKeyBind(KEY_A, SDLK_RETURN);
  962. SDL_N3DSKeyBind(KEY_B, SDLK_ESCAPE);
  963. SDL_N3DSKeyBind(KEY_CPAD_UP, SDLK_UP);
  964. SDL_N3DSKeyBind(KEY_CPAD_DOWN, SDLK_DOWN);
  965. SDL_N3DSKeyBind(KEY_CPAD_LEFT, SDLK_LEFT);
  966. SDL_N3DSKeyBind(KEY_CPAD_RIGHT, SDLK_RIGHT);
  967. #endif
  968. }
  969. VOID
  970. PAL_ShutdownInput(
  971. VOID
  972. )
  973. /*++
  974. Purpose:
  975. Shutdown the input subsystem.
  976. Parameters:
  977. None.
  978. Return value:
  979. None.
  980. --*/
  981. {
  982. #if PAL_HAS_JOYSTICKS
  983. #if SDL_VERSION_ATLEAST(2,0,0)
  984. if (g_pJoy != NULL)
  985. {
  986. SDL_JoystickClose(g_pJoy);
  987. g_pJoy = NULL;
  988. }
  989. #else
  990. if (SDL_JoystickOpened(0))
  991. {
  992. assert(g_pJoy != NULL);
  993. SDL_JoystickClose(g_pJoy);
  994. g_pJoy = NULL;
  995. }
  996. #endif
  997. #endif
  998. }
  999. VOID
  1000. PAL_ProcessEvent(
  1001. VOID
  1002. )
  1003. /*++
  1004. Purpose:
  1005. Process all events.
  1006. Parameters:
  1007. None.
  1008. Return value:
  1009. None.
  1010. --*/
  1011. {
  1012. #if PAL_HAS_NATIVEMIDI
  1013. MIDI_CheckLoop();
  1014. #endif
  1015. while (PAL_PollEvent(NULL));
  1016. }
  1017. int
  1018. PAL_PollEvent(
  1019. SDL_Event *event
  1020. )
  1021. /*++
  1022. Purpose:
  1023. Poll and process one event.
  1024. Parameters:
  1025. [OUT] event - Events polled from SDL.
  1026. Return value:
  1027. Return value of PAL_PollEvent.
  1028. --*/
  1029. {
  1030. SDL_Event evt;
  1031. int ret = SDL_PollEvent(&evt);
  1032. if (ret != 0)
  1033. {
  1034. PAL_EventFilter(&evt);
  1035. }
  1036. if (event != NULL)
  1037. {
  1038. *event = evt;
  1039. }
  1040. return ret;
  1041. }