input.c 26 KB

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