1 #ifdef OPENGL
2 #include "../ref_gl/gl_local.h"
3 #include <dlfcn.h>
4 #else
5 #include "../ref_soft/r_local.h"
6 #endif
7 
8 #include "rw_linux.h"
9 
10 // state struct passed in Init
11 
12 static cvar_t   *in_joystick;
13 static cvar_t	*joy_name;
14 static cvar_t   *joy_dev;
15 static cvar_t	*joy_advanced;
16 static cvar_t	*joy_advaxisx;
17 static cvar_t	*joy_advaxisy;
18 static cvar_t	*joy_advaxisz;
19 static cvar_t	*joy_advaxisr;
20 static cvar_t	*joy_advaxisu;
21 static cvar_t	*joy_advaxisv;
22 static cvar_t	*joy_forwardthreshold;
23 static cvar_t	*joy_sidethreshold;
24 static cvar_t	*joy_pitchthreshold;
25 static cvar_t	*joy_yawthreshold;
26 static cvar_t	*joy_forwardsensitivity;
27 static cvar_t	*joy_sidesensitivity;
28 static cvar_t	*joy_pitchsensitivity;
29 static cvar_t	*joy_yawsensitivity;
30 static cvar_t	*joy_upthreshold;
31 static cvar_t	*joy_upsensitivity;
32 
33 static cvar_t  *cl_upspeed;
34 static cvar_t  *cl_forwardspeed;
35 static cvar_t  *cl_sidespeed;
36 static cvar_t  *cl_yawspeed;
37 static cvar_t  *cl_pitchspeed;
38 static cvar_t  *cl_anglespeedkey;
39 
40 static cvar_t  *cl_run;
41 
42 static qboolean joy_avail = false;
43 static int *axis_vals = NULL;
44 static int *axis_map = NULL;
45 
46 qboolean OpenJoystick(cvar_t *);
47 void PlatformJoyCommands(int *, int *);
48 
RW_IN_JoystickCommands()49 void RW_IN_JoystickCommands() {
50   PlatformJoyCommands(axis_vals, axis_map);
51 }
52 
Joy_AdvancedUpdate_f()53 void Joy_AdvancedUpdate_f () {
54   // called once by IN_ReadJoystick and by user whenever an update is needed
55   // cvars are now available
56   if (joy_advanced->value != 0.0) {
57     if (strcmp (joy_name->string, "joystick")) {
58       // notify user of advanced controller
59       ri.Con_Printf (PRINT_ALL,"\n%s configured\n\n", joy_name->string);
60     }
61 
62     // advanced initialization here
63     // data supplied by user via joy_axisn cvars
64     axis_map[0] = atoi(joy_advaxisx->string);
65     axis_map[1] = atoi(joy_advaxisy->string);
66     axis_map[2] = atoi(joy_advaxisz->string);
67     axis_map[3] = atoi(joy_advaxisr->string);
68     axis_map[4] = atoi(joy_advaxisu->string);
69     axis_map[5] = atoi(joy_advaxisv->string);
70   }
71 }
72 
73 
RW_IN_InitJoystick()74 void RW_IN_InitJoystick() {
75   in_joystick = ri.Cvar_Get ("in_joystick", "1", CVAR_ARCHIVE);
76   if (in_joystick->value) {
77     joy_name = ri.Cvar_Get ("joy_name", "joystick", 0);
78     joy_dev = ri.Cvar_Get ("joy_dev","/dev/joy*",CVAR_ARCHIVE);
79     joy_advanced = ri.Cvar_Get ("joy_advanced","0",0);
80     joy_advaxisx = ri.Cvar_Get ("joy_advaxisx","4",0);
81     joy_advaxisy = ri.Cvar_Get ("joy_advaxisy","2",0);
82     joy_advaxisz = ri.Cvar_Get ("joy_advaxisz","0",0);
83     joy_advaxisr = ri.Cvar_Get ("joy_advaxisr","0",0);
84     joy_advaxisu = ri.Cvar_Get ("joy_advaxisu","0",0);
85     joy_advaxisv = ri.Cvar_Get ("joy_advaxisv","0",0);
86     joy_forwardthreshold = ri.Cvar_Get ("joy_forwardthreshold","0.15",0);
87     joy_sidethreshold = ri.Cvar_Get ("joy_sidethreshold","0.15",0);
88     joy_upthreshold	= ri.Cvar_Get ("joy_upthreshold", "0.15",0);
89     joy_pitchthreshold = ri.Cvar_Get ("joy_pitchthreshold","0.15",0);
90     joy_yawthreshold = ri.Cvar_Get ("joy_yawthreshold","0.15",0);
91     joy_forwardsensitivity = ri.Cvar_Get ("joy_forwardsensitivity","-1",0);
92     joy_sidesensitivity = ri.Cvar_Get ("joy_sidesensitivity","1",0);
93     joy_upsensitivity = ri.Cvar_Get ("joy_upsensitivity","-1",0);
94     joy_pitchsensitivity = ri.Cvar_Get ("joy_pitchsensitivity","1",0);
95     joy_yawsensitivity = ri.Cvar_Get ("joy_yawsensitivity","-1",0);
96 
97     cl_upspeed = ri.Cvar_Get ("cl_upspeed", "200", 0);
98     cl_forwardspeed = ri.Cvar_Get ("cl_forwardspeed", "200", 0);
99     cl_sidespeed = ri.Cvar_Get ("cl_sidespeed", "200", 0);
100     cl_yawspeed = ri.Cvar_Get ("cl_yawspeed", "140", 0);
101     cl_pitchspeed = ri.Cvar_Get ("cl_pitchspeed", "150", 0);
102 
103     cl_run = ri.Cvar_Get ("cl_run", "0", CVAR_ARCHIVE);
104 
105     if (!axis_map) {
106       int i;
107       axis_map = (int *)malloc(sizeof(int)*6);
108       axis_vals = (int *)malloc(sizeof(int)*6);
109       for (i=0;i<6;i++) {
110 	axis_map[i] = 0;
111 	axis_vals[i] = 0;
112       }
113       axis_map[0]=3;
114       axis_map[1]=1;
115     }
116 
117     ri.Cmd_AddCommand ("joy_advancedupdate", Joy_AdvancedUpdate_f);
118     if ((joy_avail = OpenJoystick(joy_dev))) {
119       ri.Con_Printf(PRINT_ALL, "Joystick Activated");
120       Joy_AdvancedUpdate_f();
121     }
122   }
123 }
124 
RW_IN_JoystickMove(usercmd_t * cmd,qboolean mlooking,cvar_t * lookstrafe,cvar_t * m_pitch)125 void RW_IN_JoystickMove(usercmd_t *cmd, qboolean mlooking,
126 			cvar_t *lookstrafe, cvar_t *m_pitch) {
127   in_state_t *in_state = getState();
128 
129   if (joy_avail) {
130     // Start
131     float speed, aspeed;
132     float jforward = ((float)axis_vals[1])/32768.0;
133     float jside = ((float)axis_vals[3])/32768.0;
134     float jup = ((float)axis_vals[5])/32768.0;
135     float jturn = ((float)axis_vals[4])/32768.0;
136     float jlook = ((float)axis_vals[2])/32768.0;
137 
138     //ri.Con_Printf(PRINT_ALL, "%g %g %g %g %g\n",
139     //jforward, jside, jup, jturn, jlook);
140 
141     if (((*in_state->in_speed_state) & 1) || cl_run->value!=0.0)
142       speed = 2;
143     else
144       speed = 1;
145     aspeed = 0.025;
146 
147     // This is the forward/backward code
148     if ((joy_advanced->value == 0.0) && mlooking) {
149       // user wants forward control to become look control
150       // if mouse invert is on, invert the joystick pitch value
151       // only absolute control support here (joy_advanced is false)
152       if (fabs(jforward) > joy_pitchthreshold->value) {
153 	if (m_pitch->value < 0.0) {
154 	  in_state->viewangles[PITCH] -=
155 	    jforward*joy_pitchsensitivity->value*aspeed*cl_pitchspeed->value;
156 	}
157 	else {
158 	  in_state->viewangles[PITCH] +=
159 	    jforward*joy_pitchsensitivity->value*aspeed*cl_pitchspeed->value;
160 	}
161       }
162     }
163     else if (fabs(jforward) > joy_forwardthreshold->value) {
164       // user wants forward control to be forward control
165       cmd->forwardmove +=
166 	jforward*joy_forwardsensitivity->value*speed*cl_forwardspeed->value;
167     }
168 
169     if (fabs(jside)>joy_sidethreshold->value) {
170       cmd->sidemove +=
171 	(jside*joy_sidesensitivity->value)*speed*cl_sidespeed->value;
172     }
173 
174     if (fabs(jup) > joy_upthreshold->value) {
175       cmd->upmove +=
176 	(jup * joy_upsensitivity->value) * speed*cl_upspeed->value;
177     }
178 
179     if (((*in_state->in_strafe_state) & 1)||(lookstrafe->value && mlooking)) {
180       // user wants turn control to become side control
181       if (fabs(jturn) > joy_sidethreshold->value) {
182 	cmd->sidemove -=
183 	  (jturn*joy_sidesensitivity->value)*speed*cl_sidespeed->value;
184       }
185     }
186     else {
187       // user wants turn control to be turn control
188       if (fabs(jturn) > joy_yawthreshold->value) {
189 	in_state->viewangles[YAW] +=
190 	  (jturn*joy_yawsensitivity->value)*aspeed*cl_yawspeed->value;
191       }
192     }
193 
194     //    if (mlooking) {
195       if (fabs(jlook) > joy_pitchthreshold->value) {
196 	// pitch movement detected and pitch movement desired by user
197 	in_state->viewangles[PITCH] +=
198 	  (jlook*joy_pitchsensitivity->value) * aspeed*cl_pitchspeed->value;
199       }
200       //}
201 
202     // End
203   }
204 }
205