input.c 25 KB

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