input.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. //
  880. // Check for joystick
  881. //
  882. #ifdef PAL_HAS_JOYSTICKS
  883. if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
  884. {
  885. g_pJoy = SDL_JoystickOpen(0);
  886. if (g_pJoy != NULL)
  887. {
  888. SDL_JoystickEventState(SDL_ENABLE);
  889. }
  890. }
  891. #endif
  892. }
  893. VOID
  894. PAL_ShutdownInput(
  895. VOID
  896. )
  897. /*++
  898. Purpose:
  899. Shutdown the input subsystem.
  900. Parameters:
  901. None.
  902. Return value:
  903. None.
  904. --*/
  905. {
  906. #ifdef PAL_HAS_JOYSTICKS
  907. #if SDL_VERSION_ATLEAST(2,0,0)
  908. if (g_pJoy != NULL)
  909. {
  910. SDL_JoystickClose(g_pJoy);
  911. g_pJoy = NULL;
  912. }
  913. #else
  914. if (SDL_JoystickOpened(0))
  915. {
  916. assert(g_pJoy != NULL);
  917. SDL_JoystickClose(g_pJoy);
  918. g_pJoy = NULL;
  919. }
  920. #endif
  921. #endif
  922. }
  923. VOID
  924. PAL_ProcessEvent(
  925. VOID
  926. )
  927. /*++
  928. Purpose:
  929. Process all events.
  930. Parameters:
  931. None.
  932. Return value:
  933. None.
  934. --*/
  935. {
  936. #ifdef PAL_HAS_NATIVEMIDI
  937. MIDI_CheckLoop();
  938. #endif
  939. while (PAL_PollEvent(NULL));
  940. }
  941. int
  942. PAL_PollEvent(
  943. SDL_Event *event
  944. )
  945. /*++
  946. Purpose:
  947. Poll and process one event.
  948. Parameters:
  949. [OUT] event - Events polled from SDL.
  950. Return value:
  951. Return value of PAL_PollEvent.
  952. --*/
  953. {
  954. SDL_Event evt;
  955. int ret = SDL_PollEvent(&evt);
  956. if (ret != 0)
  957. {
  958. PAL_EventFilter(&evt);
  959. }
  960. if (event != NULL)
  961. {
  962. *event = evt;
  963. }
  964. return ret;
  965. }