logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keyboard_report_util.cpp (3304B)


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