input.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. //
  458. // Moved an axis on joystick
  459. //
  460. switch (lpEvent->jaxis.axis)
  461. {
  462. case 0:
  463. //
  464. // X axis
  465. //
  466. if (lpEvent->jaxis.value > 3200)
  467. {
  468. g_InputState.axisX = 1;
  469. }
  470. else if (lpEvent->jaxis.value < -3200)
  471. {
  472. g_InputState.axisX = -1;
  473. }
  474. else
  475. {
  476. g_InputState.axisX = 0;
  477. }
  478. break;
  479. case 1:
  480. //
  481. // Y axis
  482. //
  483. if (lpEvent->jaxis.value > 3200)
  484. {
  485. g_InputState.axisY = 1;
  486. }
  487. else if (lpEvent->jaxis.value < -3200)
  488. {
  489. g_InputState.axisY = -1;
  490. }
  491. else
  492. {
  493. g_InputState.axisY = 0;
  494. }
  495. break;
  496. }
  497. break;
  498. case SDL_JOYHATMOTION:
  499. //
  500. // Pressed the joystick hat button
  501. //
  502. switch (lpEvent->jhat.value)
  503. {
  504. case SDL_HAT_LEFT:
  505. case SDL_HAT_LEFTUP:
  506. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  507. g_InputState.dir = kDirWest;
  508. g_InputState.dwKeyPress = kKeyLeft;
  509. break;
  510. case SDL_HAT_RIGHT:
  511. case SDL_HAT_RIGHTDOWN:
  512. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  513. g_InputState.dir = kDirEast;
  514. g_InputState.dwKeyPress = kKeyRight;
  515. break;
  516. case SDL_HAT_UP:
  517. case SDL_HAT_RIGHTUP:
  518. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  519. g_InputState.dir = kDirNorth;
  520. g_InputState.dwKeyPress = kKeyUp;
  521. break;
  522. case SDL_HAT_DOWN:
  523. case SDL_HAT_LEFTDOWN:
  524. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  525. g_InputState.dir = kDirSouth;
  526. g_InputState.dwKeyPress = kKeyDown;
  527. break;
  528. case SDL_HAT_CENTERED:
  529. g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
  530. g_InputState.dir = kDirUnknown;
  531. g_InputState.dwKeyPress = kKeyNone;
  532. break;
  533. }
  534. break;
  535. case SDL_JOYBUTTONDOWN:
  536. //
  537. // Pressed the joystick button
  538. //
  539. switch (lpEvent->jbutton.button & 1)
  540. {
  541. case 0:
  542. g_InputState.dwKeyPress |= kKeyMenu;
  543. break;
  544. case 1:
  545. g_InputState.dwKeyPress |= kKeySearch;
  546. break;
  547. }
  548. break;
  549. }
  550. #endif
  551. }
  552. #if PAL_HAS_JOYSTICKS
  553. static VOID
  554. PAL_UpdateJoyStickState(
  555. VOID
  556. )
  557. /*++
  558. Purpose:
  559. Poll & update joystick state.
  560. Parameters:
  561. None.
  562. Return value:
  563. None.
  564. --*/
  565. {
  566. if( g_InputState.axisX == 1 && g_InputState.axisY >= 0 )
  567. {
  568. g_InputState.prevdir = g_InputState.dir;
  569. g_InputState.dir = kDirEast;
  570. g_InputState.dwKeyPress |= kKeyRight;
  571. }
  572. else if( g_InputState.axisX == -1 && g_InputState.axisY <= 0 )
  573. {
  574. g_InputState.prevdir = g_InputState.dir;
  575. g_InputState.dir = kDirWest;
  576. g_InputState.dwKeyPress |= kKeyLeft;
  577. }
  578. else if( g_InputState.axisY == 1 && g_InputState.axisX <= 0 )
  579. {
  580. g_InputState.prevdir = g_InputState.dir;
  581. g_InputState.dir = kDirSouth;
  582. g_InputState.dwKeyPress |= kKeyDown;
  583. }
  584. else if( g_InputState.axisY == -1 && g_InputState.axisX >= 0 )
  585. {
  586. g_InputState.prevdir = g_InputState.dir;
  587. g_InputState.dir = kDirNorth;
  588. g_InputState.dwKeyPress |= kKeyUp;
  589. }
  590. else
  591. {
  592. g_InputState.prevdir = g_InputState.dir;
  593. g_InputState.dir = kDirUnknown;
  594. if(!input_event_filter)
  595. g_InputState.dwKeyPress = kKeyNone;
  596. }
  597. }
  598. #endif
  599. #if PAL_HAS_TOUCH
  600. #define TOUCH_NONE 0
  601. #define TOUCH_UP 1
  602. #define TOUCH_DOWN 2
  603. #define TOUCH_LEFT 3
  604. #define TOUCH_RIGHT 4
  605. #define TOUCH_BUTTON1 5
  606. #define TOUCH_BUTTON2 6
  607. #define TOUCH_BUTTON3 7
  608. #define TOUCH_BUTTON4 8
  609. static float gfTouchXMin = 0.0f;
  610. static float gfTouchXMax = 1.0f;
  611. static float gfTouchYMin = 0.0f;
  612. static float gfTouchYMax = 1.0f;
  613. static SDL_TouchID gFinger1 = -1, gFinger2 = -1;
  614. static DWORD g_dwFinger1Time = 0, g_dwFinger2Time = 0;
  615. static int g_iPrevTouch1 = TOUCH_NONE;
  616. static int g_iPrevTouch2 = TOUCH_NONE;
  617. VOID
  618. PAL_SetTouchBounds(
  619. DWORD dwScreenWidth,
  620. DWORD dwScreenHeight,
  621. SDL_Rect renderRect
  622. )
  623. {
  624. gfTouchXMin = (float)renderRect.x / dwScreenWidth;
  625. gfTouchXMax = (float)(renderRect.x + renderRect.w) / dwScreenWidth;
  626. gfTouchYMin = (float)renderRect.y / dwScreenHeight;
  627. gfTouchYMax = (float)(renderRect.y + renderRect.h) / dwScreenHeight;
  628. }
  629. static int
  630. PAL_GetTouchArea(
  631. float X,
  632. float Y
  633. )
  634. {
  635. if (X < gfTouchXMin || X > gfTouchXMax || Y < 0.5f || Y > gfTouchYMax)
  636. {
  637. //
  638. // Upper area or cropped area
  639. //
  640. return TOUCH_NONE;
  641. }
  642. else
  643. {
  644. X = (X - gfTouchXMin) / (gfTouchXMax - gfTouchXMin);
  645. Y = (Y - gfTouchYMin) / (gfTouchYMax - gfTouchYMin);
  646. }
  647. if (X < 1.0f / 3)
  648. {
  649. if (Y - 0.5f < (1.0f / 6 - fabsf(X - 1.0f / 3 / 2)) * (0.5f / (1.0f / 3)))
  650. {
  651. return TOUCH_UP;
  652. }
  653. else if (Y - 0.75f > fabsf(X - 1.0f / 3 / 2) * (0.5f / (1.0f / 3)))
  654. {
  655. return TOUCH_DOWN;
  656. }
  657. else if (X < 1.0f / 3 / 2 && fabsf(Y - 0.75f) < 0.25f - X * (0.5f / (1.0f / 3)))
  658. {
  659. return TOUCH_LEFT;
  660. }
  661. else
  662. {
  663. return TOUCH_RIGHT;
  664. }
  665. }
  666. else if (X > 1.0f - 1.0f / 3)
  667. {
  668. if (X < 1.0f - (1.0f / 3 / 2))
  669. {
  670. if (Y < 0.75f)
  671. {
  672. return TOUCH_BUTTON1;
  673. }
  674. else
  675. {
  676. return TOUCH_BUTTON3;
  677. }
  678. }
  679. else
  680. {
  681. if (Y < 0.75f)
  682. {
  683. return TOUCH_BUTTON2;
  684. }
  685. else
  686. {
  687. return TOUCH_BUTTON4;
  688. }
  689. }
  690. }
  691. else
  692. {
  693. return TOUCH_NONE;
  694. }
  695. }
  696. static VOID
  697. PAL_SetTouchAction(
  698. int area
  699. )
  700. {
  701. switch (area)
  702. {
  703. case TOUCH_UP:
  704. g_InputState.dir = kDirNorth;
  705. g_InputState.dwKeyPress |= kKeyUp;
  706. break;
  707. case TOUCH_DOWN:
  708. g_InputState.dir = kDirSouth;
  709. g_InputState.dwKeyPress |= kKeyDown;
  710. break;
  711. case TOUCH_LEFT:
  712. g_InputState.dir = kDirWest;
  713. g_InputState.dwKeyPress |= kKeyLeft;
  714. break;
  715. case TOUCH_RIGHT:
  716. g_InputState.dir = kDirEast;
  717. g_InputState.dwKeyPress |= kKeyRight;
  718. break;
  719. case TOUCH_BUTTON1:
  720. g_InputState.dwKeyPress |= kKeyForce;
  721. break;
  722. case TOUCH_BUTTON2:
  723. g_InputState.dwKeyPress |= kKeyMenu;
  724. break;
  725. case TOUCH_BUTTON3:
  726. if (gpGlobals->fInBattle)
  727. {
  728. g_InputState.dwKeyPress |= kKeyRepeat;
  729. }
  730. else
  731. {
  732. g_InputState.dwKeyPress |= kKeyUseItem;
  733. }
  734. break;
  735. case TOUCH_BUTTON4:
  736. g_InputState.dwKeyPress |= kKeySearch;
  737. break;
  738. }
  739. }
  740. static VOID
  741. PAL_UnsetTouchAction(
  742. int area
  743. )
  744. {
  745. switch (area)
  746. {
  747. case TOUCH_UP:
  748. case TOUCH_DOWN:
  749. case TOUCH_LEFT:
  750. case TOUCH_RIGHT:
  751. g_InputState.dir = kDirUnknown;
  752. break;
  753. }
  754. }
  755. static VOID
  756. PAL_TouchRepeatCheck(
  757. VOID
  758. )
  759. {
  760. if (gFinger1 != -1 && SDL_GetTicks() > g_dwFinger1Time)
  761. {
  762. PAL_UnsetTouchAction(g_iPrevTouch1);
  763. PAL_SetTouchAction(g_iPrevTouch1);
  764. g_dwFinger1Time = SDL_GetTicks() + 120;
  765. }
  766. if (gFinger2 != -1 && SDL_GetTicks() > g_dwFinger2Time)
  767. {
  768. PAL_UnsetTouchAction(g_iPrevTouch2);
  769. PAL_SetTouchAction(g_iPrevTouch2);
  770. g_dwFinger2Time = SDL_GetTicks() + 120;
  771. }
  772. }
  773. #endif
  774. static VOID
  775. PAL_TouchEventFilter(
  776. const SDL_Event *lpEvent
  777. )
  778. /*++
  779. Purpose:
  780. Handle touch events.
  781. Parameters:
  782. [IN] lpEvent - pointer to the event.
  783. Return value:
  784. None.
  785. --*/
  786. {
  787. #if PAL_HAS_TOUCH
  788. switch (lpEvent->type)
  789. {
  790. case SDL_FINGERDOWN:
  791. if (gFinger1 == -1)
  792. {
  793. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  794. gFinger1 = lpEvent->tfinger.fingerId;
  795. g_iPrevTouch1 = area;
  796. PAL_SetTouchAction(area);
  797. g_dwFinger1Time = SDL_GetTicks() + 500;
  798. }
  799. else if (gFinger2 == -1)
  800. {
  801. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  802. gFinger2 = lpEvent->tfinger.fingerId;
  803. g_iPrevTouch2 = area;
  804. PAL_SetTouchAction(area);
  805. g_dwFinger2Time = SDL_GetTicks() + 500;
  806. }
  807. break;
  808. case SDL_FINGERUP:
  809. if (lpEvent->tfinger.fingerId == gFinger1)
  810. {
  811. PAL_UnsetTouchAction(g_iPrevTouch1);
  812. gFinger1 = -1;
  813. g_iPrevTouch1 = TOUCH_NONE;
  814. }
  815. else if (lpEvent->tfinger.fingerId == gFinger2)
  816. {
  817. PAL_UnsetTouchAction(g_iPrevTouch2);
  818. gFinger2 = -1;
  819. g_iPrevTouch2 = TOUCH_NONE;
  820. }
  821. break;
  822. case SDL_FINGERMOTION:
  823. if (lpEvent->tfinger.fingerId == gFinger1)
  824. {
  825. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  826. if (g_iPrevTouch1 != area && area != TOUCH_NONE)
  827. {
  828. PAL_UnsetTouchAction(g_iPrevTouch1);
  829. g_iPrevTouch1 = area;
  830. PAL_SetTouchAction(area);
  831. g_dwFinger1Time = SDL_GetTicks() + 500;
  832. }
  833. }
  834. else if (lpEvent->tfinger.fingerId == gFinger2)
  835. {
  836. int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
  837. if (g_iPrevTouch2 != area && area != TOUCH_NONE)
  838. {
  839. PAL_UnsetTouchAction(g_iPrevTouch2);
  840. g_iPrevTouch2 = area;
  841. PAL_SetTouchAction(area);
  842. g_dwFinger2Time = SDL_GetTicks() + 500;
  843. }
  844. }
  845. break;
  846. }
  847. #endif
  848. }
  849. static int SDLCALL
  850. PAL_EventFilter(
  851. const SDL_Event *lpEvent
  852. )
  853. /*++
  854. Purpose:
  855. SDL event filter function. A filter to process all events.
  856. Parameters:
  857. [IN] lpEvent - pointer to the event.
  858. Return value:
  859. 1 = the event will be added to the internal queue.
  860. 0 = the event will be dropped from the queue.
  861. --*/
  862. {
  863. switch (lpEvent->type)
  864. {
  865. #if SDL_VERSION_ATLEAST(2,0,0)
  866. case SDL_WINDOWEVENT:
  867. if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED)
  868. {
  869. //
  870. // resized the window
  871. //
  872. VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
  873. }
  874. break;
  875. case SDL_APP_WILLENTERBACKGROUND:
  876. g_bRenderPaused = TRUE;
  877. break;
  878. case SDL_APP_DIDENTERFOREGROUND:
  879. g_bRenderPaused = FALSE;
  880. VIDEO_UpdateScreen(NULL);
  881. break;
  882. #else
  883. case SDL_VIDEORESIZE:
  884. //
  885. // resized the window
  886. //
  887. VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
  888. break;
  889. #endif
  890. case SDL_QUIT:
  891. //
  892. // clicked on the close button of the window. Quit immediately.
  893. //
  894. PAL_Shutdown(0);
  895. }
  896. PAL_KeyboardEventFilter(lpEvent);
  897. PAL_MouseEventFilter(lpEvent);
  898. PAL_JoystickEventFilter(lpEvent);
  899. PAL_TouchEventFilter(lpEvent);
  900. //
  901. // All events are handled here; don't put anything to the internal queue
  902. //
  903. return 0;
  904. }
  905. VOID
  906. PAL_ClearKeyState(
  907. VOID
  908. )
  909. /*++
  910. Purpose:
  911. Clear the record of pressed keys.
  912. Parameters:
  913. None.
  914. Return value:
  915. None.
  916. --*/
  917. {
  918. g_InputState.dwKeyPress = 0;
  919. }
  920. VOID
  921. PAL_InitInput(
  922. VOID
  923. )
  924. /*++
  925. Purpose:
  926. Initialize the input subsystem.
  927. Parameters:
  928. None.
  929. Return value:
  930. None.
  931. --*/
  932. {
  933. memset((void *)&g_InputState, 0, sizeof(g_InputState));
  934. g_InputState.dir = kDirUnknown;
  935. g_InputState.prevdir = kDirUnknown;
  936. //
  937. // Check for joystick
  938. //
  939. #if PAL_HAS_JOYSTICKS
  940. if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
  941. {
  942. int i;
  943. for (i = 0; i < SDL_NumJoysticks(); i++)
  944. {
  945. if (PAL_IS_VALID_JOYSTICK(SDL_JoystickNameForIndex(i)))
  946. {
  947. g_pJoy = SDL_JoystickOpen(i);
  948. break;
  949. }
  950. }
  951. if (g_pJoy != NULL)
  952. {
  953. SDL_JoystickEventState(SDL_ENABLE);
  954. }
  955. }
  956. #endif
  957. input_init_filter();
  958. }
  959. VOID
  960. PAL_ShutdownInput(
  961. VOID
  962. )
  963. /*++
  964. Purpose:
  965. Shutdown the input subsystem.
  966. Parameters:
  967. None.
  968. Return value:
  969. None.
  970. --*/
  971. {
  972. #if PAL_HAS_JOYSTICKS
  973. if (g_pJoy != NULL)
  974. {
  975. SDL_JoystickClose(g_pJoy);
  976. g_pJoy = NULL;
  977. }
  978. #endif
  979. input_shutdown_filter();
  980. }
  981. static int
  982. PAL_PollEvent(
  983. SDL_Event *event
  984. )
  985. /*++
  986. Purpose:
  987. Poll and process one event.
  988. Parameters:
  989. [OUT] event - Events polled from SDL.
  990. Return value:
  991. Return value of PAL_PollEvent.
  992. --*/
  993. {
  994. SDL_Event evt;
  995. int ret = SDL_PollEvent(&evt);
  996. if (ret != 0 && !input_event_filter(&evt, &g_InputState))
  997. {
  998. PAL_EventFilter(&evt);
  999. }
  1000. if (event != NULL)
  1001. {
  1002. *event = evt;
  1003. }
  1004. return ret;
  1005. }
  1006. VOID
  1007. PAL_ProcessEvent(
  1008. VOID
  1009. )
  1010. /*++
  1011. Purpose:
  1012. Process all events.
  1013. Parameters:
  1014. None.
  1015. Return value:
  1016. None.
  1017. --*/
  1018. {
  1019. while (PAL_PollEvent(NULL));
  1020. PAL_UpdateKeyboardState();
  1021. #if PAL_HAS_JOYSTICKS
  1022. PAL_UpdateJoyStickState();
  1023. #endif
  1024. #if PAL_HAS_TOUCH
  1025. PAL_TouchRepeatCheck();
  1026. #endif
  1027. }
  1028. VOID
  1029. PAL_RegisterInputFilter(
  1030. void (*init_filter)(),
  1031. int (*event_filter)(const SDL_Event *, volatile PALINPUTSTATE *),
  1032. void (*shutdown_filter)()
  1033. )
  1034. /*++
  1035. Purpose:
  1036. Register caller-defined input event filter.
  1037. Parameters:
  1038. [IN] init_filter - Filter that will be called inside PAL_InitInput
  1039. [IN] event_filter - Filter that will be called inside PAL_PollEvent,
  1040. return non-zero value from this filter disables
  1041. further internal event processing.
  1042. [IN] shutdown_filter - Filter that will be called inside PAL_ShutdownInput
  1043. Passing NULL to either parameter means the caller does not provide such filter.
  1044. Return value:
  1045. None.
  1046. --*/
  1047. {
  1048. if (init_filter)
  1049. input_init_filter = init_filter;
  1050. if (event_filter)
  1051. input_event_filter = event_filter;
  1052. if (shutdown_filter)
  1053. input_shutdown_filter = shutdown_filter;
  1054. }