]> icculus.org git repositories - btb/d2x.git/blob - main/old/joydefsw.c
remove rcs tags
[btb/d2x.git] / main / old / joydefsw.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-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #include "desw.h"
16 #include <mmsystem.h>
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <dos.h>
22
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 "victor.h"
37 #include "render.h"
38 #include "palette.h"
39 #include "newmenu.h"
40 #include "args.h"
41 #include "gamefont.h"
42 #include "text.h"
43 #include "kconfig.h"
44 #include "digi.h"
45 #include "playsave.h"
46 #include "screens.h"
47 #include "config.h"
48
49 //      Extra control stuff
50
51 // #include "cyberimp.h"
52
53
54 //##int joydefs_calibrate_flag = 0;
55 int joydefs_max_joysticks = 0;
56 int joydefs_num_joysticks = 0;
57 int Joystick_calibrating = 0;
58
59
60 extern int WriteConfigFile();   
61 extern void kconfig_set_fcs_button(int btn, int button);
62
63
64 WinJoystickDesc WinJoyDesc;
65
66 //      Windows 95:
67 //              Calibration and Menus because of automatic joystick detection
68 //              and 6 axis' (XYZ-RUV)
69
70 extern ubyte default_kconfig_settings[][MAX_CONTROLS];
71
72
73 void win95_autodetect_joystick()
74 {
75         JOYCAPS jc;
76
77 //      Set default windows joystick settings.
78 //      AUTODETECT !!!
79         joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc));
80
81 //      Do buttons first
82         default_kconfig_settings[CONTROL_WINJOYSTICK][1] = 0xff;
83         default_kconfig_settings[CONTROL_WINJOYSTICK][4] = 0xff;
84         default_kconfig_settings[CONTROL_WINJOYSTICK][26] = 0xff;
85
86         default_kconfig_settings[CONTROL_WINJOYSTICK][0] = 0x0;
87         if (jc.wMaxButtons > 1) 
88                 default_kconfig_settings[CONTROL_WINJOYSTICK][1] = 0x1;
89         if (jc.wMaxButtons > 2) 
90                 default_kconfig_settings[CONTROL_WINJOYSTICK][4] = 0x2;
91         if (jc.wMaxButtons > 3) 
92                 default_kconfig_settings[CONTROL_WINJOYSTICK][26] = 0x3;
93
94 //      Do Hat!
95         default_kconfig_settings[CONTROL_WINJOYSTICK][6] = 0xff;
96         default_kconfig_settings[CONTROL_WINJOYSTICK][7] = 0xff;
97         default_kconfig_settings[CONTROL_WINJOYSTICK][8] = 0xff;
98         default_kconfig_settings[CONTROL_WINJOYSTICK][9] = 0xff;
99
100         if (jc.wCaps & JOYCAPS_HASPOV) {
101                 default_kconfig_settings[CONTROL_WINJOYSTICK][6] = 0x10;
102                 default_kconfig_settings[CONTROL_WINJOYSTICK][7] = 0x12;
103                 default_kconfig_settings[CONTROL_WINJOYSTICK][8] = 0x13;
104                 default_kconfig_settings[CONTROL_WINJOYSTICK][9] = 0x11;
105         } 
106
107 //      Do Z-Axis (Throttle)
108
109 //      Do rudder? (R-AXIS) 
110                 
111 }
112
113
114 int win95_controls_init()
115 {
116         //int i = 0;
117         int spjoy = 0;
118
119         if (FindArg("-SpecialDevice")) spjoy = 1;
120
121         joydefs_max_joysticks = joy_init(0,spjoy);
122         if (!joy95_init_stick(1, spjoy)) return 0;
123
124 //@@    for (i = 2; i <= joydefs_max_joysticks; i++)
125 //@@            if (!joy95_init_stick(i,0)) {
126 //@@                    joydefs_max_joysticks = i-1;
127 //@@                    break;
128 //@@            }
129
130         joy_handler_win(0,0,0,0);
131         win95_autodetect_joystick();
132
133         return 1;
134 }
135
136
137 void joy_delay()
138 {
139         stop_time();
140         WinDelay(250);                  
141         joy_flush();
142         start_time();
143 }
144
145 void dump_axis_vals(int *t_vals)
146 {
147         mprintf_at((0, 2, 32, "X:%6d\n", t_vals[0]));
148         mprintf_at((0, 3, 32, "Y:%6d\n", t_vals[1]));
149         mprintf_at((0, 4, 32, "Z:%6d\n", t_vals[2]));
150         mprintf_at((0, 5, 32, "R:%6d\n", t_vals[4]));
151         mprintf_at((0, 6, 32, "U:%6d\n", t_vals[5]));
152         mprintf_at((0, 7, 32, "V:%6d\n", t_vals[6]));
153 }
154
155
156 //      MAIN CALIBRATION ROUTINES
157 //      ----------------------------------------------------------------------------
158
159 void joydefs_calibrate(void)
160 {
161         DWORD dwval;
162         BOOL flag;
163         STARTUPINFO si;
164         PROCESS_INFORMATION pi;
165         char name[80];
166         char oldname[80];
167         int j;
168         int old_mode= -1;
169         
170         Joystick_calibrating = 1;
171
172         if (GRMODEINFO(modex)) {
173                 old_mode = Screen_mode;
174                 set_screen_mode(SCREEN_MENU);
175         }
176         palette_save();
177         apply_modified_palette();
178         reset_palette_add();
179
180         gr_palette_load(gr_palette);
181
182 //      Call calibration Applet
183         joy95_get_name(JOYSTICKID1, oldname, 79);
184
185         logentry("Closing joystick for control panel init.\n");
186         joy_close();                                                    // Close down joystick polling
187
188         memset(&si, 0, sizeof(si));
189         si.cb = sizeof(si);
190         si.dwFlags = STARTF_USESHOWWINDOW;
191         si.wShowWindow = SW_SHOW;
192
193         flag = CreateProcess(
194                         NULL,
195                         "rundll32.exe shell32.dll,Control_RunDLL joy.cpl",
196                         NULL, NULL,
197                         FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL,
198                         &si, &pi);                      
199         if (!flag) {
200                 nm_messagebox( NULL, 1, TXT_OK, "UNABLE TO INITIATE\nJOYSTICK CONTROL PANEL");
201         }
202         else {
203                 dwval = WaitForInputIdle(pi.hProcess, INFINITE);
204                 mprintf((1, "JOY.CPL initiated?\n"));
205                 WinDelayIdle();                                                 // Clear out messages.
206
207                 while (WaitForSingleObject(pi.hProcess, 0) != WAIT_OBJECT_0)
208                 {
209                         if (multi_menu_poll()==-1)      {
210                                 TerminateProcess(pi.hProcess,0);
211                                 Sleep(100); 
212                                 CloseHandle(pi.hThread);
213                                 CloseHandle(pi.hProcess);
214                                 break;
215                         }
216                 }
217
218                 mprintf((1, "JOY.CPL process finished?\n"));
219                 ShowWindow(_hAppWnd, SW_MAXIMIZE);
220                 WinDelayIdle();
221                 SetForegroundWindow(_hAppWnd);
222                 WinDelayIdle();
223                 _RedrawScreen = FALSE;
224         }
225
226         if (win95_controls_init())                                      // Reinitializing joystick polling.
227                 logentry("Reinitialized joystick with new settings.\n");
228         else 
229                 logentry("Failed to reinitialize joystick.\n");
230
231 //      Set kconfig settings to updated vals.
232         joy95_get_name(JOYSTICKID1, name, 79);
233
234         if (Config_control_type == CONTROL_WINJOYSTICK) {
235                 if (strcmp(name, oldname)) {
236                         for (j=0; j<MAX_CONTROLS; j++ )
237                                 kconfig_settings[CONTROL_WINJOYSTICK][j] = default_kconfig_settings[CONTROL_WINJOYSTICK][j];
238                         kc_set_controls();
239                 }
240         }
241
242 //@@    joydefs_calibrate2();
243         
244         reset_cockpit();
245
246         palette_restore();
247
248         if (old_mode != -1)     {
249                 set_screen_mode(old_mode);
250         }
251         
252         Joystick_calibrating = 0;
253 }
254
255
256 //@@//  We should make our own windowed calibrator
257 //@@void joydefs_calibrate2()
258 //@@{
259 //@@    ubyte masks;
260 //@@    char calmsg[6][32];
261 //@@
262 //@@    int org_axis_min[7];
263 //@@    int org_axis_center[7];
264 //@@    int org_axis_max[7];
265 //@@
266 //@@    int axis_min[7] = { 0, 0, 0, 0, 0, 0, 0 };
267 //@@    int axis_cen[7] = { 0, 0, 0, 0, 0, 0, 0 };
268 //@@    int axis_max[7] = { 0, 0, 0, 0, 0, 0, 0 };
269 //@@
270 //@@    int t_vals[7];
271 //@@
272 //@@    char title[50];
273 //@@    char text[50];
274 //@@    int res;
275 //@@    int nsticks = 0;
276 //@@
277 //@@//  Get Current Calibration stuff
278 //@@    joydefs_calibrate_flag = 0;
279 //@@
280 //@@    joy_get_cal_vals(org_axis_min, org_axis_center, org_axis_max);
281 //@@
282 //@@    joy_set_cen();
283 //@@
284 //@@    masks = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, t_vals );
285 //@@
286 //@@    if (!joy_present)       {
287 //@@            nm_messagebox( NULL, 1, TXT_OK, TXT_NO_JOYSTICK );
288 //@@            return;
289 //@@    }
290 //@@    
291 //@@    masks = joy_get_present_mask();
292 //@@
293 //@@    nsticks = 1;
294 //@@
295 //@@    res = joy_calibrate_xy(&axis_min[0], &axis_cen[0],      &axis_max[0],
296 //@@                                                                    &axis_min[1], &axis_cen[1], &axis_max[1]);
297 //@@    
298 //@@    if (!res) {
299 //@@            joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
300 //@@            return;
301 //@@    }
302 //@@
303 //@@    strcpy(calmsg[0], "MOVE JOYSTICK UNTIL BAR REACHES");
304 //@@    strcpy(calmsg[1], "TOP AND PRESS BUTTON");
305 //@@    strcpy(calmsg[2], "MOVE JOYSTICK UNTIL BAR REACHES");
306 //@@    strcpy(calmsg[3], "BOTTOM AND PRESS BUTTON");
307 //@@    strcpy(calmsg[4], "RELEASE JOYSTICK");
308 //@@    strcpy(calmsg[5], "AND PRESS BUTTON");
309 //@@
310 //@@    mprintf((0, "Masks: %x\n", masks));
311 //@@
312 //@@    if (masks&JOY_1_Z_AXIS) {
313 //@@            res = joy_calibrate_bar(2, "CALIBRATE Z-AXIS", calmsg,
314 //@@                                                    &axis_min[2], &axis_cen[2], &axis_max[2]);
315 //@@            if (!res) {
316 //@@                    joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
317 //@@                    return;
318 //@@            }
319 //@@    }
320 //@@
321 //@@    if (masks&JOY_1_R_AXIS) {
322 //@@            res = joy_calibrate_bar(4, "CALIBRATE RUDDER", calmsg,
323 //@@                                                    &axis_min[4], &axis_cen[4], &axis_max[4]);
324 //@@            if (!res) {
325 //@@                    joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
326 //@@                    return;
327 //@@            }
328 //@@    }
329 //@@
330 //@@    if (masks&JOY_1_U_AXIS) {
331 //@@            res = joy_calibrate_bar(5, "CALIBRATE U-AXIS", calmsg,
332 //@@                                                    &axis_min[5], &axis_cen[5], &axis_max[5]);
333 //@@            if (!res) {
334 //@@                    joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
335 //@@                    return;
336 //@@            }
337 //@@    }
338 //@@
339 //@@    if (masks&JOY_1_V_AXIS) {
340 //@@            res = joy_calibrate_bar(6, "CALIBRATE V-AXIS", calmsg,
341 //@@                                                    &axis_min[6], &axis_cen[6], &axis_max[6]);
342 //@@            if (!res) {
343 //@@                    joy_set_cal_vals(org_axis_min, org_axis_center, org_axis_max);
344 //@@                    return;
345 //@@            }
346 //@@    }
347 //@@
348 //@@
349 //@@    joy_set_cal_vals(axis_min, axis_cen, axis_max);
350 //@@
351 //@@    WriteConfigFile();      
352 //@@}
353
354
355 //      Joydefs button detection
356 //              This function will do Windows Joystick Button stuff
357 //              may emulate Thrustmaster FCS, Flightstick Pro, etc.
358                 
359 int joydefsw_do_button()
360 {
361         int axis[7];    
362         int code=255;
363         int i;
364
365         joystick_read_raw_axis(JOY_ALL_AXIS, axis);     
366
367         switch (Config_control_type) 
368         {
369                 case CONTROL_WINJOYSTICK:
370                         code = joydefsw_do_winjoybutton(axis);
371                         break;
372         }
373
374 //      Get standard buttons
375         for (i=0; i<16; i++ )   {
376                 if ( joy_get_button_state(i) )
377                         code = i;
378         }
379
380         return code;                      
381 }
382
383
384 int joydefsw_do_winjoybutton(int *axis)
385 {
386 //      Do POV detection first.  We should use the same
387 // procedure as the DOS version so that we can emulate the
388 // Thrustmaster selections (to keep Player files the same between
389 //      versions.  (19,15,11,7,0)
390         int button;
391
392         if (axis[3] == JOY_POVBACKWARD) 
393                 button = 17;
394         else if (axis[3] == JOY_POVFORWARD)
395                 button = 19;
396         else if (axis[3] == JOY_POVLEFT)
397                 button = 16;
398         else if (axis[3] == JOY_POVRIGHT)
399                 button = 18;
400         else if (axis[3] == JOY_POVCENTERED) 
401                 button = 0;
402
403         kconfig_set_fcs_button(19,button);
404         kconfig_set_fcs_button(18,button);
405         kconfig_set_fcs_button(17,button);
406         kconfig_set_fcs_button(16, button);
407
408         if (button == 0) button = 255;
409
410         return button;
411
412
413
414
415 //      Joystick Menu 
416 //              We want to keep the config files the same for the Windows/DOS
417 //              versions.  But we also autodetect joystick stuff.
418 //              Find some way to keep this easier than the DOS version without screwing 
419 //              up configuration files.
420
421 void joydef_menuset_1(int nitems, newmenu_item * items, int *last_key, int citem )
422 {
423         //int i;
424         int oc_type = Config_control_type;
425
426         nitems = nitems;
427         last_key = last_key;
428         citem = citem;          
429
430         if (items[0].value) Config_control_type = CONTROL_NONE;
431         else if (items[1].value) Config_control_type = CONTROL_MOUSE;
432         else if (items[2].value) Config_control_type = CONTROL_WINJOYSTICK;
433
434 //@@    if ( (oc_type != Config_control_type) && (Config_control_type == CONTROL_THRUSTMASTER_FCS ) )   {
435 //@@                    nm_messagebox( TXT_IMPORTANT_NOTE, 1, TXT_OK, TXT_FCS );
436 //@@            }
437
438         if (oc_type != Config_control_type) {
439                 //##switch (Config_control_type) {
440                 //##    case    CONTROL_WINJOYSTICK:
441                 //##            joydefs_calibrate_flag = 1;
442                 //##}
443                 kc_set_controls();
444         }
445
446 }
447
448 extern ubyte kc_use_external_control;
449 extern ubyte kc_enable_external_control;
450 extern ubyte *kc_external_name;
451
452 void joydefs_config()
453 {
454         int i, old_masks, masks,nitems;
455         newmenu_item m[13];
456         int i1=11;
457         char xtext[92];
458         char joytext[128];
459         int no_joystick_option = 0;
460
461         //##joydefs_calibrate_flag = 0;
462
463         joy95_get_name(JOYSTICKID1, xtext, 127);
464         if (!strcmpi(xtext, "NO JOYSTICK DETECTED") && (Config_control_type == CONTROL_WINJOYSTICK))    {
465                 Config_control_type = CONTROL_NONE;
466                 no_joystick_option = 1;
467         }
468         else no_joystick_option = 0;
469         nm_wrap_text(joytext, xtext, 20);
470         
471         do {
472                 nitems=10;
473                 m[0].type = NM_TYPE_RADIO; m[0].text = CONTROL_TEXT(0); m[0].value = 0; m[0].group = 0;
474                 m[1].type = NM_TYPE_RADIO; m[1].text = CONTROL_TEXT(5); m[1].value = 0; m[1].group = 0;
475         
476                 if (no_joystick_option) {
477                         m[2].type = NM_TYPE_TEXT; m[2].text = joytext; m[2].value = 0; m[2].group = 0;
478                 } else {
479                         m[2].type = NM_TYPE_RADIO; m[2].text = joytext; m[2].value = 0; m[2].group = 0;
480                 }
481
482                 m[3].type = NM_TYPE_TEXT;  m[3].text="";
483                 m[4].type = NM_TYPE_MENU; m[4].text=TXT_CUST_ABOVE;
484                 m[5].type = NM_TYPE_TEXT;  m[5].text="";
485                 m[6].type = NM_TYPE_SLIDER; m[6].text=TXT_JOYS_SENSITIVITY; m[6].value=Config_joystick_sensitivity; m[6].min_value =0; m[6].max_value = 8;
486                 m[7].type = NM_TYPE_TEXT;   m[7].text="";
487                 m[8].type = NM_TYPE_TEXT;   m[8].text="";
488                 m[9].type = NM_TYPE_MENU; m[9].text=TXT_CUST_KEYBOARD;
489
490                 if (Config_control_type == CONTROL_WINJOYSTICK) m[2].value = 1;
491                 else if (Config_control_type == CONTROL_MOUSE) m[1].value = 1;
492                 else if (Config_control_type == CONTROL_NONE) m[0].value = 1;
493                 else m[0].value = 1;   
494
495                 i1 = newmenu_do1( NULL, TXT_CONTROLS, nitems, m, joydef_menuset_1, i1 );
496                 Config_joystick_sensitivity = m[6].value;
497
498                 switch(i1)      {
499                 case 4: {
500                                 old_masks = 0;
501                                 for (i=0; i<4; i++ )            {
502                                         if (kconfig_is_axes_used(i))
503                                                 old_masks |= (1<<i);
504                                 }
505                                 if ( Config_control_type==0 )
506                                         Config_control_type=0;
507                                 else if ( Config_control_type < CONTROL_MOUSE ) 
508                                         kconfig(1, CONTROL_TEXT(Config_control_type) ); 
509                                 else if ( Config_control_type == CONTROL_WINJOYSTICK) 
510                                         kconfig(3, CONTROL_TEXT(Config_control_type) );
511                                 else 
512                                         kconfig(2, CONTROL_TEXT(Config_control_type) ); 
513
514                                 masks = 0;
515                                 for (i=0; i<4; i++ )            {
516                                         if (kconfig_is_axes_used(i))
517                                                 masks |= (1<<i);
518                                 }
519
520                                 //##switch (Config_control_type) {
521                                 //##case        CONTROL_WINJOYSTICK:
522                                 //##    {
523                                 //##            for (i=0; i<4; i++ )    {
524                                 //##                    if ( (masks&(1<<i)) && (!(old_masks&(1<<i))))
525                                 //##                            joydefs_calibrate_flag = 1;
526                                 //##            }
527                                 //##    }
528                                 //##    break;
529                                 //##}
530                         }
531                         break;
532                 case 9: 
533                         kconfig(0, TXT_KEYBOARD); 
534                         break;
535                 } 
536
537         } while(i1 > -1);
538
539 //      No calibration under new system where control panel does this.
540 //      switch (Config_control_type) {
541 //              case    CONTROL_WINJOYSTICK:
542 //                      if ( joydefs_calibrate_flag )
543 //                              joydefs_calibrate();
544 //                      break;
545 //      }
546 }
547
548
549 //      Extended joystick menu
550 //      ----------------------------------------------------------------------------
551 void joydefsw_win_joyselect(char *title)
552 {
553         FILE *fp;
554         char filename[64];
555         char *token;
556         ubyte axis;
557
558         if (newmenu_filelist("Windows 95 Controls", "*.joy", filename)) {
559                 fp = fopen(filename, "rb");
560                 if (!fp) {
561                         nm_messagebox(TXT_ERROR, 1, TXT_OK, "Unable to find control file");
562                         strcpy(title, "Windows 95 Custom");
563                         return;
564                 }
565         
566         // Get Axis support.
567                 if (!fread(&axis, sizeof(ubyte), 1, fp)) {
568                         nm_messagebox(TXT_ERROR, 1, TXT_OK, "Illegal or corrupt control file");
569                         strcpy(title, "Windows 95 Custom");
570                         fclose(fp);
571                         return;
572                 }
573
574
575                 if (fread(kconfig_settings[CONTROL_WINJOYSTICK], 
576                                         sizeof(kconfig_settings[CONTROL_WINJOYSTICK]), 
577                                 1, fp) != 1) {
578                         nm_messagebox(TXT_ERROR, 1, TXT_OK, "Illegal or corrupt control file");
579                         strcpy(title, "Windows 95 Custom");
580                         fclose(fp);
581                         return;
582                 }                       
583                 fclose(fp);                                     
584
585                 token = strtok(filename, ".");
586                 strcpy(title, token);
587         }
588         else strcpy(title, "Windows 95 Custom");
589 }
590