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_HOTKEY 19 : #define INCLUDED_HOTKEY 20 : 21 : /** 22 : * @file 23 : * Hotkey system. 24 : * 25 : * Hotkeys consist of a name (an arbitrary string), and a key mapping. 26 : * The names and mappings are loaded from the config system (any 27 : * config setting with the name prefix "hotkey."). 28 : * When a hotkey is pressed one SDL_HOTKEYPRESS is triggered. While the key is 29 : * kept down repeated SDL_HOTKEYDOWN events are triggered at an interval 30 : * determined by the OS. When a hotkey is released an SDL_HOTKEYUP event is 31 : * triggered. All with the hotkey name stored in ev.user.data1 as a const char*. 32 : */ 33 : 34 : #include "ps/CStr.h" 35 : #include "ps/containers/StaticVector.h" 36 : #include "ps/Input.h" 37 : 38 : #include <unordered_map> 39 : #include <vector> 40 : 41 : // SDL_Scancode is an enum, we'll use an explicit int to avoid including SDL in this header. 42 : using SDL_Scancode_ = int; 43 : 44 : // 0x8000 is SDL_USEREVENT, this is static_asserted in Hotkey.cpp 45 : // We do this to avoid including SDL in this header. 46 : const unsigned int SDL_USEREVENT_ = 0x8000; 47 : const unsigned int SDL_HOTKEYPRESS = SDL_USEREVENT_; 48 : const unsigned int SDL_HOTKEYDOWN = SDL_USEREVENT_ + 1; 49 : const unsigned int SDL_HOTKEYUP = SDL_USEREVENT_ + 2; 50 : const unsigned int SDL_HOTKEYPRESS_SILENT = SDL_USEREVENT_ + 3; 51 : const unsigned int SDL_HOTKEYUP_SILENT = SDL_USEREVENT_ + 4; 52 : 53 : // Value not mapping to any valid SDL_SCANCODE_* 54 : constexpr SDL_Scancode_ UNUSED_HOTKEY_CODE{-1}; 55 : 56 : struct SKey 57 : { 58 : SDL_Scancode_ code; // scancode or MOUSE_ or UNIFIED_ value 59 0 : bool operator<(const SKey& o) const { return code < o.code; } 60 0 : bool operator==(const SKey& o) const { return code == o.code; } 61 : }; 62 : 63 : // Hotkey data associated with an externally-specified 'primary' keycode 64 149 : struct SHotkeyMapping 65 : { 66 : CStr name; // name of the hotkey 67 : SKey primary; // the primary key 68 : PS::StaticVector<SKey, 8> required; // list of non-primary keys that must also be active 69 : }; 70 : 71 : typedef std::vector<SHotkeyMapping> KeyMapping; 72 : 73 : // A mapping of scancodes onto the hotkeys that are associated with that key. 74 : // (A hotkey triggered by a combination of multiple keys will be in this map 75 : // multiple times.) 76 : extern std::unordered_map<SDL_Scancode_, KeyMapping> g_HotkeyMap; 77 : 78 : class CConfigDB; 79 : extern void LoadHotkeys(CConfigDB& configDB); 80 : extern void UnloadHotkeys(); 81 : 82 : /** 83 : * Updates g_HotkeyMap. 84 : */ 85 : extern Input::Reaction HotkeyStateChange(const SDL_Event& ev); 86 : 87 : /** 88 : * Detects hotkeys that should fire. This allows using EventWillFireHotkey, 89 : * (and then possibly preventing those hotkeys from firing by handling the event). 90 : */ 91 : extern Input::Reaction HotkeyInputPrepHandler(const SDL_Event& ev); 92 : /** 93 : * Actually fires hotkeys. 94 : */ 95 : extern Input::Reaction HotkeyInputActualHandler(const SDL_Event& ev); 96 : 97 : /** 98 : * @return whether the event @param ev will fire the hotkey @param keyname. 99 : */ 100 : extern bool EventWillFireHotkey(const SDL_Event& ev, const CStr& keyname); 101 : 102 : /** 103 : * Resets all currently active hotkeys (and clears in-flight hotkeys). 104 : * You should call this when something grabs key input, e.g. an input box, 105 : * as those prevent keydown/keyup messages from reaching the hotkey system, 106 : * and can lead to hotkeys being stuck active. 107 : * NB: active hotkeys are released immediately and "HotkeyUp" message sent. 108 : */ 109 : extern void ResetActiveHotkeys(); 110 : 111 : /** 112 : * @return whether the hotkey is currently pressed (i.e. active). 113 : */ 114 : extern bool HotkeyIsPressed(const CStr& keyname); 115 : 116 : #endif // INCLUDED_HOTKEY