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 : #include "precompiled.h" 19 : 20 : #include "CHotkeyPicker.h" 21 : 22 : #include "gui/CGUI.h" 23 : #include "gui/ObjectBases/IGUIObject.h" 24 : #include "gui/SGUIMessage.h" 25 : #include "lib/external_libraries/libsdl.h" 26 : #include "lib/timer.h" 27 : #include "ps/CLogger.h" 28 : #include "ps/KeyName.h" 29 : #include "scriptinterface/Conversions.h" 30 : #include "scriptinterface/Request.h" 31 : 32 : #include <SDL_events.h> 33 : #include <SDL_mouse.h> 34 : #include <algorithm> 35 : #include <js/RootingAPI.h> 36 : #include <js/TypeDecls.h> 37 : #include <js/Value.h> 38 : #include <js/ValueArray.h> 39 : #include <memory> 40 : 41 : 42 : const CStr CHotkeyPicker::EventNameCombination = "Combination"; 43 : const CStr CHotkeyPicker::EventNameKeyChange = "KeyChange"; 44 : 45 : // Don't send the scancode, JS doesn't care. 46 0 : template<> void Script::ToJSVal(const Script::Request& rq, JS::MutableHandleValue ret, const CHotkeyPicker::Key& val) 47 : { 48 0 : Script::ToJSVal(rq, ret, val.scancodeName); 49 0 : } 50 : 51 : // Unused, but JSVAL_VECTOR requires it. 52 0 : template<> bool Script::FromJSVal(const Script::Request&, const JS::HandleValue, CHotkeyPicker::Key&) 53 : { 54 0 : LOGWARNING("FromJSVal<CHotkeyPicker>: Not implemented"); 55 0 : return false; 56 : } 57 : 58 0 : JSVAL_VECTOR(CHotkeyPicker::Key); 59 : 60 0 : CHotkeyPicker::CHotkeyPicker(CGUI& pGUI) : IGUIObject(pGUI), m_TimeToCombination(this, "time_to_combination", 1.f) 61 : { 62 : // 8 keys at the same time is probably more than we'll ever need. 63 0 : m_KeysPressed.reserve(8); 64 0 : } 65 : 66 0 : void CHotkeyPicker::FireEvent(const CStr& event) 67 : { 68 0 : Script::Request rq(*m_pGUI.GetScriptInterface()); 69 : 70 0 : JS::RootedValueArray<1> args(rq.cx); 71 0 : JS::RootedValue keys(rq.cx); 72 0 : Script::ToJSVal(rq, &keys, m_KeysPressed); 73 0 : args[0].set(keys); 74 0 : ScriptEvent(event, args); 75 0 : } 76 : 77 0 : void CHotkeyPicker::Tick() 78 : { 79 0 : if (m_KeysPressed.size() == 0) 80 : return; 81 : 82 0 : double time = timer_Time(); 83 0 : if (time - m_LastKeyChange < m_TimeToCombination) 84 : return; 85 : 86 0 : FireEvent(EventNameCombination); 87 : 88 0 : return; 89 : } 90 : 91 0 : void CHotkeyPicker::HandleMessage(SGUIMessage& Message) 92 : { 93 0 : IGUIObject::HandleMessage(Message); 94 0 : switch (Message.type) 95 : { 96 0 : case GUIM_GOT_FOCUS: 97 0 : case GUIM_LOST_FOCUS: 98 0 : { 99 0 : m_KeysPressed.clear(); 100 0 : m_LastKeyChange = timer_Time(); 101 0 : break; 102 : } 103 : default: 104 : break; 105 : } 106 0 : } 107 : 108 0 : Input::Reaction CHotkeyPicker::PreemptEvent(const SDL_Event& ev) 109 : { 110 0 : switch (ev.type) 111 : { 112 : // Handle the same mouse events that hotkeys handle 113 0 : case SDL_MOUSEBUTTONDOWN: 114 0 : case SDL_MOUSEBUTTONUP: 115 0 : case SDL_MOUSEWHEEL: 116 0 : { 117 0 : SDL_Scancode scancode; 118 : 119 0 : if (ev.type != SDL_MOUSEWHEEL) 120 : { 121 : // Wait a little bit -> this gets triggered when clicking on a button, 122 : // but after the button click is processed, thus immediately triggering... 123 0 : if (timer_Time()-m_LastKeyChange < 0.2) 124 : return Input::Reaction::HANDLED; 125 : // This is from hotkeyHandler - not sure what it does in all honesty. 126 0 : if(ev.button.button >= SDL_BUTTON_X1) 127 0 : scancode = static_cast<SDL_Scancode>(MOUSE_BASE + static_cast<int>(ev.button.button) + 2); 128 : else 129 0 : scancode = static_cast<SDL_Scancode>(MOUSE_BASE + static_cast<int>(ev.button.button)); 130 : } 131 : else 132 : { 133 0 : if (ev.wheel.y > 0) 134 : scancode = static_cast<SDL_Scancode>(MOUSE_WHEELUP); 135 0 : else if (ev.wheel.y < 0) 136 : scancode = static_cast<SDL_Scancode>(MOUSE_WHEELDOWN); 137 0 : else if (ev.wheel.x > 0) 138 : scancode = static_cast<SDL_Scancode>(MOUSE_X2); 139 0 : else if (ev.wheel.x < 0) 140 : scancode = static_cast<SDL_Scancode>(MOUSE_X1); 141 : else 142 : return Input::Reaction::HANDLED; 143 : } 144 : // Don't handle keys and mouse together except for modifiers. 145 0 : m_KeysPressed.erase(std::remove_if(m_KeysPressed.begin(), m_KeysPressed.end(), [](const Key& k) { 146 0 : return static_cast<int>(k.code) < UNIFIED_SHIFT || static_cast<int>(k.code) >= UNIFIED_LAST; } ), m_KeysPressed.end()); 147 0 : m_KeysPressed.emplace_back(Key{scancode, FindScancodeName(scancode)}); 148 : // For mouse events, assume we immediately want to return. 149 0 : FireEvent(EventNameCombination); 150 : 151 0 : return Input::Reaction::HANDLED; 152 : } 153 0 : case SDL_KEYDOWN: 154 0 : case SDL_KEYUP: 155 0 : { 156 0 : SDL_Scancode scancode = ev.key.keysym.scancode; 157 : 158 : // Don't handle caps-lock, it doesn't really work in-game and it's a weird hotkey. 159 0 : if (scancode == SDL_SCANCODE_CAPSLOCK) 160 : return Input::Reaction::PASS; 161 : 162 0 : if (scancode == SDL_SCANCODE_LSHIFT || scancode == SDL_SCANCODE_RSHIFT) 163 0 : scancode = static_cast<SDL_Scancode>(UNIFIED_SHIFT); 164 : else if (scancode == SDL_SCANCODE_LCTRL || scancode == SDL_SCANCODE_RCTRL) 165 0 : scancode = static_cast<SDL_Scancode>(UNIFIED_CTRL); 166 : else if (scancode == SDL_SCANCODE_LALT || scancode == SDL_SCANCODE_RALT) 167 0 : scancode = static_cast<SDL_Scancode>(UNIFIED_ALT); 168 : else if (scancode == SDL_SCANCODE_LGUI || scancode == SDL_SCANCODE_RGUI) 169 0 : scancode = static_cast<SDL_Scancode>(UNIFIED_SUPER); 170 : 171 0 : if (ev.type == SDL_KEYDOWN) 172 : { 173 0 : std::vector<Key>::const_iterator it = \ 174 0 : std::find_if(m_KeysPressed.begin(), m_KeysPressed.end(), [&scancode](Key& k) { return k.code == scancode; }); 175 : // Can happen if multiple keys are mapped the same. 176 0 : if (it != m_KeysPressed.end()) 177 0 : return Input::Reaction::HANDLED; 178 0 : m_KeysPressed.emplace_back(Key{scancode, FindScancodeName(scancode)}); 179 : } 180 : else 181 : { 182 0 : std::vector<Key>::const_iterator it = \ 183 0 : std::find_if(m_KeysPressed.begin(), m_KeysPressed.end(), [&scancode](Key& k) { return k.code == scancode; }); 184 : // Might happen if a key was down before this object is created. 185 0 : if (it == m_KeysPressed.end()) 186 0 : return Input::Reaction::HANDLED; 187 0 : m_KeysPressed.erase(it); 188 : } 189 : 190 0 : FireEvent(EventNameKeyChange); 191 : 192 : // Register after-JS in case this takes a while (probably not but it doesn't hurt). 193 0 : m_LastKeyChange = timer_Time(); 194 0 : return Input::Reaction::HANDLED; 195 : } 196 : default: 197 : { 198 : return Input::Reaction::PASS; 199 : } 200 : } 201 0 : }