Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_INPUT_HANDLER 19 : #define INCLUDED_INPUT_HANDLER 20 : 21 : #include "ps/Input.h" 22 : 23 : #include <array> 24 : #include <concepts> 25 : #include <cstddef> 26 : #include <queue> 27 : #include <memory> 28 : #include <optional> 29 : #include <type_traits> 30 : 31 : class ScriptRequest; 32 : union SDL_Event; 33 : 34 : namespace Input 35 : { 36 : class HandlerBase; 37 : 38 : enum class Reaction 39 : { 40 : // Pass the event to the next handler in the chain. 41 : PASS, 42 : 43 : // We handled it. No other handlers will receive this event. 44 : HANDLED 45 : }; 46 : 47 : // A slot for each handler. Numbers are in invocation order. A handler can discard events. The first handler 48 : // is the only which gets all events. 49 : namespace Slot 50 : { 51 : constexpr std::integral_constant<std::size_t, 0> PRIMARY; 52 : constexpr std::integral_constant<std::size_t, 1> WINDOW; 53 : constexpr std::integral_constant<std::size_t, 2> SCREENSHOT; 54 : 55 : // These two must be called first `globalsInput` deals with some important global state, such as which 56 : // scancodes are being pressed, mouse buttons pressed, etc. while hotkeyStateChange updates the map of 57 : // active hotkeys. 58 : constexpr std::integral_constant<std::size_t, 3> HOTKEY_STATE_CHANGE; 59 : constexpr std::integral_constant<std::size_t, 4> GLOBAL; 60 : 61 : // Should be called after scancode map update (i.e. after the global input, but before UI). This never 62 : // blocks the event, but it does some processing necessary for hotkeys, which are triggered later down the 63 : // input chain. (by calling this before the UI, we can use `EventWouldTriggerHotkey` in the UI). 64 : constexpr std::integral_constant<std::size_t, 5> HOTKEY_INPUT_PREPARATION; 65 : 66 : constexpr std::integral_constant<std::size_t, 6> TOUCH_INPUT; 67 : 68 : // The console handler needs to be called before the hotkey handler so that text can be typed in without 69 : // setting off hotkeys. 70 : constexpr std::integral_constant<std::size_t, 7> CONSOLE; 71 : // Likewise for gui. 72 : constexpr std::integral_constant<std::size_t, 8> GUI; 73 : constexpr std::integral_constant<std::size_t, 9> HOTKEY_INPUT; 74 : constexpr std::integral_constant<std::size_t, 10> PROFILE_VIEWER; 75 : constexpr std::integral_constant<std::size_t, 11> GAME_VIEW; 76 : } 77 : 78 : /** 79 : * Holds a pointer to all registered `HandlerBase`s. 80 : * It does unregister all registered `Handler`s in the destructor. 81 : */ 82 : class Manager 83 : { 84 : public: 85 : Manager(); 86 : // The `Manager` needs to have a constant memory location (the `Handler`s hold a pointer to it). 87 : Manager(Manager&) = delete; 88 : Manager& operator=(Manager&) = delete; 89 : Manager(Manager&&) = delete; 90 : Manager& operator=(Manager&&) = delete; 91 : ~Manager(); 92 : 93 : void PushPriorityEvent(const SDL_Event& event); 94 : void DispatchEvent(const SDL_Event& event); 95 : 96 : template<std::size_t slot> 97 41 : HandlerBase*& Get() noexcept 98 : { 99 41 : return std::get<slot>(m_Handlers); 100 : } 101 : 102 88 : class PollEventsResult 103 : { 104 : public: 105 : class EventIterator 106 : { 107 : public: 108 : using difference_type = std::ptrdiff_t; 109 : using value_type = SDL_Event; 110 : using pointer = value_type*; 111 : using reference = value_type&; 112 : using iterator_category = std::input_iterator_tag; 113 : 114 47 : EventIterator() = default; 115 : explicit EventIterator(PollEventsResult& range); 116 : 117 : reference operator*(); 118 : pointer operator->(); 119 : 120 : EventIterator& operator++(); 121 : EventIterator& operator++(int); 122 : 123 : bool operator==(const EventIterator& other) const; 124 : 125 : private: 126 : PollEventsResult* m_Storage{nullptr}; 127 : }; 128 : 129 : explicit PollEventsResult(std::queue<SDL_Event>& priorityEvents); 130 : 131 : EventIterator begin(); 132 : EventIterator end(); 133 : 134 : private: 135 : std::queue<SDL_Event>& m_PriorityEvents; 136 : const std::unique_ptr<SDL_Event> m_Event; 137 : }; 138 : 139 : PollEventsResult PollEvents(); 140 : 141 : private: 142 : std::array<HandlerBase*, 12> m_Handlers{{}}; 143 : const std::unique_ptr<std::queue<SDL_Event>> m_PriorityEvents; 144 : }; 145 : 146 : /** 147 : * Type-erased callable 148 : */ 149 : class HandlerBase 150 : { 151 : protected: 152 : /** 153 : * Can't not be constructed themself, use `Handler` instead. 154 : * @param pos On construction the pointer is set to @c this. On 155 : * destruction the pointer will be set to nullptr. 156 : */ 157 : explicit HandlerBase(HandlerBase*& pos) noexcept; 158 : virtual ~HandlerBase(); 159 : 160 : // A `HandlerBase` needs to have a constant memory location (the `Manager` does hold a pointer to it). 161 : HandlerBase(HandlerBase&) = delete; 162 : HandlerBase& operator=(HandlerBase&) = delete; 163 : HandlerBase(HandlerBase&&) = delete; 164 : HandlerBase& operator=(HandlerBase&&) = delete; 165 : 166 : public: 167 : virtual Input::Reaction operator()(const SDL_Event& event) = 0; 168 : 169 : private: 170 : // When the handler is destructed this position han to be changed to 171 : // `nullptr`, so that it's not invoked again. 172 : HandlerBase*& m_ToReset; 173 : }; 174 : 175 : /** 176 : * Usable type to register a handler to the associated `Manager`. 177 : */ 178 : template<std::invocable<const SDL_Event&> Callback> 179 : class Handler final : private HandlerBase 180 : { 181 : public: 182 : // `slot` specifies when(in which order) the `callback` of this `Handler` is executed. 183 : template<std::size_t slot> 184 38 : explicit Handler(Manager& manager, std::integral_constant<std::size_t, slot>, Callback func) : 185 : HandlerBase{manager.Get<slot>()}, 186 40 : m_Callback{std::move(func)} 187 : { 188 : } 189 19 : ~Handler() final = default; 190 : 191 : private: 192 53 : Input::Reaction operator()(const SDL_Event& event) final 193 : { 194 53 : return m_Callback(event); 195 : } 196 : 197 : Callback m_Callback; 198 : }; 199 : } 200 : 201 : #endif // INCLUDED_INPUT_HANDLER