keyboard_report_util.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright 2017 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "keyboard_report_util.hpp"
  17. #include <cstdint>
  18. #include <vector>
  19. #include <algorithm>
  20. using namespace testing;
  21. extern std::map<uint16_t, std::string> KEYCODE_ID_TABLE;
  22. namespace {
  23. std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
  24. std::vector<uint8_t> result;
  25. #if defined(NKRO_ENABLE)
  26. # error NKRO support not implemented yet
  27. #else
  28. for (size_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  29. if (report.keys[i]) {
  30. result.emplace_back(report.keys[i]);
  31. }
  32. }
  33. #endif
  34. std::sort(result.begin(), result.end());
  35. return result;
  36. }
  37. std::vector<uint8_t> get_mods(const report_keyboard_t& report) {
  38. std::vector<uint8_t> result;
  39. for (size_t i = 0; i < 8; i++) {
  40. if (report.mods & (1 << i)) {
  41. uint8_t code = KC_LEFT_CTRL + i;
  42. result.emplace_back(code);
  43. }
  44. }
  45. std::sort(result.begin(), result.end());
  46. return result;
  47. }
  48. } // namespace
  49. bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
  50. auto lhskeys = get_keys(lhs);
  51. auto rhskeys = get_keys(rhs);
  52. return lhs.mods == rhs.mods && lhskeys == rhskeys;
  53. }
  54. std::ostream& operator<<(std::ostream& os, const report_keyboard_t& report) {
  55. auto keys = get_keys(report);
  56. auto mods = get_mods(report);
  57. os << std::setw(10) << std::left << "report: ";
  58. if (!keys.size() && !mods.size()) {
  59. return os << "empty" << std::endl;
  60. }
  61. os << "(";
  62. for (auto key = keys.cbegin(); key != keys.cend();) {
  63. os << KEYCODE_ID_TABLE.at(*key);
  64. key++;
  65. if (key != keys.cend()) {
  66. os << ", ";
  67. }
  68. }
  69. os << ") [";
  70. for (auto mod = mods.cbegin(); mod != mods.cend();) {
  71. os << KEYCODE_ID_TABLE.at(*mod);
  72. mod++;
  73. if (mod != mods.cend()) {
  74. os << ", ";
  75. }
  76. }
  77. return os << "]" << std::endl;
  78. }
  79. KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
  80. memset(&m_report, 0, sizeof(report_keyboard_t));
  81. for (auto k : keys) {
  82. if (IS_MODIFIER_KEYCODE(k)) {
  83. m_report.mods |= MOD_BIT(k);
  84. } else {
  85. add_key_byte(&m_report, k);
  86. }
  87. }
  88. }
  89. bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const {
  90. return m_report == report;
  91. }
  92. void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const {
  93. *os << "is equal to " << m_report;
  94. }
  95. void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const {
  96. *os << "is not equal to " << m_report;
  97. }