]> icculus.org git repositories - divverent/darkplaces.git/blob - in_win.c
move dynamic lights up 30 units for the client player when firstperson (so the gun...
[divverent/darkplaces.git] / in_win.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // in_win.c -- windows 95 mouse and joystick code
21 // 02/21/97 JCB Added extended DirectInput code to support external controllers.
22
23 #include "quakedef.h"
24 #include "winquake.h"
25 //#include "dosisms.h"
26
27 #include <dinput.h>
28
29 #define DINPUT_BUFFERSIZE           16
30 #define iDirectInputCreate(a,b,c,d)     pDirectInputCreate(a,b,c,d)
31
32 HRESULT (WINAPI *pDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion,
33         LPDIRECTINPUT * lplpDirectInput, LPUNKNOWN punkOuter);
34
35 // mouse variables
36 cvar_t  m_filter = {"m_filter","0"};
37
38 int                     mouse_buttons;
39 int                     mouse_oldbuttonstate;
40 POINT           current_pos;
41 int                     mouse_x, mouse_y, old_mouse_x, old_mouse_y, mx_accum, my_accum;
42
43 static qboolean restore_spi;
44 static int              originalmouseparms[3], newmouseparms[3] = {0, 0, 1};
45
46 unsigned int uiWheelMessage;
47 qboolean        mouseactive;
48 qboolean                mouseinitialized;
49 static qboolean mouseparmsvalid, mouseactivatetoggle;
50 static qboolean mouseshowtoggle = 1;
51 static qboolean dinput_acquired;
52
53 static unsigned int             mstate_di;
54
55 // joystick defines and variables
56 // where should defines be moved?
57 #define JOY_ABSOLUTE_AXIS       0x00000000              // control like a joystick
58 #define JOY_RELATIVE_AXIS       0x00000010              // control like a mouse, spinner, trackball
59 #define JOY_MAX_AXES            6                               // X, Y, Z, R, U, V
60 #define JOY_AXIS_X                      0
61 #define JOY_AXIS_Y                      1
62 #define JOY_AXIS_Z                      2
63 #define JOY_AXIS_R                      3
64 #define JOY_AXIS_U                      4
65 #define JOY_AXIS_V                      5
66
67 enum _ControlList
68 {
69         AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn
70 };
71
72 DWORD   dwAxisFlags[JOY_MAX_AXES] =
73 {
74         JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
75 };
76
77 DWORD   dwAxisMap[JOY_MAX_AXES];
78 DWORD   dwControlMap[JOY_MAX_AXES];
79 PDWORD  pdwRawValue[JOY_MAX_AXES];
80
81 // none of these cvars are saved over a session
82 // this means that advanced controller configuration needs to be executed
83 // each time.  this avoids any problems with getting back to a default usage
84 // or when changing from one controller to another.  this way at least something
85 // works.
86 cvar_t  in_joystick = {"joystick","0", true};
87 cvar_t  joy_name = {"joyname", "joystick"};
88 cvar_t  joy_advanced = {"joyadvanced", "0"};
89 cvar_t  joy_advaxisx = {"joyadvaxisx", "0"};
90 cvar_t  joy_advaxisy = {"joyadvaxisy", "0"};
91 cvar_t  joy_advaxisz = {"joyadvaxisz", "0"};
92 cvar_t  joy_advaxisr = {"joyadvaxisr", "0"};
93 cvar_t  joy_advaxisu = {"joyadvaxisu", "0"};
94 cvar_t  joy_advaxisv = {"joyadvaxisv", "0"};
95 cvar_t  joy_forwardthreshold = {"joyforwardthreshold", "0.15"};
96 cvar_t  joy_sidethreshold = {"joysidethreshold", "0.15"};
97 cvar_t  joy_pitchthreshold = {"joypitchthreshold", "0.15"};
98 cvar_t  joy_yawthreshold = {"joyyawthreshold", "0.15"};
99 cvar_t  joy_forwardsensitivity = {"joyforwardsensitivity", "-1.0"};
100 cvar_t  joy_sidesensitivity = {"joysidesensitivity", "-1.0"};
101 cvar_t  joy_pitchsensitivity = {"joypitchsensitivity", "1.0"};
102 cvar_t  joy_yawsensitivity = {"joyyawsensitivity", "-1.0"};
103 cvar_t  joy_wwhack1 = {"joywwhack1", "0.0"};
104 cvar_t  joy_wwhack2 = {"joywwhack2", "0.0"};
105
106 qboolean        joy_avail, joy_advancedinit, joy_haspov;
107 DWORD           joy_oldbuttonstate, joy_oldpovstate;
108
109 int                     joy_id;
110 DWORD           joy_flags;
111 DWORD           joy_numbuttons;
112
113 static LPDIRECTINPUT            g_pdi;
114 static LPDIRECTINPUTDEVICE      g_pMouse;
115
116 static JOYINFOEX        ji;
117
118 static HINSTANCE hInstDI;
119
120 static qboolean dinput;
121
122 typedef struct MYDATA {
123         LONG  lX;                   // X axis goes here
124         LONG  lY;                   // Y axis goes here
125         LONG  lZ;                   // Z axis goes here
126         BYTE  bButtonA;             // One button goes here
127         BYTE  bButtonB;             // Another button goes here
128         BYTE  bButtonC;             // Another button goes here
129         BYTE  bButtonD;             // Another button goes here
130 } MYDATA;
131
132 static DIOBJECTDATAFORMAT rgodf[] = {
133   { &GUID_XAxis,    FIELD_OFFSET(MYDATA, lX),       DIDFT_AXIS | DIDFT_ANYINSTANCE,   0,},
134   { &GUID_YAxis,    FIELD_OFFSET(MYDATA, lY),       DIDFT_AXIS | DIDFT_ANYINSTANCE,   0,},
135   { &GUID_ZAxis,    FIELD_OFFSET(MYDATA, lZ),       0x80000000 | DIDFT_AXIS | DIDFT_ANYINSTANCE,   0,},
136   { 0,              FIELD_OFFSET(MYDATA, bButtonA), DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
137   { 0,              FIELD_OFFSET(MYDATA, bButtonB), DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
138   { 0,              FIELD_OFFSET(MYDATA, bButtonC), 0x80000000 | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
139   { 0,              FIELD_OFFSET(MYDATA, bButtonD), 0x80000000 | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
140 };
141
142 #define NUM_OBJECTS (sizeof(rgodf) / sizeof(rgodf[0]))
143
144 static DIDATAFORMAT     df = {
145         sizeof(DIDATAFORMAT),       // this structure
146         sizeof(DIOBJECTDATAFORMAT), // size of object data format
147         DIDF_RELAXIS,               // absolute axis coordinates
148         sizeof(MYDATA),             // device data size
149         NUM_OBJECTS,                // number of objects
150         rgodf,                      // and here they are
151 };
152
153 // forward-referenced functions
154 void IN_StartupJoystick (void);
155 void Joy_AdvancedUpdate_f (void);
156 void IN_JoyMove (usercmd_t *cmd);
157
158
159 /*
160 ===========
161 Force_CenterView_f
162 ===========
163 */
164 void Force_CenterView_f (void)
165 {
166         cl.viewangles[PITCH] = 0;
167 }
168
169
170 /*
171 ===========
172 IN_UpdateClipCursor
173 ===========
174 */
175 void IN_UpdateClipCursor (void)
176 {
177
178         if (mouseinitialized && mouseactive && !dinput)
179         {
180                 ClipCursor (&window_rect);
181         }
182 }
183
184
185 /*
186 ===========
187 IN_ShowMouse
188 ===========
189 */
190 void IN_ShowMouse (void)
191 {
192         if (!mouseshowtoggle)
193         {
194                 ShowCursor (true);
195                 mouseshowtoggle = 1;
196         }
197 }
198
199
200 /*
201 ===========
202 IN_HideMouse
203 ===========
204 */
205 void IN_HideMouse (void)
206 {
207         if (mouseshowtoggle)
208         {
209                 ShowCursor (false);
210                 mouseshowtoggle = 0;
211         }
212 }
213
214
215 /*
216 ===========
217 IN_ActivateMouse
218 ===========
219 */
220 void IN_ActivateMouse (void)
221 {
222
223         mouseactivatetoggle = true;
224
225         if (mouseinitialized)
226         {
227                 if (dinput)
228                 {
229                         if (g_pMouse)
230                         {
231                                 if (!dinput_acquired)
232                                 {
233                                         IDirectInputDevice_Acquire(g_pMouse);
234                                         dinput_acquired = true;
235                                 }
236                         }
237                         else
238                         {
239                                 return;
240                         }
241                 }
242                 else
243                 {
244                         if (mouseparmsvalid)
245                                 restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0);
246
247                         SetCursorPos (window_center_x, window_center_y);
248                         SetCapture (mainwindow);
249                         ClipCursor (&window_rect);
250                         
251                 }
252
253                 mouseactive = true;
254         }
255 }
256
257
258 /*
259 ===========
260 IN_DeactivateMouse
261 ===========
262 */
263 void IN_DeactivateMouse (void)
264 {
265
266         mouseactivatetoggle = false;
267
268         if (mouseinitialized)
269         {
270                 if (dinput)
271                 {
272                         if (g_pMouse)
273                         {
274                                 if (dinput_acquired)
275                                 {
276                                         IDirectInputDevice_Unacquire(g_pMouse);
277                                         dinput_acquired = false;
278                                 }
279                         }
280                 }
281                 else
282                 {
283                         if (restore_spi)
284                                 SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0);
285
286                         ClipCursor (NULL);
287                         ReleaseCapture ();
288                 }
289
290                 mouseactive = false;
291         }
292 }
293
294
295 /*
296 ===========
297 IN_InitDInput
298 ===========
299 */
300 qboolean IN_InitDInput (void)
301 {
302     HRESULT             hr;
303         DIPROPDWORD     dipdw = {
304                 {
305                         sizeof(DIPROPDWORD),        // diph.dwSize
306                         sizeof(DIPROPHEADER),       // diph.dwHeaderSize
307                         0,                          // diph.dwObj
308                         DIPH_DEVICE,                // diph.dwHow
309                 },
310                 DINPUT_BUFFERSIZE,              // dwData
311         };
312
313         if (!hInstDI)
314         {
315                 hInstDI = LoadLibrary("dinput.dll");
316                 
317                 if (hInstDI == NULL)
318                 {
319                         Con_SafePrintf ("Couldn't load dinput.dll\n");
320                         return false;
321                 }
322         }
323
324         if (!pDirectInputCreate)
325         {
326                 pDirectInputCreate = (void *)GetProcAddress(hInstDI,"DirectInputCreateA");
327
328                 if (!pDirectInputCreate)
329                 {
330                         Con_SafePrintf ("Couldn't get DI proc addr\n");
331                         return false;
332                 }
333         }
334
335 // register with DirectInput and get an IDirectInput to play with.
336         hr = iDirectInputCreate(global_hInstance, DIRECTINPUT_VERSION, &g_pdi, NULL);
337
338         if (FAILED(hr))
339         {
340                 return false;
341         }
342
343 // obtain an interface to the system mouse device.
344         hr = IDirectInput_CreateDevice(g_pdi, &GUID_SysMouse, &g_pMouse, NULL);
345
346         if (FAILED(hr))
347         {
348                 Con_SafePrintf ("Couldn't open DI mouse device\n");
349                 return false;
350         }
351
352 // set the data format to "mouse format".
353         hr = IDirectInputDevice_SetDataFormat(g_pMouse, &df);
354
355         if (FAILED(hr))
356         {
357                 Con_SafePrintf ("Couldn't set DI mouse format\n");
358                 return false;
359         }
360
361 // set the cooperativity level.
362         hr = IDirectInputDevice_SetCooperativeLevel(g_pMouse, mainwindow,
363                         DISCL_EXCLUSIVE | DISCL_FOREGROUND);
364
365         if (FAILED(hr))
366         {
367                 Con_SafePrintf ("Couldn't set DI coop level\n");
368                 return false;
369         }
370
371
372 // set the buffer size to DINPUT_BUFFERSIZE elements.
373 // the buffer size is a DWORD property associated with the device
374         hr = IDirectInputDevice_SetProperty(g_pMouse, DIPROP_BUFFERSIZE, &dipdw.diph);
375
376         if (FAILED(hr))
377         {
378                 Con_SafePrintf ("Couldn't set DI buffersize\n");
379                 return false;
380         }
381
382         return true;
383 }
384
385
386 /*
387 ===========
388 IN_StartupMouse
389 ===========
390 */
391 void IN_StartupMouse (void)
392 {
393         if ( COM_CheckParm ("-nomouse") ) 
394                 return; 
395
396         mouseinitialized = true;
397
398         if (COM_CheckParm ("-dinput"))
399         {
400                 dinput = IN_InitDInput ();
401
402                 if (dinput)
403                 {
404                         Con_SafePrintf ("DirectInput initialized\n");
405                 }
406                 else
407                 {
408                         Con_SafePrintf ("DirectInput not initialized\n");
409                 }
410         }
411
412         if (!dinput)
413         {
414                 mouseparmsvalid = SystemParametersInfo (SPI_GETMOUSE, 0, originalmouseparms, 0);
415
416                 if (mouseparmsvalid)
417                 {
418                         if ( COM_CheckParm ("-noforcemspd") ) 
419                                 newmouseparms[2] = originalmouseparms[2];
420
421                         if ( COM_CheckParm ("-noforcemaccel") ) 
422                         {
423                                 newmouseparms[0] = originalmouseparms[0];
424                                 newmouseparms[1] = originalmouseparms[1];
425                         }
426
427                         if ( COM_CheckParm ("-noforcemparms") ) 
428                         {
429                                 newmouseparms[0] = originalmouseparms[0];
430                                 newmouseparms[1] = originalmouseparms[1];
431                                 newmouseparms[2] = originalmouseparms[2];
432                         }
433                 }
434         }
435
436         mouse_buttons = 3;
437
438 // if a fullscreen video mode was set before the mouse was initialized,
439 // set the mouse state appropriately
440         if (mouseactivatetoggle)
441                 IN_ActivateMouse ();
442 }
443
444
445 /*
446 ===========
447 IN_Init
448 ===========
449 */
450 void IN_Init (void)
451 {
452         // mouse variables
453         Cvar_RegisterVariable (&m_filter);
454
455         // joystick variables
456         Cvar_RegisterVariable (&in_joystick);
457         Cvar_RegisterVariable (&joy_name);
458         Cvar_RegisterVariable (&joy_advanced);
459         Cvar_RegisterVariable (&joy_advaxisx);
460         Cvar_RegisterVariable (&joy_advaxisy);
461         Cvar_RegisterVariable (&joy_advaxisz);
462         Cvar_RegisterVariable (&joy_advaxisr);
463         Cvar_RegisterVariable (&joy_advaxisu);
464         Cvar_RegisterVariable (&joy_advaxisv);
465         Cvar_RegisterVariable (&joy_forwardthreshold);
466         Cvar_RegisterVariable (&joy_sidethreshold);
467         Cvar_RegisterVariable (&joy_pitchthreshold);
468         Cvar_RegisterVariable (&joy_yawthreshold);
469         Cvar_RegisterVariable (&joy_forwardsensitivity);
470         Cvar_RegisterVariable (&joy_sidesensitivity);
471         Cvar_RegisterVariable (&joy_pitchsensitivity);
472         Cvar_RegisterVariable (&joy_yawsensitivity);
473         Cvar_RegisterVariable (&joy_wwhack1);
474         Cvar_RegisterVariable (&joy_wwhack2);
475
476         Cmd_AddCommand ("force_centerview", Force_CenterView_f);
477         Cmd_AddCommand ("joyadvancedupdate", Joy_AdvancedUpdate_f);
478
479         uiWheelMessage = RegisterWindowMessage ( "MSWHEEL_ROLLMSG" );
480
481         IN_StartupMouse ();
482         IN_StartupJoystick ();
483 }
484
485 /*
486 ===========
487 IN_Shutdown
488 ===========
489 */
490 void IN_Shutdown (void)
491 {
492 //      usingmouse = false;
493         IN_DeactivateMouse ();
494         IN_ShowMouse ();
495
496     if (g_pMouse)
497         {
498                 IDirectInputDevice_Release(g_pMouse);
499                 g_pMouse = NULL;
500         }
501
502     if (g_pdi)
503         {
504                 IDirectInput_Release(g_pdi);
505                 g_pdi = NULL;
506         }
507 }
508
509
510 /*
511 ===========
512 IN_MouseEvent
513 ===========
514 */
515 void IN_MouseEvent (int mstate)
516 {
517         int     i;
518
519         if (mouseactive && !dinput)
520         {
521         // perform button actions
522                 for (i=0 ; i<mouse_buttons ; i++)
523                 {
524                         if ( (mstate & (1<<i)) &&
525                                 !(mouse_oldbuttonstate & (1<<i)) )
526                         {
527                                 Key_Event (K_MOUSE1 + i, true);
528                         }
529
530                         if ( !(mstate & (1<<i)) &&
531                                 (mouse_oldbuttonstate & (1<<i)) )
532                         {
533                                 Key_Event (K_MOUSE1 + i, false);
534                         }
535                 }       
536                         
537                 mouse_oldbuttonstate = mstate;
538         }
539 }
540
541
542 /*
543 ===========
544 IN_MouseMove
545 ===========
546 */
547 void IN_MouseMove (usercmd_t *cmd)
548 {
549         int                                     i, mx, my, mouselook = (in_mlook.state & 1) || freelook.value;
550         DIDEVICEOBJECTDATA      od;
551         DWORD                           dwElements;
552         HRESULT                         hr;
553
554         if (!mouseactive)
555                 return;
556
557         if (dinput)
558         {
559                 mx = 0;
560                 my = 0;
561
562                 for (;;)
563                 {
564                         dwElements = 1;
565
566                         hr = IDirectInputDevice_GetDeviceData(g_pMouse,
567                                         sizeof(DIDEVICEOBJECTDATA), &od, &dwElements, 0);
568
569                         if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED))
570                         {
571                                 dinput_acquired = true;
572                                 IDirectInputDevice_Acquire(g_pMouse);
573                                 break;
574                         }
575
576                         /* Unable to read data or no data available */
577                         if (FAILED(hr) || dwElements == 0)
578                         {
579                                 break;
580                         }
581
582                         /* Look at the element to see what happened */
583
584                         switch (od.dwOfs)
585                         {
586                                 case DIMOFS_X:
587                                         mx += od.dwData;
588                                         break;
589
590                                 case DIMOFS_Y:
591                                         my += od.dwData;
592                                         break;
593
594                                 case DIMOFS_BUTTON0:
595                                         if (od.dwData & 0x80)
596                                                 mstate_di |= 1;
597                                         else
598                                                 mstate_di &= ~1;
599                                         break;
600
601                                 case DIMOFS_BUTTON1:
602                                         if (od.dwData & 0x80)
603                                                 mstate_di |= (1<<1);
604                                         else
605                                                 mstate_di &= ~(1<<1);
606                                         break;
607                                         
608                                 case DIMOFS_BUTTON2:
609                                         if (od.dwData & 0x80)
610                                                 mstate_di |= (1<<2);
611                                         else
612                                                 mstate_di &= ~(1<<2);
613                                         break;
614                         }
615                 }
616
617         // perform button actions
618                 for (i=0 ; i<mouse_buttons ; i++)
619                 {
620                         if ( (mstate_di & (1<<i)) &&
621                                 !(mouse_oldbuttonstate & (1<<i)) )
622                         {
623                                 Key_Event (K_MOUSE1 + i, true);
624                         }
625
626                         if ( !(mstate_di & (1<<i)) &&
627                                 (mouse_oldbuttonstate & (1<<i)) )
628                         {
629                                 Key_Event (K_MOUSE1 + i, false);
630                         }
631                 }       
632                         
633                 mouse_oldbuttonstate = mstate_di;
634         }
635         else
636         {
637                 GetCursorPos (&current_pos);
638                 mx = current_pos.x - window_center_x + mx_accum;
639                 my = current_pos.y - window_center_y + my_accum;
640                 mx_accum = 0;
641                 my_accum = 0;
642         }
643
644 //if (mx ||  my)
645 //      Con_DPrintf("mx=%d, my=%d\n", mx, my);
646
647         if (m_filter.value)
648         {
649                 mouse_x = (mx + old_mouse_x) * 0.5;
650                 mouse_y = (my + old_mouse_y) * 0.5;
651         }
652         else
653         {
654                 mouse_x = mx;
655                 mouse_y = my;
656         }
657
658         old_mouse_x = mx;
659         old_mouse_y = my;
660
661         mouse_x *= sensitivity.value;
662         mouse_y *= sensitivity.value;
663
664 // add mouse X/Y movement to cmd
665         if ( (in_strafe.state & 1) || (lookstrafe.value && mouselook))
666                 cmd->sidemove += m_side.value * mouse_x;
667         else
668                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
669
670         if (mouselook)
671                 V_StopPitchDrift ();
672         
673         // LordHavoc: changed limits on pitch from -70 to 80, to -90 to 90
674         if (mouselook && !(in_strafe.state & 1))
675         {
676                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
677                 if (cl.viewangles[PITCH] > 90)
678                         cl.viewangles[PITCH] = 90;
679                 if (cl.viewangles[PITCH] < -90)
680                         cl.viewangles[PITCH] = -90;
681         }
682         else
683         {
684                 if ((in_strafe.state & 1) && noclip_anglehack)
685                         cmd->upmove -= m_forward.value * mouse_y;
686                 else
687                         cmd->forwardmove -= m_forward.value * mouse_y;
688         }
689
690 // if the mouse has moved, force it to the center, so there's room to move
691         if (mx || my)
692         {
693                 SetCursorPos (window_center_x, window_center_y);
694         }
695 }
696
697
698 /*
699 ===========
700 IN_Move
701 ===========
702 */
703 void IN_Move (usercmd_t *cmd)
704 {
705
706         if (ActiveApp && !Minimized)
707         {
708                 IN_MouseMove (cmd);
709                 IN_JoyMove (cmd);
710         }
711 }
712
713
714 /*
715 ===========
716 IN_Accumulate
717 ===========
718 */
719 void IN_Accumulate (void)
720 {
721         if (mouseactive)
722         {
723                 if (!dinput)
724                 {
725                         GetCursorPos (&current_pos);
726
727                         mx_accum += current_pos.x - window_center_x;
728                         my_accum += current_pos.y - window_center_y;
729
730                 // force the mouse to the center, so there's room to move
731                         SetCursorPos (window_center_x, window_center_y);
732                 }
733         }
734 }
735
736
737 /*
738 ===================
739 IN_ClearStates
740 ===================
741 */
742 void IN_ClearStates (void)
743 {
744
745         if (mouseactive)
746         {
747                 mx_accum = 0;
748                 my_accum = 0;
749                 mouse_oldbuttonstate = 0;
750         }
751 }
752
753
754 /* 
755 =============== 
756 IN_StartupJoystick 
757 =============== 
758 */  
759 void IN_StartupJoystick (void) 
760
761         int                     numdevs;
762         JOYCAPS         jc;
763         MMRESULT        mmr;
764  
765         // assume no joystick
766         joy_avail = false; 
767
768         // abort startup if user requests no joystick
769         if ( COM_CheckParm ("-nojoy") ) 
770                 return; 
771  
772         // verify joystick driver is present
773         if ((numdevs = joyGetNumDevs ()) == 0)
774         {
775                 Con_Printf ("\njoystick not found -- driver not present\n\n");
776                 return;
777         }
778
779         // cycle through the joystick ids for the first valid one
780         for (joy_id=0 ; joy_id<numdevs ; joy_id++)
781         {
782                 memset (&ji, 0, sizeof(ji));
783                 ji.dwSize = sizeof(ji);
784                 ji.dwFlags = JOY_RETURNCENTERED;
785
786                 if ((mmr = joyGetPosEx (joy_id, &ji)) == JOYERR_NOERROR)
787                         break;
788         } 
789
790         // abort startup if we didn't find a valid joystick
791         if (mmr != JOYERR_NOERROR)
792         {
793                 Con_Printf ("\njoystick not found -- no valid joysticks (%x)\n\n", mmr);
794                 return;
795         }
796
797         // get the capabilities of the selected joystick
798         // abort startup if command fails
799         memset (&jc, 0, sizeof(jc));
800         if ((mmr = joyGetDevCaps (joy_id, &jc, sizeof(jc))) != JOYERR_NOERROR)
801         {
802                 Con_Printf ("\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr); 
803                 return;
804         }
805
806         // save the joystick's number of buttons and POV status
807         joy_numbuttons = jc.wNumButtons;
808         joy_haspov = jc.wCaps & JOYCAPS_HASPOV;
809
810         // old button and POV states default to no buttons pressed
811         joy_oldbuttonstate = joy_oldpovstate = 0;
812
813         // mark the joystick as available and advanced initialization not completed
814         // this is needed as cvars are not available during initialization
815
816         joy_avail = true; 
817         joy_advancedinit = false;
818
819         Con_Printf ("\njoystick detected\n\n"); 
820 }
821
822
823 /*
824 ===========
825 RawValuePointer
826 ===========
827 */
828 PDWORD RawValuePointer (int axis)
829 {
830         switch (axis)
831         {
832         case JOY_AXIS_X:
833                 return &ji.dwXpos;
834         case JOY_AXIS_Y:
835                 return &ji.dwYpos;
836         case JOY_AXIS_Z:
837                 return &ji.dwZpos;
838         case JOY_AXIS_R:
839                 return &ji.dwRpos;
840         case JOY_AXIS_U:
841                 return &ji.dwUpos;
842         case JOY_AXIS_V:
843                 return &ji.dwVpos;
844         }
845         return NULL; // LordHavoc: hush compiler warning
846 }
847
848
849 /*
850 ===========
851 Joy_AdvancedUpdate_f
852 ===========
853 */
854 void Joy_AdvancedUpdate_f (void)
855 {
856
857         // called once by IN_ReadJoystick and by user whenever an update is needed
858         // cvars are now available
859         int     i;
860         DWORD dwTemp;
861
862         // initialize all the maps
863         for (i = 0; i < JOY_MAX_AXES; i++)
864         {
865                 dwAxisMap[i] = AxisNada;
866                 dwControlMap[i] = JOY_ABSOLUTE_AXIS;
867                 pdwRawValue[i] = RawValuePointer(i);
868         }
869
870         if( joy_advanced.value == 0.0)
871         {
872                 // default joystick initialization
873                 // 2 axes only with joystick control
874                 dwAxisMap[JOY_AXIS_X] = AxisTurn;
875                 // dwControlMap[JOY_AXIS_X] = JOY_ABSOLUTE_AXIS;
876                 dwAxisMap[JOY_AXIS_Y] = AxisForward;
877                 // dwControlMap[JOY_AXIS_Y] = JOY_ABSOLUTE_AXIS;
878         }
879         else
880         {
881                 if (strcmp (joy_name.string, "joystick") != 0)
882                 {
883                         // notify user of advanced controller
884                         Con_Printf ("\n%s configured\n\n", joy_name.string);
885                 }
886
887                 // advanced initialization here
888                 // data supplied by user via joy_axisn cvars
889                 dwTemp = (DWORD) joy_advaxisx.value;
890                 dwAxisMap[JOY_AXIS_X] = dwTemp & 0x0000000f;
891                 dwControlMap[JOY_AXIS_X] = dwTemp & JOY_RELATIVE_AXIS;
892                 dwTemp = (DWORD) joy_advaxisy.value;
893                 dwAxisMap[JOY_AXIS_Y] = dwTemp & 0x0000000f;
894                 dwControlMap[JOY_AXIS_Y] = dwTemp & JOY_RELATIVE_AXIS;
895                 dwTemp = (DWORD) joy_advaxisz.value;
896                 dwAxisMap[JOY_AXIS_Z] = dwTemp & 0x0000000f;
897                 dwControlMap[JOY_AXIS_Z] = dwTemp & JOY_RELATIVE_AXIS;
898                 dwTemp = (DWORD) joy_advaxisr.value;
899                 dwAxisMap[JOY_AXIS_R] = dwTemp & 0x0000000f;
900                 dwControlMap[JOY_AXIS_R] = dwTemp & JOY_RELATIVE_AXIS;
901                 dwTemp = (DWORD) joy_advaxisu.value;
902                 dwAxisMap[JOY_AXIS_U] = dwTemp & 0x0000000f;
903                 dwControlMap[JOY_AXIS_U] = dwTemp & JOY_RELATIVE_AXIS;
904                 dwTemp = (DWORD) joy_advaxisv.value;
905                 dwAxisMap[JOY_AXIS_V] = dwTemp & 0x0000000f;
906                 dwControlMap[JOY_AXIS_V] = dwTemp & JOY_RELATIVE_AXIS;
907         }
908
909         // compute the axes to collect from DirectInput
910         joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
911         for (i = 0; i < JOY_MAX_AXES; i++)
912         {
913                 if (dwAxisMap[i] != AxisNada)
914                 {
915                         joy_flags |= dwAxisFlags[i];
916                 }
917         }
918 }
919
920
921 /*
922 ===========
923 IN_Commands
924 ===========
925 */
926 void IN_Commands (void)
927 {
928         int             i, key_index;
929         DWORD   buttonstate, povstate;
930
931         if (!joy_avail)
932         {
933                 return;
934         }
935
936         
937         // loop through the joystick buttons
938         // key a joystick event or auxillary event for higher number buttons for each state change
939         buttonstate = ji.dwButtons;
940         for (i=0 ; i < (int) joy_numbuttons ; i++)
941         {
942                 if ( (buttonstate & (1<<i)) && !(joy_oldbuttonstate & (1<<i)) )
943                 {
944                         key_index = (i < 4) ? K_JOY1 : K_AUX1;
945                         Key_Event (key_index + i, true);
946                 }
947
948                 if ( !(buttonstate & (1<<i)) && (joy_oldbuttonstate & (1<<i)) )
949                 {
950                         key_index = (i < 4) ? K_JOY1 : K_AUX1;
951                         Key_Event (key_index + i, false);
952                 }
953         }
954         joy_oldbuttonstate = buttonstate;
955
956         if (joy_haspov)
957         {
958                 // convert POV information into 4 bits of state information
959                 // this avoids any potential problems related to moving from one
960                 // direction to another without going through the center position
961                 povstate = 0;
962                 if(ji.dwPOV != JOY_POVCENTERED)
963                 {
964                         if (ji.dwPOV == JOY_POVFORWARD)
965                                 povstate |= 0x01;
966                         if (ji.dwPOV == JOY_POVRIGHT)
967                                 povstate |= 0x02;
968                         if (ji.dwPOV == JOY_POVBACKWARD)
969                                 povstate |= 0x04;
970                         if (ji.dwPOV == JOY_POVLEFT)
971                                 povstate |= 0x08;
972                 }
973                 // determine which bits have changed and key an auxillary event for each change
974                 for (i=0 ; i < 4 ; i++)
975                 {
976                         if ( (povstate & (1<<i)) && !(joy_oldpovstate & (1<<i)) )
977                         {
978                                 Key_Event (K_AUX29 + i, true);
979                         }
980
981                         if ( !(povstate & (1<<i)) && (joy_oldpovstate & (1<<i)) )
982                         {
983                                 Key_Event (K_AUX29 + i, false);
984                         }
985                 }
986                 joy_oldpovstate = povstate;
987         }
988 }
989
990
991 /* 
992 =============== 
993 IN_ReadJoystick
994 =============== 
995 */  
996 qboolean IN_ReadJoystick (void)
997 {
998
999         memset (&ji, 0, sizeof(ji));
1000         ji.dwSize = sizeof(ji);
1001         ji.dwFlags = joy_flags;
1002
1003         if (joyGetPosEx (joy_id, &ji) == JOYERR_NOERROR)
1004         {
1005                 // this is a hack -- there is a bug in the Logitech WingMan Warrior DirectInput Driver
1006                 // rather than having 32768 be the zero point, they have the zero point at 32668
1007                 // go figure -- anyway, now we get the full resolution out of the device
1008                 if (joy_wwhack1.value != 0.0)
1009                 {
1010                         ji.dwUpos += 100;
1011                 }
1012                 return true;
1013         }
1014         else
1015         {
1016                 // read error occurred
1017                 // turning off the joystick seems too harsh for 1 read error,\
1018                 // but what should be done?
1019                 // Con_Printf ("IN_ReadJoystick: no response\n");
1020                 // joy_avail = false;
1021                 return false;
1022         }
1023 }
1024
1025
1026 /*
1027 ===========
1028 IN_JoyMove
1029 ===========
1030 */
1031 void IN_JoyMove (usercmd_t *cmd)
1032 {
1033         float   speed, aspeed;
1034         float   fAxisValue, fTemp;
1035         int             i, mouselook = (in_mlook.state & 1) || freelook.value;
1036
1037         // complete initialization if first time in
1038         // this is needed as cvars are not available at initialization time
1039         if( joy_advancedinit != true )
1040         {
1041                 Joy_AdvancedUpdate_f();
1042                 joy_advancedinit = true;
1043         }
1044
1045         // verify joystick is available and that the user wants to use it
1046         if (!joy_avail || !in_joystick.value)
1047         {
1048                 return; 
1049         }
1050  
1051         // collect the joystick data, if possible
1052         if (IN_ReadJoystick () != true)
1053         {
1054                 return;
1055         }
1056
1057         if (in_speed.state & 1)
1058                 speed = cl_movespeedkey.value;
1059         else
1060                 speed = 1;
1061         aspeed = speed * host_realframetime;
1062
1063         // loop through the axes
1064         for (i = 0; i < JOY_MAX_AXES; i++)
1065         {
1066                 // get the floating point zero-centered, potentially-inverted data for the current axis
1067                 fAxisValue = (float) *pdwRawValue[i];
1068                 // move centerpoint to zero
1069                 fAxisValue -= 32768.0;
1070
1071                 if (joy_wwhack2.value != 0.0)
1072                 {
1073                         if (dwAxisMap[i] == AxisTurn)
1074                         {
1075                                 // this is a special formula for the Logitech WingMan Warrior
1076                                 // y=ax^b; where a = 300 and b = 1.3
1077                                 // also x values are in increments of 800 (so this is factored out)
1078                                 // then bounds check result to level out excessively high spin rates
1079                                 fTemp = 300.0 * pow(abs(fAxisValue) / 800.0, 1.3);
1080                                 if (fTemp > 14000.0)
1081                                         fTemp = 14000.0;
1082                                 // restore direction information
1083                                 fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp;
1084                         }
1085                 }
1086
1087                 // convert range from -32768..32767 to -1..1 
1088                 fAxisValue /= 32768.0;
1089
1090                 switch (dwAxisMap[i])
1091                 {
1092                 case AxisForward:
1093                         if ((joy_advanced.value == 0.0) && mouselook)
1094                         {
1095                                 // user wants forward control to become look control
1096                                 if (fabs(fAxisValue) > joy_pitchthreshold.value)
1097                                 {               
1098                                         // if mouse invert is on, invert the joystick pitch value
1099                                         // only absolute control support here (joy_advanced is false)
1100                                         if (m_pitch.value < 0.0)
1101                                         {
1102                                                 cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1103                                         }
1104                                         else
1105                                         {
1106                                                 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1107                                         }
1108                                         V_StopPitchDrift();
1109                                 }
1110                                 else
1111                                 {
1112                                         // no pitch movement
1113                                         // disable pitch return-to-center unless requested by user
1114                                         // *** this code can be removed when the lookspring bug is fixed
1115                                         // *** the bug always has the lookspring feature on
1116                                         if(lookspring.value == 0.0)
1117                                                 V_StopPitchDrift();
1118                                 }
1119                         }
1120                         else
1121                         {
1122                                 // user wants forward control to be forward control
1123                                 if (fabs(fAxisValue) > joy_forwardthreshold.value)
1124                                 {
1125                                         cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
1126                                 }
1127                         }
1128                         break;
1129
1130                 case AxisSide:
1131                         if (fabs(fAxisValue) > joy_sidethreshold.value)
1132                         {
1133                                 cmd->sidemove += (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value;
1134                         }
1135                         break;
1136
1137                 case AxisTurn:
1138                         if ((in_strafe.state & 1) || (lookstrafe.value && mouselook))
1139                         {
1140                                 // user wants turn control to become side control
1141                                 if (fabs(fAxisValue) > joy_sidethreshold.value)
1142                                 {
1143                                         cmd->sidemove -= (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value;
1144                                 }
1145                         }
1146                         else
1147                         {
1148                                 // user wants turn control to be turn control
1149                                 if (fabs(fAxisValue) > joy_yawthreshold.value)
1150                                 {
1151                                         if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
1152                                         {
1153                                                 cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * aspeed * cl_yawspeed.value;
1154                                         }
1155                                         else
1156                                         {
1157                                                 cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * speed * 180.0;
1158                                         }
1159
1160                                 }
1161                         }
1162                         break;
1163
1164                 case AxisLook:
1165                         if (mouselook)
1166                         {
1167                                 if (fabs(fAxisValue) > joy_pitchthreshold.value)
1168                                 {
1169                                         // pitch movement detected and pitch movement desired by user
1170                                         if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
1171                                         {
1172                                                 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1173                                         }
1174                                         else
1175                                         {
1176                                                 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * speed * 180.0;
1177                                         }
1178                                         V_StopPitchDrift();
1179                                 }
1180                                 else
1181                                 {
1182                                         // no pitch movement
1183                                         // disable pitch return-to-center unless requested by user
1184                                         // *** this code can be removed when the lookspring bug is fixed
1185                                         // *** the bug always has the lookspring feature on
1186                                         if(lookspring.value == 0.0)
1187                                                 V_StopPitchDrift();
1188                                 }
1189                         }
1190                         break;
1191
1192                 default:
1193                         break;
1194                 }
1195         }
1196
1197         // bounds check pitch
1198         if (cl.viewangles[PITCH] > 80.0)
1199                 cl.viewangles[PITCH] = 80.0;
1200         if (cl.viewangles[PITCH] < -70.0)
1201                 cl.viewangles[PITCH] = -70.0;
1202 }