DEADSOFTWARE

fixed strafe button crash; fixed analog triggers
[d2df-sdl.git] / src / engine / e_input.pas
index e8a03f739c54fc28653208d16a638af5143da97e..d4081ee5615075eaa89c26fb80962c47e82e1dda 100644 (file)
@@ -1,3 +1,19 @@
+(* Copyright (C)  DooM 2D:Forever Developers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
+{$MODE DELPHI}
 unit e_input;
 
 interface
@@ -5,65 +21,76 @@ interface
 uses
   SysUtils,
   e_log,
-  SDL;
+  SDL2;
 
 const
-  e_MaxKbdKeys  = 321;
+  e_MaxKbdKeys  = SDL_NUM_SCANCODES;
   e_MaxJoys     = 4;
   e_MaxJoyBtns  = 32;
-  e_MaxJoyAxes  = 4;
-  e_MaxJoyHats  = 4;
-  
+  e_MaxJoyAxes  = 8;
+  e_MaxJoyHats  = 8;
+
   e_MaxJoyKeys = e_MaxJoyBtns + e_MaxJoyAxes*2 + e_MaxJoyHats*4;
-  
+
   e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys - 1;
   // $$$..$$$ -  321 Keyboard buttons/keys
   // $$$..$$$ - 4*32 Joystick buttons
-  // $$$..$$$ -  4*4 Joystick axes (- and +)
+  // $$$..$$$ -  8*2 Joystick axes (- and +)
   // $$$..$$$ -  4*4 Joystick hats (L U R D)
-  
+
   // these are apparently used in g_gui and g_game and elsewhere
-  IK_UNKNOWN = SDLK_UNKNOWN;
   IK_INVALID = 65535;
-  IK_ESCAPE  = SDLK_ESCAPE;
-  IK_RETURN  = SDLK_RETURN;
-  IK_ENTER   = SDLK_RETURN;
-  IK_UP      = SDLK_UP;
-  IK_DOWN    = SDLK_DOWN;
-  IK_LEFT    = SDLK_LEFT;
-  IK_RIGHT   = SDLK_RIGHT;
-  IK_DELETE  = SDLK_DELETE;
-  IK_HOME    = SDLK_HOME;
-  IK_INSERT  = SDLK_INSERT;
-  IK_SPACE   = SDLK_SPACE;
-  IK_CONTROL = SDLK_LCTRL;
-  IK_SHIFT   = SDLK_LSHIFT;
-  IK_TAB     = SDLK_TAB;
-  IK_PAGEUP  = SDLK_PAGEUP;
-  IK_PAGEDN  = SDLK_PAGEDOWN; 
-  IK_F2      = SDLK_F2;
-  IK_F3      = SDLK_F3;
-  IK_F4      = SDLK_F4;
-  IK_F5      = SDLK_F5;
-  IK_F6      = SDLK_F6;
-  IK_F7      = SDLK_F7;
-  IK_F8      = SDLK_F8;
-  IK_F9      = SDLK_F9;
-  IK_F10     = SDLK_F10;
-  IK_END     = SDLK_END;
-  IK_BACKSPACE = SDLK_BACKSPACE;
-  IK_BACKQUOTE = SDLK_BACKQUOTE;
-  IK_PAUSE   = SDLK_PAUSE;
+  IK_ESCAPE  = SDL_SCANCODE_ESCAPE;
+  IK_RETURN  = SDL_SCANCODE_RETURN;
+  IK_KPRETURN= SDL_SCANCODE_KP_ENTER;
+  IK_ENTER   = SDL_SCANCODE_RETURN;
+  IK_UP      = SDL_SCANCODE_UP;
+  IK_KPUP    = SDL_SCANCODE_KP_8;
+  IK_DOWN    = SDL_SCANCODE_DOWN;
+  IK_KPDOWN  = SDL_SCANCODE_KP_2;
+  IK_LEFT    = SDL_SCANCODE_LEFT;
+  IK_KPLEFT  = SDL_SCANCODE_KP_4;
+  IK_RIGHT   = SDL_SCANCODE_RIGHT;
+  IK_KPRIGHT = SDL_SCANCODE_KP_6;
+  IK_DELETE  = SDL_SCANCODE_DELETE;
+  IK_HOME    = SDL_SCANCODE_HOME;
+  IK_KPHOME  = SDL_SCANCODE_KP_7;
+  IK_INSERT  = SDL_SCANCODE_INSERT;
+  IK_SPACE   = SDL_SCANCODE_SPACE;
+  IK_CONTROL = SDL_SCANCODE_LCTRL;
+  IK_SHIFT   = SDL_SCANCODE_LSHIFT;
+  IK_TAB     = SDL_SCANCODE_TAB;
+  IK_PAGEUP  = SDL_SCANCODE_PAGEUP;
+  IK_KPPAGEUP= SDL_SCANCODE_KP_9;
+  IK_PAGEDN  = SDL_SCANCODE_PAGEDOWN;
+  IK_KPPAGEDN= SDL_SCANCODE_KP_3;
+  IK_F2      = SDL_SCANCODE_F2;
+  IK_F3      = SDL_SCANCODE_F3;
+  IK_F4      = SDL_SCANCODE_F4;
+  IK_F5      = SDL_SCANCODE_F5;
+  IK_F6      = SDL_SCANCODE_F6;
+  IK_F7      = SDL_SCANCODE_F7;
+  IK_F8      = SDL_SCANCODE_F8;
+  IK_F9      = SDL_SCANCODE_F9;
+  IK_F10     = SDL_SCANCODE_F10;
+  IK_END     = SDL_SCANCODE_END;
+  IK_KPEND   = SDL_SCANCODE_KP_1;
+  IK_BACKSPACE = SDL_SCANCODE_BACKSPACE;
+  IK_BACKQUOTE = SDL_SCANCODE_GRAVE;
+  IK_GRAVE     = SDL_SCANCODE_GRAVE;
+  IK_PAUSE   = SDL_SCANCODE_PAUSE;
+  IK_Y       = SDL_SCANCODE_Y;
+  IK_N       = SDL_SCANCODE_N;
   // TODO: think of something better than this shit
-  IK_LASTKEY = 320;
-  
+  IK_LASTKEY = SDL_NUM_SCANCODES-1;
+
   AX_MINUS  = 0;
   AX_PLUS   = 1;
   HAT_LEFT  = 0;
   HAT_UP    = 1;
   HAT_RIGHT = 2;
   HAT_DOWN  = 3;
-  
+
 function  e_InitInput(): Boolean;
 procedure e_ReleaseInput();
 procedure e_ClearInputBuffer();
@@ -107,16 +134,17 @@ type
     Hats:    Byte;
     ButtBuf: array [0..e_MaxJoyBtns] of Boolean;
     AxisBuf: array [0..e_MaxJoyAxes] of Integer;
+    AxisZero: array [0..e_MaxJoyAxes] of Integer;
     HatBuf:  array [0..e_MaxJoyHats] of array [HAT_LEFT..HAT_DOWN] of Boolean;
   end;
 
 var
   KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
-  Joysticks: array of TJoystick = nil;        
+  Joysticks: array of TJoystick = nil;
 
 function OpenJoysticks(): Byte;
 var
-  i, k, c: Integer;
+  i, j, k, c: Integer;
   joy: PSDL_Joystick;
 begin
   Result := 0;
@@ -129,7 +157,8 @@ begin
     if joy <> nil then
     begin
       Inc(c);
-      e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' as joystick ' + IntToStr(c) + ':', MSG_NOTIFY);
+      e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' (' + SDL_JoystickName(joy) +
+                 ') as joystick ' + IntToStr(c) + ':', MSG_NOTIFY);
       SetLength(Joysticks, c);
       with Joysticks[c-1] do
       begin
@@ -138,6 +167,8 @@ begin
         Axes := Min(e_MaxJoyAxes, SDL_JoystickNumAxes(joy));
         Buttons := Min(e_MaxJoyBtns, SDL_JoystickNumButtons(joy));
         Hats := Min(e_MaxJoyHats, SDL_JoystickNumHats(joy));
+        // TODO: find proper solution for this xbox trigger shit
+        for j := 0 to Axes do AxisZero[j] := SDL_JoystickGetAxis(joy, j);
         e_WriteLog('       ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
                    IntToStr(Hats) + ' hats.', MSG_NOTIFY);
       end;
@@ -156,7 +187,7 @@ begin
       SDL_JoystickClose(Handle);
   SetLength(Joysticks, 0);
 end;
-  
+
 function PollKeyboard(): Boolean;
 var
   Keys: PByte;
@@ -164,20 +195,21 @@ var
   i: Cardinal;
 begin
   Result := False;
-  Keys := SDL_GetKeyState(@NKeys);
+  Keys := SDL_GetKeyboardState(@NKeys);
   if (Keys = nil) or (NKeys < 1) then
     Exit;
   for i := 0 to NKeys do
     KeyBuffer[i] := ((PByte(Cardinal(Keys) + i)^) <> 0);
   for i := NKeys to High(KeyBuffer) do
     KeyBuffer[i] := False;
-end;  
-  
+end;
+
 function PollJoysticks(): Boolean;
 var
-  i, j, d: Word;
+  i, j: Word;
   hat: Byte;
 begin
+  Result := False;
   if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
   SDL_JoystickUpdate();
   for j := Low(Joysticks) to High(Joysticks) do
@@ -191,13 +223,12 @@ begin
       begin
         hat := SDL_JoystickGetHat(Handle, i);
         HatBuf[i, HAT_UP] := LongBool(hat and SDL_HAT_UP);
-        HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);  
-        HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);  
-        HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);  
+        HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);
+        HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);
+        HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
       end;
     end;
-  Result := False;
-end;    
+end;
 
 procedure GenerateKeyNames();
 var
@@ -205,12 +236,8 @@ var
 begin
   // keyboard key names
   for i := 0 to IK_LASTKEY do
-  begin
-    e_KeyNames[i] := SDL_GetKeyName(i);
-    if e_KeyNames[i] = 'unknown key' then
-      e_KeyNames[i] := '';
-  end;
-  
+    e_KeyNames[i] := SDL_GetScancodeName(i);
+
   // joysticks
   for j := 0 to e_MaxJoys-1 do
   begin
@@ -236,11 +263,11 @@ begin
     end;
   end;
 end;
-  
+
 function e_InitInput(): Boolean;
 begin
   Result := False;
-  
+
   e_JoysticksAvailable := OpenJoysticks();
   e_EnableInput := True;
   GenerateKeyNames();
@@ -253,7 +280,7 @@ begin
   ReleaseJoysticks();
   e_JoysticksAvailable := 0;
 end;
-                                                         
+
 procedure e_ClearInputBuffer();
 var
   i, j, d: Integer;
@@ -270,7 +297,7 @@ begin
     for j := Low(Joysticks[i].HatBuf) to High(Joysticks[i].HatBuf) do
       for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
         Joysticks[i].HatBuf[j, d] := False;
-  end; 
+  end;
 end;
 
 function e_PollInput(): Boolean;
@@ -289,12 +316,12 @@ var
 begin
   Result := False;
   if (Key = IK_INVALID) or (Key = 0) then Exit;
-  
+
   if (Key < KBRD_END) then
   begin // Keyboard buttons/keys
     Result := KeyBuffer[Key];
   end
-  
+
   else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
   begin // Joystick buttons
     JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
@@ -306,7 +333,7 @@ begin
       Result := Joysticks[JoyI].ButtBuf[Key];
     end;
   end
-  
+
   else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
   begin // Joystick axes
     JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
@@ -317,14 +344,16 @@ begin
       Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
       dir := Key mod 2;
       if dir = AX_MINUS then
-        Result := Joysticks[JoyI].AxisBuf[Key div 2] < -e_JoystickDeadzones[JoyI]
-      else 
-        Result := Joysticks[JoyI].AxisBuf[Key div 2] > e_JoystickDeadzones[JoyI]
-    end;    
+        Result := Joysticks[JoyI].AxisBuf[Key div 2] <
+          Joysticks[JoyI].AxisZero[Key div 2] - e_JoystickDeadzones[JoyI]
+      else
+        Result := Joysticks[JoyI].AxisBuf[Key div 2] >
+          Joysticks[JoyI].AxisZero[Key div 2] + e_JoystickDeadzones[JoyI]
+    end;
   end
-  
+
   else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
-  begin // Joystick hats 
+  begin // Joystick hats
     JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
     if JoyI >= e_JoysticksAvailable then
       Result := False
@@ -333,7 +362,7 @@ begin
       Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
       dir := Key mod 4;
       Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
-    end;    
+    end;
   end;
 end;
 
@@ -342,12 +371,12 @@ var
   JoyI, dir: Integer;
 begin
   if (Key = IK_INVALID) or (Key = 0) then Exit;
-  
+
   if (Key < KBRD_END) then
   begin // Keyboard buttons/keys
     keyBuffer[key] := (state <> 0);
   end
-  
+
   else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
   begin // Joystick buttons
     JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
@@ -359,9 +388,9 @@ begin
       Joysticks[JoyI].ButtBuf[Key] := (state <> 0);
     end;
   end
-  
+
   else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
-  begin // Joystick axes      
+  begin // Joystick axes
     JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
     if JoyI >= e_JoysticksAvailable then
       Exit
@@ -369,11 +398,11 @@ begin
     begin
       Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
       Joysticks[JoyI].AxisBuf[Key div 2] := state;
-    end; 
+    end;
   end
-  
+
   else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
-  begin // Joystick hats        
+  begin // Joystick hats
     JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
     if JoyI >= e_JoysticksAvailable then
       Exit
@@ -382,7 +411,7 @@ begin
       Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
       dir := Key mod 4;
       Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
-    end;    
+    end;
   end;
 end;