input.c 23 KB

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