DEADSOFTWARE

Holmes should eat player keys now
[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;
33 e_MaxJoyKeys = e_MaxJoyBtns + e_MaxJoyAxes*2 + e_MaxJoyHats*4;
35 e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys - 1;
36 // $$$..$$$ - 321 Keyboard buttons/keys
37 // $$$..$$$ - 4*32 Joystick buttons
38 // $$$..$$$ - 8*2 Joystick axes (- and +)
39 // $$$..$$$ - 4*4 Joystick hats (L U R D)
41 // these are apparently used in g_gui and g_game and elsewhere
42 IK_INVALID = 65535;
43 IK_ESCAPE = SDL_SCANCODE_ESCAPE;
44 IK_RETURN = SDL_SCANCODE_RETURN;
45 IK_KPRETURN= SDL_SCANCODE_KP_ENTER;
46 IK_ENTER = SDL_SCANCODE_RETURN;
47 IK_UP = SDL_SCANCODE_UP;
48 IK_KPUP = SDL_SCANCODE_KP_8;
49 IK_DOWN = SDL_SCANCODE_DOWN;
50 IK_KPDOWN = SDL_SCANCODE_KP_2;
51 IK_LEFT = SDL_SCANCODE_LEFT;
52 IK_KPLEFT = SDL_SCANCODE_KP_4;
53 IK_RIGHT = SDL_SCANCODE_RIGHT;
54 IK_KPRIGHT = SDL_SCANCODE_KP_6;
55 IK_DELETE = SDL_SCANCODE_DELETE;
56 IK_HOME = SDL_SCANCODE_HOME;
57 IK_KPHOME = SDL_SCANCODE_KP_7;
58 IK_INSERT = SDL_SCANCODE_INSERT;
59 IK_SPACE = SDL_SCANCODE_SPACE;
60 IK_CONTROL = SDL_SCANCODE_LCTRL;
61 IK_SHIFT = SDL_SCANCODE_LSHIFT;
62 IK_TAB = SDL_SCANCODE_TAB;
63 IK_PAGEUP = SDL_SCANCODE_PAGEUP;
64 IK_KPPAGEUP= SDL_SCANCODE_KP_9;
65 IK_PAGEDN = SDL_SCANCODE_PAGEDOWN;
66 IK_KPPAGEDN= SDL_SCANCODE_KP_3;
67 IK_F2 = SDL_SCANCODE_F2;
68 IK_F3 = SDL_SCANCODE_F3;
69 IK_F4 = SDL_SCANCODE_F4;
70 IK_F5 = SDL_SCANCODE_F5;
71 IK_F6 = SDL_SCANCODE_F6;
72 IK_F7 = SDL_SCANCODE_F7;
73 IK_F8 = SDL_SCANCODE_F8;
74 IK_F9 = SDL_SCANCODE_F9;
75 IK_F10 = SDL_SCANCODE_F10;
76 IK_END = SDL_SCANCODE_END;
77 IK_KPEND = SDL_SCANCODE_KP_1;
78 IK_BACKSPACE = SDL_SCANCODE_BACKSPACE;
79 IK_BACKQUOTE = SDL_SCANCODE_GRAVE;
80 IK_GRAVE = SDL_SCANCODE_GRAVE;
81 IK_PAUSE = SDL_SCANCODE_PAUSE;
82 IK_Y = SDL_SCANCODE_Y;
83 IK_N = SDL_SCANCODE_N;
84 // TODO: think of something better than this shit
85 IK_LASTKEY = SDL_NUM_SCANCODES-1;
87 AX_MINUS = 0;
88 AX_PLUS = 1;
89 HAT_LEFT = 0;
90 HAT_UP = 1;
91 HAT_RIGHT = 2;
92 HAT_DOWN = 3;
94 function e_InitInput(): Boolean;
95 procedure e_ReleaseInput();
96 procedure e_ClearInputBuffer();
97 function e_PollInput(): Boolean;
98 function e_KeyPressed(Key: Word): Boolean;
99 function e_AnyKeyPressed(): Boolean;
100 function e_GetFirstKeyPressed(): Word;
101 function e_JoystickStateToString(mode: Integer): String;
102 function e_JoyByHandle(handle: Word): Integer;
103 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
104 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
105 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
106 procedure e_SetKeyState(key: Word; state: Integer);
108 procedure e_UnpressAllKeys ();
109 procedure e_KeyUpDown (key: Word; down: Boolean);
111 var
112 {e_MouseInfo: TMouseInfo;}
113 e_EnableInput: Boolean = False;
114 e_JoysticksAvailable: Byte = 0;
115 e_JoystickDeadzones: array [0..e_MaxJoys-1] of Integer = (8192, 8192, 8192, 8192);
116 e_KeyNames: array [0..e_MaxInputKeys] of String;
118 implementation
120 uses Math;
122 const
123 KBRD_END = e_MaxKbdKeys;
124 JOYK_BEG = KBRD_END;
125 JOYK_END = JOYK_BEG + e_MaxJoyBtns*e_MaxJoys;
126 JOYA_BEG = JOYK_END;
127 JOYA_END = JOYA_BEG + e_MaxJoyAxes*2*e_MaxJoys;
128 JOYH_BEG = JOYA_END;
129 JOYH_END = JOYH_BEG + e_MaxJoyHats*4*e_MaxJoys;
131 type
132 TJoystick = record
133 ID: Byte;
134 Handle: PSDL_Joystick;
135 Axes: Byte;
136 Buttons: Byte;
137 Hats: Byte;
138 ButtBuf: array [0..e_MaxJoyBtns] of Boolean;
139 AxisBuf: array [0..e_MaxJoyAxes] of Integer;
140 AxisZero: array [0..e_MaxJoyAxes] of Integer;
141 HatBuf: array [0..e_MaxJoyHats] of array [HAT_LEFT..HAT_DOWN] of Boolean;
142 end;
144 var
145 KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
146 Joysticks: array of TJoystick = nil;
148 function OpenJoysticks(): Byte;
149 var
150 i, j, k, c: Integer;
151 joy: PSDL_Joystick;
152 begin
153 Result := 0;
154 k := Min(e_MaxJoys, SDL_NumJoysticks());
155 if k = 0 then Exit;
156 c := 0;
157 for i := 0 to k do
158 begin
159 joy := SDL_JoystickOpen(i);
160 if joy <> nil then
161 begin
162 Inc(c);
163 e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' (' + SDL_JoystickName(joy) +
164 ') as joystick ' + IntToStr(c) + ':', MSG_NOTIFY);
165 SetLength(Joysticks, c);
166 with Joysticks[c-1] do
167 begin
168 ID := i;
169 Handle := joy;
170 Axes := Min(e_MaxJoyAxes, SDL_JoystickNumAxes(joy));
171 Buttons := Min(e_MaxJoyBtns, SDL_JoystickNumButtons(joy));
172 Hats := Min(e_MaxJoyHats, SDL_JoystickNumHats(joy));
173 // TODO: find proper solution for this xbox trigger shit
174 for j := 0 to Axes do AxisZero[j] := SDL_JoystickGetAxis(joy, j);
175 e_WriteLog(' ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
176 IntToStr(Hats) + ' hats.', MSG_NOTIFY);
177 end;
178 end;
179 end;
180 Result := c;
181 end;
183 procedure ReleaseJoysticks();
184 var
185 i: Integer;
186 begin
187 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
188 for i := Low(Joysticks) to High(Joysticks) do
189 with Joysticks[i] do
190 SDL_JoystickClose(Handle);
191 SetLength(Joysticks, 0);
192 end;
195 procedure e_UnpressAllKeys ();
196 var
197 i: Integer;
198 begin
199 for i := 0 to High(KeyBuffer) do KeyBuffer[i] := False;
200 end;
203 procedure e_KeyUpDown (key: Word; down: Boolean);
204 begin
205 if (key > 0) and (key < Length(KeyBuffer)) then KeyBuffer[key] := down;
206 end;
209 function PollKeyboard(): Boolean;
210 var
211 Keys: PByte;
212 NKeys: Integer;
213 i: NativeUInt;
214 begin
215 Result := False;
216 Keys := SDL_GetKeyboardState(@NKeys);
217 if (Keys = nil) or (NKeys < 1) then Exit;
218 for i := 0 to NKeys do
219 begin
220 if ((PByte(NativeUInt(Keys) + i)^) <> 0) then KeyBuffer[i] := false;
221 end;
222 for i := NKeys to High(KeyBuffer) do KeyBuffer[i] := False;
223 end;
225 function PollJoysticks(): Boolean;
226 var
227 i, j: Word;
228 hat: Byte;
229 begin
230 Result := False;
231 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
232 SDL_JoystickUpdate();
233 for j := Low(Joysticks) to High(Joysticks) do
234 with Joysticks[j] do
235 begin
236 for i := 0 to Buttons do
237 ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
238 for i := 0 to Axes do
239 AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
240 for i := 0 to Hats do
241 begin
242 hat := SDL_JoystickGetHat(Handle, i);
243 HatBuf[i, HAT_UP] := LongBool(hat and SDL_HAT_UP);
244 HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);
245 HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);
246 HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
247 end;
248 end;
249 end;
251 procedure GenerateKeyNames();
252 var
253 i, j, k: LongWord;
254 begin
255 // keyboard key names
256 for i := 0 to IK_LASTKEY do
257 e_KeyNames[i] := SDL_GetScancodeName(i);
259 // joysticks
260 for j := 0 to e_MaxJoys-1 do
261 begin
262 k := JOYK_BEG + j * e_MaxJoyBtns;
263 // buttons
264 for i := 0 to e_MaxJoyBtns-1 do
265 e_KeyNames[k + i] := Format('JOY%d B%d', [j, i]);
266 k := JOYA_BEG + j * e_MaxJoyAxes * 2;
267 // axes
268 for i := 0 to e_MaxJoyAxes-1 do
269 begin
270 e_KeyNames[k + i*2 ] := Format('JOY%d A%d+', [j, i]);
271 e_KeyNames[k + i*2 + 1] := Format('JOY%d A%d-', [j, i]);
272 end;
273 k := JOYH_BEG + j * e_MaxJoyHats * 4;
274 // hats
275 for i := 0 to e_MaxJoyHats-1 do
276 begin
277 e_KeyNames[k + i*4 ] := Format('JOY%d D%dL', [j, i]);
278 e_KeyNames[k + i*4 + 1] := Format('JOY%d D%dU', [j, i]);
279 e_KeyNames[k + i*4 + 2] := Format('JOY%d D%dR', [j, i]);
280 e_KeyNames[k + i*4 + 3] := Format('JOY%d D%dD', [j, i]);
281 end;
282 end;
283 end;
285 function e_InitInput(): Boolean;
286 begin
287 Result := False;
289 e_JoysticksAvailable := OpenJoysticks();
290 e_EnableInput := True;
291 GenerateKeyNames();
293 Result := True;
294 end;
296 procedure e_ReleaseInput();
297 begin
298 ReleaseJoysticks();
299 e_JoysticksAvailable := 0;
300 end;
302 procedure e_ClearInputBuffer();
303 var
304 i, j, d: Integer;
305 begin
306 for i := Low(KeyBuffer) to High(KeyBuffer) do
307 KeyBuffer[i] := False;
308 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then
309 for i := Low(Joysticks) to High(Joysticks) do
310 begin
311 for j := Low(Joysticks[i].ButtBuf) to High(Joysticks[i].ButtBuf) do
312 Joysticks[i].ButtBuf[j] := False;
313 for j := Low(Joysticks[i].AxisBuf) to High(Joysticks[i].AxisBuf) do
314 Joysticks[i].AxisBuf[j] := 0;
315 for j := Low(Joysticks[i].HatBuf) to High(Joysticks[i].HatBuf) do
316 for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
317 Joysticks[i].HatBuf[j, d] := False;
318 end;
319 end;
321 function e_PollInput(): Boolean;
322 var
323 kb, js: Boolean;
324 begin
325 kb := PollKeyboard();
326 js := PollJoysticks();
328 Result := kb or js;
329 end;
331 function e_KeyPressed(Key: Word): Boolean;
332 var
333 joyi, dir: Integer;
334 begin
335 Result := False;
336 if (Key = IK_INVALID) or (Key = 0) then Exit;
338 if (Key < KBRD_END) then
339 begin // Keyboard buttons/keys
340 Result := KeyBuffer[Key];
341 end
343 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
344 begin // Joystick buttons
345 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
346 if JoyI >= e_JoysticksAvailable then
347 Result := False
348 else
349 begin
350 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
351 Result := Joysticks[JoyI].ButtBuf[Key];
352 end;
353 end
355 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
356 begin // Joystick axes
357 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
358 if JoyI >= e_JoysticksAvailable then
359 Result := False
360 else
361 begin
362 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
363 dir := Key mod 2;
364 if dir = AX_MINUS then
365 Result := Joysticks[JoyI].AxisBuf[Key div 2] <
366 Joysticks[JoyI].AxisZero[Key div 2] - e_JoystickDeadzones[JoyI]
367 else
368 Result := Joysticks[JoyI].AxisBuf[Key div 2] >
369 Joysticks[JoyI].AxisZero[Key div 2] + e_JoystickDeadzones[JoyI]
370 end;
371 end
373 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
374 begin // Joystick hats
375 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
376 if JoyI >= e_JoysticksAvailable then
377 Result := False
378 else
379 begin
380 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
381 dir := Key mod 4;
382 Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
383 end;
384 end;
385 end;
387 procedure e_SetKeyState(key: Word; state: Integer);
388 var
389 JoyI, dir: Integer;
390 begin
391 if (Key = IK_INVALID) or (Key = 0) then Exit;
393 if (Key < KBRD_END) then
394 begin // Keyboard buttons/keys
395 keyBuffer[key] := (state <> 0);
396 end
398 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
399 begin // Joystick buttons
400 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
401 if JoyI >= e_JoysticksAvailable then
402 Exit
403 else
404 begin
405 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
406 Joysticks[JoyI].ButtBuf[Key] := (state <> 0);
407 end;
408 end
410 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
411 begin // Joystick axes
412 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
413 if JoyI >= e_JoysticksAvailable then
414 Exit
415 else
416 begin
417 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
418 Joysticks[JoyI].AxisBuf[Key div 2] := state;
419 end;
420 end
422 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
423 begin // Joystick hats
424 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
425 if JoyI >= e_JoysticksAvailable then
426 Exit
427 else
428 begin
429 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
430 dir := Key mod 4;
431 Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
432 end;
433 end;
434 end;
436 function e_AnyKeyPressed(): Boolean;
437 var
438 k: Word;
439 begin
440 Result := False;
442 for k := 1 to e_MaxInputKeys do
443 if e_KeyPressed(k) then
444 begin
445 Result := True;
446 Break;
447 end;
448 end;
450 function e_GetFirstKeyPressed(): Word;
451 var
452 k: Word;
453 begin
454 Result := IK_INVALID;
456 for k := 1 to e_MaxInputKeys do
457 if e_KeyPressed(k) then
458 begin
459 Result := k;
460 Break;
461 end;
462 end;
464 ////////////////////////////////////////////////////////////////////////////////
466 function e_JoystickStateToString(mode: Integer): String;
467 begin
468 Result := '';
469 end;
471 function e_JoyByHandle(handle: Word): Integer;
472 var
473 i: Integer;
474 begin
475 Result := -1;
476 if Joysticks = nil then Exit;
477 for i := Low(Joysticks) to High(Joysticks) do
478 if Joysticks[i].ID = handle then
479 begin
480 Result := i;
481 Exit;
482 end;
483 end;
485 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
486 begin
487 Result := 0;
488 if id >= Length(Joysticks) then Exit;
489 Result := JOYK_BEG + id*e_MaxJoyBtns + btn;
490 end;
492 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
493 begin
494 Result := 0;
495 if id >= Length(Joysticks) then Exit;
496 Result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir;
497 end;
499 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
500 begin
501 Result := 0;
502 if id >= Length(Joysticks) then Exit;
503 Result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir;
504 end;
506 end.