Skip to content

Commit

Permalink
KEYMAPPER: Remove automapping dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsoliman committed Feb 21, 2012
1 parent d0c655f commit aa42d78
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 501 deletions.
5 changes: 2 additions & 3 deletions backends/keymapper/action.cpp
Expand Up @@ -28,9 +28,8 @@

namespace Common {

Action::Action(Keymap *boss, const char *i, String des, ActionType typ,
KeyType prefKey, int pri, int flg)
: _boss(boss), description(des), type(typ), preferredKey(prefKey),
Action::Action(Keymap *boss, const char *i, String des, int pri, int flg)
: _boss(boss), description(des),
priority(pri), flags(flg), _hwKey(0) {
assert(i);
assert(_boss);
Expand Down
5 changes: 0 additions & 5 deletions backends/keymapper/action.h
Expand Up @@ -27,7 +27,6 @@

#ifdef ENABLE_KEYMAPPER

#include "backends/keymapper/types.h"
#include "common/events.h"
#include "common/func.h"
#include "common/list.h"
Expand All @@ -54,8 +53,6 @@ struct Action {

/** Events to be sent when mapped key is pressed */
List<Event> events;
ActionType type;
KeyType preferredKey;
int priority;
int group;
int flags;
Expand All @@ -67,8 +64,6 @@ struct Action {

public:
Action(Keymap *boss, const char *id, String des = "",
ActionType typ = kGenericActionType,
KeyType prefKey = kGenericKeyType,
int pri = 0, int flg = 0 );

void addEvent(const Event &evt) {
Expand Down
12 changes: 3 additions & 9 deletions backends/keymapper/hardware-key.h
Expand Up @@ -27,7 +27,6 @@

#ifdef ENABLE_KEYMAPPER

#include "backends/keymapper/types.h"
#include "common/textconsole.h"

namespace Common {
Expand All @@ -50,12 +49,8 @@ struct HardwareKey {
*/
KeyState key;

KeyType type;
ActionType preferredAction;

HardwareKey(const char *i, KeyState ky = KeyState(), String desc = "",
KeyType typ = kGenericKeyType, ActionType prefAct = kGenericActionType)
: key(ky), description(desc), type(typ), preferredAction(prefAct) {
HardwareKey(const char *i, KeyState ky = KeyState(), String desc = "")
: key(ky), description(desc) {
assert(i);
Common::strlcpy(hwKeyId, i, HWKEY_ID_SIZE);
}
Expand All @@ -69,7 +64,6 @@ struct KeyTableEntry {
KeyCode keycode;
uint16 ascii;
const char *desc;
KeyType preferredAction;
bool shiftable;
};

Expand Down Expand Up @@ -170,7 +164,7 @@ class HardwareKeySet {
snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc);
}

addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc, key->preferredAction ));
addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc));
}
}
}
Expand Down
141 changes: 0 additions & 141 deletions backends/keymapper/keymap.cpp
Expand Up @@ -212,147 +212,6 @@ bool Keymap::isComplete(const HardwareKeySet *hwKeys) {
return allMapped || (numberMapped == hwKeys->size());
}

// TODO:
// - current weakness:
// - if an action finds a key with required type but a parent action with
// higher priority is using it, that key is never used
void Keymap::automaticMapping(HardwareKeySet *hwKeys) {
#if 0 //disabling the broken automapper for now
// Create copies of action and key lists.
List<Action *> actions(_actions);
List<const HardwareKey *> keys(hwKeys->getHardwareKeys());

List<Action *>::iterator actIt;
List<const HardwareKey *>::iterator keyIt, selectedKey;

// Remove actions and keys from local lists that have already been mapped.
actIt = actions.begin();

while (actIt != actions.end()) {
Action *act = *actIt;
const HardwareKey *key = act->getMappedKey();

if (key) {
keys.remove(key);
actIt = actions.erase(actIt);
} else {
++actIt;
}
}

// Sort remaining actions by priority.
ActionPriorityComp priorityComp;
sort(actions.begin(), actions.end(), priorityComp);

// First mapping pass:
// - Match if a key's preferred action type is the same as the action's
// type, or vice versa.
// - Priority is given to:
// - keys that match action types over key types.
// - keys that have not been used by parent maps.
// - If a key has been used by a parent map the new action must have a
// higher priority than the parent action.
// - As soon as the number of skipped actions equals the number of keys
// remaining we stop matching. This means that the second pass will assign keys
// to these higher priority skipped actions.
uint skipped = 0;
actIt = actions.begin();

while (actIt != actions.end() && skipped < keys.size()) {
selectedKey = keys.end();
int matchRank = 0;
Action *act = *actIt;

for (keyIt = keys.begin(); keyIt != keys.end(); ++keyIt) {
if ((*keyIt)->preferredAction == act->type && act->type != kGenericActionType) {
Action *parentAct = getParentMappedAction((*keyIt)->key);

if (!parentAct) {
selectedKey = keyIt;
break;
} else if (parentAct->priority <= act->priority && matchRank < 3) {
selectedKey = keyIt;
matchRank = 3;
}
} else if ((*keyIt)->type == act->preferredKey && act->preferredKey != kGenericKeyType && matchRank < 2) {
Action *parentAct = getParentMappedAction((*keyIt)->key);

if (!parentAct) {
selectedKey = keyIt;
matchRank = 2;
} else if (parentAct->priority <= act->priority && matchRank < 1) {
selectedKey = keyIt;
matchRank = 1;
}
}
}
if (selectedKey != keys.end()) {
// Map action and delete action & key from local lists.
act->mapKey(*selectedKey);
keys.erase(selectedKey);
actIt = actions.erase(actIt);
} else {
// Skip action (will be mapped in next pass).
++actIt;
++skipped;
}
}

// Second mapping pass:
// - Maps any remaining actions to keys
// - priority given to:
// - keys that have no parent action
// - keys whose parent action has lower priority than the new action
// - keys whose parent action has the lowest priority
// - is guaranteed to match a key if they are not all used up
for (actIt = actions.begin(); actIt != actions.end(); ++actIt) {
selectedKey = keys.end();

int matchRank = 0;
int lowestPriority = 0;
Action *act = *actIt;

for (keyIt = keys.begin(); keyIt != keys.end(); ++keyIt) {
Action *parentAct = getParentMappedAction((*keyIt)->key);

if (!parentAct) {
selectedKey = keyIt;
break;
} else if (matchRank < 2) {
if (parentAct->priority <= act->priority) {
matchRank = 2;
selectedKey = keyIt;
} else if (parentAct->priority < lowestPriority || matchRank == 0) {
matchRank = 1;
lowestPriority = parentAct->priority;
selectedKey = keyIt;
}
}
}

if (selectedKey != keys.end()) {
act->mapKey(*selectedKey);
keys.erase(selectedKey);
} else {// no match = no keys left
break;
}
}
#endif
}

Action *Keymap::getParentMappedAction(KeyState key) {
if (_parent) {
Action *act = _parent->getMappedAction(key);

if (act)
return act;
else
return _parent->getParentMappedAction(key);
} else {
return 0;
}
}

} // End of namespace Common

#endif // #ifdef ENABLE_KEYMAPPER
11 changes: 1 addition & 10 deletions backends/keymapper/keymap.h
Expand Up @@ -52,7 +52,7 @@ template<> struct Hash<KeyState>

class Keymap {
public:
Keymap(const String& name, Keymap *parent = 0) : _name(name), _parent(parent) {}
Keymap(const String& name) : _name(name) {}
Keymap(const Keymap& km);
~Keymap();

Expand Down Expand Up @@ -91,17 +91,13 @@ class Keymap {
*/
void saveMappings();


void automaticMapping(HardwareKeySet *hwKeys);

/**
* Returns true if all UserAction's in Keymap are mapped, or,
* all HardwareKey's from the given set have been used up.
*/
bool isComplete(const HardwareKeySet *hwKeys);

const String& getName() { return _name; }
Keymap *getParent() { return _parent; }

private:
friend struct Action;
Expand Down Expand Up @@ -131,12 +127,7 @@ class Keymap {
Action *findAction(const char *id);
const Action *findAction(const char *id) const;

void internalMapKey(Action *action, HardwareKey *hwKey);

Action *getParentMappedAction(KeyState key);

String _name;
Keymap *_parent;
List<Action *> _actions;
HashMap<KeyState, Action *> _keymap;
ConfigManager::Domain *_configDomain;
Expand Down
1 change: 0 additions & 1 deletion backends/keymapper/keymapper.cpp
Expand Up @@ -104,7 +104,6 @@ void Keymapper::initKeymap(Domain &domain, Keymap *map) {
map->loadMappings(_hardwareKeys);

if (map->isComplete(_hardwareKeys) == false) {
map->automaticMapping(_hardwareKeys);
map->saveMappings();
ConfMan.flushToDisk();
}
Expand Down
74 changes: 0 additions & 74 deletions backends/keymapper/types.h

This file was deleted.

7 changes: 2 additions & 5 deletions backends/platform/android/events.cpp
Expand Up @@ -227,17 +227,14 @@ void OSystem_Android::setupKeymapper() {
HardwareKeySet *keySet = new HardwareKeySet();

keySet->addHardwareKey(
new HardwareKey("n", KeyState(KEYCODE_n), "n (vk)",
kTriggerLeftKeyType,
kVirtualKeyboardActionType));
new HardwareKey("n", KeyState(KEYCODE_n), "n (vk)"));

mapper->registerHardwareKeySet(keySet);

Keymap *globalMap = new Keymap(kGlobalKeymapName);
Action *act;

act = new Action(globalMap, "VIRT", "Display keyboard",
kVirtualKeyboardActionType);
act = new Action(globalMap, "VIRT", "Display keyboard");
act->addKeyEvent(KeyState(KEYCODE_F7, ASCII_F7, 0));

mapper->addGlobalKeymap(globalMap);
Expand Down

0 comments on commit aa42d78

Please sign in to comment.