input.c 26 KB

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