keyboard_report_util.cpp 3.2 KB

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