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