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