input.c 26 KB

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