From 8ef299f67e48bbe5319c6f7f395d5992253531e4 Mon Sep 17 00:00:00 2001 From: stephena Date: Sun, 23 Oct 2005 14:51:51 +0000 Subject: [PATCH] Fixed problems with 'keymap' and 'joymap' lists when new events are added to Stella. The total number of possible events is now prepended to these lists, and this number is checked when reloading Stella. So if the number of events in Stella changes, the defaults are used for key and joy mappings. Note that under certain circumstances this algorithm can still be fooled. Specifically, if we add a new event and remove another, the total count will remain the same, but the list should be invalidated. With the current code, this invalidation won't happen. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@855 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/emucore/EventHandler.cxx | 58 ++++++++++++++--------------- stella/src/emucore/EventHandler.hxx | 16 ++++++-- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/stella/src/emucore/EventHandler.cxx b/stella/src/emucore/EventHandler.cxx index 47c639861..cf64657a7 100644 --- a/stella/src/emucore/EventHandler.cxx +++ b/stella/src/emucore/EventHandler.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: EventHandler.cxx,v 1.106 2005-10-18 18:49:46 stephena Exp $ +// $Id: EventHandler.cxx,v 1.107 2005-10-23 14:51:51 stephena Exp $ //============================================================================ #include @@ -1218,22 +1218,14 @@ void EventHandler::setActionMappings() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::setKeymap() { - // Since istringstream swallows whitespace, we have to make the - // delimiters be spaces string list = myOSystem->settings().getString("keymap"); - replace(list.begin(), list.end(), ':', ' '); + IntArray map; - if(isValidList(list, SDLK_LAST)) + if(isValidList(list, map, SDLK_LAST)) { - istringstream buf(list); - string key; - // Fill the keymap table with events for(Int32 i = 0; i < SDLK_LAST; ++i) - { - buf >> key; - myKeyTable[i] = (Event::Type) atoi(key.c_str()); - } + myKeyTable[i] = (Event::Type) map[i]; } else setDefaultKeymap(); @@ -1242,22 +1234,14 @@ void EventHandler::setKeymap() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::setJoymap() { - // Since istringstream swallows whitespace, we have to make the - // delimiters be spaces string list = myOSystem->settings().getString("joymap"); - replace(list.begin(), list.end(), ':', ' '); + IntArray map; - if(isValidList(list, kNumJoysticks*kNumJoyButtons)) + if(isValidList(list, map, kNumJoysticks*kNumJoyButtons)) { - istringstream buf(list); - string key; - // Fill the joymap table with events for(Int32 i = 0; i < kNumJoysticks*kNumJoyButtons; ++i) - { - buf >> key; - myJoyTable[i] = (Event::Type) atoi(key.c_str()); - } + myJoyTable[i] = (Event::Type) map[i]; } else setDefaultJoymap(); @@ -1443,7 +1427,9 @@ void EventHandler::setDefaultJoymap() void EventHandler::saveKeyMapping() { // Iterate through the keymap table and create a colon-separated list + // Prepend the event count, so we can check it on next load ostringstream keybuf; + keybuf << Event::LastType << ":"; for(uInt32 i = 0; i < SDLK_LAST; ++i) keybuf << myKeyTable[i] << ":"; @@ -1454,7 +1440,9 @@ void EventHandler::saveKeyMapping() void EventHandler::saveJoyMapping() { // Iterate through the joymap table and create a colon-separated list + // Prepend the event count, so we can check it on next load ostringstream joybuf; + joybuf << Event::LastType << ":"; for(Int32 i = 0; i < kNumJoysticks * kNumJoyButtons; ++i) joybuf << myJoyTable[i] << ":"; @@ -1462,17 +1450,27 @@ void EventHandler::saveJoyMapping() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool EventHandler::isValidList(string list, uInt32 length) +bool EventHandler::isValidList(string& list, IntArray& map, uInt32 length) { - // Rudimentary check to see if the list contains 'length' keys - istringstream buf(list); string key; - uInt32 i = 0; + Event::Type event; - while(buf >> key) - i++; + // Since istringstream swallows whitespace, we have to make the + // delimiters be spaces + replace(list.begin(), list.end(), ':', ' '); + istringstream buf(list); - return (i == length); + // Get event count, which should be the first int in the list + buf >> key; + event = (Event::Type) atoi(key.c_str()); + if(event == Event::LastType) + while(buf >> key) + map.push_back(atoi(key.c_str())); + + if(event == Event::LastType && map.size() == length) + return true; + else + return false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/emucore/EventHandler.hxx b/stella/src/emucore/EventHandler.hxx index ce179ac99..474141927 100644 --- a/stella/src/emucore/EventHandler.hxx +++ b/stella/src/emucore/EventHandler.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: EventHandler.hxx,v 1.52 2005-10-02 19:10:39 stephena Exp $ +// $Id: EventHandler.hxx,v 1.53 2005-10-23 14:51:51 stephena Exp $ //============================================================================ #ifndef EVENTHANDLER_HXX @@ -23,6 +23,7 @@ #include "bspf.hxx" #include "Event.hxx" +#include "Array.hxx" class Console; class OSystem; @@ -74,7 +75,7 @@ struct Stella_Joystick { mapping can take place. @author Stephen Anthony - @version $Id: EventHandler.hxx,v 1.52 2005-10-02 19:10:39 stephena Exp $ + @version $Id: EventHandler.hxx,v 1.53 2005-10-23 14:51:51 stephena Exp $ */ class EventHandler { @@ -317,7 +318,16 @@ class EventHandler void saveKeyMapping(); void saveJoyMapping(); - bool isValidList(string list, uInt32 length); + /** + Tests if a mapping list is valid, both by length and by event count. + + @param list The string containing the mappings, separated by ':' + @param map The result of parsing the string for int mappings + @param length The number of items that should be in the list + + @return True if valid list, else false + */ + bool isValidList(string& list, IntArray& map, uInt32 length); void saveState(); void changeState();