MCPcopy Create free account
hub / github.com/CalcProgrammer1/OpenRGB / SwapKey

Method SwapKey

KeyboardLayoutManager/KeyboardLayoutManager.cpp:584–664  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

582}
583
584void KeyboardLayoutManager::SwapKey(keyboard_led swp_key)
585{
586 /*---------------------------------------------------------------------*\
587 | Get the swap point |
588 \*---------------------------------------------------------------------*/
589 unsigned int swp_row = swp_key.row;
590 unsigned int swp_col = swp_key.col;
591 const char* swp_name = swp_key.name;
592 unsigned int swp_value = swp_key.value;
593
594 /*---------------------------------------------------------------------*\
595 | If the keymap is empty, insert the key |
596 \*---------------------------------------------------------------------*/
597 if(keymap.size() == 0)
598 {
599 keymap.push_back(swp_key);
600 return;
601 }
602
603 /*---------------------------------------------------------------------*\
604 | Otherwise, loop through and either swap an existing entry or insert |
605 | a new entry if the given location does not already have a key present |
606 \*---------------------------------------------------------------------*/
607 for(unsigned int key_idx = 0; key_idx < keymap.size(); key_idx++)
608 {
609 /*---------------------------------------------------------------------*\
610 | If the row and column are identical, we've found the swap location |
611 \*---------------------------------------------------------------------*/
612 if((swp_row == keymap[key_idx].row) && (swp_col == keymap[key_idx].col))
613 {
614 /*---------------------------------------------------------------------*\
615 | If the key to be swapped in is an unused key, we want to remove the |
616 | entry from the keymap rather than perform a swap |
617 \*---------------------------------------------------------------------*/
618 if(strlen(swp_name) == 0)
619 {
620 keymap.erase(keymap.begin() + key_idx);
621 }
622 /*---------------------------------------------------------------------*\
623 | Otherwise, update the entry at this position with the new name and |
624 | value |
625 \*---------------------------------------------------------------------*/
626 else
627 {
628 std::string swap_name = (strlen(swp_name) == 0) ? LOG_MSG_UNUSED_KEY : swp_name;
629 LOG_DEBUG("[%s] Swapping in %s and %s out @ %02d, %02d", KLM_CLASS_NAME, swap_name.c_str(), keymap[key_idx].name, swp_row, swp_col);
630 keymap[key_idx].name = swp_name;
631 keymap[key_idx].value = swp_value;
632 }
633 break;
634 }
635
636 /*---------------------------------------------------------------------*\
637 | If the key row is greater than the swap key row OR the key row is |
638 | equal to the swap key row and the key column is greater than the swap |
639 | key column, we've gone past the swap location without a match. In |
640 | this situation, we need to insert the swap key into the empty location|
641 | without performing a shift right. |

Callers

nothing calls this directly

Calls 5

sizeMethod · 0.45
push_backMethod · 0.45
eraseMethod · 0.45
beginMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected