input.c 24 KB

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