[Scummvm-cvs-logs] SF.net SVN: scummvm:[35971] scummvm/trunk/backends/keymapper

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Wed Jan 21 03:02:55 CET 2009


Revision: 35971
          http://scummvm.svn.sourceforge.net/scummvm/?rev=35971&view=rev
Author:   fingolfin
Date:     2009-01-21 02:02:55 +0000 (Wed, 21 Jan 2009)

Log Message:
-----------
more cleanup

Modified Paths:
--------------
    scummvm/trunk/backends/keymapper/hardware-key.h
    scummvm/trunk/backends/keymapper/keymap.cpp
    scummvm/trunk/backends/keymapper/keymapper.cpp
    scummvm/trunk/backends/keymapper/keymapper.h
    scummvm/trunk/backends/keymapper/remap-dialog.cpp

Modified: scummvm/trunk/backends/keymapper/hardware-key.h
===================================================================
--- scummvm/trunk/backends/keymapper/hardware-key.h	2009-01-21 01:26:04 UTC (rev 35970)
+++ scummvm/trunk/backends/keymapper/hardware-key.h	2009-01-21 02:02:55 UTC (rev 35971)
@@ -36,14 +36,17 @@
 
 
 #define HWKEY_ID_SIZE (4)
+
 /**
 * Describes an available hardware key 
 */
 struct HardwareKey {
 	/** unique id used for saving/loading to config */
-	char id[HWKEY_ID_SIZE];
+	char hwKeyId[HWKEY_ID_SIZE];
+
 	/** Human readable description */
 	String description; 
+
 	/** 
 	* The KeyState that is generated by the back-end 
 	* when this hardware key is pressed.
@@ -57,20 +60,19 @@
 				KeyType typ = kGenericKeyType, ActionType prefAct = kGenericActionType)
 		: key(ky), description(desc), type(typ), preferredAction(prefAct) {
 		assert(i);
-		strncpy(id, i, HWKEY_ID_SIZE);
+		strncpy(hwKeyId, i, HWKEY_ID_SIZE);
 	}
 };
 
 
 /**
  * Simple class to encapsulate a device's set of HardwareKeys.
- * Each device should extend this and call addHardwareKey a number of times
+ * Each device should instantiate this and call addHardwareKey a number of times
  * in its constructor to define the device's available keys.
  */ 
 class HardwareKeySet {
 public:
 
-	HardwareKeySet() : _count(0) {}
 	virtual ~HardwareKeySet() {
 		List<const HardwareKey*>::iterator it;
 		for (it = _keys.begin(); it != _keys.end(); it++)
@@ -80,13 +82,12 @@
 	void addHardwareKey(HardwareKey *key) {
 		checkForKey(key);
 		_keys.push_back(key);
-		++_count;
 	}
 
 	const HardwareKey *findHardwareKey(const char *id) const {
 		List<const HardwareKey*>::iterator it;
 		for (it = _keys.begin(); it != _keys.end(); it++) {
-			if (strncmp((*it)->id, id, HWKEY_ID_SIZE) == 0)
+			if (strncmp((*it)->hwKeyId, id, HWKEY_ID_SIZE) == 0)
 				return (*it);
 		}
 		return 0;
@@ -101,12 +102,12 @@
 		return 0;
 	}
 
-	List<const HardwareKey*> getHardwareKeys() const {
+	const List<const HardwareKey*> &getHardwareKeys() const {
 		return _keys;
 	}
 
-	uint count() const {
-		return _count;
+	uint size() const {
+		return _keys.size();
 	}
 
 
@@ -115,15 +116,14 @@
 	void checkForKey(HardwareKey *key) {
 		List<const HardwareKey*>::iterator it;
 		for (it = _keys.begin(); it != _keys.end(); it++) {
-			if (strncmp((*it)->id, key->id, HWKEY_ID_SIZE) == 0)
-				error("Error adding HardwareKey '%s' - id of %s already in use!", key->description.c_str(), key->id);
+			if (strncmp((*it)->hwKeyId, key->hwKeyId, HWKEY_ID_SIZE) == 0)
+				error("Error adding HardwareKey '%s' - id of %s already in use!", key->description.c_str(), key->hwKeyId);
 			else if ((*it)->key == key->key)
 				error("Error adding HardwareKey '%s' - key already in use!", key->description.c_str());
 		}
 	}
 
 	List<const HardwareKey*> _keys;
-	uint _count;
 };
 
 

Modified: scummvm/trunk/backends/keymapper/keymap.cpp
===================================================================
--- scummvm/trunk/backends/keymapper/keymap.cpp	2009-01-21 01:26:04 UTC (rev 35970)
+++ scummvm/trunk/backends/keymapper/keymap.cpp	2009-01-21 02:02:55 UTC (rev 35971)
@@ -139,21 +139,20 @@
 }
 
 void Keymap::saveMappings() {
-	if (!_configDomain) return;
+	if (!_configDomain)
+		return;
 	List<Action*>::const_iterator it;
 	String prefix = KEYMAP_KEY_PREFIX + _name + "_";
 	for (it = _actions.begin(); it != _actions.end(); it++) {
 		uint actIdLen = strlen((*it)->id);
 		actIdLen = (actIdLen > ACTION_ID_SIZE) ? ACTION_ID_SIZE : actIdLen;
 		String actId((*it)->id, (*it)->id + actIdLen);
+		char hwId[HWKEY_ID_SIZE+1];
+		memset(hwId, 0, HWKEY_ID_SIZE+1);
 		if ((*it)->getMappedKey()) {
-			uint hwIdLen = strlen((*it)->getMappedKey()->id);
-			hwIdLen = (hwIdLen > HWKEY_ID_SIZE) ? HWKEY_ID_SIZE : hwIdLen;
-			String hwId((*it)->getMappedKey()->id, (*it)->getMappedKey()->id + hwIdLen);
-			_configDomain->setVal(prefix + actId, hwId);
-		} else {
-			_configDomain->setVal(prefix + actId, "");
+			memcpy(hwId, (*it)->getMappedKey()->hwKeyId, HWKEY_ID_SIZE);
 		}
+		_configDomain->setVal(prefix + actId, hwId);
 	}
 }
 
@@ -168,7 +167,7 @@
 			allMapped = false;
 		}
 	}
-	return allMapped || (numberMapped == hwKeys->count());
+	return allMapped || (numberMapped == hwKeys->size());
 }
 
 // TODO:

Modified: scummvm/trunk/backends/keymapper/keymapper.cpp
===================================================================
--- scummvm/trunk/backends/keymapper/keymapper.cpp	2009-01-21 01:26:04 UTC (rev 35970)
+++ scummvm/trunk/backends/keymapper/keymapper.cpp	2009-01-21 02:02:55 UTC (rev 35971)
@@ -68,34 +68,31 @@
 }
 
 void Keymapper::addGlobalKeymap(Keymap *keymap) {
-	initKeymap(_globalDomain.getConfigDomain(), keymap);
-	_globalDomain.addKeymap(keymap);
+	initKeymap(_globalDomain, keymap);
 }
 
-void Keymapper::refreshGameDomain() {
+void Keymapper::addGameKeymap(Keymap *keymap) {
+	if (ConfMan.getActiveDomain() == 0)
+		error("Call to Keymapper::addGameKeymap when no game loaded");
+	
+	// Detect whether the active game changed since last call.
+	// If so, flush the game key configuration.
 	if (_gameDomain.getConfigDomain() != ConfMan.getActiveDomain()) {
 		cleanupGameKeymaps();
 		_gameDomain.setConfigDomain(ConfMan.getActiveDomain());
 	}
+	initKeymap(_gameDomain, keymap);
 }
 
-void Keymapper::addGameKeymap(Keymap *keymap) {
-	if (ConfMan.getActiveDomain() == 0)
-		error("Call to Keymapper::addGameKeymap when no game loaded");
-		
-	refreshGameDomain();
-	initKeymap(_gameDomain.getConfigDomain(), keymap);
-	_gameDomain.addKeymap(keymap);
-}
-
-void Keymapper::initKeymap(ConfigManager::Domain *domain, Keymap *map) {
-	map->setConfigDomain(domain);
+void Keymapper::initKeymap(Domain &domain, Keymap *map) {
+	map->setConfigDomain(domain.getConfigDomain());
 	map->loadMappings(_hardwareKeys);
 	if (map->isComplete(_hardwareKeys) == false) {
 		map->automaticMapping(_hardwareKeys);
 		map->saveMappings();
 		ConfMan.flushToDisk();
 	}
+	domain.addKeymap(map);
 }
 
 void Keymapper::cleanupGameKeymaps() {
@@ -157,9 +154,11 @@
 		for (int i = _activeMaps.size() - 1; i >= 0; --i) {
 			MapRecord mr = _activeMaps[i];
 			action = mr.keymap->getMappedAction(key);
-			if (action || mr.inherit == false) break;
+			if (action || mr.inherit == false)
+				break;
 		}
-		if (action) _keysDown[key] = action;
+		if (action)
+			_keysDown[key] = action;
 	} else {
 		HashMap<KeyState, Action*>::iterator it = _keysDown.find(key);
 		if (it != _keysDown.end()) {
@@ -167,7 +166,8 @@
 			_keysDown.erase(key);
 		}
 	}
-	if (!action) return false;
+	if (!action)
+		return false;
 	executeAction(action, keyDown);
 	return true;
 }
@@ -215,7 +215,7 @@
 	}
 }
 
-const HardwareKey *Keymapper::getHardwareKey(const KeyState& key) {
+const HardwareKey *Keymapper::findHardwareKey(const KeyState& key) {
 	return (_hardwareKeys) ? _hardwareKeys->findHardwareKey(key) : 0;
 }
 

Modified: scummvm/trunk/backends/keymapper/keymapper.h
===================================================================
--- scummvm/trunk/backends/keymapper/keymapper.h	2009-01-21 01:26:04 UTC (rev 35970)
+++ scummvm/trunk/backends/keymapper/keymapper.h	2009-01-21 02:02:55 UTC (rev 35971)
@@ -84,9 +84,12 @@
 	void registerHardwareKeySet(HardwareKeySet *keys);
 
 	/**
-	 * Get the HardwareKeySet that is registered with the Keymapper
+	 * Get a list of all registered HardwareKeys
 	 */
-	HardwareKeySet *getHardwareKeySet() { return _hardwareKeys; }
+	const List<const HardwareKey*> &getHardwareKeys() const {
+		assert(_hardwareKeys);
+		return _hardwareKeys->getHardwareKeys();
+	}
 
 	/**
 	 * Add a keymap to the global domain.
@@ -161,7 +164,7 @@
 	/**
 	 * Return a HardwareKey pointer for the given key state
 	 */
-	const HardwareKey *getHardwareKey(const KeyState& key);
+	const HardwareKey *findHardwareKey(const KeyState& key);
 
 	Domain& getGlobalDomain() { return _globalDomain; }
 	Domain& getGameDomain() { return _gameDomain; }
@@ -169,8 +172,7 @@
 
 private:
 
-	void initKeymap(ConfigManager::Domain *domain, Keymap *keymap);
-	void refreshGameDomain();
+	void initKeymap(Domain &domain, Keymap *keymap);
 
 	Domain _globalDomain;
 	Domain _gameDomain;

Modified: scummvm/trunk/backends/keymapper/remap-dialog.cpp
===================================================================
--- scummvm/trunk/backends/keymapper/remap-dialog.cpp	2009-01-21 01:26:04 UTC (rev 35970)
+++ scummvm/trunk/backends/keymapper/remap-dialog.cpp	2009-01-21 02:02:55 UTC (rev 35971)
@@ -205,7 +205,7 @@
 
 void RemapDialog::handleKeyUp(Common::KeyState state) {
 	if (_activeRemapAction) {
-		const HardwareKey *hwkey = _keymapper->getHardwareKey(state);
+		const HardwareKey *hwkey = _keymapper->findHardwareKey(state);
 		if (hwkey) {
 			_activeRemapAction->mapKey(hwkey);
 			// TODO:   _activeRemapAction->getParent()->saveMappings();
@@ -235,7 +235,7 @@
 	if (_activeKeymaps->size() > 0 && _kmPopUp->getSelected() == 0) {
 		// load active keymaps
 
-		List<const HardwareKey*> freeKeys (_keymapper->getHardwareKeySet()->getHardwareKeys());
+		List<const HardwareKey*> freeKeys (_keymapper->getHardwareKeys());
 
 		// add most active keymap's keys
 		Keymapper::MapRecord top = _activeKeymaps->top();


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list