input.c 23 KB

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