KEYMAPPER: Add non-key inputs to HardwareInput

This commit is contained in:
Tarek Soliman 2012-02-24 13:55:48 -06:00
parent 101ec2b885
commit 4ee1a3acea
6 changed files with 97 additions and 28 deletions

View file

@ -209,16 +209,33 @@ const HardwareInput *HardwareInputSet::findHardwareInput(String id) const {
return 0;
}
const HardwareInput *HardwareInputSet::findHardwareInput(const HardwareInputCode code) const {
List<const HardwareInput *>::const_iterator it;
for (it = _inputs.begin(); it != _inputs.end(); ++it) {
const HardwareInput *entry = *it;
if (entry->type == kHardwareInputTypeGeneric && entry->inputCode == code)
return entry;
}
return 0;
}
const HardwareInput *HardwareInputSet::findHardwareInput(const KeyState& keystate) const {
List<const HardwareInput *>::const_iterator it;
for (it = _inputs.begin(); it != _inputs.end(); ++it) {
if ((*it)->key == keystate)
return (*it);
const HardwareInput *entry = *it;
if (entry->type == kHardwareInputTypeKeyboard && entry->key == keystate)
return entry;
}
return 0;
}
void HardwareInputSet::addHardwareInputs(const HardwareInputTableEntry inputs[]) {
for (const HardwareInputTableEntry *entry = inputs; entry->hwId; ++entry)
addHardwareInput(new HardwareInput(entry->hwId, entry->code, entry->desc));
}
void HardwareInputSet::addHardwareInputs(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) {
const KeyTableEntry *key;
const ModifierTableEntry *mod;
@ -247,10 +264,6 @@ void HardwareInputSet::addHardwareInputs(const KeyTableEntry keys[], const Modif
}
}
void HardwareInputSet::addHardwareInputs(const KeyTableEntry keys[]) {
addHardwareInputs(keys, defaultModifiers);
}
void HardwareInputSet::removeHardwareInput(const HardwareInput *input) {
if (!input)
return;
@ -259,7 +272,16 @@ void HardwareInputSet::removeHardwareInput(const HardwareInput *input) {
for (it = _inputs.begin(); it != _inputs.end(); ++it) {
const HardwareInput *entry = (*it);
if (entry->id == input->id || entry->key == input->key) {
bool match = false;
if (entry->id == input->id)
match = true;
else if (input->type == entry->type) {
if (input->type == kHardwareInputTypeGeneric && input->inputCode == entry->inputCode)
match = true;
else if (input->type == kHardwareInputTypeKeyboard && input->key == entry->key)
match = true;
}
if (match) {
debug(7, "Removing hardware input [%s] (%s) because it matches [%s] (%s)", entry->id.c_str(), entry->description.c_str(), input->id.c_str(), input->description.c_str());
delete entry;
_inputs.erase(it);

View file

@ -34,6 +34,15 @@
namespace Common {
typedef uint32 HardwareInputCode;
enum HardwareInputType {
/** Input that sends single events */
kHardwareInputTypeGeneric,
/** Input that usually send -up and -down events */
kHardwareInputTypeKeyboard
};
/**
* Describes an available hardware input
*/
@ -44,14 +53,33 @@ struct HardwareInput {
/** Human readable description */
String description;
const HardwareInputType type;
/**
* The KeyState that is generated by the back-end
* when this hardware key is pressed.
*/
* A platform specific unique identifier for an input event
* generated when this input is triggered.
* This is only relevant when type == kHardwareInputTypeGeneric
*/
HardwareInputCode inputCode;
/**
* The KeyState that is generated by the back-end
* when this hardware key is pressed.
* This is only relevant when type == kHardwareInputTypeKeyboard
*/
KeyState key;
HardwareInput(String i, KeyState ky = KeyState(), String desc = "")
: id(i), key(ky), description(desc) { }
HardwareInput(String i, HardwareInputCode ic = 0, String desc = "")
: id(i), inputCode(ic), description(desc), type(kHardwareInputTypeGeneric) { }
HardwareInput(String i, KeyState ky, String desc = "")
: id(i), key(ky), description(desc), type(kHardwareInputTypeKeyboard) { }
};
struct HardwareInputTableEntry {
const char *hwId;
HardwareInputCode code;
const char *desc;
};
/**
@ -97,12 +125,20 @@ public:
const HardwareInput *findHardwareInput(String id) const;
const HardwareInput *findHardwareInput(const HardwareInputCode code) const;
const HardwareInput *findHardwareInput(const KeyState& keystate) const;
const List<const HardwareInput *> &getHardwareInputs() const { return _inputs; }
uint size() const { return _inputs.size(); }
/**
* Add hardware inputs to the set out of a table.
* @param inputs table of available inputs
*/
void addHardwareInputs(const HardwareInputTableEntry inputs[]);
/**
* Add hardware inputs to the set out of key and modifier tables.
* @param keys table of available keys
@ -110,13 +146,6 @@ public:
*/
void addHardwareInputs(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]);
/**
* Add hardware inputs to the set out of a key table.
* The default modifiers are applied to the key entries
* @param keys table of available keys
*/
void addHardwareInputs(const KeyTableEntry keys[]);
void removeHardwareInput(const HardwareInput *input);
private:

View file

@ -39,7 +39,8 @@ Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap(), _configDoma
for (it = _actions.begin(); it != _actions.end(); ++it) {
const HardwareInput *hwInput = (*it)->getMappedInput();
if (hwInput) {
//FIXME: Add support for kHardwareInputTypeGeneric
if (hwInput && hwInput->type == kHardwareInputTypeKeyboard) {
_keymap[hwInput->key] = *it;
}
}
@ -62,21 +63,25 @@ void Keymap::addAction(Action *action) {
void Keymap::registerMapping(Action *action, const HardwareInput *hwInput) {
HashMap<KeyState, Action *>::iterator it;
it = _keymap.find(hwInput->key);
//FIXME: Add support for kHardwareInputTypeGeneric
if (hwInput->type == kHardwareInputTypeKeyboard) {
it = _keymap.find(hwInput->key);
// if key is already mapped to a different action then un-map it
if (it != _keymap.end() && action != it->_value) {
it->_value->mapInput(0);
// if key is already mapped to a different action then un-map it
if (it != _keymap.end() && action != it->_value) {
it->_value->mapInput(0);
}
_keymap[hwInput->key] = action;
}
_keymap[hwInput->key] = action;
}
void Keymap::unregisterMapping(Action *action) {
const HardwareInput *hwInput = action->getMappedInput();
if (hwInput) {
_keymap.erase(hwInput->key);
//FIXME: Add support for kHardwareInputTypeGeneric
if (hwInput->type == kHardwareInputTypeKeyboard)
_keymap.erase(hwInput->key);
}
}

View file

@ -295,6 +295,10 @@ const HardwareInput *Keymapper::findHardwareInput(const KeyState& key) {
return (_hardwareInputs) ? _hardwareInputs->findHardwareInput(key) : 0;
}
const HardwareInput *Keymapper::findHardwareInput(const HardwareInputCode code) {
return (_hardwareInputs) ? _hardwareInputs->findHardwareInput(code) : 0;
}
} // End of namespace Common
#endif // #ifdef ENABLE_KEYMAPPER

View file

@ -172,6 +172,11 @@ public:
*/
const HardwareInput *findHardwareInput(const KeyState& key);
/**
* Return a HardwareInput pointer for the given input code
*/
const HardwareInput *findHardwareInput(const HardwareInputCode code);
Domain& getGlobalDomain() { return _globalDomain; }
Domain& getGameDomain() { return _gameDomain; }
const Stack<MapRecord>& getActiveStack() const { return _activeMaps; }

View file

@ -354,9 +354,13 @@ void RemapDialog::loadKeymap() {
Keymapper::MapRecord mr = activeKeymaps[i];
debug(3, "RemapDialog::loadKeymap keymap: %s", mr.keymap->getName().c_str());
List<const HardwareInput *>::iterator inputIt = freeInputs.begin();
const HardwareInput *input = *inputIt;
while (inputIt != freeInputs.end()) {
Action *act = mr.keymap->getMappedAction((*inputIt)->key);
Action *act = 0;
// FIXME: Add support for kHardwareInputTypeGeneric
if (input->type == kHardwareInputTypeKeyboard)
act = mr.keymap->getMappedAction(input->key);
if (act) {
ActionInfo info = {act, true, act->description + " (" + mr.keymap->getName() + ")"};