From 701ce73df9530864d3449cddbdd658129033b583 Mon Sep 17 00:00:00 2001 From: phosit Date: Mon, 14 Jul 2025 20:14:31 +0200 Subject: [PATCH] Change the return type of event-handler The return value is more strongly typed. --- source/graphics/CameraController.cpp | 33 ++++++------- source/graphics/CameraController.h | 4 +- source/graphics/GameView.cpp | 12 +++-- source/graphics/GameView.h | 7 +-- source/graphics/ICameraController.h | 7 ++- source/gui/CGUI.cpp | 11 ++--- source/gui/CGUI.h | 4 +- source/gui/GUIManager.cpp | 16 +++--- source/gui/GUIManager.h | 7 ++- source/gui/ObjectBases/IGUIObject.cpp | 20 ++++++-- source/gui/ObjectBases/IGUIObject.h | 11 ++--- source/gui/ObjectTypes/CDropDown.cpp | 14 +++--- source/gui/ObjectTypes/CDropDown.h | 4 +- source/gui/ObjectTypes/CHotkeyPicker.cpp | 18 +++---- source/gui/ObjectTypes/CHotkeyPicker.h | 4 +- source/gui/ObjectTypes/CInput.cpp | 49 ++++++++++--------- source/gui/ObjectTypes/CInput.h | 7 ++- source/gui/ObjectTypes/CList.cpp | 18 +++---- source/gui/ObjectTypes/CList.h | 3 +- source/gui/tests/test_GuiManager.h | 2 +- source/lib/input.h | 45 ----------------- source/main.cpp | 16 +++--- source/ps/CConsole.cpp | 24 ++++----- source/ps/CConsole.h | 5 +- source/ps/GameSetup/GameSetup.h | 2 +- source/ps/Globals.cpp | 16 +++--- source/ps/Globals.h | 6 +-- source/ps/Hotkey.cpp | 20 ++++---- source/ps/Hotkey.h | 10 ++-- source/ps/Input.cpp | 3 +- source/ps/Input.h | 16 ++++-- source/ps/ProfileViewer.cpp | 14 +++--- source/ps/ProfileViewer.h | 7 ++- source/ps/TouchInput.cpp | 10 ++-- source/ps/TouchInput.h | 8 ++- source/ps/VideoMode.h | 1 + source/ps/tests/test_Hotkeys.h | 1 - source/ps/tests/test_Input.h | 24 ++++----- .../Handlers/GraphicsSetupHandlers.cpp | 2 + .../GameInterface/Handlers/MiscHandlers.cpp | 3 ++ 40 files changed, 227 insertions(+), 257 deletions(-) delete mode 100644 source/lib/input.h diff --git a/source/graphics/CameraController.cpp b/source/graphics/CameraController.cpp index d1bcc9cfde..f93a19b1bd 100644 --- a/source/graphics/CameraController.cpp +++ b/source/graphics/CameraController.cpp @@ -22,7 +22,6 @@ #include "graphics/Camera.h" #include "graphics/Terrain.h" #include "lib/external_libraries/libsdl.h" -#include "lib/input.h" #include "maths/MathUtil.h" #include "maths/Matrix3D.h" #include "maths/Quaternion.h" @@ -655,7 +654,7 @@ void CCameraController::FocusHeight(bool smooth) m_PosY.Add(diff); } -InReaction CCameraController::HandleEvent(const SDL_Event& ev) +Input::Reaction CCameraController::HandleEvent(const SDL_Event& ev) { switch (ev.type) { @@ -665,14 +664,14 @@ InReaction CCameraController::HandleEvent(const SDL_Event& ev) if (hotkey == "camera.reset") { ResetCameraAngleZoom(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.togglebirdseyeview") { ToggleBirdsEyeView(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } - return IN_PASS; + return Input::Reaction::PASS; } case SDL_HOTKEYDOWN: @@ -685,64 +684,64 @@ InReaction CCameraController::HandleEvent(const SDL_Event& ev) if (hotkey == "camera.zoom.wheel.in") { m_Zoom.AddSmoothly(-m_ViewZoomSpeedWheel); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.zoom.wheel.out") { m_Zoom.AddSmoothly(m_ViewZoomSpeedWheel); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.rotate.wheel.cw") { m_RotateY.AddSmoothly(m_ViewRotateYSpeedWheel); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.rotate.wheel.ccw") { m_RotateY.AddSmoothly(-m_ViewRotateYSpeedWheel); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.scroll.speed.increase") { m_ViewScrollSpeed *= m_ViewScrollSpeedModifier; LOGMESSAGERENDER("Scroll speed increased to %.1f", m_ViewScrollSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.scroll.speed.decrease") { m_ViewScrollSpeed /= m_ViewScrollSpeedModifier; LOGMESSAGERENDER("Scroll speed decreased to %.1f", m_ViewScrollSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.rotate.speed.increase") { m_ViewRotateXSpeed *= m_ViewRotateSpeedModifier; m_ViewRotateYSpeed *= m_ViewRotateSpeedModifier; LOGMESSAGERENDER("Rotate speed increased to X=%.3f, Y=%.3f", m_ViewRotateXSpeed, m_ViewRotateYSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.rotate.speed.decrease") { m_ViewRotateXSpeed /= m_ViewRotateSpeedModifier; m_ViewRotateYSpeed /= m_ViewRotateSpeedModifier; LOGMESSAGERENDER("Rotate speed decreased to X=%.3f, Y=%.3f", m_ViewRotateXSpeed, m_ViewRotateYSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.zoom.speed.increase") { m_ViewZoomSpeed *= m_ViewZoomSpeedModifier; LOGMESSAGERENDER("Zoom speed increased to %.1f", m_ViewZoomSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "camera.zoom.speed.decrease") { m_ViewZoomSpeed /= m_ViewZoomSpeedModifier; LOGMESSAGERENDER("Zoom speed decreased to %.1f", m_ViewZoomSpeed); - return IN_HANDLED; + return Input::Reaction::HANDLED; } - return IN_PASS; + return Input::Reaction::PASS; } } - return IN_PASS; + return Input::Reaction::PASS; } diff --git a/source/graphics/CameraController.h b/source/graphics/CameraController.h index 8f64dd139b..ff6df9910b 100644 --- a/source/graphics/CameraController.h +++ b/source/graphics/CameraController.h @@ -21,7 +21,7 @@ #include "graphics/ICameraController.h" #include "graphics/SmoothedValue.h" #include "lib/code_annotation.h" -#include "lib/input.h" +#include "ps/Input.h" #include "simulation2/system/Entity.h" #include @@ -41,7 +41,7 @@ public: void LoadConfig() override; - InReaction HandleEvent(const SDL_Event& ev) override; + Input::Reaction HandleEvent(const SDL_Event& ev) override; CVector3D GetCameraPivot() const override; CVector3D GetCameraPosition() const override; diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index 5f38558a16..a163a913f2 100644 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -33,9 +33,11 @@ #include "graphics/Terrain.h" #include "graphics/TerrainTextureManager.h" #include "graphics/TerritoryTexture.h" +#include "graphics/Unit.h" +#include "graphics/UnitManager.h" #include "lib/external_libraries/libsdl.h" -#include "lib/input.h" #include "lib/posix/posix_types.h" +#include "lib/timer.h" #include "lobby/XmppClient.h" #include "maths/BoundingBoxAligned.h" #include "maths/Frustum.h" @@ -351,18 +353,18 @@ entity_id_t CGameView::GetFollowedEntity() return m->CameraController->GetFollowedEntity(); } -InReaction game_view_handler(const SDL_Event& ev) +Input::Reaction game_view_handler(const SDL_Event& ev) { // put any events that must be processed even if inactive here if (!g_app_has_focus || !g_Game || !g_Game->IsGameStarted() || g_Game->GetView()->GetCinema()->IsPlaying()) - return IN_PASS; + return Input::Reaction::PASS; CGameView *pView=g_Game->GetView(); return pView->HandleEvent(ev); } -InReaction CGameView::HandleEvent(const SDL_Event& ev) +Input::Reaction CGameView::HandleEvent(const SDL_Event& ev) { switch(ev.type) { @@ -395,7 +397,7 @@ InReaction CGameView::HandleEvent(const SDL_Event& ev) sceneRenderer.SetModelRenderMode(SOLID); sceneRenderer.SetOverlayRenderMode(SOLID); } - return IN_HANDLED; + return Input::Reaction::HANDLED; } } } diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h index 0de173a1a1..3443bb8145 100644 --- a/source/graphics/GameView.h +++ b/source/graphics/GameView.h @@ -19,7 +19,8 @@ #define INCLUDED_GAMEVIEW #include "lib/code_annotation.h" -#include "lib/input.h" // InReaction - can't forward-declare enum +#include "ps/Input.h" +#include "renderer/backend/IDeviceCommandContext.h" #include "renderer/Scene.h" #include "simulation2/system/Entity.h" @@ -58,7 +59,7 @@ public: void Render(Renderer::Backend::IDeviceCommandContext* deviceCommandContext); void RenderOverlays(Renderer::Backend::IDeviceCommandContext* deviceCommandContext); - InReaction HandleEvent(const SDL_Event& ev); + Input::Reaction HandleEvent(const SDL_Event& ev); CVector3D GetCameraPivot() const; CVector3D GetCameraPosition() const; @@ -98,6 +99,6 @@ private: CGameViewImpl* m; }; -extern InReaction game_view_handler(const SDL_Event& ev); +Input::Reaction game_view_handler(const SDL_Event& ev); #endif // INCLUDED_GAMEVIEW diff --git a/source/graphics/ICameraController.h b/source/graphics/ICameraController.h index e71075876c..05c743ca91 100644 --- a/source/graphics/ICameraController.h +++ b/source/graphics/ICameraController.h @@ -18,15 +18,14 @@ #ifndef INCLUDED_ICAMERACONTROLLER #define INCLUDED_ICAMERACONTROLLER +#include "graphics/Camera.h" #include "lib/code_annotation.h" -#include "lib/input.h" // InReaction - can't forward-declare enum +#include "ps/Input.h" #include "simulation2/system/Entity.h" class CCamera; class CVector3D; struct SViewPort; -union SDL_Event; - /** * @interface ICameraController defines a camera controller interface. The camera object * is owned by the camera controller's owner. It is therefore guaranteed that the lifetime @@ -43,7 +42,7 @@ public: virtual void LoadConfig() = 0; - virtual InReaction HandleEvent(const SDL_Event& ev) = 0; + virtual Input::Reaction HandleEvent(const SDL_Event& ev) = 0; virtual CVector3D GetCameraPivot() const = 0; virtual CVector3D GetCameraPosition() const = 0; diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 9304713e06..9db0243885 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -36,7 +36,6 @@ #include "lib/debug.h" #include "lib/external_libraries/libsdl.h" #include "lib/file/vfs/vfs_util.h" -#include "lib/input.h" #include "lib/path.h" #include "lib/timer.h" #include "lib/utf8.h" @@ -111,9 +110,9 @@ CGUI::~CGUI() g_NetClient->Unregister(m_ScriptInterface.get()); } -InReaction CGUI::HandleEvent(const SDL_Event& ev) +Input::Reaction CGUI::HandleEvent(const SDL_Event& ev) { - InReaction ret = IN_PASS; + Input::Reaction ret = Input::Reaction::PASS; if (ev.type == SDL_HOTKEYDOWN || ev.type == SDL_HOTKEYPRESS || ev.type == SDL_HOTKEYUP) { @@ -123,7 +122,7 @@ InReaction CGUI::HandleEvent(const SDL_Event& ev) if (m_GlobalHotkeys.find(hotkey) != m_GlobalHotkeys.end() && m_GlobalHotkeys[hotkey].find(eventName) != m_GlobalHotkeys[hotkey].end()) { - ret = IN_HANDLED; + ret = Input::Reaction::HANDLED; ScriptRequest rq(m_ScriptInterface); JS::RootedObject globalObj(rq.cx, rq.glob); @@ -188,7 +187,7 @@ InReaction CGUI::HandleEvent(const SDL_Event& ev) // pNearest will after this point at the hovered object, possibly nullptr IGUIObject* pNearest = FindObjectUnderMouse(); - if (ret == IN_PASS) + if (ret == Input::Reaction::PASS) { // Now we'll call UpdateMouseOver on *all* objects, // we'll input the one hovered, and they will each @@ -286,7 +285,7 @@ InReaction CGUI::HandleEvent(const SDL_Event& ev) m_MousePos = oldMousePos; // Let GUI items handle keys after everything else, e.g. for input boxes. - if (ret == IN_PASS && GetFocusedObject()) + if (ret == Input::Reaction::PASS && GetFocusedObject()) { if (ev.type == SDL_KEYUP || ev.type == SDL_KEYDOWN || ev.type == SDL_HOTKEYUP || ev.type == SDL_HOTKEYDOWN || diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index 30922555a0..850c856eaa 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -31,13 +31,13 @@ #include "gui/SettingTypes/CGUIColor.h" #include "lib/code_annotation.h" #include "lib/file/vfs/vfs_path.h" -#include "lib/input.h" #include "lib/types.h" #include "maths/Rect.h" #include "maths/Vector2D.h" #include "ps/CStr.h" #include "scriptinterface/ModuleLoader.h" #include "scriptinterface/StructuredClone.h" +#include "ps/Input.h" #include #include @@ -136,7 +136,7 @@ public: * * @param ev SDL Event, like mouse/keyboard input */ - InReaction HandleEvent(const SDL_Event& ev); + Input::Reaction HandleEvent(const SDL_Event& ev); /** * Load a GUI XML file into the GUI. diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index e4daa4b20a..72edb79750 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -82,10 +82,10 @@ CGUIManager* g_GUI = nullptr; // called from main loop when (input) events are received. // event is passed to other handlers if false is returned. // trampoline: we don't want to make the HandleEvent implementation static -InReaction gui_handler(const SDL_Event& ev) +Input::Reaction gui_handler(const SDL_Event& ev) { if (!g_GUI) - return IN_PASS; + return Input::Reaction::PASS; PROFILE("GUI event handler"); return g_GUI->HandleEvent(ev); @@ -371,7 +371,7 @@ Status CGUIManager::ReloadAllPages() return INFO::OK; } -InReaction CGUIManager::HandleEvent(const SDL_Event& ev) +Input::Reaction CGUIManager::HandleEvent(const SDL_Event& ev) { // We want scripts to have access to the raw input events, so they can do complex // processing when necessary (e.g. for unit selection and camera movement). @@ -389,13 +389,13 @@ InReaction CGUIManager::HandleEvent(const SDL_Event& ev) JS::RootedValue global(rq.cx, rq.globalValue()); if (ScriptFunction::Call(rq, global, "handleInputBeforeGui", handled, ev, top()->FindObjectUnderMouse())) if (handled) - return IN_HANDLED; + return Input::Reaction::HANDLED; } { PROFILE("handle event in native GUI"); - InReaction r = top()->HandleEvent(ev); - if (r != IN_PASS) + Input::Reaction r = top()->HandleEvent(ev); + if (r != Input::Reaction::PASS) return r; } @@ -407,10 +407,10 @@ InReaction CGUIManager::HandleEvent(const SDL_Event& ev) PROFILE("handleInputAfterGui"); if (ScriptFunction::Call(rq, global, "handleInputAfterGui", handled, ev)) if (handled) - return IN_HANDLED; + return Input::Reaction::HANDLED; } - return IN_PASS; + return Input::Reaction::PASS; } diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index 22cd5462d5..ecfbbb4c4b 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -19,10 +19,10 @@ #define INCLUDED_GUIMANAGER #include "lib/code_annotation.h" -#include "lib/input.h" #include "lib/path.h" #include "lib/status.h" #include "ps/CStr.h" +#include "ps/Input.h" #include "ps/TemplateLoader.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/StructuredClone.h" @@ -43,7 +43,6 @@ class ScriptContext; namespace JS { class HandleValueArray; } namespace JS { class Value; } namespace PS { template class StaticVector; } -union SDL_Event; /** * External interface to the GUI system. @@ -99,7 +98,7 @@ public: /** * Pass input events to the currently active GUI page. */ - InReaction HandleEvent(const SDL_Event& ev); + Input::Reaction HandleEvent(const SDL_Event& ev); /** * See CGUI::SendEventToAll; applies to the currently active page. @@ -242,6 +241,6 @@ private: extern CGUIManager* g_GUI; -extern InReaction gui_handler(const SDL_Event& ev); +extern Input::Reaction gui_handler(const SDL_Event& ev); #endif // INCLUDED_GUIMANAGER diff --git a/source/gui/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp index 9651210c27..86682edc85 100644 --- a/source/gui/ObjectBases/IGUIObject.cpp +++ b/source/gui/ObjectBases/IGUIObject.cpp @@ -388,7 +388,7 @@ void IGUIObject::UnsetScriptHandler(const CStr& eventName) m_pGUI.m_EventObjects.erase(it2); } -InReaction IGUIObject::SendEvent(EGUIMessageType type, const CStr& eventName) +Input::Reaction IGUIObject::SendEvent(EGUIMessageType type, const CStr& eventName) { PROFILE2_EVENT("gui event"); PROFILE2_ATTR("type: %s", eventName.c_str()); @@ -399,10 +399,10 @@ InReaction IGUIObject::SendEvent(EGUIMessageType type, const CStr& eventName) ScriptEvent(eventName); - return msg.skipped ? IN_PASS : IN_HANDLED; + return msg.skipped ? Input::Reaction::PASS : Input::Reaction::HANDLED; } -InReaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventName) +Input::Reaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventName) { PROFILE2_EVENT("gui mouse event"); PROFILE2_ATTR("type: %s", eventName.c_str()); @@ -442,12 +442,12 @@ InReaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventNam if ((type == GUIM_MOUSE_WHEEL_UP || type == GUIM_MOUSE_WHEEL_DOWN || type == GUIM_MOUSE_WHEEL_LEFT || type == GUIM_MOUSE_WHEEL_RIGHT) && msg.skipped) { if (GetParent()) - msg.Skip(GetParent()->SendMouseEvent(type, eventName) == IN_PASS); + msg.Skip(GetParent()->SendMouseEvent(type, eventName) == Input::Reaction::PASS); else msg.Skip(false); } - return msg.skipped ? IN_PASS : IN_HANDLED; + return msg.skipped ? Input::Reaction::PASS : Input::Reaction::HANDLED; } bool IGUIObject::ScriptEvent(const CStr& eventName, @@ -568,3 +568,13 @@ void IGUIObject::DrawInArea(CCanvas2D& canvas, CRect& area) bool IGUIObject::IsHiddenOrGhostOrOutOfBoundaries() const { return !m_IsInsideBoundaries || IsHiddenOrGhost(); } + +Input::Reaction IGUIObject::PreemptEvent(const SDL_Event&) +{ + return Input::Reaction::PASS; +} + +Input::Reaction IGUIObject::ManuallyHandleKeys(const SDL_Event&) +{ + return Input::Reaction::PASS; +} diff --git a/source/gui/ObjectBases/IGUIObject.h b/source/gui/ObjectBases/IGUIObject.h index 3d4f550095..adf2213731 100644 --- a/source/gui/ObjectBases/IGUIObject.h +++ b/source/gui/ObjectBases/IGUIObject.h @@ -30,9 +30,9 @@ #include "gui/SettingTypes/CGUIHotkey.h" #include "gui/SettingTypes/CGUISize.h" #include "lib/code_annotation.h" -#include "lib/input.h" #include "maths/Rect.h" #include "ps/CStr.h" +#include "ps/Input.h" #include #include @@ -49,7 +49,6 @@ class JSTracer; class XMBData; class XMBElement; namespace JS { class HandleValueArray; } -union SDL_Event; #define GUI_OBJECT(obj) \ public: \ @@ -182,12 +181,12 @@ public: * @param eventName String representation of event name * @return IN_HANDLED if event was handled, or IN_PASS if skipped */ - InReaction SendEvent(EGUIMessageType type, const CStr& eventName); + Input::Reaction SendEvent(EGUIMessageType type, const CStr& eventName); /** * Same as SendEvent, but passes mouse coordinates and button state as an argument. */ - InReaction SendMouseEvent(EGUIMessageType type, const CStr& eventName); + Input::Reaction SendMouseEvent(EGUIMessageType type, const CStr& eventName); /** * All sizes are relative to resolution, and the calculation @@ -292,7 +291,7 @@ protected: * Returns either IN_PASS or IN_HANDLED. If IN_HANDLED, then * the event won't be passed on and processed by other handlers. */ - virtual InReaction PreemptEvent(const SDL_Event&) { return IN_PASS; } + virtual Input::Reaction PreemptEvent(const SDL_Event&); /** * Some objects need to handle the text-related SDL_Event manually. @@ -304,7 +303,7 @@ protected: * the key won't be passed on and processed by other handlers. * This is used for keys that the GUI uses. */ - virtual InReaction ManuallyHandleKeys(const SDL_Event&) { return IN_PASS; } + virtual Input::Reaction ManuallyHandleKeys(const SDL_Event&); /** * Applies the given style to the object. diff --git a/source/gui/ObjectTypes/CDropDown.cpp b/source/gui/ObjectTypes/CDropDown.cpp index a528d99f12..430c5c9975 100644 --- a/source/gui/ObjectTypes/CDropDown.cpp +++ b/source/gui/ObjectTypes/CDropDown.cpp @@ -272,9 +272,9 @@ void CDropDown::HandleMessage(SGUIMessage& Message) SetupText(); } -InReaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev) +Input::Reaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev) { - InReaction result = IN_PASS; + Input::Reaction result{Input::Reaction::PASS}; bool update_highlight = false; if (ev.type == SDL_KEYDOWN) @@ -285,7 +285,7 @@ InReaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev) { case '\r': m_Open = false; - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_HOME: @@ -295,7 +295,7 @@ InReaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev) case SDLK_PAGEUP: case SDLK_PAGEDOWN: if (!m_Open) - return IN_PASS; + return Input::Reaction::PASS; // Set current selected item to highlighted, before // then really processing these in CList::ManuallyHandleKeys() m_Selected.Set(m_ElementHighlight, true); @@ -349,14 +349,14 @@ InReaction CDropDown::ManuallyHandleKeys(const SDL_Event& ev) update_highlight = true; GetScrollBar(0).SetPos(m_ItemsYPositions[closest] - 60); } - result = IN_HANDLED; + result = Input::Reaction::HANDLED; } break; } } - if (CList::ManuallyHandleKeys(ev) == IN_HANDLED) - result = IN_HANDLED; + if (CList::ManuallyHandleKeys(ev) == Input::Reaction::HANDLED) + result = Input::Reaction::HANDLED; if (update_highlight) m_ElementHighlight = m_Selected; diff --git a/source/gui/ObjectTypes/CDropDown.h b/source/gui/ObjectTypes/CDropDown.h index 39b44272df..0583463865 100644 --- a/source/gui/ObjectTypes/CDropDown.h +++ b/source/gui/ObjectTypes/CDropDown.h @@ -33,11 +33,11 @@ GUI Object - Drop Down (list) #include "gui/ObjectBases/IGUIObject.h" #include "gui/ObjectTypes/CList.h" #include "gui/SettingTypes/CGUIColor.h" -#include "lib/input.h" #include "lib/types.h" #include "maths/Rect.h" #include "maths/Vector2D.h" #include "ps/CStr.h" +#include "ps/Input.h" #include @@ -68,7 +68,7 @@ public: /** * Handle events manually to catch keyboard inputting. */ - virtual InReaction ManuallyHandleKeys(const SDL_Event& ev); + virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev); /** * Draws the Button diff --git a/source/gui/ObjectTypes/CHotkeyPicker.cpp b/source/gui/ObjectTypes/CHotkeyPicker.cpp index 18b5465a82..dfb3b1df30 100644 --- a/source/gui/ObjectTypes/CHotkeyPicker.cpp +++ b/source/gui/ObjectTypes/CHotkeyPicker.cpp @@ -105,7 +105,7 @@ void CHotkeyPicker::HandleMessage(SGUIMessage& Message) } } -InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) +Input::Reaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) { switch (ev.type) { @@ -121,7 +121,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) // Wait a little bit -> this gets triggered when clicking on a button, // but after the button click is processed, thus immediately triggering... if (timer_Time()-m_LastKeyChange < 0.2) - return IN_HANDLED; + return Input::Reaction::HANDLED; // This is from hotkeyHandler - not sure what it does in all honesty. if(ev.button.button >= SDL_BUTTON_X1) scancode = static_cast(MOUSE_BASE + static_cast(ev.button.button) + 2); @@ -139,7 +139,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) else if (ev.wheel.x < 0) scancode = static_cast(MOUSE_X1); else - return IN_HANDLED; + return Input::Reaction::HANDLED; } // Don't handle keys and mouse together except for modifiers. m_KeysPressed.erase(std::remove_if(m_KeysPressed.begin(), m_KeysPressed.end(), [](const Key& k) { @@ -148,7 +148,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) // For mouse events, assume we immediately want to return. FireEvent(EventNameCombination); - return IN_HANDLED; + return Input::Reaction::HANDLED; } case SDL_KEYDOWN: case SDL_KEYUP: @@ -157,7 +157,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) // Don't handle caps-lock, it doesn't really work in-game and it's a weird hotkey. if (scancode == SDL_SCANCODE_CAPSLOCK) - return IN_PASS; + return Input::Reaction::PASS; if (scancode == SDL_SCANCODE_LSHIFT || scancode == SDL_SCANCODE_RSHIFT) scancode = static_cast(UNIFIED_SHIFT); @@ -174,7 +174,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) std::find_if(m_KeysPressed.begin(), m_KeysPressed.end(), [&scancode](Key& k) { return k.code == scancode; }); // Can happen if multiple keys are mapped the same. if (it != m_KeysPressed.end()) - return IN_HANDLED; + return Input::Reaction::HANDLED; m_KeysPressed.emplace_back(Key{scancode, FindScancodeName(scancode)}); } else @@ -183,7 +183,7 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) std::find_if(m_KeysPressed.begin(), m_KeysPressed.end(), [&scancode](Key& k) { return k.code == scancode; }); // Might happen if a key was down before this object is created. if (it == m_KeysPressed.end()) - return IN_HANDLED; + return Input::Reaction::HANDLED; m_KeysPressed.erase(it); } @@ -191,11 +191,11 @@ InReaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) // Register after-JS in case this takes a while (probably not but it doesn't hurt). m_LastKeyChange = timer_Time(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } default: { - return IN_PASS; + return Input::Reaction::PASS; } } } diff --git a/source/gui/ObjectTypes/CHotkeyPicker.h b/source/gui/ObjectTypes/CHotkeyPicker.h index a503314a95..84bcfa3ae3 100644 --- a/source/gui/ObjectTypes/CHotkeyPicker.h +++ b/source/gui/ObjectTypes/CHotkeyPicker.h @@ -20,8 +20,8 @@ #include "gui/CGUISetting.h" #include "gui/ObjectBases/IGUIObject.h" -#include "lib/input.h" #include "ps/CStr.h" +#include "ps/Input.h" #include #include @@ -53,7 +53,7 @@ public: virtual void HandleMessage(SGUIMessage& Message); // Pre-empt events: this is our sole purpose. - virtual InReaction PreemptEvent(const SDL_Event& ev); + virtual Input::Reaction PreemptEvent(const SDL_Event& ev); struct Key { diff --git a/source/gui/ObjectTypes/CInput.cpp b/source/gui/ObjectTypes/CInput.cpp index d75f7f2e27..b9c41b87b3 100644 --- a/source/gui/ObjectTypes/CInput.cpp +++ b/source/gui/ObjectTypes/CInput.cpp @@ -37,6 +37,7 @@ #include "ps/ConfigDB.h" #include "ps/Globals.h" #include "ps/Hotkey.h" +#include "ps/Input.h" #include #include @@ -113,7 +114,7 @@ void CInput::ClearComposedText() m_iComposedPos = 0; } -InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) +Input::Reaction CInput::ManuallyHandleKeys(const SDL_Event& ev) { ENSURE(m_iBufferPos != -1); @@ -126,7 +127,7 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) case SDL_HOTKEYDOWN: { if (m_ComposingText) - return IN_HANDLED; + return Input::Reaction::HANDLED; return ManuallyHandleHotkeyEvent(ev); } @@ -135,14 +136,14 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) case SDL_TEXTINPUT: { if (m_Readonly) - return IN_PASS; + return Input::Reaction::PASS; // Text has been committed, either single key presses or through an IME std::wstring text = wstring_from_utf8(ev.text.text); // Check max length if (m_MaxLength != 0 && caption.length() + text.length() > static_cast(m_MaxLength)) - return IN_HANDLED; + return Input::Reaction::HANDLED; m_WantedX = 0.0f; @@ -169,12 +170,12 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) UpdateAutoScroll(); SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); - return IN_HANDLED; + return Input::Reaction::HANDLED; } case SDL_TEXTEDITING: { if (m_Readonly) - return IN_PASS; + return Input::Reaction::PASS; // Text is being composed with an IME // TODO: indicate this by e.g. underlining the uncommitted text @@ -217,7 +218,7 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) UpdateAutoScroll(); SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); - return IN_HANDLED; + return Input::Reaction::HANDLED; } case SDL_KEYDOWN: case SDL_KEYUP: @@ -237,10 +238,10 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) if (keyCode == SDLK_ESCAPE || EventWillFireHotkey(ev, "cancel") || g_scancodes[SDL_SCANCODE_LCTRL] || g_scancodes[SDL_SCANCODE_RCTRL] || g_scancodes[SDL_SCANCODE_LGUI] || g_scancodes[SDL_SCANCODE_RGUI]) - return IN_PASS; + return Input::Reaction::PASS; if (m_ComposingText) - return IN_HANDLED; + return Input::Reaction::HANDLED; if (ev.type == SDL_KEYDOWN) { @@ -249,11 +250,11 @@ InReaction CInput::ManuallyHandleKeys(const SDL_Event& ev) UpdateBufferPositionSetting(); } - return IN_HANDLED; + return Input::Reaction::HANDLED; } default: { - return IN_PASS; + return Input::Reaction::PASS; } } } @@ -619,7 +620,7 @@ void CInput::SetupGeneratedPlaceholderText() m_GeneratedPlaceholderTextValid = true; } -InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) +Input::Reaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) { bool shiftKeyPressed = g_scancodes[SDL_SCANCODE_LSHIFT] || g_scancodes[SDL_SCANCODE_RSHIFT]; @@ -632,13 +633,13 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) if (hotkey == "paste") { if (m_Readonly) - return IN_PASS; + return Input::Reaction::PASS; m_WantedX = 0.0f; char* utf8_text = SDL_GetClipboardText(); if (!utf8_text) - return IN_HANDLED; + return Input::Reaction::HANDLED; std::wstring text = wstring_from_utf8(utf8_text); SDL_free(utf8_text); @@ -665,12 +666,12 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "copy" || hotkey == "cut") { if (m_Readonly && hotkey == "cut") - return IN_PASS; + return Input::Reaction::PASS; m_WantedX = 0.0f; @@ -702,12 +703,12 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) } } - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "text.delete.left") { if (m_Readonly) - return IN_PASS; + return Input::Reaction::PASS; m_WantedX = 0.0f; @@ -748,12 +749,12 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); } UpdateAutoScroll(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "text.delete.right") { if (m_Readonly) - return IN_PASS; + return Input::Reaction::PASS; m_WantedX = 0.0f; @@ -785,7 +786,7 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) } UpdateAutoScroll(); SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "text.move.left") { @@ -838,7 +839,7 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) UpdateBufferPositionSetting(); UpdateAutoScroll(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "text.move.right") { @@ -881,10 +882,10 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event& ev) UpdateBufferPositionSetting(); UpdateAutoScroll(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } - return IN_PASS; + return Input::Reaction::PASS; } void CInput::ResetStates() diff --git a/source/gui/ObjectTypes/CInput.h b/source/gui/ObjectTypes/CInput.h index 190eddbc72..67312882a4 100644 --- a/source/gui/ObjectTypes/CInput.h +++ b/source/gui/ObjectTypes/CInput.h @@ -25,10 +25,10 @@ #include "gui/ObjectBases/IGUIScrollBarOwner.h" #include "gui/SettingTypes/CGUIColor.h" #include "gui/SettingTypes/CGUIString.h" -#include "lib/input.h" #include "lib/types.h" #include "maths/Rect.h" #include "ps/CStr.h" +#include "ps/Input.h" #include #include @@ -36,7 +36,6 @@ class CCanvas2D; class CGUI; -union SDL_Event; /** * Text field where you can input and edit the text. @@ -80,7 +79,7 @@ protected: /** * Handle events manually to catch keyboard inputting. */ - virtual InReaction ManuallyHandleKeys(const SDL_Event& ev); + virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev); /** * Handle events manually to catch keys which change the text. @@ -95,7 +94,7 @@ protected: /** * Handle hotkey events (called by ManuallyHandleKeys) */ - virtual InReaction ManuallyHandleHotkeyEvent(const SDL_Event& ev); + virtual Input::Reaction ManuallyHandleHotkeyEvent(const SDL_Event& ev); /** * @see IGUIObject#HandleSizeChanged() diff --git a/source/gui/ObjectTypes/CList.cpp b/source/gui/ObjectTypes/CList.cpp index 0be7180f10..e0ecefb986 100644 --- a/source/gui/ObjectTypes/CList.cpp +++ b/source/gui/ObjectTypes/CList.cpp @@ -260,9 +260,9 @@ void CList::HandleMessage(SGUIMessage& Message) IGUITextOwner::HandleMessage(Message); } -InReaction CList::ManuallyHandleKeys(const SDL_Event& ev) +Input::Reaction CList::ManuallyHandleKeys(const SDL_Event& ev) { - InReaction result = IN_PASS; + Input::Reaction result{Input::Reaction::PASS}; if (ev.type == SDL_KEYDOWN) { @@ -273,39 +273,39 @@ InReaction CList::ManuallyHandleKeys(const SDL_Event& ev) case SDLK_HOME: SelectFirstElement(); UpdateAutoScroll(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_END: SelectLastElement(); UpdateAutoScroll(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_UP: SelectPrevElement(); UpdateAutoScroll(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_DOWN: SelectNextElement(); UpdateAutoScroll(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_PAGEUP: GetScrollBar(0).ScrollMinusPlenty(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; case SDLK_PAGEDOWN: GetScrollBar(0).ScrollPlusPlenty(); - result = IN_HANDLED; + result = Input::Reaction::HANDLED; break; default: // Do nothing - result = IN_PASS; + result = Input::Reaction::PASS; } } diff --git a/source/gui/ObjectTypes/CList.h b/source/gui/ObjectTypes/CList.h index 8fc2a1639d..d94fffb2eb 100644 --- a/source/gui/ObjectTypes/CList.h +++ b/source/gui/ObjectTypes/CList.h @@ -25,7 +25,6 @@ #include "gui/ObjectBases/IGUITextOwner.h" #include "gui/SettingTypes/CGUIColor.h" #include "gui/SettingTypes/CGUIList.h" -#include "lib/input.h" #include "lib/types.h" #include "maths/Rect.h" #include "ps/CStr.h" @@ -89,7 +88,7 @@ protected: /** * Handle events manually to catch keyboard inputting. */ - virtual InReaction ManuallyHandleKeys(const SDL_Event& ev); + virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev); /** * Draws the List box diff --git a/source/gui/tests/test_GuiManager.h b/source/gui/tests/test_GuiManager.h index ad415e3d0c..13b8c27169 100644 --- a/source/gui/tests/test_GuiManager.h +++ b/source/gui/tests/test_GuiManager.h @@ -23,7 +23,6 @@ #include "lib/external_libraries/libsdl.h" #include "lib/file/file_system.h" #include "lib/file/vfs/vfs.h" -#include "lib/input.h" #include "lib/path.h" #include "ps/CLogger.h" #include "ps/CStr.h" @@ -31,6 +30,7 @@ #include "ps/Filesystem.h" #include "ps/GameSetup/GameSetup.h" #include "ps/Hotkey.h" +#include "ps/VideoMode.h" #include "ps/XML/Xeromyces.h" #include "ps/VideoMode.h" #include "scriptinterface/FunctionWrapper.h" diff --git a/source/lib/input.h b/source/lib/input.h deleted file mode 100644 index 0cd434e186..0000000000 --- a/source/lib/input.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright (C) 2026 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * SDL input redirector; dispatches to multiple handlers. - */ - -#ifndef INCLUDED_INPUT -#define INCLUDED_INPUT - -union SDL_Event; - -// input handler return values. -enum InReaction -{ - // (the handlers' return values are checked and these - // 'strange' values might bring errors to light) - - // pass the event to the next handler in the chain - IN_PASS = 4, - - // we've handled it; no other handlers will receive this event. - IN_HANDLED = 2 -}; - -#endif // #ifndef INCLUDED_INPUT diff --git a/source/main.cpp b/source/main.cpp index 412d201d7f..37f3450447 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -195,7 +195,7 @@ void RestartEngine() } // main app message handler -static InReaction MainInputHandler(const SDL_Event& ev) +static Input::Reaction MainInputHandler(const SDL_Event& ev) { switch(ev.type) { @@ -239,27 +239,27 @@ static InReaction MainInputHandler(const SDL_Event& ev) if (hotkey == "exit") { QuitEngine(EXIT_SUCCESS); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "screenshot") { g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::DEFAULT); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "bigscreenshot") { g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::BIG); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "togglefullscreen") { g_VideoMode.ToggleFullscreen(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "profile2.toggle") { g_Profiler2.Toggle(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (hotkey == "mousegrabtoggle") { @@ -270,7 +270,7 @@ static InReaction MainInputHandler(const SDL_Event& ev) break; } - return IN_PASS; + return Input::Reaction::PASS; } @@ -756,7 +756,7 @@ static void RunGameOrAtlas(const std::span argv) private: ScriptInterface scriptInterface{"Engine", "gui", *g_ScriptContext}; std::unique_ptr inputHandlers; - Input::Handler mainInputHandler{ + Input::Handler mainInputHandler{ g_VideoMode.m_InputManager, Input::Slot::PRIMARY, MainInputHandler}; }; diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index 222c366776..e445381d9c 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -692,10 +692,10 @@ static bool isUnprintableChar(SDL_Keysym key) } } -InReaction conInputHandler(const SDL_Event& ev) +Input::Reaction conInputHandler(const SDL_Event& ev) { if (!g_Console) - return IN_PASS; + return Input::Reaction::PASS; if (static_cast(ev.type) == SDL_HOTKEYPRESS) { @@ -705,19 +705,19 @@ InReaction conInputHandler(const SDL_Event& ev) { ResetActiveHotkeys(); g_Console->ToggleVisible(); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (g_Console->IsActive() && hotkey == "copy") { std::string text = utf8_from_wstring(g_Console->GetBuffer()); SDL_SetClipboardText(text.c_str()); - return IN_HANDLED; + return Input::Reaction::HANDLED; } else if (g_Console->IsActive() && hotkey == "paste") { char* utf8_text = SDL_GetClipboardText(); if (!utf8_text) - return IN_HANDLED; + return Input::Reaction::HANDLED; std::wstring text = wstring_from_utf8(utf8_text); SDL_free(utf8_text); @@ -725,12 +725,12 @@ InReaction conInputHandler(const SDL_Event& ev) for (wchar_t c : text) g_Console->InsertChar(0, c); - return IN_HANDLED; + return Input::Reaction::HANDLED; } } if (!g_Console->IsActive()) - return IN_PASS; + return Input::Reaction::PASS; // In SDL2, we no longer get Unicode wchars via SDL_Keysym // we use text input events instead and they provide UTF-8 chars @@ -740,12 +740,12 @@ InReaction conInputHandler(const SDL_Event& ev) std::wstring wstr = wstring_from_utf8(ev.text.text); for (size_t i = 0; i < wstr.length(); ++i) g_Console->InsertChar(0, wstr[i]); - return IN_HANDLED; + return Input::Reaction::HANDLED; } // TODO: text editing events for IME support if (ev.type != SDL_KEYDOWN && ev.type != SDL_KEYUP) - return IN_PASS; + return Input::Reaction::PASS; int sym = ev.key.keysym.sym; @@ -754,7 +754,7 @@ InReaction conInputHandler(const SDL_Event& ev) !HotkeyIsPressed("console.toggle")) { g_Console->InsertChar(sym, 0); - return IN_HANDLED; + return Input::Reaction::HANDLED; } // We have a probably printable key - we should return HANDLED so it can't trigger hotkeys. @@ -765,7 +765,7 @@ InReaction conInputHandler(const SDL_Event& ev) if (EventWillFireHotkey(ev, "console.toggle") || g_scancodes[SDL_SCANCODE_LCTRL] || g_scancodes[SDL_SCANCODE_RCTRL] || g_scancodes[SDL_SCANCODE_LGUI] || g_scancodes[SDL_SCANCODE_RGUI]) - return IN_PASS; + return Input::Reaction::PASS; - return IN_HANDLED; + return Input::Reaction::HANDLED; } diff --git a/source/ps/CConsole.h b/source/ps/CConsole.h index 7bca986539..d68a6d6615 100644 --- a/source/ps/CConsole.h +++ b/source/ps/CConsole.h @@ -24,7 +24,7 @@ #include "lib/code_annotation.h" #include "lib/file/vfs/vfs_path.h" -#include "lib/input.h" +#include "ps/Input.h" #include #include @@ -34,7 +34,6 @@ class CCanvas2D; class CTextRenderer; -union SDL_Event; /** * In-game console. @@ -137,6 +136,6 @@ private: extern CConsole* g_Console; -extern InReaction conInputHandler(const SDL_Event& ev); +extern Input::Reaction conInputHandler(const SDL_Event& ev); #endif // INCLUDED_CCONSOLE diff --git a/source/ps/GameSetup/GameSetup.h b/source/ps/GameSetup/GameSetup.h index 1b971f111c..87aab46fbc 100644 --- a/source/ps/GameSetup/GameSetup.h +++ b/source/ps/GameSetup/GameSetup.h @@ -73,7 +73,7 @@ void InitVfs(const CmdLineArgs& args); */ extern bool Init(const CmdLineArgs& args, int flags); -using InputHandlers = std::queue>; +using InputHandlers = std::queue>; [[nodiscard]] std::unique_ptr MakeInputHandlers(); /** diff --git a/source/ps/Globals.cpp b/source/ps/Globals.cpp index ea6e432b8c..9c3244eb2c 100644 --- a/source/ps/Globals.cpp +++ b/source/ps/Globals.cpp @@ -25,6 +25,10 @@ #include #include #include +#include "network/NetClient.h" +#include "ps/GameSetup/Config.h" +#include "ps/Input.h" +#include "soundmanager/ISoundManager.h" bool g_app_minimized = false; bool g_app_has_focus = true; @@ -40,7 +44,7 @@ bool g_mouse_buttons[MOUSE_LAST - MOUSE_BASE] = {0}; PIFrequencyFilter g_frequencyFilter; // updates the state of the above; never swallows messages. -InReaction GlobalsInputHandler(const SDL_Event& ev) +Input::Reaction GlobalsInputHandler(const SDL_Event& ev) { size_t c; @@ -69,12 +73,12 @@ InReaction GlobalsInputHandler(const SDL_Event& ev) g_mouse_active = false; break; } - return IN_PASS; + return Input::Reaction::PASS; case SDL_MOUSEMOTION: g_mouse_x = ev.motion.x; g_mouse_y = ev.motion.y; - return IN_PASS; + return Input::Reaction::PASS; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: @@ -86,15 +90,15 @@ InReaction GlobalsInputHandler(const SDL_Event& ev) // don't complain: just ignore people with too many mouse buttons //debug_warn(L"invalid mouse button"); } - return IN_PASS; + return Input::Reaction::PASS; case SDL_KEYDOWN: case SDL_KEYUP: g_scancodes[ev.key.keysym.scancode] = (ev.type == SDL_KEYDOWN); - return IN_PASS; + return Input::Reaction::PASS; default: - return IN_PASS; + return Input::Reaction::PASS; } UNREACHABLE; diff --git a/source/ps/Globals.h b/source/ps/Globals.h index 86764b5fb8..c5bf981706 100644 --- a/source/ps/Globals.h +++ b/source/ps/Globals.h @@ -19,14 +19,12 @@ #define INCLUDED_PS_GLOBALS #include "lib/frequency_filter.h" -#include "lib/input.h" +#include "ps/Input.h" #include "ps/KeyName.h" #include #include -union SDL_Event; - // thin abstraction layer on top of SDL. // game code should use it instead of SDL_GetMouseState etc. because // Atlas does not completely emulate SDL (it can only send events). @@ -59,7 +57,7 @@ extern std::unordered_map g_scancodes; */ extern bool g_mouse_buttons[MOUSE_LAST - MOUSE_BASE]; -extern InReaction GlobalsInputHandler(const SDL_Event& ev); +extern Input::Reaction GlobalsInputHandler(const SDL_Event& ev); extern PIFrequencyFilter g_frequencyFilter; diff --git a/source/ps/Hotkey.cpp b/source/ps/Hotkey.cpp index 41062a5785..3062a8dd8d 100644 --- a/source/ps/Hotkey.cpp +++ b/source/ps/Hotkey.cpp @@ -175,16 +175,16 @@ bool isPressed(const SKey& key) return false; } -InReaction HotkeyStateChange(const SDL_Event& ev) +Input::Reaction HotkeyStateChange(const SDL_Event& ev) { if (ev.type == SDL_HOTKEYPRESS || ev.type == SDL_HOTKEYPRESS_SILENT) g_HotkeyStatus[static_cast(ev.user.data1)] = true; else if (ev.type == SDL_HOTKEYUP || ev.type == SDL_HOTKEYUP_SILENT) g_HotkeyStatus[static_cast(ev.user.data1)] = false; - return IN_PASS; + return Input::Reaction::PASS; } -InReaction HotkeyInputPrepHandler(const SDL_Event& ev) +Input::Reaction HotkeyInputPrepHandler(const SDL_Event& ev) { int scancode = SDL_SCANCODE_UNKNOWN; @@ -230,11 +230,11 @@ InReaction HotkeyInputPrepHandler(const SDL_Event& ev) scancode = MOUSE_X1; break; } - return IN_PASS; + return Input::Reaction::PASS; default: - return IN_PASS; + return Input::Reaction::PASS; } // Somewhat hackish: @@ -273,7 +273,7 @@ InReaction HotkeyInputPrepHandler(const SDL_Event& ev) // Check whether we have any hotkeys registered that include this scancode. if (g_HotkeyMap.find(scancode) == g_HotkeyMap.end()) - return IN_PASS; + return Input::Reaction::PASS; currentEvent = &ev; @@ -357,13 +357,13 @@ InReaction HotkeyInputPrepHandler(const SDL_Event& ev) } } - return IN_PASS; + return Input::Reaction::PASS; } -InReaction HotkeyInputActualHandler(const SDL_Event& ev) +Input::Reaction HotkeyInputActualHandler(const SDL_Event& ev) { if (!currentEvent) - return IN_PASS; + return Input::Reaction::PASS; bool isInstantaneous = ev.type == SDL_MOUSEWHEEL; @@ -461,7 +461,7 @@ InReaction HotkeyInputActualHandler(const SDL_Event& ev) g_VideoMode.m_InputManager.PushPriorityEvent(hotkeyNotification); } - return IN_PASS; + return Input::Reaction::PASS; } bool EventWillFireHotkey(const SDL_Event& ev, const CStr& keyname) diff --git a/source/ps/Hotkey.h b/source/ps/Hotkey.h index 4f54009aeb..40c24ae9e1 100644 --- a/source/ps/Hotkey.h +++ b/source/ps/Hotkey.h @@ -31,16 +31,14 @@ * triggered. All with the hotkey name stored in ev.user.data1 as a const char*. */ -#include "lib/input.h" #include "lib/types.h" #include "ps/CStr.h" #include "ps/containers/StaticVector.h" +#include "ps/Input.h" #include #include -union SDL_Event; - // SDL_Scancode is an enum, we'll use an explicit int to avoid including SDL in this header. using SDL_Scancode_ = int; @@ -85,17 +83,17 @@ extern void UnloadHotkeys(); /** * Updates g_HotkeyMap. */ -extern InReaction HotkeyStateChange(const SDL_Event& ev); +extern Input::Reaction HotkeyStateChange(const SDL_Event& ev); /** * Detects hotkeys that should fire. This allows using EventWillFireHotkey, * (and then possibly preventing those hotkeys from firing by handling the event). */ -extern InReaction HotkeyInputPrepHandler(const SDL_Event& ev); +extern Input::Reaction HotkeyInputPrepHandler(const SDL_Event& ev); /** * Actually fires hotkeys. */ -extern InReaction HotkeyInputActualHandler(const SDL_Event& ev); +extern Input::Reaction HotkeyInputActualHandler(const SDL_Event& ev); /** * @return whether the event @param ev will fire the hotkey @param keyname. diff --git a/source/ps/Input.cpp b/source/ps/Input.cpp index 9190b2f644..f2e28485d1 100644 --- a/source/ps/Input.cpp +++ b/source/ps/Input.cpp @@ -48,7 +48,7 @@ void Manager::DispatchEvent(const SDL_Event& event) // Looks like std::find_if, but std::find_if does not guarantee the order of the handlers. for (const auto handler : m_Handlers) { - if (handler && (*handler)(event) == IN_HANDLED) + if (handler && (*handler)(event) == Reaction::HANDLED) return; } } @@ -115,6 +115,7 @@ Manager::PollEventsResult Manager::PollEvents() return PollEventsResult{*m_PriorityEvents}; } + HandlerBase::HandlerBase(HandlerBase*& pos) noexcept : toReset{&pos} { diff --git a/source/ps/Input.h b/source/ps/Input.h index 7c0c2c4c64..4400be9f64 100644 --- a/source/ps/Input.h +++ b/source/ps/Input.h @@ -18,7 +18,7 @@ #ifndef INCLUDED_INPUT_HANDLER #define INCLUDED_INPUT_HANDLER -#include "lib/input.h" +#include "ps/Input.h" #include #include @@ -29,11 +29,21 @@ #include class ScriptRequest; +union SDL_Event; namespace Input { class HandlerBase; +enum class Reaction +{ + // Pass the event to the next handler in the chain. + PASS, + + // We handled it. No other handlers will receive this event. + HANDLED +}; + // A slot for each handler. Numbers are in invocation order. A handler can discard events. The first handler // is the only which gets all events. namespace Slot @@ -152,7 +162,7 @@ protected: HandlerBase& operator=(HandlerBase&&) = delete; public: - virtual InReaction operator()(const SDL_Event& event) = 0; + virtual Input::Reaction operator()(const SDL_Event& event) = 0; private: HandlerBase** toReset; @@ -175,7 +185,7 @@ public: ~Handler() final = default; private: - InReaction operator()(const SDL_Event& event) final + Input::Reaction operator()(const SDL_Event& event) final { return callback(event); } diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index b846367333..057d4eb4f3 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -271,7 +271,7 @@ void CProfileViewer::RenderProfile(CCanvas2D& canvas) // Handle input -InReaction CProfileViewer::Input(const SDL_Event& ev) +Input::Reaction CProfileViewer::Input(const SDL_Event& ev) { switch(ev.type) { @@ -284,7 +284,7 @@ InReaction CProfileViewer::Input(const SDL_Event& ev) if (k >= SDLK_0 && k <= SDLK_9) { m->NavigateTree(k - SDLK_0); - return IN_HANDLED; + return Input::Reaction::HANDLED; } break; } @@ -322,24 +322,24 @@ InReaction CProfileViewer::Input(const SDL_Event& ev) m->profileVisible = false; } } - return( IN_HANDLED ); + return Input::Reaction::HANDLED; } else if( hotkey == "profile.save" ) { SaveToFile(); - return( IN_HANDLED ); + return Input::Reaction::HANDLED; } break; } - return( IN_PASS ); + return Input::Reaction::PASS; } -InReaction CProfileViewer::InputThunk(const SDL_Event& ev) +Input::Reaction CProfileViewer::InputThunk(const SDL_Event& ev) { if (CProfileViewer::IsInitialised()) return g_ProfileViewer.Input(ev); - return IN_PASS; + return Input::Reaction::PASS; } diff --git a/source/ps/ProfileViewer.h b/source/ps/ProfileViewer.h index 67d98c7297..a29a881aa4 100644 --- a/source/ps/ProfileViewer.h +++ b/source/ps/ProfileViewer.h @@ -22,15 +22,14 @@ #ifndef INCLUDED_PROFILE_VIEWER #define INCLUDED_PROFILE_VIEWER -#include "lib/input.h" #include "ps/CStr.h" +#include "ps/Input.h" #include "ps/Singleton.h" #include #include class CCanvas2D; -union SDL_Event; /** * Struct ProfileColumn: Describes one column of an AbstractProfileTable. @@ -156,7 +155,7 @@ public: * @return IN_PASS or IN_HANDLED depending on whether the event relates * to the profiling display. */ - InReaction Input(const SDL_Event& ev); + Input::Reaction Input(const SDL_Event& ev); /** * AddRootTable: Add a new profile table as a root table (i.e. the @@ -178,7 +177,7 @@ public: * This allows our input handler to be installed via in_add_handler * like a normal, global function input handler. */ - static InReaction InputThunk(const SDL_Event& ev); + static Input::Reaction InputThunk(const SDL_Event& ev); /** * SaveToFile: Save the current profiler data (for all profile tables) diff --git a/source/ps/TouchInput.cpp b/source/ps/TouchInput.cpp index b79cf4d1c1..77982c62ee 100644 --- a/source/ps/TouchInput.cpp +++ b/source/ps/TouchInput.cpp @@ -187,10 +187,10 @@ void CTouchInput::Frame() } } -InReaction CTouchInput::HandleEvent([[maybe_unused]] const SDL_Event& ev) +Input::Reaction CTouchInput::HandleEvent(const SDL_Event& ev) { if (!IsEnabled()) - return IN_PASS; + return Input::Reaction::PASS; #if EMULATE_FINGERS_WITH_MOUSE switch(ev.type) @@ -282,16 +282,16 @@ InReaction CTouchInput::HandleEvent([[maybe_unused]] const SDL_Event& ev) OnFingerUp(ev.tfinger.fingerId, g_xres * ev.tfinger.x, g_yres * ev.tfinger.y); else if (ev.type == SDL_FINGERMOTION) OnFingerMotion(ev.tfinger.fingerId, g_xres * ev.tfinger.x, g_yres * ev.tfinger.y); - return IN_HANDLED; + return Input::Reaction::HANDLED; } } - return IN_PASS; + return Input::Reaction::PASS; } CTouchInput g_TouchInput; -InReaction touch_input_handler(const SDL_Event& ev) +Input::Reaction touch_input_handler(const SDL_Event& ev) { return g_TouchInput.HandleEvent(ev); } diff --git a/source/ps/TouchInput.h b/source/ps/TouchInput.h index 50adb45216..f8e2a359e4 100644 --- a/source/ps/TouchInput.h +++ b/source/ps/TouchInput.h @@ -18,14 +18,12 @@ #ifndef INCLUDED_TOUCHINPUT #define INCLUDED_TOUCHINPUT -#include "lib/input.h" #include "maths/Vector2D.h" #include "maths/Vector3D.h" +#include "ps/Input.h" #include -union SDL_Event; - /** * Maps touch events (e.g. on Android touchscreen devices) onto mouse events * and camera movement. @@ -41,7 +39,7 @@ public: */ bool IsEnabled(); - InReaction HandleEvent(const SDL_Event& ev); + Input::Reaction HandleEvent(const SDL_Event& ev); /** * Should be called once per frame to perform updates. @@ -90,6 +88,6 @@ private: extern CTouchInput g_TouchInput; -extern InReaction touch_input_handler(const SDL_Event& ev); +extern Input::Reaction touch_input_handler(const SDL_Event& ev); #endif // INCLUDED_TOUCHINPUT diff --git a/source/ps/VideoMode.h b/source/ps/VideoMode.h index 6211a82194..272c471cc3 100644 --- a/source/ps/VideoMode.h +++ b/source/ps/VideoMode.h @@ -18,6 +18,7 @@ #ifndef INCLUDED_VIDEOMODE #define INCLUDED_VIDEOMODE +#include "ps/CStrForward.h" #include "ps/Input.h" #include "renderer/backend/Backend.h" diff --git a/source/ps/tests/test_Hotkeys.h b/source/ps/tests/test_Hotkeys.h index ed42d622e4..32784a84cf 100644 --- a/source/ps/tests/test_Hotkeys.h +++ b/source/ps/tests/test_Hotkeys.h @@ -25,7 +25,6 @@ #include "lib/external_libraries/libsdl.h" #include "lib/file/file_system.h" #include "lib/file/vfs/vfs.h" -#include "lib/input.h" #include "lib/path.h" #include "ps/CStr.h" #include "ps/ConfigDB.h" diff --git a/source/ps/tests/test_Input.h b/source/ps/tests/test_Input.h index 1c9e430336..a4fd35aba0 100644 --- a/source/ps/tests/test_Input.h +++ b/source/ps/tests/test_Input.h @@ -48,8 +48,7 @@ class TestInput : public CxxTest::TestSuite static void PushPriorityEvent(Input::Manager& manager, const std::uint32_t eventType) { - const SDL_Event ev{MakeEvent(eventType)}; - manager.PushPriorityEvent(ev); + manager.PushPriorityEvent(MakeEvent(eventType)); } public: @@ -116,12 +115,11 @@ public: bool triggered{false}; Input::Handler _{manager, std::integral_constant{}, [&](const SDL_Event&){ triggered = true; - return IN_HANDLED; + return Input::Reaction::HANDLED; }}; TS_ASSERT(!triggered); - SDL_Event ev{MakeEvent(GetEventType(1))}; - manager.DispatchEvent(ev); + manager.DispatchEvent(MakeEvent(GetEventType(1))); TS_ASSERT(triggered); } @@ -132,21 +130,20 @@ public: const std::uint32_t filteredEventType{eventTypeStart + 1}; [[maybe_unused]] Input::Handler filter{manager, std::integral_constant{}, [&](const SDL_Event& ev){ - return ev.type == filteredEventType ? IN_HANDLED : IN_PASS; + return ev.type == filteredEventType ? Input::Reaction::HANDLED : + Input::Reaction::PASS; }}; bool triggered{false}; [[maybe_unused]] Input::Handler test{manager, std::integral_constant{}, [&](const SDL_Event&){ triggered = true; - return IN_HANDLED; + return Input::Reaction::HANDLED; }}; - SDL_Event ev0{MakeEvent(filteredEventType)}; - manager.DispatchEvent(ev0); + manager.DispatchEvent(MakeEvent(filteredEventType)); TS_ASSERT(!triggered); - SDL_Event ev1{MakeEvent(eventTypeStart)}; - manager.DispatchEvent(ev1); + manager.DispatchEvent(MakeEvent(eventTypeStart)); TS_ASSERT(triggered); } @@ -157,12 +154,11 @@ public: { Input::Handler _{manager, std::integral_constant{}, [&](const SDL_Event&){ triggered = true; - return IN_HANDLED; + return Input::Reaction::HANDLED; }}; } - SDL_Event ev{MakeEvent(GetEventType(1))}; - manager.DispatchEvent(ev); + manager.DispatchEvent(MakeEvent(GetEventType(1))); TS_ASSERT(!triggered); } }; diff --git a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp index ff9bd43d6f..02e3683708 100644 --- a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp @@ -21,6 +21,7 @@ #include "gui/GUIManager.h" #include "lib/debug.h" +#include "lib/external_libraries/libsdl.h" #include "lib/input.h" #include "lib/sysdep/os.h" #include "lib/timer.h" @@ -146,6 +147,7 @@ MESSAGEHANDLER(Shutdown) g_AtlasGameLoop->view = AtlasView::GetView_None(); g_InputHandlers.reset(); ShutdownNetworkAndUI(); + g_InputHandlers.reset(); g_ScriptInterface.reset(); ShutdownConfigAndSubsequent(); g_FileLogger.reset(); diff --git a/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp index 047e320a03..16ed1e1c79 100644 --- a/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp @@ -20,12 +20,15 @@ #include "MessageHandler.h" #include "gui/CGUI.h" #include "gui/GUIManager.h" +#include "lib/external_libraries/libsdl.h" #include "lib/input.h" #include "lib/path.h" #include "lib/types.h" #include "maths/MathUtil.h" #include "ps/Game.h" #include "ps/GameSetup/Config.h" +#include "ps/GameSetup/GameSetup.h" +#include "ps/Input.h" #include "ps/VideoMode.h" #include "renderer/Renderer.h" #include "scriptinterface/ScriptInterface.h"