diff options
author | lokher <lokher@gmail.com> | 2023-04-26 16:32:15 +0800 |
---|---|---|
committer | lokher <lokher@gmail.com> | 2023-04-26 16:32:15 +0800 |
commit | e4f4ceaf3f2e3d25fb282273a81f9b58790fc427 (patch) | |
tree | c0a257eab0ffe5238fdf2c04882e8ee1fe8fc46e /tests/test_common/keyboard_report_util.cpp | |
parent | 103badc87cb50db1ff3851c84331e86ba78fb681 (diff) |
merge upstream 713427c
Diffstat (limited to 'tests/test_common/keyboard_report_util.cpp')
-rw-r--r-- | tests/test_common/keyboard_report_util.cpp | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp index 7908e64f7f..cb7f7ae735 100644 --- a/tests/test_common/keyboard_report_util.cpp +++ b/tests/test_common/keyboard_report_util.cpp @@ -15,11 +15,16 @@ */ #include "keyboard_report_util.hpp" +#include <cstdint> #include <vector> #include <algorithm> + using namespace testing; +extern std::map<uint16_t, std::string> KEYCODE_ID_TABLE; + namespace { + std::vector<uint8_t> get_keys(const report_keyboard_t& report) { std::vector<uint8_t> result; #if defined(NKRO_ENABLE) @@ -36,6 +41,19 @@ std::vector<uint8_t> get_keys(const report_keyboard_t& report) { std::sort(result.begin(), result.end()); return result; } + +std::vector<uint8_t> get_mods(const report_keyboard_t& report) { + std::vector<uint8_t> result; + for (size_t i = 0; i < 8; i++) { + if (report.mods & (1 << i)) { + uint8_t code = KC_LEFT_CTRL + i; + result.emplace_back(code); + } + } + std::sort(result.begin(), result.end()); + return result; +} + } // namespace bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { @@ -44,27 +62,42 @@ bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { return lhs.mods == rhs.mods && lhskeys == rhskeys; } -std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& report) { +std::ostream& operator<<(std::ostream& os, const report_keyboard_t& report) { auto keys = get_keys(report); + auto mods = get_mods(report); - // TODO: This should probably print friendly names for the keys - stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys ("; + os << std::setw(10) << std::left << "report: "; + + if (!keys.size() && !mods.size()) { + return os << "empty" << std::endl; + } + os << "("; for (auto key = keys.cbegin(); key != keys.cend();) { - stream << +(*key); + os << KEYCODE_ID_TABLE.at(*key); key++; if (key != keys.cend()) { - stream << ","; + os << ", "; + } + } + + os << ") ["; + + for (auto mod = mods.cbegin(); mod != mods.cend();) { + os << KEYCODE_ID_TABLE.at(*mod); + mod++; + if (mod != mods.cend()) { + os << ", "; } } - return stream << ")" << std::endl; + return os << "]" << std::endl; } KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) { memset(m_report.raw, 0, sizeof(m_report.raw)); for (auto k : keys) { - if (IS_MOD(k)) { + if (IS_MODIFIER_KEYCODE(k)) { m_report.mods |= MOD_BIT(k); } else { add_key_to_report(&m_report, k); |