input.c 24 KB

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