input.c 25 KB

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