input.c 24 KB

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