Skip to content

Commit

Permalink
Defined behavior of !Virtual in output to always release
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jul 14, 2024
1 parent 7739e96 commit 20a91de
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/runtime/Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,12 @@ void Stage::apply_output(ConstKeySequenceRange sequence,
break;
}
}
else {
else if (event.state == KeyState::Not && is_virtual_key(event.key)) {
// !Virtual inserts a Virtual down to toggle when not already pressed
if (find_key(m_sequence, event.key) != m_sequence.end())
update_output({ event.key, KeyState::Down }, trigger, context_index);
}
else{
update_output(event, trigger, context_index);
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/test/test0_ParseKeySequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ TEST_CASE("Input Expression", "[ParseKeySequence]") {
CHECK_NOTHROW(parse_input("? A Any"));
CHECK_NOTHROW(parse_input("? A !Any B"));

// Virtual
CHECK(parse_input("Virtual0") == (KeySequence{
KeyEvent(Key::first_virtual, KeyState::Down),
KeyEvent(Key::first_virtual, KeyState::UpAsync),
}));
CHECK(parse_input("!Virtual0 A") == (KeySequence{
KeyEvent(Key::first_virtual, KeyState::Not),
KeyEvent(Key::A, KeyState::Down),
KeyEvent(Key::A, KeyState::UpAsync),
}));

// Timeout
CHECK(parse_input("A 1000ms") == (KeySequence{
KeyEvent(Key::A, KeyState::Down),
Expand Down Expand Up @@ -536,10 +547,13 @@ TEST_CASE("Output Expression", "[ParseKeySequence]") {
CHECK(parse_output("Virtual0") == (KeySequence{
KeyEvent(Key::first_virtual, KeyState::Down),
}));
CHECK_NOTHROW(parse_output("Virtual100") == (KeySequence{
KeyEvent(static_cast<Key>(*Key::first_virtual + 100), KeyState::Down),
CHECK(parse_output("!Virtual0") == (KeySequence{
KeyEvent(Key::first_virtual, KeyState::Not),
}));
CHECK_THROWS(parse_output("Virtual1000"));
CHECK_NOTHROW(parse_output("Virtual255") == (KeySequence{
KeyEvent(static_cast<Key>(*Key::first_virtual + 255), KeyState::Down),
}));
CHECK_THROWS(parse_output("Virtual256"));

// Timeout
CHECK(parse_output("A 1000ms") == (KeySequence{
Expand Down
48 changes: 48 additions & 0 deletions src/test/test3_Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,54 @@ TEST_CASE("Toggle Virtual", "[Stage]") {
REQUIRE(apply_input(stage, "+B") == "+B");
REQUIRE(apply_input(stage, "-B") == "-B");
}
//--------------------------------------------------------------------

TEST_CASE("Not Virtual", "[Stage]") {
auto config = R"(
A >> Virtual1
B >> !Virtual1
C >> !Virtual1 Virtual1
Virtual1{X} >> Y
)";
Stage stage = create_stage(config);

CHECK(apply_input(stage, "+X") == "+X");
CHECK(apply_input(stage, "-X") == "-X");
REQUIRE(stage.is_clear());

// Virtual toggles
CHECK(apply_input(stage, "+A") == "+Virtual1");
CHECK(apply_input(stage, "+Virtual1") == "");
CHECK(apply_input(stage, "-A") == "-Virtual1");
CHECK(apply_input(stage, "+Y") == "+Y");
CHECK(apply_input(stage, "-Y") == "-Y");

// !Virtual only releases
CHECK(apply_input(stage, "+B") == "+Virtual1");
CHECK(apply_input(stage, "-Virtual1") == "");
CHECK(apply_input(stage, "-B") == "-Virtual1");
CHECK(apply_input(stage, "+X") == "+X");
CHECK(apply_input(stage, "-X") == "-X");

CHECK(apply_input(stage, "+B") == "");
CHECK(apply_input(stage, "-B") == "");
CHECK(apply_input(stage, "+X") == "+X");
CHECK(apply_input(stage, "-X") == "-X");
REQUIRE(stage.is_clear());

// !Virtual Virtual ensures it is pressed
CHECK(apply_input(stage, "+C") == "+Virtual1");
CHECK(apply_input(stage, "+Virtual1") == "");
CHECK(apply_input(stage, "-C") == "-Virtual1");
CHECK(apply_input(stage, "+Y") == "+Y");
CHECK(apply_input(stage, "-Y") == "-Y");

CHECK(apply_input(stage, "+C") == "+Virtual1 +Virtual1");
CHECK(apply_input(stage, "-Virtual1 +Virtual1") == "");
CHECK(apply_input(stage, "-C") == "-Virtual1");
CHECK(apply_input(stage, "+Y") == "+Y");
CHECK(apply_input(stage, "-Y") == "-Y");
}

//--------------------------------------------------------------------

Expand Down

0 comments on commit 20a91de

Please sign in to comment.