aboutsummaryrefslogtreecommitdiff
path: root/backends/common
diff options
context:
space:
mode:
authorStephen Kennedy2008-07-24 10:00:56 +0000
committerStephen Kennedy2008-07-24 10:00:56 +0000
commitdfcdbb0d335128c99d13ba017a4e36f2338f7736 (patch)
tree727f48a0e5e589229788b0c42b0a73cafd31b39e /backends/common
parent2f064da1021344f28c9106285475c72930638390 (diff)
downloadscummvm-rg350-dfcdbb0d335128c99d13ba017a4e36f2338f7736.tar.gz
scummvm-rg350-dfcdbb0d335128c99d13ba017a4e36f2338f7736.tar.bz2
scummvm-rg350-dfcdbb0d335128c99d13ba017a4e36f2338f7736.zip
KeymapManager - implemented loading/saving of keymaps
- Refactoring of code to map a key to a UserAction - now we call a method on UserAction to do it (and it then tells the Keymap class) - General cleanup of code svn-id: r33262
Diffstat (limited to 'backends/common')
-rw-r--r--backends/common/hardware-key.h4
-rw-r--r--backends/common/keymap-manager.cpp61
-rw-r--r--backends/common/keymap-manager.h8
-rw-r--r--backends/common/keymap.cpp41
-rw-r--r--backends/common/keymap.h42
-rw-r--r--backends/common/keymapper.cpp13
-rw-r--r--backends/common/keymapper.h13
-rw-r--r--backends/common/user-action.cpp33
-rw-r--r--backends/common/user-action.h23
-rw-r--r--backends/common/virtual-keyboard.cpp2
10 files changed, 160 insertions, 80 deletions
diff --git a/backends/common/hardware-key.h b/backends/common/hardware-key.h
index de1984dc31..f6253c0b96 100644
--- a/backends/common/hardware-key.h
+++ b/backends/common/hardware-key.h
@@ -80,9 +80,9 @@ private:
List<HardwareKey*>::iterator it;
for (it = _keys.begin(); it != _keys.end(); it++) {
if ((*it)->id == key->id)
- error("HardwareKey with id %d already given!\n", key->id);
+ error("HardwareKey with id %d already given!", key->id);
else if ((*it)->key == key->key)
- error("HardwareKey with same KeyState already given!\n");
+ error("HardwareKey with same KeyState already given!");
}
}
diff --git a/backends/common/keymap-manager.cpp b/backends/common/keymap-manager.cpp
index 8ef89add6e..9386dc5b5e 100644
--- a/backends/common/keymap-manager.cpp
+++ b/backends/common/keymap-manager.cpp
@@ -32,6 +32,11 @@ Keymap *KeymapManager::Domain::getKeymap(const String& name) {
return 0;
}
+void KeymapManager::registerHardwareKeySet(HardwareKeySet *keys) {
+ if (_hardwareKeys)
+ error("Hardware key set already registered!");
+ _hardwareKeys = keys;
+}
void KeymapManager::registerDefaultGlobalKeymap(Keymap *map) {
ConfigManager::Domain *dom = ConfMan.getDomain(ConfigManager::kApplicationDomain);
@@ -75,13 +80,65 @@ void KeymapManager::initKeymap(ConfigManager::Domain *domain,
bool KeymapManager::loadKeymap(ConfigManager::Domain *domain,
const String& name,
Keymap *map) {
+ ConfigManager::Domain::iterator it;
+ String prefix = "km_" + name + "_";
+ for (it = domain->begin(); it != domain->end(); it++) {
+ const String& key = it->_key;
+ if (!key.hasPrefix(prefix.c_str()))
+ continue;
+
+ // parse UserAction ID
+ const char *actionIdStart = key.c_str() + prefix.size();
+ char *err;
+ int32 actionId = (int32) strtol(actionIdStart, &err, 0);
+ if (err == actionIdStart) {
+ warning("'%s' is not a valid UserAction ID", err);
+ continue;
+ }
+ UserAction *ua = map->getUserAction(actionId);
+ if (!ua) {
+ warning("'%s' keymap does not contain UserAction with ID %d",
+ name.c_str(), (int)actionId);
+ continue;
+ }
+
+ // parse HardwareKey ID
+ int32 hwKeyId = (int32) strtol(it->_value.c_str(), &err, 0);
+ if (err == it->_value.c_str()) {
+ warning("'%s' is not a valid HardwareKey ID", err);
+ continue;
+ }
+ const HardwareKey *hwKey = _hardwareKeys->findHardwareKey(hwKeyId);
+ if (!hwKey) {
+ warning("HardwareKey with ID %d not known", (int)hwKeyId);
+ continue;
+ }
+
+ ua->mapKey(hwKey);
+ }
+ return isMapComplete(map);
+}
+
+bool KeymapManager::isMapComplete(const Keymap *map) {
return false;
}
void KeymapManager::saveKeymap(ConfigManager::Domain *domain,
const String& name,
- Keymap *map) {
-
+ const Keymap *map) {
+ const Array<UserAction>& actions = map->getUserActions();
+ Array<UserAction>::const_iterator it;
+ char buf[11];
+ for (it = actions.begin(); it != actions.end(); it++) {
+ String key("km_");
+ sprintf(buf, "%d", it->id);
+ key += name + "_" + buf;
+ if (it->getMappedKey())
+ sprintf(buf, "%d", it->getMappedKey()->id);
+ else
+ strcpy(buf, "");
+ domain->setVal(key, buf);
+ }
}
diff --git a/backends/common/keymap-manager.h b/backends/common/keymap-manager.h
index c056fbe024..b74133d781 100644
--- a/backends/common/keymap-manager.h
+++ b/backends/common/keymap-manager.h
@@ -1,6 +1,7 @@
#ifndef COMMON_KEYMAP_MANAGER
#define COMMON_KEYMAP_MANAGER
+#include "backends/common/hardware-key.h"
#include "backends/common/keymap.h"
#include "common/config-manager.h"
#include "common/hash-str.h"
@@ -32,6 +33,8 @@ public:
KeymapMap _keymaps;
};
+ void registerHardwareKeySet(HardwareKeySet *keys);
+
void registerDefaultGlobalKeymap(Keymap *map);
void registerGlobalKeymap(const String& name, Keymap *map);
@@ -46,11 +49,14 @@ private:
void initKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
bool loadKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
- void saveKeymap(ConfigManager::Domain *domain, const String& name, Keymap *keymap);
+ void saveKeymap(ConfigManager::Domain *domain, const String& name, const Keymap *keymap);
void automaticMap(Keymap *map);
+ bool isMapComplete(const Keymap *map);
Domain _globalDomain;
Domain _gameDomain;
+
+ HardwareKeySet *_hardwareKeys;
};
} // end of namespace Common
diff --git a/backends/common/keymap.cpp b/backends/common/keymap.cpp
index 770e308fbc..639b03f78f 100644
--- a/backends/common/keymap.cpp
+++ b/backends/common/keymap.cpp
@@ -1,12 +1,14 @@
#include "backends/common/keymap.h"
+#include "backends/common/hardware-key.h"
namespace Common {
Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap() {
init();
for (uint i = 0; i < _actions.size(); i++) {
- if (_actions[i].hwKey) {
- _keymap[_actions[i].hwKey->key] = &_actions[i];
+ const HardwareKey *hwKey = _actions[i].getMappedKey();
+ if (hwKey) {
+ _keymap[hwKey->key] = &_actions[i];
}
}
}
@@ -15,41 +17,30 @@ void Keymap::init() {
_actions.reserve(20);
}
-void Keymap::addAction(const UserAction& action) {
+void Keymap::addAction(UserAction& action) {
if (findUserAction(action.id))
- error("UserAction with id %d already in KeyMap!\n", action.id);
+ error("UserAction with id %d already in KeyMap!", action.id);
+ action.setParent(this);
_actions.push_back(action);
- _actions[_actions.size()-1].hwKey = 0;
}
-void Keymap::mapKeyToAction(UserAction *action, HardwareKey *key) {
- for (uint i = 0; i < _actions.size(); i++) {
- if (&_actions[i] == action) {
- internalMapKey(action, key);
- return;
- }
- }
- error("UserAction not contained in KeyMap\n");
-}
-
-void Keymap::mapKeyToAction(int32 id, HardwareKey *key) {
- UserAction *act = findUserAction(id);
- if (act)
- internalMapKey(act, key);
-}
-
-void Keymap::internalMapKey(UserAction *action, HardwareKey *hwKey) {
+void Keymap::registerMapping(UserAction *action, const HardwareKey *hwKey) {
HashMap<KeyState, UserAction*>::iterator it;
it = _keymap.find(hwKey->key);
// if key is already mapped to an action then un-map it
if (it != _keymap.end())
- it->_value->hwKey = 0;
+ it->_value->mapKey(0);
- action->hwKey = hwKey;
_keymap[hwKey->key] = action;
}
-const UserAction *Keymap::getUserAction(int32 id) const {
+void Keymap::unregisterMapping(UserAction *action) {
+ const HardwareKey *hwKey = action->getMappedKey();
+ if (hwKey)
+ _keymap[hwKey->key] = 0;
+}
+
+UserAction *Keymap::getUserAction(int32 id) {
return findUserAction(id);
}
diff --git a/backends/common/keymap.h b/backends/common/keymap.h
index 5197e86b48..d9bc8f08df 100644
--- a/backends/common/keymap.h
+++ b/backends/common/keymap.h
@@ -1,15 +1,16 @@
#ifndef COMMON_KEYMAP
#define COMMON_KEYMAP
-#include "backends/common/hardware-key.h"
-#include "backends/common/user-action.h"
#include "common/array.h"
#include "common/keyboard.h"
#include "common/func.h"
#include "common/hashmap.h"
+#include "backends/common/user-action.h"
namespace Common {
+struct HardwareKey;
+
/**
* Hash function for KeyState
*/
@@ -34,30 +35,14 @@ public:
* adding it at the back of the internal array
* @param action the UserAction to add
*/
- void addAction(const UserAction& action);
-
- /**
- * Maps a HardwareKey to the given UserAction
- * @param action must point to a UserAction in this Keymap
- * @param key pointer to HardwareKey to map
- * @note if action does not point to a UserAction in this Keymap a
- * fatal error will occur
- */
- void mapKeyToAction(UserAction *action, HardwareKey *key);
-
- /**
- * Maps a HardwareKey to the UserAction of the given id
- * @param id id of the UserAction to map to
- * @param key pointer to HardwareKey to map
- */
- void mapKeyToAction(int32 id, HardwareKey *key);
+ void addAction(UserAction& action);
/**
* Retrieves the UserAction with the given id
* @param id id of UserAction to retrieve
* @return Pointer to the UserAction or 0 if not found
*/
- const UserAction *getUserAction(int32 id) const;
+ UserAction *getUserAction(int32 id);
/**
* Get a read-only array of all the UserActions contained in this Keymap
@@ -72,7 +57,22 @@ public:
UserAction *getMappedAction(const KeyState& ks) const;
private:
-
+ friend struct UserAction;
+ /**
+ * Registers a HardwareKey to the given UserAction
+ * @param action UserAction in this Keymap
+ * @param key pointer to HardwareKey to map
+ * @see UserAction::mapKey
+ */
+ void registerMapping(UserAction *action, const HardwareKey *key);
+
+ /**
+ * Unregisters a HardwareKey from the given UserAction (if one is mapped)
+ * @param action UserAction in this Keymap
+ * @see UserAction::mapKey
+ */
+ void unregisterMapping(UserAction *action);
+
UserAction *findUserAction(int32 id);
const UserAction *findUserAction(int32 id) const;
diff --git a/backends/common/keymapper.cpp b/backends/common/keymapper.cpp
index bb98eff7ef..d7d6b047aa 100644
--- a/backends/common/keymapper.cpp
+++ b/backends/common/keymapper.cpp
@@ -7,17 +7,10 @@ Keymapper::Keymapper(EventManager *evtMgr) {
_eventMan = evtMgr;
_keymapMan = new KeymapManager();
_currentMap = 0;
- _hardwareKeys = 0;
}
void Keymapper::registerHardwareKeySet(HardwareKeySet *keys) {
- if (_hardwareKeys)
- error("Hardware key set already registered!\n");
- _hardwareKeys = keys;
-}
-
-const HardwareKeySet *Keymapper::getHardwareKeySet() const {
- return _hardwareKeys;
+ _keymapMan->registerHardwareKeySet(keys);
}
void Keymapper::addGlobalKeyMap(const String& name, Keymap *keymap) {
@@ -35,7 +28,7 @@ void Keymapper::addGameKeyMap(const String& name, Keymap *keymap) {
void Keymapper::initGame() {
if (ConfMan.getActiveDomain() == 0)
- error("Call to Keymapper::initGame when no game loaded\n");
+ error("Call to Keymapper::initGame when no game loaded");
if (_gameId.size() > 0)
cleanupGame();
@@ -51,7 +44,7 @@ void Keymapper::cleanupGame() {
bool Keymapper::switchKeymap(const String& name) {
Keymap *new_map = _keymapMan->getKeymap(name);
if (!new_map) {
- warning("Keymap '%s' not registered\n", name.c_str());
+ warning("Keymap '%s' not registered", name.c_str());
return false;
}
_currentMap = new_map;
diff --git a/backends/common/keymapper.h b/backends/common/keymapper.h
index 3252d336e1..bbdd0eaca6 100644
--- a/backends/common/keymapper.h
+++ b/backends/common/keymapper.h
@@ -1,12 +1,16 @@
#ifndef COMMON_KEYMAPPER
#define COMMON_KEYMAPPER
-#include "backends/common/keymap.h"
+#include "common/events.h"
#include "common/list.h"
+
namespace Common {
+struct HardwareKey;
+class HardwareKeySet;
class KeymapManager;
+class Keymap;
class Keymapper {
public:
@@ -20,11 +24,6 @@ public:
void registerHardwareKeySet(HardwareKeySet *keys);
/**
- * Get the HardwareKeySet that is registered with the Keymapper
- */
- const HardwareKeySet *getHardwareKeySet() const;
-
- /**
* Add a keymap to the global domain.
* If a saved key setup exists for it in the ini file it will be used.
* Else, the key setup will be automatically mapped.
@@ -88,8 +87,6 @@ private:
Keymap *_currentMap;
- const HardwareKeySet *_hardwareKeys;
-
};
} // end of namespace Common
diff --git a/backends/common/user-action.cpp b/backends/common/user-action.cpp
new file mode 100644
index 0000000000..f6b24339e9
--- /dev/null
+++ b/backends/common/user-action.cpp
@@ -0,0 +1,33 @@
+#include "backends/common/user-action.h"
+#include "backends/common/keymap.h"
+
+namespace Common {
+
+UserAction::UserAction(String des, UserActionCategory cat, UserActionType ty,
+ int pr, int gr, int fl) {
+ description = des;
+ category = cat;
+ type = ty;
+ priority = pr;
+ group = gr;
+ flags = fl;
+ _hwKey = 0;
+ _parent = 0;
+}
+
+void UserAction::setParent(Keymap *parent) {
+ _parent = parent;
+}
+
+void UserAction::mapKey(const HardwareKey *key) {
+ assert(_parent);
+ if (_hwKey) _parent->unregisterMapping(this);
+ _hwKey = key;
+ if (_hwKey) _parent->registerMapping(this, key);
+}
+
+const HardwareKey *UserAction::getMappedKey() const {
+ return _hwKey;
+}
+
+} // end of namespace Common \ No newline at end of file
diff --git a/backends/common/user-action.h b/backends/common/user-action.h
index 31b5473708..2e7c73334b 100644
--- a/backends/common/user-action.h
+++ b/backends/common/user-action.h
@@ -8,6 +8,8 @@
namespace Common {
struct HardwareKey;
+class Keymap;
+
enum UserActionType {
kGenericUserActionType,
@@ -48,21 +50,22 @@ struct UserAction {
int group;
int flags;
+private:
/** Hardware key that is mapped to this UserAction */
- HardwareKey *hwKey;
+ const HardwareKey *_hwKey;
+ Keymap *_parent;
+public:
UserAction( String des = "",
UserActionCategory cat = kGenericUserActionCategory,
UserActionType ty = kGenericUserActionType,
- int pr = 0, int gr = 0, int fl = 0 ) {
- description = des;
- category = cat;
- type = ty;
- priority = pr;
- group = gr;
- flags = fl;
- hwKey = 0;
- }
+ int pr = 0, int gr = 0, int fl = 0 );
+
+ void setParent(Keymap *parent);
+
+ void mapKey(const HardwareKey *key);
+
+ const HardwareKey *getMappedKey() const;
};
} // end of namespace Common
diff --git a/backends/common/virtual-keyboard.cpp b/backends/common/virtual-keyboard.cpp
index 21d188dced..0a061df545 100644
--- a/backends/common/virtual-keyboard.cpp
+++ b/backends/common/virtual-keyboard.cpp
@@ -103,7 +103,7 @@ bool VirtualKeyboard::loadKeyboardPack(Common::String packName) {
return false;
}
} else {
- warning("Could not find %s.xml file in %s.zip keyboard pack\n", packName.c_str(), packName.c_str());
+ warning("Could not find %s.xml file in %s.zip keyboard pack", packName.c_str(), packName.c_str());
unzClose(zipFile);
return false;
}