Convert legacy keyboard mappings.

This commit is contained in:
Christian Speckner 2020-11-28 12:46:48 +01:00
parent 234a2745c4
commit 9ce8e9a02a
5 changed files with 59 additions and 44 deletions

View File

@ -16,6 +16,7 @@
//============================================================================ //============================================================================
#include "KeyMap.hxx" #include "KeyMap.hxx"
#include "Logger.hxx"
#include "jsonDefinitions.hxx" #include "jsonDefinitions.hxx"
#include <map> #include <map>
@ -198,6 +199,7 @@ int KeyMap::loadMapping(const json& mappings, const EventMode mode) {
int i = 0; int i = 0;
for (const json& mapping: mappings) { for (const json& mapping: mappings) {
try {
add( add(
mapping.at("event").get<Event::Type>(), mapping.at("event").get<Event::Type>(),
mode, mode,
@ -206,27 +208,39 @@ int KeyMap::loadMapping(const json& mappings, const EventMode mode) {
); );
i++; i++;
} catch (json::exception) {
Logger::error("ignoring bad keyboard mapping");
}
} }
return i; return i;
} }
/*
int KeyMap::loadMapping(string& list, const EventMode mode) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
json KeyMap::convertLegacyMapping(string list)
{ {
json convertedMapping = json::array();
// Since istringstream swallows whitespace, we have to make the // Since istringstream swallows whitespace, we have to make the
// delimiters be spaces // delimiters be spaces
std::replace(list.begin(), list.end(), '|', ' '); std::replace(list.begin(), list.end(), '|', ' ');
std::replace(list.begin(), list.end(), ':', ' '); std::replace(list.begin(), list.end(), ':', ' ');
std::replace(list.begin(), list.end(), ',', ' '); std::replace(list.begin(), list.end(), ',', ' ');
istringstream buf(list); istringstream buf(list);
int event, key, mod, i = 0; int event, key, mod;
while (buf >> event && buf >> key && buf >> mod && ++i) while (buf >> event && buf >> key && buf >> mod) {
add(Event::Type(event), mode, key, mod); json mapping = json::object();
return i; mapping["event"] = Event::Type(event);
mapping["key"] = StellaKey(key);
mapping["mod"] = StellaMod(mod);
convertedMapping.push_back(mapping);
}
return convertedMapping;
} }
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyMap::eraseMode(const EventMode mode) void KeyMap::eraseMode(const EventMode mode)

View File

@ -90,6 +90,8 @@ class KeyMap
nlohmann::json saveMapping(const EventMode mode) const; nlohmann::json saveMapping(const EventMode mode) const;
int loadMapping(const nlohmann::json& mapping, const EventMode mode); int loadMapping(const nlohmann::json& mapping, const EventMode mode);
static nlohmann::json convertLegacyMapping(string list);
/** Erase all mappings for given mode */ /** Erase all mappings for given mode */
void eraseMode(const EventMode mode); void eraseMode(const EventMode mode);
/** Erase given event's mapping for given mode */ /** Erase given event's mapping for given mode */

View File

@ -55,7 +55,7 @@ PhysicalJoystickHandler::PhysicalJoystickHandler(
for (const json& mapping: mappings) { for (const json& mapping: mappings) {
if (!mapping.contains("name")) { if (!mapping.contains("name")) {
Logger::error("igmoring bad joystick mapping"); Logger::error("ignoring bad joystick mapping");
continue; continue;
} }

View File

@ -49,38 +49,15 @@ PhysicalKeyboardHandler::PhysicalKeyboardHandler(OSystem& system, EventHandler&
// Compare if event list version has changed so that key maps became invalid // Compare if event list version has changed so that key maps became invalid
if (version == Event::VERSION) if (version == Event::VERSION)
{ {
try { loadSerializedMappings(myOSystem.settings().getString("keymap_emu"), EventMode::kCommonMode);
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_emu")), EventMode::kCommonMode); loadSerializedMappings(myOSystem.settings().getString("keymap_joy"), EventMode::kJoystickMode);
} catch (json::exception) { loadSerializedMappings(myOSystem.settings().getString("keymap_pad"), EventMode::kPaddlesMode);
Logger::error("ignoring bad keyboard mappings for mode: common"); loadSerializedMappings(myOSystem.settings().getString("keymap_key"), EventMode::kKeypadMode);
} loadSerializedMappings(myOSystem.settings().getString("keymap_ui"), EventMode::kMenuMode);
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_joy")), EventMode::kJoystickMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: joystick");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_pad")), EventMode::kPaddlesMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: paddles");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_key")), EventMode::kKeypadMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: keypad");
}
try {
myKeyMap.loadMapping(json::parse(myOSystem.settings().getString("keymap_ui")), EventMode::kMenuMode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings for mode: UI");
}
updateDefaults = true; updateDefaults = true;
} }
myKeyMap.enableMod() = myOSystem.settings().getBool("modcombo"); myKeyMap.enableMod() = myOSystem.settings().getBool("modcombo");
setDefaultMapping(Event::NoType, EventMode::kEmulationMode, updateDefaults); setDefaultMapping(Event::NoType, EventMode::kEmulationMode, updateDefaults);
@ -90,6 +67,26 @@ PhysicalKeyboardHandler::PhysicalKeyboardHandler(OSystem& system, EventHandler&
#endif // DEBUG #endif // DEBUG
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalKeyboardHandler::loadSerializedMappings(const string& serializedMapping, EventMode mode)
{
json mapping;
try {
mapping = json::parse(serializedMapping);
} catch (json::exception) {
Logger::info("converting legacy keyboard mappings");
mapping = KeyMap::convertLegacyMapping(serializedMapping);
}
try {
myKeyMap.loadMapping(mapping, mode);
} catch (json::exception) {
Logger::error("ignoring bad keyboard mappings");
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PhysicalKeyboardHandler::isMappingUsed(EventMode mode, const EventMapping& map) const bool PhysicalKeyboardHandler::isMappingUsed(EventMode mode, const EventMapping& map) const
{ {

View File

@ -45,6 +45,8 @@ class PhysicalKeyboardHandler
PhysicalKeyboardHandler(OSystem& system, EventHandler& handler); PhysicalKeyboardHandler(OSystem& system, EventHandler& handler);
void loadSerializedMappings(const string& serializedMappings, EventMode mode);
void setDefaultMapping(Event::Type type, EventMode mode, bool updateDefaults = false); void setDefaultMapping(Event::Type type, EventMode mode, bool updateDefaults = false);
/** define mappings for current controllers */ /** define mappings for current controllers */