]> icculus.org git repositories - btb/d2x.git/blob - arch/dos/joydefs.c
convert control type, joystick sensitivity to cvars
[btb/d2x.git] / arch / dos / joydefs.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  * 
16  * .
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "pstypes.h"
29 #include "mono.h"
30 #include "key.h"
31 #include "joy.h"
32 #include "timer.h"
33 #include "error.h"
34
35 #include "inferno.h"
36 #include "game.h"
37 #include "object.h"
38 #include "player.h"
39
40 #include "controls.h"
41 #include "joydefs.h"
42 #include "render.h"
43 #include "palette.h"
44 #include "newmenu.h"
45 #include "args.h"
46 #include "text.h"
47 #include "kconfig.h"
48 #include "digi.h"
49 #include "playsave.h"
50
51 int joydefs_calibrate_flag = 0;
52
53 int Joy_is_Sidewinder = 0;
54
55 void joy_delay()
56 {
57 #ifdef __MSDOS__
58         int t1 = TICKER + 19/4;                 // Wait 1/4 second...
59         stop_time();
60         while( TICKER < t1 );
61         joy_flush();
62         start_time();
63 #endif
64 }
65
66
67 int joycal_message( char * title, char * text )
68 {
69         int i;
70         newmenu_item    m[2];
71         m[0].type = NM_TYPE_TEXT; m[0].text = text;
72         m[1].type = NM_TYPE_MENU; m[1].text = TXT_OK;
73         i = newmenu_do( title, NULL, 2, m, NULL );
74         if ( i < 0 ) 
75                 return 1;
76         return 0;
77 }
78
79 extern int WriteConfigFile();   
80
81 void joydefs_calibrate()
82 {
83         ubyte masks;
84         int org_axis_min[4];
85         int org_axis_center[4];
86         int org_axis_max[4];
87
88         int axis_min[4] = { 0, 0, 0, 0 };
89         int axis_cen[4] = { 0, 0, 0, 0 };
90         int axis_max[4] = { 0, 0, 0, 0 };
91
92         int temp_values[4];
93         char title[50];
94         char text[50];
95         int nsticks = 0;
96
97         joydefs_calibrate_flag = 0;
98
99         if ( (Config_control_type.intval != CONTROL_JOYSTICK) &&
100                  (Config_control_type.intval != CONTROL_FLIGHTSTICK_PRO) &&
101                  (Config_control_type.intval != CONTROL_THRUSTMASTER_FCS) &&
102                  (Config_control_type.intval != CONTROL_GRAVIS_GAMEPAD) )
103                 return;
104
105         joy_get_cal_vals(org_axis_min, org_axis_center, org_axis_max);
106
107         joy_set_cen();
108         joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
109
110         if (!joy_present)       {
111                 nm_messagebox( NULL, 1, TXT_OK, TXT_NO_JOYSTICK );
112                 return;
113         }
114         
115         masks = joy_get_present_mask();
116
117         if ( masks == JOY_ALL_AXIS )
118                 nsticks = 2;
119         else
120                 nsticks = 1;
121
122         if ( nsticks == 2 && !Joy_is_Sidewinder)     {
123                 sprintf( title, "%s #1\n%s", TXT_JOYSTICK, TXT_UPPER_LEFT);
124                 sprintf( text, "%s #1 %s", TXT_MOVE_JOYSTICK, TXT_TO_UL);
125         } else {
126                 sprintf( title, "%s\n%s", TXT_JOYSTICK, TXT_UPPER_LEFT);
127                 sprintf( text, "%s %s", TXT_MOVE_JOYSTICK, TXT_TO_UL);
128         }
129         if (joycal_message( title, text )) {
130                 joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
131                 return;
132         }
133         joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
134         axis_min[0] = temp_values[0];
135         axis_min[1] = temp_values[1];
136         joy_delay();
137
138         if ( nsticks == 2 && !Joy_is_Sidewinder)     {
139                 sprintf( title, "%s #1\n%s", TXT_JOYSTICK, TXT_LOWER_RIGHT);
140                 sprintf( text, "%s #1 %s", TXT_MOVE_JOYSTICK, TXT_TO_LR);
141         } else {
142                 sprintf( title, "%s\n%s", TXT_JOYSTICK, TXT_LOWER_RIGHT);
143                 sprintf( text, "%s %s", TXT_MOVE_JOYSTICK, TXT_TO_LR);
144         }
145         if (joycal_message( title, text)) {
146                 joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
147                 return;
148         }
149         joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
150         axis_max[0] = temp_values[0];
151         axis_max[1] = temp_values[1];
152         joy_delay();
153
154         if ( nsticks == 2 && !Joy_is_Sidewinder)     {
155                 sprintf( title, "%s #1\n%s", TXT_JOYSTICK, TXT_CENTER);
156                 sprintf( text, "%s #1 %s", TXT_MOVE_JOYSTICK, TXT_TO_C);
157         } else {
158                 sprintf( title, "%s\n%s", TXT_JOYSTICK, TXT_CENTER);
159                 sprintf( text, "%s %s", TXT_MOVE_JOYSTICK, TXT_TO_C);
160         }
161         if (joycal_message( title, text)) {
162                 joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
163                 return;
164         }
165         joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
166         axis_cen[0] = temp_values[0];
167         axis_cen[1] = temp_values[1];
168         joy_delay();
169
170         // The fcs uses axes 3 for hat, so don't calibrate it.
171         if (Config_control_type.intval == CONTROL_THRUSTMASTER_FCS) {
172                 axis_min[3] = 0;
173                 axis_cen[3] = temp_values[3]/2;
174                 axis_max[3] = temp_values[3];
175                 joy_delay();
176         }
177
178         if (Joy_is_Sidewinder || Config_control_type.intval != CONTROL_THRUSTMASTER_FCS) {
179             //    masks = joy_get_present_mask();
180
181                 if ( nsticks == 2 )     {
182                         if ( kconfig_is_axes_used(2) || kconfig_is_axes_used(3) )       {
183                                 if(Joy_is_Sidewinder)
184                                  {
185                                   sprintf( title, "%s\nTWIST-LEFT", TXT_JOYSTICK);
186                                   sprintf( text, "Twist Joystick to \nthe left side");
187                                  }
188                                 else
189                                  {
190                                   sprintf( title, "%s #2\n%s", TXT_JOYSTICK, TXT_UPPER_LEFT);
191                                   sprintf( text, "%s #2 %s", TXT_MOVE_JOYSTICK, TXT_TO_UL);
192                                  }
193                                 if (joycal_message( title, text )) {
194                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
195                                         return;
196                                 }
197                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
198                                 axis_min[2] = temp_values[2];
199                                 axis_min[3] = temp_values[3];
200                                 joy_delay();
201
202                                 if(Joy_is_Sidewinder)
203                                  {
204                                   sprintf( title, "%s\nTWIST-RIGHT", TXT_JOYSTICK);
205                                   sprintf( text, "Twist Joystick to \nthe right side");
206                                  }
207                                 else
208                                  {
209                                   sprintf( title, "%s #2\n%s", TXT_JOYSTICK, TXT_LOWER_RIGHT);
210                                   sprintf( text, "%s #2 %s", TXT_MOVE_JOYSTICK, TXT_TO_LR);
211                                  }
212                                 if (joycal_message( title, text ))      {
213                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max); 
214                                         return;
215                                 }
216                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
217                                 axis_max[2] = temp_values[2];
218                                 axis_max[3] = temp_values[3];
219                                 joy_delay();
220
221                                 
222                                 if(Joy_is_Sidewinder)
223                                  {
224                                   sprintf( title, "%s\nCENTER", TXT_JOYSTICK);
225                                   sprintf( text, "%s %s",TXT_MOVE_JOYSTICK, TXT_TO_C);
226                                  }
227                                 else
228                                  {
229                                    sprintf( title, "%s #2\n%s", TXT_JOYSTICK, TXT_CENTER);
230                                    sprintf( text, "%s #2 %s", TXT_MOVE_JOYSTICK, TXT_TO_C);
231                                  }
232
233                                 if (joycal_message( title, text ))      {
234                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
235                                         return;
236                                 }
237                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
238                                 axis_cen[2] = temp_values[2];
239                                 axis_cen[3] = temp_values[3];   
240                                 joy_delay();
241                         }
242                 } else if ( (!(masks & JOY_2_X_AXIS)) && (masks & JOY_2_Y_AXIS) )       {
243                         if ( kconfig_is_axes_used(3) )  {
244                                 // A throttle axis!!!!!
245                                 sprintf( title, "%s\n%s", TXT_THROTTLE, TXT_FORWARD);
246                                 if (joycal_message( title, TXT_MOVE_THROTTLE_F))        {
247                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
248                                         return;
249                                 }
250                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
251                                 axis_min[3] = temp_values[3];
252                                 joy_delay();
253                 
254                                 sprintf( title, "%s\n%s", TXT_THROTTLE, TXT_REVERSE);
255                                 if (joycal_message( title, TXT_MOVE_THROTTLE_R)) {
256                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
257                                         return;
258                                 }
259                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
260                                 axis_max[3] = temp_values[3];
261                                 joy_delay();
262                 
263                                 sprintf( title, "%s\n%s", TXT_THROTTLE, TXT_CENTER);
264                                 if (joycal_message( title, TXT_MOVE_THROTTLE_C)) {
265                                         joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
266                                         return;
267                                 }
268                                 joystick_read_raw_axis( JOY_ALL_AXIS, temp_values );
269                                 axis_cen[3] = temp_values[3];
270                                 joy_delay();
271                         }
272                 }
273         }
274         joy_set_cal_vals(axis_min, axis_cen, axis_max);
275
276
277 //added 9/1/98 by Victor Rachels to make sidewinder calibratable
278 /*         if(Joy_is_Sidewinder)
279                 cvar_setint(&Config_control_type, tempstick); */
280 //end this section addition - Victor Rachels
281
282
283         WriteConfigFile();
284 }
285
286
287 //char *control_text[CONTROL_MAX_TYPES] = { "Keyboard only", "Joystick", "Flightstick Pro", "Thrustmaster FCS", "Gravis Gamepad", "Mouse", "Cyberman" };
288
289 void joydef_menuset_1(int nitems, newmenu_item * items, int *last_key, int citem )
290 {
291         int i;
292         int oc_type = Config_control_type.intval;
293
294         nitems = nitems;
295         last_key = last_key;
296         citem = citem;          
297
298         for (i=0; i<CONTROL_MAX_TYPES; i++ )
299                 if (items[i].value)
300                         cvar_setint(&Config_control_type, i);
301
302         if ( (oc_type != Config_control_type.intval) && (Config_control_type.intval == CONTROL_THRUSTMASTER_FCS ) ) {
303                 nm_messagebox( TXT_IMPORTANT_NOTE, 1, TXT_OK, TXT_FCS );
304         }
305
306         if (oc_type != Config_control_type.intval) {
307                 switch (Config_control_type.intval) {
308         //              case    CONTROL_NONE:
309                         case    CONTROL_JOYSTICK:
310                         case    CONTROL_FLIGHTSTICK_PRO:
311                         case    CONTROL_THRUSTMASTER_FCS:
312                         case    CONTROL_GRAVIS_GAMEPAD:
313         //              case    CONTROL_MOUSE:
314         //              case    CONTROL_CYBERMAN:
315                                 joydefs_calibrate_flag = 1;
316                 }
317                 kc_set_controls();
318         }
319
320 }
321
322 extern ubyte kc_use_external_control;
323 extern ubyte kc_enable_external_control;
324 extern ubyte *kc_external_name;
325
326 void joydefs_config()
327 {
328         char xtext[128];
329         int i, old_masks, masks;
330         newmenu_item m[13];
331         int i1=9;
332         int nitems;
333
334         do {
335                 nitems = 12;
336                 m[0].type = NM_TYPE_RADIO; m[0].text = CONTROL_TEXT(0); m[0].value = 0; m[0].group = 0;
337                 m[1].type = NM_TYPE_RADIO; m[1].text = CONTROL_TEXT(1); m[1].value = 0; m[1].group = 0;
338                 m[2].type = NM_TYPE_RADIO; m[2].text = CONTROL_TEXT(2); m[2].value = 0; m[2].group = 0;
339                 m[3].type = NM_TYPE_RADIO; m[3].text = CONTROL_TEXT(3); m[3].value = 0; m[3].group = 0;
340                 m[4].type = NM_TYPE_RADIO; m[4].text = CONTROL_TEXT(4); m[4].value = 0; m[4].group = 0;
341                 m[5].type = NM_TYPE_RADIO; m[5].text = CONTROL_TEXT(5); m[5].value = 0; m[5].group = 0;
342                 m[6].type = NM_TYPE_RADIO; m[6].text = CONTROL_TEXT(6); m[6].value = 0; m[6].group = 0;
343                 m[7].type = NM_TYPE_MENU; m[7].text=TXT_CUST_ABOVE;
344                 m[8].type = NM_TYPE_TEXT; m[8].text="";
345                 m[9].type = NM_TYPE_MENU; m[9].text=TXT_CUST_KEYBOARD;
346 //added on 2/5/99 by Victor Rachels to add d1x key menu
347                 m[10].type = NM_TYPE_MENU; m[10].text="CUSTOMIZE D1X KEYS";
348 //end this section addition - VR
349                 m[11].type = NM_TYPE_CHECK; m[11].text= "Joy is Sidewinder"; m[11].value=Joy_is_Sidewinder;
350         
351                 if ( kc_use_external_control )  {
352                         sprintf( xtext, "Enable %s", kc_external_name );
353                         m[12].type = NM_TYPE_CHECK; m[12].text = xtext; m[12].value = kc_enable_external_control;
354                         nitems = nitems + 1;
355                 }
356                 
357                 m[Config_control_type.intval].value = 1;
358          
359                 i1 = newmenu_do1( NULL, TXT_CONTROLS, nitems, m, joydef_menuset_1, i1 );
360
361 //added on 9/1/98 by Victor Rachels to add Sidewinder calibration
362                 Joy_is_Sidewinder = m[11].value;
363 //end this section addition
364
365                 switch(i1)      {
366                 case 7: {
367                                 old_masks = 0;
368                                 for (i=0; i<4; i++ )            {
369                                         if (kconfig_is_axes_used(i))
370                                                 old_masks |= (1<<i);
371                                 }
372                                 if ( Config_control_type.intval == CONTROL_NONE )
373                                         // nothing...
374                                         cvar_setint(&Config_control_type, CONTROL_NONE);
375                                 else if ( Config_control_type.intval < CONTROL_MOUSE )
376                                         kconfig(1, CONTROL_TEXT(Config_control_type.intval) );
377                                 else 
378                                         kconfig(2, CONTROL_TEXT(Config_control_type.intval) );
379
380                                 masks = 0;
381                                 for (i=0; i<4; i++ )            {
382                                         if (kconfig_is_axes_used(i))
383                                                 masks |= (1<<i);
384                                 }
385
386                                 switch (Config_control_type.intval) {
387                                 case    CONTROL_JOYSTICK:
388                                 case    CONTROL_FLIGHTSTICK_PRO:
389                                 case    CONTROL_THRUSTMASTER_FCS:       
390                                         {
391                                                 for (i=0; i<4; i++ )    {
392                                                         if ( (masks&(1<<i)) && (!(old_masks&(1<<i))))
393                                                                 joydefs_calibrate_flag = 1;
394                                                 }
395                                         }
396                                         break;
397                                 }
398                         }
399                         break;
400                 case 9: 
401                         kconfig(0, TXT_KEYBOARD); 
402                         break;
403 //added on 2/5/99 by Victor Rachels to add d1x key menu
404                 case 10:
405                         kconfig(3,"D1X Keys");
406                         break;
407 //end this section addition - VR
408                 }
409
410                 if ( kc_use_external_control )  {
411                         kc_enable_external_control = m[12].value;
412                 }
413
414         } while(i1>-1);
415
416         switch (Config_control_type.intval) {
417         case    CONTROL_JOYSTICK:
418         case    CONTROL_FLIGHTSTICK_PRO:
419         case    CONTROL_THRUSTMASTER_FCS:
420         case    CONTROL_GRAVIS_GAMEPAD:
421                 if ( joydefs_calibrate_flag )
422                         joydefs_calibrate();
423                 break;
424         }
425
426 }