Merge pull request #867 from keyboardio/layer/activation-order
Switch to activation-order for Layerspull/923/head
commit
a4e45881ab
@ -0,0 +1,104 @@
|
||||
/* -*- mode: c++ -*-
|
||||
* Kaleidoscope - Firmware for computer input devices
|
||||
* Copyright (C) 2020 Keyboard.io, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Kaleidoscope.h>
|
||||
#include <Kaleidoscope-FocusSerial.h>
|
||||
#include <Kaleidoscope-MouseKeys.h>
|
||||
|
||||
enum { PRIMARY, NUMPAD, FUNCTION }; // layers
|
||||
|
||||
// *INDENT-OFF*
|
||||
KEYMAPS(
|
||||
[PRIMARY] = KEYMAP_STACKED
|
||||
(___, Key_1, Key_2, Key_3, Key_4, Key_5, XXX,
|
||||
Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
|
||||
Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G,
|
||||
Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape,
|
||||
Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift,
|
||||
ShiftToLayer(FUNCTION),
|
||||
|
||||
XXX, Key_6, Key_7, Key_8, Key_9, Key_0, LockLayer(NUMPAD),
|
||||
Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
|
||||
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
|
||||
Key_RightAlt, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,
|
||||
Key_RightShift, Key_LeftAlt, Key_Spacebar, Key_RightControl,
|
||||
ShiftToLayer(FUNCTION)),
|
||||
|
||||
[NUMPAD] = KEYMAP_STACKED
|
||||
(___, ___, ___, ___, ___, ___, ___,
|
||||
___, ___, ___, ___, ___, ___, ___,
|
||||
___, ___, ___, ___, ___, ___,
|
||||
___, ___, ___, ___, ___, ___, ___,
|
||||
___, ___, ___, ___,
|
||||
___,
|
||||
|
||||
XXX, ___, Key_7, Key_8, Key_9, Key_KeypadSubtract, ___,
|
||||
___, ___, Key_4, Key_5, Key_6, Key_KeypadAdd, ___,
|
||||
___, Key_1, Key_2, Key_3, Key_Equals, ___,
|
||||
___, ___, Key_0, Key_Period, Key_KeypadMultiply, Key_KeypadDivide, Key_Enter,
|
||||
___, ___, ___, ___,
|
||||
___),
|
||||
|
||||
[FUNCTION] = KEYMAP_STACKED
|
||||
(ShiftToLayer(NUMPAD), Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_CapsLock,
|
||||
Key_Tab, ___, Key_mouseUp, ___, Key_mouseBtnR, Key_mouseWarpEnd, Key_mouseWarpNE,
|
||||
Key_Home, Key_mouseL, Key_mouseDn, Key_mouseR, Key_mouseBtnL, Key_mouseWarpNW,
|
||||
Key_End, Key_PrintScreen, Key_Insert, ___, Key_mouseBtnM, Key_mouseWarpSW, Key_mouseWarpSE,
|
||||
___, Key_Delete, ___, ___,
|
||||
___,
|
||||
|
||||
Consumer_ScanPreviousTrack, Key_F6, Key_F7, Key_F8, Key_F9, Key_F10, Key_F11,
|
||||
Consumer_PlaySlashPause, Consumer_ScanNextTrack, Key_LeftCurlyBracket, Key_RightCurlyBracket, Key_LeftBracket, Key_RightBracket, Key_F12,
|
||||
Key_LeftArrow, Key_DownArrow, Key_UpArrow, Key_RightArrow, ___, ___,
|
||||
Key_PcApplication, Consumer_Mute, Consumer_VolumeDecrement, Consumer_VolumeIncrement, ___, Key_Backslash, Key_Pipe,
|
||||
___, ___, Key_Enter, ___,
|
||||
___)
|
||||
)
|
||||
// *INDENT-OFF*
|
||||
|
||||
namespace kaleidoscope {
|
||||
class LayerDumper: public Plugin {
|
||||
public:
|
||||
LayerDumper() {}
|
||||
|
||||
static void dumpLayerState(uint8_t index, uint8_t layer) {
|
||||
Serial.print(index);
|
||||
Serial.print(" -> ");
|
||||
Serial.println(layer);
|
||||
}
|
||||
|
||||
EventHandlerResult onLayerChange() {
|
||||
Serial.println("Active Layers:");
|
||||
Layer.forEachActiveLayer(&dumpLayerState);
|
||||
Serial.println();
|
||||
return EventHandlerResult::OK;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
kaleidoscope::LayerDumper LayerDumper;
|
||||
|
||||
KALEIDOSCOPE_INIT_PLUGINS(Focus, LayerDumper, MouseKeys);
|
||||
|
||||
void setup() {
|
||||
Kaleidoscope.setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Kaleidoscope.loop();
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/* -*- mode: c++ -*-
|
||||
* Copyright (C) 2020 Keyboard.io, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Kaleidoscope.h"
|
||||
|
||||
#include "Kaleidoscope-Macros.h"
|
||||
|
||||
// *INDENT-OFF*
|
||||
|
||||
KEYMAPS(
|
||||
[0] = KEYMAP_STACKED
|
||||
(
|
||||
Key_0 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,M(0)
|
||||
,ShiftToLayer(1)
|
||||
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_0
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX
|
||||
,ShiftToLayer(2)
|
||||
),
|
||||
|
||||
[1] = KEYMAP_STACKED
|
||||
(
|
||||
Key_1 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX
|
||||
,___
|
||||
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_1
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX
|
||||
,___
|
||||
),
|
||||
|
||||
[2] = KEYMAP_STACKED
|
||||
(
|
||||
Key_2 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX
|
||||
,___
|
||||
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_2
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
|
||||
,XXX ,XXX ,XXX ,XXX
|
||||
,___
|
||||
)
|
||||
) // KEYMAPS(
|
||||
|
||||
// *INDENT-ON*
|
||||
|
||||
KALEIDOSCOPE_INIT_PLUGINS(Macros);
|
||||
|
||||
const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
|
||||
switch (macroIndex) {
|
||||
case 0:
|
||||
if (keyToggledOn(keyState))
|
||||
Layer.deactivate(0);
|
||||
else
|
||||
Layer.activate(0);
|
||||
break;
|
||||
}
|
||||
return MACRO_NONE;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Kaleidoscope.setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Kaleidoscope.loop();
|
||||
}
|
@ -0,0 +1,206 @@
|
||||
/* -*- mode: c++ -*-
|
||||
* Copyright (C) 2020 Keyboard.io, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "testing/setup-googletest.h"
|
||||
|
||||
SETUP_GOOGLETEST();
|
||||
|
||||
namespace kaleidoscope {
|
||||
namespace testing {
|
||||
namespace {
|
||||
|
||||
using ::testing::IsEmpty;
|
||||
|
||||
class LayerActivationOrder : public VirtualDeviceTest {
|
||||
public:
|
||||
const KeyAddr KEYSWITCH_TOP_LEFT = KeyAddr{0, 0}; // layer-dependent key
|
||||
const KeyAddr KEYSWITCH_TOP_RIGHT = KeyAddr{0, 15}; // layer-dependent key
|
||||
const KeyAddr KEYSWITCH_LEFT_PALM = KeyAddr{3, 6}; // ShiftToLayer(1)
|
||||
const KeyAddr KEYSWITCH_RIGHT_PALM = KeyAddr{3, 9}; // ShiftToLayer(2)
|
||||
const KeyAddr KEYSWITCH_LEFT_THUMB_RIGHTMOST = KeyAddr{3, 7}; // L0 deactivate macro
|
||||
|
||||
const Key LAYER0_KEY = Key_0;
|
||||
const Key LAYER1_KEY = Key_1;
|
||||
const Key LAYER2_KEY = Key_2;
|
||||
|
||||
void pressKeyswitch(const KeyAddr& addr) {
|
||||
sim_.Press(addr.row(), addr.col());
|
||||
}
|
||||
|
||||
void releaseKeyswitch(const KeyAddr& addr) {
|
||||
sim_.Release(addr.row(), addr.col());
|
||||
}
|
||||
|
||||
auto pressKeyswitchAndRunCycle(const KeyAddr& addr) {
|
||||
pressKeyswitch(addr);
|
||||
return RunCycle();
|
||||
}
|
||||
|
||||
auto releaseKeyswitchAndRunCycle(const KeyAddr& addr) {
|
||||
releaseKeyswitch(addr);
|
||||
return RunCycle();
|
||||
}
|
||||
|
||||
void assertSingleKeyboardReportContaining(std::unique_ptr<State> &state, Key k) {
|
||||
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
|
||||
EXPECT_THAT(
|
||||
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
|
||||
Contains(k));
|
||||
}
|
||||
|
||||
void assertSingleKeyboardReportNotContaining(std::unique_ptr<State> &state, Key k) {
|
||||
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
|
||||
EXPECT_THAT(
|
||||
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
|
||||
::testing::Not(Contains(k)));
|
||||
}
|
||||
|
||||
void assertSingleEmptyReport(std::unique_ptr<State> &state) {
|
||||
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
|
||||
EXPECT_THAT(
|
||||
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
|
||||
IsEmpty());
|
||||
}
|
||||
|
||||
void assertNoReport(std::unique_ptr<State> &state) {
|
||||
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 0);
|
||||
}
|
||||
|
||||
void assertNoReportAfterCycle() {
|
||||
auto state = RunCycle();
|
||||
assertNoReport(state);
|
||||
}
|
||||
|
||||
void TestPressAndRelease(const KeyAddr& addr, Key k) {
|
||||
auto state = pressKeyswitchAndRunCycle(addr);
|
||||
assertSingleKeyboardReportContaining(state, k);
|
||||
|
||||
state = releaseKeyswitchAndRunCycle(addr);
|
||||
assertSingleEmptyReport(state);
|
||||
|
||||
assertNoReportAfterCycle();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LayerActivationOrder, BaseLayerHasNotRegressed) {
|
||||
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER0_KEY);
|
||||
}
|
||||
|
||||
TEST_F(LayerActivationOrder, ShifToLayerOne) {
|
||||
// Pressing (KEYSWITCH_LEFT_PALM) shifts to Layer 1, and we stay there until release.
|
||||
auto state = pressKeyswitchAndRunCycle(KEYSWITCH_LEFT_PALM);
|
||||
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER1_KEY);
|
||||
|
||||
// Releasing (KEYSWITCH_LEFT_PALM) gets us back to the base layer
|
||||
state = releaseKeyswitchAndRunCycle(KEYSWITCH_LEFT_PALM);
|
||||
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER0_KEY);
|
||||
}
|
||||
|
||||
TEST_F(LayerActivationOrder, ShiftingWithCaching) {
|
||||
// Pressing (KEYSWITCH_TOP_LEFT) will activate the key on layer 0
|
||||
auto state = pressKeyswitchAndRunCycle(KEYSWITCH_TOP_LEFT);
|
||||
assertSingleKeyboardReportContaining(state, LAYER0_KEY);
|
||||
|
||||
// Pressing (KEYSWITCH_LEFT_PALM) will switch to Layer 1
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_LEFT_PALM);
|
||||
|
||||
// ...since we're still pressing (KEYSWITCH_TOP_LEFT), and there was no change
|
||||
// in the HID states, we shouldn't emit a report.
|
||||
assertNoReport(state);
|
||||
|
||||
// Pressing (KEYSWITCH_TOP_RIGHT), the report shall contain keys from both
|
||||
// layer 0 and layer1, because we started holding the layer 0 key prior to
|
||||
// switching layers, so it's code should remain cached.
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_TOP_RIGHT);
|
||||
assertSingleKeyboardReportContaining(state, LAYER0_KEY);
|
||||
assertSingleKeyboardReportContaining(state, LAYER1_KEY);
|
||||
|
||||
// Releasing (KEYSWITCH_TOP_LEFT), the report should now contain the key from
|
||||
// layer1 only, and should not contain the layer0 key anymore.
|
||||
state = releaseKeyswitchAndRunCycle(KEYSWITCH_TOP_LEFT);
|
||||
assertSingleKeyboardReportContaining(state, LAYER1_KEY);
|
||||
assertSingleKeyboardReportNotContaining(state, LAYER0_KEY);
|
||||
|
||||
// Release (KEYSWITCH_TOP_RIGHT)
|
||||
state = releaseKeyswitchAndRunCycle(KEYSWITCH_TOP_RIGHT);
|
||||
|
||||
// Test the layer 1 key in isolation again
|
||||
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER1_KEY);
|
||||
|
||||
// Release the layer key as well.
|
||||
state = releaseKeyswitchAndRunCycle(KEYSWITCH_LEFT_PALM);
|
||||
|
||||
// Since the layer key release is internal to us, we shouldn't send a report.
|
||||
assertNoReport(state);
|
||||
}
|
||||
|
||||
TEST_F(LayerActivationOrder, Ordering) {
|
||||
// Pressing (KEYSWITCH_RIGHT_PALM) will switch to Layer 2
|
||||
auto state = pressKeyswitchAndRunCycle(KEYSWITCH_RIGHT_PALM);
|
||||
|
||||
// Pressing (KEYSWITCH_TOP_LEFT) will activate a key on layer 2
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_TOP_LEFT);
|
||||
assertSingleKeyboardReportContaining(state, LAYER2_KEY);
|
||||
|
||||
// Pressing (KEYSWITCH_LEFT_PALM) will activate Layer 1
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_LEFT_PALM);
|
||||
|
||||
// Pressing (KEYSWITCH_TOP_RIGHT) will activate the layer 1 key now, due to
|
||||
// activation ordering.
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_TOP_RIGHT);
|
||||
|
||||
// We should have both the layer 1 and the layer 2 key active, because we're
|
||||
// holding both.
|
||||
assertSingleKeyboardReportContaining(state, LAYER1_KEY);
|
||||
assertSingleKeyboardReportContaining(state, LAYER2_KEY);
|
||||
|
||||
// Releaseing all held keys, we should get an empty report.
|
||||
releaseKeyswitch(KEYSWITCH_TOP_LEFT);
|
||||
releaseKeyswitch(KEYSWITCH_TOP_RIGHT);
|
||||
releaseKeyswitch(KEYSWITCH_LEFT_PALM);
|
||||
releaseKeyswitch(KEYSWITCH_RIGHT_PALM);
|
||||
state = RunCycle();
|
||||
|
||||
assertSingleEmptyReport(state);
|
||||
|
||||
// One more cycle, and we should generate no report at all
|
||||
state = RunCycle();
|
||||
assertNoReport(state);
|
||||
}
|
||||
|
||||
TEST_F(LayerActivationOrder, LayerZero) {
|
||||
// Pressing the rightmost of the left thumb keys should deactivate layer 0
|
||||
auto state = pressKeyswitchAndRunCycle(KEYSWITCH_LEFT_THUMB_RIGHTMOST);
|
||||
|
||||
// Pressing KEYSWITCH_TOP_LEFT should fall back to activating the key on layer 0
|
||||
state = pressKeyswitchAndRunCycle(KEYSWITCH_TOP_LEFT);
|
||||
assertSingleKeyboardReportContaining(state, LAYER0_KEY);
|
||||
|
||||
// Releasing all keys should generate a single empty report
|
||||
releaseKeyswitch(KEYSWITCH_TOP_LEFT);
|
||||
releaseKeyswitch(KEYSWITCH_LEFT_THUMB_RIGHTMOST);
|
||||
state = RunCycle();
|
||||
|
||||
assertSingleEmptyReport(state);
|
||||
|
||||
// Afterwards, we should generate no more reports.
|
||||
state = RunCycle();
|
||||
assertNoReport(state);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace kaleidoscope
|
Loading…
Reference in new issue