DEADSOFTWARE

83508f3ae090329f44ee33599377eceb31c9e6c1
[d2df-sdl.git] / src / engine / e_input.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_input;
19 interface
21 uses
22 SysUtils,
23 e_log,
24 SDL2;
26 const
27 e_MaxKbdKeys = SDL_NUM_SCANCODES;
28 e_MaxJoys = 4;
29 e_MaxJoyBtns = 32;
30 e_MaxJoyAxes = 8;
31 e_MaxJoyHats = 8;
32 e_MaxVirtKeys = 48;
34 e_MaxJoyKeys = e_MaxJoyBtns + e_MaxJoyAxes*2 + e_MaxJoyHats*4;
36 e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys + e_MaxVirtKeys - 1;
37 // $$$..$$$ - 321 Keyboard buttons/keys
38 // $$$..$$$ - 4*32 Joystick buttons
39 // $$$..$$$ - 8*2 Joystick axes (- and +)
40 // $$$..$$$ - 4*4 Joystick hats (L U R D)
41 // $$$..$$$ - 48 Virtual buttons/keys
43 // these are apparently used in g_gui and g_game and elsewhere
44 IK_INVALID = 65535;
45 IK_ESCAPE = SDL_SCANCODE_ESCAPE;
46 IK_RETURN = SDL_SCANCODE_RETURN;
47 IK_KPRETURN= SDL_SCANCODE_KP_ENTER;
48 IK_ENTER = SDL_SCANCODE_RETURN;
49 IK_KPINSERT = SDL_SCANCODE_KP_0;
50 IK_UP = SDL_SCANCODE_UP;
51 IK_KPUP = SDL_SCANCODE_KP_8;
52 IK_DOWN = SDL_SCANCODE_DOWN;
53 IK_KPDOWN = SDL_SCANCODE_KP_2;
54 IK_LEFT = SDL_SCANCODE_LEFT;
55 IK_KPLEFT = SDL_SCANCODE_KP_4;
56 IK_RIGHT = SDL_SCANCODE_RIGHT;
57 IK_KPRIGHT = SDL_SCANCODE_KP_6;
58 IK_DELETE = SDL_SCANCODE_DELETE;
59 IK_HOME = SDL_SCANCODE_HOME;
60 IK_KPHOME = SDL_SCANCODE_KP_7;
61 IK_INSERT = SDL_SCANCODE_INSERT;
62 IK_SPACE = SDL_SCANCODE_SPACE;
63 IK_CONTROL = SDL_SCANCODE_LCTRL;
64 IK_SHIFT = SDL_SCANCODE_LSHIFT;
65 IK_ALT = SDL_SCANCODE_LALT;
66 IK_TAB = SDL_SCANCODE_TAB;
67 IK_PAGEUP = SDL_SCANCODE_PAGEUP;
68 IK_KPPAGEUP= SDL_SCANCODE_KP_9;
69 IK_PAGEDN = SDL_SCANCODE_PAGEDOWN;
70 IK_KPPAGEDN= SDL_SCANCODE_KP_3;
71 IK_KP5 = SDL_SCANCODE_KP_5;
72 IK_NUMLOCK = SDL_SCANCODE_NUMLOCKCLEAR;
73 IK_KPDIVIDE= SDL_SCANCODE_KP_DIVIDE;
74 IK_KPMULTIPLE= SDL_SCANCODE_KP_MULTIPLY;
75 IK_KPMINUS = SDL_SCANCODE_KP_MINUS;
76 IK_KPPLUS = SDL_SCANCODE_KP_PLUS;
77 IK_KPENTER = SDL_SCANCODE_KP_ENTER;
78 IK_KPDOT = SDL_SCANCODE_KP_PERIOD;
79 IK_CAPSLOCK= SDL_SCANCODE_CAPSLOCK;
80 IK_RSHIFT = SDL_SCANCODE_RSHIFT;
81 IK_CTRL = SDL_SCANCODE_LCTRL;
82 IK_RCTRL = SDL_SCANCODE_RCTRL;
83 IK_RALT = SDL_SCANCODE_RALT;
84 IK_WIN = SDL_SCANCODE_LGUI;
85 IK_RWIN = SDL_SCANCODE_RGUI;
86 IK_MENU = SDL_SCANCODE_MENU;
87 IK_PRINTSCR= SDL_SCANCODE_PRINTSCREEN;
88 IK_SCROLLLOCK= SDL_SCANCODE_SCROLLLOCK;
89 IK_LBRACKET= SDL_SCANCODE_LEFTBRACKET;
90 IK_RBRACKET= SDL_SCANCODE_RIGHTBRACKET;
91 IK_SEMICOLON= SDL_SCANCODE_SEMICOLON;
92 IK_QUOTE = SDL_SCANCODE_APOSTROPHE;
93 IK_BACKSLASH= SDL_SCANCODE_BACKSLASH;
94 IK_SLASH = SDL_SCANCODE_SLASH;
95 IK_COMMA = SDL_SCANCODE_COMMA;
96 IK_DOT = SDL_SCANCODE_PERIOD;
97 IK_EQUALS = SDL_SCANCODE_EQUALS;
98 IK_0 = SDL_SCANCODE_0;
99 IK_1 = SDL_SCANCODE_1;
100 IK_2 = SDL_SCANCODE_2;
101 IK_3 = SDL_SCANCODE_3;
102 IK_4 = SDL_SCANCODE_4;
103 IK_5 = SDL_SCANCODE_5;
104 IK_6 = SDL_SCANCODE_6;
105 IK_7 = SDL_SCANCODE_7;
106 IK_8 = SDL_SCANCODE_8;
107 IK_9 = SDL_SCANCODE_9;
108 IK_F1 = SDL_SCANCODE_F1;
109 IK_F2 = SDL_SCANCODE_F2;
110 IK_F3 = SDL_SCANCODE_F3;
111 IK_F4 = SDL_SCANCODE_F4;
112 IK_F5 = SDL_SCANCODE_F5;
113 IK_F6 = SDL_SCANCODE_F6;
114 IK_F7 = SDL_SCANCODE_F7;
115 IK_F8 = SDL_SCANCODE_F8;
116 IK_F9 = SDL_SCANCODE_F9;
117 IK_F10 = SDL_SCANCODE_F10;
118 IK_F11 = SDL_SCANCODE_F11;
119 IK_F12 = SDL_SCANCODE_F12;
120 IK_END = SDL_SCANCODE_END;
121 IK_KPEND = SDL_SCANCODE_KP_1;
122 IK_BACKSPACE = SDL_SCANCODE_BACKSPACE;
123 IK_BACKQUOTE = SDL_SCANCODE_GRAVE;
124 IK_GRAVE = SDL_SCANCODE_GRAVE;
125 IK_PAUSE = SDL_SCANCODE_PAUSE;
126 IK_Y = SDL_SCANCODE_Y;
127 IK_N = SDL_SCANCODE_N;
128 IK_W = SDL_SCANCODE_W;
129 IK_A = SDL_SCANCODE_A;
130 IK_S = SDL_SCANCODE_S;
131 IK_D = SDL_SCANCODE_D;
132 IK_Q = SDL_SCANCODE_Q;
133 IK_E = SDL_SCANCODE_E;
134 IK_H = SDL_SCANCODE_H;
135 IK_J = SDL_SCANCODE_J;
136 IK_T = SDL_SCANCODE_T;
137 IK_Z = SDL_SCANCODE_Z;
138 IK_MINUS = SDL_SCANCODE_MINUS;
139 // TODO: think of something better than this shit
140 IK_LASTKEY = SDL_NUM_SCANCODES-1;
142 VK_FIRSTKEY = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys;
143 VK_LEFT = VK_FIRSTKEY + 0;
144 VK_RIGHT = VK_FIRSTKEY + 1;
145 VK_UP = VK_FIRSTKEY + 2;
146 VK_DOWN = VK_FIRSTKEY + 3;
147 VK_FIRE = VK_FIRSTKEY + 4;
148 VK_OPEN = VK_FIRSTKEY + 5;
149 VK_JUMP = VK_FIRSTKEY + 6;
150 VK_CHAT = VK_FIRSTKEY + 7;
151 VK_ESCAPE = VK_FIRSTKEY + 8;
152 VK_0 = VK_FIRSTKEY + 9;
153 VK_1 = VK_FIRSTKEY + 10;
154 VK_2 = VK_FIRSTKEY + 11;
155 VK_3 = VK_FIRSTKEY + 12;
156 VK_4 = VK_FIRSTKEY + 13;
157 VK_5 = VK_FIRSTKEY + 14;
158 VK_6 = VK_FIRSTKEY + 15;
159 VK_7 = VK_FIRSTKEY + 16;
160 VK_8 = VK_FIRSTKEY + 17;
161 VK_9 = VK_FIRSTKEY + 18;
162 VK_A = VK_FIRSTKEY + 19;
163 VK_B = VK_FIRSTKEY + 20;
164 VK_C = VK_FIRSTKEY + 21;
165 VK_D = VK_FIRSTKEY + 22;
166 VK_E = VK_FIRSTKEY + 23;
167 VK_F = VK_FIRSTKEY + 24;
168 VK_CONSOLE = VK_FIRSTKEY + 25;
169 VK_STATUS = VK_FIRSTKEY + 26;
170 VK_TEAM = VK_FIRSTKEY + 27;
171 VK_PREV = VK_FIRSTKEY + 28;
172 VK_NEXT = VK_FIRSTKEY + 29;
173 VK_STRAFE = VK_FIRSTKEY + 30;
174 VK_LSTRAFE = VK_FIRSTKEY + 31;
175 VK_RSTRAFE = VK_FIRSTKEY + 32;
176 VK_PRINTSCR = VK_FIRSTKEY + 33;
177 VK_SHOWKBD = VK_FIRSTKEY + 34;
178 VK_HIDEKBD = VK_FIRSTKEY + 35;
179 VK_LASTKEY = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys + e_MaxVirtKeys - 1;
181 AX_MINUS = 0;
182 AX_PLUS = 1;
183 HAT_LEFT = 0;
184 HAT_UP = 1;
185 HAT_RIGHT = 2;
186 HAT_DOWN = 3;
188 function e_InitInput(): Boolean;
189 procedure e_ReleaseInput();
190 procedure e_ClearInputBuffer();
191 //function e_PollInput(): Boolean;
192 procedure e_PollJoysticks(); // call this from message loop to update joysticks
193 function e_KeyPressed(Key: Word): Boolean;
194 function e_AnyKeyPressed(): Boolean;
195 function e_GetFirstKeyPressed(): Word;
196 function e_JoystickStateToString(mode: Integer): String;
197 function e_JoyByHandle(handle: Word): Integer;
198 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
199 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
200 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
201 procedure e_SetKeyState(key: Word; state: Integer);
203 procedure e_UnpressAllKeys ();
204 procedure e_KeyUpDown (key: Word; down: Boolean);
206 type
207 TJoystick = record
208 ID: Byte;
209 Handle: PSDL_Joystick;
210 Axes: Byte;
211 Buttons: Byte;
212 Hats: Byte;
213 ButtBuf: array [0..e_MaxJoyBtns] of Boolean;
214 AxisBuf: array [0..e_MaxJoyAxes] of Integer;
215 AxisZero: array [0..e_MaxJoyAxes] of Integer;
216 HatBuf: array [0..e_MaxJoyHats] of array [HAT_LEFT..HAT_DOWN] of Boolean;
217 end;
219 var
220 {e_MouseInfo: TMouseInfo;}
221 e_EnableInput: Boolean = False;
222 e_JoysticksAvailable: Byte = 0;
223 e_JoystickDeadzones: array [0..e_MaxJoys-1] of Integer = (8192, 8192, 8192, 8192);
224 e_KeyNames: array [0..e_MaxInputKeys] of String;
225 Joysticks: array of TJoystick = nil;
227 implementation
229 uses Math;
231 const
232 KBRD_END = e_MaxKbdKeys;
233 JOYK_BEG = KBRD_END;
234 JOYK_END = JOYK_BEG + e_MaxJoyBtns*e_MaxJoys;
235 JOYA_BEG = JOYK_END;
236 JOYA_END = JOYA_BEG + e_MaxJoyAxes*2*e_MaxJoys;
237 JOYH_BEG = JOYA_END;
238 JOYH_END = JOYH_BEG + e_MaxJoyHats*4*e_MaxJoys;
239 VIRT_BEG = JOYH_END;
240 VIRT_END = VIRT_BEG + e_MaxVirtKeys;
242 var
243 KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
244 JoyBuffer: array [JOYK_BEG..JOYH_END] of Boolean;
245 VirtBuffer: array [0..e_MaxVirtKeys] of Boolean;
247 function OpenJoysticks(): Byte;
248 var
249 i, j, k, c: Integer;
250 joy: PSDL_Joystick;
251 begin
252 Result := 0;
253 k := Min(e_MaxJoys, SDL_NumJoysticks());
254 if k = 0 then Exit;
255 c := 0;
256 for i := 0 to k do
257 begin
258 joy := SDL_JoystickOpen(i);
259 if joy <> nil then
260 begin
261 Inc(c);
262 e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' (' + SDL_JoystickName(joy) +
263 ') as joystick ' + IntToStr(c) + ':', TMsgType.Notify);
264 SetLength(Joysticks, c);
265 with Joysticks[c-1] do
266 begin
267 ID := i;
268 Handle := joy;
269 Axes := Min(e_MaxJoyAxes, SDL_JoystickNumAxes(joy));
270 Buttons := Min(e_MaxJoyBtns, SDL_JoystickNumButtons(joy));
271 Hats := Min(e_MaxJoyHats, SDL_JoystickNumHats(joy));
272 // TODO: find proper solution for this xbox trigger shit
273 for j := 0 to Axes do AxisZero[j] := SDL_JoystickGetAxis(joy, j);
274 e_WriteLog(' ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
275 IntToStr(Hats) + ' hats.', TMsgType.Notify);
276 end;
277 end;
278 end;
279 Result := c;
280 end;
282 procedure ReleaseJoysticks();
283 var
284 i: Integer;
285 begin
286 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
287 for i := Low(Joysticks) to High(Joysticks) do
288 with Joysticks[i] do
289 SDL_JoystickClose(Handle);
290 SetLength(Joysticks, 0);
291 end;
294 procedure e_UnpressAllKeys ();
295 var
296 i: Integer;
297 begin
298 for i := 0 to High(KeyBuffer) do KeyBuffer[i] := False;
299 for i := JOYK_BEG to JOYH_END do JoyBuffer[i] := False;
300 for i := 0 to High(VirtBuffer) do VirtBuffer[i] := False;
301 end;
304 procedure e_KeyUpDown (key: Word; down: Boolean);
305 begin
306 if (key > 0) and (key < Length(KeyBuffer)) then KeyBuffer[key] := down
307 else if (key >= JOYK_BEG) and (key < JOYH_END) then JoyBuffer[key] := down
308 else if (key >= VIRT_BEG) and (key < VIRT_END) then VirtBuffer[key - VIRT_BEG] := down
309 end;
312 function PollKeyboard(): Boolean;
314 var
315 Keys: PByte;
316 NKeys: Integer;
317 i: NativeUInt;
319 begin
320 Result := False;
322 Keys := SDL_GetKeyboardState(@NKeys);
323 if (Keys = nil) or (NKeys < 1) then Exit;
324 for i := 0 to NKeys do
325 begin
326 if ((PByte(NativeUInt(Keys) + i)^) <> 0) then KeyBuffer[i] := false;
327 end;
328 for i := NKeys to High(KeyBuffer) do KeyBuffer[i] := False;
330 end;
332 procedure e_PollJoysticks();
333 var
334 i, j: Word;
335 hat: Byte;
336 begin
337 //Result := False;
338 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
339 SDL_JoystickUpdate();
340 for j := Low(Joysticks) to High(Joysticks) do
341 begin
342 with Joysticks[j] do
343 begin
344 for i := 0 to Buttons do ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
345 for i := 0 to Axes do AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
346 for i := 0 to Hats do
347 begin
348 hat := SDL_JoystickGetHat(Handle, i);
349 HatBuf[i, HAT_UP] := LongBool(hat and SDL_HAT_UP);
350 HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);
351 HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);
352 HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
353 end;
354 end;
355 end;
356 end;
358 procedure GenerateKeyNames();
359 var
360 i, j, k: LongWord;
361 begin
362 // keyboard key names
363 e_KeyNames[IK_0] := '0';
364 e_KeyNames[IK_1] := '1';
365 e_KeyNames[IK_2] := '2';
366 e_KeyNames[IK_3] := '3';
367 e_KeyNames[IK_4] := '4';
368 e_KeyNames[IK_5] := '5';
369 e_KeyNames[IK_6] := '6';
370 e_KeyNames[IK_7] := '7';
371 e_KeyNames[IK_8] := '8';
372 e_KeyNames[IK_9] := '9';
373 for i := IK_A to IK_Z do
374 e_KeyNames[i] := '' + chr(ord('a') + (i - IK_a));
375 e_KeyNames[IK_ESCAPE] := 'ESCAPE';
376 e_KeyNames[IK_ENTER] := 'ENTER';
377 e_KeyNames[IK_TAB] := 'TAB';
378 e_KeyNames[IK_BACKSPACE] := 'BACKSPACE';
379 e_KeyNames[IK_SPACE] := 'SPACE';
380 e_KeyNames[IK_UP] := 'UP';
381 e_KeyNames[IK_LEFT] := 'LEFT';
382 e_KeyNames[IK_RIGHT] := 'RIGHT';
383 e_KeyNames[IK_DOWN] := 'DOWN';
384 e_KeyNames[IK_INSERT] := 'INSERT';
385 e_KeyNames[IK_DELETE] := 'DELETE';
386 e_KeyNames[IK_HOME] := 'HOME';
387 e_KeyNames[IK_END] := 'END';
388 e_KeyNames[IK_PAGEUP] := 'PGUP';
389 e_KeyNames[IK_PAGEDN] := 'PGDOWN';
390 e_KeyNames[IK_KPINSERT] := 'PAD0';
391 e_KeyNames[IK_KPEND] := 'PAD1';
392 e_KeyNames[IK_KPDOWN] := 'PAD2';
393 e_KeyNames[IK_KPPAGEDN] := 'PAD3';
394 e_KeyNames[IK_KPLEFT] := 'PAD4';
395 e_KeyNames[IK_KP5] := 'PAD5';
396 e_KeyNames[IK_KPRIGHT] := 'PAD6';
397 e_KeyNames[IK_KPHOME] := 'PAD7';
398 e_KeyNames[IK_KPUP] := 'PAD8';
399 e_KeyNames[IK_KPPAGEUP] := 'PAD9';
400 e_KeyNames[IK_NUMLOCK] := 'NUM';
401 e_KeyNames[IK_KPDIVIDE] := 'PAD/';
402 e_KeyNames[IK_KPMULTIPLE] := 'PAD*';
403 e_KeyNames[IK_KPMINUS] := 'PAD-';
404 e_KeyNames[IK_KPPLUS] := 'PAD+';
405 e_KeyNames[IK_KPENTER] := 'PADENTER';
406 e_KeyNames[IK_KPDOT] := 'PAD.';
407 e_KeyNames[IK_CAPSLOCK] := 'CAPS';
408 e_KeyNames[IK_BACKQUOTE] := 'BACKQUOTE';
409 e_KeyNames[IK_F1] := 'F1';
410 e_KeyNames[IK_F2] := 'F2';
411 e_KeyNames[IK_F3] := 'F3';
412 e_KeyNames[IK_F4] := 'F4';
413 e_KeyNames[IK_F5] := 'F5';
414 e_KeyNames[IK_F6] := 'F6';
415 e_KeyNames[IK_F7] := 'F7';
416 e_KeyNames[IK_F8] := 'F8';
417 e_KeyNames[IK_F9] := 'F9';
418 e_KeyNames[IK_F10] := 'F10';
419 e_KeyNames[IK_F11] := 'F11';
420 e_KeyNames[IK_F12] := 'F12';
421 e_KeyNames[IK_SHIFT] := 'LSHIFT';
422 e_KeyNames[IK_RSHIFT] := 'RSHIFT';
423 e_KeyNames[IK_CTRL] := 'LCTRL';
424 e_KeyNames[IK_RCTRL] := 'RCTRL';
425 e_KeyNames[IK_ALT] := 'LALT';
426 e_KeyNames[IK_RALT] := 'RALT';
427 e_KeyNames[IK_WIN] := 'LWIN';
428 e_KeyNames[IK_RWIN] := 'RWIN';
429 e_KeyNames[IK_MENU] := 'MENU';
430 e_KeyNames[IK_PRINTSCR] := 'PSCRN';
431 e_KeyNames[IK_SCROLLLOCK] := 'SCROLL';
432 e_KeyNames[IK_PAUSE] := 'PAUSE';
433 e_KeyNames[IK_LBRACKET] := '[';
434 e_KeyNames[IK_RBRACKET] := ']';
435 e_KeyNames[IK_SEMICOLON] := ';';
436 e_KeyNames[IK_QUOTE] := '''';
437 e_KeyNames[IK_BACKSLASH] := '\';
438 e_KeyNames[IK_SLASH] := '/';
439 e_KeyNames[IK_COMMA] := ',';
440 e_KeyNames[IK_DOT] := '.';
441 e_KeyNames[IK_MINUS] := '-';
442 e_KeyNames[IK_EQUALS] := '=';
444 //for i := 0 to IK_LASTKEY do
445 // e_KeyNames[i] := SDL_GetScancodeName(i);
447 // joysticks
448 for j := 0 to e_MaxJoys-1 do
449 begin
450 k := JOYK_BEG + j * e_MaxJoyBtns;
451 // buttons
452 for i := 0 to e_MaxJoyBtns-1 do
453 e_KeyNames[k + i] := Format('JOY%dB%d', [j, i]);
454 k := JOYA_BEG + j * e_MaxJoyAxes * 2;
455 // axes
456 for i := 0 to e_MaxJoyAxes-1 do
457 begin
458 e_KeyNames[k + i*2 ] := Format('JOY%dA%d+', [j, i]);
459 e_KeyNames[k + i*2 + 1] := Format('JOY%dA%d-', [j, i]);
460 end;
461 k := JOYH_BEG + j * e_MaxJoyHats * 4;
462 // hats
463 for i := 0 to e_MaxJoyHats-1 do
464 begin
465 e_KeyNames[k + i*4 ] := Format('JOY%dD%dL', [j, i]);
466 e_KeyNames[k + i*4 + 1] := Format('JOY%dD%dU', [j, i]);
467 e_KeyNames[k + i*4 + 2] := Format('JOY%dD%dR', [j, i]);
468 e_KeyNames[k + i*4 + 3] := Format('JOY%dD%dD', [j, i]);
469 end;
470 end;
472 // vitrual keys
473 for i := 0 to e_MaxVirtKeys-1 do
474 e_KeyNames[VIRT_BEG + i] := 'VIRTUAL' + IntToStr(i);
475 end;
477 function e_InitInput(): Boolean;
478 begin
479 Result := False;
481 e_JoysticksAvailable := OpenJoysticks();
482 e_EnableInput := True;
483 GenerateKeyNames();
485 Result := True;
486 end;
488 procedure e_ReleaseInput();
489 begin
490 ReleaseJoysticks();
491 e_JoysticksAvailable := 0;
492 end;
494 procedure e_ClearInputBuffer();
495 var
496 i, j, d: Integer;
497 begin
498 for i := Low(KeyBuffer) to High(KeyBuffer) do
499 KeyBuffer[i] := False;
500 for i := JOYK_BEG to JOYH_END do
501 JoyBuffer[i] := False;
502 for i := Low(VirtBuffer) to High(VirtBuffer) do
503 VirtBuffer[i] := False;
504 (***
505 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then
506 for i := Low(Joysticks) to High(Joysticks) do
507 begin
508 for j := Low(Joysticks[i].ButtBuf) to High(Joysticks[i].ButtBuf) do
509 Joysticks[i].ButtBuf[j] := False;
510 for j := Low(Joysticks[i].AxisBuf) to High(Joysticks[i].AxisBuf) do
511 Joysticks[i].AxisBuf[j] := 0;
512 for j := Low(Joysticks[i].HatBuf) to High(Joysticks[i].HatBuf) do
513 for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
514 Joysticks[i].HatBuf[j, d] := False;
515 end;
516 ***)
517 end;
520 function e_PollInput(): Boolean;
521 var
522 kb, js: Boolean;
523 begin
524 kb := PollKeyboard();
525 js := e_PollJoysticks();
527 Result := kb or js;
528 end;
531 function e_KeyPressed(Key: Word): Boolean;
532 var
533 joyi, dir: Integer;
534 begin
535 Result := False;
536 if (Key = IK_INVALID) or (Key = 0) then Exit;
538 if (Key < KBRD_END) then
539 Result := KeyBuffer[Key]
540 else if (Key >= JOYK_BEG) and (Key < JOYH_END) then
541 Result := JoyBuffer[Key]
542 else if (Key >= VIRT_BEG) and (Key < VIRT_END) then
543 Result := VirtBuffer[Key - VIRT_BEG]
545 (***
546 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
547 begin // Joystick buttons
548 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
549 if JoyI >= e_JoysticksAvailable then
550 Result := False
551 else
552 begin
553 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
554 Result := Joysticks[JoyI].ButtBuf[Key];
555 end;
556 end
558 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
559 begin // Joystick axes
560 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
561 if JoyI >= e_JoysticksAvailable then
562 Result := False
563 else
564 begin
565 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
566 dir := Key mod 2;
567 if dir = AX_MINUS then
568 Result := Joysticks[JoyI].AxisBuf[Key div 2] <
569 Joysticks[JoyI].AxisZero[Key div 2] - e_JoystickDeadzones[JoyI]
570 else
571 Result := Joysticks[JoyI].AxisBuf[Key div 2] >
572 Joysticks[JoyI].AxisZero[Key div 2] + e_JoystickDeadzones[JoyI]
573 end;
574 end
576 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
577 begin // Joystick hats
578 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
579 if JoyI >= e_JoysticksAvailable then
580 Result := False
581 else
582 begin
583 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
584 dir := Key mod 4;
585 Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
586 end;
587 end
588 ***)
589 end;
591 procedure e_SetKeyState(key: Word; state: Integer);
592 var
593 JoyI, dir: Integer;
594 begin
595 if (Key = IK_INVALID) or (Key = 0) then Exit;
597 if (Key < KBRD_END) then
598 keyBuffer[key] := (state <> 0)
599 else if (Key >= JOYK_BEG) and (Key <= JOYH_END) then
600 JoyBuffer[key] := (state <> 0)
601 else if (Key >= VIRT_BEG) and (Key <= VIRT_END) then
602 VirtBuffer[Key - VIRT_BEG] := (state <> 0)
604 (***
605 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
606 begin // Joystick buttons
607 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
608 if JoyI >= e_JoysticksAvailable then
609 Exit
610 else
611 begin
612 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
613 Joysticks[JoyI].ButtBuf[Key] := (state <> 0);
614 end;
615 end
617 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
618 begin // Joystick axes
619 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
620 if JoyI >= e_JoysticksAvailable then
621 Exit
622 else
623 begin
624 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
625 Joysticks[JoyI].AxisBuf[Key div 2] := state;
626 end;
627 end
629 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
630 begin // Joystick hats
631 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
632 if JoyI >= e_JoysticksAvailable then
633 Exit
634 else
635 begin
636 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
637 dir := Key mod 4;
638 Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
639 end;
640 end
641 ***)
642 end;
644 function e_AnyKeyPressed(): Boolean;
645 var
646 k: Word;
647 begin
648 Result := False;
650 for k := 1 to e_MaxInputKeys do
651 if e_KeyPressed(k) then
652 begin
653 Result := True;
654 Break;
655 end;
656 end;
658 function e_GetFirstKeyPressed(): Word;
659 var
660 k: Word;
661 begin
662 Result := IK_INVALID;
664 for k := 1 to e_MaxInputKeys do
665 if e_KeyPressed(k) then
666 begin
667 Result := k;
668 Break;
669 end;
670 end;
672 ////////////////////////////////////////////////////////////////////////////////
674 function e_JoystickStateToString(mode: Integer): String;
675 begin
676 Result := '';
677 end;
679 function e_JoyByHandle(handle: Word): Integer;
680 var
681 i: Integer;
682 begin
683 Result := -1;
684 if Joysticks = nil then Exit;
685 for i := Low(Joysticks) to High(Joysticks) do
686 if Joysticks[i].ID = handle then
687 begin
688 Result := i;
689 Exit;
690 end;
691 end;
693 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
694 begin
695 Result := 0;
696 if id >= e_MaxJoys then Exit;
697 Result := JOYK_BEG + id*e_MaxJoyBtns + btn;
698 end;
700 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
701 begin
702 Result := 0;
703 if id >= e_MaxJoys then Exit;
704 Result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir;
705 end;
707 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
708 begin
709 Result := 0;
710 if id >= e_MaxJoys then Exit;
711 Result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir;
712 end;
715 end.