diff options
author | Tarek Soliman | 2012-02-09 01:26:11 -0600 |
---|---|---|
committer | Tarek Soliman | 2012-02-12 13:28:13 -0600 |
commit | 52da780fbc10200b91d92e7cd04932b101c7539b (patch) | |
tree | 6f7ace246c4a711ec760c35d102492e0c3da75d3 /backends | |
parent | 0ba3335674dcd88d34d1d2ab8453658191d31c38 (diff) | |
download | scummvm-rg350-52da780fbc10200b91d92e7cd04932b101c7539b.tar.gz scummvm-rg350-52da780fbc10200b91d92e7cd04932b101c7539b.tar.bz2 scummvm-rg350-52da780fbc10200b91d92e7cd04932b101c7539b.zip |
KEYMAPPER: Refactor HardwareKeySet generation
Diffstat (limited to 'backends')
-rw-r--r-- | backends/keymapper/hardware-key.h | 66 | ||||
-rw-r--r-- | backends/platform/sdl/hardwarekeys.cpp | 50 | ||||
-rw-r--r-- | backends/platform/sdl/sdl.h | 5 |
3 files changed, 72 insertions, 49 deletions
diff --git a/backends/keymapper/hardware-key.h b/backends/keymapper/hardware-key.h index 32df042525..713b086791 100644 --- a/backends/keymapper/hardware-key.h +++ b/backends/keymapper/hardware-key.h @@ -32,7 +32,6 @@ namespace Common { - #define HWKEY_ID_SIZE (30) /** @@ -62,6 +61,27 @@ struct HardwareKey { } }; +/** + * Entry in a static table of available non-modifier keys + */ +struct KeyTableEntry { + const char *hwId; + KeyCode keycode; + uint16 ascii; + const char *desc; + KeyType preferredAction; + bool shiftable; +}; + +/** + * Entry in a static table of available key modifiers + */ +struct ModifierTableEntry { + byte flag; + const char *id; + const char *desc; + bool shiftable; +}; /** * Simple class to encapsulate a device's set of HardwareKeys. @@ -71,6 +91,17 @@ struct HardwareKey { class HardwareKeySet { public: + /** + * Add hardware keys to the set out of key and modifier tables. + * @param keys table of available keys + * @param modifiers table of available modifiers + */ + HardwareKeySet(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) { + addHardwareKeys(keys, modifiers); + } + + HardwareKeySet() { } + virtual ~HardwareKeySet() { List<const HardwareKey*>::const_iterator it; @@ -111,6 +142,38 @@ public: return _keys.size(); } + /** + * Add hardware keys to the set out of key and modifier tables. + * @param keys table of available keys + * @param modifiers table of available modifiers + */ + void addHardwareKeys(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) { + const KeyTableEntry *key; + const ModifierTableEntry *mod; + char fullKeyId[50]; + char fullKeyDesc[100]; + uint16 ascii; + + for (mod = modifiers; mod->id; mod++) { + for (key = keys; key->hwId; key++) { + ascii = key->ascii; + + if (mod->shiftable && key->shiftable) { + snprintf(fullKeyId, 50, "%s%c", mod->id, toupper(key->hwId[0])); + snprintf(fullKeyDesc, 100, "%s%c", mod->desc, toupper(key->desc[0])); + ascii = toupper(key->ascii); + } else if (mod->shiftable) { + snprintf(fullKeyId, 50, "S+%s%s", mod->id, key->hwId); + snprintf(fullKeyDesc, 100, "Shift+%s%s", mod->desc, key->desc); + } else { + snprintf(fullKeyId, 50, "%s%s", mod->id, key->hwId); + snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc); + } + + addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc, key->preferredAction )); + } + } + } private: @@ -128,7 +191,6 @@ private: List<const HardwareKey*> _keys; }; - } // End of namespace Common #endif // #ifdef ENABLE_KEYMAPPER diff --git a/backends/platform/sdl/hardwarekeys.cpp b/backends/platform/sdl/hardwarekeys.cpp index 9a33e357da..3e9378602e 100644 --- a/backends/platform/sdl/hardwarekeys.cpp +++ b/backends/platform/sdl/hardwarekeys.cpp @@ -28,16 +28,7 @@ using namespace Common; -struct Key { - const char *hwId; - KeyCode keycode; - uint16 ascii; - const char *desc; - KeyType preferredAction; - bool shiftable; -}; - -static const Key keys[] = { +static const KeyTableEntry sdlKeys[] = { {"BACKSPACE", KEYCODE_BACKSPACE, ASCII_BACKSPACE, "Backspace", kActionKeyType, false}, {"TAB", KEYCODE_TAB, ASCII_TAB, "Tab", kActionKeyType, false}, {"CLEAR", KEYCODE_CLEAR, 0, "Clear", kActionKeyType, false}, @@ -173,14 +164,7 @@ static const Key keys[] = { {0, KEYCODE_INVALID, 0, 0, kGenericKeyType, false} }; -struct Mod { - byte flag; - const char *id; - const char *desc; - bool shiftable; -}; - -static const Mod modifiers[] = { +static const ModifierTableEntry sdlModifiers[] = { { 0, "", "", false }, { KBD_CTRL, "C+", "Ctrl+", false }, { KBD_ALT, "A+", "Alt+", false }, @@ -195,35 +179,7 @@ static const Mod modifiers[] = { Common::HardwareKeySet *OSystem_SDL::getHardwareKeySet() { #ifdef ENABLE_KEYMAPPER - HardwareKeySet *keySet = new HardwareKeySet(); - const Key *key; - const Mod *mod; - char fullKeyId[50]; - char fullKeyDesc[100]; - uint16 ascii; - - for (mod = modifiers; mod->id; mod++) { - for (key = keys; key->hwId; key++) { - ascii = key->ascii; - - if (mod->shiftable && key->shiftable) { - snprintf(fullKeyId, 50, "%s%c", mod->id, toupper(key->hwId[0])); - snprintf(fullKeyDesc, 100, "%s%c", mod->desc, toupper(key->desc[0])); - ascii = toupper(key->ascii); - } else if (mod->shiftable) { - snprintf(fullKeyId, 50, "S+%s%s", mod->id, key->hwId); - snprintf(fullKeyDesc, 100, "Shift+%s%s", mod->desc, key->desc); - } else { - snprintf(fullKeyId, 50, "%s%s", mod->id, key->hwId); - snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc); - } - - keySet->addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc, key->preferredAction )); - } - } - - return keySet; - + return new HardwareKeySet(sdlKeys, sdlModifiers); #else return 0; #endif diff --git a/backends/platform/sdl/sdl.h b/backends/platform/sdl/sdl.h index 22d79dbfe7..6c84c5c26a 100644 --- a/backends/platform/sdl/sdl.h +++ b/backends/platform/sdl/sdl.h @@ -30,6 +30,11 @@ #include "backends/events/sdl/sdl-events.h" #include "backends/log/log.h" +namespace Common { +struct KeyTableEntry; +struct ModifierTableEntry; +} + /** * Base OSystem class for all SDL ports. */ |