]> icculus.org git repositories - btb/d2x.git/blob - main/kconfig.c
fix for non-linux joysticks
[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 #ifdef RCS
16 static char rcsid[] = "$Id: kconfig.c,v 1.5 2001-01-29 14:08:24 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20
21 #ifdef WINDOWS
22 #include "desw.h"
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30
31 #include "pa_enabl.h"                   //$$POLY_ACC
32 #include "error.h"
33 #include "pstypes.h"
34 #include "gr.h"
35 #include "mono.h"
36 #include "key.h"
37 #include "palette.h"
38 #include "game.h"
39 #include "gamefont.h"
40 #include "iff.h"
41 #include "u_mem.h"
42 #include "joy.h"
43 #include "mouse.h"
44 #include "kconfig.h"
45 #include "gauges.h"
46 #include "joydefs.h"
47 #include "songs.h"
48 #include "render.h"
49 #include "digi.h"
50 #include "newmenu.h"
51 #include "endlevel.h"
52 #include "multi.h"
53 #include "timer.h"
54 #include "text.h"
55 #include "player.h"
56 #include "menu.h"
57 #include "automap.h"
58 #include "args.h"
59 #include "lighting.h"
60 #include "ai.h"
61 #include "cntrlcen.h"
62 #if defined (TACTILE)
63  #include "tactile.h"
64 #endif
65
66 #if defined(POLY_ACC)
67 #include "poly_acc.h"
68 #endif
69
70 #include "d_delay.h"
71 #include "collide.h"
72
73 #ifdef __ENV_LINUX__
74 #include "linux_joystick.h"
75 #endif
76
77 ubyte ExtGameStatus=1;
78
79 vms_vector ExtForceVec;
80 vms_matrix ExtApplyForceMatrix;
81
82 int ExtJoltInfo[3]={0,0,0};
83 int ExtXVibrateInfo[2]={0,0};
84 int ExtYVibrateInfo[2]={0,0};
85 ubyte ExtXVibrateClear=0;
86 ubyte ExtYVibrateClear=0;
87
88 #define TABLE_CREATION 1
89
90 // Array used to 'blink' the cursor while waiting for a keypress.
91 byte 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 };
92
93 //char * invert_text[2] = { "N", "Y" };
94 //char * joybutton_text[28] = { "BTN 1", "BTN 2", "BTN 3", "BTN 4", "", "TRIG", "LEFT", "HAT \81", "RIGHT", "", "", "HAT \80", "MID", "", "", "HAT \7f", "", "", "", "HAT \82", "TRIG", "LEFT", "RIGHT", "", "UP","DOWN","LEFT", "RIGHT" };
95 //char * joyaxis_text[4] = { "X1", "Y1", "X2", "Y2" };
96 //char * mouseaxis_text[2] = { "L/R", "F/B" };
97 //char * mousebutton_text[3] = { "Left", "Right", "Mid" };
98
99 int invert_text[2] = { TNUM_N, TNUM_Y };
100
101 #ifdef WINDOWS
102         int joybutton_text[28] = 
103         { TNUM_BTN_1, TNUM_BTN_2, TNUM_BTN_3, TNUM_BTN_4,
104           -1, -1, -1, -1,
105           -1, -1, -1, -1,
106           -1, -1, -1, -1,
107           TNUM_HAT_L, TNUM_HAT_R, TNUM_HAT_U, TNUM_HAT_D,
108           -1, -1, -1, -1,
109           -1, -1, -1, -1
110         };
111         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
112 #else
113         int joybutton_text[28] = 
114         { TNUM_BTN_1, TNUM_BTN_2, TNUM_BTN_3, TNUM_BTN_4,
115           -1, TNUM_TRIG, TNUM_LEFT, TNUM_HAT_L,
116          TNUM_RIGHT, -1, TNUM_HAT2_D, TNUM_HAT_R,
117          TNUM_MID, -1, TNUM_HAT2_R, TNUM_HAT_U,
118          TNUM_HAT2_L, -1, TNUM_HAT2_U, TNUM_HAT_D,
119          TNUM_TRIG, TNUM_LEFT, TNUM_RIGHT, -1, 
120          TNUM_UP, TNUM_DOWN, TNUM_LEFT, TNUM_RIGHT };
121
122         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
123 //      int joyaxis_text[4] = { TNUM_X1, TNUM_Y1, TNUM_X2, TNUM_Y2 };
124 #endif
125         
126 int mouseaxis_text[2] = { TNUM_L_R, TNUM_F_B };
127 #ifndef MACINTOSH
128 int mousebutton_text[3] = { TNUM_LEFT, TNUM_RIGHT, TNUM_MID };
129 #else
130 char *mousebutton_text[3] = { "Btn", "", "" };          // only one silly mouse button on the mac
131 #endif
132
133 #ifdef MACINTOSH
134 char * key_text[256] = {         
135 "","S","D","F","H","G","Z","X","C","V","","B","Q", "W", "E", "R",
136 "Y","T","1","2","3","4","6","5","=","9","7","-", "8", "0", "]", "O",
137 "U","[","I","P","RET","L","J","'","K", ";", "\\", ",", "/", "N", "M", ".",
138 "TAB","SPC","`","DEL","","ESC","","APL","SHIFT","CAPSL","OPTN","CTRL","","","","A",
139 "","PAD.","","PAD*","","PAD+","","NMLCK","","","","PAD/","ENTER","","PAD-","",
140 "","PAD=","PAD0","PAD1","PAD2","PAD3","PAD4","PAD5","PAD6","PAD7","","PAD8","PAD9","","","",
141 "F5","F6","F7","","F8","F9","","F11","","F13","","F14","","F10","","F12",
142 "","PAUSE","HELP","HOME","PGUP","DEL","","END","F2","","F1","LARW","RARW","DARW","UARW","",
143 "","","","","","","","","","","","","","","","",
144 "","","","","","","","","","","","","","","","",
145 "","","","","","","","","","","","","","","","",
146 "","","","","","","","","","","","","","","","",
147 "","","","","","","","","","","","","","","","",
148 "","","","","","","","","","","","","","","","",
149 "","","","","","","","","","","","","","","","",
150 "","","","","","","","","","","","","","","",""};
151 #else
152 #ifndef OGL
153 char * key_text[256] = {         \
154 "","ESC","1","2","3","4","5","6","7","8","9","0","-",                   \
155 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",                           \
156 "P","[","]","\83","LCTRL","A","S","D","F",        \
157 "G","H","J","K","L",";","'","`",        \
158 "LSHFT","\\","Z","X","C","V","B","N","M",",",      \
159 ".","/","RSHFT","PAD*","LALT","SPC",      \
160 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",        \
161 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",   \
162 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0", \
163 "PAD.","","","","F11","F12","","","","","","","","","",         \
164 "","","","","","","","","","","","","","","","","","","","",     \
165 "","","","","","","","","","","","","","","","","","","","",     \
166 "","","","","","","","","","","","","","","","","","",           \
167 "PAD\83","RCTRL","","","","","","","","","","","","","", \
168 "","","","","","","","","","","PAD/","","","RALT","",      \
169 "","","","","","","","","","","","","","HOME","\82","PGUP",     \
170 "","\81","","\7f","","END","\80","PGDN","INS",       \
171 "DEL","","","","","","","","","","","","","","","","","",     \
172 "","","","","","","","","","","","","","","","","","","","",     \
173 "","","","","","","" };
174 #endif /* OGL */
175 #endif
176
177 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_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS, KEY_EQUAL, KEY_PRINT_SCREEN };
178
179 //extern void GameLoop(int, int );
180
181 extern void transfer_energy_to_shield(fix);
182 extern void CyclePrimary(),CycleSecondary(),InitMarkerInput();
183 extern ubyte DefiningMarkerMessage;
184 extern char CybermouseActive;
185
186 #ifdef WINDOWS
187 extern int joydefsw_do_button();
188 extern int joydefsw_do_winjoybutton(int *axis);
189 extern joydefsw_win_joyselect(char *title);
190 #endif
191
192 control_info Controls;
193
194 fix Cruise_speed=0;
195
196 // macros for drawing lo/hi res kconfig screens (see scores.c as well)
197
198 #define LHX(x)          ((x)*(MenuHires?2:1))
199 #define LHY(y)          ((y)*(MenuHires?2.4:1))
200
201
202 #define BT_KEY                          0
203 #define BT_MOUSE_BUTTON         1
204 #define BT_MOUSE_AXIS           2
205 #define BT_JOY_BUTTON           3
206 #define BT_JOY_AXIS                     4
207 #define BT_INVERT                               5
208
209 char *btype_text[] = { "BT_KEY", "BT_MOUSE_BUTTON", "BT_MOUSE_AXIS", "BT_JOY_BUTTON", "BT_JOY_AXIS", "BT_INVERT" };
210
211 #define INFO_Y 28
212
213 typedef struct kc_item {
214         short id;                               // The id of this item
215         short x, y;                             
216         short w1;
217         short w2;
218         short u,d,l,r;
219         //short text_num1;
220         char *text;
221         ubyte type;
222         ubyte value;            // what key,button,etc
223 } kc_item;
224
225 int Num_items=28;
226 kc_item *All_items;
227
228 ubyte kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS];
229
230 //----------- WARNING!!!!!!! -------------------------------------------
231 // THESE NEXT FOUR BLOCKS OF DATA ARE GENERATED BY PRESSING DEL+F12 WHEN
232 // IN THE KEYBOARD CONFIG SCREEN.  BASICALLY, THAT PROCEDURE MODIFIES THE
233 // U,D,L,R FIELDS OF THE ARRAYS AND DUMPS THE NEW ARRAYS INTO KCONFIG.COD
234 //-------------------------------------------------------------------------
235 /*ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
236 {0xc8,0x48,0xd0,0x50,0xcb,0x4b,0xcd,0x4d,0x38,0xff,0xff,0x4f,0xff,0x51,0xff,0x4a,0xff,0x4e,0xff,0xff,0x10,0x47,0x12,0x49,0x1d,0x9d,0x39,0xff,0x21,0xff,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,0xff,0x0,0x0},
237 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
238 {0x5,0xc,0xff,0xff,0xff,0xff,0x7,0xf,0x13,0xb,0xff,0x6,0x8,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
239 {0x0,0x1,0xff,0xff,0x2,0xff,0x7,0xf,0x13,0xb,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
240 {0x3,0x0,0x1,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
241 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
242 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
243 };*/                                                                              
244
245 #ifndef MACINTOSH
246 ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
247 {0xc8,0x48,0xd0,0x50,0xcb,0x4b,0xcd,0x4d,0x38,0xff,0xff,0x4f,0xff,0x51,0xff,0x4a,0xff,0x4e,0xff,0xff,0x10,0x47,0x12,0x49,0x1d,0x9d,0x39,0xff,0x21,0xff,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,0xff,0x0,0x0},
248 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
249 {0x5,0xc,0xff,0xff,0xff,0xff,0x7,0xf,0x13,0xb,0xff,0x6,0x8,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0},
250 {0x0,0x1,0xff,0xff,0x2,0xff,0x7,0xf,0x13,0xb,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
251 {0x3,0x0,0x1,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
252 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
253 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
254 #ifdef WINDOWS
255 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
256 #endif
257 };
258
259
260
261 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
262         {  0, 15, 49, 71, 26, 55,  2, 55,  1,"Pitch forward", BT_KEY, 255 },
263         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
264         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
265         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
266         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
267         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
268         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
269         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
270         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
271         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
272         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
273         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
274         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
275         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
276         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
277         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
278         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
279         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
280         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
281         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
282         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
283         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
284         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
285         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
286         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
287         { 25,158, 49,112, 26, 54, 27, 24,  2,"Fire primary", BT_KEY, 255 },
288         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
289         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
290         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
291         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
292         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
293         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
294         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
295         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
296         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
297         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
298         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
299         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
300         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
301         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
302         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
303         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
304         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
305         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
306         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
307         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
308         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
309         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
310         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
311         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
312         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
313         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
314         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
315         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
316         { 54,158,171, 83, 26, 52, 56, 51, 55,"Energy->Shield", BT_KEY, 255 },
317         { 55,158,171,112, 26, 53,  0, 54,  0,"Energy->Shield", BT_KEY, 255 },
318    { 56,158,179,83,  26, 54,  0, 0,  0, "Toggle Bomb",  BT_KEY,255},
319 };
320 kc_item kc_joystick[NUM_OTHER_CONTROLS] = {
321         {  0, 25, 46, 85, 26, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
322         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
323         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
324         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
325         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
326         {  5,180, 46, 79, 26, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
327         {  6,180, 54, 79, 26,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
328         {  7,180, 62, 79, 26,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
329         {  8,180, 70, 79, 26,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
330         {  9,180, 78, 79, 26,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
331         { 10,180, 90, 79, 26,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
332         { 11,180, 98, 79, 26, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
333         { 12,180,106, 79, 26, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
334         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
335         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
336         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
337         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
338         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
339         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
340         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
341         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
342         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
343         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
344         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
345         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
346         { 25, 25,109, 85, 26,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
347         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
348         { 27, 25,117, 85, 26, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
349         { 28,180,114, 79, 26, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
350         { 29,180,122, 79, 26, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
351         { 30, 25,125, 85, 26, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
352 };
353 kc_item kc_superjoy[NUM_OTHER_CONTROLS] = {
354         {  0, 25, 46, 85, 26, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
355         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
356         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
357         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
358         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
359         {  5,180, 46, 79, 26, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
360         {  6,180, 54, 79, 26,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
361         {  7,180, 62, 79, 26,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
362         {  8,180, 70, 79, 26,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
363         {  9,180, 78, 79, 26,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
364         { 10,180, 90, 79, 26,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
365         { 11,180, 98, 79, 26, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
366         { 12,180,106, 79, 26, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
367         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
368         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
369         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
370         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
371         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
372         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
373         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
374         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
375         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
376         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
377         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
378         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
379         { 25, 25,109, 85, 26,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
380         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
381         { 27, 25,117, 85, 26, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
382         { 28,180,114, 79, 26, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
383         { 29,180,122, 79, 26, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
384         { 30, 25,125, 85, 26, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
385 };
386
387 kc_item kc_mouse[NUM_OTHER_CONTROLS] = {
388         {  0, 25, 46, 85, 26, 12,  1, 24,  5,"Fire primary", BT_MOUSE_BUTTON, 255 },
389         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_MOUSE_BUTTON, 255 },
390         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_MOUSE_BUTTON, 255 },
391         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_MOUSE_BUTTON, 255 },
392         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_MOUSE_BUTTON, 255 },
393         {  5,180, 46, 59, 26, 24,  6,  0,  1,"Slide on", BT_MOUSE_BUTTON, 255 },
394         {  6,180, 54, 59, 26,  5,  7,  1,  4,"Slide left", BT_MOUSE_BUTTON, 255 },
395         {  7,180, 62, 59, 26,  6,  8,  4, 26,"Slide right", BT_MOUSE_BUTTON, 255 },
396         {  8,180, 70, 59, 26,  7,  9, 26,  9,"Slide up", BT_MOUSE_BUTTON, 255 },
397         {  9,180, 78, 59, 26,  8, 10,  8,  2,"Slide down", BT_MOUSE_BUTTON, 255 },
398         { 10,180, 90, 59, 26,  9, 11,  2,  3,"Bank on", BT_MOUSE_BUTTON, 255 },
399         { 11,180, 98, 59, 26, 10, 12,  3, 12,"Bank left", BT_MOUSE_BUTTON, 255 },
400         { 12,180,106, 59, 26, 11,  0, 11, 25,"Bank right", BT_MOUSE_BUTTON, 255 },
401         { 13,103,138, 58, 26, 27, 15, 25, 14,"Pitch U/D", BT_MOUSE_AXIS, 255 },
402         { 14,103,138,106,  8, 23, 16, 13, 15,"Pitch U/D", BT_INVERT, 255 },
403         { 15,103,146, 58, 26, 13, 17, 14, 16,"Turn L/R", BT_MOUSE_AXIS, 255 },
404         { 16,103,146,106,  8, 14, 18, 15, 17,"Turn L/R", BT_INVERT, 255 },
405         { 17,103,154, 58, 26, 15, 19, 16, 18,"Slide L/R", BT_MOUSE_AXIS, 255 },
406         { 18,103,154,106,  8, 16, 20, 17, 19,"Slide L/R", BT_INVERT, 255 },
407         { 19,103,162, 58, 26, 17, 21, 18, 20,"Slide U/D", BT_MOUSE_AXIS, 255 },
408         { 20,103,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
409         { 21,103,170, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_MOUSE_AXIS, 255 },
410         { 22,103,170,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
411         { 23,103,182, 58, 26, 21, 14, 22, 24,"throttle", BT_MOUSE_AXIS, 255 },
412         { 24,103,182,106,  8, 22,  5, 23,  0,"throttle", BT_INVERT, 255 },
413         { 25, 25,109, 85, 26,  3, 27, 12, 13,"REAR VIEW", BT_MOUSE_BUTTON, 255 },
414         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_MOUSE_BUTTON, 255 },
415         { 27, 25,117, 85, 26, 25, 13, 25, 13,"Afterburner", BT_MOUSE_BUTTON, 255 },
416 };
417
418 #else           // ifndef MACINTOSH (following are macintosh controls)
419
420 ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
421 {0x5b,0x7e,0x54,0x7d,0x56,0x7b,0x58,0x7c,0x3a,0xff,0xff,0x53,0xff,0x55,0xff,0x4e,0xff,0x45,0xff,0xff,0xc,0x59,0xe,0x5c,0x3b,0x24,0x31,0xff,0x3,0xff,0x3f,0xff,0x6,0xff,0xb,0xff,0xf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x30,0xff,0x1,0xff,0x2b,0xff,0x2f,0xff,0x4,0xff,0x11,0xff,0xff,0xff,0x0,0x0},
422 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
423 {0x0,0x3,0xff,0xff,0xff,0xff,0xb,0xc,0x9,0xa,0xff,0x1,0x2,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0},
424 {0x5,0x4,0xff,0xff,0x6,0xff,0x3,0x2,0x0,0x1,0xff,0x8,0xa,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x3,0x1,0xb,0x7,0xd,0xe,0xf,0xc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
425 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
426 {0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
427 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
428 };
429
430 ubyte default_firebird_settings[MAX_CONTROLS] =
431 {0x0,0x1,0xff,0xff,0x2,0xff,0x4,0x6,0x5,0x7,0xff,0xb,0xc,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x03,0x0,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0};
432
433 ubyte default_mousestick_settings[MAX_CONTROLS] =
434 {0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3,0x4,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0};
435
436 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
437         {  0, 15, 49, 71, 26, 55,  2, 55,  1,"Pitch forward", BT_KEY, 255 },
438         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
439         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
440         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
441         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
442         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
443         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
444         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
445         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
446         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
447         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
448         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
449         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
450         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
451         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
452         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
453         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
454         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
455         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
456         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
457         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
458         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
459         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
460         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
461         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
462         { 25,158, 49,112, 26, 54, 27, 24,  2,"Fire primary", BT_KEY, 255 },
463         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
464         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
465         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
466         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
467         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
468         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
469         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
470         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
471         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
472         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
473         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
474         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
475         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
476         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
477         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
478         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
479         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
480         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
481         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
482         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
483         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
484         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
485         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
486         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
487         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
488         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
489         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
490         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
491         { 54,158,171, 83, 26, 52, 25, 51, 55,"Energy->Shield", BT_KEY, 255 },
492         { 55,158,171,112, 26, 53,  0, 54,  0,"Energy->Shield", BT_KEY, 255 },
493 };
494 kc_item kc_joystick[NUM_OTHER_CONTROLS] = {
495         {  0, 25, 46, 85, 28, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
496         {  1, 25, 54, 85, 28,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
497         {  2, 25, 85, 85, 28, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
498         {  3, 25, 93, 85, 28,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
499         {  4, 25, 62, 85, 28,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
500         {  5,180, 46, 79, 28, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
501         {  6,180, 54, 79, 28,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
502         {  7,180, 62, 79, 28,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
503         {  8,180, 70, 79, 28,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
504         {  9,180, 78, 79, 28,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
505         { 10,180, 90, 79, 28,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
506         { 11,180, 98, 79, 28, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
507         { 12,180,106, 79, 28, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
508         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
509         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
510         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
511         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
512         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
513         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
514         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
515         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
516         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
517         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
518         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
519         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
520         { 25, 25,109, 85, 28,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
521         { 26, 25, 70, 85, 28,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
522         { 27, 25,117, 85, 28, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
523         { 28,180,114, 79, 28, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
524         { 29,180,122, 79, 28, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
525         { 30, 25,125, 85, 28, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
526 };
527 kc_item kc_superjoy[NUM_OTHER_CONTROLS] = {
528         {  0, 25, 46, 85, 26, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
529         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
530         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
531         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
532         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
533         {  5,180, 46, 79, 26, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
534         {  6,180, 54, 79, 26,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
535         {  7,180, 62, 79, 26,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
536         {  8,180, 70, 79, 26,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
537         {  9,180, 78, 79, 26,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
538         { 10,180, 90, 79, 26,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
539         { 11,180, 98, 79, 26, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
540         { 12,180,106, 79, 26, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
541         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
542         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
543         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
544         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
545         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
546         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
547         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
548         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
549         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
550         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
551         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
552         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
553         { 25, 25,109, 85, 26,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
554         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
555         { 27, 25,117, 85, 26, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
556         { 28,180,114, 79, 26, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
557         { 29,180,122, 79, 26, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
558         { 30, 25,125, 85, 26, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
559 };
560
561 kc_item kc_mouse[NUM_OTHER_CONTROLS] = {
562         {  0, 25, 46, 85, 26, 28,  1, 28,  5,"Fire primary", BT_MOUSE_BUTTON, 255 },
563         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_MOUSE_BUTTON, 255 },
564         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_MOUSE_BUTTON, 255 },
565         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_MOUSE_BUTTON, 255 },
566         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_MOUSE_BUTTON, 255 },
567         {  5,180, 46, 59, 26, 24,  6,  0,  1,"Slide on", BT_MOUSE_BUTTON, 255 },
568         {  6,180, 54, 59, 26,  5,  7,  1,  4,"Slide left", BT_MOUSE_BUTTON, 255 },
569         {  7,180, 62, 59, 26,  6,  8,  4, 26,"Slide right", BT_MOUSE_BUTTON, 255 },
570         {  8,180, 70, 59, 26,  7,  9, 26,  9,"Slide up", BT_MOUSE_BUTTON, 255 },
571         {  9,180, 78, 59, 26,  8, 10,  8,  2,"Slide down", BT_MOUSE_BUTTON, 255 },
572         { 10,180, 90, 59, 26,  9, 11,  2,  3,"Bank on", BT_MOUSE_BUTTON, 255 },
573         { 11,180, 98, 59, 26, 10, 12,  3, 12,"Bank left", BT_MOUSE_BUTTON, 255 },
574         { 12,180,106, 59, 26, 11, 28, 11, 25,"Bank right", BT_MOUSE_BUTTON, 255 },
575         { 13,103,138, 58, 26, 27, 15, 27, 14,"Pitch U/D", BT_MOUSE_AXIS, 255 },
576         { 14,103,138,106,  8, 23, 16, 13, 15,"Pitch U/D", BT_INVERT, 255 },
577         { 15,103,146, 58, 26, 13, 17, 14, 16,"Turn L/R", BT_MOUSE_AXIS, 255 },
578         { 16,103,146,106,  8, 14, 18, 15, 17,"Turn L/R", BT_INVERT, 255 },
579         { 17,103,154, 58, 26, 15, 19, 16, 18,"Slide L/R", BT_MOUSE_AXIS, 255 },
580         { 18,103,154,106,  8, 16, 20, 17, 19,"Slide L/R", BT_INVERT, 255 },
581         { 19,103,162, 58, 26, 17, 21, 18, 20,"Slide U/D", BT_MOUSE_AXIS, 255 },
582         { 20,103,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
583         { 21,103,170, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_MOUSE_AXIS, 255 },
584         { 22,103,170,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
585         { 23,103,182, 58, 26, 21, 14, 22, 24,"throttle", BT_MOUSE_AXIS, 255 },
586         { 24,103,182,106,  8, 22,  5, 23, 28,"throttle", BT_INVERT, 255 },
587         { 25, 25,109, 85, 26,  3, 27, 12, 27,"REAR VIEW", BT_MOUSE_BUTTON, 255 },
588         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_MOUSE_BUTTON, 255 },
589         { 27, 25,117, 85, 26, 25, 13, 25, 13,"Afterburner", BT_MOUSE_BUTTON, 255 },
590 };
591
592 #endif
593
594 void kc_drawitem( kc_item *item, int is_current );
595 void kc_change_key( kc_item * item );
596 void kc_change_joybutton( kc_item * item );
597 void kc_change_mousebutton( kc_item * item );
598 void kc_change_joyaxis( kc_item * item );
599 void kc_change_mouseaxis( kc_item * item );
600 void kc_change_invert( kc_item * item );
601 void kconfig_read_fcs( int raw_axis );
602 void kconfig_set_fcs_button( int btn, int button );
603 void kconfig_read_external_controls( void );
604
605 int kconfig_is_axes_used(int axis)
606 {
607         int i;
608         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
609                 if (( kc_joystick[i].type == BT_JOY_AXIS ) && (kc_joystick[i].value == axis ))
610                         return 1;
611         }
612         return 0;
613 }
614
615 #ifdef TABLE_CREATION
616 int find_item_at( kc_item * items, int nitems, int x, int y )
617 {
618         int i;
619         
620         for (i=0; i<nitems; i++ )       {
621                 if ( ((items[i].x+items[i].w1)==x) && (items[i].y==y))
622                         return i;
623         }
624         return -1;
625 }
626
627 int find_next_item_up( kc_item * items, int nitems, int citem )
628 {
629         int x, y, i;
630
631         y = items[citem].y;
632         x = items[citem].x+items[citem].w1;
633         
634         do {    
635                 y--;
636                 if ( y < 0 ) {
637                         y = grd_curcanv->cv_bitmap.bm_h-1;
638                         x--;
639                         if ( x < 0 ) {
640                                 x = grd_curcanv->cv_bitmap.bm_w-1;
641                         }
642                 }
643                 i = find_item_at( items, nitems, x, y );
644         } while ( i < 0 );
645         
646         return i;
647 }
648
649 int find_next_item_down( kc_item * items, int nitems, int citem )
650 {
651         int x, y, i;
652
653         y = items[citem].y;
654         x = items[citem].x+items[citem].w1;
655         
656         do {    
657                 y++;
658                 if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
659                         y = 0;
660                         x++;
661                         if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
662                                 x = 0;
663                         }
664                 }
665                 i = find_item_at( items, nitems, x, y );
666         } while ( i < 0 );
667         
668         return i;
669 }
670
671 int find_next_item_right( kc_item * items, int nitems, int citem )
672 {
673         int x, y, i;
674
675         y = items[citem].y;
676         x = items[citem].x+items[citem].w1;
677         
678         do {    
679                 x++;
680                 if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
681                         x = 0;
682                         y++;
683                         if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
684                                 y = 0;
685                         }
686                 }
687                 i = find_item_at( items, nitems, x, y );
688         } while ( i < 0 );
689         
690         return i;
691 }
692
693 int find_next_item_left( kc_item * items, int nitems, int citem )
694 {
695         int x, y, i;
696
697         y = items[citem].y;
698         x = items[citem].x+items[citem].w1;
699         
700         do {    
701                 x--;
702                 if ( x < 0 ) {
703                         x = grd_curcanv->cv_bitmap.bm_w-1;
704                         y--;
705                         if ( y < 0 ) {
706                                 y = grd_curcanv->cv_bitmap.bm_h-1;
707                         }
708                 }
709                 i = find_item_at( items, nitems, x, y );
710         } while ( i < 0 );
711         
712         return i;
713 }
714 #endif
715
716 #if defined(MACINTOSH) || defined(WINDOWS)
717 int get_item_height(kc_item *item)
718 {
719         int w, h, aw;
720         char btext[10];
721
722         if (item->value==255) {
723                 sprintf( btext, "" );
724         } else {
725                 switch( item->type )    {
726                         case BT_KEY:
727                                 strncpy( btext, key_text[item->value], 10 ); break;
728                         case BT_MOUSE_BUTTON:
729                         #ifdef WINDOWS
730                                 strncpy( btext, Text_string[mousebutton_text[item->value]], 10); break;
731                         #else
732                                 strncpy( btext, mousebutton_text[item->value], 10 ); break;
733                         #endif
734                         case BT_MOUSE_AXIS:
735                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
736                         case BT_JOY_BUTTON:
737                                 if ( joybutton_text[item->value] !=-1 )
738                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
739                                 else
740                                         sprintf( btext, "BTN%d", item->value );
741                                 break;
742                         case BT_JOY_AXIS:
743                                 strncpy( btext, Text_string[joyaxis_text[item->value]], 10 ); break;
744                         case BT_INVERT:
745                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
746                 }
747         }
748         gr_get_string_size(btext, &w, &h, &aw  );
749
750         return h;
751 }
752 #endif
753
754 void kconfig_sub(kc_item * items,int nitems, char * title)
755 {
756 WINDOS(
757         dd_grs_canvas * save_canvas,
758         grs_canvas * save_canvas
759 );
760         grs_font * save_font;
761         int old_keyd_repeat;
762         #if defined(MACINTOSH) || defined(WINDOWS) 
763         int mouse_state, omouse_state, mx, my, x1, x2, y1, y2;
764         int close_x, close_y, close_size;
765         #endif
766
767         int i,k,ocitem,citem;
768         int time_stopped = 0;
769 WIN(char *old_bg_pcx);
770
771 WIN(old_bg_pcx = _SCRContext.bkg_filename);
772 WIN(DEFINE_SCREEN(NULL));
773
774         All_items = items;
775         Num_items = nitems;
776
777         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
778         {
779                 time_stopped = 1;
780                 stop_time();
781         }
782
783 //      if (Config_control_type == CONTROL_WINJOYSTICK) {
784 //              WINDOS(
785 //                      joydefsw_win_joyselect(title2); title = title2,
786 //                      Int3()
787 //              );                                                                                              // Get Samir...
788 //      }
789
790 WINDOS(
791         save_canvas = dd_grd_curcanv,
792         save_canvas = grd_curcanv
793 );
794
795
796 WINDOS(
797         dd_gr_set_current_canvas(NULL),
798         gr_set_current_canvas(NULL)
799 );              
800         save_font = grd_curcanv->cv_font;
801
802 #ifdef WINDOWS
803 KConfigPaint:
804 #endif
805         game_flush_inputs();
806         old_keyd_repeat = keyd_repeat;
807         keyd_repeat = 1;
808
809         //gr_clear_canvas( BM_XRGB(0,0,0) );
810
811         nm_draw_background(0,0,grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
812    gr_palette_load (gr_palette);
813
814         grd_curcanv->cv_font = MEDIUM3_FONT;
815
816 WIN(DDGRLOCK(dd_grd_curcanv));  
817         {
818                 char * p;
819                 p = strchr( title, '\n' );
820                 if ( p ) *p = 32;
821                 gr_string( 0x8000, LHY(8), title );
822                 if ( p ) *p = '\n';
823         }
824
825
826 //      if ( items == kc_keyboard )     {
827 //              gr_string( 0x8000, 8, "Keyboard" );
828 //      } else if ( items == kc_joystick )      {
829 //              gr_string( 0x8000, 8, "Joysticks" );
830 //      } else if ( items == kc_mouse ) {
831 //              gr_string( 0x8000, 8, "Mouse" );
832 //      }
833
834 #if defined(MACINTOSH) || defined(WINDOWS)
835         close_x = close_y = 15;
836         close_size = 10;
837         gr_setcolor( BM_XRGB(0, 0, 0) );
838         gr_rect(close_x, close_y, close_x + close_size, close_y + close_size);
839         gr_setcolor( BM_XRGB(21, 21, 21) );
840         gr_rect( close_x + 2, close_y + 2, close_x + close_size - 2, close_y + close_size -2 );
841         #endif
842
843         grd_curcanv->cv_font = GAME_FONT;
844         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
845
846         #ifndef MACINTOSH
847         gr_string( 0x8000, LHY(20), TXT_KCONFIG_STRING_1 );
848         #else
849         gr_string( 0x8000, LHY(20), "Enter changes, ctrl-d deletes, ctrl-r resets defaults, ESC exits");
850         #endif
851         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
852         if ( items == kc_keyboard )     {
853                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
854                 gr_setcolor( BM_XRGB(31,27,6) );
855                 
856                 gr_scanline( LHX(98), LHX(106), LHY(42) );
857                 gr_scanline( LHX(120), LHX(128), LHY(42) );
858                 gr_pixel( LHX(98), LHY(43) );                                           
859                 gr_pixel( LHX(98), LHY(44) );                                           
860                 gr_pixel( LHX(128), LHY(43) );                                          
861                 gr_pixel( LHX(128), LHY(44) );                                          
862                 
863                 gr_string( LHX(109), LHY(40), "OR" );
864
865                 gr_scanline( LHX(253), LHX(261), LHY(42) );
866                 gr_scanline( LHX(274), LHX(283), LHY(42) );
867                 gr_pixel( LHX(253), LHY(43) );                                          
868                 gr_pixel( LHX(253), LHY(44) );                                          
869                 gr_pixel( LHX(283), LHY(43) );                                          
870                 gr_pixel( LHX(283), LHY(44) );                                          
871
872                 gr_string( LHX(264), LHY(40), "OR" );
873
874         } if ( items == kc_joystick )   {
875                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
876                 gr_setcolor( BM_XRGB(31,27,6) );
877                 gr_scanline( LHX(18), LHX(135), LHY(37) );
878                 gr_scanline( LHX(181), LHX(294), LHY(37) );
879                 gr_scanline( LHX(18), LHX(144), LHY(119+18) );
880                 gr_scanline( LHX(174), LHX(294), LHY(119+18) );
881                 gr_string( 0x8000, LHY(35), TXT_BUTTONS );
882                 gr_string( 0x8000,LHY(117+18), TXT_AXES );
883                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
884                 gr_string( LHX(81), LHY(137+8), TXT_AXIS );
885                 gr_string( LHX(111), LHY(137+8), TXT_INVERT );
886                 gr_string( LHX(222), LHY(137+8), TXT_AXIS );
887                 gr_string( LHX(252), LHY(137+8), TXT_INVERT );
888         } else if ( items == kc_mouse ) {
889                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
890                 gr_setcolor( BM_XRGB(31,27,6) );
891                 gr_scanline( LHX(18), LHX(135), LHY(37) );
892                 gr_scanline( LHX(181), LHX(294), LHY(37) );
893                 gr_scanline( LHX(18), LHX(144), LHY(119+5) );
894                 gr_scanline( LHX(174), LHX(294), LHY(119+5) );
895                 gr_string( 0x8000, LHY(35), TXT_BUTTONS );
896                 gr_string( 0x8000,LHY(117+5), TXT_AXES );
897                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
898                 gr_string( LHX(169), LHY(129), TXT_AXIS );
899                 gr_string( LHX(199), LHY(129), TXT_INVERT );
900         }
901 WIN(DDGRUNLOCK(dd_grd_curcanv));        
902
903         for (i=0; i<nitems; i++ )       {
904                 kc_drawitem( &items[i], 0 );
905         }
906
907         citem = 0;
908         kc_drawitem( &items[citem], 1 );
909         
910         WIN(ShowCursorW());
911         MAC(show_cursor();)
912
913 #if defined(MACINTOSH) || defined(WINDOWS) 
914         mouse_state = omouse_state = 0;
915 #endif
916
917         while(1)                {
918         //      Windows addendum to allow for kconfig input.
919         #ifdef WINDOWS
920                 {
921                         MSG msg;
922
923                         DoMessageStuff(&msg);
924
925                         if (_RedrawScreen) {
926                                 _RedrawScreen = FALSE;
927
928                                 dd_gr_set_current_canvas(NULL); 
929
930                                 goto KConfigPaint;
931                         }
932
933                         DDGRRESTORE;
934                 }
935         #endif
936                 gr_update();
937
938                 //see if redbook song needs to be restarted
939                 songs_check_redbook_repeat();
940
941                 k = key_inkey();
942                 
943 #if defined(MACINTOSH) || defined(WINDOWS)
944                 omouse_state = mouse_state;
945                 mouse_state = mouse_button_state(0);
946 #endif
947                 
948                 if ( !time_stopped ) {
949                         #ifdef NETWORK
950                         if (multi_menu_poll() == -1)
951                                 k = -2;
952                         #endif
953                 }
954                 ocitem = citem;
955                 switch( k )     {
956                 case KEY_BACKSP:
957                         Int3();
958                         break;
959                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
960                 case KEY_PRINT_SCREEN:
961                         save_screen_shot(0);
962                         break;                                                  
963                 case KEY_CTRLED+KEY_D:
964                         items[citem].value = 255;
965                         kc_drawitem( &items[citem], 1 );
966                         break;
967                 case KEY_CTRLED+KEY_R:  
968                         if ( items==kc_keyboard )       {
969                                 for (i=0; i<NUM_KEY_CONTROLS; i++ )             {
970                                         items[i].value=default_kconfig_settings[0][i];
971                                         kc_drawitem( &items[i], 0 );
972                                 }
973                         } else {
974                                 #ifdef MACINTOSH
975                           // hack for firebire and mousestick default controls since I made
976                           // them the same control type -- dumb dumb dumb
977                                 if (joy_have_firebird())
978                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
979                                                 items[i].value = default_firebird_settings[i];
980                                                 kc_drawitem( &items[i], 0 );
981                                         }
982                                 else if (joy_have_mousestick())
983                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
984                                                 items[i].value = default_mousestick_settings[i];
985                                                 kc_drawitem( &items[i], 0 );
986                                         }
987                                 else
988                                 #endif  // note link to above else
989                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
990                                                 items[i].value = default_kconfig_settings[Config_control_type][i];
991                                                 kc_drawitem( &items[i], 0 );
992                                         }
993                         }
994                         kc_drawitem( &items[citem], 1 );
995                         break;
996                 case KEY_DELETE:
997                         items[citem].value=255;
998                         kc_drawitem( &items[citem], 1 );
999                         break;
1000                 case KEY_UP:            
1001                 case KEY_PAD8:
1002 #ifdef TABLE_CREATION
1003                         if (items[citem].u==-1) items[citem].u=find_next_item_up( items,nitems, citem);
1004 #endif
1005                         citem = items[citem].u; 
1006                         break;
1007                 
1008                 case KEY_DOWN:  
1009                 case KEY_PAD2:
1010 #ifdef TABLE_CREATION
1011                         if (items[citem].d==-1) items[citem].d=find_next_item_down( items,nitems, citem);
1012 #endif
1013                         citem = items[citem].d; 
1014                         break;
1015                 case KEY_LEFT:  
1016                 case KEY_PAD4:
1017 #ifdef TABLE_CREATION
1018                         if (items[citem].l==-1) items[citem].l=find_next_item_left( items,nitems, citem);
1019 #endif
1020                         citem = items[citem].l; 
1021                         break;
1022                 case KEY_RIGHT:         
1023                 case KEY_PAD6:
1024 #ifdef TABLE_CREATION
1025                         if (items[citem].r==-1) items[citem].r=find_next_item_right( items,nitems, citem);
1026 #endif
1027                         citem = items[citem].r; 
1028                         break;
1029                 case KEY_ENTER: 
1030                 case KEY_PADENTER:      
1031                         switch( items[citem].type )     {
1032                         case BT_KEY:            kc_change_key( &items[citem] ); break;
1033                         case BT_MOUSE_BUTTON:   kc_change_mousebutton( &items[citem] ); break;
1034                         case BT_MOUSE_AXIS:     kc_change_mouseaxis( &items[citem] ); break;
1035                         case BT_JOY_BUTTON:     kc_change_joybutton( &items[citem] ); break;
1036                         case BT_JOY_AXIS:       kc_change_joyaxis( &items[citem] ); break;
1037                         case BT_INVERT:         kc_change_invert( &items[citem] ); break;
1038                         }
1039                         break;
1040                 case -2:        
1041                 case KEY_ESC:
1042                         grd_curcanv->cv_font    = save_font;
1043
1044                 WIN(DEFINE_SCREEN(old_bg_pcx));
1045
1046                 WINDOS(
1047                         dd_gr_set_current_canvas(save_canvas),
1048                         gr_set_current_canvas( save_canvas )
1049                 );                      
1050                         keyd_repeat = old_keyd_repeat;
1051                         game_flush_inputs();
1052                         WIN( HideCursorW());
1053                         MAC( hide_cursor(); )
1054                         if (time_stopped)
1055                                 start_time();
1056                         return;
1057 #ifdef TABLE_CREATION
1058                 case KEY_DEBUGGED+KEY_F12:      {
1059                         FILE * fp;
1060                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
1061                                 kc_keyboard[i].u = find_next_item_up( kc_keyboard,NUM_KEY_CONTROLS, i);
1062                                 kc_keyboard[i].d = find_next_item_down( kc_keyboard,NUM_KEY_CONTROLS, i);
1063                                 kc_keyboard[i].l = find_next_item_left( kc_keyboard,NUM_KEY_CONTROLS, i);
1064                                 kc_keyboard[i].r = find_next_item_right( kc_keyboard,NUM_KEY_CONTROLS, i);
1065                         }
1066                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1067                                 kc_joystick[i].u = find_next_item_up( kc_joystick,NUM_OTHER_CONTROLS, i);
1068                                 kc_joystick[i].d = find_next_item_down( kc_joystick,NUM_OTHER_CONTROLS, i);
1069                                 kc_joystick[i].l = find_next_item_left( kc_joystick,NUM_OTHER_CONTROLS, i);
1070                                 kc_joystick[i].r = find_next_item_right( kc_joystick,NUM_OTHER_CONTROLS, i);
1071                         }
1072                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1073                                 kc_mouse[i].u = find_next_item_up( kc_mouse,NUM_OTHER_CONTROLS, i);
1074                                 kc_mouse[i].d = find_next_item_down( kc_mouse,NUM_OTHER_CONTROLS, i);
1075                                 kc_mouse[i].l = find_next_item_left( kc_mouse,NUM_OTHER_CONTROLS, i);
1076                                 kc_mouse[i].r = find_next_item_right( kc_mouse,NUM_OTHER_CONTROLS, i);
1077                         }
1078                         fp = fopen( "kconfig.cod", "wt" );
1079
1080                         fprintf( fp, "ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {\n" );
1081                         for (i=0; i<CONTROL_MAX_TYPES; i++ )    {
1082                                 int j;
1083                                 fprintf( fp, "{0x%x", kconfig_settings[i][0] );
1084                                 for (j=1; j<MAX_CONTROLS; j++ )
1085                                         fprintf( fp, ",0x%x", kconfig_settings[i][j] );
1086                                 fprintf( fp, "},\n" );
1087                         }
1088                         fprintf( fp, "};\n" );
1089                 
1090                         fprintf( fp, "\nkc_item kc_keyboard[NUM_KEY_CONTROLS] = {\n" );
1091                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
1092                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1093                                         kc_keyboard[i].id, kc_keyboard[i].x, kc_keyboard[i].y, kc_keyboard[i].w1, kc_keyboard[i].w2,
1094                                         kc_keyboard[i].u, kc_keyboard[i].d, kc_keyboard[i].l, kc_keyboard[i].r,
1095                                         34, kc_keyboard[i].text, 34, btype_text[kc_keyboard[i].type] );
1096                         }
1097                         fprintf( fp, "};" );
1098
1099                         fprintf( fp, "\nkc_item kc_joystick[NUM_OTHER_CONTROLS] = {\n" );
1100                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1101                                 if (kc_joystick[i].type == BT_JOY_BUTTON)
1102                                         fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1103                                                 kc_joystick[i].id, kc_joystick[i].x, kc_joystick[i].y, kc_joystick[i].w1, kc_joystick[i].w2,
1104                                                 kc_joystick[i].u, kc_joystick[i].d, kc_joystick[i].l, kc_joystick[i].r,
1105                                                 34, kc_joystick[i].text, 34, btype_text[kc_joystick[i].type] );
1106                 else
1107                                         fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1108                                                 kc_joystick[i].id, kc_joystick[i].x, kc_joystick[i].y, kc_joystick[i].w1, kc_joystick[i].w2,
1109                                                 kc_joystick[i].u, kc_joystick[i].d, kc_joystick[i].l, kc_joystick[i].r,
1110                                                 34, kc_joystick[i].text, 34, btype_text[kc_joystick[i].type] );
1111                         }
1112                         fprintf( fp, "};" );
1113
1114                         fprintf( fp, "\nkc_item kc_mouse[NUM_OTHER_CONTROLS] = {\n" );
1115                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1116                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1117                                         kc_mouse[i].id, kc_mouse[i].x, kc_mouse[i].y, kc_mouse[i].w1, kc_mouse[i].w2,
1118                                         kc_mouse[i].u, kc_mouse[i].d, kc_mouse[i].l, kc_mouse[i].r,
1119                                         34, kc_mouse[i].text, 34, btype_text[kc_mouse[i].type] );
1120                         }
1121                         fprintf( fp, "};" );
1122
1123                         fclose(fp);
1124
1125                         }
1126                         break;
1127 #endif
1128                 }
1129
1130         #if defined (MACINTOSH) || defined(WINDOWS)
1131                 if ( (mouse_state && !omouse_state) || (mouse_state && omouse_state) ) {
1132                         int item_height;
1133                         
1134                         mouse_get_pos(&mx, &my);
1135                         for (i=0; i<nitems; i++ )       {
1136                                 item_height = get_item_height( &items[i] );
1137                                 x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[i].x) + LHX(items[i].w1);
1138                                 x2 = x1 + LHX(items[i].w2);
1139                                 y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[i].y);
1140                                 y2 = y1 + LHX(item_height);
1141                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1142                                         citem = i;
1143                                         break;
1144                                 }
1145                         }
1146                 }
1147                 else if ( !mouse_state && omouse_state ) {
1148                         int item_height;
1149                         
1150                         mouse_get_pos(&mx, &my);
1151                         item_height = get_item_height( &items[citem] );
1152                         x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[citem].x) + LHX(items[citem].w1);
1153                         x2 = x1 + LHX(items[citem].w2);
1154                         y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[citem].y);
1155                         y2 = y1 + LHY(item_height);
1156                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1157                                 WIN(HideCursorW());
1158                                 MAC(hide_cursor();)
1159                                 switch( items[citem].type )     {
1160                                 case BT_KEY:                            kc_change_key( &items[citem] ); break;
1161                                 case BT_MOUSE_BUTTON:   kc_change_mousebutton( &items[citem] ); break;
1162                                 case BT_MOUSE_AXIS:             kc_change_mouseaxis( &items[citem] ); break;
1163                                 case BT_JOY_BUTTON:             kc_change_joybutton( &items[citem] ); break;
1164                                 case BT_JOY_AXIS:               kc_change_joyaxis( &items[citem] ); break;
1165                                 case BT_INVERT:                         kc_change_invert( &items[citem] ); break;
1166                                 }
1167                                 MAC(show_cursor();)
1168                                 WIN(ShowCursorW());
1169                         } else {
1170                                 x1 = grd_curcanv->cv_bitmap.bm_x + close_x + 2;
1171                                 x2 = x1 + close_size - 2;
1172                                 y1 = grd_curcanv->cv_bitmap.bm_y + close_y + 2;
1173                                 y2 = y1 + close_size - 2;
1174                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
1175                                         grd_curcanv->cv_font    = save_font;
1176                                         WINDOS(dd_gr_set_current_canvas( save_canvas ),
1177                                                         gr_set_current_canvas( save_canvas ));
1178                                         keyd_repeat = old_keyd_repeat;
1179                                         game_flush_inputs();
1180                                         WIN(HideCursorW());
1181                                         MAC(hide_cursor();)
1182                                         if (time_stopped)
1183                                                 start_time();
1184                                         return;
1185                                 }
1186                         }
1187
1188                 }
1189                 #endif          // MACINTOSH WINDOWS
1190                 
1191                 if (ocitem!=citem)      {
1192                         MAC(hide_cursor();)
1193                         WIN(HideCursorW());
1194                         kc_drawitem( &items[ocitem], 0 );
1195                         kc_drawitem( &items[citem], 1 );
1196                         WIN(ShowCursorW());
1197                         MAC(show_cursor();)
1198                 }
1199         }
1200 }
1201
1202
1203 void kc_drawitem( kc_item *item, int is_current )
1204 {
1205         int x, w, h, aw;
1206         char btext[16];
1207 //      PA_DFX (pa_set_frontbuffer_current());
1208 WIN(DDGRLOCK(dd_grd_curcanv));
1209
1210         if (is_current)
1211                 gr_set_fontcolor( BM_XRGB(20,20,29), -1 );
1212         else
1213                 gr_set_fontcolor( BM_XRGB(15,15,24), -1 );
1214    gr_string( LHX(item->x), LHY(item->y), item->text );
1215 WIN(DDGRUNLOCK(dd_grd_curcanv));
1216
1217         if (item->value==255) {
1218                 strcpy( btext, "" );
1219         } else {
1220                 switch( item->type )    {
1221                         case BT_KEY:
1222                                 strncpy( btext, key_text[item->value], 10 ); break;
1223                         case BT_MOUSE_BUTTON:
1224                                 #ifndef MACINTOSH
1225                                 strncpy( btext, Text_string[mousebutton_text[item->value]], 10 ); break;
1226                                 #else
1227                                 strncpy( btext, mousebutton_text[item->value], 10 ); break;
1228                                 #endif
1229                         case BT_MOUSE_AXIS:
1230                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
1231                         case BT_JOY_BUTTON:
1232 #ifndef MACINTOSH
1233         #ifdef WINDOWS
1234                                 if (joybutton_text[item->value] != -1) 
1235                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
1236                                 else 
1237                                         sprintf( btext, "BTN%2d", item->value+1 );
1238         #else   
1239                                 if ( joybutton_text[item->value] !=-1 )
1240                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
1241                                 else
1242                                         sprintf( btext, "BTN%d", item->value );
1243         #endif
1244 #else
1245                                 strncpy( btext, joy_btn_name( item->value ), 10);
1246                                 if (btext == NULL)
1247                                         sprintf( btext, "BTN%d", item->value );
1248 #endif
1249                                 break;
1250                         case BT_JOY_AXIS:
1251                                 strncpy( btext, Text_string[joyaxis_text[item->value]], 10 ); break;
1252                         case BT_INVERT:
1253                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
1254                 }
1255         }
1256         if (item->w1) {
1257         WIN(DDGRLOCK(dd_grd_curcanv));
1258                 gr_get_string_size(btext, &w, &h, &aw  );
1259
1260                 if (is_current)
1261                         gr_setcolor( BM_XRGB(21,0,24) );
1262                 else
1263                         gr_setcolor( BM_XRGB(16,0,19) );
1264                 gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1265                 
1266                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1267
1268                 x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1269         
1270                 gr_string( x, LHY(item->y), btext );
1271 //              PA_DFX (pa_set_backbuffer_current());
1272
1273         WIN(DDGRUNLOCK(dd_grd_curcanv));
1274         }
1275 }
1276
1277
1278 static int looper=0;
1279
1280 void kc_drawquestion( kc_item *item )
1281 {
1282         int c, x, w, h, aw;
1283
1284 WIN(DDGRLOCK(dd_grd_curcanv));  
1285   // PA_DFX (pa_set_frontbuffer_current());
1286
1287         gr_get_string_size("?", &w, &h, &aw  );
1288
1289         c = BM_XRGB(21,0,24);
1290
1291         //@@gr_setcolor( gr_fade_table[fades[looper]*256+c] );
1292         gr_setcolor(BM_XRGB(21*fades[looper]/31,0,24*fades[looper]/31));
1293         looper++;
1294         if (looper>63) looper=0;
1295
1296         gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1297         
1298         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1299
1300         x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1301    
1302         gr_string( x, LHY(item->y), "?" );
1303 //      PA_DFX (pa_set_backbuffer_current());
1304 WIN(DDGRUNLOCK(dd_grd_curcanv));
1305 gr_update();
1306 }
1307
1308 void kc_change_key( kc_item * item )
1309 {
1310         int i,n,f,k;
1311         ubyte keycode;
1312
1313 WIN(DDGRLOCK(dd_grd_curcanv));
1314         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1315         
1316         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_KEY );
1317 WIN(DDGRUNLOCK(dd_grd_curcanv));        
1318
1319         game_flush_inputs();
1320         keycode=255;
1321         k=255;
1322         
1323         while( (k!=KEY_ESC) && (keycode==255) ) 
1324         {                               
1325         #ifdef WINDOWS
1326                 {
1327                         MSG msg;
1328
1329                         DoMessageStuff(&msg);
1330                         DDGRRESTORE;
1331                 
1332                 }
1333         #endif
1334
1335                 #ifdef NETWORK
1336                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1337                         multi_menu_poll();
1338                 #endif
1339 //              if ( Game_mode & GM_MULTI )
1340 //                      GameLoop( 0, 0 );                               // Continue
1341                 k = key_inkey();
1342                 d_delay(10);
1343                 kc_drawquestion( item );
1344         
1345                 for (i=0; i<256; i++ )  {
1346                         if (keyd_pressed[i] && (strlen(key_text[i])>0)) {
1347                                 f = 0;
1348                                 for (n=0; n<sizeof(system_keys); n++ )
1349                                         if ( system_keys[n] == i )
1350                                                 f=1;
1351                                 if (!f) 
1352                                         keycode=i;
1353                         }
1354                 }
1355         }
1356
1357         if (k!=KEY_ESC) {
1358                 for (i=0; i<Num_items; i++ )    {
1359                         n = item - All_items;
1360                         if ( (i!=n) && (All_items[i].type==BT_KEY) && (All_items[i].value==keycode) )           {
1361                                 All_items[i].value = 255;
1362                                 kc_drawitem( &All_items[i], 0 );
1363                         }
1364                 }
1365                 item->value = keycode;
1366         }
1367         kc_drawitem( item, 1 );
1368
1369 WIN(DDGRLOCK(dd_grd_curcanv));
1370         gr_set_fontcolor( BM_XRGB(28,28,28), BM_XRGB(0,0,0) );
1371 WIN(DDGRUNLOCK(dd_grd_curcanv));
1372
1373         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1374
1375         game_flush_inputs();
1376
1377 }
1378
1379 void kc_change_joybutton( kc_item * item )
1380 {
1381         int n,i,k;
1382         ubyte code;
1383
1384 WIN(DDGRLOCK(dd_grd_curcanv));
1385         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1386         
1387         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_JBUTTON );
1388 WIN(DDGRUNLOCK(dd_grd_curcanv));        
1389
1390         game_flush_inputs();
1391         code=255;
1392         k=255;
1393         
1394         while( (k!=KEY_ESC) && (code==255))     
1395         {                               
1396         #ifdef WINDOWS
1397                 {
1398                         MSG msg;
1399                         DoMessageStuff(&msg);
1400                         DDGRRESTORE;
1401                 }
1402         #endif
1403
1404                 #ifdef NETWORK
1405                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1406                         multi_menu_poll();
1407                 #endif
1408 //              if ( Game_mode & GM_MULTI )
1409 //                      GameLoop( 0, 0 );                               // Continue
1410                 k = key_inkey();
1411                 d_delay(10);
1412
1413                 if (k == KEY_PRINT_SCREEN)
1414                         save_screen_shot(0);
1415
1416                 kc_drawquestion( item );
1417
1418                 WIN(code = joydefsw_do_button());
1419                 
1420 #ifdef MACINTOSH
1421                 code = joy_do_buttons();
1422 #endif
1423                 
1424 #if !defined(WINDOWS) && !defined(MACINTOSH)
1425                 if (Config_control_type==CONTROL_THRUSTMASTER_FCS)      {
1426                         int axis[4];
1427                         joystick_read_raw_axis( JOY_ALL_AXIS, axis );
1428                         kconfig_read_fcs( axis[3] );
1429                         if ( joy_get_button_state(7) ) code = 7;
1430                         if ( joy_get_button_state(11) ) code = 11;
1431                         if ( joy_get_button_state(15) ) code = 15;
1432                         if ( joy_get_button_state(19) ) code = 19;
1433                         for (i=0; i<4; i++ )    {
1434                                 if ( joy_get_button_state(i) )
1435                                         code = i;
1436                         }
1437                 } else if (Config_control_type==CONTROL_FLIGHTSTICK_PRO) {
1438                         for (i=4; i<20; i++ )   {
1439                                 if ( joy_get_button_state(i)  ) {
1440                                         code = i;
1441                                         mprintf(( 0, "JB: %d\n", code ));
1442                                 }
1443                         }
1444                 } else {
1445                         for (i=0; i<MAX_BUTTONS; i++ )  {
1446                                 if ( joy_get_button_state(i) )
1447                                         code = i;
1448                         }
1449                 }
1450 #endif
1451         }
1452         if (code!=255)  {
1453                 for (i=0; i<Num_items; i++ )    {
1454                         n = item - All_items;
1455                         if ( (i!=n) && (All_items[i].type==BT_JOY_BUTTON) && (All_items[i].value==code) ) {
1456                                 All_items[i].value = 255;
1457                                 kc_drawitem( &All_items[i], 0 );
1458                         }
1459                 }
1460                 item->value = code;
1461         }
1462         kc_drawitem( item, 1 );
1463         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1464         game_flush_inputs();
1465 }
1466
1467 void kc_change_mousebutton( kc_item * item )
1468 {
1469         int n,i,b,k;
1470         ubyte code;
1471
1472 WIN(DDGRLOCK(dd_grd_curcanv));
1473         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1474         
1475         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_MBUTTON );
1476 WIN(DDGRUNLOCK(dd_grd_curcanv));        
1477
1478
1479         game_flush_inputs();
1480         code=255;
1481         k=255;
1482         
1483         while( (k!=KEY_ESC) && (code==255))     
1484         {                               
1485         #ifdef WINDOWS
1486                 {
1487                         MSG msg;
1488                         DoMessageStuff(&msg);
1489                         DDGRRESTORE;
1490                 }
1491         #endif
1492
1493                 #ifdef NETWORK
1494                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1495                         multi_menu_poll();
1496                 #endif
1497 //              if ( Game_mode & GM_MULTI )
1498 //                      GameLoop( 0, 0 );                               // Continue
1499                 k = key_inkey();
1500                 d_delay(10);
1501
1502                 if (k == KEY_PRINT_SCREEN)
1503                         save_screen_shot(0);
1504
1505                 kc_drawquestion( item );
1506
1507                 b = mouse_get_btns();
1508                 for (i=0; i<3; i++ )    {
1509                         if ( b & (1<<i) )       
1510                                 code = i;
1511                 }
1512         }
1513         if (code!=255)  {
1514                 for (i=0; i<Num_items; i++ )    {
1515                         n = item - All_items;
1516                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_BUTTON) && (All_items[i].value==code) )             {
1517                                 All_items[i].value = 255;
1518                                 kc_drawitem( &All_items[i], 0 );
1519                         }
1520                 }
1521                 item->value = code;
1522         }
1523         kc_drawitem( item, 1 );
1524         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1525         game_flush_inputs();
1526
1527 }
1528
1529 void kc_change_joyaxis( kc_item * item )
1530 {
1531 #ifdef __ENV_LINUX
1532         int axis[MAX_AXES];
1533         int old_axis[MAX_AXES];
1534 #else
1535         int axis[JOY_NUM_AXES];
1536         int old_axis[JOY_NUM_AXES];
1537 #endif
1538         int n,i,k;
1539         ubyte code;
1540    WINDOS (
1541          int numaxis=7,
1542          int numaxis=6
1543         );
1544
1545 WIN(DDGRLOCK(dd_grd_curcanv));
1546         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1547         
1548         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_JOY_AXIS );
1549 WIN(DDGRUNLOCK(dd_grd_curcanv));        
1550
1551         game_flush_inputs();
1552         code=255;
1553         k=255;
1554
1555         WINDOS(
1556                 joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, old_axis ),
1557                 joystick_read_raw_axis( JOY_ALL_AXIS, old_axis )
1558         );
1559
1560         while( (k!=KEY_ESC) && (code==255))     
1561         {                               
1562         #ifdef WINDOWS
1563                 {
1564                         MSG msg;
1565                         DoMessageStuff(&msg);
1566                         DDGRRESTORE;
1567                 }
1568         #endif
1569
1570                 #ifdef NETWORK
1571                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1572                         multi_menu_poll();
1573                 #endif
1574 //              if ( Game_mode & GM_MULTI )
1575 //                      GameLoop( 0, 0 );                               // Continue
1576                 k = key_inkey();
1577                 d_delay(10);
1578
1579                 if (k == KEY_PRINT_SCREEN)
1580                         save_screen_shot(0);
1581
1582                 kc_drawquestion( item );
1583
1584                 WINDOS(
1585                         joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, axis ),
1586                         joystick_read_raw_axis( JOY_ALL_AXIS, axis )
1587                 );
1588                 
1589                 for (i=0; i<numaxis; i++ )      {
1590 #if defined (MACINTOSH)
1591                         if ( abs(axis[i]-old_axis[i])>100 )     {
1592 #elif defined(WINDOWS)
1593                         if ( abs(axis[i]-old_axis[i])>1024 )    {
1594 #else 
1595                         if ( abs(axis[i]-old_axis[i])>200 )     {
1596 #endif
1597                                 code = i;
1598                                 printf("Axis Movement detected: Axis %i\n", i);
1599                         }
1600                         //old_axis[i] = axis[i];
1601                 }
1602                 for (i=0; i<Num_items; i++ )    
1603                  {
1604                         n = item - All_items;
1605                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) 
1606                                 code = 255;
1607                  }
1608         
1609         }
1610         if (code!=255)  {
1611                 for (i=0; i<Num_items; i++ )    {
1612                         n = item - All_items;
1613                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) {
1614                                 All_items[i].value = 255;
1615                                 kc_drawitem( &All_items[i], 0 );
1616                         }
1617                 }
1618
1619                 item->value = code;                                      
1620         }
1621         kc_drawitem( item, 1 );
1622         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1623         game_flush_inputs();
1624
1625 }
1626
1627 void kc_change_mouseaxis( kc_item * item )
1628 {
1629         int i,n,k;
1630         ubyte code;
1631         int dx,dy;
1632
1633 WIN(DDGRLOCK(dd_grd_curcanv));
1634         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1635         
1636         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_MSE_AXIS );
1637 WIN(DDGRUNLOCK(dd_grd_curcanv));        
1638
1639         game_flush_inputs();
1640         code=255;
1641         k=255;
1642
1643         mouse_get_delta( &dx, &dy );
1644
1645         while( (k!=KEY_ESC) && (code==255))     
1646         {                               
1647         #ifdef WINDOWS
1648                 {
1649                         MSG msg;
1650                         DoMessageStuff(&msg);
1651                         DDGRRESTORE;
1652                 }
1653         #endif
1654
1655                 #ifdef NETWORK
1656                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1657                         multi_menu_poll();
1658                 #endif
1659 //              if ( Game_mode & GM_MULTI )
1660 //                      GameLoop( 0, 0 );                               // Continue
1661                 k = key_inkey();
1662                 d_delay(10);
1663
1664                 if (k == KEY_PRINT_SCREEN)
1665                         save_screen_shot(0);
1666
1667                 kc_drawquestion( item );
1668
1669                 mouse_get_delta( &dx, &dy );
1670                 if ( abs(dx)>20 ) code = 0;
1671                 if ( abs(dy)>20 )       code = 1;
1672         }
1673         if (code!=255)  {
1674                 for (i=0; i<Num_items; i++ )    {
1675                         n = item - All_items;
1676                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_AXIS) && (All_items[i].value==code) )               {
1677                                 All_items[i].value = 255;
1678                                 kc_drawitem( &All_items[i], 0 );
1679                         }
1680                 }
1681                 item->value = code;
1682         }
1683         kc_drawitem( item, 1 );
1684         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1685         game_flush_inputs();
1686
1687 }
1688
1689
1690 void kc_change_invert( kc_item * item )
1691 {
1692         game_flush_inputs();
1693
1694         if (item->value)
1695                 item->value = 0;
1696         else 
1697                 item->value = 1;
1698
1699         kc_drawitem( item, 1 );
1700
1701 }
1702
1703 #include "screens.h"
1704
1705 extern void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
1706
1707 void kconfig(int n, char * title)
1708 {
1709         int i;
1710         grs_bitmap *save_bm;
1711
1712         set_screen_mode( SCREEN_MENU );
1713
1714         kc_set_controls();
1715
1716         //save screen
1717         WIN(mouse_set_mode(0));
1718         WIN(dd_gr_set_current_canvas(NULL));
1719         
1720 #if defined(POLY_ACC)
1721         save_bm = gr_create_bitmap2( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h, grd_curcanv->cv_bitmap.bm_type, NULL );
1722 #else
1723         save_bm = gr_create_bitmap( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
1724 #endif
1725         Assert( save_bm != NULL );
1726         
1727         WIN(DDGRLOCK(dd_grd_curcanv));
1728                 gr_bm_bitblt(grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_w, 
1729                                                  0, 0, 0, 0, &grd_curcanv->cv_bitmap, save_bm );
1730         WIN(DDGRUNLOCK(dd_grd_curcanv));
1731
1732         switch(n)       {
1733         case 0:kconfig_sub( kc_keyboard, NUM_KEY_CONTROLS, title );break;
1734         case 1:kconfig_sub( kc_joystick, NUM_OTHER_CONTROLS, title );break;
1735         case 2:kconfig_sub( kc_mouse, NUM_OTHER_CONTROLS, title ); break;
1736         case 3:kconfig_sub( kc_superjoy, NUM_OTHER_CONTROLS, title); break;
1737         default:
1738                 Int3();
1739                 return;
1740         }
1741
1742         //restore screen
1743         WIN(mouse_set_mode(1));
1744         WIN(dd_gr_set_current_canvas(NULL));
1745         WIN(DDGRLOCK(dd_grd_curcanv));
1746                 gr_bitmap(0, 0, save_bm);
1747         WIN(DDGRUNLOCK(dd_grd_curcanv));
1748         gr_free_bitmap(save_bm);
1749
1750         reset_cockpit();                //force cockpit redraw next time
1751
1752         // Update save values...
1753         
1754         for (i=0; i<NUM_KEY_CONTROLS; i++ )     
1755                 kconfig_settings[0][i] = kc_keyboard[i].value;
1756
1757         if ( (Config_control_type>0) && (Config_control_type<5)) { 
1758                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1759                         kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
1760         } else if (Config_control_type>4 && Config_control_type<CONTROL_WINJOYSTICK) {
1761                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1762                         kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
1763         }
1764         else if (Config_control_type == CONTROL_WINJOYSTICK) {
1765                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1766                         kconfig_settings[Config_control_type][i] = kc_superjoy[i].value;
1767         }
1768 }
1769
1770
1771 void kconfig_read_fcs( int raw_axis )
1772 {
1773         int raw_button, button, axis_min[4], axis_center[4], axis_max[4];
1774
1775         if (Config_control_type!=CONTROL_THRUSTMASTER_FCS) return;
1776
1777         joy_get_cal_vals(axis_min, axis_center, axis_max);
1778
1779         if ( axis_max[3] > 1 )
1780                 raw_button = (raw_axis*100)/axis_max[3];
1781         else
1782                 raw_button = 0;
1783
1784         if ( raw_button > 88 )
1785                 button = 0;
1786         else if ( raw_button > 63 )
1787                 button = 7;
1788         else if ( raw_button > 39 )
1789                 button = 11;
1790         else if ( raw_button > 15 )
1791                 button = 15;
1792         else    
1793                 button = 19;
1794
1795         kconfig_set_fcs_button( 19, button );
1796         kconfig_set_fcs_button( 15, button );
1797         kconfig_set_fcs_button( 11, button );
1798         kconfig_set_fcs_button( 7, button );
1799 }
1800                 
1801
1802 void kconfig_set_fcs_button( int btn, int button )
1803 {
1804         int state,time_down,upcount,downcount;
1805         state = time_down = upcount = downcount = 0;
1806
1807         if ( joy_get_button_state(btn) ) {
1808                 if ( btn==button )      {
1809                         state = 1;
1810                         time_down = FrameTime;
1811                 } else {
1812                         upcount=1;
1813                 }
1814         } else {
1815                 if ( btn==button )      {
1816                         state = 1;
1817                         time_down = FrameTime;
1818                         downcount=1;
1819                 } else {
1820                         upcount=1;
1821                 }
1822         }                               
1823                         
1824         joy_set_btn_values( btn, state, time_down, downcount, upcount );
1825                                         
1826 }
1827
1828
1829
1830 fix Last_angles_p = 0;
1831 fix Last_angles_b = 0;
1832 fix Last_angles_h = 0;
1833 ubyte Last_angles_read = 0;
1834
1835 extern int                      VR_sensitivity;
1836                                                 
1837 int VR_sense_range[3] = { 25, 50, 75 };
1838
1839 #if 0 //ndef MACINTOSH
1840 read_head_tracker()
1841 {
1842 #ifndef WINDOWS
1843
1844         fix yaw, pitch, roll;
1845         int buttons;
1846
1847 //------ read vfx1 helmet --------
1848         if (vfx1_installed) {
1849                 vfx_get_data(&yaw,&pitch,&roll,&buttons);
1850         } else if (iglasses_headset_installed)  {
1851                 iglasses_read_headset( &yaw, &pitch, &roll );
1852         } else if (Victor_headset_installed)   {
1853                 victor_read_headset_filtered( &yaw, &pitch, &roll );
1854         } else {
1855                 return;
1856         }
1857
1858         Use_player_head_angles = 0;
1859         if ( Last_angles_read ) {
1860                 fix yaw1 = yaw;
1861                 
1862                 yaw1 = yaw;
1863                 if ( (Last_angles_h < (F1_0/4) ) && (yaw > ((F1_0*3)/4) ) )     
1864                         yaw1 -= F1_0;
1865                 else if ( (yaw < (F1_0/4) ) && (Last_angles_h > ((F1_0*3)/4) ) )        
1866                         yaw1 += F1_0;
1867         
1868                 Controls.pitch_time     += fixmul((pitch- Last_angles_p)*VR_sense_range[VR_sensitivity],FrameTime);
1869                 Controls.heading_time+= fixmul((yaw1 -  Last_angles_h)*VR_sense_range[VR_sensitivity],FrameTime);
1870                 Controls.bank_time      += fixmul((roll - Last_angles_b)*VR_sense_range[VR_sensitivity],FrameTime);
1871         }
1872         Last_angles_read = 1;
1873         Last_angles_p = pitch;
1874         Last_angles_h = yaw;
1875         Last_angles_b = roll;
1876 #endif
1877 }
1878 #endif
1879
1880 #define PH_SCALE        8
1881
1882 #ifdef WINDOWS
1883 #define JOYSTICK_READ_TIME      (F1_0/40)               //      Read joystick at 40 Hz.
1884 #else
1885 #define JOYSTICK_READ_TIME      (F1_0/10)               //      Read joystick at 10 Hz.
1886 #endif
1887
1888 fix     LastReadTime = 0;
1889
1890 fix     joy_axis[7];
1891
1892 ubyte                   kc_use_external_control = 0;
1893 ubyte                           kc_enable_external_control = 0;
1894 ubyte                   kc_external_intno = 0;
1895 ext_control_info        *kc_external_control = NULL;
1896 ubyte                           *kc_external_name = NULL;
1897 ubyte                           kc_external_version = 0;
1898 extern int Automap_active;
1899
1900 void kconfig_init_external_controls(int intno, int address)
1901 {
1902         int i;
1903         kc_external_intno = intno;
1904         kc_external_control     = (ext_control_info *)address;
1905         kc_use_external_control = 1;
1906         kc_enable_external_control  = 1;
1907
1908         i = FindArg ( "-xname" );
1909         if ( i )        
1910                 kc_external_name = Args[i+1];
1911         else
1912                 kc_external_name = "External Controller";
1913
1914    for (i=0;i<strlen (kc_external_name);i++)
1915     if (kc_external_name[i]=='_')
1916           kc_external_name[i]=' '; 
1917
1918         i = FindArg ( "-xver" );
1919         if ( i )
1920                 kc_external_version = atoi(Args[i+1]);
1921         
1922         printf( "%s int: 0x%x, data: 0x%p, ver:%d\n", kc_external_name, kc_external_intno, kc_external_control, kc_external_version );
1923
1924 }
1925
1926 #if !defined(MACINTOSH)
1927 /*void kconfig_read_external_controls()
1928 {
1929         union REGS r;
1930
1931         if ( !kc_enable_external_control && !CybermouseActive) 
1932                 return;
1933
1934         if ( kc_external_version == 0 ) 
1935                 memset( kc_external_control, 0, sizeof(control_info) );
1936         else if ( kc_external_version > 0 )     {
1937                 memset( kc_external_control, 0, sizeof(control_info)+sizeof(vms_angvec) + 64 );
1938                 if ( kc_external_version > 1 ) {
1939                         // Write ship pos and angles to external controls...
1940                         ubyte *temp_ptr = (ubyte *)kc_external_control;
1941                         vms_vector *ship_pos;
1942                         vms_matrix *ship_orient;
1943                         memset( kc_external_control, 0, sizeof(control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector)+sizeof(vms_matrix) );
1944                         temp_ptr += sizeof(control_info)+sizeof(vms_angvec) + 64;
1945                         ship_pos = (vms_vector *)temp_ptr;
1946                         temp_ptr += sizeof(vms_vector);
1947                         ship_orient = (vms_matrix *)temp_ptr;
1948                         // Fill in ship postion...
1949                         *ship_pos = Objects[Players[Player_num].objnum].pos;
1950                         // Fill in ship orientation...
1951                         *ship_orient = Objects[Players[Player_num].objnum].orient;
1952                 }
1953         }
1954
1955         if ( Automap_active )                    // (If in automap...)
1956                 kc_external_control->automap_state = 1;
1957         memset(&r,0,sizeof(r));
1958
1959 #ifndef WINDOWS
1960   
1961    if (!CybermouseActive)
1962         int386 ( kc_external_intno, &r, &r);            // Read external info...
1963 //      else
1964   //            ReadOWL (kc_external_control);
1965
1966 #endif
1967
1968         if ( Player_num > -1 )  {
1969                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
1970                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
1971                 Auto_leveling_on = 0;
1972
1973                 if ( kc_external_version > 0 ) {                
1974                         vms_matrix tempm, ViewMatrix;
1975                         vms_angvec * Kconfig_abs_movement;
1976                         char * oem_message;
1977         
1978                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(control_info));
1979         
1980                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
1981                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
1982                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1983                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
1984                         }
1985                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
1986                         if (oem_message[0] != '\0' )
1987                                 HUD_init_message( oem_message );
1988                 }
1989         }
1990
1991         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
1992         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
1993         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
1994         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
1995         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
1996         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
1997         Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
1998         Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
1999         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
2000         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
2001         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
2002         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
2003         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
2004         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
2005         Controls.automap_down_count += kc_external_control->automap_down_count;
2006         Controls.automap_state |= kc_external_control->automap_state;
2007 } */
2008 #endif
2009
2010 #ifdef WINDOWS
2011 void controls_read_all_win()
2012 {
2013         int i;
2014         int slide_on, bank_on;
2015         int dx, dy;
2016         fix ctime;
2017         fix mouse_axis[2];
2018         int raw_joy_axis[7];
2019         int mouse_buttons;
2020         fix k0, k1, k2, k3, kp;
2021         fix k4, k5, k6, k7, kh;
2022         ubyte channel_masks;
2023         int use_mouse, use_joystick;
2024         int speed_factor=1;
2025
2026         if (Game_turbo_mode)
2027                 speed_factor = 2;
2028         
2029         {
2030                 fix temp = Controls.heading_time;
2031                 fix temp1 = Controls.pitch_time;
2032                 memset( &Controls, 0, sizeof(control_info) );
2033                 Controls.heading_time = temp;
2034                 Controls.pitch_time = temp1;
2035         }
2036         slide_on = 0;
2037         bank_on = 0;
2038
2039         ctime = timer_get_fixed_seconds();
2040
2041 //      DO JOYSTICK (X,Y,Z  R,U,V)
2042 //      ----------------------------------------------------------------------------
2043         if ( (LastReadTime + JOYSTICK_READ_TIME) > ctime ) {
2044                 if ((ctime < 0) && (LastReadTime > 0))
2045                         LastReadTime = ctime;
2046                 use_joystick=1;
2047         } else if ((Config_control_type==CONTROL_WINJOYSTICK)) {
2048                 LastReadTime = ctime;
2049                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, raw_joy_axis );
2050
2051                 for (i=0; i<7; i++ )    {
2052                         if (channel_masks&(1<<i))       {
2053                                 int joy_null_value = 28;
2054
2055                         // DO POV if joystick has a hat.
2056                         //      --------------------------------------------------------------------
2057                                 if (i == 3) {
2058                                         joydefsw_do_winjoybutton(raw_joy_axis);
2059                                 }
2060                                 else {
2061                                 // DO AXIS
2062                                 //      --------------------------------------------------------------------    
2063                                 //      mprintf((0, "(%d)=%d,", i, raw_joy_axis[i]));
2064                                         raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2065                                 //      mprintf((0, "%d  ",raw_joy_axis[i]));
2066                                 //      if (i%2) mprintf((0,"\n"));
2067
2068         
2069                                         if (kc_superjoy[23].value==i)           // If this is the throttle
2070                                                 joy_null_value = 32;                            // Then use a larger dead-zone
2071         
2072                                         if (raw_joy_axis[i] > (joy_null_value-2)) 
2073                                                 raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2074                                         else if (raw_joy_axis[i] < -(joy_null_value+2))
2075                                                 raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2076                                         else
2077                                                 raw_joy_axis[i] = 0;
2078                                         joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2079                                 }
2080                         } else {
2081                                 joy_axis[i] = 0;
2082                         }
2083                 }       
2084                 use_joystick=1;
2085
2086         } else {
2087                 for (i=0; i<6; i++ )
2088                         joy_axis[i] = 0;
2089                 use_joystick=0;
2090         }
2091
2092 //      DO MOUSE 
2093 //      ----------------------------------------------------------------------------
2094         if (Config_control_type==5) {
2095                 mouse_get_delta( &dx, &dy );
2096                 mouse_axis[0] = (dx*FrameTime)/35;
2097                 mouse_axis[1] = (dy*FrameTime)/25;
2098                 mouse_buttons = mouse_get_btns();
2099                 //mprintf(( 0, "Mouse %d,%d b:%d, 0x%x\n", mouse_axis[0], mouse_axis[1], mouse_buttons, FrameTime ));
2100                 use_mouse=1;
2101    } else {
2102                 mouse_axis[0] = 0;
2103                 mouse_axis[1] = 0;
2104                 mouse_buttons = 0;
2105                 use_mouse=0;
2106         }
2107
2108 //------------- Read slide_on -------------
2109         
2110         // From keyboard...
2111         if ( kc_keyboard[8].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[8].value ];
2112         if ( kc_keyboard[9].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[9].value ];
2113         // From joystick...
2114         if ((use_joystick)&&(kc_superjoy[5].value<255)) slide_on |= joy_get_button_state( kc_superjoy[5].value );
2115         // From mouse...
2116         if ((use_mouse)&&(kc_mouse[5].value<255)) slide_on |= mouse_buttons & (1<<kc_mouse[5].value);
2117
2118 //------------- Read bank_on ---------------
2119
2120         // From keyboard...
2121         if ( kc_keyboard[18].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[18].value ];
2122         if ( kc_keyboard[19].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[19].value ];
2123         // From joystick...
2124         if ( (use_joystick)&&(kc_superjoy[10].value < 255 )) bank_on |= joy_get_button_state( kc_superjoy[10].value );
2125         // From mouse...
2126         if ( (use_mouse)&&(kc_mouse[10].value < 255 )) bank_on |= mouse_buttons & (1<<kc_mouse[10].value);
2127
2128 //------------ Read pitch_time -----------
2129         if ( !slide_on )        {
2130                 // mprintf((0, "pitch: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2131                 kp = 0;
2132                 k0 = speed_factor*key_down_time( kc_keyboard[0].value )/2;      // Divide by two since we want pitch to go slower
2133                 k1 = speed_factor*key_down_time( kc_keyboard[1].value )/2;
2134                 k2 = speed_factor*key_down_time( kc_keyboard[2].value )/2;
2135                 k3 = speed_factor*key_down_time( kc_keyboard[3].value )/2;
2136
2137                 // From keyboard...
2138                 if ( kc_keyboard[0].value < 255 ) kp += k0/PH_SCALE;
2139                 if ( kc_keyboard[1].value < 255 ) kp += k1/PH_SCALE;
2140                 if ( kc_keyboard[2].value < 255 ) kp -= k2/PH_SCALE;
2141                 if ( kc_keyboard[3].value < 255 ) kp -= k3/PH_SCALE;
2142
2143                 // From Cyberman...
2144                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2145                         kp += mouse_button_down_time(MB_PITCH_FORWARD)/(PH_SCALE*2);
2146                         kp -= mouse_button_down_time(MB_PITCH_BACKWARD)/(PH_SCALE*2);
2147                 }
2148         
2149                 if (kp == 0)
2150                         Controls.pitch_time = 0;
2151                 else if (kp > 0) {
2152                         if (Controls.pitch_time < 0)
2153                                 Controls.pitch_time = 0;
2154                 } else // kp < 0
2155                         if (Controls.pitch_time > 0)
2156                                 Controls.pitch_time = 0;
2157                 Controls.pitch_time += kp;
2158         
2159                 // From joystick...
2160                 if ( (use_joystick)&&(kc_superjoy[13].value < 255 ))    {
2161                         if ( !kc_superjoy[14].value )           // If not inverted...
2162                                 Controls.pitch_time -= (joy_axis[kc_superjoy[13].value]*Config_joystick_sensitivity)/8;
2163                         else
2164                                 Controls.pitch_time += (joy_axis[kc_superjoy[13].value]*Config_joystick_sensitivity)/8;
2165                 }
2166         
2167                 // From mouse...
2168                 //mprintf(( 0, "UM: %d, PV: %d\n", use_mouse, kc_mouse[13].value ));
2169                 if ( (use_mouse)&&(kc_mouse[13].value < 255) )  {
2170                         if ( !kc_mouse[14].value )              // If not inverted...
2171                                 Controls.pitch_time -= (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2172                         else
2173                                 Controls.pitch_time += (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2174                 }
2175         } else {
2176                 Controls.pitch_time = 0;
2177         }
2178
2179
2180 //----------- Read vertical_thrust_time -----------------
2181
2182         if ( slide_on ) {
2183                 k0 = speed_factor*key_down_time( kc_keyboard[0].value );
2184                 k1 = speed_factor*key_down_time( kc_keyboard[1].value );
2185                 k2 = speed_factor*key_down_time( kc_keyboard[2].value );
2186                 k3 = speed_factor*key_down_time( kc_keyboard[3].value );
2187
2188                 // From keyboard...
2189                 if ( kc_keyboard[0].value < 255 ) Controls.vertical_thrust_time += k0;
2190                 if ( kc_keyboard[1].value < 255 ) Controls.vertical_thrust_time += k1;
2191                 if ( kc_keyboard[2].value < 255 ) Controls.vertical_thrust_time -= k2;
2192                 if ( kc_keyboard[3].value < 255 ) Controls.vertical_thrust_time -= k3;
2193
2194                 // From Cyberman...
2195                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2196                         Controls.vertical_thrust_time -= mouse_button_down_time(MB_PITCH_FORWARD);
2197                         Controls.vertical_thrust_time += mouse_button_down_time(MB_PITCH_BACKWARD);
2198                 }
2199         
2200                 // From joystick...
2201                 if ((use_joystick)&&( kc_superjoy[13].value < 255 ))    {
2202                         if ( !kc_superjoy[14].value )           // If not inverted...
2203                                 Controls.vertical_thrust_time += joy_axis[kc_superjoy[13].value];
2204                         else
2205                                 Controls.vertical_thrust_time -= joy_axis[kc_superjoy[13].value];
2206                 }
2207         
2208                 // From mouse...
2209                 if ( (use_mouse)&&(kc_mouse[13].value < 255 ))  {
2210                         if ( !kc_mouse[14].value )              // If not inverted...
2211                                 Controls.vertical_thrust_time -= mouse_axis[kc_mouse[13].value];
2212                         else
2213                                 Controls.vertical_thrust_time += mouse_axis[kc_mouse[13].value];
2214                 }
2215         }
2216
2217         // From keyboard...
2218         if ( kc_keyboard[14].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[14].value );
2219         if ( kc_keyboard[15].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[15].value );
2220         if ( kc_keyboard[16].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[16].value );
2221         if ( kc_keyboard[17].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[17].value );
2222         
2223         // From joystick...
2224         if ((use_joystick)&&( kc_superjoy[19].value < 255 ))    {
2225                 if ( !kc_superjoy[20].value )           // If not inverted...
2226                         Controls.vertical_thrust_time += joy_axis[kc_superjoy[19].value];
2227                 else
2228                         Controls.vertical_thrust_time -= joy_axis[kc_superjoy[19].value];
2229         }
2230
2231         // From joystick buttons
2232         if ( (use_joystick)&&(kc_superjoy[8].value < 255 )) Controls.vertical_thrust_time += joy_get_button_down_time( kc_superjoy[8].value );
2233         if ( (use_joystick)&&(kc_superjoy[9].value < 255 )) Controls.vertical_thrust_time -= joy_get_button_down_time( kc_superjoy[9].value );
2234
2235         // From mouse buttons
2236         if ( (use_mouse)&&(kc_mouse[8].value < 255 )) Controls.vertical_thrust_time += mouse_button_down_time( kc_mouse[8].value );
2237         if ( (use_mouse)&&(kc_mouse[9].value < 255 )) Controls.vertical_thrust_time -= mouse_button_down_time( kc_mouse[9].value );
2238
2239         // From mouse...
2240         if ( (use_mouse)&&(kc_mouse[19].value < 255 ))  {
2241                 if ( !kc_mouse[20].value )              // If not inverted...
2242                         Controls.vertical_thrust_time += mouse_axis[kc_mouse[19].value];
2243                 else
2244                         Controls.vertical_thrust_time -= mouse_axis[kc_mouse[19].value];
2245         }
2246
2247         // From Cyberman...
2248         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2249                 Controls.vertical_thrust_time += mouse_button_down_time(MB_Z_UP)/2;
2250                 Controls.vertical_thrust_time -= mouse_button_down_time(MB_Z_DOWN)/2;
2251         }
2252
2253 //---------- Read heading_time -----------
2254
2255         if (!slide_on && !bank_on)      {
2256                 //mprintf((0, "heading: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2257                 kh = 0;
2258                 k4 = speed_factor*key_down_time( kc_keyboard[4].value );
2259                 k5 = speed_factor*key_down_time( kc_keyboard[5].value );
2260                 k6 = speed_factor*key_down_time( kc_keyboard[6].value );
2261                 k7 = speed_factor*key_down_time( kc_keyboard[7].value );
2262
2263                 // From keyboard...
2264                 if ( kc_keyboard[4].value < 255 ) kh -= k4/PH_SCALE;
2265                 if ( kc_keyboard[5].value < 255 ) kh -= k5/PH_SCALE;
2266                 if ( kc_keyboard[6].value < 255 ) kh += k6/PH_SCALE;
2267                 if ( kc_keyboard[7].value < 255 ) kh += k7/PH_SCALE;
2268
2269                 // From Cyberman...
2270                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2271                         kh -= mouse_button_down_time(MB_HEAD_LEFT)/PH_SCALE;
2272                         kh += mouse_button_down_time(MB_HEAD_RIGHT)/PH_SCALE;
2273                 }
2274         
2275                 if (kh == 0)
2276                         Controls.heading_time = 0;
2277                 else if (kh > 0) {
2278                         if (Controls.heading_time < 0)
2279                                 Controls.heading_time = 0;
2280                 } else // kh < 0
2281                         if (Controls.heading_time > 0)
2282                                 Controls.heading_time = 0;
2283                 Controls.heading_time += kh;
2284
2285                 // From joystick...
2286                 if ( (use_joystick)&&(kc_superjoy[15].value < 255 ))    {
2287                         if ( !kc_superjoy[16].value )           // If not inverted...
2288                                 Controls.heading_time += (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2289                         else
2290                                 Controls.heading_time -= (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2291                 }
2292         
2293                 // From mouse...
2294                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2295                         if ( !kc_mouse[16].value )              // If not inverted...
2296                                 Controls.heading_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2297                         else
2298                                 Controls.heading_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2299                 }
2300         } else {
2301                 Controls.heading_time = 0;
2302         }
2303
2304 //----------- Read sideways_thrust_time -----------------
2305
2306         if ( slide_on ) {
2307                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
2308                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
2309                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
2310                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
2311
2312                 // From keyboard...
2313                 if ( kc_keyboard[4].value < 255 ) Controls.sideways_thrust_time -= k0;
2314                 if ( kc_keyboard[5].value < 255 ) Controls.sideways_thrust_time -= k1;
2315                 if ( kc_keyboard[6].value < 255 ) Controls.sideways_thrust_time += k2;
2316                 if ( kc_keyboard[7].value < 255 ) Controls.sideways_thrust_time += k3;
2317         
2318                 // From joystick...
2319                 if ( (use_joystick)&&(kc_superjoy[15].value < 255 ))    {
2320                         if ( !kc_superjoy[16].value )           // If not inverted...
2321                                 Controls.sideways_thrust_time += joy_axis[kc_superjoy[15].value];
2322                         else
2323                                 Controls.sideways_thrust_time -= joy_axis[kc_superjoy[15].value];
2324                 }
2325                 
2326                 // From cyberman
2327                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2328                         Controls.sideways_thrust_time -= mouse_button_down_time(MB_HEAD_LEFT);
2329                         Controls.sideways_thrust_time += mouse_button_down_time(MB_HEAD_RIGHT);
2330                 }
2331         
2332                 // From mouse...
2333                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2334                         if ( !kc_mouse[16].value )              // If not inverted...
2335                                 Controls.sideways_thrust_time += mouse_axis[kc_mouse[15].value];
2336                         else
2337                                 Controls.sideways_thrust_time -= mouse_axis[kc_mouse[15].value];
2338                 }
2339         }
2340
2341         // From keyboard...
2342         if ( kc_keyboard[10].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[10].value );
2343         if ( kc_keyboard[11].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[11].value );
2344         if ( kc_keyboard[12].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[12].value );
2345         if ( kc_keyboard[13].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[13].value );
2346         
2347         // From joystick...
2348         if ( (use_joystick)&&(kc_superjoy[17].value < 255 ))    {
2349                 if ( !kc_superjoy[18].value )           // If not inverted...
2350                         Controls.sideways_thrust_time -= joy_axis[kc_superjoy[17].value];
2351                 else
2352                         Controls.sideways_thrust_time += joy_axis[kc_superjoy[17].value];
2353         }
2354
2355         // From joystick buttons
2356         if ( (use_joystick)&&(kc_superjoy[6].value < 255 )) Controls.sideways_thrust_time -= joy_get_button_down_time( kc_superjoy[6].value );
2357         if ( (use_joystick)&&(kc_superjoy[7].value < 255 )) Controls.sideways_thrust_time += joy_get_button_down_time( kc_superjoy[7].value );
2358
2359         // From mouse buttons
2360         if ( (use_mouse)&&(kc_mouse[6].value < 255 )) Controls.sideways_thrust_time -= mouse_button_down_time( kc_mouse[6].value );
2361         if ( (use_mouse)&&(kc_mouse[7].value < 255 )) Controls.sideways_thrust_time += mouse_button_down_time( kc_mouse[7].value );
2362
2363         // From mouse...
2364         if ( (use_mouse)&&(kc_mouse[17].value < 255 ))  {
2365                 if ( !kc_mouse[18].value )              // If not inverted...
2366                         Controls.sideways_thrust_time += mouse_axis[kc_mouse[17].value];
2367                 else
2368                         Controls.sideways_thrust_time -= mouse_axis[kc_mouse[17].value];
2369         }
2370
2371 //----------- Read bank_time -----------------
2372
2373         if ( bank_on )  {
2374                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
2375                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
2376                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
2377                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
2378
2379                 // From keyboard...
2380                 if ( kc_keyboard[4].value < 255 ) Controls.bank_time += k0;
2381                 if ( kc_keyboard[5].value < 255 ) Controls.bank_time += k1;
2382                 if ( kc_keyboard[6].value < 255 ) Controls.bank_time -= k2;
2383                 if ( kc_keyboard[7].value < 255 ) Controls.bank_time -= k3;
2384
2385                 // From Cyberman...
2386                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2387                         Controls.bank_time -= mouse_button_down_time(MB_HEAD_LEFT);
2388                         Controls.bank_time += mouse_button_down_time(MB_HEAD_RIGHT);
2389                 }
2390
2391                 // From joystick...
2392                 if ( (use_joystick)&&(kc_superjoy[15].value < 255) )    {
2393                         if ( !kc_superjoy[16].value )           // If not inverted...
2394                                 Controls.bank_time -= (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2395                         else
2396                                 Controls.bank_time += (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2397                 }
2398         
2399                 // From mouse...
2400                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2401                         if ( !kc_mouse[16].value )              // If not inverted...
2402                                 Controls.bank_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2403                         else
2404                                 Controls.bank_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2405                 }
2406         }
2407
2408         // From keyboard...
2409         if ( kc_keyboard[20].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[20].value );
2410         if ( kc_keyboard[21].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[21].value );
2411         if ( kc_keyboard[22].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[22].value );
2412         if ( kc_keyboard[23].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[23].value );
2413
2414         // From joystick...
2415         if ( (use_joystick)&&(kc_superjoy[21].value < 255) )    {
2416                 if ( !kc_superjoy[22].value )           // If not inverted...
2417                         Controls.bank_time -= joy_axis[kc_superjoy[21].value];
2418                 else
2419                         Controls.bank_time += joy_axis[kc_superjoy[21].value];
2420         }
2421
2422         // From joystick buttons
2423         if ( (use_joystick)&&(kc_superjoy[11].value < 255 )) Controls.bank_time += joy_get_button_down_time( kc_superjoy[11].value );
2424         if ( (use_joystick)&&(kc_superjoy[12].value < 255 )) Controls.bank_time -= joy_get_button_down_time( kc_superjoy[12].value );
2425
2426         // From mouse buttons
2427         if ( (use_mouse)&&(kc_mouse[11].value < 255 )) Controls.bank_time += mouse_button_down_time( kc_mouse[11].value );
2428         if ( (use_mouse)&&(kc_mouse[12].value < 255 )) Controls.bank_time -= mouse_button_down_time( kc_mouse[12].value );
2429
2430         // From mouse...
2431         if ( (use_mouse)&&(kc_mouse[21].value < 255 ))  {
2432                 if ( !kc_mouse[22].value )              // If not inverted...
2433                         Controls.bank_time += mouse_axis[kc_mouse[21].value];
2434                 else
2435                         Controls.bank_time -= mouse_axis[kc_mouse[21].value];
2436         }
2437
2438         // From Cyberman
2439         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2440                 Controls.bank_time += mouse_button_down_time(MB_BANK_LEFT);
2441                 Controls.bank_time -= mouse_button_down_time(MB_BANK_RIGHT);
2442         }
2443
2444 //----------- Read forward_thrust_time -------------
2445
2446         // From keyboard...
2447         if ( kc_keyboard[30].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[30].value );
2448         if ( kc_keyboard[31].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[31].value );
2449         if ( kc_keyboard[32].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[32].value );
2450         if ( kc_keyboard[33].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[33].value );
2451
2452         // From joystick...
2453         if ( (use_joystick)&&(kc_superjoy[23].value < 255 ))    {
2454                 if ( !kc_superjoy[24].value )           // If not inverted...
2455                         Controls.forward_thrust_time -= joy_axis[kc_superjoy[23].value];
2456                 else
2457                         Controls.forward_thrust_time += joy_axis[kc_superjoy[23].value];
2458         }
2459
2460         // From joystick buttons
2461         if ( (use_joystick)&&(kc_superjoy[2].value < 255 )) Controls.forward_thrust_time += joy_get_button_down_time( kc_superjoy[2].value );
2462         if ( (use_joystick)&&(kc_superjoy[3].value < 255 )) Controls.forward_thrust_time -= joy_get_button_down_time( kc_superjoy[3].value );
2463
2464         // From mouse...
2465         if ( (use_mouse)&&(kc_mouse[23].value < 255 ))  {
2466                 if ( !kc_mouse[24].value )              // If not inverted...
2467                         Controls.forward_thrust_time -= mouse_axis[kc_mouse[23].value];
2468                 else
2469                         Controls.forward_thrust_time += mouse_axis[kc_mouse[23].value];
2470         }
2471
2472         // From mouse buttons
2473         if ( (use_mouse)&&(kc_mouse[2].value < 255 )) Controls.forward_thrust_time += mouse_button_down_time( kc_mouse[2].value );
2474         if ( (use_mouse)&&(kc_mouse[3].value < 255 )) Controls.forward_thrust_time -= mouse_button_down_time( kc_mouse[3].value );
2475
2476 //----------- Read afterburner_state -------------
2477
2478         // From keyboard...
2479         if ( kc_keyboard[46].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[46].value];
2480         if ( kc_keyboard[47].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[47].value];
2481
2482         if ( (use_mouse)&&(kc_mouse[27].value < 255 )) Controls.afterburner_state |= mouse_button_state(kc_mouse[27].value);
2483
2484         if ( (use_joystick)&&(kc_superjoy[27].value < 255 )) Controls.afterburner_state |= joy_get_button_state(kc_superjoy[27].value);
2485
2486 //-------Read headlight key--------------------------
2487         if (kc_keyboard[52].value < 255 )
2488                    Controls.headlight_count=key_down_count(kc_keyboard[52].value);
2489         if (kc_keyboard[53].value < 255 )
2490                    Controls.headlight_count=key_down_count(kc_keyboard[53].value);
2491                   if ((use_joystick)&&(kc_superjoy[30].value < 255 )) 
2492          Controls.headlight_count=joy_get_button_down_cnt(kc_superjoy[30].value);
2493                  
2494 //--------Read Cycle Primary Key------------------
2495
2496                   if (kc_keyboard[48].value<255)
2497                          Controls.cycle_primary_count=key_down_count(kc_keyboard[48].value);
2498                   if (kc_keyboard[49].value<255)
2499                         Controls.cycle_primary_count+=key_down_count(kc_keyboard[49].value);
2500              if ((use_joystick)&&(kc_superjoy[28].value < 255 )) 
2501          Controls.cycle_primary_count+=joy_get_button_down_cnt(kc_superjoy[28].value);
2502
2503 //--------Read Cycle Secondary Key------------------
2504
2505                   if (kc_keyboard[50].value<255)
2506                          Controls.cycle_secondary_count=key_down_count(kc_keyboard[50].value);
2507                   if (kc_keyboard[51].value<255)
2508                         Controls.cycle_secondary_count+=key_down_count(kc_keyboard[51].value);
2509              if ((use_joystick)&&(kc_superjoy[29].value < 255 )) 
2510                         Controls.cycle_secondary_count=joy_get_button_down_cnt(kc_superjoy[29].value);
2511
2512 //--------Read Toggle Bomb key----------------------
2513
2514                   if (kc_keyboard[56].value<255 && key_down_count(kc_keyboard[56].value))
2515          {
2516           int bomb = Secondary_last_was_super[PROXIMITY_INDEX]?PROXIMITY_INDEX:SMART_MINE_INDEX;
2517
2518                          if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX] &&
2519                                   !Players[Player_num].secondary_ammo[SMART_MINE_INDEX])
2520                            {
2521                                  digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
2522                                  HUD_init_message ("No bombs available!");
2523                                 }
2524                          else
2525                                 {       
2526                                  if (Players[Player_num].secondary_ammo[bomb]==0)
2527                                         {
2528                                          digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
2529                                          HUD_init_message ("No %s available!",(bomb==SMART_MINE_INDEX)?"Smart mines":"Proximity bombs");
2530                                         }
2531                                   else
2532                                         {
2533                                Secondary_last_was_super[PROXIMITY_INDEX]=!Secondary_last_was_super[PROXIMITY_INDEX];
2534                                          digi_play_sample_once( SOUND_GOOD_SELECTION_SECONDARY, F1_0 );
2535                                         }
2536                                 }
2537                         }
2538           
2539 //---------Read Energy->Shield key----------
2540
2541         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[54].value])
2542                 transfer_energy_to_shield(key_down_time(kc_keyboard[54].value));
2543         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[55].value])
2544                 transfer_energy_to_shield(key_down_time(kc_keyboard[55].value));
2545
2546 //----------- Read fire_primary_down_count
2547         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[24].value);
2548         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[25].value);
2549         if ((use_joystick)&&(kc_superjoy[0].value < 255 )) Controls.fire_primary_down_count += joy_get_button_down_cnt(kc_superjoy[0].value);
2550         if ((use_mouse)&&(kc_mouse[0].value < 255 )) Controls.fire_primary_down_count += mouse_button_down_count(kc_mouse[0].value);
2551
2552 //----------- Read fire_primary_state
2553         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[24].value];
2554         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[25].value];
2555         if ((use_joystick)&&(kc_superjoy[0].value < 255 )) Controls.fire_primary_state |= joy_get_button_state(kc_superjoy[0].value);
2556         if ((use_mouse)&&(kc_mouse[0].value < 255) ) Controls.fire_primary_state |= mouse_button_state(kc_mouse[0].value);
2557
2558 //----------- Read fire_secondary_down_count
2559         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[26].value);
2560         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[27].value);
2561         if ((use_joystick)&&(kc_superjoy[1].value < 255 )) Controls.fire_secondary_down_count += joy_get_button_down_cnt(kc_superjoy[1].value);
2562         if ((use_mouse)&&(kc_mouse[1].value < 255 )) Controls.fire_secondary_down_count += mouse_button_down_count(kc_mouse[1].value);
2563
2564 //----------- Read fire_secondary_state
2565         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[26].value];
2566         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[27].value];
2567         if ((use_joystick)&&(kc_superjoy[1].value < 255 )) Controls.fire_secondary_state |= joy_get_button_state(kc_superjoy[1].value);
2568         if ((use_mouse)&&(kc_mouse[1].value < 255) ) Controls.fire_secondary_state |= mouse_button_state(kc_mouse[1].value);
2569
2570 //----------- Read fire_flare_down_count
2571         if (kc_keyboard[28].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[28].value);
2572         if (kc_keyboard[29].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[29].value);
2573         if ((use_joystick)&&(kc_superjoy[4].value < 255 )) Controls.fire_flare_down_count += joy_get_button_down_cnt(kc_superjoy[4].value);
2574         if ((use_mouse)&&(kc_mouse[4].value < 255 )) Controls.fire_flare_down_count += mouse_button_down_count(kc_mouse[4].value);
2575
2576 //----------- Read drop_bomb_down_count
2577         if (kc_keyboard[34].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[34].value);
2578         if (kc_keyboard[35].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[35].value);
2579         if ((use_joystick)&&(kc_superjoy[26].value < 255 )) Controls.drop_bomb_down_count += joy_get_button_down_cnt(kc_superjoy[26].value);
2580         if ((use_mouse)&&(kc_mouse[26].value < 255 )) Controls.drop_bomb_down_count += mouse_button_down_count(kc_mouse[26].value);
2581
2582 //----------- Read rear_view_down_count
2583         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[36].value);
2584         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[37].value);
2585         if ((use_joystick)&&(kc_superjoy[25].value < 255 )) Controls.rear_view_down_count += joy_get_button_down_cnt(kc_superjoy[25].value);
2586         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_count += mouse_button_down_count(kc_mouse[25].value);
2587
2588 //----------- Read rear_view_down_state
2589         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[36].value];
2590         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[37].value];
2591         if ((use_joystick)&&(kc_superjoy[25].value < 255 )) Controls.rear_view_down_state |= joy_get_button_state(kc_superjoy[25].value);
2592         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_state |= mouse_button_state(kc_mouse[25].value);
2593
2594 //----------- Read automap_down_count
2595         if (kc_keyboard[44].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[44].value);
2596         if (kc_keyboard[45].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[45].value);
2597
2598 //----------- Read automap_state
2599         if (kc_keyboard[44].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[44].value];
2600         if (kc_keyboard[45].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[45].value];
2601
2602 //----------- Read stupid-cruise-control-type of throttle.
2603         {
2604                 if ( kc_keyboard[38].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[38].value)*5,FrameTime);
2605                 if ( kc_keyboard[39].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[39].value)*5,FrameTime);
2606                 if ( kc_keyboard[40].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[40].value)*5,FrameTime);
2607                 if ( kc_keyboard[41].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[41].value)*5,FrameTime);
2608                 if ( (kc_keyboard[42].value < 255) && (key_down_count(kc_keyboard[42].value)) )
2609                         Cruise_speed = 0;
2610                 if ( (kc_keyboard[43].value < 255) && (key_down_count(kc_keyboard[43].value)) )
2611                         Cruise_speed = 0;
2612         
2613                 if (Cruise_speed > i2f(100) ) Cruise_speed = i2f(100);
2614                 if (Cruise_speed < 0 ) Cruise_speed = 0;
2615         
2616                 if (Controls.forward_thrust_time==0)
2617                         Controls.forward_thrust_time = fixmul(Cruise_speed,FrameTime)/100;
2618         }
2619
2620         //read_head_tracker();
2621
2622         // Read external controls
2623         if (kc_use_external_control || CybermouseActive)
2624                 kconfig_read_external_controls();
2625
2626 //----------- Clamp values between -FrameTime and FrameTime
2627         if (FrameTime > F1_0 )
2628                 mprintf( (1, "Bogus frame time of %.2f seconds\n", f2fl(FrameTime) ));
2629
2630         if (Controls.pitch_time > FrameTime/2 ) Controls.pitch_time = FrameTime/2;
2631         if (Controls.vertical_thrust_time > FrameTime ) Controls.vertical_thrust_time = FrameTime;
2632         if (Controls.heading_time > FrameTime ) Controls.heading_time = FrameTime;
2633         if (Controls.sideways_thrust_time > FrameTime ) Controls.sideways_thrust_time = FrameTime;
2634         if (Controls.bank_time > FrameTime ) Controls.bank_time = FrameTime;
2635         if (Controls.forward_thrust_time > FrameTime ) Controls.forward_thrust_time = FrameTime;
2636 //      if (Controls.afterburner_time > FrameTime ) Controls.afterburner_time = FrameTime;
2637
2638         if (Controls.pitch_time < -FrameTime/2 ) Controls.pitch_time = -FrameTime/2;
2639         if (Controls.vertical_thrust_time < -FrameTime ) Controls.vertical_thrust_time = -FrameTime;
2640         if (Controls.heading_time < -FrameTime ) Controls.heading_time = -FrameTime;
2641         if (Controls.sideways_thrust_time < -FrameTime ) Controls.sideways_thrust_time = -FrameTime;
2642         if (Controls.bank_time < -FrameTime ) Controls.bank_time = -FrameTime;
2643         if (Controls.forward_thrust_time < -FrameTime ) Controls.forward_thrust_time = -FrameTime;
2644 //      if (Controls.afterburner_time < -FrameTime ) Controls.afterburner_time = -FrameTime;
2645
2646
2647 //--------- Don't do anything if in debug mode
2648         #ifndef RELEASE
2649         if ( keyd_pressed[KEY_DELETE] ) {
2650                 memset( &Controls, 0, sizeof(control_info) );
2651         }
2652         #endif
2653 }
2654 #else
2655
2656
2657 fix Next_toggle_time[3]={0,0,0};
2658
2659 int allowed_to_toggle(int i)
2660 {
2661   //used for keeping tabs of when its ok to toggle headlight,primary,and secondary
2662  
2663         if (Next_toggle_time[i] > GameTime)
2664                 if (Next_toggle_time[i] < GameTime + (F1_0/8))  //      In case time is bogus, never wait > 1 second.
2665                         return 0;
2666
2667         Next_toggle_time[i] = GameTime + (F1_0/8);
2668
2669         return 1;
2670 }
2671
2672
2673
2674 void controls_read_all()
2675 {
2676         int i;
2677         int slide_on, bank_on;
2678         int dx, dy;
2679         int idx, idy;
2680         fix ctime;
2681         fix mouse_axis[2];
2682 #ifdef __ENV_LINUX__
2683         int raw_joy_axis[MAX_AXES];
2684 #else
2685         int raw_joy_axis[JOY_NUM_AXES];
2686 #endif
2687         int mouse_buttons;
2688         fix k0, k1, k2, k3, kp;
2689         fix k4, k5, k6, k7, kh;
2690         ubyte channel_masks;
2691         int use_mouse, use_joystick;
2692         int speed_factor=1;
2693
2694         mouse_buttons=0;
2695         use_mouse=0;
2696
2697         if (Game_turbo_mode)
2698                 speed_factor = 2;
2699         
2700         {
2701                 fix temp = Controls.heading_time;
2702                 fix temp1 = Controls.pitch_time;
2703                 memset( &Controls, 0, sizeof(control_info) );
2704                 Controls.heading_time = temp;
2705                 Controls.pitch_time = temp1;
2706         }
2707         slide_on = 0;
2708         bank_on = 0;
2709
2710         ctime = timer_get_fixed_seconds();
2711
2712         //---------  Read Joystick -----------
2713 #ifndef MACINTOSH
2714         if ( (LastReadTime + JOYSTICK_READ_TIME > ctime) && (Config_control_type!=CONTROL_THRUSTMASTER_FCS) ) {
2715                 if ((ctime < 0) && (LastReadTime > 0))
2716                         LastReadTime = ctime;
2717                 use_joystick=1;
2718         } else if ((Config_control_type>0) && (Config_control_type<5) ) {
2719                 LastReadTime = ctime;
2720                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
2721                 
2722                 for (i=0; i<6; i++ )    {
2723 #ifndef  __ENV_LINUX__
2724                         if (channel_masks&(1<<i))       {
2725 #endif
2726                                 int joy_null_value = 10;
2727
2728                                 if ( (i==3) && (Config_control_type==CONTROL_THRUSTMASTER_FCS) )        {
2729                                         kconfig_read_fcs( raw_joy_axis[i] );
2730                                 } else {
2731                                         raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2732         
2733                                         if (kc_joystick[23].value==i)           // If this is the throttle
2734                                                 joy_null_value = 20;            // Then use a larger dead-zone
2735         
2736                                         if (raw_joy_axis[i] > joy_null_value) 
2737                                           raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2738                                         else if (raw_joy_axis[i] < -joy_null_value)
2739                                           raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2740                                         else
2741                                           raw_joy_axis[i] = 0;
2742                                         joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2743                                 }
2744 #ifndef  __ENV_LINUX__
2745                         } else {
2746                                 joy_axis[i] = 0;
2747                         }
2748 #endif
2749                 }       
2750                 use_joystick=1;
2751         } else {
2752                 for (i=0; i<4; i++ )
2753                         joy_axis[i] = 0;
2754                 use_joystick=0;
2755         }
2756 #else   // MACINTOSH
2757         //---------  Read Joystick -----------
2758         if ((Config_control_type>0) && (Config_control_type<5) ) {
2759                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
2760                 for (i=0; i<4; i++ )    {
2761                         if (channel_masks&(1<<i))       {
2762                                 int joy_null_value = 10;
2763
2764                                 raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2765
2766                                 if (kc_joystick[23].value==i)           // If this is the throttle
2767                                         joy_null_value = 20;                            // Then use a larger dead-zone
2768
2769                                 if (raw_joy_axis[i] > joy_null_value) 
2770                                         raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2771                                 else if (raw_joy_axis[i] < -joy_null_value)
2772                                         raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2773                                 else
2774                                         raw_joy_axis[i] = 0;
2775                                 joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2776                         } else {
2777                                 joy_axis[i] = 0;
2778                         }
2779                 }       
2780                 use_joystick=1;
2781         } else {
2782                 for (i=0; i<4; i++ )
2783                         joy_axis[i] = 0;
2784                 use_joystick=0;
2785         }
2786 #endif          // ifndef MACINTOSH
2787
2788         if (Config_control_type==5 && !CybermouseActive) {
2789                 //---------  Read Mouse -----------
2790                 mouse_get_delta( &dx, &dy );
2791                 mouse_axis[0] = (dx*FrameTime)/35;
2792                 mouse_axis[1] = (dy*FrameTime)/25;
2793                 mouse_buttons = mouse_get_btns();
2794                 //mprintf(( 0, "Mouse %d,%d b:%d, 0x%x\n", mouse_axis[0], mouse_axis[1], mouse_buttons, FrameTime ));
2795                 use_mouse=1;
2796         } else if (Config_control_type==6 && !CybermouseActive) {
2797                 //---------  Read Cyberman -----------
2798                 mouse_get_cyberman_pos(&idx,&idy );
2799                 mouse_axis[0] = (idx*FrameTime)/128;
2800                 mouse_axis[1] = (idy*FrameTime)/128;
2801                 mouse_buttons = mouse_get_btns();
2802                 use_mouse=1;
2803         } else if (CybermouseActive) {
2804 //              ReadOWL (kc_external_control);
2805 //              CybermouseAdjust();
2806         } else {
2807                 mouse_axis[0] = 0;
2808                 mouse_axis[1] = 0;
2809                 mouse_buttons = 0;
2810                 use_mouse=0;
2811         }
2812
2813 //------------- Read slide_on -------------
2814         
2815         // From keyboard...
2816         if ( kc_keyboard[8].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[8].value ];
2817         if ( kc_keyboard[9].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[9].value ];
2818         // From joystick...
2819         if ((use_joystick)&&(kc_joystick[5].value<255)) slide_on |= joy_get_button_state( kc_joystick[5].value );
2820         // From mouse...
2821         if ((use_mouse)&&(kc_mouse[5].value<255)) slide_on |= mouse_buttons & (1<<kc_mouse[5].value);
2822
2823 //------------- Read bank_on ---------------
2824
2825         // From keyboard...
2826         if ( kc_keyboard[18].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[18].value ];
2827         if ( kc_keyboard[19].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[19].value ];
2828         // From joystick...
2829         if ( (use_joystick)&&(kc_joystick[10].value < 255 )) bank_on |= joy_get_button_state( kc_joystick[10].value );
2830         // From mouse...
2831         if ( (use_mouse)&&(kc_mouse[10].value < 255 )) bank_on |= mouse_buttons & (1<<kc_mouse[10].value);
2832
2833 //------------ Read pitch_time -----------
2834         if ( !slide_on )        {
2835                 // mprintf((0, "pitch: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2836                 kp = 0;
2837                 k0 = speed_factor*key_down_time( kc_keyboard[0].value )/2;      // Divide by two since we want pitch to go slower
2838                 k1 = speed_factor*key_down_time( kc_keyboard[1].value )/2;
2839                 k2 = speed_factor*key_down_time( kc_keyboard[2].value )/2;
2840                 k3 = speed_factor*key_down_time( kc_keyboard[3].value )/2;
2841
2842                 // From keyboard...
2843                 if ( kc_keyboard[0].value < 255 ) kp += k0/PH_SCALE;
2844                 if ( kc_keyboard[1].value < 255 ) kp += k1/PH_SCALE;
2845                 if ( kc_keyboard[2].value < 255 ) kp -= k2/PH_SCALE;
2846                 if ( kc_keyboard[3].value < 255 ) kp -= k3/PH_SCALE;
2847
2848                 // From Cyberman...
2849                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2850                         kp += mouse_button_down_time(MB_PITCH_FORWARD)/(PH_SCALE*2);
2851                         kp -= mouse_button_down_time(MB_PITCH_BACKWARD)/(PH_SCALE*2);
2852                 }
2853         
2854                 if (kp == 0)
2855                         Controls.pitch_time = 0;
2856                 else if (kp > 0) {
2857                         if (Controls.pitch_time < 0)
2858                                 Controls.pitch_time = 0;
2859                 } else // kp < 0
2860                         if (Controls.pitch_time > 0)
2861                                 Controls.pitch_time = 0;
2862                 Controls.pitch_time += kp;
2863         
2864                 // From joystick...
2865                 if ( (use_joystick)&&(kc_joystick[13].value < 255 ))    {
2866                         if ( !kc_joystick[14].value )           // If not inverted...
2867                                 Controls.pitch_time -= (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
2868                         else
2869                                 Controls.pitch_time += (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
2870                 }
2871         
2872                 // From mouse...
2873                 //mprintf(( 0, "UM: %d, PV: %d\n", use_mouse, kc_mouse[13].value ));
2874                 if ( (use_mouse)&&(kc_mouse[13].value < 255) )  {
2875                         if ( !kc_mouse[14].value )              // If not inverted...
2876                                 Controls.pitch_time -= (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2877                         else
2878                                 Controls.pitch_time += (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2879                 }
2880         } else {
2881                 Controls.pitch_time = 0;
2882         }
2883
2884
2885 //----------- Read vertical_thrust_time -----------------
2886
2887         if ( slide_on ) {
2888                 k0 = speed_factor*key_down_time( kc_keyboard[0].value );
2889                 k1 = speed_factor*key_down_time( kc_keyboard[1].value );
2890                 k2 = speed_factor*key_down_time( kc_keyboard[2].value );
2891                 k3 = speed_factor*key_down_time( kc_keyboard[3].value );
2892
2893                 // From keyboard...
2894                 if ( kc_keyboard[0].value < 255 ) Controls.vertical_thrust_time += k0;
2895                 if ( kc_keyboard[1].value < 255 ) Controls.vertical_thrust_time += k1;
2896                 if ( kc_keyboard[2].value < 255 ) Controls.vertical_thrust_time -= k2;
2897                 if ( kc_keyboard[3].value < 255 ) Controls.vertical_thrust_time -= k3;
2898
2899                 // From Cyberman...
2900                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2901                         Controls.vertical_thrust_time -= mouse_button_down_time(MB_PITCH_FORWARD);
2902                         Controls.vertical_thrust_time += mouse_button_down_time(MB_PITCH_BACKWARD);
2903                 }
2904         
2905                 // From joystick...
2906                 if ((use_joystick)&&( kc_joystick[13].value < 255 ))    {
2907                         if ( !kc_joystick[14].value )           // If not inverted...
2908                                 Controls.vertical_thrust_time += joy_axis[kc_joystick[13].value];
2909                         else
2910                                 Controls.vertical_thrust_time -= joy_axis[kc_joystick[13].value];
2911                 }
2912         
2913                 // From mouse...
2914                 if ( (use_mouse)&&(kc_mouse[13].value < 255 ))  {
2915                         if ( !kc_mouse[14].value )              // If not inverted...
2916                                 Controls.vertical_thrust_time -= mouse_axis[kc_mouse[13].value];
2917                         else
2918                                 Controls.vertical_thrust_time += mouse_axis[kc_mouse[13].value];
2919                 }
2920         }
2921
2922         // From keyboard...
2923         if ( kc_keyboard[14].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[14].value );
2924         if ( kc_keyboard[15].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[15].value );
2925         if ( kc_keyboard[16].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[16].value );
2926         if ( kc_keyboard[17].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[17].value );
2927         
2928         // From joystick...
2929         if ((use_joystick)&&( kc_joystick[19].value < 255 ))    {
2930                 if ( !kc_joystick[20].value )           // If not inverted...
2931                         Controls.vertical_thrust_time += joy_axis[kc_joystick[19].value];
2932                 else
2933                         Controls.vertical_thrust_time -= joy_axis[kc_joystick[19].value];
2934         }
2935
2936         // From joystick buttons
2937         if ( (use_joystick)&&(kc_joystick[8].value < 255 )) Controls.vertical_thrust_time += joy_get_button_down_time( kc_joystick[8].value );
2938         if ( (use_joystick)&&(kc_joystick[9].value < 255 )) Controls.vertical_thrust_time -= joy_get_button_down_time( kc_joystick[9].value );
2939
2940         // From mouse buttons
2941         if ( (use_mouse)&&(kc_mouse[8].value < 255 )) Controls.vertical_thrust_time += mouse_button_down_time( kc_mouse[8].value );
2942         if ( (use_mouse)&&(kc_mouse[9].value < 255 )) Controls.vertical_thrust_time -= mouse_button_down_time( kc_mouse[9].value );
2943
2944         // From mouse...
2945         if ( (use_mouse)&&(kc_mouse[19].value < 255 ))  {
2946                 if ( !kc_mouse[20].value )              // If not inverted...
2947                         Controls.vertical_thrust_time += mouse_axis[kc_mouse[19].value];
2948                 else
2949                         Controls.vertical_thrust_time -= mouse_axis[kc_mouse[19].value];
2950         }
2951
2952         // From Cyberman...
2953         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2954                 Controls.vertical_thrust_time += mouse_button_down_time(MB_Z_UP)/2;
2955                 Controls.vertical_thrust_time -= mouse_button_down_time(MB_Z_DOWN)/2;
2956         }
2957
2958 //---------- Read heading_time -----------
2959
2960         if (!slide_on && !bank_on)      {
2961                 //mprintf((0, "heading: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2962                 kh = 0;
2963                 k4 = speed_factor*key_down_time( kc_keyboard[4].value );
2964                 k5 = speed_factor*key_down_time( kc_keyboard[5].value );
2965                 k6 = speed_factor*key_down_time( kc_keyboard[6].value );
2966                 k7 = speed_factor*key_down_time( kc_keyboard[7].value );
2967
2968                 // From keyboard...
2969                 if ( kc_keyboard[4].value < 255 ) kh -= k4/PH_SCALE;
2970                 if ( kc_keyboard[5].value < 255 ) kh -= k5/PH_SCALE;
2971                 if ( kc_keyboard[6].value < 255 ) kh += k6/PH_SCALE;
2972                 if ( kc_keyboard[7].value < 255 ) kh += k7/PH_SCALE;
2973
2974                 // From Cyberman...
2975                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2976                         kh -= mouse_button_down_time(MB_HEAD_LEFT)/PH_SCALE;
2977                         kh += mouse_button_down_time(MB_HEAD_RIGHT)/PH_SCALE;
2978                 }
2979         
2980                 if (kh == 0)
2981                         Controls.heading_time = 0;
2982                 else if (kh > 0) {
2983                         if (Controls.heading_time < 0)
2984                                 Controls.heading_time = 0;
2985                 } else // kh < 0
2986                         if (Controls.heading_time > 0)
2987                                 Controls.heading_time = 0;
2988                 Controls.heading_time += kh;
2989
2990                 // From joystick...
2991                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
2992                         if ( !kc_joystick[16].value )           // If not inverted...
2993                                 Controls.heading_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
2994                         else
2995                                 Controls.heading_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
2996                 }
2997         
2998                 // From mouse...
2999                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3000                         if ( !kc_mouse[16].value )              // If not inverted...
3001                                 Controls.heading_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3002                         else
3003                                 Controls.heading_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3004                 }
3005         } else {
3006                 Controls.heading_time = 0;
3007         }
3008
3009 //----------- Read sideways_thrust_time -----------------
3010
3011         if ( slide_on ) {
3012                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
3013                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
3014                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
3015                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
3016
3017                 // From keyboard...
3018                 if ( kc_keyboard[4].value < 255 ) Controls.sideways_thrust_time -= k0;
3019                 if ( kc_keyboard[5].value < 255 ) Controls.sideways_thrust_time -= k1;
3020                 if ( kc_keyboard[6].value < 255 ) Controls.sideways_thrust_time += k2;
3021                 if ( kc_keyboard[7].value < 255 ) Controls.sideways_thrust_time += k3;
3022         
3023                 // From joystick...
3024                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
3025                         if ( !kc_joystick[16].value )           // If not inverted...
3026                                 Controls.sideways_thrust_time += joy_axis[kc_joystick[15].value];
3027                         else
3028                                 Controls.sideways_thrust_time -= joy_axis[kc_joystick[15].value];
3029                 }
3030                 
3031                 // From cyberman
3032                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3033                         Controls.sideways_thrust_time -= mouse_button_down_time(MB_HEAD_LEFT);
3034                         Controls.sideways_thrust_time += mouse_button_down_time(MB_HEAD_RIGHT);
3035                 }
3036         
3037                 // From mouse...
3038                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3039                         if ( !kc_mouse[16].value )              // If not inverted...
3040                                 Controls.sideways_thrust_time += mouse_axis[kc_mouse[15].value];
3041                         else
3042                                 Controls.sideways_thrust_time -= mouse_axis[kc_mouse[15].value];
3043                 }
3044         }
3045
3046         // From keyboard...
3047         if ( kc_keyboard[10].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[10].value );
3048         if ( kc_keyboard[11].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[11].value );
3049         if ( kc_keyboard[12].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[12].value );
3050         if ( kc_keyboard[13].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[13].value );
3051         
3052         // From joystick...
3053         if ( (use_joystick)&&(kc_joystick[17].value < 255 ))    {
3054                 if ( !kc_joystick[18].value )           // If not inverted...
3055                         Controls.sideways_thrust_time -= joy_axis[kc_joystick[17].value];
3056                 else
3057                         Controls.sideways_thrust_time += joy_axis[kc_joystick[17].value];
3058         }
3059
3060         // From joystick buttons
3061         if ( (use_joystick)&&(kc_joystick[6].value < 255 )) Controls.sideways_thrust_time -= joy_get_button_down_time( kc_joystick[6].value );
3062         if ( (use_joystick)&&(kc_joystick[7].value < 255 )) Controls.sideways_thrust_time += joy_get_button_down_time( kc_joystick[7].value );
3063
3064         // From mouse buttons
3065         if ( (use_mouse)&&(kc_mouse[6].value < 255 )) Controls.sideways_thrust_time -= mouse_button_down_time( kc_mouse[6].value );
3066         if ( (use_mouse)&&(kc_mouse[7].value < 255 )) Controls.sideways_thrust_time += mouse_button_down_time( kc_mouse[7].value );
3067
3068         // From mouse...
3069         if ( (use_mouse)&&(kc_mouse[17].value < 255 ))  {
3070                 if ( !kc_mouse[18].value )              // If not inverted...
3071                         Controls.sideways_thrust_time += mouse_axis[kc_mouse[17].value];
3072                 else
3073                         Controls.sideways_thrust_time -= mouse_axis[kc_mouse[17].value];
3074         }
3075
3076 //----------- Read bank_time -----------------
3077
3078         if ( bank_on )  {
3079                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
3080                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
3081                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
3082                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
3083
3084                 // From keyboard...
3085                 if ( kc_keyboard[4].value < 255 ) Controls.bank_time += k0;
3086                 if ( kc_keyboard[5].value < 255 ) Controls.bank_time += k1;
3087                 if ( kc_keyboard[6].value < 255 ) Controls.bank_time -= k2;
3088                 if ( kc_keyboard[7].value < 255 ) Controls.bank_time -= k3;
3089
3090                 // From Cyberman...
3091                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3092                         Controls.bank_time -= mouse_button_down_time(MB_HEAD_LEFT);
3093                         Controls.bank_time += mouse_button_down_time(MB_HEAD_RIGHT);
3094                 }
3095
3096                 // From joystick...
3097                 if ( (use_joystick)&&(kc_joystick[15].value < 255) )    {
3098                         if ( !kc_joystick[16].value )           // If not inverted...
3099                                 Controls.bank_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3100                         else
3101                                 Controls.bank_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3102                 }
3103         
3104                 // From mouse...
3105                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3106                         if ( !kc_mouse[16].value )              // If not inverted...
3107                                 Controls.bank_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3108                         else
3109                                 Controls.bank_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3110                 }
3111         }
3112
3113         // From keyboard...
3114         if ( kc_keyboard[20].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[20].value );
3115         if ( kc_keyboard[21].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[21].value );
3116         if ( kc_keyboard[22].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[22].value );
3117         if ( kc_keyboard[23].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[23].value );
3118
3119         // From joystick...
3120         if ( (use_joystick)&&(kc_joystick[21].value < 255) )    {
3121                 if ( !kc_joystick[22].value )           // If not inverted...
3122                         Controls.bank_time -= joy_axis[kc_joystick[21].value];
3123                 else
3124                         Controls.bank_time += joy_axis[kc_joystick[21].value];
3125         }
3126
3127         // From joystick buttons
3128         if ( (use_joystick)&&(kc_joystick[11].value < 255 )) Controls.bank_time += joy_get_button_down_time( kc_joystick[11].value );
3129         if ( (use_joystick)&&(kc_joystick[12].value < 255 )) Controls.bank_time -= joy_get_button_down_time( kc_joystick[12].value );
3130
3131         // From mouse buttons
3132         if ( (use_mouse)&&(kc_mouse[11].value < 255 )) Controls.bank_time += mouse_button_down_time( kc_mouse[11].value );
3133         if ( (use_mouse)&&(kc_mouse[12].value < 255 )) Controls.bank_time -= mouse_button_down_time( kc_mouse[12].value );
3134
3135         // From mouse...
3136         if ( (use_mouse)&&(kc_mouse[21].value < 255 ))  {
3137                 if ( !kc_mouse[22].value )              // If not inverted...
3138                         Controls.bank_time += mouse_axis[kc_mouse[21].value];
3139                 else
3140                         Controls.bank_time -= mouse_axis[kc_mouse[21].value];
3141         }
3142
3143         // From Cyberman
3144         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3145                 Controls.bank_time += mouse_button_down_time(MB_BANK_LEFT);
3146                 Controls.bank_time -= mouse_button_down_time(MB_BANK_RIGHT);
3147         }
3148
3149 //----------- Read forward_thrust_time -------------
3150
3151         // From keyboard...
3152         if ( kc_keyboard[30].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[30].value );
3153         if ( kc_keyboard[31].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[31].value );
3154         if ( kc_keyboard[32].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[32].value );
3155         if ( kc_keyboard[33].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[33].value );
3156
3157         // From joystick...
3158         if ( (use_joystick)&&(kc_joystick[23].value < 255 ))    {
3159                 if ( !kc_joystick[24].value )           // If not inverted...
3160                         Controls.forward_thrust_time -= joy_axis[kc_joystick[23].value];
3161                 else
3162                         Controls.forward_thrust_time += joy_axis[kc_joystick[23].value];
3163         }
3164
3165         // From joystick buttons
3166         if ( (use_joystick)&&(kc_joystick[2].value < 255 )) Controls.forward_thrust_time += joy_get_button_down_time( kc_joystick[2].value );
3167         if ( (use_joystick)&&(kc_joystick[3].value < 255 )) Controls.forward_thrust_time -= joy_get_button_down_time( kc_joystick[3].value );
3168
3169         // From mouse...
3170         if ( (use_mouse)&&(kc_mouse[23].value < 255 ))  {
3171                 if ( !kc_mouse[24].value )              // If not inverted...
3172                         Controls.forward_thrust_time -= mouse_axis[kc_mouse[23].value];
3173                 else
3174                         Controls.forward_thrust_time += mouse_axis[kc_mouse[23].value];
3175         }
3176
3177         // From mouse buttons
3178         if ( (use_mouse)&&(kc_mouse[2].value < 255 )) Controls.forward_thrust_time += mouse_button_down_time( kc_mouse[2].value );
3179         if ( (use_mouse)&&(kc_mouse[3].value < 255 )) Controls.forward_thrust_time -= mouse_button_down_time( kc_mouse[3].value );
3180
3181 //----------- Read afterburner_state -------------
3182
3183         // From keyboard...
3184         if ( kc_keyboard[46].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[46].value];
3185         if ( kc_keyboard[47].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[47].value];
3186
3187         if ( (use_mouse)&&(kc_mouse[27].value < 255 )) Controls.afterburner_state |= mouse_button_state(kc_mouse[27].value);
3188
3189         if ( (use_joystick)&&(kc_joystick[27].value < 255 )) Controls.afterburner_state |= joy_get_button_state(kc_joystick[27].value);
3190
3191 //-------Read headlight key--------------------------
3192
3193         if (kc_keyboard[52].value < 255 )
3194                    Controls.headlight_count=key_down_count(kc_keyboard[52].value);
3195         if (kc_keyboard[53].value < 255 )
3196                    Controls.headlight_count+=key_down_count(kc_keyboard[53].value);
3197                   if ((use_joystick)&&(kc_joystick[30].value < 255 )) 
3198                Controls.headlight_count+=joy_get_button_down_cnt(kc_joystick[30].value);
3199
3200 //--------Read Cycle Primary Key------------------
3201
3202                   if (kc_keyboard[48].value<255)
3203                          Controls.cycle_primary_count=key_down_count(kc_keyboard[48].value);
3204                   if (kc_keyboard[49].value<255)
3205                         Controls.cycle_primary_count+=key_down_count(kc_keyboard[49].value);
3206              if ((use_joystick)&&(kc_joystick[28].value < 255 )) 
3207          Controls.cycle_primary_count+=joy_get_button_down_cnt(kc_joystick[28].value);
3208
3209
3210 //--------Read Cycle Secondary Key------------------
3211
3212                   if (kc_keyboard[50].value<255)
3213                          Controls.cycle_secondary_count=key_down_count(kc_keyboard[50].value);
3214                   if (kc_keyboard[51].value<255)
3215                         Controls.cycle_secondary_count+=key_down_count(kc_keyboard[51].value);
3216              if ((use_joystick)&&(kc_joystick[29].value < 255 )) 
3217                         Controls.cycle_secondary_count=joy_get_button_down_cnt(kc_joystick[29].value);
3218
3219 //--------Read Toggle Bomb key----------------------
3220
3221                   if (kc_keyboard[56].value<255 && key_down_count(kc_keyboard[56].value))
3222          {
3223           int bomb = Secondary_last_was_super[PROXIMITY_INDEX]?PROXIMITY_INDEX:SMART_MINE_INDEX;
3224
3225                          if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX] &&
3226                                   !Players[Player_num].secondary_ammo[SMART_MINE_INDEX])
3227                            {
3228                                  digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
3229                                  HUD_init_message ("No bombs available!");
3230                                 }
3231                          else
3232                                 {       
3233                                  if (Players[Player_num].secondary_ammo[bomb]==0)
3234                                         {
3235                                          digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
3236                                          HUD_init_message ("No %s available!",(bomb==SMART_MINE_INDEX)?"Smart mines":"Proximity bombs");
3237                                         }
3238                                   else
3239                                         {
3240                                Secondary_last_was_super[PROXIMITY_INDEX]=!Secondary_last_was_super[PROXIMITY_INDEX];
3241                                          digi_play_sample_once( SOUND_GOOD_SELECTION_SECONDARY, F1_0 );
3242                                         }
3243                                 }
3244                         }
3245           
3246 //---------Read Energy->Shield key----------
3247
3248         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[54].value])
3249                 transfer_energy_to_shield(key_down_time(kc_keyboard[54].value));
3250         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[55].value])
3251                 transfer_energy_to_shield(key_down_time(kc_keyboard[55].value));
3252
3253 //----------- Read fire_primary_down_count
3254         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[24].value);
3255         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[25].value);
3256         if ((use_joystick)&&(kc_joystick[0].value < 255 )) Controls.fire_primary_down_count += joy_get_button_down_cnt(kc_joystick[0].value);
3257         if ((use_mouse)&&(kc_mouse[0].value < 255 )) Controls.fire_primary_down_count += mouse_button_down_count(kc_mouse[0].value);
3258
3259 //----------- Read fire_primary_state
3260         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[24].value];
3261         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[25].value];
3262         if ((use_joystick)&&(kc_joystick[0].value < 255 )) Controls.fire_primary_state |= joy_get_button_state(kc_joystick[0].value);
3263         if ((use_mouse)&&(kc_mouse[0].value < 255) ) Controls.fire_primary_state |= mouse_button_state(kc_mouse[0].value);
3264
3265 //----------- Read fire_secondary_down_count
3266         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[26].value);
3267         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[27].value);
3268         if ((use_joystick)&&(kc_joystick[1].value < 255 )) Controls.fire_secondary_down_count += joy_get_button_down_cnt(kc_joystick[1].value);
3269         if ((use_mouse)&&(kc_mouse[1].value < 255 )) Controls.fire_secondary_down_count += mouse_button_down_count(kc_mouse[1].value);
3270
3271 //----------- Read fire_secondary_state
3272         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[26].value];
3273         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[27].value];
3274         if ((use_joystick)&&(kc_joystick[1].value < 255 )) Controls.fire_secondary_state |= joy_get_button_state(kc_joystick[1].value);
3275         if ((use_mouse)&&(kc_mouse[1].value < 255) ) Controls.fire_secondary_state |= mouse_button_state(kc_mouse[1].value);
3276
3277 //----------- Read fire_flare_down_count
3278         if (kc_keyboard[28].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[28].value);
3279         if (kc_keyboard[29].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[29].value);
3280         if ((use_joystick)&&(kc_joystick[4].value < 255 )) Controls.fire_flare_down_count += joy_get_button_down_cnt(kc_joystick[4].value);
3281         if ((use_mouse)&&(kc_mouse[4].value < 255 )) Controls.fire_flare_down_count += mouse_button_down_count(kc_mouse[4].value);
3282
3283 //----------- Read drop_bomb_down_count
3284         if (kc_keyboard[34].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[34].value);
3285         if (kc_keyboard[35].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[35].value);
3286         if ((use_joystick)&&(kc_joystick[26].value < 255 )) Controls.drop_bomb_down_count += joy_get_button_down_cnt(kc_joystick[26].value);
3287         if ((use_mouse)&&(kc_mouse[26].value < 255 )) Controls.drop_bomb_down_count += mouse_button_down_count(kc_mouse[26].value);
3288
3289 //----------- Read rear_view_down_count
3290         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[36].value);
3291         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[37].value);
3292         if ((use_joystick)&&(kc_joystick[25].value < 255 )) Controls.rear_view_down_count += joy_get_button_down_cnt(kc_joystick[25].value);
3293         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_count += mouse_button_down_count(kc_mouse[25].value);
3294
3295 //----------- Read rear_view_down_state
3296         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[36].value];
3297         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[37].value];
3298         if ((use_joystick)&&(kc_joystick[25].value < 255 )) Controls.rear_view_down_state |= joy_get_button_state(kc_joystick[25].value);
3299         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_state |= mouse_button_state(kc_mouse[25].value);
3300
3301 //----------- Read automap_down_count
3302         if (kc_keyboard[44].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[44].value);
3303         if (kc_keyboard[45].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[45].value);
3304
3305 //----------- Read automap_state
3306         if (kc_keyboard[44].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[44].value];
3307         if (kc_keyboard[45].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[45].value];
3308
3309 //----------- Read stupid-cruise-control-type of throttle.
3310         {
3311                 if ( kc_keyboard[38].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[38].value)*5,FrameTime);
3312                 if ( kc_keyboard[39].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[39].value)*5,FrameTime);
3313                 if ( kc_keyboard[40].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[40].value)*5,FrameTime);
3314                 if ( kc_keyboard[41].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[41].value)*5,FrameTime);
3315                 if ( (kc_keyboard[42].value < 255) && (key_down_count(kc_keyboard[42].value)) )
3316                         Cruise_speed = 0;
3317                 if ( (kc_keyboard[43].value < 255) && (key_down_count(kc_keyboard[43].value)) )
3318                         Cruise_speed = 0;
3319         
3320                 if (Cruise_speed > i2f(100) ) Cruise_speed = i2f(100);
3321                 if (Cruise_speed < 0 ) Cruise_speed = 0;
3322         
3323                 if (Controls.forward_thrust_time==0)
3324                         Controls.forward_thrust_time = fixmul(Cruise_speed,FrameTime)/100;
3325         }
3326
3327 #if !defined(MACINTOSH)
3328         //read_head_tracker();
3329
3330         // Read external controls
3331         // if (kc_use_external_control || CybermouseActive)
3332         //      kconfig_read_external_controls();
3333 #endif
3334
3335 //----------- Clamp values between -FrameTime and FrameTime
3336         if (FrameTime > F1_0 )
3337                 mprintf( (1, "Bogus frame time of %.2f seconds\n", f2fl(FrameTime) ));
3338
3339         if (Controls.pitch_time > FrameTime/2 ) Controls.pitch_time = FrameTime/2;
3340         if (Controls.vertical_thrust_time > FrameTime ) Controls.vertical_thrust_time = FrameTime;
3341         if (Controls.heading_time > FrameTime ) Controls.heading_time = FrameTime;
3342         if (Controls.sideways_thrust_time > FrameTime ) Controls.sideways_thrust_time = FrameTime;
3343         if (Controls.bank_time > FrameTime ) Controls.bank_time = FrameTime;
3344         if (Controls.forward_thrust_time > FrameTime ) Controls.forward_thrust_time = FrameTime;
3345 //      if (Controls.afterburner_time > FrameTime ) Controls.afterburner_time = FrameTime;
3346
3347         if (Controls.pitch_time < -FrameTime/2 ) Controls.pitch_time = -FrameTime/2;
3348         if (Controls.vertical_thrust_time < -FrameTime ) Controls.vertical_thrust_time = -FrameTime;
3349         if (Controls.heading_time < -FrameTime ) Controls.heading_time = -FrameTime;
3350         if (Controls.sideways_thrust_time < -FrameTime ) Controls.sideways_thrust_time = -FrameTime;
3351         if (Controls.bank_time < -FrameTime ) Controls.bank_time = -FrameTime;
3352         if (Controls.forward_thrust_time < -FrameTime ) Controls.forward_thrust_time = -FrameTime;
3353 //      if (Controls.afterburner_time < -FrameTime ) Controls.afterburner_time = -FrameTime;
3354
3355
3356 //--------- Don't do anything if in debug mode
3357         #ifndef RELEASE
3358         if ( keyd_pressed[KEY_DELETE] ) {
3359                 memset( &Controls, 0, sizeof(control_info) );
3360         }
3361         #endif
3362 }
3363 #endif
3364
3365 void reset_cruise(void)
3366 {
3367         Cruise_speed=0;
3368 }
3369
3370
3371 void kc_set_controls()
3372 {
3373         int i;
3374
3375         for (i=0; i<NUM_KEY_CONTROLS; i++ )     
3376                 kc_keyboard[i].value = kconfig_settings[0][i];
3377
3378         if ( (Config_control_type>0) && (Config_control_type<5)) {
3379                 for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
3380                         kc_joystick[i].value = kconfig_settings[Config_control_type][i];
3381                         if (kc_joystick[i].type == BT_INVERT )  {
3382                                 if (kc_joystick[i].value!=1)
3383                                         kc_joystick[i].value    = 0;
3384                                 kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
3385                         }
3386                 }
3387         } else if (Config_control_type>4 && Config_control_type<CONTROL_WINJOYSTICK) {
3388                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
3389                         kc_mouse[i].value = kconfig_settings[Config_control_type][i];
3390                         if (kc_mouse[i].type == BT_INVERT )     {
3391                                 if (kc_mouse[i].value!=1)
3392                                         kc_mouse[i].value       = 0;
3393                                 kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
3394                         }
3395                 }
3396         } else if (Config_control_type == CONTROL_WINJOYSTICK) {
3397                 for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
3398                         kc_superjoy[i].value = kconfig_settings[Config_control_type][i];
3399                         if (kc_superjoy[i].type == BT_INVERT )  {
3400                                 if (kc_superjoy[i].value!=1)
3401                                         kc_superjoy[i].value    = 0;
3402                                 kconfig_settings[Config_control_type][i] = kc_superjoy[i].value;
3403                         }
3404                 }
3405         }
3406 }
3407
3408 #if 0 //ndef MACINTOSH  // no mac support for vr headset
3409
3410 void kconfig_center_headset()
3411 {
3412 #ifndef WINDOWS
3413         if (vfx1_installed)
3414                 vfx_center_headset();
3415 #endif
3416 //      } else if (iglasses_headset_installed)  {
3417 //      } else if (Victor_headset_installed)   {
3418 //      } else {
3419 //      }
3420
3421 }
3422
3423 #endif  // end of #ifndef MACINTOSH for kconfig_center_headset
3424
3425 void CybermouseAdjust ()
3426  {
3427 /*      if ( Player_num > -1 )  {
3428                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
3429                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
3430                 Auto_leveling_on = 0;
3431
3432                 if ( kc_external_version > 0 ) {                
3433                         vms_matrix tempm, ViewMatrix;
3434                         vms_angvec * Kconfig_abs_movement;
3435                         char * oem_message;
3436         
3437                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(control_info));
3438         
3439                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
3440                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
3441                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
3442                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
3443                         }
3444                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
3445                         if (oem_message[0] != '\0' )
3446                                 HUD_init_message( oem_message );
3447                 }
3448         }*/
3449
3450         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
3451         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
3452         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
3453         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
3454         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
3455         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
3456 //      Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
3457 //      Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
3458         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
3459         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
3460         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
3461         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
3462         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
3463         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
3464 //      Controls.automap_down_count += kc_external_control->automap_down_count;
3465 //      Controls.automap_state |= kc_external_control->automap_state;
3466   } 
3467
3468 char GetKeyValue (char key)
3469   {
3470         mprintf ((0,"Returning %c!\n",kc_keyboard[(int)key].value));
3471         return (kc_keyboard[(int)key].value);
3472   }
3473
3474 #if !defined(MACINTOSH)
3475
3476
3477 extern object *obj_find_first_of_type (int);
3478 void kconfig_read_external_controls()
3479 {
3480         //union REGS r;
3481    int i;
3482
3483         if ( !kc_enable_external_control ) return;
3484
3485         if ( kc_external_version == 0 ) 
3486                 memset( kc_external_control, 0, sizeof(ext_control_info));
3487         else if ( kc_external_version > 0 )     {
3488         
3489                 if (kc_external_version>=4)
3490                         memset( kc_external_control, 0, sizeof(advanced_ext_control_info));
3491       else if (kc_external_version>0)     
3492                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 );
3493                 else if (kc_external_version>2)
3494                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof(vms_matrix) +4 );
3495
3496                 if ( kc_external_version > 1 ) {
3497                         // Write ship pos and angles to external controls...
3498                         ubyte *temp_ptr = (ubyte *)kc_external_control;
3499                         vms_vector *ship_pos;
3500                         vms_matrix *ship_orient;
3501                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector)+sizeof(vms_matrix) );
3502                         temp_ptr += sizeof(ext_control_info) + sizeof(vms_angvec) + 64;
3503                         ship_pos = (vms_vector *)temp_ptr;
3504                         temp_ptr += sizeof(vms_vector);
3505                         ship_orient = (vms_matrix *)temp_ptr;
3506                         // Fill in ship postion...
3507                         *ship_pos = Objects[Players[Player_num].objnum].pos;
3508                         // Fill in ship orientation...
3509                         *ship_orient = Objects[Players[Player_num].objnum].orient;
3510                 }
3511     if (kc_external_version>=4)
3512           {
3513            advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
3514  
3515       temp_ptr->headlight_state=(Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON);
3516                 temp_ptr->primary_weapon_flags=Players[Player_num].primary_weapon_flags;
3517                 temp_ptr->secondary_weapon_flags=Players[Player_num].secondary_weapon_flags;
3518       temp_ptr->current_primary_weapon=Primary_weapon;
3519       temp_ptr->current_secondary_weapon=Secondary_weapon;
3520
3521       temp_ptr->current_guidebot_command=Escort_goal_object;
3522
3523            temp_ptr->force_vector=ExtForceVec;
3524                 temp_ptr->force_matrix=ExtApplyForceMatrix;
3525            for (i=0;i<3;i++)
3526        temp_ptr->joltinfo[i]=ExtJoltInfo[i];  
3527       for (i=0;i<2;i++)
3528                    temp_ptr->x_vibrate_info[i]=ExtXVibrateInfo[i];
3529                 temp_ptr->x_vibrate_clear=ExtXVibrateClear;
3530            temp_ptr->game_status=ExtGameStatus;
3531    
3532       memset ((void *)&ExtForceVec,0,sizeof(vms_vector));
3533       memset ((void *)&ExtApplyForceMatrix,0,sizeof(vms_matrix));
3534       
3535       for (i=0;i<3;i++)
3536                  ExtJoltInfo[i]=0;
3537       for (i=0;i<2;i++)
3538                  ExtXVibrateInfo[i]=0;
3539       ExtXVibrateClear=0;
3540      }
3541         }
3542
3543         if ( Automap_active )                   // (If in automap...)
3544                 kc_external_control->automap_state = 1;
3545         //memset(&r,0,sizeof(r));
3546
3547   #if !defined WINDOWS && !defined __ENV_LINUX__ && !defined __ENV_DJGPP__
3548  
3549         int386 ( kc_external_intno, &r, &r);            // Read external info...
3550
3551   #endif 
3552
3553         if ( Player_num > -1 )  {
3554                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
3555                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
3556                 Auto_leveling_on = 0;
3557
3558                 if ( kc_external_version > 0 ) {                
3559                         vms_matrix tempm, ViewMatrix;
3560                         vms_angvec * Kconfig_abs_movement;
3561                         char * oem_message;
3562         
3563                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(ext_control_info));
3564         
3565                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
3566                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
3567                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
3568                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
3569                         }
3570                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
3571                         if (oem_message[0] != '\0' )
3572                                 HUD_init_message( oem_message );
3573                 }
3574         }
3575
3576         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
3577         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
3578         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
3579         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
3580         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
3581         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
3582         Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
3583         Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
3584         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
3585         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
3586         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
3587         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
3588         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
3589         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
3590         Controls.automap_down_count += kc_external_control->automap_down_count;
3591         Controls.automap_state |= kc_external_control->automap_state;
3592         
3593    if (kc_external_version>=3)
3594          {
3595                 ubyte *temp_ptr = (ubyte *)kc_external_control;
3596                 temp_ptr += (sizeof(ext_control_info) + sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof (vms_matrix));
3597   
3598            if (*(temp_ptr))
3599                  Controls.cycle_primary_count=(*(temp_ptr));
3600            if (*(temp_ptr+1))
3601                  Controls.cycle_secondary_count=(*(temp_ptr+1));
3602
3603                 if (*(temp_ptr+2))
3604                  Controls.afterburner_state=(*(temp_ptr+2));
3605                 if (*(temp_ptr+3))
3606                  Controls.headlight_count=(*(temp_ptr+3));
3607          }
3608    if (kc_external_version>=4)
3609          {
3610      int i;
3611           advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
3612      
3613      for (i=0;i<128;i++)
3614            if (temp_ptr->keyboard[i])
3615                         key_putkey (i);
3616
3617      if (temp_ptr->Reactor_blown)
3618       {
3619        if (Game_mode & GM_MULTI)
3620                     net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
3621                  else
3622                          do_controlcen_destroyed_stuff(obj_find_first_of_type (OBJ_CNTRLCEN));
3623            }
3624     }
3625   
3626 }
3627 #endif
3628