in_win.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // in_win.c -- windows 95 mouse and joystick code
  16. // 02/21/97 JCB Added extended DirectInput code to support external controllers.
  17. #include "../client/client.h"
  18. #include "winquake.h"
  19. extern unsigned sys_msg_time;
  20. // joystick defines and variables
  21. // where should defines be moved?
  22. #define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick
  23. #define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball
  24. #define JOY_MAX_AXES 6 // X, Y, Z, R, U, V
  25. #define JOY_AXIS_X 0
  26. #define JOY_AXIS_Y 1
  27. #define JOY_AXIS_Z 2
  28. #define JOY_AXIS_R 3
  29. #define JOY_AXIS_U 4
  30. #define JOY_AXIS_V 5
  31. enum _ControlList
  32. {
  33. AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn, AxisUp
  34. };
  35. DWORD dwAxisFlags[JOY_MAX_AXES] =
  36. {
  37. JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
  38. };
  39. DWORD dwAxisMap[JOY_MAX_AXES];
  40. DWORD dwControlMap[JOY_MAX_AXES];
  41. PDWORD pdwRawValue[JOY_MAX_AXES];
  42. cvar_t *in_mouse;
  43. cvar_t *in_joystick;
  44. // none of these cvars are saved over a session
  45. // this means that advanced controller configuration needs to be executed
  46. // each time. this avoids any problems with getting back to a default usage
  47. // or when changing from one controller to another. this way at least something
  48. // works.
  49. cvar_t *joy_name;
  50. cvar_t *joy_advanced;
  51. cvar_t *joy_advaxisx;
  52. cvar_t *joy_advaxisy;
  53. cvar_t *joy_advaxisz;
  54. cvar_t *joy_advaxisr;
  55. cvar_t *joy_advaxisu;
  56. cvar_t *joy_advaxisv;
  57. cvar_t *joy_forwardthreshold;
  58. cvar_t *joy_sidethreshold;
  59. cvar_t *joy_pitchthreshold;
  60. cvar_t *joy_yawthreshold;
  61. cvar_t *joy_forwardsensitivity;
  62. cvar_t *joy_sidesensitivity;
  63. cvar_t *joy_pitchsensitivity;
  64. cvar_t *joy_yawsensitivity;
  65. cvar_t *joy_upthreshold;
  66. cvar_t *joy_upsensitivity;
  67. qboolean joy_avail, joy_advancedinit, joy_haspov;
  68. DWORD joy_oldbuttonstate, joy_oldpovstate;
  69. int joy_id;
  70. DWORD joy_flags;
  71. DWORD joy_numbuttons;
  72. static JOYINFOEX ji;
  73. qboolean in_appactive;
  74. // forward-referenced functions
  75. void IN_StartupJoystick (void);
  76. void Joy_AdvancedUpdate_f (void);
  77. void IN_JoyMove (usercmd_t *cmd);
  78. /*
  79. ============================================================
  80. MOUSE CONTROL
  81. ============================================================
  82. */
  83. // mouse variables
  84. cvar_t *m_filter;
  85. qboolean mlooking;
  86. void IN_MLookDown (void) { mlooking = true; }
  87. void IN_MLookUp (void) {
  88. mlooking = false;
  89. if (!freelook->value && lookspring->value)
  90. IN_CenterView ();
  91. }
  92. int mouse_buttons;
  93. int mouse_oldbuttonstate;
  94. POINT current_pos;
  95. int mouse_x, mouse_y, old_mouse_x, old_mouse_y, mx_accum, my_accum;
  96. int old_x, old_y;
  97. qboolean mouseactive; // false when not focus app
  98. qboolean restore_spi;
  99. qboolean mouseinitialized;
  100. int originalmouseparms[3], newmouseparms[3] = {0, 0, 1};
  101. qboolean mouseparmsvalid;
  102. int window_center_x, window_center_y;
  103. RECT window_rect;
  104. /*
  105. ===========
  106. IN_ActivateMouse
  107. Called when the window gains focus or changes in some way
  108. ===========
  109. */
  110. void IN_ActivateMouse (void)
  111. {
  112. int width, height;
  113. if (!mouseinitialized)
  114. return;
  115. if (!in_mouse->value)
  116. {
  117. mouseactive = false;
  118. return;
  119. }
  120. if (mouseactive)
  121. return;
  122. mouseactive = true;
  123. if (mouseparmsvalid)
  124. restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0);
  125. width = GetSystemMetrics (SM_CXSCREEN);
  126. height = GetSystemMetrics (SM_CYSCREEN);
  127. GetWindowRect ( cl_hwnd, &window_rect);
  128. if (window_rect.left < 0)
  129. window_rect.left = 0;
  130. if (window_rect.top < 0)
  131. window_rect.top = 0;
  132. if (window_rect.right >= width)
  133. window_rect.right = width-1;
  134. if (window_rect.bottom >= height-1)
  135. window_rect.bottom = height-1;
  136. window_center_x = (window_rect.right + window_rect.left)/2;
  137. window_center_y = (window_rect.top + window_rect.bottom)/2;
  138. SetCursorPos (window_center_x, window_center_y);
  139. old_x = window_center_x;
  140. old_y = window_center_y;
  141. SetCapture ( cl_hwnd );
  142. ClipCursor (&window_rect);
  143. while (ShowCursor (FALSE) >= 0)
  144. ;
  145. }
  146. /*
  147. ===========
  148. IN_DeactivateMouse
  149. Called when the window loses focus
  150. ===========
  151. */
  152. void IN_DeactivateMouse (void)
  153. {
  154. if (!mouseinitialized)
  155. return;
  156. if (!mouseactive)
  157. return;
  158. if (restore_spi)
  159. SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0);
  160. mouseactive = false;
  161. ClipCursor (NULL);
  162. ReleaseCapture ();
  163. while (ShowCursor (TRUE) < 0)
  164. ;
  165. }
  166. /*
  167. ===========
  168. IN_StartupMouse
  169. ===========
  170. */
  171. void IN_StartupMouse (void)
  172. {
  173. cvar_t *cv;
  174. cv = Cvar_Get ("in_initmouse", "1", CVAR_NOSET);
  175. if ( !cv->value )
  176. return;
  177. mouseinitialized = true;
  178. mouseparmsvalid = SystemParametersInfo (SPI_GETMOUSE, 0, originalmouseparms, 0);
  179. mouse_buttons = 3;
  180. }
  181. /*
  182. ===========
  183. IN_MouseEvent
  184. ===========
  185. */
  186. void IN_MouseEvent (int mstate)
  187. {
  188. int i;
  189. if (!mouseinitialized)
  190. return;
  191. // perform button actions
  192. for (i=0 ; i<mouse_buttons ; i++)
  193. {
  194. if ( (mstate & (1<<i)) &&
  195. !(mouse_oldbuttonstate & (1<<i)) )
  196. {
  197. Key_Event (K_MOUSE1 + i, true, sys_msg_time);
  198. }
  199. if ( !(mstate & (1<<i)) &&
  200. (mouse_oldbuttonstate & (1<<i)) )
  201. {
  202. Key_Event (K_MOUSE1 + i, false, sys_msg_time);
  203. }
  204. }
  205. mouse_oldbuttonstate = mstate;
  206. }
  207. /*
  208. ===========
  209. IN_MouseMove
  210. ===========
  211. */
  212. void IN_MouseMove (usercmd_t *cmd)
  213. {
  214. int mx, my;
  215. if (!mouseactive)
  216. return;
  217. // find mouse movement
  218. if (!GetCursorPos (&current_pos))
  219. return;
  220. mx = current_pos.x - window_center_x;
  221. my = current_pos.y - window_center_y;
  222. #if 0
  223. if (!mx && !my)
  224. return;
  225. #endif
  226. if (m_filter->value)
  227. {
  228. mouse_x = (mx + old_mouse_x) * 0.5;
  229. mouse_y = (my + old_mouse_y) * 0.5;
  230. }
  231. else
  232. {
  233. mouse_x = mx;
  234. mouse_y = my;
  235. }
  236. old_mouse_x = mx;
  237. old_mouse_y = my;
  238. mouse_x *= sensitivity->value;
  239. mouse_y *= sensitivity->value;
  240. // add mouse X/Y movement to cmd
  241. if ( (in_strafe.state & 1) || (lookstrafe->value && mlooking ))
  242. cmd->sidemove += m_side->value * mouse_x;
  243. else
  244. cl.viewangles[YAW] -= m_yaw->value * mouse_x;
  245. if ( (mlooking || freelook->value) && !(in_strafe.state & 1))
  246. {
  247. cl.viewangles[PITCH] += m_pitch->value * mouse_y;
  248. }
  249. else
  250. {
  251. cmd->forwardmove -= m_forward->value * mouse_y;
  252. }
  253. // force the mouse to the center, so there's room to move
  254. if (mx || my)
  255. SetCursorPos (window_center_x, window_center_y);
  256. }
  257. /*
  258. =========================================================================
  259. VIEW CENTERING
  260. =========================================================================
  261. */
  262. cvar_t *v_centermove;
  263. cvar_t *v_centerspeed;
  264. /*
  265. ===========
  266. IN_Init
  267. ===========
  268. */
  269. void IN_Init (void)
  270. {
  271. // mouse variables
  272. m_filter = Cvar_Get ("m_filter", "0", 0);
  273. in_mouse = Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
  274. // joystick variables
  275. in_joystick = Cvar_Get ("in_joystick", "0", CVAR_ARCHIVE);
  276. joy_name = Cvar_Get ("joy_name", "joystick", 0);
  277. joy_advanced = Cvar_Get ("joy_advanced", "0", 0);
  278. joy_advaxisx = Cvar_Get ("joy_advaxisx", "0", 0);
  279. joy_advaxisy = Cvar_Get ("joy_advaxisy", "0", 0);
  280. joy_advaxisz = Cvar_Get ("joy_advaxisz", "0", 0);
  281. joy_advaxisr = Cvar_Get ("joy_advaxisr", "0", 0);
  282. joy_advaxisu = Cvar_Get ("joy_advaxisu", "0", 0);
  283. joy_advaxisv = Cvar_Get ("joy_advaxisv", "0", 0);
  284. joy_forwardthreshold = Cvar_Get ("joy_forwardthreshold", "0.15", 0);
  285. joy_sidethreshold = Cvar_Get ("joy_sidethreshold", "0.15", 0);
  286. joy_upthreshold = Cvar_Get ("joy_upthreshold", "0.15", 0);
  287. joy_pitchthreshold = Cvar_Get ("joy_pitchthreshold", "0.15", 0);
  288. joy_yawthreshold = Cvar_Get ("joy_yawthreshold", "0.15", 0);
  289. joy_forwardsensitivity = Cvar_Get ("joy_forwardsensitivity", "-1", 0);
  290. joy_sidesensitivity = Cvar_Get ("joy_sidesensitivity", "-1", 0);
  291. joy_upsensitivity = Cvar_Get ("joy_upsensitivity", "-1", 0);
  292. joy_pitchsensitivity = Cvar_Get ("joy_pitchsensitivity", "1", 0);
  293. joy_yawsensitivity = Cvar_Get ("joy_yawsensitivity", "-1", 0);
  294. // centering
  295. v_centermove = Cvar_Get ("v_centermove", "0.15", 0);
  296. v_centerspeed = Cvar_Get ("v_centerspeed", "500", 0);
  297. Cmd_AddCommand ("+mlook", IN_MLookDown);
  298. Cmd_AddCommand ("-mlook", IN_MLookUp);
  299. Cmd_AddCommand ("joy_advancedupdate", Joy_AdvancedUpdate_f);
  300. IN_StartupMouse ();
  301. IN_StartupJoystick ();
  302. }
  303. /*
  304. ===========
  305. IN_Shutdown
  306. ===========
  307. */
  308. void IN_Shutdown (void)
  309. {
  310. IN_DeactivateMouse ();
  311. }
  312. /*
  313. ===========
  314. IN_Activate
  315. Called when the main window gains or loses focus.
  316. The window may have been destroyed and recreated
  317. between a deactivate and an activate.
  318. ===========
  319. */
  320. void IN_Activate (qboolean active)
  321. {
  322. in_appactive = active;
  323. mouseactive = !active; // force a new window check or turn off
  324. }
  325. /*
  326. ==================
  327. IN_Frame
  328. Called every frame, even if not generating commands
  329. ==================
  330. */
  331. void IN_Frame (void)
  332. {
  333. if (!mouseinitialized)
  334. return;
  335. if (!in_mouse || !in_appactive)
  336. {
  337. IN_DeactivateMouse ();
  338. return;
  339. }
  340. if ( !cl.refresh_prepped
  341. || cls.key_dest == key_console
  342. || cls.key_dest == key_menu)
  343. {
  344. // temporarily deactivate if in fullscreen
  345. if (Cvar_VariableValue ("vid_fullscreen") == 0)
  346. {
  347. IN_DeactivateMouse ();
  348. return;
  349. }
  350. }
  351. IN_ActivateMouse ();
  352. }
  353. /*
  354. ===========
  355. IN_Move
  356. ===========
  357. */
  358. void IN_Move (usercmd_t *cmd)
  359. {
  360. IN_MouseMove (cmd);
  361. if (ActiveApp)
  362. IN_JoyMove (cmd);
  363. }
  364. /*
  365. ===================
  366. IN_ClearStates
  367. ===================
  368. */
  369. void IN_ClearStates (void)
  370. {
  371. mx_accum = 0;
  372. my_accum = 0;
  373. mouse_oldbuttonstate = 0;
  374. }
  375. /*
  376. =========================================================================
  377. JOYSTICK
  378. =========================================================================
  379. */
  380. /*
  381. ===============
  382. IN_StartupJoystick
  383. ===============
  384. */
  385. void IN_StartupJoystick (void)
  386. {
  387. int numdevs;
  388. JOYCAPS jc;
  389. MMRESULT mmr;
  390. cvar_t *cv;
  391. // assume no joystick
  392. joy_avail = false;
  393. // abort startup if user requests no joystick
  394. cv = Cvar_Get ("in_initjoy", "1", CVAR_NOSET);
  395. if ( !cv->value )
  396. return;
  397. // verify joystick driver is present
  398. if ((numdevs = joyGetNumDevs ()) == 0)
  399. {
  400. // Com_Printf ("\njoystick not found -- driver not present\n\n");
  401. return;
  402. }
  403. // cycle through the joystick ids for the first valid one
  404. for (joy_id=0 ; joy_id<numdevs ; joy_id++)
  405. {
  406. memset (&ji, 0, sizeof(ji));
  407. ji.dwSize = sizeof(ji);
  408. ji.dwFlags = JOY_RETURNCENTERED;
  409. if ((mmr = joyGetPosEx (joy_id, &ji)) == JOYERR_NOERROR)
  410. break;
  411. }
  412. // abort startup if we didn't find a valid joystick
  413. if (mmr != JOYERR_NOERROR)
  414. {
  415. Com_Printf ("\njoystick not found -- no valid joysticks (%x)\n\n", mmr);
  416. return;
  417. }
  418. // get the capabilities of the selected joystick
  419. // abort startup if command fails
  420. memset (&jc, 0, sizeof(jc));
  421. if ((mmr = joyGetDevCaps (joy_id, &jc, sizeof(jc))) != JOYERR_NOERROR)
  422. {
  423. Com_Printf ("\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr);
  424. return;
  425. }
  426. // save the joystick's number of buttons and POV status
  427. joy_numbuttons = jc.wNumButtons;
  428. joy_haspov = jc.wCaps & JOYCAPS_HASPOV;
  429. // old button and POV states default to no buttons pressed
  430. joy_oldbuttonstate = joy_oldpovstate = 0;
  431. // mark the joystick as available and advanced initialization not completed
  432. // this is needed as cvars are not available during initialization
  433. joy_avail = true;
  434. joy_advancedinit = false;
  435. Com_Printf ("\njoystick detected\n\n");
  436. }
  437. /*
  438. ===========
  439. RawValuePointer
  440. ===========
  441. */
  442. PDWORD RawValuePointer (int axis)
  443. {
  444. switch (axis)
  445. {
  446. case JOY_AXIS_X:
  447. return &ji.dwXpos;
  448. case JOY_AXIS_Y:
  449. return &ji.dwYpos;
  450. case JOY_AXIS_Z:
  451. return &ji.dwZpos;
  452. case JOY_AXIS_R:
  453. return &ji.dwRpos;
  454. case JOY_AXIS_U:
  455. return &ji.dwUpos;
  456. case JOY_AXIS_V:
  457. return &ji.dwVpos;
  458. }
  459. }
  460. /*
  461. ===========
  462. Joy_AdvancedUpdate_f
  463. ===========
  464. */
  465. void Joy_AdvancedUpdate_f (void)
  466. {
  467. // called once by IN_ReadJoystick and by user whenever an update is needed
  468. // cvars are now available
  469. int i;
  470. DWORD dwTemp;
  471. // initialize all the maps
  472. for (i = 0; i < JOY_MAX_AXES; i++)
  473. {
  474. dwAxisMap[i] = AxisNada;
  475. dwControlMap[i] = JOY_ABSOLUTE_AXIS;
  476. pdwRawValue[i] = RawValuePointer(i);
  477. }
  478. if( joy_advanced->value == 0.0)
  479. {
  480. // default joystick initialization
  481. // 2 axes only with joystick control
  482. dwAxisMap[JOY_AXIS_X] = AxisTurn;
  483. // dwControlMap[JOY_AXIS_X] = JOY_ABSOLUTE_AXIS;
  484. dwAxisMap[JOY_AXIS_Y] = AxisForward;
  485. // dwControlMap[JOY_AXIS_Y] = JOY_ABSOLUTE_AXIS;
  486. }
  487. else
  488. {
  489. if (strcmp (joy_name->string, "joystick") != 0)
  490. {
  491. // notify user of advanced controller
  492. Com_Printf ("\n%s configured\n\n", joy_name->string);
  493. }
  494. // advanced initialization here
  495. // data supplied by user via joy_axisn cvars
  496. dwTemp = (DWORD) joy_advaxisx->value;
  497. dwAxisMap[JOY_AXIS_X] = dwTemp & 0x0000000f;
  498. dwControlMap[JOY_AXIS_X] = dwTemp & JOY_RELATIVE_AXIS;
  499. dwTemp = (DWORD) joy_advaxisy->value;
  500. dwAxisMap[JOY_AXIS_Y] = dwTemp & 0x0000000f;
  501. dwControlMap[JOY_AXIS_Y] = dwTemp & JOY_RELATIVE_AXIS;
  502. dwTemp = (DWORD) joy_advaxisz->value;
  503. dwAxisMap[JOY_AXIS_Z] = dwTemp & 0x0000000f;
  504. dwControlMap[JOY_AXIS_Z] = dwTemp & JOY_RELATIVE_AXIS;
  505. dwTemp = (DWORD) joy_advaxisr->value;
  506. dwAxisMap[JOY_AXIS_R] = dwTemp & 0x0000000f;
  507. dwControlMap[JOY_AXIS_R] = dwTemp & JOY_RELATIVE_AXIS;
  508. dwTemp = (DWORD) joy_advaxisu->value;
  509. dwAxisMap[JOY_AXIS_U] = dwTemp & 0x0000000f;
  510. dwControlMap[JOY_AXIS_U] = dwTemp & JOY_RELATIVE_AXIS;
  511. dwTemp = (DWORD) joy_advaxisv->value;
  512. dwAxisMap[JOY_AXIS_V] = dwTemp & 0x0000000f;
  513. dwControlMap[JOY_AXIS_V] = dwTemp & JOY_RELATIVE_AXIS;
  514. }
  515. // compute the axes to collect from DirectInput
  516. joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
  517. for (i = 0; i < JOY_MAX_AXES; i++)
  518. {
  519. if (dwAxisMap[i] != AxisNada)
  520. {
  521. joy_flags |= dwAxisFlags[i];
  522. }
  523. }
  524. }
  525. /*
  526. ===========
  527. IN_Commands
  528. ===========
  529. */
  530. void IN_Commands (void)
  531. {
  532. int i, key_index;
  533. DWORD buttonstate, povstate;
  534. if (!joy_avail)
  535. {
  536. return;
  537. }
  538. // loop through the joystick buttons
  539. // key a joystick event or auxillary event for higher number buttons for each state change
  540. buttonstate = ji.dwButtons;
  541. for (i=0 ; i < joy_numbuttons ; i++)
  542. {
  543. if ( (buttonstate & (1<<i)) && !(joy_oldbuttonstate & (1<<i)) )
  544. {
  545. key_index = (i < 4) ? K_JOY1 : K_AUX1;
  546. Key_Event (key_index + i, true, 0);
  547. }
  548. if ( !(buttonstate & (1<<i)) && (joy_oldbuttonstate & (1<<i)) )
  549. {
  550. key_index = (i < 4) ? K_JOY1 : K_AUX1;
  551. Key_Event (key_index + i, false, 0);
  552. }
  553. }
  554. joy_oldbuttonstate = buttonstate;
  555. if (joy_haspov)
  556. {
  557. // convert POV information into 4 bits of state information
  558. // this avoids any potential problems related to moving from one
  559. // direction to another without going through the center position
  560. povstate = 0;
  561. if(ji.dwPOV != JOY_POVCENTERED)
  562. {
  563. if (ji.dwPOV == JOY_POVFORWARD)
  564. povstate |= 0x01;
  565. if (ji.dwPOV == JOY_POVRIGHT)
  566. povstate |= 0x02;
  567. if (ji.dwPOV == JOY_POVBACKWARD)
  568. povstate |= 0x04;
  569. if (ji.dwPOV == JOY_POVLEFT)
  570. povstate |= 0x08;
  571. }
  572. // determine which bits have changed and key an auxillary event for each change
  573. for (i=0 ; i < 4 ; i++)
  574. {
  575. if ( (povstate & (1<<i)) && !(joy_oldpovstate & (1<<i)) )
  576. {
  577. Key_Event (K_AUX29 + i, true, 0);
  578. }
  579. if ( !(povstate & (1<<i)) && (joy_oldpovstate & (1<<i)) )
  580. {
  581. Key_Event (K_AUX29 + i, false, 0);
  582. }
  583. }
  584. joy_oldpovstate = povstate;
  585. }
  586. }
  587. /*
  588. ===============
  589. IN_ReadJoystick
  590. ===============
  591. */
  592. qboolean IN_ReadJoystick (void)
  593. {
  594. memset (&ji, 0, sizeof(ji));
  595. ji.dwSize = sizeof(ji);
  596. ji.dwFlags = joy_flags;
  597. if (joyGetPosEx (joy_id, &ji) == JOYERR_NOERROR)
  598. {
  599. return true;
  600. }
  601. else
  602. {
  603. // read error occurred
  604. // turning off the joystick seems too harsh for 1 read error,\
  605. // but what should be done?
  606. // Com_Printf ("IN_ReadJoystick: no response\n");
  607. // joy_avail = false;
  608. return false;
  609. }
  610. }
  611. /*
  612. ===========
  613. IN_JoyMove
  614. ===========
  615. */
  616. void IN_JoyMove (usercmd_t *cmd)
  617. {
  618. float speed, aspeed;
  619. float fAxisValue;
  620. int i;
  621. // complete initialization if first time in
  622. // this is needed as cvars are not available at initialization time
  623. if( joy_advancedinit != true )
  624. {
  625. Joy_AdvancedUpdate_f();
  626. joy_advancedinit = true;
  627. }
  628. // verify joystick is available and that the user wants to use it
  629. if (!joy_avail || !in_joystick->value)
  630. {
  631. return;
  632. }
  633. // collect the joystick data, if possible
  634. if (IN_ReadJoystick () != true)
  635. {
  636. return;
  637. }
  638. if ( (in_speed.state & 1) ^ (int)cl_run->value)
  639. speed = 2;
  640. else
  641. speed = 1;
  642. aspeed = speed * cls.frametime;
  643. // loop through the axes
  644. for (i = 0; i < JOY_MAX_AXES; i++)
  645. {
  646. // get the floating point zero-centered, potentially-inverted data for the current axis
  647. fAxisValue = (float) *pdwRawValue[i];
  648. // move centerpoint to zero
  649. fAxisValue -= 32768.0;
  650. // convert range from -32768..32767 to -1..1
  651. fAxisValue /= 32768.0;
  652. switch (dwAxisMap[i])
  653. {
  654. case AxisForward:
  655. if ((joy_advanced->value == 0.0) && mlooking)
  656. {
  657. // user wants forward control to become look control
  658. if (fabs(fAxisValue) > joy_pitchthreshold->value)
  659. {
  660. // if mouse invert is on, invert the joystick pitch value
  661. // only absolute control support here (joy_advanced is false)
  662. if (m_pitch->value < 0.0)
  663. {
  664. cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity->value) * aspeed * cl_pitchspeed->value;
  665. }
  666. else
  667. {
  668. cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity->value) * aspeed * cl_pitchspeed->value;
  669. }
  670. }
  671. }
  672. else
  673. {
  674. // user wants forward control to be forward control
  675. if (fabs(fAxisValue) > joy_forwardthreshold->value)
  676. {
  677. cmd->forwardmove += (fAxisValue * joy_forwardsensitivity->value) * speed * cl_forwardspeed->value;
  678. }
  679. }
  680. break;
  681. case AxisSide:
  682. if (fabs(fAxisValue) > joy_sidethreshold->value)
  683. {
  684. cmd->sidemove += (fAxisValue * joy_sidesensitivity->value) * speed * cl_sidespeed->value;
  685. }
  686. break;
  687. case AxisUp:
  688. if (fabs(fAxisValue) > joy_upthreshold->value)
  689. {
  690. cmd->upmove += (fAxisValue * joy_upsensitivity->value) * speed * cl_upspeed->value;
  691. }
  692. break;
  693. case AxisTurn:
  694. if ((in_strafe.state & 1) || (lookstrafe->value && mlooking))
  695. {
  696. // user wants turn control to become side control
  697. if (fabs(fAxisValue) > joy_sidethreshold->value)
  698. {
  699. cmd->sidemove -= (fAxisValue * joy_sidesensitivity->value) * speed * cl_sidespeed->value;
  700. }
  701. }
  702. else
  703. {
  704. // user wants turn control to be turn control
  705. if (fabs(fAxisValue) > joy_yawthreshold->value)
  706. {
  707. if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
  708. {
  709. cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity->value) * aspeed * cl_yawspeed->value;
  710. }
  711. else
  712. {
  713. cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity->value) * speed * 180.0;
  714. }
  715. }
  716. }
  717. break;
  718. case AxisLook:
  719. if (mlooking)
  720. {
  721. if (fabs(fAxisValue) > joy_pitchthreshold->value)
  722. {
  723. // pitch movement detected and pitch movement desired by user
  724. if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
  725. {
  726. cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity->value) * aspeed * cl_pitchspeed->value;
  727. }
  728. else
  729. {
  730. cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity->value) * speed * 180.0;
  731. }
  732. }
  733. }
  734. break;
  735. default:
  736. break;
  737. }
  738. }
  739. }