keyboard_report_util.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #elif defined(RING_BUFFERED_6KRO_REPORT_ENABLE)
  28. # error 6KRO support not implemented yet
  29. #else
  30. for (size_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  31. if (report.keys[i]) {
  32. result.emplace_back(report.keys[i]);
  33. }
  34. }
  35. #endif
  36. std::sort(result.begin(), result.end());
  37. return result;
  38. }
  39. std::vector<uint8_t> get_mods(const report_keyboard_t& report) {
  40. std::vector<uint8_t> result;
  41. for (size_t i = 0; i < 8; i++) {
  42. if (report.mods & (1 << i)) {
  43. uint8_t code = KC_LEFT_CTRL + i;
  44. result.emplace_back(code);
  45. }
  46. }
  47. std::sort(result.begin(), result.end());
  48. return result;
  49. }
  50. } // namespace
  51. bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
  52. auto lhskeys = get_keys(lhs);
  53. auto rhskeys = get_keys(rhs);
  54. return lhs.mods == rhs.mods && lhskeys == rhskeys;
  55. }
  56. std::ostream& operator<<(std::ostream& os, const report_keyboard_t& report) {
  57. auto keys = get_keys(report);
  58. auto mods = get_mods(report);
  59. os << std::setw(10) << std::left << "report: ";
  60. if (!keys.size() && !mods.size()) {
  61. return os << "empty" << std::endl;
  62. }
  63. os << "(";
  64. for (auto key = keys.cbegin(); key != keys.cend();) {
  65. os << KEYCODE_ID_TABLE.at(*key);
  66. key++;
  67. if (key != keys.cend()) {
  68. os << ", ";
  69. }
  70. }
  71. os << ") [";
  72. for (auto mod = mods.cbegin(); mod != mods.cend();) {
  73. os << KEYCODE_ID_TABLE.at(*mod);
  74. mod++;
  75. if (mod != mods.cend()) {
  76. os << ", ";
  77. }
  78. }
  79. return os << "]" << std::endl;
  80. }
  81. KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
  82. memset(&m_report, 0, sizeof(report_keyboard_t));
  83. for (auto k : keys) {
  84. if (IS_MODIFIER_KEYCODE(k)) {
  85. m_report.mods |= MOD_BIT(k);
  86. } else {
  87. add_key_byte(&m_report, k);
  88. }
  89. }
  90. }
  91. bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const {
  92. return m_report == report;
  93. }
  94. void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const {
  95. *os << "is equal to " << m_report;
  96. }
  97. void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const {
  98. *os << "is not equal to " << m_report;
  99. }