input.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. #ifdef PAL_HAS_TOUCH
  587. #define TOUCH_NONE 0
  588. #define TOUCH_UP 1
  589. #define TOUCH_DOWN 2
  590. #define TOUCH_LEFT 3
  591. #define TOUCH_RIGHT 4
  592. #define TOUCH_BUTTON1 5
  593. #define TOUCH_BUTTON2 6
  594. #define TOUCH_BUTTON3 7
  595. #define TOUCH_BUTTON4 8
  596. static int
  597. PAL_GetTouchArea(
  598. float X,
  599. float Y
  600. )
  601. {
  602. if (Y < 0.5)
  603. {
  604. //
  605. // Upper area
  606. //
  607. return TOUCH_NONE;
  608. }
  609. else if (X < 0.4)
  610. {
  611. if (Y - 0.5 < (0.2 - fabs(X - 0.2)) * (0.25 / 0.2))
  612. {
  613. return TOUCH_UP;
  614. }
  615. else if (Y - 0.75 > fabs(X - 0.2) * (0.25 / 0.2))
  616. {
  617. return TOUCH_DOWN;
  618. }
  619. else if (X < 0.2 && fabs(Y - 0.75) < 0.25 - X * (0.25 / 0.2))
  620. {
  621. return TOUCH_LEFT;
  622. }
  623. else
  624. {
  625. return TOUCH_RIGHT;
  626. }
  627. }
  628. else if (X > 0.6)
  629. {
  630. if (X < 0.8)
  631. {
  632. if (Y < 0.75)
  633. {
  634. return TOUCH_BUTTON1;
  635. }
  636. else
  637. {
  638. return TOUCH_BUTTON3;
  639. }
  640. }
  641. else
  642. {
  643. if (Y < 0.75)
  644. {
  645. return TOUCH_BUTTON2;
  646. }
  647. else
  648. {
  649. return TOUCH_BUTTON4;
  650. }
  651. }
  652. }
  653. return TOUCH_NONE;
  654. }
  655. static VOID
  656. PAL_SetTouchAction(
  657. int area
  658. )
  659. {
  660. switch (area)
  661. {
  662. case TOUCH_UP:
  663. g_InputState.dir = kDirNorth;
  664. g_InputState.dwKeyPress |= kKeyUp;
  665. break;
  666. case TOUCH_DOWN:
  667. g_InputState.dir = kDirSouth;
  668. g_InputState.dwKeyPress |= kKeyDown;
  669. break;
  670. case TOUCH_LEFT:
  671. g_InputState.dir = kDirWest;
  672. g_InputState.dwKeyPress |= kKeyLeft;
  673. break;
  674. case TOUCH_RIGHT:
  675. g_InputState.dir = kDirEast;
  676. g_InputState.dwKeyPress |= kKeyRight;
  677. break;
  678. case TOUCH_BUTTON1:
  679. break;
  680. case TOUCH_BUTTON2:
  681. g_InputState.dwKeyPress |= kKeyMenu;
  682. break;
  683. case TOUCH_BUTTON3:
  684. break;
  685. case TOUCH_BUTTON4:
  686. g_InputState.dwKeyPress |= kKeySearch;
  687. break;
  688. }
  689. }
  690. static VOID
  691. PAL_UnsetTouchAction(
  692. int area
  693. )
  694. {
  695. switch (area)
  696. {
  697. case TOUCH_UP:
  698. case TOUCH_DOWN:
  699. case TOUCH_LEFT:
  700. case TOUCH_RIGHT:
  701. g_InputState.dir = kDirUnknown;
  702. break;
  703. }
  704. }
  705. #endif
  706. static VOID
  707. PAL_TouchEventFilter(
  708. const SDL_Event *lpEvent
  709. )
  710. /*++
  711. Purpose:
  712. Handle touch events.
  713. Parameters:
  714. [IN] lpEvent - pointer to the event.
  715. Return value:
  716. None.
  717. --*/
  718. {
  719. #ifdef PAL_HAS_TOUCH
  720. static SDL_TouchID finger1 = 0, finger2 = 0;
  721. static int prev_touch1 = TOUCH_NONE;
  722. static int prev_touch2 = TOUCH_NONE;
  723. switch (lpEvent->type)
  724. {
  725. case SDL_FINGERDOWN:
  726. if (finger1 == 0)
  727. {
  728. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  729. if (area != TOUCH_NONE)
  730. {
  731. finger1 = lpEvent->tfinger.fingerId;
  732. prev_touch1 = area;
  733. PAL_SetTouchAction(area);
  734. }
  735. }
  736. else if (finger2 == 0)
  737. {
  738. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  739. if (area != TOUCH_NONE)
  740. {
  741. finger2 = lpEvent->tfinger.fingerId;
  742. prev_touch2 = area;
  743. PAL_SetTouchAction(area);
  744. }
  745. }
  746. break;
  747. case SDL_FINGERUP:
  748. if (lpEvent->tfinger.fingerId == finger1)
  749. {
  750. PAL_UnsetTouchAction(prev_touch1);
  751. finger1 = 0;
  752. prev_touch1 = TOUCH_NONE;
  753. }
  754. else if (lpEvent->tfinger.fingerId == finger2)
  755. {
  756. PAL_UnsetTouchAction(prev_touch2);
  757. finger2 = 0;
  758. prev_touch2 = TOUCH_NONE;
  759. }
  760. break;
  761. case SDL_FINGERMOTION:
  762. if (lpEvent->tfinger.fingerId == finger1)
  763. {
  764. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  765. PAL_UnsetTouchAction(prev_touch1);
  766. prev_touch1 = area;
  767. PAL_SetTouchAction(area);
  768. }
  769. else if (lpEvent->tfinger.fingerId == finger2)
  770. {
  771. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  772. PAL_UnsetTouchAction(prev_touch2);
  773. prev_touch2 = area;
  774. PAL_SetTouchAction(area);
  775. }
  776. break;
  777. }
  778. #endif
  779. }
  780. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  781. static int SDLCALL
  782. PAL_EventFilter(
  783. const SDL_Event *lpEvent
  784. )
  785. #else
  786. static int SDLCALL
  787. PAL_EventFilter(
  788. void *userdata,
  789. SDL_Event *lpEvent
  790. )
  791. #endif
  792. /*++
  793. Purpose:
  794. SDL event filter function. A filter to process all events.
  795. Parameters:
  796. [IN] lpEvent - pointer to the event.
  797. Return value:
  798. 1 = the event will be added to the internal queue.
  799. 0 = the event will be dropped from the queue.
  800. --*/
  801. {
  802. switch (lpEvent->type)
  803. {
  804. #if SDL_VERSION_ATLEAST(2,0,0)
  805. case SDL_WINDOWEVENT:
  806. if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED)
  807. {
  808. //
  809. // resized the window
  810. //
  811. VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
  812. }
  813. break;
  814. #else
  815. case SDL_VIDEORESIZE:
  816. //
  817. // resized the window
  818. //
  819. VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
  820. break;
  821. #endif
  822. case SDL_QUIT:
  823. //
  824. // clicked on the close button of the window. Quit immediately.
  825. //
  826. PAL_Shutdown();
  827. exit(0);
  828. }
  829. PAL_KeyboardEventFilter(lpEvent);
  830. PAL_MouseEventFilter(lpEvent);
  831. PAL_JoystickEventFilter(lpEvent);
  832. PAL_TouchEventFilter(lpEvent);
  833. //
  834. // All events are handled here; don't put anything to the internal queue
  835. //
  836. return 0;
  837. }
  838. VOID
  839. PAL_ClearKeyState(
  840. VOID
  841. )
  842. /*++
  843. Purpose:
  844. Clear the record of pressed keys.
  845. Parameters:
  846. None.
  847. Return value:
  848. None.
  849. --*/
  850. {
  851. g_InputState.dwKeyPress = 0;
  852. }
  853. VOID
  854. PAL_InitInput(
  855. VOID
  856. )
  857. /*++
  858. Purpose:
  859. Initialize the input subsystem.
  860. Parameters:
  861. None.
  862. Return value:
  863. None.
  864. --*/
  865. {
  866. memset(&g_InputState, 0, sizeof(g_InputState));
  867. g_InputState.dir = kDirUnknown;
  868. g_InputState.prevdir = kDirUnknown;
  869. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  870. SDL_SetEventFilter(PAL_EventFilter);
  871. #else
  872. SDL_SetEventFilter(PAL_EventFilter, NULL);
  873. #endif
  874. //
  875. // Check for joystick
  876. //
  877. #ifdef PAL_HAS_JOYSTICKS
  878. if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
  879. {
  880. g_pJoy = SDL_JoystickOpen(0);
  881. if (g_pJoy != NULL)
  882. {
  883. SDL_JoystickEventState(SDL_ENABLE);
  884. }
  885. }
  886. #endif
  887. }
  888. VOID
  889. PAL_ShutdownInput(
  890. VOID
  891. )
  892. /*++
  893. Purpose:
  894. Shutdown the input subsystem.
  895. Parameters:
  896. None.
  897. Return value:
  898. None.
  899. --*/
  900. {
  901. #ifdef PAL_HAS_JOYSTICKS
  902. #if SDL_VERSION_ATLEAST(2,0,0)
  903. if (g_pJoy != NULL)
  904. {
  905. SDL_JoystickClose(g_pJoy);
  906. g_pJoy = NULL;
  907. }
  908. #else
  909. if (SDL_JoystickOpened(0))
  910. {
  911. assert(g_pJoy != NULL);
  912. SDL_JoystickClose(g_pJoy);
  913. g_pJoy = NULL;
  914. }
  915. #endif
  916. #endif
  917. }
  918. VOID
  919. PAL_ProcessEvent(
  920. VOID
  921. )
  922. /*++
  923. Purpose:
  924. Process all events.
  925. Parameters:
  926. None.
  927. Return value:
  928. None.
  929. --*/
  930. {
  931. #ifdef PAL_HAS_NATIVEMIDI
  932. MIDI_CheckLoop();
  933. #endif
  934. while (SDL_PollEvent(NULL));
  935. }