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