input.c 22 KB

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