input.c 22 KB

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