Skip to content

Commit

Permalink
Report keycodes instead of matrix position in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlK90 committed Jul 2, 2022
1 parent c670cb1 commit fb21c93
Show file tree
Hide file tree
Showing 10 changed files with 828 additions and 19 deletions.
1 change: 1 addition & 0 deletions builddefs/build_full_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $(TEST)_SRC := \
tests/test_common/matrix.c \
tests/test_common/test_driver.cpp \
tests/test_common/keyboard_report_util.cpp \
tests/test_common/keycode_util.cpp \
tests/test_common/test_fixture.cpp \
tests/test_common/test_keymap_key.cpp \
tests/test_common/test_logger.cpp \
Expand Down
3 changes: 0 additions & 3 deletions quantum/process_keycode/process_auto_shift.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
# define AUTO_SHIFT_TIMEOUT 175
#endif

#define IS_LT(kc) ((kc) >= QK_LAYER_TAP && (kc) <= QK_LAYER_TAP_MAX)
#define IS_MT(kc) ((kc) >= QK_MOD_TAP && (kc) <= QK_MOD_TAP_MAX)
#define IS_RETRO(kc) (IS_MT(kc) || IS_LT(kc))
#define DO_GET_AUTOSHIFT_TIMEOUT(keycode, record, ...) record
// clang-format off
#define AUTO_SHIFT_ALPHA KC_A ... KC_Z
Expand Down
12 changes: 12 additions & 0 deletions quantum/quantum_keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -806,33 +806,45 @@ enum quantum_keycodes {
// keycode modeled after the old version, kept below for this.
/* #define TO(layer, when) (QK_TO | (when << 0x4) | (layer & 0xFF)) */
#define TO(layer) (QK_TO | (ON_PRESS << 0x4) | ((layer)&0xFF))
#define IS_TO(kc) ((kc) >= QK_TO && (kc) <= QK_TO_MAX)

// Momentary switch layer - 256 layer max
#define MO(layer) (QK_MOMENTARY | ((layer)&0xFF))
#define IS_MO(kc) ((kc) >= QK_MOMENTARY && (kc) <= QK_MOMENTARY_MAX)

// Set default layer - 256 layer max
#define DF(layer) (QK_DEF_LAYER | ((layer)&0xFF))
#define IS_DF(kc) ((kc) >= QK_DEF_LAYER && (kc) <= QK_DEF_LAYER_MAX)

// Toggle to layer - 256 layer max
#define TG(layer) (QK_TOGGLE_LAYER | ((layer)&0xFF))
#define IS_TG(kc) ((kc) >= QK_TOGGLE_LAYER && (kc) <= QK_TOGGLE_LAYER_MAX)

// One-shot layer - 256 layer max
#define OSL(layer) (QK_ONE_SHOT_LAYER | ((layer)&0xFF))
#define IS_OSL(kc) ((kc) >= QK_ONE_SHOT_LAYER && (kc) <= QK_ONE_SHOT_LAYER_MAX)

// L-ayer M-od: Momentary switch layer with modifiers active - 16 layer max, left mods only
#define LM(layer, mod) (QK_LAYER_MOD | (((layer)&0xF) << 4) | ((mod)&0xF))
#define IS_LM(kc) ((kc) >= QK_LAYER_MOD && (kc) <= QK_LAYER_MOD_MAX)

// One-shot mod
#define OSM(mod) (QK_ONE_SHOT_MOD | ((mod)&0xFF))
#define IS_OSM(kc) ((kc) >= QK_ONE_SHOT_MOD && (kc) <= QK_ONE_SHOT_MOD_MAX)

// Layer tap-toggle
#define TT(layer) (QK_LAYER_TAP_TOGGLE | ((layer)&0xFF))
#define IS_TT(kc) ((kc) >= QK_LAYER_TAP_TOGGLE && (kc) <= QK_LAYER_TAP_TOGGLE_MAX)

// L-ayer, T-ap - 256 keycode max, 16 layer max
#define LT(layer, kc) (QK_LAYER_TAP | (((layer)&0xF) << 8) | ((kc)&0xFF))
#define IS_LT(kc) ((kc) >= QK_LAYER_TAP && (kc) <= QK_LAYER_TAP_MAX)

// M-od, T-ap - 256 keycode max
#define MT(mod, kc) (QK_MOD_TAP | (((mod)&0x1F) << 8) | ((kc)&0xFF))
#define IS_MT(kc) ((kc) >= QK_MOD_TAP && (kc) <= QK_MOD_TAP_MAX)

#define IS_RETRO(kc) (IS_MT(kc) || IS_LT(kc))

#define LCTL_T(kc) MT(MOD_LCTL, kc)
#define RCTL_T(kc) MT(MOD_RCTL, kc)
Expand Down
37 changes: 32 additions & 5 deletions tests/test_common/keyboard_report_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
*/

#include "keyboard_report_util.hpp"
#include <cstdint>
#include <vector>
#include <algorithm>
#include <map>
#include <keycode.h>
#include <sys/types.h>
using namespace testing;

extern std::map<int, std::string> KEYCODE_IDENTIFIER;

namespace {
std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
std::vector<uint8_t> result;
Expand All @@ -36,6 +42,18 @@ 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) {
Expand All @@ -46,19 +64,28 @@ bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {

std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& report) {
auto keys = get_keys(report);

// TODO: This should probably print friendly names for the keys
stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys (";
stream << "Report send: Keys [";

for (auto key = keys.cbegin(); key != keys.cend();) {
stream << +(*key);
stream << KEYCODE_IDENTIFIER.at(*key);
key++;
if (key != keys.cend()) {
stream << ",";
}
}

return stream << ")" << std::endl;
stream << "] Mods [";

auto mods = get_mods(report);
for (auto mod = mods.cbegin(); mod != mods.cend();) {
stream << KEYCODE_IDENTIFIER.at(*mod);
mod++;
if (mod != mods.cend()) {
stream << ",";
}
}

return stream << "]" << std::endl;
}

KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
Expand Down
Loading

0 comments on commit fb21c93

Please sign in to comment.