You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kaleidoscope/src/layers.cpp

150 lines
3.6 KiB

#include "Kaleidoscope.h"
static uint8_t DefaultLayer;
static uint32_t LayerState;
uint8_t Layer_::highestLayer;
Key Layer_::keyMap[ROWS][COLS];
8 years ago
Key(*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM;
bool Layer_::repeat_first_press = true;
static void handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) {
8 years ago
if (keymapEntry.keyCode >= MOMENTARY_OFFSET) {
uint8_t target = keymapEntry.keyCode - MOMENTARY_OFFSET;
if (keyToggledOn(keyState)) {
8 years ago
if (target == KEYMAP_NEXT) {
Layer.next();
} else if (target == KEYMAP_PREVIOUS) {
Layer.previous();
} else {
Layer.on(target);
}
}
if (keyToggledOff(keyState)) {
8 years ago
if (target == KEYMAP_NEXT) {
Layer.previous();
} else if (target == KEYMAP_PREVIOUS) {
Layer.next();
} else {
Layer.off(target);
}
/*
* When toggling a layer off, we mask all keys still held. Masked keys
* will be ignored until released and pressed again (see
* `handleKeyswitchEvent` in key_events.cpp).
*
* We do this because when holding a momentary layer switch key, then
* pressing and holding some others, they will fire as keys on the
* momentary layer. But if we release the momentary layer switch key
* before releasing the others, they will continue firing, but from
* another layer. When typing fast, it may easily happen that we end up in
* a situation where the layer key releases first (in the same scan cycle,
* but handled first), and it will emit a key from the wrong layer. So we
* ignore held keys after releasing a layer key, until they are pressed
* again, to avoid the aforementioned issue.
*/
if (!Layer.repeat_first_press)
KeyboardHardware.maskHeldKeys();
8 years ago
}
// switch keymap and stay there
} else if (keyToggledOn(keyState)) {
8 years ago
if (Layer.isOn(keymapEntry.keyCode) && keymapEntry.keyCode)
Layer.off(keymapEntry.keyCode);
else
Layer.on(keymapEntry.keyCode);
}
}
Key
Layer_::eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) {
8 years ago
if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP))
return mappedKey;
handleKeymapKeyswitchEvent(mappedKey, keyState);
8 years ago
return Key_NoKey;
}
8 years ago
Layer_::Layer_(void) {
defaultLayer(0);
}
Key
8 years ago
Layer_::getKeyFromPROGMEM(uint8_t layer, byte row, byte col) {
Key key;
8 years ago
key.raw = pgm_read_word(&(keymaps[layer][row][col]));
8 years ago
return key;
}
void
Layer_::updateKeyCache(byte row, byte col) {
int8_t layer = highestLayer;
for (layer = highestLayer; layer >= DefaultLayer; layer--) {
if (Layer.isOn(layer)) {
Key mappedKey = (*getKey)(layer, row, col);
if (mappedKey != Key_Transparent) {
keyMap[row][col] = mappedKey;
break;
8 years ago
}
}
8 years ago
}
}
8 years ago
uint8_t Layer_::top(void) {
for (int8_t i = 31; i >= 0; i--) {
if (bitRead(LayerState, i))
return i;
}
return 0;
}
8 years ago
void Layer_::move(uint8_t layer) {
LayerState = 0;
on(layer);
}
8 years ago
void Layer_::on(uint8_t layer) {
bitSet(LayerState, layer);
if (layer > highestLayer)
highestLayer = layer;
}
8 years ago
void Layer_::off(uint8_t layer) {
bitClear(LayerState, layer);
if (layer == highestLayer)
highestLayer = top();
}
8 years ago
boolean Layer_::isOn(uint8_t layer) {
return bitRead(LayerState, layer);
}
8 years ago
void Layer_::next(void) {
on(top() + 1);
}
8 years ago
void Layer_::previous(void) {
off(top());
}
8 years ago
void Layer_::defaultLayer(uint8_t layer) {
move(layer);
DefaultLayer = layer;
}
8 years ago
uint8_t Layer_::defaultLayer(void) {
return DefaultLayer;
}
8 years ago
uint32_t Layer_::getLayerState(void) {
return LayerState;
}
Layer_ Layer;