input.c 25 KB

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