input.c 20 KB

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