Coversion for legacy joystick mappings.

This commit is contained in:
Christian Speckner 2020-11-28 12:15:02 +01:00
parent 62bd47f56d
commit 234a2745c4
6 changed files with 64 additions and 24 deletions

View File

@ -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)

View File

@ -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 */

View File

@ -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)
{

View File

@ -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);

View File

@ -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 "<mode>|" 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)

View File

@ -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;