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