Instead of having a primary and a temporary layer, store the state of at most 32 layers in a bitfield. These can be individually turned on and off, and key lookup starts from the top, and goes downwards until the default layer to find a non-transparent key. This allows one to reuse a partially transparent layer: set the default, and the transparent parts will be reused. The numpad layer was updated accordingly. Having an interface to the layer switching things also makes it easier to build other behaviour on top of these. As part of the rework, layer handling was moved to a separate file, and into its own, full-blown handler. Furthermore, we now use a single bit for all keymap events. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>pull/57/head
parent
cda154359d
commit
7adf80dacc
@ -1,5 +1,5 @@
|
||||
#NAME: GENERIC_FN2
|
||||
skip F1 F2 F3 F4 F5 NoKey LCtrl RCtrl NoKey F6 F7 F8 F9 Keymap0 skip
|
||||
skip F1 F2 F3 F4 F5 NoKey LCtrl RCtrl NoKey F6 F7 F8 F9 Trans skip
|
||||
Tab mouseBtnM mouseUp skip mouseWarpNW mouseWarpNE NoKey mouseBtnL mouseBtnR NoKey playPause { } [ ] sleep
|
||||
Home mouseL mouseDn mouseR mouseWarpSW mouseWarpSE nextTrack LGUI RAlt Delete LArrow DnArrow UpArrow RArrow F11 F12
|
||||
End Z X C V mouseWarpEnd skip LShift RShift skip volumeDown volumeUp BacklightDn BacklightUp \ |
|
||||
End Z X C V mouseWarpEnd Trans LShift RShift Trans volumeDown volumeUp BacklightDn BacklightUp \ |
|
||||
|
@ -1,5 +1,5 @@
|
||||
#NAME: NUMPAD
|
||||
LEDEffectNext 1 2 3 4 5 skip LCtrl RCtrl skip 6 Keypad7 Keypad8 Keypad9 KeypadMinus Keymap0
|
||||
` Q W E R T Keymap1_Momentary Backspace Space Keymap1_Momentary Y Keypad4 Keypad5 Keypad6 KeypadPlus =
|
||||
PageUp A S D F G Tab LGUI RAlt Return H Keypad1 Keypad2 Keypad3 Equals '
|
||||
PageDn Z X C V B Esc LShift RShift skip N Keypad0 KeypadDot KeypadMultiply KeypadSlash Enter
|
||||
Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Keypad7 Keypad8 Keypad9 KeypadMinus Trans
|
||||
Trans Trans Trans Trans Trans Trans Keymap1_Momentary Trans Trans Keymap1_Momentary Trans Keypad4 Keypad5 Keypad6 KeypadPlus Trans
|
||||
Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Keypad1 Keypad2 Keypad3 Equals '
|
||||
Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Trans Keypad0 KeypadDot KeypadMultiply KeypadSlash Enter
|
||||
|
@ -0,0 +1,113 @@
|
||||
#include "layers.h"
|
||||
#include "key_events.h"
|
||||
|
||||
static uint8_t DefaultLayer;
|
||||
static uint32_t LayerState;
|
||||
|
||||
static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) {
|
||||
if (keymapEntry.rawKey >= MOMENTARY_OFFSET) {
|
||||
if (key_toggled_on(keyState)) {
|
||||
if ( keymapEntry.rawKey == KEYMAP_NEXT) {
|
||||
Layer.next();
|
||||
} else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) {
|
||||
Layer.previous();
|
||||
} else {
|
||||
Layer.on(keymapEntry.rawKey - MOMENTARY_OFFSET);
|
||||
}
|
||||
}
|
||||
if (key_toggled_off(keyState)) {
|
||||
if ( keymapEntry.rawKey == KEYMAP_NEXT) {
|
||||
Layer.previous();
|
||||
} else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) {
|
||||
Layer.next();
|
||||
} else {
|
||||
Layer.off(keymapEntry.rawKey - MOMENTARY_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
// switch keymap and stay there
|
||||
} else if (key_toggled_on(keyState)) {
|
||||
Layer.on (keymapEntry.rawKey);
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
layerEventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) {
|
||||
if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP))
|
||||
return false;
|
||||
|
||||
handle_keymap_key_event(mappedKey, keyState);
|
||||
return true;
|
||||
}
|
||||
|
||||
Layer_::Layer_ (void) {
|
||||
}
|
||||
|
||||
void Layer_::begin (void) {
|
||||
defaultLayer (0);
|
||||
event_handler_hook_add (layerEventHandler);
|
||||
}
|
||||
|
||||
Key Layer_::lookup(byte row, byte col) {
|
||||
Key mappedKey;
|
||||
int8_t layer = 31;
|
||||
|
||||
mappedKey.raw = Key_Transparent.raw;
|
||||
|
||||
while (mappedKey.raw == Key_Transparent.raw &&
|
||||
layer >= DefaultLayer) {
|
||||
if (Layer.isOn (layer))
|
||||
mappedKey.raw = pgm_read_word(&(keymaps[layer][row][col]));
|
||||
layer--;
|
||||
}
|
||||
|
||||
return mappedKey;
|
||||
}
|
||||
|
||||
uint8_t Layer_::top (void) {
|
||||
for (int8_t i = 31; i >= 0; i--) {
|
||||
if (bitRead (LayerState, i))
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Layer_::move (uint8_t layer) {
|
||||
LayerState = 0;
|
||||
on (layer);
|
||||
}
|
||||
|
||||
void Layer_::on (uint8_t layer) {
|
||||
bitSet (LayerState, layer);
|
||||
}
|
||||
|
||||
void Layer_::off (uint8_t layer) {
|
||||
bitClear (LayerState, layer);
|
||||
}
|
||||
|
||||
boolean Layer_::isOn (uint8_t layer) {
|
||||
return bitRead(LayerState, layer);
|
||||
}
|
||||
|
||||
void Layer_::next (void) {
|
||||
on (top () + 1);
|
||||
}
|
||||
|
||||
void Layer_::previous (void) {
|
||||
off (top ());
|
||||
}
|
||||
|
||||
void Layer_::defaultLayer (uint8_t layer) {
|
||||
move (layer);
|
||||
DefaultLayer = layer;
|
||||
}
|
||||
|
||||
uint8_t Layer_::defaultLayer (void) {
|
||||
return DefaultLayer;
|
||||
}
|
||||
|
||||
uint32_t Layer_::getLayerState (void) {
|
||||
return LayerState;
|
||||
}
|
||||
|
||||
Layer_ Layer;
|
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "key_defs.h"
|
||||
#include "plugin.h"
|
||||
|
||||
class Layer_ : public KeyboardioPlugin {
|
||||
public:
|
||||
Layer_(void);
|
||||
|
||||
virtual void begin(void) final;
|
||||
|
||||
static Key lookup(byte row, byte col);
|
||||
static void on(uint8_t layer);
|
||||
static void off(uint8_t layer);
|
||||
static void move(uint8_t layer);
|
||||
|
||||
static uint8_t top(void);
|
||||
static void next(void);
|
||||
static void previous(void);
|
||||
|
||||
static boolean isOn(uint8_t layer);
|
||||
|
||||
static void defaultLayer(uint8_t layer);
|
||||
static uint8_t defaultLayer(void);
|
||||
|
||||
static uint32_t getLayerState(void);
|
||||
};
|
||||
|
||||
extern Layer_ Layer;
|
Loading…
Reference in new issue