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