6521: Minor refactoring of engine input r=Chairn a=Robyt3



## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [X] Considered possible null pointers and out of bounds array indexing
- [X] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2023-04-22 18:43:30 +00:00 committed by GitHub
commit 15620354b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 72 additions and 79 deletions

View file

@ -11,8 +11,6 @@
#include "input.h"
//print >>f, "int inp_key_code(const char *key_name) { int i; if (!strcmp(key_name, \"-?-\")) return -1; else for (i = 0; i < 512; i++) if (!strcmp(key_strings[i], key_name)) return i; return -1; }"
// this header is protected so you don't include it from anywhere
#define KEYS_INCLUDE
#include "keynames.h"
@ -32,8 +30,8 @@ void CInput::AddEvent(char *pText, int Key, int Flags)
{
m_aInputEvents[m_NumEvents].m_Key = Key;
m_aInputEvents[m_NumEvents].m_Flags = Flags;
if(!pText)
m_aInputEvents[m_NumEvents].m_aText[0] = 0;
if(pText == nullptr)
m_aInputEvents[m_NumEvents].m_aText[0] = '\0';
else
str_copy(m_aInputEvents[m_NumEvents].m_aText, pText);
m_aInputEvents[m_NumEvents].m_InputCount = m_InputCounter;
@ -57,13 +55,13 @@ CInput::CInput()
m_NumEvents = 0;
m_MouseFocus = true;
m_pClipboardText = NULL;
m_pClipboardText = nullptr;
m_NumTextInputInstances = 0;
m_EditingTextLen = -1;
m_aEditingText[0] = 0;
m_aEditingText[0] = '\0';
m_aDropFile[0] = 0;
m_aDropFile[0] = '\0';
}
void CInput::Init()
@ -290,7 +288,7 @@ void CInput::MouseModeRelative()
#endif
Graphics()->SetWindowGrab(true);
// Clear pending relative mouse motion
SDL_GetRelativeMouseState(0x0, 0x0);
SDL_GetRelativeMouseState(nullptr, nullptr);
}
void CInput::NativeMousePos(int *pX, int *pY) const
@ -300,7 +298,7 @@ void CInput::NativeMousePos(int *pX, int *pY) const
bool CInput::NativeMousePressed(int Index)
{
int i = SDL_GetMouseState(NULL, NULL);
int i = SDL_GetMouseState(nullptr, nullptr);
return (i & SDL_BUTTON(Index)) != 0;
}
@ -335,14 +333,14 @@ void CInput::Clear()
bool CInput::KeyState(int Key) const
{
if(Key < 0 || Key >= KEY_LAST)
if(Key < KEY_FIRST || Key >= KEY_LAST)
return false;
return m_aInputState[Key];
}
void CInput::UpdateMouseState()
{
const int MouseState = SDL_GetMouseState(NULL, NULL);
const int MouseState = SDL_GetMouseState(nullptr, nullptr);
if(MouseState & SDL_BUTTON(SDL_BUTTON_LEFT))
m_aInputState[KEY_MOUSE_1] = 1;
if(MouseState & SDL_BUTTON(SDL_BUTTON_RIGHT))
@ -408,24 +406,24 @@ void CInput::HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event)
{
m_aInputState[LeftKey] = true;
m_aInputCount[LeftKey] = m_InputCounter;
AddEvent(0, LeftKey, IInput::FLAG_PRESS);
AddEvent(nullptr, LeftKey, IInput::FLAG_PRESS);
}
else if(Event.value > SDL_JOYSTICK_AXIS_MIN * DeadZone && m_aInputState[LeftKey])
{
m_aInputState[LeftKey] = false;
AddEvent(0, LeftKey, IInput::FLAG_RELEASE);
AddEvent(nullptr, LeftKey, IInput::FLAG_RELEASE);
}
if(Event.value >= SDL_JOYSTICK_AXIS_MAX * DeadZone && !m_aInputState[RightKey])
{
m_aInputState[RightKey] = true;
m_aInputCount[RightKey] = m_InputCounter;
AddEvent(0, RightKey, IInput::FLAG_PRESS);
AddEvent(nullptr, RightKey, IInput::FLAG_PRESS);
}
else if(Event.value < SDL_JOYSTICK_AXIS_MAX * DeadZone && m_aInputState[RightKey])
{
m_aInputState[RightKey] = false;
AddEvent(0, RightKey, IInput::FLAG_RELEASE);
AddEvent(nullptr, RightKey, IInput::FLAG_RELEASE);
}
}
@ -445,12 +443,12 @@ void CInput::HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event)
{
m_aInputState[Key] = true;
m_aInputCount[Key] = m_InputCounter;
AddEvent(0, Key, IInput::FLAG_PRESS);
AddEvent(nullptr, Key, IInput::FLAG_PRESS);
}
else if(Event.type == SDL_JOYBUTTONUP)
{
m_aInputState[Key] = false;
AddEvent(0, Key, IInput::FLAG_RELEASE);
AddEvent(nullptr, Key, IInput::FLAG_RELEASE);
}
}
@ -472,7 +470,7 @@ void CInput::HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event)
if(Key != HatKeys[0] && Key != HatKeys[1] && m_aInputState[Key])
{
m_aInputState[Key] = false;
AddEvent(0, Key, IInput::FLAG_RELEASE);
AddEvent(nullptr, Key, IInput::FLAG_RELEASE);
}
}
@ -482,7 +480,7 @@ void CInput::HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event)
{
m_aInputState[CurrentKey] = true;
m_aInputCount[CurrentKey] = m_InputCounter;
AddEvent(0, CurrentKey, IInput::FLAG_PRESS);
AddEvent(nullptr, CurrentKey, IInput::FLAG_PRESS);
}
}
}
@ -612,13 +610,13 @@ int CInput::Update()
}
else
{
m_aEditingText[0] = 0;
m_aEditingText[0] = '\0';
}
break;
}
case SDL_TEXTINPUT:
m_EditingTextLen = -1;
AddEvent(Event.text.text, 0, IInput::FLAG_TEXT);
AddEvent(Event.text.text, KEY_UNKNOWN, IInput::FLAG_TEXT);
break;
// handle keys
case SDL_KEYDOWN:
@ -731,7 +729,7 @@ int CInput::Update()
// Enable this in case SDL 2.0.16 has major bugs or 2.0.18 still doesn't fix tabbing out with relative mouse
// MouseModeRelative();
// Clear pending relative mouse motion
SDL_GetRelativeMouseState(0x0, 0x0);
SDL_GetRelativeMouseState(nullptr, nullptr);
}
m_MouseFocus = true;
IgnoreKeys = true;
@ -780,7 +778,7 @@ int CInput::Update()
m_aInputState[Scancode] = 1;
m_aInputCount[Scancode] = m_InputCounter;
}
AddEvent(0, Scancode, Action);
AddEvent(nullptr, Scancode, Action);
}
}

View file

@ -77,7 +77,7 @@ private:
void AddEvent(char *pText, int Key, int Flags);
void Clear() override;
bool IsEventValid(CEvent *pEvent) const override { return pEvent->m_InputCount == m_InputCounter; }
bool IsEventValid(const CEvent &Event) const override { return Event.m_InputCount == m_InputCounter; }
// quick access to input
unsigned short m_aInputCount[g_MaxKeys]; // tw-KEY

View file

@ -33,7 +33,7 @@ protected:
};
// quick access to events
int m_NumEvents;
size_t m_NumEvents;
IInput::CEvent m_aInputEvents[INPUT_BUFFER_SIZE];
int64_t m_LastUpdate;
float m_UpdateTime;
@ -41,10 +41,9 @@ protected:
public:
enum
{
FLAG_PRESS = 1,
FLAG_RELEASE = 2,
FLAG_REPEAT = 4,
FLAG_TEXT = 8,
FLAG_PRESS = 1 << 0,
FLAG_RELEASE = 1 << 1,
FLAG_TEXT = 1 << 2,
};
enum ECursorType
{
@ -54,19 +53,15 @@ public:
};
// events
int NumEvents() const { return m_NumEvents; }
virtual bool IsEventValid(CEvent *pEvent) const = 0;
CEvent GetEvent(int Index) const
size_t NumEvents() const { return m_NumEvents; }
virtual bool IsEventValid(const CEvent &Event) const = 0;
const CEvent &GetEvent(size_t Index) const
{
if(Index < 0 || Index >= m_NumEvents)
{
IInput::CEvent e = {0, 0};
return e;
}
dbg_assert(Index < m_NumEvents, "Index invalid");
return m_aInputEvents[Index];
}
CEvent *GetEventsRaw() { return m_aInputEvents; }
int *GetEventCountRaw() { return &m_NumEvents; }
size_t *GetEventCountRaw() { return &m_NumEvents; }
/**
* @return Rolling average of the time in seconds between

View file

@ -209,9 +209,9 @@ public:
virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) { return false; }
/**
* Called on a input event.
* @param e The input event.
* @param Event The input event.
*/
virtual bool OnInput(IInput::CEvent e) { return false; }
virtual bool OnInput(const IInput::CEvent &Event) { return false; }
};
#endif

View file

@ -8,7 +8,7 @@
static const ColorRGBA gs_BindPrintColor{1.0f, 1.0f, 0.8f, 1.0f};
bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
bool CBinds::CBindsSpecial::OnInput(const IInput::CEvent &Event)
{
// only handle F and composed F binds
if(((Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) || (Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24)) && (Event.m_Key != KEY_F5 || !m_pClient->m_Menus.IsActive()))
@ -123,33 +123,33 @@ int CBinds::GetModifierMaskOfKey(int Key)
}
}
bool CBinds::OnInput(IInput::CEvent e)
bool CBinds::OnInput(const IInput::CEvent &Event)
{
// don't handle invalid events
if(e.m_Key <= 0 || e.m_Key >= KEY_LAST)
if(Event.m_Key <= KEY_FIRST || Event.m_Key >= KEY_LAST)
return false;
int Mask = GetModifierMask(Input());
int KeyModifierMask = GetModifierMaskOfKey(e.m_Key);
int KeyModifierMask = GetModifierMaskOfKey(Event.m_Key);
Mask &= ~KeyModifierMask;
bool ret = false;
if(m_aapKeyBindings[Mask][e.m_Key])
if(m_aapKeyBindings[Mask][Event.m_Key])
{
if(e.m_Flags & IInput::FLAG_PRESS)
Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][e.m_Key]);
if(e.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][e.m_Key]);
if(Event.m_Flags & IInput::FLAG_PRESS)
Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][Event.m_Key]);
if(Event.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][Event.m_Key]);
ret = true;
}
if(m_aapKeyBindings[0][e.m_Key] && !ret)
if(m_aapKeyBindings[0][Event.m_Key] && !ret)
{
// When ctrl+shift are pressed (ctrl+shift binds and also the hard-coded ctrl+shift+d, ctrl+shift+g, ctrl+shift+e), ignore other +xxx binds
if(e.m_Flags & IInput::FLAG_PRESS && Mask != ((1 << MODIFIER_CTRL) | (1 << MODIFIER_SHIFT)) && Mask != ((1 << MODIFIER_GUI) | (1 << MODIFIER_SHIFT)))
Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][e.m_Key]);
if(e.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][e.m_Key]);
if(Event.m_Flags & IInput::FLAG_PRESS && Mask != ((1 << MODIFIER_CTRL) | (1 << MODIFIER_SHIFT)) && Mask != ((1 << MODIFIER_GUI) | (1 << MODIFIER_SHIFT)))
Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][Event.m_Key]);
if(Event.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][Event.m_Key]);
ret = true;
}

View file

@ -32,7 +32,7 @@ public:
public:
CBinds *m_pBinds;
virtual int Sizeof() const override { return sizeof(*this); }
virtual bool OnInput(IInput::CEvent Event) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
};
enum
@ -60,7 +60,7 @@ public:
static const char *GetKeyBindModifiersName(int ModifierCombination);
virtual void OnConsoleInit() override;
virtual bool OnInput(IInput::CEvent Event) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
// DDRace

View file

@ -165,7 +165,7 @@ void CChat::OnInit()
Console()->Chain("cl_chat_old", ConchainChatOld, this);
}
bool CChat::OnInput(IInput::CEvent Event)
bool CChat::OnInput(const IInput::CEvent &Event)
{
if(m_Mode == MODE_NONE)
return false;

View file

@ -166,7 +166,7 @@ public:
void Reset();
void OnRelease() override;
void OnMessage(int MsgType, void *pRawMsg) override;
bool OnInput(IInput::CEvent Event) override;
bool OnInput(const IInput::CEvent &Event) override;
void OnInit() override;
void RebuildChat();

View file

@ -203,7 +203,7 @@ void CGameConsole::CInstance::PossibleArgumentsCompleteCallback(int Index, const
}
}
void CGameConsole::CInstance::OnInput(IInput::CEvent Event)
void CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
{
bool Handled = false;
@ -939,7 +939,7 @@ void CGameConsole::OnMessage(int MsgType, void *pRawMsg)
{
}
bool CGameConsole::OnInput(IInput::CEvent Event)
bool CGameConsole::OnInput(const IInput::CEvent &Event)
{
// accept input when opening, but not at first frame to discard the input that caused the console to open
if(m_ConsoleState != CONSOLE_OPEN && (m_ConsoleState != CONSOLE_OPENING || m_StateChangeEnd == TimeNow() + m_StateChangeDuration))

View file

@ -71,7 +71,7 @@ class CGameConsole : public CComponent
void ExecuteLine(const char *pLine);
void OnInput(IInput::CEvent Event);
void OnInput(const IInput::CEvent &Event);
void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor);
const char *GetString() const { return m_Input.GetString(); }
@ -138,7 +138,7 @@ public:
virtual void OnReset() override;
virtual void OnRender() override;
virtual void OnMessage(int MsgType, void *pRawMsg) override;
virtual bool OnInput(IInput::CEvent Events) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
bool IsClosed() { return m_ConsoleState == CONSOLE_CLOSED; }
};

View file

@ -58,7 +58,7 @@ float CMenus::ms_ButtonHeight = 25.0f;
float CMenus::ms_ListheaderHeight = 17.0f;
IInput::CEvent CMenus::m_aInputEvents[MAX_INPUTEVENTS];
int CMenus::m_NumInputEvents;
size_t CMenus::m_NumInputEvents;
CMenus::CMenus()
{
@ -2297,7 +2297,7 @@ bool CMenus::OnCursorMove(float x, float y, IInput::ECursorType CursorType)
return true;
}
bool CMenus::OnInput(IInput::CEvent Event)
bool CMenus::OnInput(const IInput::CEvent &Event)
{
// special handle esc and enter for popup purposes
if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)

View file

@ -55,7 +55,7 @@ public:
int m_ModifierCombination;
CMenusKeyBinder();
virtual int Sizeof() const override { return sizeof(*this); }
virtual bool OnInput(IInput::CEvent Event) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
};
class CMenus : public CComponent
@ -340,7 +340,7 @@ protected:
MAX_INPUTEVENTS = 32
};
static IInput::CEvent m_aInputEvents[MAX_INPUTEVENTS];
static int m_NumInputEvents;
static size_t m_NumInputEvents;
// some settings
static float ms_ButtonHeight;
@ -593,7 +593,7 @@ public:
virtual void OnWindowResize() override;
virtual void OnReset() override;
virtual void OnRender() override;
virtual bool OnInput(IInput::CEvent Event) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) override;
virtual void OnShutdown() override;

View file

@ -48,7 +48,7 @@ CMenusKeyBinder::CMenusKeyBinder()
m_ModifierCombination = 0;
}
bool CMenusKeyBinder::OnInput(IInput::CEvent Event)
bool CMenusKeyBinder::OnInput(const IInput::CEvent &Event)
{
if(m_TakeKey)
{

View file

@ -131,7 +131,7 @@ void CMotd::OnMessage(int MsgType, void *pRawMsg)
}
}
bool CMotd::OnInput(IInput::CEvent Event)
bool CMotd::OnInput(const IInput::CEvent &Event)
{
if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
{

View file

@ -28,7 +28,7 @@ public:
virtual void OnStateChange(int NewState, int OldState) override;
virtual void OnWindowResize() override;
virtual void OnMessage(int MsgType, void *pRawMsg) override;
virtual bool OnInput(IInput::CEvent Event) override;
virtual bool OnInput(const IInput::CEvent &Event) override;
};
#endif

View file

@ -392,15 +392,15 @@ void CGameClient::OnUpdate()
}
// handle key presses
for(int i = 0; i < Input()->NumEvents(); i++)
for(size_t i = 0; i < Input()->NumEvents(); i++)
{
IInput::CEvent e = Input()->GetEvent(i);
if(!Input()->IsEventValid(&e))
const IInput::CEvent &Event = Input()->GetEvent(i);
if(!Input()->IsEventValid(Event))
continue;
for(auto &pComponent : m_vpInput)
{
if(pComponent->OnInput(e))
if(pComponent->OnInput(Event))
break;
}
}

View file

@ -101,7 +101,7 @@ void CUI::Init(IKernel *pKernel)
CUIElementBase::Init(this);
}
void CUI::InitInputs(IInput::CEvent *pInputEventsArray, int *pInputEventCount)
void CUI::InitInputs(IInput::CEvent *pInputEventsArray, size_t *pInputEventCount)
{
m_pInputEventsArray = pInputEventsArray;
m_pInputEventCount = pInputEventCount;
@ -812,7 +812,7 @@ bool CUI::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned
ReturnValue = true;
}
for(int i = 0; i < *m_pInputEventCount; i++)
for(size_t i = 0; i < *m_pInputEventCount; i++)
{
int LastCursor = m_CurCursor;
int Len, NumChars;

View file

@ -243,7 +243,7 @@ private:
bool m_MouseSlow = false;
IInput::CEvent *m_pInputEventsArray;
int *m_pInputEventCount;
size_t *m_pInputEventCount;
unsigned m_HotkeysPressed = 0;
bool m_MouseIsPress = false;
@ -297,7 +297,7 @@ public:
static float ms_FontmodHeight;
void Init(IKernel *pKernel);
void InitInputs(IInput::CEvent *pInputEventsArray, int *pInputEventCount);
void InitInputs(IInput::CEvent *pInputEventsArray, size_t *pInputEventCount);
IClient *Client() const { return m_pClient; }
IGraphics *Graphics() const { return m_pGraphics; }
IInput *Input() const { return m_pInput; }

View file

@ -6856,7 +6856,7 @@ void CEditor::OnUpdate()
Reset();
}
for(int i = 0; i < Input()->NumEvents(); i++)
for(size_t i = 0; i < Input()->NumEvents(); i++)
UI()->OnInput(Input()->GetEvent(i));
// handle cursor movement