input.c 22 KB

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