input.c 25 KB

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