input.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. 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 = -1, finger2 = -1;
  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 == -1)
  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 == -1)
  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 = -1;
  755. prev_touch1 = TOUCH_NONE;
  756. }
  757. else if (lpEvent->tfinger.fingerId == finger2)
  758. {
  759. PAL_UnsetTouchAction(prev_touch2);
  760. finger2 = -1;
  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 && area != TOUCH_NONE)
  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 && area != TOUCH_NONE)
  779. {
  780. PAL_UnsetTouchAction(prev_touch2);
  781. prev_touch2 = area;
  782. PAL_SetTouchAction(area);
  783. }
  784. }
  785. break;
  786. }
  787. #endif
  788. }
  789. static int SDLCALL
  790. PAL_EventFilter(
  791. const SDL_Event *lpEvent
  792. )
  793. /*++
  794. Purpose:
  795. SDL event filter function. A filter to process all events.
  796. Parameters:
  797. [IN] lpEvent - pointer to the event.
  798. Return value:
  799. 1 = the event will be added to the internal queue.
  800. 0 = the event will be dropped from the queue.
  801. --*/
  802. {
  803. switch (lpEvent->type)
  804. {
  805. #if SDL_VERSION_ATLEAST(2,0,0)
  806. case SDL_WINDOWEVENT:
  807. if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED)
  808. {
  809. //
  810. // resized the window
  811. //
  812. VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
  813. }
  814. break;
  815. #ifdef __IOS__
  816. case SDL_APP_WILLENTERBACKGROUND:
  817. g_bRenderPaused = TRUE;
  818. break;
  819. case SDL_APP_DIDENTERFOREGROUND:
  820. g_bRenderPaused = FALSE;
  821. VIDEO_UpdateScreen(NULL);
  822. break;
  823. #endif
  824. #else
  825. case SDL_VIDEORESIZE:
  826. //
  827. // resized the window
  828. //
  829. VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
  830. break;
  831. #endif
  832. case SDL_QUIT:
  833. //
  834. // clicked on the close button of the window. Quit immediately.
  835. //
  836. PAL_Shutdown();
  837. exit(0);
  838. }
  839. PAL_KeyboardEventFilter(lpEvent);
  840. PAL_MouseEventFilter(lpEvent);
  841. PAL_JoystickEventFilter(lpEvent);
  842. PAL_TouchEventFilter(lpEvent);
  843. //
  844. // All events are handled here; don't put anything to the internal queue
  845. //
  846. return 0;
  847. }
  848. VOID
  849. PAL_ClearKeyState(
  850. VOID
  851. )
  852. /*++
  853. Purpose:
  854. Clear the record of pressed keys.
  855. Parameters:
  856. None.
  857. Return value:
  858. None.
  859. --*/
  860. {
  861. g_InputState.dwKeyPress = 0;
  862. }
  863. VOID
  864. PAL_InitInput(
  865. VOID
  866. )
  867. /*++
  868. Purpose:
  869. Initialize the input subsystem.
  870. Parameters:
  871. None.
  872. Return value:
  873. None.
  874. --*/
  875. {
  876. memset((void *)&g_InputState, 0, sizeof(g_InputState));
  877. g_InputState.dir = kDirUnknown;
  878. g_InputState.prevdir = kDirUnknown;
  879. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  880. SDL_SetEventFilter(PAL_EventFilter);
  881. #endif
  882. //
  883. // Check for joystick
  884. //
  885. #ifdef PAL_HAS_JOYSTICKS
  886. if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
  887. {
  888. g_pJoy = SDL_JoystickOpen(0);
  889. if (g_pJoy != NULL)
  890. {
  891. SDL_JoystickEventState(SDL_ENABLE);
  892. }
  893. }
  894. #endif
  895. }
  896. VOID
  897. PAL_ShutdownInput(
  898. VOID
  899. )
  900. /*++
  901. Purpose:
  902. Shutdown the input subsystem.
  903. Parameters:
  904. None.
  905. Return value:
  906. None.
  907. --*/
  908. {
  909. #ifdef PAL_HAS_JOYSTICKS
  910. #if SDL_VERSION_ATLEAST(2,0,0)
  911. if (g_pJoy != NULL)
  912. {
  913. SDL_JoystickClose(g_pJoy);
  914. g_pJoy = NULL;
  915. }
  916. #else
  917. if (SDL_JoystickOpened(0))
  918. {
  919. assert(g_pJoy != NULL);
  920. SDL_JoystickClose(g_pJoy);
  921. g_pJoy = NULL;
  922. }
  923. #endif
  924. #endif
  925. }
  926. VOID
  927. PAL_ProcessEvent(
  928. VOID
  929. )
  930. /*++
  931. Purpose:
  932. Process all events.
  933. Parameters:
  934. None.
  935. Return value:
  936. None.
  937. --*/
  938. {
  939. #ifdef PAL_HAS_NATIVEMIDI
  940. MIDI_CheckLoop();
  941. #endif
  942. while (PAL_PollEvent(NULL));
  943. }
  944. int
  945. PAL_PollEvent(
  946. SDL_Event *event
  947. )
  948. /*++
  949. Purpose:
  950. Poll and process one event.
  951. Parameters:
  952. [OUT] event - Events polled from SDL.
  953. Return value:
  954. Return value of PAL_PollEvent.
  955. --*/
  956. {
  957. #if SDL_VERSION_ATLEAST(2, 0, 0)
  958. //
  959. // SDL2 changed the timing of event callback so we need to handle
  960. // events manually when polled
  961. //
  962. SDL_Event evt;
  963. int ret = SDL_PollEvent(&evt);
  964. if (ret != 0)
  965. {
  966. PAL_EventFilter(&evt);
  967. }
  968. if (event != NULL)
  969. {
  970. *event = evt;
  971. }
  972. return ret;
  973. #else
  974. return SDL_PollEvent(event);
  975. #endif
  976. }