From 5493aebf0d303f954da49d3f8b775d6e004ed94e Mon Sep 17 00:00:00 2001 From: Cyp Date: Sat, 20 May 2017 20:07:29 +0200 Subject: [PATCH] keymap: Refactor. Use std::list, remove dead code. --- src/keyedit.cpp | 30 ++--- src/keymap.cpp | 315 +++++++++--------------------------------------- src/keymap.h | 22 +--- 3 files changed, 70 insertions(+), 297 deletions(-) diff --git a/src/keyedit.cpp b/src/keyedit.cpp index e72d0c77fc5..fef7987cbb5 100644 --- a/src/keyedit.cpp +++ b/src/keyedit.cpp @@ -136,7 +136,7 @@ static bool pushedKeyCombo(KEY_CODE subkey) keyReAssignMapping(metakey, subkey, KEY_IGNORE, (KEY_CODE)KEY_MAXSCAN); /* Try and see if its there already - damn well should be! */ - psMapping = keyGetMappingFromFunction((void *)selectedKeyMap->function); + psMapping = keyGetMappingFromFunction(selectedKeyMap->function); /* Cough if it's not there */ ASSERT_OR_RETURN(false, psMapping != nullptr, "Trying to patch a non-existant function mapping - whoop whoop!!!"); @@ -273,7 +273,7 @@ static void displayKeyMap(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset) // draw name iV_SetTextColour(WZCOL_FORM_TEXT); - iV_DrawText(_(psMapping->pName), x + 2, y + (psWidget->height() / 2) + 3, font_regular); + iV_DrawText(_(psMapping->name.c_str()), x + 2, y + (psWidget->height() / 2) + 3, font_regular); // draw binding keyMapToString(sKey, psMapping); @@ -286,11 +286,6 @@ static void displayKeyMap(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset) iV_DrawText(sKey, x + 364, y + (psWidget->height() / 2) + 3, font_regular); } -static bool keyMappingSort(KEY_MAPPING const *a, KEY_MAPPING const *b) -{ - return strcmp(a->pName, b->pName) < 0; -} - // //////////////////////////////////////////////////////////////////////////// bool startKeyMapEditor(bool first) { @@ -328,14 +323,16 @@ bool startKeyMapEditor(bool first) //Put the buttons on it std::vector mappings; - for (KEY_MAPPING *m = keyMappings; m != nullptr; m = m->psNext) + for (KEY_MAPPING &m : keyMappings) { - if (m->status != KEYMAP__DEBUG && m->status != KEYMAP___HIDE) // if it's not a debug mapping.. + if (m.status != KEYMAP__DEBUG && m.status != KEYMAP___HIDE) // if it's not a debug mapping.. { - mappings.push_back(m); + mappings.push_back(&m); } } - std::sort(mappings.begin(), mappings.end(), keyMappingSort); + std::sort(mappings.begin(), mappings.end(), [](KEY_MAPPING *a, KEY_MAPPING *b) { + return a->name < b->name; + }); /* Add our first mapping to the form */ /* Now add the others... */ for (std::vector::const_iterator i = mappings.begin(); i != mappings.end(); ++i) @@ -359,7 +356,6 @@ bool startKeyMapEditor(bool first) // FIXME: Use the endian-safe physfs functions. bool saveKeyMap() { - KEY_MAPPING *psMapping; SDWORD count; char name[128]; PHYSFS_file *pfile; @@ -384,19 +380,15 @@ bool saveKeyMap() } // write out number of entries. - count = 0; - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) - { - count++; - } + count = keyMappings.size(); WRITE(&count, sizeof(count)); WRITE(&keymapVersion, 8); - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) + for (auto psMapping = keyMappings.begin(); psMapping != keyMappings.end(); ++psMapping) { // save this map. // name - sstrcpy(name, psMapping->pName); + sstrcpy(name, psMapping->name.c_str()); WRITE(&name, 128); WRITE(&psMapping->status, sizeof(KEY_STATUS)); // status diff --git a/src/keymap.cpp b/src/keymap.cpp index a080c274d56..0047e65c080 100644 --- a/src/keymap.cpp +++ b/src/keymap.cpp @@ -42,23 +42,15 @@ #include "keyedit.h" static UDWORD asciiKeyCodeToTable(KEY_CODE code); +static KEY_CODE getQwertyKey(); // ---------------------------------------------------------------------------------- -KEY_MAPPING *keyGetMappingFromFunction(void *function) +KEY_MAPPING *keyGetMappingFromFunction(void (*function)()) { - KEY_MAPPING *psMapping, *psReturn; - - for (psMapping = keyMappings, psReturn = nullptr; - psMapping && !psReturn; - psMapping = psMapping->psNext) - { - if ((void *)psMapping->function == function) - { - psReturn = psMapping; - } - } - - return (psReturn); + auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [function](KEY_MAPPING const &mapping) { + return mapping.function == function; + }); + return mapping != keyMappings.end()? &*mapping : nullptr; } // ---------------------------------------------------------------------------------- /* Some stuff allowing the user to add key mappings themselves */ @@ -79,14 +71,10 @@ static bool bWantDebugMappings[MAX_PLAYERS] = {false}; // ---------------------------------------------------------------------------------- /* The linked list of present key mappings */ -KEY_MAPPING *keyMappings; - -/* Holds number of active mappings */ -UDWORD numActiveMappings; +std::list keyMappings; /* Last meta and sub key that were recorded */ static KEY_CODE lastMetaKey, lastSubKey; -static bool bKeyProcessing = true; static void kf_NOOP() {} @@ -272,19 +260,16 @@ _keymapsave keyMapSaveTable[] = these will be read in from a .cfg file customisable by the player from an in-game menu */ -void keyInitMappings(bool bForceDefaults) +void keyInitMappings(bool bForceDefaults) { - UDWORD i; - keyMappings = nullptr; - numActiveMappings = 0; - bKeyProcessing = true; + keyMappings.clear(); for (unsigned n = 0; n < MAX_PLAYERS; ++n) { processDebugMappings(n, false); } - for (i = 0; i < NUM_QWERTY_KEYS; i++) + for (unsigned i = 0; i < NUM_QWERTY_KEYS; i++) { qwertyKeyMappings[i].psMapping = nullptr; } @@ -481,25 +466,12 @@ void keyInitMappings(bool bForceDefaults) //bool keyAddMapping(KEY_CODE metaCode, KEY_CODE subCode, KEY_ACTION action,void *function, char *name) KEY_MAPPING *keyAddMapping(KEY_STATUS status, KEY_CODE metaCode, KEY_CODE subCode, KEY_ACTION action, void (*pKeyMapFunc)(), const char *name) { - KEY_MAPPING *newMapping; - /* Get some memory for our binding */ - newMapping = (KEY_MAPPING *)malloc(sizeof(KEY_MAPPING)); - if (newMapping == nullptr) - { - debug(LOG_FATAL, "keyAddMapping: Out of memory!"); - abort(); - return nullptr; - } + keyMappings.emplace_front(); + KEY_MAPPING *newMapping = &keyMappings.front(); /* Copy over the name */ - newMapping->pName = strdup(name); - if (newMapping->pName == nullptr) - { - debug(LOG_FATAL, "keyAddMapping: Out of memory!"); - abort(); - return nullptr; - } + newMapping->name = name; /* Fill up our entries, first the ones that activate it */ newMapping->metaKeyCode = metaCode; @@ -535,117 +507,40 @@ KEY_MAPPING *keyAddMapping(KEY_STATUS status, KEY_CODE metaCode, KEY_CODE subCod newMapping->altMetaKeyCode = KEY_RMETA; } - /* Set it to be active */ - newMapping->active = true; - /* Add it to the start of the list */ - newMapping->psNext = keyMappings; - keyMappings = newMapping; - numActiveMappings++; - - return (newMapping); -} - -// ---------------------------------------------------------------------------------- -/* Removes a mapping from the list specified by the key codes */ -bool keyRemoveMapping(KEY_CODE metaCode, KEY_CODE subCode) -{ - KEY_MAPPING *mapping; - - mapping = keyFindMapping(metaCode, subCode); - return (keyRemoveMappingPt(mapping)); + return newMapping; } // ---------------------------------------------------------------------------------- /* Returns a pointer to a mapping if it exists - NULL otherwise */ KEY_MAPPING *keyFindMapping(KEY_CODE metaCode, KEY_CODE subCode) { - KEY_MAPPING *psCurr; - - /* See if we can find it */ - for (psCurr = keyMappings; psCurr != nullptr; psCurr = psCurr->psNext) - { - if (psCurr->metaKeyCode == metaCode && psCurr->subKeyCode == subCode) - { - return (psCurr); - } - } - return (nullptr); + auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [metaCode, subCode](KEY_MAPPING const &mapping) { + return mapping.metaKeyCode == metaCode && mapping.subKeyCode == subCode; + }); + return mapping != keyMappings.end()? &*mapping : nullptr; } // ---------------------------------------------------------------------------------- /* clears the mappings list and frees the memory */ -void keyClearMappings() +void keyClearMappings() { - while (keyMappings) - { - keyRemoveMappingPt(keyMappings); - } + keyMappings.clear(); } // ---------------------------------------------------------------------------------- /* Removes a mapping specified by a pointer */ -bool keyRemoveMappingPt(KEY_MAPPING *psToRemove) +static bool keyRemoveMappingPt(KEY_MAPPING *psToRemove) { - KEY_MAPPING *psPrev, *psCurr; - - if (psToRemove == nullptr) + auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [psToRemove](KEY_MAPPING const &mapping) { + return &mapping == psToRemove; + }); + if (mapping != keyMappings.end()) { - return (false); + keyMappings.erase(mapping); + return true; } - - if (psToRemove == keyMappings && keyMappings->psNext == nullptr) - { - if (keyMappings->pName) - { - free(keyMappings->pName); // ffs - } - free(keyMappings); - keyMappings = nullptr; - numActiveMappings = 0; - return (true); - } - - /* See if we can find it */ - for (psPrev = nullptr, psCurr = keyMappings; - psCurr != nullptr && psCurr != psToRemove; - psPrev = psCurr, psCurr = psCurr->psNext) - { - /*NOP*/ - } - - /* If it was found... */ - if (psCurr == psToRemove) - { - /* See if it was the first element */ - if (psPrev) - { - /* It wasn't */ - psPrev->psNext = psCurr->psNext; - } - else - { - /* It was */ - keyMappings = psCurr->psNext; - } - /* Free up the memory, first for the string */ - if (psCurr->pName) - { - free(psCurr->pName); // only free it if it was allocated in the first place (ffs) - } - /* and then for the mapping itself */ - free(psCurr); - numActiveMappings--; - return (true); - } - return (false); -} - -// ---------------------------------------------------------------------------------- -/* Just returns how many are active */ -UDWORD getNumMappings() -{ - return (numActiveMappings); + return false; } @@ -691,11 +586,8 @@ static bool checkQwertyKeys() /* Manages update of all the active function mappings */ void keyProcessMappings(bool bExclude) { - KEY_MAPPING *keyToProcess; - bool bMetaKeyDown; - /* Bomb out if there are none */ - if (!keyMappings || !numActiveMappings || !bKeyProcessing) + if (keyMappings.empty()) { return; } @@ -704,27 +596,14 @@ void keyProcessMappings(bool bExclude) (void) checkQwertyKeys(); /* Check for the meta keys */ - if (keyDown(KEY_LCTRL) || keyDown(KEY_RCTRL) || keyDown(KEY_LALT) + bool bMetaKeyDown = keyDown(KEY_LCTRL) || keyDown(KEY_RCTRL) || keyDown(KEY_LALT) || keyDown(KEY_RALT) || keyDown(KEY_LSHIFT) || keyDown(KEY_RSHIFT) - || keyDown(KEY_LMETA) || keyDown(KEY_RMETA)) - { - bMetaKeyDown = true; - } - else - { - bMetaKeyDown = false; - } + || keyDown(KEY_LMETA) || keyDown(KEY_RMETA); /* Run through all our mappings */ - for (keyToProcess = keyMappings; keyToProcess != nullptr; keyToProcess = keyToProcess->psNext) + for (auto keyToProcess = keyMappings.begin(); keyToProcess != keyMappings.end(); ++keyToProcess) { - /* We haven't acted upon it */ - if (!keyToProcess->active) - { - /* Get out if it's inactive */ - break; - } - /* Skip innappropriate ones when necessary */ + /* Skip inappropriate ones when necessary */ if (bExclude && keyToProcess->status != KEYMAP_ALWAYS_PROCESS) { break; @@ -883,11 +762,11 @@ static void keyShowMapping(KEY_MAPPING *psMapping) keyScanToString(psMapping->subKeyCode, (char *)&asciiSub, 20); if (onlySub) { - CONPRINTF(ConsoleString, (ConsoleString, "%s - %s", asciiSub, _(psMapping->pName))); + CONPRINTF(ConsoleString, (ConsoleString, "%s - %s", asciiSub, _(psMapping->name.c_str()))); } else { - CONPRINTF(ConsoleString, (ConsoleString, "%s and %s - %s", asciiMeta, asciiSub, _(psMapping->pName))); + CONPRINTF(ConsoleString, (ConsoleString, "%s and %s - %s", asciiMeta, asciiSub, _(psMapping->name.c_str()))); } debug(LOG_INPUT, "Received %s from Console", ConsoleString); } @@ -896,11 +775,9 @@ static void keyShowMapping(KEY_MAPPING *psMapping) // this function isn't really module static - should be removed - debug only void keyShowMappings() { - KEY_MAPPING *psMapping; - - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) + for (auto &mapping : keyMappings) { - keyShowMapping(psMapping); + keyShowMapping(&mapping); } } @@ -908,77 +785,38 @@ void keyShowMappings() /* Returns the key code of the last sub key pressed - allows called functions to have a simple stack */ KEY_CODE getLastSubKey() { - return (lastSubKey); + return lastSubKey; } // ---------------------------------------------------------------------------------- /* Returns the key code of the last meta key pressed - allows called functions to have a simple stack */ KEY_CODE getLastMetaKey() { - return (lastMetaKey); -} - -// ---------------------------------------------------------------------------------- -/* Allows us to enable/disable the whole mapping system */ -void keyEnableProcessing(bool val) -{ - bKeyProcessing = val; -} - -// ---------------------------------------------------------------------------------- -/* Sets all mappings to be inactive */ -void keyAllMappingsInactive() -{ - KEY_MAPPING *psMapping; - - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) - { - psMapping->active = false; - } -} - -// ---------------------------------------------------------------------------------- -void keyAllMappingsActive() -{ - KEY_MAPPING *psMapping; - - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) - { - psMapping->active = true; - } -} - -// ---------------------------------------------------------------------------------- -/* Allows us to make active/inactive specific mappings */ -void keySetMappingStatus(KEY_MAPPING *psMapping, bool state) -{ - psMapping->active = state; + return lastMetaKey; } static const KEY_CODE qwertyCodes[26] = { // +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ - KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, + KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, // +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ // +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ - KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, + KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, // +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ // +---+ +---+ +---+ +---+ +---+ +---+ +---+ - KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M + KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M // +---+ +---+ +---+ +---+ +---+ +---+ +---+ }; /* Returns the key code of the first ascii key that its finds has been PRESSED */ -KEY_CODE getQwertyKey() +static KEY_CODE getQwertyKey() { - unsigned i; - - for (i = 0; i < ARRAY_SIZE(qwertyCodes); ++i) + for (KEY_CODE code : qwertyCodes) { - if (keyPressed(qwertyCodes[i])) + if (keyPressed(code)) { - return qwertyCodes[i]; // Top-, middle- or bottom-row key pressed. + return code; // Top-, middle- or bottom-row key pressed. } } @@ -1070,62 +908,19 @@ std::string getWantedDebugMappingStatuses(bool val) return ret; } // ---------------------------------------------------------------------------------- -bool keyReAssignMapping(KEY_CODE origMetaCode, KEY_CODE origSubCode, - KEY_CODE newMetaCode, KEY_CODE newSubCode) +bool keyReAssignMapping(KEY_CODE origMetaCode, KEY_CODE origSubCode, KEY_CODE newMetaCode, KEY_CODE newSubCode) { - KEY_MAPPING *psMapping; - bool bFound; - - for (psMapping = keyMappings, bFound = false; psMapping && !bFound; - psMapping = psMapping->psNext) + /* Find the original */ + if (KEY_MAPPING *psMapping = keyFindMapping(origMetaCode, origSubCode)) { - /* Find the original */ - if (psMapping->metaKeyCode == origMetaCode && psMapping->subKeyCode == origSubCode) + /* Not all can be remapped */ + if (psMapping->status != KEYMAP_ALWAYS || psMapping->status == KEYMAP_ALWAYS_PROCESS) { - /* Not all can be remapped */ - if (psMapping->status != KEYMAP_ALWAYS || psMapping->status == KEYMAP_ALWAYS_PROCESS) - { - psMapping->metaKeyCode = newMetaCode; - psMapping->subKeyCode = newSubCode; - bFound = true; - } + psMapping->metaKeyCode = newMetaCode; + psMapping->subKeyCode = newSubCode; + return true; } } - return (bFound); + return false; } - -// ---------------------------------------------------------------------------------- -KEY_MAPPING *getKeyMapFromName(char *pName) -{ - KEY_MAPPING *psMapping; - for (psMapping = keyMappings; psMapping; psMapping = psMapping->psNext) - { - if (strcmp(pName, psMapping->pName) == false) - { - return (psMapping); - } - } - return (nullptr); -} - -// ---------------------------------------------------------------------------------- -bool keyReAssignMappingName(char *pName, KEY_CODE newMetaCode, KEY_CODE newSubCode) -{ - KEY_MAPPING *psMapping; - - - psMapping = getKeyMapFromName(pName); - if (psMapping) - { - if (psMapping->status == KEYMAP_ASSIGNABLE) - { - (void)keyAddMapping(psMapping->status, newMetaCode, - newSubCode, psMapping->action, psMapping->function, psMapping->pName); - keyRemoveMappingPt(psMapping); - return (true); - } - } - return (false); -} -// ---------------------------------------------------------------------------------- diff --git a/src/keymap.h b/src/keymap.h index 145edbd37ac..6e423bd4e01 100644 --- a/src/keymap.h +++ b/src/keymap.h @@ -23,6 +23,7 @@ #include "lib/framework/input.h" + enum KEY_ACTION { KEYMAP_DOWN, @@ -42,44 +43,29 @@ enum KEY_STATUS struct KEY_MAPPING { void (*function)(); - bool active; KEY_STATUS status; UDWORD lastCalled; KEY_CODE metaKeyCode; KEY_CODE altMetaKeyCode; KEY_CODE subKeyCode; KEY_ACTION action; - char *pName; - KEY_MAPPING *psNext; + std::string name; }; KEY_MAPPING *keyAddMapping(KEY_STATUS status, KEY_CODE metaCode, KEY_CODE subcode, KEY_ACTION action, void (*pKeyMapFunc)(), const char *name); -bool keyRemoveMapping(KEY_CODE metaCode, KEY_CODE subCode); -KEY_MAPPING *keyGetMappingFromFunction(void *function); -bool keyRemoveMappingPt(KEY_MAPPING *psToRemove); +KEY_MAPPING *keyGetMappingFromFunction(void (*function)()); KEY_MAPPING *keyFindMapping(KEY_CODE metaCode, KEY_CODE subCode); void keyClearMappings(); void keyProcessMappings(bool bExclude); void keyInitMappings(bool bForceDefaults); -UDWORD getNumMappings(); KEY_CODE getLastSubKey(); KEY_CODE getLastMetaKey(); -KEY_MAPPING *getLastMapping(); -void keyEnableProcessing(bool val); -void keyStatusAllInactive(); -void keyAllMappingsInactive(); -void keyAllMappingsActive(); -void keySetMappingStatus(KEY_MAPPING *psMapping, bool state); void processDebugMappings(unsigned player, bool val); bool getDebugMappingStatus(); bool getWantedDebugMappingStatus(unsigned player); std::string getWantedDebugMappingStatuses(bool val); -bool keyReAssignMappingName(char *pName, KEY_CODE newMetaCode, KEY_CODE newSubCode); bool keyReAssignMapping(KEY_CODE origMetaCode, KEY_CODE origSubCode, KEY_CODE newMetaCode, KEY_CODE newSubCode); -KEY_MAPPING *getKeyMapFromName(char *pName); - -KEY_CODE getQwertyKey(); UDWORD getMarkerX(KEY_CODE code); UDWORD getMarkerY(KEY_CODE code); @@ -88,7 +74,7 @@ SDWORD getMarkerSpin(KEY_CODE code); // for keymap editor. typedef void (*_keymapsave)(); extern _keymapsave keyMapSaveTable[]; -extern KEY_MAPPING *keyMappings; +extern std::list keyMappings; void keyShowMappings();