Rewrite OneShot plugin

This is a complete rewrite of OneShot, based on the keymap cache
redesign. This allows OneShot to abort the release of a key, causing
its cache entry to stay valid if it's in an active state after the key
is released, allowing us to fix #896 (double-tapping a layer shift key
doesn't make it sticky).

Instead of tracking `Key` values, OneShot now uses two bitfields of
the keyboard in order to track the OneShot state of every valid
`KeyAddr` independently. This could enable the creation of a OneShot
"meta" key, which could be used as a way to make any key on the
keyboard exhibit OneShot behaviour.

The new OneShot plugin immediately replaces the OneShot `Key` value
with its corresponding "normal" key, and activates its OneShot status
by setting one bit in one of the bitfields.

Also included:

* A rewrite of LED-ActiveModColor that makes it compatible
  with the new OneShot, and add support for Qukeys

* Updates to Escape-OneShot for compatibility and efficiency

* Minor updates to Qukeys

* The new KeyAddrBitfield class

KeyAddrBitfield:

This class can be used to represent a binary state of the physical key
addresses on the keyboard. For example, ActiveModColor can use to to
mark all the keys which should be highlighted at any given time. It
includes a very efficient iterator, which returns only `KeyAddr`
values corresponding to bits that are set in the bitfield. It checks a
whole byte at a time before examining individual bits, so if most bits
are unset most of the time, it's very fast, and suitable for use in
hooks that get called every cycle.

ActiveModColor:

This makes LED-ActiveModColor compatible with Qukeys, and removes its
16-modifier limit, while simultaneously reducing it's footprint in RAM
and eliminating a potential buffer overrun bug where it could have
written past the end of its state array.

Fixes #882
Fixes #894
Fixes #896

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/1024/head
Michael Richters 4 years ago
parent f1e727dba7
commit dc21cc2895
No known key found for this signature in database
GPG Key ID: 1288FD13E4EEF0C0

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active. * Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active.
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc * Copyright (C) 2016-2020 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU General Public License as published by the Free Software
@ -19,24 +19,31 @@
#include <Kaleidoscope-OneShot.h> #include <Kaleidoscope-OneShot.h>
#include <Kaleidoscope-Escape-OneShot.h> #include <Kaleidoscope-Escape-OneShot.h>
#include "kaleidoscope/keyswitch_state.h" #include "kaleidoscope/keyswitch_state.h"
#include "kaleidoscope/layers.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
EventHandlerResult EscapeOneShot::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState) { EventHandlerResult EscapeOneShot::onKeyswitchEvent(
if (mapped_key != Key_Escape || (keyState & INJECTED)) Key &key, KeyAddr key_addr, uint8_t key_state) {
return EventHandlerResult::OK; // We only act on an escape key that has just been pressed, and not
// generated by some other plugin. Also, only if at least one
if (!keyToggledOn(keyState)) // OneShot key is active and/or sticky. Last, only if there are no
return EventHandlerResult::OK; // OneShot keys currently being held.
if (key == Key_Escape &&
if ((!::OneShot.isActive() || ::OneShot.isPressed()) && !::OneShot.isSticky()) { keyToggledOn(key_state) &&
return EventHandlerResult::OK; !(key_state & INJECTED) &&
} ::OneShot.isActive()) {
// Cancel all OneShot keys
::OneShot.cancel(true); ::OneShot.cancel(true);
mapped_key = Key_NoKey; // Change the escape key to a blank key, and signal that event processing is
// complete.
key = Key_NoKey;
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
}
// Otherwise, do nothing
return EventHandlerResult::OK;
} }
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active. * Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active.
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc * Copyright (C) 2016-2020 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU General Public License as published by the Free Software
@ -25,8 +25,7 @@ class EscapeOneShot : public kaleidoscope::Plugin {
public: public:
EscapeOneShot(void) {} EscapeOneShot(void) {}
EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState); EventHandlerResult onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state);
}; };
} }
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-ActiveModColor -- Light up the LEDs under the active modifiers * Kaleidoscope-LED-ActiveModColor -- Light up the LEDs under the active modifiers
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc * Copyright (C) 2016-2020 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU General Public License as published by the Free Software
@ -18,34 +18,44 @@
#include <Kaleidoscope-LED-ActiveModColor.h> #include <Kaleidoscope-LED-ActiveModColor.h>
#include <Kaleidoscope-OneShot.h> #include <Kaleidoscope-OneShot.h>
#include "kaleidoscope/layers.h" #include "kaleidoscope/layers.h"
#include "kaleidoscope/keyswitch_state.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
KeyAddr ActiveModColorEffect::mod_keys_[MAX_MODS_PER_LAYER]; KeyAddrBitfield ActiveModColorEffect::mod_key_bits_;
uint8_t ActiveModColorEffect::mod_key_count_;
bool ActiveModColorEffect::highlight_normal_modifiers_ = true; bool ActiveModColorEffect::highlight_normal_modifiers_ = true;
cRGB ActiveModColorEffect::highlight_color = (cRGB) { cRGB ActiveModColorEffect::highlight_color = CRGB(160, 160, 160);
160, 160, 160 cRGB ActiveModColorEffect::oneshot_color = CRGB(160, 160, 0);
};
cRGB ActiveModColorEffect::sticky_color = CRGB(160, 0, 0); cRGB ActiveModColorEffect::sticky_color = CRGB(160, 0, 0);
EventHandlerResult ActiveModColorEffect::onLayerChange() { EventHandlerResult ActiveModColorEffect::onKeyswitchEvent(
if (!Runtime.has_leds) Key &key,
return EventHandlerResult::OK; KeyAddr key_addr,
uint8_t key_state) {
mod_key_count_ = 0;
for (auto key_addr : KeyAddr::all()) { // If `key_addr` is not a physical key address, ignore it:
Key k = Layer.lookupOnActiveLayer(key_addr); if (! key_addr.isValid()) {
return EventHandlerResult::OK;
}
if (::OneShot.isOneShotKey(k) || if (keyToggledOn(key_state)) {
(highlight_normal_modifiers_ && ( // If a key toggles on, we check its value. If it's a OneShot key,
(k >= Key_LeftControl && k <= Key_RightGui) || // it will get highlighted. Conditionally (if
(k.getFlags() == (SYNTHETIC | SWITCH_TO_KEYMAP))))) { // `highlight_normal_modifiers_` is set), we also highlight
mod_keys_[mod_key_count_++] = key_addr; // modifier and layer-shift keys.
if ((key >= Key_LeftControl && key <= Key_RightGui) ||
(key.getFlags() == (SYNTHETIC | SWITCH_TO_KEYMAP))) {
mod_key_bits_.set(key_addr);
}
} else if (keyToggledOff(key_state)) {
// Things get a bit ugly here because this plugin might come
// before OneShot in the order, so we can't just count on OneShot
// stopping the suppressed release event before we see it here.
if (mod_key_bits_.read(key_addr) && !::OneShot.isActive(key_addr)) {
mod_key_bits_.clear(key_addr);
::LEDControl.refreshAt(key_addr);
} }
} }
@ -53,36 +63,20 @@ EventHandlerResult ActiveModColorEffect::onLayerChange() {
} }
EventHandlerResult ActiveModColorEffect::beforeReportingState() { EventHandlerResult ActiveModColorEffect::beforeReportingState() {
if (mod_key_count_ == 0) {
onLayerChange();
}
for (uint8_t i = 0; i < mod_key_count_; i++) { // This loop iterates through only the `key_addr`s that have their
const KeyAddr &key_addr = mod_keys_[i]; // bits in the `mod_key_bits_` bitfield set.
for (KeyAddr key_addr : mod_key_bits_) {
Key k = Layer.lookupOnActiveLayer(key_addr); if (::OneShot.isTemporary(key_addr)) {
// Temporary OneShot keys get one color:
if (::OneShot.isOneShotKey(k)) { ::LEDControl.setCrgbAt(key_addr, oneshot_color);
if (::OneShot.isSticky(k)) } else if (::OneShot.isSticky(key_addr)) {
// Sticky OneShot keys get another color:
::LEDControl.setCrgbAt(key_addr, sticky_color); ::LEDControl.setCrgbAt(key_addr, sticky_color);
else if (::OneShot.isActive(k)) } else if (highlight_normal_modifiers_) {
::LEDControl.setCrgbAt(key_addr, highlight_color); // Normal modifiers get a third color:
else
::LEDControl.refreshAt(key_addr);
} else if (k >= Key_LeftControl && k <= Key_RightGui) {
if (kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(k))
::LEDControl.setCrgbAt(key_addr, highlight_color); ::LEDControl.setCrgbAt(key_addr, highlight_color);
else
::LEDControl.refreshAt(key_addr);
} else if (k.getFlags() == (SYNTHETIC | SWITCH_TO_KEYMAP)) {
uint8_t layer = k.getKeyCode();
if (layer >= LAYER_SHIFT_OFFSET)
layer -= LAYER_SHIFT_OFFSET;
if (Layer.isActive(layer))
::LEDControl.setCrgbAt(key_addr, highlight_color);
else
::LEDControl.refreshAt(key_addr);
} }
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-ActiveModColor -- Light up the LEDs under the active modifiers * Kaleidoscope-LED-ActiveModColor -- Light up the LEDs under the active modifiers
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc * Copyright (C) 2016-2020 Keyboard.io, Inc
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU General Public License as published by the Free Software
@ -18,6 +18,7 @@
#pragma once #pragma once
#include "kaleidoscope/Runtime.h" #include "kaleidoscope/Runtime.h"
#include "kaleidoscope/KeyAddrBitfield.h"
#include <Kaleidoscope-LEDControl.h> #include <Kaleidoscope-LEDControl.h>
#define MAX_MODS_PER_LAYER 16 #define MAX_MODS_PER_LAYER 16
@ -29,22 +30,21 @@ class ActiveModColorEffect : public kaleidoscope::Plugin {
ActiveModColorEffect(void) {} ActiveModColorEffect(void) {}
static cRGB highlight_color; static cRGB highlight_color;
static cRGB oneshot_color;
static cRGB sticky_color; static cRGB sticky_color;
static void highlightNormalModifiers(bool value) { static void highlightNormalModifiers(bool value) {
highlight_normal_modifiers_ = value; highlight_normal_modifiers_ = value;
} }
EventHandlerResult onKeyswitchEvent(Key &key,
KeyAddr key_addr,
uint8_t key_state);
EventHandlerResult beforeReportingState(); EventHandlerResult beforeReportingState();
EventHandlerResult onLayerChange();
EventHandlerResult onSetup() {
return onLayerChange();
}
private: private:
static bool highlight_normal_modifiers_; static bool highlight_normal_modifiers_;
static KeyAddr mod_keys_[MAX_MODS_PER_LAYER]; static KeyAddrBitfield mod_key_bits_;
static uint8_t mod_key_count_;
}; };
} }
} }

@ -19,274 +19,375 @@
#include <Kaleidoscope-FocusSerial.h> #include <Kaleidoscope-FocusSerial.h>
#include "kaleidoscope/keyswitch_state.h" #include "kaleidoscope/keyswitch_state.h"
#include "kaleidoscope/key_events.h" #include "kaleidoscope/key_events.h"
#include "kaleidoscope/layers.h"
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
// ---- state --------- // ----------------------------------------------------------------------------
// Configuration variables
uint16_t OneShot::start_time_ = 0;
uint16_t OneShot::time_out = 2500; uint16_t OneShot::time_out = 2500;
uint16_t OneShot::hold_time_out = 250; uint16_t OneShot::hold_time_out = 250;
int16_t OneShot::double_tap_time_out = -1; int16_t OneShot::double_tap_time_out = -1;
OneShot::key_state_t OneShot::state_[OneShot::ONESHOT_KEY_COUNT];
Key OneShot::prev_key_; // ----------------------------------------------------------------------------
bool OneShot::should_cancel_ = false; // State variables
bool OneShot::should_cancel_stickies_ = false;
uint16_t OneShot::stickable_keys_ = -1;
bool OneShot::isPressed() {
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) { KeyAddrBitfield OneShot::temp_addrs_;
if (state_[i].pressed) KeyAddrBitfield OneShot::glue_addrs_;
uint16_t OneShot::start_time_ = 0;
KeyAddr OneShot::prev_key_addr_ = OneShot::invalid_key_addr;
uint8_t OneShot::release_countdown_ = 0;
// ============================================================================
// Public interface
// ----------------------------------------------------------------------------
// Configuration functions
void OneShot::enableStickability(Key key) {
uint8_t n = getKeyIndex(key);
stickable_keys_ |= (1 << n);
}
void OneShot::disableStickability(Key key) {
uint8_t n = getKeyIndex(key);
stickable_keys_ &= ~(1 << n);
}
void OneShot::enableStickabilityForModifiers() {
stickable_keys_ |= stickable_modifiers_mask;
}
void OneShot::disableStickabilityForModifiers() {
stickable_keys_ &= ~stickable_modifiers_mask;
}
void OneShot::enableStickabilityForLayers() {
stickable_keys_ |= stickable_layers_mask;
}
void OneShot::disableStickabilityForLayers() {
stickable_keys_ &= ~stickable_layers_mask;
}
// ----------------------------------------------------------------------------
// Global tests for any OneShot key
bool OneShot::isActive() {
for (KeyAddr key_addr __attribute__((unused)) : temp_addrs_) {
return true;
}
for (KeyAddr key_addr __attribute__((unused)) : glue_addrs_) {
return true; return true;
} }
return false; return false;
} }
bool OneShot::isSticky() { bool OneShot::isSticky() {
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) { for (KeyAddr key_addr : glue_addrs_) {
if (state_[i].sticky) if (! temp_addrs_.read(key_addr)) {
return true; return true;
} }
}
return false; return false;
} }
bool OneShot::isStickable(Key key) { // ----------------------------------------------------------------------------
return state_[key.getRaw() - ranges::OS_FIRST].stickable; // Key-specific OneShot key tests
}
// ---- OneShot stuff ---- // These functions are particularly useful for ActiveModColor, which
void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) { // could potentially use three different color values for the three
Key key; // states (sticky | active && !sticky | pressed && !active).
if (idx < 8) { bool OneShot::isModifier(Key key) {
key = Key(Key_LeftControl.getKeyCode() + idx, // Returns `true` if `key` is a modifier key, including modifiers
Key_LeftControl.getFlags()); // with extra mod flags applied (e.g. `Key_Meh`).
} else { if ((key.getFlags() & (SYNTHETIC | RESERVED)) != 0) {
key = Key(LAYER_SHIFT_OFFSET + idx - 8, return false;
KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP);
} }
return (key.getKeyCode() >= Key_LeftControl.getKeyCode() &&
handleKeyswitchEvent(key, UnknownKeyswitchLocation, key_state | INJECTED); key.getKeyCode() <= Key_RightGui.getKeyCode());
} }
void OneShot::activateOneShot(uint8_t idx) { bool OneShot::isLayerShift(Key key) {
injectNormalKey(idx, IS_PRESSED); // Returns `true` if `key` is a layer-shift key.
return (key.getFlags() == (SYNTHETIC | SWITCH_TO_KEYMAP) &&
key.getKeyCode() >= LAYER_SHIFT_OFFSET &&
key.getKeyCode() < LAYER_MOVE_OFFSET);
} }
void OneShot::cancelOneShot(uint8_t idx) { bool OneShot::isStickable(Key key) {
state_[idx].active = false; int8_t n;
injectNormalKey(idx, WAS_PRESSED); if (isModifier(key)) {
n = key.getKeyCode() - Key_LeftControl.getKeyCode();
return bitRead(stickable_keys_, n);
} else if (isLayerShift(key)) {
n = oneshot_mod_count + key.getKeyCode() - LAYER_SHIFT_OFFSET;
if (n < oneshot_key_count) {
return bitRead(stickable_keys_, n);
}
}
return false;
} }
EventHandlerResult OneShot::onNameQuery() { bool OneShot::isTemporary(KeyAddr key_addr) {
return ::Focus.sendName(F("OneShot")); return temp_addrs_.read(key_addr);
} }
EventHandlerResult OneShot::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState) { bool OneShot::isSticky(KeyAddr key_addr) {
uint8_t idx = mapped_key.getRaw() - ranges::OS_FIRST; return (glue_addrs_.read(key_addr) && !temp_addrs_.read(key_addr));
}
if (keyState & INJECTED)
return EventHandlerResult::OK;
if (!isActive()) {
if (!isOneShotKey_(mapped_key)) {
return EventHandlerResult::OK;
}
if (keyToggledOff(keyState)) { bool OneShot::isActive(KeyAddr key_addr) {
state_[idx].pressed = false; return (isTemporary(key_addr) || glue_addrs_.read(key_addr));
} else if (keyToggledOn(keyState)) { }
start_time_ = Runtime.millisAtCycleStart();
state_[idx].position = key_addr.toInt();
state_[idx].pressed = true;
state_[idx].active = true;
prev_key_ = mapped_key;
activateOneShot(idx); // ----------------------------------------------------------------------------
} // Other functions
return EventHandlerResult::EVENT_CONSUMED; // Cancel all active OneShot keys (if `cancel_stickies` is true) or
// just non-sticky active OneShot keys. This function is called by
// Escape-OneShot to release active OneShot keys.
void OneShot::cancel(bool cancel_stickies) {
if (cancel_stickies) {
for (KeyAddr key_addr : glue_addrs_) {
releaseKey(key_addr);
} }
if (isOneShotKey_(mapped_key)) {
if (state_[idx].sticky) {
if (keyToggledOn(keyState)) { // maybe on _off instead?
prev_key_ = mapped_key;
state_[idx].sticky = false;
cancelOneShot(idx);
should_cancel_ = false;
} }
for (KeyAddr key_addr : temp_addrs_) {
if (glue_addrs_.read(key_addr)) {
releaseKey(key_addr);
} else { } else {
if (keyToggledOff(keyState)) { temp_addrs_.clear(key_addr);
state_[idx].pressed = false;
if (Runtime.hasTimeExpired(start_time_, hold_time_out)) {
cancelOneShot(idx);
should_cancel_ = false;
} }
} }
}
if (keyToggledOn(keyState)) { // ----------------------------------------------------------------------------
state_[idx].pressed = true; // Plugin hook functions
if (prev_key_ == mapped_key && isStickable(mapped_key)) { EventHandlerResult OneShot::onNameQuery() {
uint16_t dtto = (double_tap_time_out == -1) ? time_out : double_tap_time_out; return ::Focus.sendName(F("OneShot"));
if (!Runtime.hasTimeExpired(start_time_, dtto)) { }
state_[idx].sticky = true;
prev_key_ = mapped_key; EventHandlerResult OneShot::onKeyswitchEvent(
} Key &key, KeyAddr key_addr, uint8_t key_state) {
} else {
start_time_ = Runtime.millisAtCycleStart();
state_[idx].position = key_addr.toInt(); // Ignore injected key events. This prevents re-processing events
state_[idx].active = true; // that the hook functions generate (by calling `injectNormalKey()`
prev_key_ = mapped_key; // via one of the `*OneShot()` functions). There are more robust
// ways to do this, but since OneShot is intended to react to only
// physical keypresses, this is adequate.
if (key_state & INJECTED)
return EventHandlerResult::OK;
activateOneShot(idx); bool temp = temp_addrs_.read(key_addr);
bool glue = glue_addrs_.read(key_addr);
if (keyToggledOn(key_state)) {
if (!temp && !glue) {
// This key_addr is not in a OneShot state.
if (isOneShotKey(key)) {
// Replace the OneShot key with its corresponding normal key.
pressKey(key_addr, key);
return EventHandlerResult::ABORT;
} else if (!isModifier(key) && !isLayerShift(key)) {
// Only trigger release of temporary OneShot keys if the
// pressed key is neither a modifier nor a layer shift.
release_countdown_ = (1 << 1);
} }
// return EventHandlerResult::OK;
} else if (temp && glue) {
// This key_addr is in the temporary OneShot state.
if (key_addr == prev_key_addr_) {
// The same OneShot key has been pressed twice in a row. It
// will either become sticky (if it has been double-tapped),
// or it will be become a normal key. Either way, its `temp`
// state will be cleared.
temp_addrs_.clear(key_addr);
// Derive the true double-tap timeout value if we're using the default.
uint16_t dtto = (double_tap_time_out < 0) ? time_out : double_tap_time_out;
// If the key is not stickable, or the double-tap timeout has
// expired, clear the `glue` state, as well; this OneShot key
// has been cancelled, and will become a normal key.
if (!isStickable(key) || hasTimedOut(dtto)) {
glue_addrs_.clear(key_addr);
} else {
return EventHandlerResult::ABORT;
} }
} else {
// This is a temporary OneShot key, but has not been pressed
// twice in a row, so we need to clear its state.
temp_addrs_.clear(key_addr);
glue_addrs_.clear(key_addr);
} }
return EventHandlerResult::EVENT_CONSUMED; } else if (!temp && glue) {
// This is a sticky OneShot key that has been pressed. Clear
// state now, so it will become a normal key.
glue_addrs_.clear(key_addr);
} else { // (temp && !glue)
// A key has been pressed that is in the "pending" OneShot
// state. Since this key should have entered the "temporary"
// OneShot state as soon as it was released (from its first
// press), it should only be possible to release a key that's in
// this state.
}
// Always record the address of a keypress. It might be useful for
// other plugins, so this could perhaps be tracked in the
// Kaleidoscope core.
prev_key_addr_ = key_addr;
} else if (keyToggledOff(key_state)) {
if (temp || glue) {
// Any key in the "pending" OneShot state needs its `glue` state
// bit set to make it "temporary". If it's in the "sticky"
// OneShot state, this is redundant, but we're trading time
// efficiency to get smaller binary size.
glue_addrs_.set(key_addr);
// This is an active OneShot key that has just been released. We
// need to stop that event from sending a report, and instead
// send a "hold" event. This is handled in the
// `beforeReportingState()` hook below.
//Layer.updateLiveCompositeKeymap(key_addr, key);
return EventHandlerResult::ABORT;
} }
// ordinary key here, with some event } else {
// This key is being held.
if (temp && !glue) {
// This key is in the "pending" OneShot state. We need to check
// its hold timeout, and turn it back into a normal key if it
// has timed out.
if (hasTimedOut(hold_time_out)) {
temp_addrs_.clear(key_addr);
}
}
if (keyIsPressed(keyState)) { if (isOneShotKey(key)) {
prev_key_ = mapped_key; // whoops! someone cancelled a oneshot key while it was being
if (!(mapped_key >= Key_LeftControl && mapped_key <= Key_RightGui) && // held; reactivate it, but set it as a normal modifier
!(mapped_key.getFlags() == (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP))) { // instead. Or better yet, mask it, in case of a layer change.
should_cancel_ = true;
} }
} }
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
// For any active OneShot modifier keys, keep those modifiers active
// in the keyboard HID report.
EventHandlerResult OneShot::beforeReportingState() { EventHandlerResult OneShot::beforeReportingState() {
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT / 2; i++) { for (KeyAddr key_addr : glue_addrs_) {
if (state_[i].active) { holdKey(key_addr);
activateOneShot(i);
}
} }
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
EventHandlerResult OneShot::afterEachCycle() { EventHandlerResult OneShot::afterEachCycle() {
bool oneshot_active = false; // If a normal, non-modifier key has been pressed, or if active,
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) { // non-sticky OneShot keys have timed out, this is where they get
if (state_[i].active) { // released. Release is triggered when `release_countdown_` gets to
oneshot_active = true; // 1, not 0, because most of the time it will be 0 (see below). It
break; // gets set to 2 on the press of a normal key when there are any
} // active OneShot keys; that way, the OneShot keys will stay active
} // long enough to apply to the newly-pressed key.
if (oneshot_active && hasTimedOut()) if ((release_countdown_ == 1) || hasTimedOut(time_out)) {
cancel(); for (KeyAddr key_addr : temp_addrs_) {
if (glue_addrs_.read(key_addr)) {
bool is_cancelled = false; releaseKey(key_addr);
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) {
if (should_cancel_) {
if (state_[i].sticky) {
if (should_cancel_stickies_) {
is_cancelled = true;
state_[i].sticky = false;
cancelOneShot(i);
state_[i].pressed = false;
} }
} else if (state_[i].active && !state_[i].pressed) { temp_addrs_.clear(key_addr);
is_cancelled = true;
cancelOneShot(i);
} }
} }
} // Also, advance the counter for OneShot keys that have been
// cancelled by the press of a non-OneShot, non-modifier key. An
if (is_cancelled) { // unconditional bit shift should be more efficient than checking
should_cancel_ = false; // for zero to avoid underflow.
should_cancel_stickies_ = false; release_countdown_ >>= 1;
}
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
void OneShot::inject(Key mapped_key, uint8_t key_state) { // ============================================================================
onKeyswitchEvent(mapped_key, UnknownKeyswitchLocation, key_state); // Private functions, not exposed to other plugins
}
// --- glue code ---
bool OneShot::isActive(void) {
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) {
if ((state_[i].active && !hasTimedOut()) ||
state_[i].pressed ||
state_[i].sticky)
return true;
}
return false;
}
bool OneShot::isActive(Key key) { // ----------------------------------------------------------------------------
uint8_t idx = key.getRaw() - ranges::OS_FIRST; // Helper functions for acting on OneShot key events
return (state_[idx].active && !hasTimedOut()) || uint8_t OneShot::getOneShotKeyIndex(Key oneshot_key) {
state_[idx].pressed || // The calling function is responsible for verifying that
state_[idx].sticky; // `oneshot_key` is an actual OneShot key (i.e. call
// `isOneShotKey(oneshot_key)` first).
uint8_t index = oneshot_key.getRaw() - ranges::OS_FIRST;
return index;
} }
bool OneShot::isSticky(Key key) { uint8_t OneShot::getKeyIndex(Key key) {
uint8_t idx = key.getRaw() - ranges::OS_FIRST; // Default to returning a value that's out of range. This should be
// harmless because we only use the returned index to reference a
return state_[idx].sticky; // bit in a bitfield, not as a memory address.
} uint8_t n{oneshot_key_count};
bool OneShot::isModifierActive(Key key) { if (isOneShotKey(key)) {
if (key < Key_LeftControl || key > Key_RightGui) n = getOneShotKeyIndex(key);
return false; } else if (isModifier(key)) {
n = key.getKeyCode() - Key_LeftControl.getKeyCode();
uint8_t idx = key.getKeyCode() - Key_LeftControl.getKeyCode(); } else if (isLayerShift(key)) {
return state_[idx].active; n = oneshot_mod_count + key.getKeyCode() - LAYER_SHIFT_OFFSET;
}
void OneShot::cancel(bool with_stickies) {
should_cancel_ = true;
should_cancel_stickies_ = with_stickies;
}
void OneShot::enableStickability(Key key) {
if (key >= ranges::OS_FIRST && key <= ranges::OS_LAST)
state_[key.getRaw() - ranges::OS_FIRST].stickable = true;
}
void OneShot::disableStickability(Key key) {
if (key >= ranges::OS_FIRST && key <= ranges::OS_LAST)
state_[key.getRaw() - ranges::OS_FIRST].stickable = false;
}
void OneShot::enableStickabilityForModifiers() {
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT / 2; i++) {
state_[i].stickable = true;
} }
return n;
} }
void OneShot::enableStickabilityForLayers() { Key OneShot::decodeOneShotKey(Key oneshot_key) {
for (uint8_t i = ONESHOT_KEY_COUNT / 2; i < ONESHOT_KEY_COUNT; i++) { // The calling function is responsible for verifying that
state_[i].stickable = true; // `oneshot_key` is an actual OneShot key (i.e. call
// `isOneShotKey(oneshot_key)` first).
uint8_t n = getOneShotKeyIndex(oneshot_key);
if (n < oneshot_mod_count) {
return Key(Key_LeftControl.getKeyCode() + n,
Key_LeftControl.getFlags());
} else {
return Key(LAYER_SHIFT_OFFSET + n - oneshot_mod_count,
KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP);
} }
} }
void OneShot::disableStickabilityForModifiers() { // ------------------------------------------------------------------------------
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT / 2; i++) { // Helper functions for sending key events for keys in OneShot states
state_[i].stickable = false;
}
}
void OneShot::disableStickabilityForLayers() { void OneShot::pressKey(KeyAddr key_addr, Key oneshot_key) {
for (uint8_t i = ONESHOT_KEY_COUNT / 2; i < ONESHOT_KEY_COUNT; i++) { Key key = decodeOneShotKey(oneshot_key);
state_[i].stickable = false; prev_key_addr_ = key_addr;
} start_time_ = Runtime.millisAtCycleStart();
temp_addrs_.set(key_addr);
handleKeyswitchEvent(key, key_addr, IS_PRESSED | INJECTED);
} }
void OneShot::holdKey(KeyAddr key_addr) {
handleKeyswitchEvent(Key_NoKey, key_addr, WAS_PRESSED | IS_PRESSED | INJECTED);
} }
void OneShot::releaseKey(KeyAddr key_addr) {
glue_addrs_.clear(key_addr);
temp_addrs_.clear(key_addr);
handleKeyswitchEvent(Key_NoKey, key_addr, WAS_PRESSED | INJECTED);
} }
} // namespace plugin
} // namespace kaleidoscope
kaleidoscope::plugin::OneShot OneShot; kaleidoscope::plugin::OneShot OneShot;

@ -19,6 +19,11 @@
#include "kaleidoscope/Runtime.h" #include "kaleidoscope/Runtime.h"
#include <Kaleidoscope-Ranges.h> #include <Kaleidoscope-Ranges.h>
#include "kaleidoscope/key_events.h"
#include "kaleidoscope/KeyAddrBitfield.h"
// ----------------------------------------------------------------------------
// Keymap macros
#define OSM(kc) Key(kaleidoscope::ranges::OSM_FIRST + (Key_ ## kc).getKeyCode() - Key_LeftControl.getKeyCode()) #define OSM(kc) Key(kaleidoscope::ranges::OSM_FIRST + (Key_ ## kc).getKeyCode() - Key_LeftControl.getKeyCode())
#define OSL(n) Key(kaleidoscope::ranges::OSL_FIRST + n) #define OSL(n) Key(kaleidoscope::ranges::OSL_FIRST + n)
@ -28,25 +33,11 @@ namespace plugin {
class OneShot : public kaleidoscope::Plugin { class OneShot : public kaleidoscope::Plugin {
public: public:
OneShot(void) { // Constructor
for (uint8_t i = 0; i < ONESHOT_KEY_COUNT; i++) { OneShot() {}
state_[i].stickable = true;
}
}
static bool isOneShotKey(Key key) { // --------------------------------------------------------------------------
return (key.getRaw() >= kaleidoscope::ranges::OS_FIRST && key.getRaw() <= kaleidoscope::ranges::OS_LAST); // Configuration functions
}
static bool isActive(void);
static bool isActive(Key key);
static bool isPressed();
static bool isSticky();
static bool isSticky(Key key);
static void cancel(bool with_stickies = false);
static uint16_t time_out;
static int16_t double_tap_time_out;
static uint16_t hold_time_out;
static inline void enableStickablity() {} static inline void enableStickablity() {}
static void enableStickability(Key key); static void enableStickability(Key key);
@ -68,46 +59,86 @@ class OneShot : public kaleidoscope::Plugin {
static void disableStickabilityForModifiers(); static void disableStickabilityForModifiers();
static void disableStickabilityForLayers(); static void disableStickabilityForLayers();
static bool isStickable(Key key); // --------------------------------------------------------------------------
// Global test functions
static bool isActive();
static bool isSticky();
// --------------------------------------------------------------------------
// Single-key test functions
static bool isOneShotKey(Key key) {
return (key.getRaw() >= kaleidoscope::ranges::OS_FIRST &&
key.getRaw() <= kaleidoscope::ranges::OS_LAST);
}
static bool isStickable(Key key); // inline?
static bool isModifierActive(Key key); static bool isTemporary(KeyAddr key_addr); // inline?
static bool isSticky(KeyAddr key_addr); // inline?
static bool isActive(KeyAddr key_addr); // inline?
// --------------------------------------------------------------------------
// Utility function for other plugins to cancel OneShot keys
static void cancel(bool with_stickies = false);
// --------------------------------------------------------------------------
// Vestigial functions?
void inject(Key key, uint8_t key_state) {}
static bool isModifierActive(Key key) {
return false;
}
// --------------------------------------------------------------------------
// Configuration variables (should probably be private)
static uint16_t time_out;
static uint16_t hold_time_out;
static int16_t double_tap_time_out;
// --------------------------------------------------------------------------
// Plugin hook functions
EventHandlerResult onNameQuery(); EventHandlerResult onNameQuery();
EventHandlerResult onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state);
EventHandlerResult beforeReportingState(); EventHandlerResult beforeReportingState();
EventHandlerResult afterEachCycle(); EventHandlerResult afterEachCycle();
EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState);
void inject(Key mapped_key, uint8_t key_state);
private: private:
static constexpr uint8_t ONESHOT_KEY_COUNT = 16;
typedef struct {
bool active: 1;
bool pressed: 1;
bool stickable: 1;
bool sticky: 1;
uint8_t __reserved: 4;
uint8_t position;
} key_state_t;
static key_state_t state_[ONESHOT_KEY_COUNT];
static uint16_t start_time_; // --------------------------------------------------------------------------
static Key prev_key_; // Constants
static bool should_cancel_; static constexpr uint8_t oneshot_key_count = 16;
static bool should_cancel_stickies_; static constexpr uint8_t oneshot_mod_count = 8;
static constexpr uint8_t oneshot_layer_count = oneshot_key_count - oneshot_mod_count;
static constexpr uint16_t stickable_modifiers_mask = uint16_t(uint16_t(-1) >> oneshot_layer_count);
static constexpr uint16_t stickable_layers_mask = uint16_t(uint16_t(-1) << oneshot_mod_count);
static constexpr KeyAddr invalid_key_addr = KeyAddr(KeyAddr::invalid_state);
static void injectNormalKey(uint8_t idx, uint8_t key_state); // --------------------------------------------------------------------------
static void activateOneShot(uint8_t idx); // State variables
static void cancelOneShot(uint8_t idx); static uint16_t stickable_keys_;
static bool isOneShotKey_(Key key) { static KeyAddrBitfield temp_addrs_;
return key.getRaw() >= ranges::OS_FIRST && key.getRaw() <= ranges::OS_LAST; static KeyAddrBitfield glue_addrs_;
}
static bool hasTimedOut() { static uint16_t start_time_;
return Runtime.hasTimeExpired(start_time_, time_out); static KeyAddr prev_key_addr_;
static uint8_t release_countdown_;
// --------------------------------------------------------------------------
// Internal utility functions
static bool hasTimedOut(uint16_t ttl) {
return Runtime.hasTimeExpired(start_time_, ttl);
} }
static uint8_t getOneShotKeyIndex(Key oneshot_key);
static uint8_t getKeyIndex(Key key);
static Key decodeOneShotKey(Key oneshot_key);
static void pressKey(KeyAddr key_addr, Key oneshot_key);
static void holdKey(KeyAddr key_addr);
static void releaseKey(KeyAddr key_addr);
}; };
}
} } // namespace plugin
} // namespace kaleidoscope
extern kaleidoscope::plugin::OneShot OneShot; extern kaleidoscope::plugin::OneShot OneShot;

@ -327,11 +327,10 @@ void Qukeys::flushEvent(Key event_key) {
// that qukey's primary and alternate `Key` values for use later. We do this // that qukey's primary and alternate `Key` values for use later. We do this
// because it's much more efficient than doing that as a separate step. // because it's much more efficient than doing that as a separate step.
bool Qukeys::isQukey(KeyAddr k) { bool Qukeys::isQukey(KeyAddr k) {
// First, look up the value from the keymap. We need to do a full lookup, not // First, look up the value from the keymap. This value should be
// just looking up the cached value (i.e. `Layer.lookup(k)`), because the // correct in the cache, even if there's been a layer change since
// cached value will be out of date if a layer change happened since the // the key was pressed.
// keyswitch toggled on. Key key = Layer.lookup(k);
Key key = Layer.lookupOnActiveLayer(k);
// Next, we check to see if this is a DualUse-type qukey (defined in the keymap) // Next, we check to see if this is a DualUse-type qukey (defined in the keymap)
if (isDualUseKey(key)) { if (isDualUseKey(key)) {

@ -0,0 +1,184 @@
/* 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/>.
*/
#pragma once
#include <Arduino.h>
#include "kaleidoscope/KeyAddr.h"
namespace kaleidoscope {
// Return the number of `UnitType` units required to store `n` bits. Both `UnitType` &
// `WidthType` should be integer types. `WidthType` is whatever type the parameter `n` is
// stored as, and can be deduced by the compiler, so it's not necessary to declare it
// when calling this function (e.g. `bitfieldSize<uint16_t>(n)`). The default `UnitType`
// is `byte` (i.e. `uint8_t`, which is almost always what we want, so most of the time we
// can also drop that template parameter (e.g. `bitfieldSize(n)`).
template <typename _UnitType = byte, typename _WidthType>
constexpr _WidthType bitfieldSize(_WidthType n) {
return ((n - 1) / (8 * sizeof(_UnitType))) + 1;
}
// ================================================================================
// Generic Bitfield class, useful for defining KeyAddrBitfield, and others.
class KeyAddrBitfield {
public:
static constexpr uint8_t size = KeyAddr::upper_limit;
static constexpr uint8_t block_size = 8 * sizeof(uint8_t);
static constexpr uint8_t total_blocks = bitfieldSize<uint8_t>(size);
static constexpr uint8_t blockIndex(KeyAddr k) {
return k.toInt() / block_size;
}
static constexpr uint8_t bitIndex(KeyAddr k) {
return k.toInt() % block_size;
}
static constexpr KeyAddr index(uint8_t block_index, uint8_t bit_index) {
uint8_t offset = (block_index * block_size) + bit_index;
return KeyAddr(offset);
}
bool read(KeyAddr k) const {
// assert(k.toInt() < size);
return bitRead(data_[blockIndex(k)], bitIndex(k));
}
void set(KeyAddr k) {
// assert(k.toInt() < size);
bitSet(data_[blockIndex(k)], bitIndex(k));
}
void clear(KeyAddr k) {
// assert(k.toInt() < size);
bitClear(data_[blockIndex(k)], bitIndex(k));
}
void write(KeyAddr k, bool value) {
// assert(k.toInt() < size);
bitWrite(data_[blockIndex(k)], bitIndex(k), value);
}
// This function returns the number of set bits in the bitfield up to and
// including the bit at index `k`. Two important things to note: it doesn't
// verify that the bit for index `k` is set (the caller must do so first,
// using `read()`), and what is returned is 1-indexed, so the caller will need
// to subtract 1 before using it as an array index (e.g. when doing a `Key`
// lookup for a sparse keymap layer).
uint8_t ordinal(KeyAddr k) const {
// assert(k.toInt() < size);
uint8_t block_index = blockIndex(k);
uint8_t count{0};
for (uint8_t b{0}; b < block_index; ++b) {
count += __builtin_popcount(data_[b]);
}
uint8_t last_data_unit = data_[block_index];
last_data_unit &= ~(0xFF << bitIndex(k));
count += __builtin_popcount(last_data_unit);
return count;
}
uint8_t &block(uint8_t block_index) {
// assert(block_index < total_blocks);
return data_[block_index];
}
private:
uint8_t data_[total_blocks] = {};
// ----------------------------------------------------------------------------
// Iterator!
public:
class Iterator;
friend class KeyAddrBitfield::Iterator;
Iterator begin() {
return Iterator{*this, 0};
}
Iterator end() {
return Iterator{*this, total_blocks};
}
class Iterator {
public:
Iterator(KeyAddrBitfield &bitfield, uint8_t x)
: bitfield_(bitfield), block_index_(x) {}
bool operator!=(const Iterator &other) {
// First, the test for the end condition (return false when all the blocks have been
// tested):
while (block_index_ < other.block_index_) {
// Get the data for the block at `block_index_` from the bitfield, then shift it
// by the number of bits we've already checked (`bit_index_`):
block_ = bitfield_.data_[block_index_];
block_ >>= bit_index_;
// Now we iterate through that block until we either find a bit that is set, or we
// find that there are no more bits set. If (as expected most of the time) no bits
// are set, we do nothing:
while (block_ != 0) {
// If the low (remaining) bit is set, generate an `KeyAddr` object from the
// bitfield coordinates and store it for the dereference operator to return:
if (block_ & 1) {
index_ = KeyAddrBitfield::index(block_index_, bit_index_);
return true;
}
// The low bit wasn't set, so we shift the data block by one and track that
// shift with the bit coordinate (`bit_index_`):
block_ >>= 1;
bit_index_ += 1;
}
// When we're done checking a block, move on to the next one:
block_index_ += 1;
bit_index_ = 0;
}
return false;
}
KeyAddr operator*() {
// assert(index_ < size);
return index_;
}
void operator++() {
++bit_index_;
}
private:
KeyAddrBitfield &bitfield_;
uint8_t block_index_; // index of the block
uint8_t bit_index_{0}; // bit index in the block
uint8_t block_;
KeyAddr index_;
}; // class Iterator {
} __attribute__((packed)); // class KeyAddrBitfield {
} // namespace kaleidoscope {
// ================================================================================
// How to use the iterator above:
#if 0
// To use the KeyAddrBitfield::Iterator, write a loop like the following:
KeyAddrBitfield bitfield;
for (KeyAddr k : bitfield) {
// Here, you'll get a `KeyAddr` object for each bit that is set in `bitfield`.
}
#endif
Loading…
Cancel
Save