]> icculus.org git repositories - btb/d2x.git/blob - main/kconfig.c
more header cleanup
[btb/d2x.git] / main / kconfig.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  *
16  * Routines to configure keyboard, joystick, etc..
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29
30 #ifdef WINDOWS
31 #include "desw.h"
32 #endif
33 #include "error.h"
34 #include "gr.h"
35 #include "vid.h"
36 #include "mono.h"
37 #include "key.h"
38 #include "inferno.h"
39 #include "iff.h"
40 #include "u_mem.h"
41 #include "joy.h"
42 #include "mouse.h"
43 #include "timer.h"
44 #include "args.h"
45 #if defined (TACTILE)
46  #include "tactile.h"
47 #endif
48 #ifdef USE_LINUX_JOY
49 #include "joystick.h"
50 #endif
51
52
53 ubyte ExtGameStatus=1;
54
55 vms_vector ExtForceVec;
56 vms_matrix ExtApplyForceMatrix;
57
58 int ExtJoltInfo[3]={0,0,0};
59 int ExtXVibrateInfo[2]={0,0};
60 int ExtYVibrateInfo[2]={0,0};
61 ubyte ExtXVibrateClear=0;
62 ubyte ExtYVibrateClear=0;
63
64 #define TABLE_CREATION 1
65
66 // Array used to 'blink' the cursor while waiting for a keypress.
67 sbyte fades[64] = { 1,1,1,2,2,3,4,4,5,6,8,9,10,12,13,15,16,17,19,20,22,23,24,26,27,28,28,29,30,30,31,31,31,31,31,30,30,29,28,28,27,26,24,23,22,20,19,17,16,15,13,12,10,9,8,6,5,4,4,3,2,2,1,1 };
68
69 //char * invert_text[2] = { "N", "Y" };
70 //char * joyaxis_text[4] = { "X1", "Y1", "X2", "Y2" };
71 //char * mouseaxis_text[2] = { "L/R", "F/B" };
72
73 int invert_text[2] = { TNUM_N, TNUM_Y };
74
75 #ifndef USE_LINUX_JOY
76 #if defined(SDL_INPUT)
77 char *joyaxis_text[JOY_MAX_AXES];
78 #else
79         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
80 //      int joyaxis_text[4] = { TNUM_X1, TNUM_Y1, TNUM_X2, TNUM_Y2 };
81 #endif
82 #endif
83
84 int mouseaxis_text[3] = { TNUM_L_R, TNUM_F_B, TNUM_Z1 };
85
86 #if !defined OGL && !defined SDL_INPUT
87 char * key_text[256] = {         \
88 "","ESC","1","2","3","4","5","6","7","8","9","0","-",                   \
89 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",                           \
90 "P","[","]","\x83","LCTRL","A","S","D","F",        \
91 "G","H","J","K","L",";","'","`",        \
92 "LSHFT","\\","Z","X","C","V","B","N","M",",",      \
93 ".","/","RSHFT","PAD*","LALT","SPC",      \
94 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",        \
95 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",   \
96 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0", \
97 "PAD.","","","","F11","F12","","","","","","","","","",         \
98 "","","","","","","","","","","","","","","","","","","","",     \
99 "","","","","","","","","","","","","","","","","","","","",     \
100 "","","","","","","","","","","","","","","","","","",           \
101 "PAD\x83","RCTRL","","","","","","","","","","","","","", \
102 "","","","","","","","","","","PAD/","","","RALT","",      \
103 "","","","","","","","","","","","","","HOME","\x82","PGUP",     \
104 "","\x81","","\x7f","","END","\x80","PGDN","INS",       \
105 "DEL","","","","","","","","","","","","","","","","","",     \
106 "","","","","","","","","","","","","","","","","","","","",     \
107 "","","","","","","" };
108 #endif /* OGL */
109
110 ubyte system_keys[] = { KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_PRINT_SCREEN };
111
112 //extern void GameLoop(int, int );
113
114 // macros for drawing lo/hi res kconfig screens (see scores.c as well)
115
116 #define LHX(x)          ((x)*(MenuHires?2:1))
117 #define LHY(y)          ((y)*(MenuHires?2.4:1))
118
119
120 #define BT_KEY                          0
121 //#define BT_MOUSE_BUTTON       1
122 #define BT_MOUSE_AXIS           2
123 //#define BT_JOY_BUTTON                 3
124 #define BT_JOY_AXIS                     4
125 #define BT_INVERT                               5
126
127 char *btype_text[] = { "BT_KEY", "BT_MOUSE_BUTTON", "BT_MOUSE_AXIS", "BT_JOY_BUTTON", "BT_JOY_AXIS", "BT_INVERT" };
128
129 #define INFO_Y 28
130
131 typedef struct kc_item {
132         short id;                               // The id of this item
133         short x, y;                             
134         short w1;
135         short w2;
136         short u,d,l,r;
137         //short text_num1;
138         char *text;
139         ubyte type;
140         ubyte value;            // what key,button,etc
141 } kc_item;
142
143 int Num_items=28;
144 kc_item *All_items;
145
146
147 #define NUM_KEY_CONTROLS    57
148 #define NUM_OTHER_CONTROLS  24
149 #define NUM_D2X_CONTROLS    24
150
151 //----------- WARNING!!!!!!! -------------------------------------------
152 // THESE NEXT FOUR BLOCKS OF DATA ARE GENERATED BY PRESSING DEL+F12 WHEN
153 // IN THE KEYBOARD CONFIG SCREEN.  BASICALLY, THAT PROCEDURE MODIFIES THE
154 // U,D,L,R FIELDS OF THE ARRAYS AND DUMPS THE NEW ARRAYS INTO KCONFIG.COD
155 //-------------------------------------------------------------------------
156
157 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
158         {  0, 15, 49, 71, 26, 55,  2, 56,  1,"Pitch forward", BT_KEY, 255 },
159         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
160         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
161         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
162         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
163         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
164         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
165         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
166         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
167         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
168         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
169         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
170         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
171         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
172         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
173         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
174         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
175         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
176         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
177         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
178         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
179         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
180         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
181         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
182         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
183         { 25,158, 49,112, 26, 56, 27, 24,  2,"Fire primary", BT_KEY, 255 },
184         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
185         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
186         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
187         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
188         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
189         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
190         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
191         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
192         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
193         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
194         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
195         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
196         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
197         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
198         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
199         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
200         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
201         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
202         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
203         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
204         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
205         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
206         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
207         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
208         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
209         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
210         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
211         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
212         { 54,158,171, 83, 26, 52, 56, 51, 55,"Energy->Shield", BT_KEY, 255 },
213         { 55,158,171,112, 26, 53,  0, 54, 56,"Energy->Shield", BT_KEY, 255 },
214         { 56,158,179, 83, 26, 54, 25, 55,  0,"Toggle Bomb", BT_KEY, 255 },
215 };
216
217 char *kc_key_bind_text[NUM_KEY_CONTROLS] = {
218         "+lookdown",    "+lookdown",
219         "+lookup",      "+lookup",
220         "+left",        "+left",
221         "+right",       "+right",
222         "+strafe",      "+strafe",
223         "+moveleft",    "+moveleft",
224         "+moveright",   "+moveright",
225         "+moveup",      "+moveup",
226         "+movedown",    "+movedown",
227         "+bank",        "+bank",
228         "+bankleft",    "+bankleft",
229         "+bankright",   "+bankright",
230         "+attack",      "+attack",
231         "+attack2",     "+attack2",
232         "flare",        "flare",
233         "+forward",     "+forward",
234         "+back",        "+back",
235         "bomb",         "bomb",
236         "+rearview",    "+rearview",
237         "+cruiseup",    "+cruiseup",
238         "+cruisedown",  "+cruisedown",
239         "+cruiseoff",   "+cruiseoff",
240         "+automap",     "+automap",
241         "+afterburner", "+afterburner",
242         "cycle",        "cycle",
243         "cycle2",       "cycle2",
244         "headlight",    "headlight",
245         "+nrgshield",   "+nrgshield",
246         "togglebomb",
247 };
248
249 ubyte default_kc_keyboard_settings[NUM_KEY_CONTROLS] = {0x48,0xc8,0x50,0xd0,0x4b,0xcb,0x4d,0xcd,0x38,0xff,0x4f,0xff,0x51,0xff,0x4a,0xff,0x4e,0xff,0x2a,0xff,0x10,0x47,0x12,0x49,0x1d,0x80,0x39,0x81,0x21,0x24,0x1e,0xff,0x2c,0xff,0x30,0xff,0x13,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf,0xff,0x1f,0xff,0x33,0xff,0x34,0xff,0x23,0xff,0x14,0xff,0xff};
250
251 kc_item kc_other[NUM_OTHER_CONTROLS] = {
252         {  0, 22,138, 51, 40, 23,  2, 23,  1,"Pitch U/D", BT_JOY_AXIS, 255 },
253         {  1, 22,138, 99,  8, 10,  3,  0, 12,"Pitch U/D", BT_INVERT, 255 },
254         {  2, 22,146, 51, 40,  0,  4, 13,  3,"Turn L/R", BT_JOY_AXIS, 255 },
255         {  3, 22,146, 99,  8,  1,  5,  2, 14,"Turn L/R", BT_INVERT, 255 },
256         {  4, 22,154, 51, 40,  2,  6, 15,  5,"Slide L/R", BT_JOY_AXIS, 255 },
257         {  5, 22,154, 99,  8,  3,  7,  4, 16,"Slide L/R", BT_INVERT, 255 },
258         {  6, 22,162, 51, 40,  4,  8, 17,  7,"Slide U/D", BT_JOY_AXIS, 255 },
259         {  7, 22,162, 99,  8,  5,  9,  6, 18,"Slide U/D", BT_INVERT, 255 },
260         {  8, 22,170, 51, 40,  6, 10, 19,  9,"Bank L/R", BT_JOY_AXIS, 255 },
261         {  9, 22,170, 99,  8,  7, 11,  8, 20,"Bank L/R", BT_INVERT, 255 },
262         { 10, 22,182, 51, 40,  8,  1, 21, 11,"throttle", BT_JOY_AXIS, 255 },
263         { 11, 22,182, 99,  8,  9, 12, 10, 22,"throttle", BT_INVERT, 255 },
264         { 12,182,138, 51, 40, 11, 14,  1, 13,"Pitch U/D", BT_MOUSE_AXIS, 255 },
265         { 13,182,138, 99,  8, 22, 15, 12,  2,"Pitch U/D", BT_INVERT, 255 },
266         { 14,182,146, 51, 40, 12, 16,  3, 15,"Turn L/R", BT_MOUSE_AXIS, 255 },
267         { 15,182,146, 99,  8, 13, 17, 14,  4,"Turn L/R", BT_INVERT, 255 },
268         { 16,182,154, 51, 40, 14, 18,  5, 17,"Slide L/R", BT_MOUSE_AXIS, 255 },
269         { 17,182,154, 99,  8, 15, 19, 16,  6,"Slide L/R", BT_INVERT, 255 },
270         { 18,182,162, 51, 40, 16, 20,  7, 19,"Slide U/D", BT_MOUSE_AXIS, 255 },
271         { 19,182,162, 99,  8, 17, 21, 18,  8,"Slide U/D", BT_INVERT, 255 },
272         { 20,182,170, 51, 40, 18, 22,  9, 21,"Bank L/R", BT_MOUSE_AXIS, 255 },
273         { 21,182,170, 99,  8, 19, 23, 20, 10,"Bank L/R", BT_INVERT, 255 },
274         { 22,182,182, 51, 40, 20, 13, 11, 23,"throttle", BT_MOUSE_AXIS, 255 },
275         { 23,182,182, 99,  8, 21,  0, 22,  0,"throttle", BT_INVERT, 255 },
276 };
277
278 kc_axis_map kc_other_axismap[NUM_OTHER_CONTROLS] = {
279         AXIS_PITCH,     AXIS_NONE,
280         AXIS_TURN,      AXIS_NONE,
281         AXIS_LEFTRIGHT, AXIS_NONE,
282         AXIS_UPDOWN,    AXIS_NONE,
283         AXIS_BANK,      AXIS_NONE,
284         AXIS_THROTTLE,  AXIS_NONE,
285         AXIS_PITCH,     AXIS_NONE,
286         AXIS_TURN,      AXIS_NONE,
287         AXIS_LEFTRIGHT, AXIS_NONE,
288         AXIS_UPDOWN,    AXIS_NONE,
289         AXIS_BANK,      AXIS_NONE,
290         AXIS_THROTTLE,  AXIS_NONE,
291 };
292
293 ubyte default_kc_other_settings[NUM_OTHER_CONTROLS] = {0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x2,0x0};
294
295 kc_item kc_d2x[NUM_D2X_CONTROLS] = {
296 //        id,x,y,w1,w2,u,d,l,r,text_num1,type,value
297         {  0, 15, 49, 71, 26, 23,  2, 23,  1,"WEAPON 1", BT_KEY, 255 },
298         {  1, 15, 49,100, 26, 22,  3,  0,  2,"WEAPON 1", BT_KEY, 255 },
299         {  2, 15, 57, 71, 26,  0,  4,  1,  3,"WEAPON 2", BT_KEY, 255 },
300         {  3, 15, 57,100, 26,  1,  5,  2,  4,"WEAPON 2", BT_KEY, 255 },
301         {  4, 15, 65, 71, 26,  2,  6,  3,  5,"WEAPON 3", BT_KEY, 255 },
302         {  5, 15, 65,100, 26,  3,  7,  4,  6,"WEAPON 3", BT_KEY, 255 },
303         {  6, 15, 73, 71, 26,  4,  8,  5,  7,"WEAPON 4", BT_KEY, 255 },
304         {  7, 15, 73,100, 26,  5,  9,  6,  8,"WEAPON 4", BT_KEY, 255 },
305         {  8, 15, 81, 71, 26,  6, 10,  7,  9,"WEAPON 5", BT_KEY, 255 },
306         {  9, 15, 81,100, 26,  7, 11,  8, 10,"WEAPON 5", BT_KEY, 255 },
307         { 10, 15, 89, 71, 26,  8, 12,  9, 11,"WEAPON 6", BT_KEY, 255 },
308         { 11, 15, 89,100, 26,  9, 13, 10, 12,"WEAPON 6", BT_KEY, 255 },
309         { 12, 15, 97, 71, 26, 10, 14, 11, 13,"WEAPON 7", BT_KEY, 255 },
310         { 13, 15, 97,100, 26, 11, 15, 12, 14,"WEAPON 7", BT_KEY, 255 },
311         { 14, 15,105, 71, 26, 12, 16, 13, 15,"WEAPON 8", BT_KEY, 255 },
312         { 15, 15,105,100, 26, 13, 17, 14, 16,"WEAPON 8", BT_KEY, 255 },
313         { 16, 15,113, 71, 26, 14, 18, 15, 17,"WEAPON 9", BT_KEY, 255 },
314         { 17, 15,113,100, 26, 15, 19, 16, 18,"WEAPON 9", BT_KEY, 255 },
315         { 18, 15,121, 71, 26, 16, 20, 17, 19,"WEAPON 0", BT_KEY, 255 },
316         { 19, 15,121,100, 26, 17, 21, 18, 20,"WEAPON 0", BT_KEY, 255 },
317         { 20, 15,131, 71, 26, 18, 22, 19, 21,"sizedown", BT_KEY, 255 },
318         { 21, 15,131,100, 26, 19, 23, 20, 22,"sizedown", BT_KEY, 255 },
319         { 22, 15,139, 71, 26, 20,  1, 21, 23,"sizeup", BT_KEY, 255 },
320         { 23, 15,139,100, 26, 21,  0, 22,  0,"sizeup", BT_KEY, 255 },
321         //{ 24,  8,147, 78, 26, 22, 26, 23, 25, "TOGGLE_PRIM AUTO", BT_KEY, 255},
322         //{ 25,  8,147,107, 26, 23, 27, 24, 26, "TOGGLE_PRIM_AUTO", BT_KEY, 255},
323         //{ 26,  8,155, 78, 26, 24,  1, 25, 27, "TOGGLE SEC AUTO", BT_KEY, 255},
324         //{ 27,  8,155,107, 26, 25,  0, 26,  0, "TOGGLE SEC AUTO", BT_KEY, 255},
325 };
326
327 ubyte default_kc_d2x_settings[NUM_D2X_CONTROLS] = {0x2,0xff,0x3,0xff,0x4,0xff,0x5,0xff,0x6,0xff,0x7,0xff,0x8,0xff,0x9,0xff,0xa,0xff,0xb,0xff,0xc,0xff,0xd,0xff};
328
329
330 void kc_drawitem( kc_item *item, int is_current );
331 void kc_change_key( kc_item * item );
332 void kc_next_joyaxis(kc_item *item);  //added by WraithX on 11/22/00
333 void kc_change_joyaxis( kc_item * item );
334 void kc_change_mouseaxis( kc_item * item );
335 void kc_change_invert( kc_item * item );
336
337 int kconfig_is_axes_used(int axis)
338 {
339         int i;
340         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
341                 if (( kc_other[i].type == BT_JOY_AXIS ) && (kc_other[i].value == axis ))
342                         return 1;
343         }
344         return 0;
345 }
346
347 #ifdef TABLE_CREATION
348 int find_item_at( kc_item * items, int nitems, int x, int y )
349 {
350         int i;
351         
352         for (i=0; i<nitems; i++ )       {
353                 if ( ((items[i].x+items[i].w1)==x) && (items[i].y==y))
354                         return i;
355         }
356         return -1;
357 }
358
359 int find_next_item_up( kc_item * items, int nitems, int citem )
360 {
361         int x, y, i;
362
363         y = items[citem].y;
364         x = items[citem].x+items[citem].w1;
365         
366         do {    
367                 y--;
368                 if ( y < 0 ) {
369                         y = grd_curcanv->cv_bitmap.bm_h-1;
370                         x--;
371                         if ( x < 0 ) {
372                                 x = grd_curcanv->cv_bitmap.bm_w-1;
373                         }
374                 }
375                 i = find_item_at( items, nitems, x, y );
376         } while ( i < 0 );
377         
378         return i;
379 }
380
381 int find_next_item_down( kc_item * items, int nitems, int citem )
382 {
383         int x, y, i;
384
385         y = items[citem].y;
386         x = items[citem].x+items[citem].w1;
387         
388         do {    
389                 y++;
390                 if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
391                         y = 0;
392                         x++;
393                         if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
394                                 x = 0;
395                         }
396                 }
397                 i = find_item_at( items, nitems, x, y );
398         } while ( i < 0 );
399         
400         return i;
401 }
402
403 int find_next_item_right( kc_item * items, int nitems, int citem )
404 {
405         int x, y, i;
406
407         y = items[citem].y;
408         x = items[citem].x+items[citem].w1;
409         
410         do {    
411                 x++;
412                 if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
413                         x = 0;
414                         y++;
415                         if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
416                                 y = 0;
417                         }
418                 }
419                 i = find_item_at( items, nitems, x, y );
420         } while ( i < 0 );
421         
422         return i;
423 }
424
425 int find_next_item_left( kc_item * items, int nitems, int citem )
426 {
427         int x, y, i;
428
429         y = items[citem].y;
430         x = items[citem].x+items[citem].w1;
431         
432         do {    
433                 x--;
434                 if ( x < 0 ) {
435                         x = grd_curcanv->cv_bitmap.bm_w-1;
436                         y--;
437                         if ( y < 0 ) {
438                                 y = grd_curcanv->cv_bitmap.bm_h-1;
439                         }
440                 }
441                 i = find_item_at( items, nitems, x, y );
442         } while ( i < 0 );
443         
444         return i;
445 }
446 #endif
447
448 #ifdef NEWMENU_MOUSE
449 int get_item_height(kc_item *item)
450 {
451         int w, h, aw;
452         char btext[10];
453
454         if (item->value==255) {
455                 strcpy(btext, "");
456         } else {
457                 switch( item->type )    {
458                         case BT_KEY:
459                                 strncpy( btext, key_text[item->value], 10 ); break;
460                         case BT_MOUSE_AXIS:
461                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
462                         case BT_JOY_AXIS:
463 #ifdef USE_LINUX_JOY
464                                 sprintf( btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number (item->value) );
465 #elif defined(SDL_INPUT)
466                                 if (joyaxis_text[item->value])
467                                         strncpy(btext, joyaxis_text[item->value], 10);
468                                 else
469                                         sprintf(btext, "AXIS%2d", item->value + 1);
470 #else
471                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
472 #endif
473                                 break;
474                         case BT_INVERT:
475                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
476                 }
477         }
478         gr_get_string_size(btext, &w, &h, &aw  );
479
480         return h;
481 }
482 #endif
483
484 void kconfig_sub(kc_item * items,int nitems, char * title)
485 {
486         grs_canvas * save_canvas;
487         grs_font * save_font;
488         int old_keyd_repeat;
489 #ifdef NEWMENU_MOUSE
490         int mouse_state, omouse_state, mx, my, x1, x2, y1, y2;
491         int close_x, close_y, close_size;
492 #endif
493
494         int i,k,ocitem,citem;
495         int time_stopped = 0;
496
497         All_items = items;
498         Num_items = nitems;
499
500         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
501         {
502                 time_stopped = 1;
503                 stop_time();
504         }
505
506         save_canvas = grd_curcanv;
507
508
509         gr_set_current_canvas(NULL);
510         save_font = grd_curcanv->cv_font;
511
512         game_flush_inputs();
513         old_keyd_repeat = keyd_repeat;
514         keyd_repeat = 1;
515
516         //gr_clear_canvas( BM_XRGB(0,0,0) );
517
518         nm_draw_background(0, 0, grd_curcanv->cv_bitmap.bm_w - 1, grd_curcanv->cv_bitmap.bm_h - 1);
519    gr_palette_load (gr_palette);
520
521         grd_curcanv->cv_font = MEDIUM3_FONT;
522
523         {
524                 char * p;
525                 p = strchr( title, '\n' );
526                 if ( p ) *p = 32;
527                 gr_string( 0x8000, LHY(8), title );
528                 if ( p ) *p = '\n';
529         }
530
531
532 //      if ( items == kc_keyboard )     {
533 //              gr_string( 0x8000, 8, "Keyboard" );
534 //      } else if ( items == kc_other ) {
535 //              gr_string( 0x8000, 8, "Others" );
536 //      }
537
538 #ifdef NEWMENU_MOUSE
539         close_x = close_y = MenuHires?15:7;
540         close_size = MenuHires?10:5;
541         /*
542         gr_setcolor( BM_XRGB(0, 0, 0) );
543         gr_rect(close_x, close_y, close_x + close_size, close_y + close_size);
544         gr_setcolor( BM_XRGB(21, 21, 21) );
545         gr_rect(close_x + LHX(1), close_y + LHX(1), close_x + close_size - LHX(1), close_y + close_size - LHX(1));
546         */
547 #endif
548
549         grd_curcanv->cv_font = GAME_FONT;
550         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
551
552         gr_string( 0x8000, LHY(20), TXT_KCONFIG_STRING_1 );
553         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
554         if ( items == kc_keyboard )     {
555                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
556                 gr_setcolor( BM_XRGB(31,27,6) );
557                 
558                 gr_scanline( LHX(98), LHX(106), LHY(42) );
559                 gr_scanline( LHX(120), LHX(128), LHY(42) );
560                 gr_pixel( LHX(98), LHY(43) );                                           
561                 gr_pixel( LHX(98), LHY(44) );                                           
562                 gr_pixel( LHX(128), LHY(43) );                                          
563                 gr_pixel( LHX(128), LHY(44) );                                          
564                 
565                 gr_string( LHX(109), LHY(40), "OR" );
566
567                 gr_scanline( LHX(253), LHX(261), LHY(42) );
568                 gr_scanline( LHX(274), LHX(283), LHY(42) );
569                 gr_pixel( LHX(253), LHY(43) );                                          
570                 gr_pixel( LHX(253), LHY(44) );                                          
571                 gr_pixel( LHX(283), LHY(43) );                                          
572                 gr_pixel( LHX(283), LHY(44) );                                          
573
574                 gr_string( LHX(264), LHY(40), "OR" );
575
576         } if ( items == kc_other )      {
577                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
578                 gr_setcolor( BM_XRGB(31,27,6) );
579                 gr_scanline( LHX(18), LHX(60), LHY(119+5) );
580                 gr_scanline( LHX(102), LHX(144), LHY(119+5) );
581                 gr_string( LHX(63), LHY(117+5), TXT_CONTROL_JOYSTICK );
582                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
583                 gr_string( LHX(84), LHY(129), TXT_AXIS );
584                 gr_string( LHX(110), LHY(129), TXT_INVERT );
585
586                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
587                 gr_setcolor( BM_XRGB(31,27,6) );
588                 gr_scanline( LHX(178), LHX(226), LHY(119+5) );
589                 gr_scanline( LHX(256), LHX(304), LHY(119+5) );
590                 gr_string( LHX(229), LHY(117+5), TXT_CONTROL_MOUSE );
591                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
592                 gr_string( LHX(244), LHY(129), TXT_AXIS );
593                 gr_string( LHX(270), LHY(129), TXT_INVERT );
594         }
595         else if ( items == kc_d2x )
596         {
597                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
598                 gr_setcolor( BM_XRGB(31,27,6) );
599
600                 gr_scanline( LHX(98), LHX(106), LHY(42) );
601                 gr_scanline( LHX(120), LHX(128), LHY(42) );
602                 gr_pixel( LHX(98), LHY(43) );
603                 gr_pixel( LHX(98), LHY(44) );
604                 gr_pixel( LHX(128), LHY(43) );
605                 gr_pixel( LHX(128), LHY(44) );
606
607                 gr_string(LHX(109), LHY(40), "OR");
608         }
609
610         for (i=0; i<nitems; i++ )       {
611                 kc_drawitem( &items[i], 0 );
612         }
613
614         citem = 0;
615         while(items[citem].id == -1)
616                 citem++;
617         kc_drawitem( &items[citem], 1 );
618
619         newmenu_show_cursor();
620
621 #ifdef NEWMENU_MOUSE
622         mouse_state = omouse_state = 0;
623 #endif
624
625         while(1)                {
626         //      Windows addendum to allow for kconfig input.
627                 vid_update();
628
629                 //see if redbook song needs to be restarted
630                 songs_check_redbook_repeat();
631
632                 k = newmenu_inkey();
633
634 #ifdef NEWMENU_MOUSE
635                 omouse_state = mouse_state;
636                 mouse_state = mouse_button_state(0);
637 #endif
638
639                 if ( !time_stopped ) {
640                         #ifdef NETWORK
641                         if (multi_menu_poll() == -1)
642                                 k = -2;
643                         #endif
644                 }
645                 ocitem = citem;
646                 switch( k )     {
647                 case KEY_BACKSP:
648                         Int3();
649                         break;
650                 case KEY_COMMAND+KEY_SHIFTED+KEY_3:
651                 case KEY_PRINT_SCREEN:
652                         save_screen_shot(0);
653                         break;                                                  
654                 case KEY_CTRLED+KEY_D:
655                         items[citem].value = 255;
656                         kc_drawitem( &items[citem], 1 );
657                         break;
658                 case KEY_CTRLED+KEY_R:  
659                         if ( items == kc_keyboard )
660                                 for (i = 0; i < NUM_KEY_CONTROLS; i++) {
661                                         items[i].value = default_kc_keyboard_settings[i];
662                                         kc_drawitem( &items[i], 0 );
663                                 }
664                         else if ( items == kc_other )
665                                 for (i = 0; i < NUM_OTHER_CONTROLS; i++) {
666                                         items[i].value = default_kc_other_settings[i];
667                                         kc_drawitem( &items[i], 0 );
668                                 }
669                         else if ( items == kc_d2x )
670                                 for (i = 0; i < NUM_D2X_CONTROLS; i++)
671                                 {
672                                         items[i].value = default_kc_d2x_settings[i];
673                                         kc_drawitem( &items[i], 0 );
674                                 }
675                         kc_drawitem( &items[citem], 1 );
676                         break;
677                 case KEY_DELETE:
678                         items[citem].value=255;
679                         kc_drawitem( &items[citem], 1 );
680                         break;
681                 case KEY_UP:            
682                 case KEY_PAD8:
683 #ifdef TABLE_CREATION
684                         if (items[citem].u==-1) items[citem].u=find_next_item_up( items,nitems, citem);
685 #endif
686                         citem = items[citem].u; 
687                         break;
688                 
689                 case KEY_DOWN:  
690                 case KEY_PAD2:
691 #ifdef TABLE_CREATION
692                         if (items[citem].d==-1) items[citem].d=find_next_item_down( items,nitems, citem);
693 #endif
694                         citem = items[citem].d; 
695                         break;
696                 case KEY_LEFT:  
697                 case KEY_PAD4:
698 #ifdef TABLE_CREATION
699                         if (items[citem].l==-1) items[citem].l=find_next_item_left( items,nitems, citem);
700 #endif
701                         citem = items[citem].l; 
702                         break;
703                 case KEY_RIGHT:         
704                 case KEY_PAD6:
705 #ifdef TABLE_CREATION
706                         if (items[citem].r==-1) items[citem].r=find_next_item_right( items,nitems, citem);
707 #endif
708                         citem = items[citem].r; 
709                         break;
710                 case KEY_ENTER: 
711                 case KEY_PADENTER:      
712                         switch( items[citem].type )     {
713                         case BT_KEY:            kc_change_key( &items[citem] ); break;
714                         case BT_MOUSE_AXIS:     kc_change_mouseaxis( &items[citem] ); break;
715                         case BT_JOY_AXIS:       kc_change_joyaxis( &items[citem] ); break;
716                         case BT_INVERT:         kc_change_invert( &items[citem] ); break;
717                         }
718                         break;
719                 //the following case added by WraithX on 11/22/00 to work around the weird joystick bug...
720                 case KEY_SPACEBAR:
721                         switch(items[citem].type)
722                         {
723                         case BT_JOY_AXIS:
724                                 kc_next_joyaxis(&items[citem]);
725                                 break;
726                         }
727                         break;
728                 //end addition by WraithX
729                 case -2:        
730                 case KEY_ESC:
731                         grd_curcanv->cv_font    = save_font;
732
733                         gr_set_current_canvas( save_canvas );
734                         keyd_repeat = old_keyd_repeat;
735                         game_flush_inputs();
736                         newmenu_hide_cursor();
737                         if (time_stopped)
738                                 start_time();
739                         return;
740 #ifdef TABLE_CREATION
741                 case KEY_DEBUGGED+KEY_SHIFTED+KEY_2:
742                 case KEY_DEBUGGED+KEY_F12:      {
743                         FILE * fp;
744                         int j;
745
746                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
747                                 kc_keyboard[i].u = find_next_item_up( kc_keyboard,NUM_KEY_CONTROLS, i);
748                                 kc_keyboard[i].d = find_next_item_down( kc_keyboard,NUM_KEY_CONTROLS, i);
749                                 kc_keyboard[i].l = find_next_item_left( kc_keyboard,NUM_KEY_CONTROLS, i);
750                                 kc_keyboard[i].r = find_next_item_right( kc_keyboard,NUM_KEY_CONTROLS, i);
751                         }
752                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
753                                 kc_other[i].u = find_next_item_up( kc_other,NUM_OTHER_CONTROLS, i);
754                                 kc_other[i].d = find_next_item_down( kc_other,NUM_OTHER_CONTROLS, i);
755                                 kc_other[i].l = find_next_item_left( kc_other,NUM_OTHER_CONTROLS, i);
756                                 kc_other[i].r = find_next_item_right( kc_other,NUM_OTHER_CONTROLS, i);
757                         }
758                         for (i = 0; i < NUM_D2X_CONTROLS; i++ ) {
759                                 kc_d2x[i].u = find_next_item_up( kc_d2x, NUM_D2X_CONTROLS, i);
760                                 kc_d2x[i].d = find_next_item_down( kc_d2x, NUM_D2X_CONTROLS, i);
761                                 kc_d2x[i].l = find_next_item_left( kc_d2x, NUM_D2X_CONTROLS, i);
762                                 kc_d2x[i].r = find_next_item_right( kc_d2x, NUM_D2X_CONTROLS, i);
763                         }
764                         fp = stderr; //fopen( "kconfig.cod", "wt" );
765
766                         fprintf( fp, "kc_item kc_keyboard[NUM_KEY_CONTROLS] = {\n" );
767                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
768                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
769                                         kc_keyboard[i].id, kc_keyboard[i].x, kc_keyboard[i].y, kc_keyboard[i].w1, kc_keyboard[i].w2,
770                                         kc_keyboard[i].u, kc_keyboard[i].d, kc_keyboard[i].l, kc_keyboard[i].r,
771                                         34, kc_keyboard[i].text, 34, btype_text[kc_keyboard[i].type] );
772                         }
773                         fprintf( fp, "};\n\n" );
774                         fprintf( fp, "ubyte default_kc_keyboard_settings[NUM_KEY_CONTROLS] = " );
775                         fprintf( fp, "{0x%x", kc_keyboard[0].value );
776                         for (j = 1; j < NUM_KEY_CONTROLS; j++)
777                                 fprintf( fp, ",0x%x", kc_keyboard[j].value );
778                         fprintf( fp, "};\n\n" );
779
780                         fprintf( fp, "kc_item kc_other[NUM_OTHER_CONTROLS] = {\n" );
781                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
782                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n",
783                                         kc_other[i].id, kc_other[i].x, kc_other[i].y, kc_other[i].w1, kc_other[i].w2,
784                                         kc_other[i].u, kc_other[i].d, kc_other[i].l, kc_other[i].r,
785                                         34, kc_other[i].text, 34, btype_text[kc_other[i].type] );
786                         }
787                         fprintf( fp, "};\n\n" );
788                         fprintf( fp, "ubyte default_kc_other_settings[NUM_OTHER_CONTROLS] = " );
789                         fprintf( fp, "{0x%x", kc_other[0].value );
790                         for (j = 1; j < NUM_OTHER_CONTROLS; j++)
791                                 fprintf( fp, ",0x%x", kc_other[j].value );
792                         fprintf( fp, "};\n" );
793
794                         fprintf( fp, "kc_item kc_d2x[NUM_D2X_CONTROLS] = {\n" );
795                         for (i=0; i<NUM_D2X_CONTROLS; i++ )     {
796                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n",
797                                                 kc_d2x[i].id, kc_d2x[i].x, kc_d2x[i].y, kc_d2x[i].w1, kc_d2x[i].w2,
798                                                 kc_d2x[i].u, kc_d2x[i].d, kc_d2x[i].l, kc_d2x[i].r,
799                                                 34, kc_d2x[i].text, 34, btype_text[kc_d2x[i].type] );
800                         }
801                         fprintf( fp, "};\n\n" );
802                         fprintf( fp, "ubyte default_kc_d2x_settings[NUM_D2X_CONTROLS] = " );
803                         fprintf( fp, "{0x%x", kc_d2x[0].value );
804                         for (j = 1; j < NUM_D2X_CONTROLS; j++)
805                                 fprintf( fp, ",0x%x", kc_d2x[j].value );
806                         fprintf( fp, "};\n" );
807
808                         fclose(fp);
809
810                         }
811                         break;
812 #endif
813                 }
814
815 #ifdef NEWMENU_MOUSE
816                 if ( (mouse_state && !omouse_state) || (mouse_state && omouse_state) ) {
817                         int item_height;
818                         
819                         mouse_get_pos(&mx, &my);
820                         for (i=0; i<nitems; i++ )       {
821                                 item_height = get_item_height( &items[i] );
822                                 x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[i].x) + LHX(items[i].w1);
823                                 x2 = x1 + LHX(items[i].w2);
824                                 y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[i].y);
825                                 y2 = y1 + LHX(item_height);
826                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
827                                         citem = i;
828                                         break;
829                                 }
830                         }
831                 }
832                 else if ( !mouse_state && omouse_state ) {
833                         int item_height;
834                         
835                         mouse_get_pos(&mx, &my);
836                         item_height = get_item_height( &items[citem] );
837                         x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[citem].x) + LHX(items[citem].w1);
838                         x2 = x1 + LHX(items[citem].w2);
839                         y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[citem].y);
840                         y2 = y1 + LHY(item_height);
841                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
842                                 newmenu_hide_cursor();
843                                 switch( items[citem].type )     {
844                                 case BT_KEY:                            kc_change_key( &items[citem] ); break;
845                                 case BT_MOUSE_AXIS:             kc_change_mouseaxis( &items[citem] ); break;
846                                 case BT_JOY_AXIS:               kc_change_joyaxis( &items[citem] ); break;
847                                 case BT_INVERT:                         kc_change_invert( &items[citem] ); break;
848                                 }
849                                 newmenu_show_cursor();
850                         } else {
851                                 x1 = grd_curcanv->cv_bitmap.bm_x + close_x + LHX(1);
852                                 x2 = x1 + close_size - LHX(1);
853                                 y1 = grd_curcanv->cv_bitmap.bm_y + close_y + LHX(1);
854                                 y2 = y1 + close_size - LHX(1);
855                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
856                                         grd_curcanv->cv_font    = save_font;
857                                         gr_set_current_canvas( save_canvas );
858                                         keyd_repeat = old_keyd_repeat;
859                                         game_flush_inputs();
860                                         newmenu_hide_cursor();
861                                         if (time_stopped)
862                                                 start_time();
863                                         return;
864                                 }
865                         }
866
867                 }
868 #endif // NEWMENU_MOUSE
869
870                 if (ocitem!=citem)      {
871                         newmenu_hide_cursor();
872                         kc_drawitem( &items[ocitem], 0 );
873                         kc_drawitem( &items[citem], 1 );
874                         newmenu_show_cursor();
875                 }
876         }
877 }
878
879
880 void kc_drawitem( kc_item *item, int is_current )
881 {
882         int x, w, h, aw;
883         char btext[16];
884
885         if (is_current)
886                 gr_set_fontcolor( BM_XRGB(20,20,29), -1 );
887         else
888                 gr_set_fontcolor( BM_XRGB(15,15,24), -1 );
889    gr_string( LHX(item->x), LHY(item->y), item->text );
890
891         if (item->value==255) {
892                 strcpy( btext, "" );
893         } else {
894                 switch( item->type )    {
895                         case BT_KEY:
896                                 strncpy( btext, key_text[item->value], 10 ); break;
897                         case BT_MOUSE_AXIS:
898                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
899                         case BT_JOY_AXIS:
900 #ifdef USE_LINUX_JOY
901                                 sprintf(btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number(item->value));
902 #elif defined(SDL_INPUT)
903                                 if (joyaxis_text[item->value])
904                                         strncpy(btext, joyaxis_text[item->value], 10);
905                                 else
906                                         sprintf(btext, "AXIS%2d", item->value + 1);
907 #else
908                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
909 #endif
910                                 break;
911                         case BT_INVERT:
912                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
913                 }
914         }
915         if (item->w1) {
916                 gr_get_string_size(btext, &w, &h, &aw  );
917
918                 if (is_current)
919                         gr_setcolor( BM_XRGB(21,0,24) );
920                 else
921                         gr_setcolor( BM_XRGB(16,0,19) );
922                 gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
923                 
924                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
925
926                 x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
927         
928                 gr_string( x, LHY(item->y), btext );
929         }
930 }
931
932
933 static int looper=0;
934
935 void kc_drawquestion( kc_item *item )
936 {
937         int c, x, w, h, aw;
938
939         gr_get_string_size("?", &w, &h, &aw  );
940
941         c = BM_XRGB(21,0,24);
942
943         //@@gr_setcolor( gr_fade_table[fades[looper]*256+c] );
944         gr_setcolor(BM_XRGB(21*fades[looper]/31,0,24*fades[looper]/31));
945         looper++;
946         if (looper>63) looper=0;
947
948         gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
949         
950         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
951
952         x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
953    
954         gr_string( x, LHY(item->y), "?" );
955         vid_update();
956 }
957
958 void kc_change_key( kc_item * item )
959 {
960         int i,n,f,k;
961         ubyte keycode;
962
963         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
964         
965         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_KEY );
966
967         game_flush_inputs();
968         keycode=255;
969         k=255;
970         
971         while( (k!=KEY_ESC) && (keycode==255) ) 
972         {                               
973                 #ifdef NETWORK
974                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
975                         multi_menu_poll();
976                 #endif
977 //              if ( Game_mode & GM_MULTI )
978 //                      GameLoop( 0, 0 );                               // Continue
979                 k = key_inkey();
980                 timer_delay(f0_1/10);
981                 kc_drawquestion( item );
982         
983                 for (i=0; i<256; i++ )  {
984                         if (keyd_pressed[i] && (strlen(key_text[i])>0)) {
985                                 f = 0;
986                                 for (n=0; n<sizeof(system_keys); n++ )
987                                         if ( system_keys[n] == i )
988                                                 f=1;
989                                 if (!f) 
990                                         keycode=i;
991                         }
992                 }
993         }
994
995         if (k!=KEY_ESC) {
996                 for (i=0; i<Num_items; i++ )    {
997                         n = (int)(item - All_items);
998                         if ( (i!=n) && (All_items[i].type==BT_KEY) && (All_items[i].value==keycode) )           {
999                                 All_items[i].value = 255;
1000                                 kc_drawitem( &All_items[i], 0 );
1001                         }
1002                 }
1003                 item->value = keycode;
1004         }
1005         kc_drawitem( item, 1 );
1006
1007         gr_set_fontcolor( BM_XRGB(28,28,28), BM_XRGB(0,0,0) );
1008
1009         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1010
1011         game_flush_inputs();
1012
1013 }
1014
1015
1016 // the following function added by WraithX on 11/22/00 to work around the weird joystick bug... - modified my Matt Mueller to skip already allocated axes
1017 void kc_next_joyaxis(kc_item *item)
1018 {
1019         int n, i, k, max, tries;
1020         ubyte code = 0;
1021
1022         k = 255;
1023         n = 0;
1024         i = 0;
1025
1026         // I modelled this ifdef after the code in the kc_change_joyaxis method.
1027         // So, if somethin's not workin here, it might not be workin there either.
1028         max = JOY_MAX_AXES;
1029         tries = 1;
1030         code = (item->value + 1) % max;
1031
1032         if (code != 255)
1033         {
1034                 for (i = 0; i < Num_items; i++)
1035                 {
1036                         n = (int)(item - All_items);
1037                         if ((i != n) && (All_items[i].type == BT_JOY_AXIS) && (All_items[i].value == code))
1038                         {
1039                                 if (tries > max)
1040                                         return; // all axes allocated already
1041                                 i = -1; // -1 so the i++ will push back to 0
1042                                 code = (item->value + ++tries) % max; // try next axis
1043                         }//end if
1044                 }//end for
1045
1046                 item->value = code;
1047         }//end if
1048
1049         kc_drawitem(item, 1);
1050         nm_restore_background(0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h);
1051         game_flush_inputs();
1052
1053 }//method kc_next_joyaxis
1054 //end addition by WraithX
1055
1056
1057 void kc_change_joyaxis( kc_item * item )
1058 {
1059         int axis[JOY_MAX_AXES];
1060         int old_axis[JOY_MAX_AXES];
1061         int numaxis = joy_num_axes;
1062         int n,i,k;
1063         ubyte code;
1064
1065         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1066         
1067         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_JOY_AXIS );
1068
1069         game_flush_inputs();
1070         code=255;
1071         k=255;
1072
1073         joystick_read_raw_axis( JOY_ALL_AXIS, old_axis );
1074
1075         while( (k!=KEY_ESC) && (code==255))     
1076         {                               
1077                 #ifdef NETWORK
1078                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1079                         multi_menu_poll();
1080                 #endif
1081 //              if ( Game_mode & GM_MULTI )
1082 //                      GameLoop( 0, 0 );                               // Continue
1083                 k = newmenu_inkey();
1084                 timer_delay(f0_1/10);
1085
1086                 if (k == KEY_PRINT_SCREEN)
1087                         save_screen_shot(0);
1088
1089                 kc_drawquestion( item );
1090
1091                 joystick_read_raw_axis( JOY_ALL_AXIS, axis );
1092
1093                 for (i=0; i<numaxis; i++ )      {
1094                         if ( abs(axis[i]-old_axis[i])>100 )
1095                         {
1096                                 code = i;
1097                                 con_printf(CON_DEBUG, "Axis Movement detected: Axis %i\n", i);
1098                         }
1099                         //old_axis[i] = axis[i];
1100                 }
1101                 for (i=0; i<Num_items; i++ )    
1102                  {
1103                         n = (int)(item - All_items);
1104                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) 
1105                                 code = 255;
1106                  }
1107         
1108         }
1109         if (code!=255)  {
1110                 for (i=0; i<Num_items; i++ )    {
1111                         n = (int)(item - All_items);
1112                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) {
1113                                 All_items[i].value = 255;
1114                                 kc_drawitem( &All_items[i], 0 );
1115                         }
1116                 }
1117
1118                 item->value = code;                                      
1119         }
1120         kc_drawitem( item, 1 );
1121         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1122         game_flush_inputs();
1123
1124 }
1125
1126 void kc_change_mouseaxis( kc_item * item )
1127 {
1128         int i,n,k;
1129         ubyte code;
1130         int dx, dy, dz;
1131
1132         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1133         
1134         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_MSE_AXIS );
1135
1136         game_flush_inputs();
1137         code=255;
1138         k=255;
1139
1140         mouse_get_delta( &dx, &dy, &dz );
1141
1142         while( (k!=KEY_ESC) && (code==255))     
1143         {                               
1144                 #ifdef NETWORK
1145                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1146                         multi_menu_poll();
1147                 #endif
1148 //              if ( Game_mode & GM_MULTI )
1149 //                      GameLoop( 0, 0 );                               // Continue
1150                 k = newmenu_inkey();
1151                 timer_delay(f0_1/10);
1152
1153                 if (k == KEY_PRINT_SCREEN)
1154                         save_screen_shot(0);
1155
1156                 kc_drawquestion( item );
1157
1158                 mouse_get_delta( &dx, &dy, &dz );
1159                 if ( abs(dx)>20 ) code = 0;
1160                 if ( abs(dy)>20 ) code = 1;
1161                 if ( abs(dz)>20 ) code = 2;
1162         }
1163         if (code!=255)  {
1164                 for (i=0; i<Num_items; i++ )    {
1165                         n = (int)(item - All_items);
1166                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_AXIS) && (All_items[i].value==code) )               {
1167                                 All_items[i].value = 255;
1168                                 kc_drawitem( &All_items[i], 0 );
1169                         }
1170                 }
1171                 item->value = code;
1172         }
1173         kc_drawitem( item, 1 );
1174         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1175         game_flush_inputs();
1176
1177 }
1178
1179
1180 void kc_change_invert( kc_item * item )
1181 {
1182         game_flush_inputs();
1183
1184         if (item->value)
1185                 item->value = 0;
1186         else 
1187                 item->value = 1;
1188
1189         kc_drawitem( item, 1 );
1190
1191 }
1192
1193 #include "screens.h"
1194
1195 void kconfig(int n, char * title)
1196 {
1197         int i, j;
1198         grs_bitmap *save_bm;
1199
1200         set_screen_mode( SCREEN_MENU );
1201
1202         kc_set_controls();
1203
1204         //save screen
1205         save_bm = gr_create_bitmap( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
1206         Assert( save_bm != NULL );
1207         
1208         gr_bm_bitblt(grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_w, 
1209                                         0, 0, 0, 0, &grd_curcanv->cv_bitmap, save_bm );
1210
1211         switch(n)       {
1212         case 0:kconfig_sub( kc_keyboard, NUM_KEY_CONTROLS, title );break;
1213         case 1:kconfig_sub( kc_other, NUM_OTHER_CONTROLS, title );break;
1214         case 2:kconfig_sub( kc_d2x, NUM_D2X_CONTROLS, title ); break;
1215         default:
1216                 Int3();
1217                 return;
1218         }
1219
1220         //restore screen
1221         gr_bitmap(0, 0, save_bm);
1222         gr_free_bitmap(save_bm);
1223
1224 #if 0 // set_screen_mode always calls this later... right?
1225         reset_cockpit();                //force cockpit redraw next time
1226 #endif
1227
1228         // Update save values...
1229         
1230         for (j=0; j<256; j++)
1231                 if (key_binding(j)) {
1232                         for (i = 0; i < NUM_KEY_CONTROLS; i++)
1233                                 if (strlen(key_text[j]) && !stricmp(key_binding(j), kc_key_bind_text[i])) {
1234                                         cmd_appendf("unbind %s", key_text[j]);
1235                                         break;
1236                                 }
1237                         for (i = 0; i < NUM_D2X_CONTROLS; i++)
1238                                 if (kc_d2x[i].type == BT_KEY && strlen(key_text[j]) && !stricmp(key_binding(j), kc_d2x[i].text)) {
1239                                         cmd_appendf("unbind %s", key_text[j]);
1240                                         break;
1241                                 }
1242                 }
1243
1244         for (i=0; i<NUM_KEY_CONTROLS; i++ )
1245                 if (kc_keyboard[i].value != 255 && key_text[kc_keyboard[i].value])
1246                         cmd_appendf("bind \"%s\" \"%s\"", key_text[kc_keyboard[i].value], kc_key_bind_text[i]);
1247
1248         for (i = 0; i < NUM_D2X_CONTROLS; i++)
1249                 if (kc_d2x[i].value != 255 && strlen(key_text[kc_d2x[i].value]))
1250                         cmd_appendf("bind \"%s\" \"%s\"", key_text[kc_d2x[i].value], kc_d2x[i].text);
1251
1252         for (i = 0; i < 6; i++) {
1253                 cvar_setint(&joy_advaxes[i], AXIS_NONE);
1254                 cvar_setint(&joy_invert[i], 0);
1255         }
1256         for (i = 0; i < 3; i++) {
1257                 cvar_setint(&mouse_axes[i], AXIS_NONE);
1258                 cvar_setint(&mouse_invert[i], 0);
1259         }
1260         for (i = 0; i < NUM_OTHER_CONTROLS; i++) {
1261                 if (kc_other[i].type == BT_JOY_AXIS && kc_other[i].value != 255) {
1262                         cvar_setint(&joy_advaxes[kc_other[i].value], kc_other_axismap[i]);
1263                         cvar_setint(&joy_invert[kc_other[i].value], kc_other[i+1].value);
1264                 }
1265                 if (kc_other[i].type == BT_MOUSE_AXIS && kc_other[i].value != 255) {
1266                         cvar_setint(&mouse_axes[kc_other[i].value], kc_other_axismap[i]);
1267                         cvar_setint(&mouse_invert[kc_other[i].value], kc_other[i+1].value);
1268                 }
1269         }
1270
1271         cmd_queue_process();
1272 }
1273
1274
1275 fix Last_angles_p = 0;
1276 fix Last_angles_b = 0;
1277 fix Last_angles_h = 0;
1278 ubyte Last_angles_read = 0;
1279
1280 extern int                      VR_sensitivity;
1281                                                 
1282 int VR_sense_range[3] = { 25, 50, 75 };
1283
1284 #if 0 // unused
1285 read_head_tracker()
1286 {
1287         fix yaw, pitch, roll;
1288         int buttons;
1289
1290 //------ read vfx1 helmet --------
1291         if (vfx1_installed) {
1292                 vfx_get_data(&yaw,&pitch,&roll,&buttons);
1293         } else if (iglasses_headset_installed)  {
1294                 iglasses_read_headset( &yaw, &pitch, &roll );
1295         } else if (Victor_headset_installed)   {
1296                 victor_read_headset_filtered( &yaw, &pitch, &roll );
1297         } else {
1298                 return;
1299         }
1300
1301         Use_player_head_angles = 0;
1302         if ( Last_angles_read ) {
1303                 fix yaw1 = yaw;
1304                 
1305                 yaw1 = yaw;
1306                 if ( (Last_angles_h < (F1_0/4) ) && (yaw > ((F1_0*3)/4) ) )     
1307                         yaw1 -= F1_0;
1308                 else if ( (yaw < (F1_0/4) ) && (Last_angles_h > ((F1_0*3)/4) ) )        
1309                         yaw1 += F1_0;
1310         
1311                 Controls.pitch_time     += fixmul((pitch- Last_angles_p)*VR_sense_range[VR_sensitivity],FrameTime);
1312                 Controls.heading_time+= fixmul((yaw1 -  Last_angles_h)*VR_sense_range[VR_sensitivity],FrameTime);
1313                 Controls.bank_time      += fixmul((roll - Last_angles_b)*VR_sense_range[VR_sensitivity],FrameTime);
1314         }
1315         Last_angles_read = 1;
1316         Last_angles_p = pitch;
1317         Last_angles_h = yaw;
1318         Last_angles_b = roll;
1319 }
1320 #endif
1321
1322
1323 void kc_set_controls()
1324 {
1325         int i, j;
1326
1327         for (i=0; i<NUM_KEY_CONTROLS; i++ )
1328                 kc_keyboard[i].value = 255;
1329
1330         for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
1331                 if (kc_other[i].type == BT_INVERT)
1332                         kc_other[i].value = 0;
1333                 else
1334                         kc_other[i].value = 255;
1335         }
1336
1337         for (i=0; i<NUM_D2X_CONTROLS; i++ )
1338                 kc_d2x[i].value = 255;
1339
1340         for (j = 0; j < 256; j++)
1341                 if (key_binding(j) && strlen(key_text[j])) {
1342                         for (i = 0; i < NUM_KEY_CONTROLS; i++)
1343                                 if (kc_keyboard[i].value == 255
1344                                         && !stricmp(key_binding(j), kc_key_bind_text[i])) {
1345                                         kc_keyboard[i].value = j;
1346                                         break;
1347                                 }
1348                 }
1349
1350         for(j = 0; j < 256; j++)
1351                 if (key_binding(j) && strlen(key_text[j])) {
1352                         for (i = 0; i < NUM_D2X_CONTROLS; i++)
1353                                 if (kc_d2x[i].value == 255
1354                                         && !stricmp(key_binding(j), kc_d2x[i].text)) {
1355                                         kc_d2x[i].value = j;
1356                                         break;
1357                                 }
1358                 }
1359
1360         for (i = 0; i < 6; i++) {
1361                 int inv = joy_invert[i].intval;
1362                 switch (joy_advaxes[i].intval) {
1363                         case AXIS_PITCH:     kc_other[ 0].value = i; kc_other[ 1].value = inv; break;
1364                         case AXIS_TURN:      kc_other[ 2].value = i; kc_other[ 3].value = inv; break;
1365                         case AXIS_LEFTRIGHT: kc_other[ 4].value = i; kc_other[ 5].value = inv; break;
1366                         case AXIS_UPDOWN:    kc_other[ 6].value = i; kc_other[ 7].value = inv; break;
1367                         case AXIS_BANK:      kc_other[ 8].value = i; kc_other[ 9].value = inv; break;
1368                         case AXIS_THROTTLE:  kc_other[10].value = i; kc_other[11].value = inv; break;
1369                         case AXIS_NONE:      break;
1370                         default:
1371                                 Int3();
1372                                 break;
1373                 }
1374         }
1375
1376         for (i = 0; i < 3; i++) {
1377                 int inv = mouse_invert[i].intval;
1378                 switch (mouse_axes[i].intval) {
1379                         case AXIS_PITCH:     kc_other[12].value = i; kc_other[13].value = inv; break;
1380                         case AXIS_TURN:      kc_other[14].value = i; kc_other[15].value = inv; break;
1381                         case AXIS_LEFTRIGHT: kc_other[16].value = i; kc_other[17].value = inv; break;
1382                         case AXIS_UPDOWN:    kc_other[18].value = i; kc_other[19].value = inv; break;
1383                         case AXIS_BANK:      kc_other[20].value = i; kc_other[21].value = inv; break;
1384                         case AXIS_THROTTLE:  kc_other[22].value = i; kc_other[23].value = inv; break;
1385                         case AXIS_NONE:      break;
1386                         default:
1387                                 Int3();
1388                                 break;
1389                 }
1390         }
1391 }
1392
1393 #if 0 // no mac support for vr headset
1394
1395 void kconfig_center_headset()
1396 {
1397         if (vfx1_installed)
1398                 vfx_center_headset();
1399 //      } else if (iglasses_headset_installed)  {
1400 //      } else if (Victor_headset_installed)   {
1401 //      } else {
1402 //      }
1403
1404 }
1405
1406 #endif // end of #if for kconfig_center_headset