diff --git a/src/common/JoyMap.cxx b/src/common/JoyMap.cxx index 266dd5e74..a49c1be4d 100644 --- a/src/common/JoyMap.cxx +++ b/src/common/JoyMap.cxx @@ -16,6 +16,9 @@ //============================================================================ #include "JoyMap.hxx" +#include "jsonDefinitions.hxx" + +using json = nlohmann::json; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void JoyMap::add(const Event::Type event, const JoyMapping& mapping) @@ -183,48 +186,26 @@ JoyMap::JoyMappingArray JoyMap::getEventMapping(const Event::Type event, const E } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -string JoyMap::saveMapping(const EventMode mode) const +json JoyMap::saveMapping(const EventMode mode) const { - using MapType = std::pair; - std::vector sortedMap(myMap.begin(), myMap.end()); + json eventMappings = json::array(); - std::sort(sortedMap.begin(), sortedMap.end(), - [](const MapType& a, const MapType& b) - { - // Event::Type first - if(a.second != b.second) - return a.second < b.second; + for (auto& item: myMap) { + if (item.first.mode != mode) continue; - if(a.first.button != b.first.button) - return a.first.button < b.first.button; + json eventMapping = json::object(); - if(a.first.axis != b.first.axis) - return a.first.axis < b.first.axis; + eventMapping["event"] = item.second; + eventMapping["button"] = item.first.button; + eventMapping["axis"] = item.first.axis; + eventMapping["axisDirection"] = item.first.adir; + eventMapping["hat"] = item.first.hat; + eventMapping["hatDirection"] = item.first.hdir; - if(a.first.adir != b.first.adir) - return a.first.adir < b.first.adir; - - if(a.first.hat != b.first.hat) - return a.first.hat < b.first.hat; - - return a.first.hdir < b.first.hdir; - } - ); - - ostringstream buf; - - for (auto item : sortedMap) - { - if (item.first.mode == mode) - { - if (buf.str() != "") - buf << "|"; - buf << item.second << ":" << item.first.button << "," - << int(item.first.axis) << "," << int(item.first.adir) << "," - << item.first.hat << "," << int(item.first.hdir); - } + eventMappings.push_back(eventMapping); } - return buf.str(); + + return eventMappings; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/JoyMap.hxx b/src/common/JoyMap.hxx index e3cc7ff7a..b480aad1b 100644 --- a/src/common/JoyMap.hxx +++ b/src/common/JoyMap.hxx @@ -22,6 +22,7 @@ #include "Event.hxx" #include "EventHandlerConstants.hxx" +#include "json.hxx" /** This class handles controller mappings in Stella. @@ -110,7 +111,7 @@ class JoyMap JoyMappingArray getEventMapping(const Event::Type event, const EventMode mode) const; - string saveMapping(const EventMode mode) const; + nlohmann::json saveMapping(const EventMode mode) const; int loadMapping(string& list, const EventMode mode); /** Erase all mappings for given mode */ diff --git a/src/common/PJoystickHandler.cxx b/src/common/PJoystickHandler.cxx index f9c616921..2deeaf111 100644 --- a/src/common/PJoystickHandler.cxx +++ b/src/common/PJoystickHandler.cxx @@ -29,6 +29,8 @@ static constexpr char CTRL_DELIM = '^'; +using json = nlohmann::json; + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - PhysicalJoystickHandler::PhysicalJoystickHandler( OSystem& system, EventHandler& handler) @@ -51,7 +53,8 @@ PhysicalJoystickHandler::PhysicalJoystickHandler( istringstream namebuf(joymap); getline(namebuf, joyname, PhysicalJoystick::MODE_DELIM); if(joyname.length() != 0) - myDatabase.emplace(joyname, StickInfo(joymap)); + // TODO: convert old mapping to json + myDatabase.emplace(joyname, StickInfo()); } } } @@ -526,15 +529,16 @@ void PhysicalJoystickHandler::saveMapping() { // Save the joystick mapping hash table, making sure to update it with // any changes that have been made during the program run - ostringstream joybuf; + json mapping = json::array(); for(const auto& i: myDatabase) { - const string& map = i.second.joy ? i.second.joy->getMap() : i.second.mapping; - if(map != "") - joybuf << CTRL_DELIM << map; + json map = i.second.joy ? i.second.joy->getMap() : i.second.mapping; + + if (!map.is_null()) mapping.emplace_back(map); } - myOSystem.settings().setValue("joymap", joybuf.str()); + + myOSystem.settings().setValue("joymap", mapping.dump()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/PJoystickHandler.hxx b/src/common/PJoystickHandler.hxx index fe7aad585..d5c7c5e3b 100644 --- a/src/common/PJoystickHandler.hxx +++ b/src/common/PJoystickHandler.hxx @@ -28,6 +28,7 @@ class Event; #include "EventHandlerConstants.hxx" #include "PhysicalJoystick.hxx" #include "Variant.hxx" +#include "json.hxx" using PhysicalJoystickPtr = shared_ptr; @@ -48,10 +49,10 @@ class PhysicalJoystickHandler private: struct StickInfo { - StickInfo(const string& map = EmptyString, PhysicalJoystickPtr stick = nullptr) + StickInfo(const nlohmann::json& map = nullptr, PhysicalJoystickPtr stick = nullptr) : mapping(map), joy(std::move(stick)) {} - string mapping; + nlohmann::json mapping; PhysicalJoystickPtr joy; friend ostream& operator<<(ostream& os, const StickInfo& si) { diff --git a/src/common/PhysicalJoystick.cxx b/src/common/PhysicalJoystick.cxx index b784c45ae..dc45d4a10 100644 --- a/src/common/PhysicalJoystick.cxx +++ b/src/common/PhysicalJoystick.cxx @@ -22,6 +22,17 @@ #include "Vec.hxx" #include "bspf.hxx" #include "PhysicalJoystick.hxx" +#include "jsonDefinitions.hxx" + +using json = nlohmann::json; + +namespace { + string jsonName(EventMode eventMode) { + json serializedName = eventMode; + + return serializedName.get(); + } +} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void PhysicalJoystick::initialize(int index, const string& desc, @@ -45,21 +56,18 @@ void PhysicalJoystick::initialize(int index, const string& desc, } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -string PhysicalJoystick::getMap() const +json PhysicalJoystick::getMap() const { - // The mapping structure (for remappable devices) is defined as follows: - // '>'['|'(':'