input.c 24 KB

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