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