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