diff --git a/src/common/JoyMap.cxx b/src/common/JoyMap.cxx index 87eed9d34..409d2677c 100644 --- a/src/common/JoyMap.cxx +++ b/src/common/JoyMap.cxx @@ -247,25 +247,39 @@ int JoyMap::loadMapping(const json& eventMappings, const EventMode mode) return i; } -#if 0 -int JoyMap::loadMapping(string& list, const EventMode mode) + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +json JoyMap::convertLegacyMapping(string list) { + json eventMappings = json::array(); + // Since istringstream swallows whitespace, we have to make the // delimiters be spaces std::replace(list.begin(), list.end(), '|', ' '); std::replace(list.begin(), list.end(), ':', ' '); std::replace(list.begin(), list.end(), ',', ' '); + istringstream buf(list); - int event, button, axis, adir, hat, hdir, i = 0; + int event, button, axis, adir, hat, hdir; while (buf >> event && buf >> button && buf >> axis && buf >> adir - && buf >> hat && buf >> hdir && ++i) - add(Event::Type(event), EventMode(mode), button, JoyAxis(axis), JoyDir(adir), hat, JoyHatDir(hdir)); + && buf >> hat && buf >> hdir) + { + json eventMapping = json::object(); - return i; + eventMapping["event"] = Event::Type(event); + eventMapping["button"] = button; + eventMapping["axis"] = JoyAxis(axis); + eventMapping["axisDirection"] = JoyDir(adir); + eventMapping["hat"] = hat; + eventMapping["hatDirection"] = JoyHatDir(hdir); + + eventMappings.push_back(eventMapping); + } + + return eventMappings; } -#endif // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void JoyMap::eraseMode(const EventMode mode) diff --git a/src/common/JoyMap.hxx b/src/common/JoyMap.hxx index d013e82bc..6bf87009e 100644 --- a/src/common/JoyMap.hxx +++ b/src/common/JoyMap.hxx @@ -114,6 +114,8 @@ class JoyMap nlohmann::json saveMapping(const EventMode mode) const; int loadMapping(const nlohmann::json& eventMappings, const EventMode mode); + static nlohmann::json convertLegacyMapping(string list); + /** Erase all mappings for given mode */ void eraseMode(const EventMode mode); /** Erase given event's mapping for given mode */ diff --git a/src/common/PJoystickHandler.cxx b/src/common/PJoystickHandler.cxx index 590fcda44..f045d292a 100644 --- a/src/common/PJoystickHandler.cxx +++ b/src/common/PJoystickHandler.cxx @@ -43,13 +43,14 @@ PhysicalJoystickHandler::PhysicalJoystickHandler( } json mappings; + const string& serializedMapping = myOSystem.settings().getString("joymap"); try { - mappings = json::parse(myOSystem.settings().getString("joymap")); + mappings = json::parse(serializedMapping); } catch (json::exception) { - // TODO: error handling + migration + Logger::info("converting legacy joystrick mappings"); - mappings = json::array(); + mappings = convertLegacyMapping(serializedMapping); } for (const json& mapping: mappings) { @@ -62,6 +63,29 @@ PhysicalJoystickHandler::PhysicalJoystickHandler( } } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +json PhysicalJoystickHandler::convertLegacyMapping(const string& mapping) +{ + constexpr char CTRL_DELIM = '^'; + + istringstream buf(mapping); + string joymap, joyname; + + getline(buf, joymap, CTRL_DELIM); // event list size, ignore + + json convertedMapping = json::array(); + + while(getline(buf, joymap, CTRL_DELIM)) + { + istringstream namebuf(joymap); + getline(namebuf, joyname, PhysicalJoystick::MODE_DELIM); + + convertedMapping.push_back(PhysicalJoystick::convertLegacyMapping(joymap, joyname)); + } + + return convertedMapping; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int PhysicalJoystickHandler::add(const PhysicalJoystickPtr& stick) { diff --git a/src/common/PJoystickHandler.hxx b/src/common/PJoystickHandler.hxx index d5c7c5e3b..8e43bdf97 100644 --- a/src/common/PJoystickHandler.hxx +++ b/src/common/PJoystickHandler.hxx @@ -64,6 +64,8 @@ class PhysicalJoystickHandler public: PhysicalJoystickHandler(OSystem& system, EventHandler& handler); + static nlohmann::json convertLegacyMapping(const string& mapping); + /** Return stick ID on success, -1 on failure. */ int add(const PhysicalJoystickPtr& stick); bool remove(int id); diff --git a/src/common/PhysicalJoystick.cxx b/src/common/PhysicalJoystick.cxx index 67abd435e..6310c8e4d 100644 --- a/src/common/PhysicalJoystick.cxx +++ b/src/common/PhysicalJoystick.cxx @@ -106,12 +106,12 @@ bool PhysicalJoystick::setMap(const json& map) return true; } -#if 0 -bool PhysicalJoystick::setMap(const string& mapString) +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +json PhysicalJoystick::convertLegacyMapping(const string& mapping, const string& name) { - istringstream buf(mapString); + istringstream buf(mapping); + json convertedMapping = json::object(); string map; - int i = 0; // Skip joystick name getline(buf, map, MODE_DELIM); @@ -128,19 +128,14 @@ bool PhysicalJoystick::setMap(const string& mapString) // Remove leading "|" string map.erase(0, 2); - joyMap.loadMapping(map, EventMode(mode)); - i++; - } - // Brief error checking - if(i != 5) - { - cerr << "ERROR: Invalid controller mappings found" << endl; - return false; + json mappingForMode = JoyMap::convertLegacyMapping(map); + mappingForMode["name"] = name; + + convertedMapping[jsonName(EventMode(mode))] = mappingForMode; } - return true; + return convertedMapping; } -#endif // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void PhysicalJoystick::eraseMap(EventMode mode) diff --git a/src/common/PhysicalJoystick.hxx b/src/common/PhysicalJoystick.hxx index 027aba47f..454008f51 100644 --- a/src/common/PhysicalJoystick.hxx +++ b/src/common/PhysicalJoystick.hxx @@ -47,6 +47,9 @@ class PhysicalJoystick nlohmann::json getMap() const; bool setMap(const nlohmann::json& map); + + static nlohmann::json convertLegacyMapping(const string& mapping, const string& name); + void eraseMap(EventMode mode); void eraseEvent(Event::Type event, EventMode mode); string about() const;