input.c 25 KB

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