LCOV - code coverage report
Current view: top level - source/ps/scripting - JSInterface_Hotkey.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 7 79 8.9 %
Date: 2026-12-03 01:19:39 Functions: 1 9 11.1 %

          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 "JSInterface_Hotkey.h"
      21             : 
      22             : #include "ps/CLogger.h"
      23             : #include "ps/CStr.h"
      24             : #include "ps/ConfigDB.h"
      25             : #include "ps/Hotkey.h"
      26             : #include "ps/KeyName.h"
      27             : #include "ps/containers/StaticVector.h"
      28             : #include "scriptinterface/FunctionWrapper.h"
      29             : #include "scriptinterface/Conversions.h"
      30             : #include "scriptinterface/Request.h"
      31             : 
      32             : #include <SDL_scancode.h>
      33             : #include <js/PropertyAndElement.h>
      34             : #include <js/RootingAPI.h>
      35             : #include <js/TypeDecls.h>
      36             : #include <js/Value.h>
      37             : #include <jsapi.h>
      38             : #include <set>
      39             : #include <string>
      40             : #include <unordered_map>
      41             : #include <utility>
      42             : #include <vector>
      43             : 
      44             : /**
      45             :  * Convert an unordered map to a JS object, mapping keys to values.
      46             :  * Assumes T to have a c_str() method that returns a const char*
      47             :  * NB: this is unordered since no particular effort is made to preserve order.
      48             :  * TODO: this could be moved to ScriptConversions.cpp if the need arises.
      49             :  */
      50             : template<typename T, typename U>
      51           0 : static void ToJSVal_unordered_map(const Script::Request& rq, JS::MutableHandleValue ret, const std::unordered_map<T, U>& val)
      52             : {
      53           0 :     JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
      54           0 :     if (!obj)
      55             :     {
      56           0 :         ret.setUndefined();
      57           0 :         return;
      58             :     }
      59           0 :     for (const std::pair<const T, U>& item : val)
      60             :     {
      61           0 :         JS::RootedValue el(rq.cx);
      62           0 :         Script::ToJSVal<U>(rq, &el, item.second);
      63           0 :         JS_SetProperty(rq.cx, obj, item.first.c_str(), el);
      64             :     }
      65           0 :     ret.setObject(*obj);
      66           0 : }
      67             : 
      68             : template<>
      69           0 : void Script::ToJSVal<std::unordered_map<std::string, std::vector<std::vector<std::string>>>>(const Script::Request& rq, JS::MutableHandleValue ret, const std::unordered_map<std::string, std::vector<std::vector<std::string>>>& val)
      70             : {
      71           0 :     ToJSVal_unordered_map(rq, ret, val);
      72           0 : }
      73             : 
      74             : template<>
      75           0 : void Script::ToJSVal<std::unordered_map<std::string, std::string>>(const Script::Request& rq, JS::MutableHandleValue ret, const std::unordered_map<std::string, std::string>& val)
      76             : {
      77           0 :     ToJSVal_unordered_map(rq, ret, val);
      78           0 : }
      79             : 
      80             : namespace
      81             : {
      82             : /**
      83             :  * @return a (js) object mapping hotkey name (from cfg files) to a list ofscancode names
      84             :  */
      85           0 : JS::Value GetHotkeyMap(const Script::Request& rq)
      86             : {
      87           0 :     JS::RootedValue hotkeyMap(rq.cx);
      88             : 
      89           0 :     std::unordered_map<std::string, std::vector<std::vector<std::string>>> hotkeys;
      90           0 :     for (const std::pair<const SDL_Scancode_, KeyMapping>& key : g_HotkeyMap)
      91           0 :         for (const SHotkeyMapping& mapping : key.second)
      92             :         {
      93           0 :             std::vector<std::string> keymap;
      94           0 :             if (key.first != UNUSED_HOTKEY_CODE)
      95           0 :                 keymap.push_back(FindScancodeName(static_cast<SDL_Scancode>(key.first)));
      96           0 :             for (const SKey& secondary_key : mapping.required)
      97           0 :                 keymap.push_back(FindScancodeName(static_cast<SDL_Scancode>(secondary_key.code)));
      98             :             // If keymap is empty (== unused) or size 1, push the combination.
      99             :             // Otherwise, all permutations of the combination will exist, so pick one using an arbitrary order.
     100           0 :             if (keymap.size() < 2 || keymap[0] < keymap[1])
     101           0 :                 hotkeys[mapping.name].emplace_back(keymap);
     102           0 :         }
     103           0 :     Script::ToJSVal(rq, &hotkeyMap, hotkeys);
     104             : 
     105           0 :     return hotkeyMap;
     106           0 : }
     107             : 
     108             : /**
     109             :  * @return a (js) object mapping scancode names to their locale-dependent name.
     110             :  */
     111           0 : JS::Value GetScancodeKeyNames(const Script::Request& rq)
     112             : {
     113           0 :     JS::RootedValue obj(rq.cx);
     114           0 :     std::unordered_map<std::string, std::string> map;
     115             : 
     116             :     // Get the name of all scancodes.
     117             :     // This is slightly wasteful but should be fine overall, they are dense.
     118           0 :     for (int i = 0; i < MOUSE_LAST; ++i)
     119           0 :         map[FindScancodeName(static_cast<SDL_Scancode>(i))] = FindKeyName(static_cast<SDL_Scancode>(i));
     120           0 :     Script::ToJSVal(rq, &obj, map);
     121             : 
     122           0 :     return obj;
     123           0 : }
     124             : 
     125           0 : void ReloadHotkeys()
     126             : {
     127           0 :     UnloadHotkeys();
     128           0 :     LoadHotkeys(g_ConfigDB);
     129           0 : }
     130             : 
     131           0 : JS::Value GetConflicts(const Script::Request& rq, JS::HandleValue combination)
     132             : {
     133           0 :     std::vector<std::string> keys;
     134           0 :     if (!Script::FromJSVal(rq, combination, keys))
     135             :     {
     136           0 :         LOGERROR("Invalid hotkey combination");
     137           0 :         return JS::NullValue();
     138             :     }
     139             : 
     140           0 :     if (keys.empty())
     141           0 :         return JS::NullValue();
     142             : 
     143             :     // Pick a random code as a starting point of the hotkeys (they are all equivalent).
     144           0 :     SDL_Scancode_ startCode = FindScancode(keys.back());
     145             : 
     146           0 :     std::unordered_map<SDL_Scancode_, KeyMapping>::const_iterator it = g_HotkeyMap.find(startCode);
     147           0 :     if (it == g_HotkeyMap.end())
     148           0 :         return JS::NullValue();
     149             : 
     150             :     // Create a sorted vector with the remaining keys.
     151           0 :     keys.pop_back();
     152             : 
     153           0 :     std::set<SKey> codes;
     154           0 :     for (const std::string& key : keys)
     155           0 :         codes.insert(SKey{ FindScancode(key) });
     156             : 
     157           0 :     std::vector<CStr> conflicts;
     158             :     // This isn't very efficient, but we shouldn't iterate too many hotkeys
     159             :     // since we at least have one matching key.
     160           0 :     for (const SHotkeyMapping& keymap : it->second)
     161             :     {
     162           0 :         std::set<SKey> match(keymap.required.begin(), keymap.required.end());
     163           0 :         if (codes == match)
     164           0 :             conflicts.emplace_back(keymap.name);
     165           0 :     }
     166           0 :     if (conflicts.empty())
     167           0 :         return JS::NullValue();
     168             : 
     169           0 :     JS::RootedValue ret(rq.cx);
     170           0 :     Script::ToJSVal(rq, &ret, conflicts);
     171           0 :     return ret;
     172           0 : }
     173             : }
     174             : 
     175          26 : void JSI_Hotkey::RegisterScriptFunctions(const Script::Request& rq)
     176             : {
     177          32 :     Script::Function::Register<&HotkeyIsPressed>(rq, "HotkeyIsPressed");
     178          26 :     Script::Function::Register<&GetHotkeyMap>(rq, "GetHotkeyMap");
     179          26 :     Script::Function::Register<&GetScancodeKeyNames>(rq, "GetScancodeKeyNames");
     180          26 :     Script::Function::Register<&ReloadHotkeys>(rq, "ReloadHotkeys");
     181          26 :     Script::Function::Register<&GetConflicts>(rq, "GetConflicts");
     182          26 : }

Generated by: LCOV version 1.16