input.c 24 KB

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